blob: e3764ece0cd2e850a33e6a4aa2224bd7e00a9b7a [file] [log] [blame]
Vikram S. Adve243dd452001-09-18 13:03:13 +00001// $Id$
Chris Lattner20b1ea02001-09-14 03:47:57 +00002//***************************************************************************
3// File:
4// SparcInstrSelection.cpp
5//
6// Purpose:
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00007// BURS instruction selection for SPARC V9 architecture.
Chris Lattner20b1ea02001-09-14 03:47:57 +00008//
9// History:
10// 7/02/01 - Vikram Adve - Created
11//**************************************************************************/
12
13#include "SparcInternals.h"
Vikram S. Adve7fe27872001-10-18 00:26:20 +000014#include "SparcInstrSelectionSupport.h"
Vikram S. Adve74825322002-03-18 03:15:35 +000015#include "SparcRegClassInfo.h"
Vikram S. Adve8557b222001-10-10 20:56:33 +000016#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000017#include "llvm/CodeGen/MachineInstr.h"
18#include "llvm/CodeGen/InstrForest.h"
19#include "llvm/CodeGen/InstrSelection.h"
Chris Lattner9c461082002-02-03 07:50:56 +000020#include "llvm/CodeGen/MachineCodeForMethod.h"
21#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000022#include "llvm/DerivedTypes.h"
23#include "llvm/iTerminators.h"
24#include "llvm/iMemory.h"
25#include "llvm/iOther.h"
26#include "llvm/BasicBlock.h"
27#include "llvm/Method.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000028#include "llvm/ConstantVals.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000029#include "Support/MathExtras.h"
Chris Lattner749655f2001-10-13 06:54:30 +000030#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000031using std::vector;
Chris Lattner20b1ea02001-09-14 03:47:57 +000032
33//************************* Forward Declarations ***************************/
34
35
Vikram S. Adve74825322002-03-18 03:15:35 +000036static void SetMemOperands_Internal (vector<MachineInstr*>& mvec,
37 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000038 const InstructionNode* vmInstrNode,
39 Value* ptrVal,
Chris Lattner697954c2002-01-20 22:54:45 +000040 const std::vector<Value*>& idxVec,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000041 const TargetMachine& target);
Chris Lattner20b1ea02001-09-14 03:47:57 +000042
43
44//************************ Internal Functions ******************************/
45
Chris Lattner20b1ea02001-09-14 03:47:57 +000046
Chris Lattner20b1ea02001-09-14 03:47:57 +000047static inline MachineOpCode
48ChooseBprInstruction(const InstructionNode* instrNode)
49{
50 MachineOpCode opCode;
51
52 Instruction* setCCInstr =
53 ((InstructionNode*) instrNode->leftChild())->getInstruction();
54
55 switch(setCCInstr->getOpcode())
56 {
57 case Instruction::SetEQ: opCode = BRZ; break;
58 case Instruction::SetNE: opCode = BRNZ; break;
59 case Instruction::SetLE: opCode = BRLEZ; break;
60 case Instruction::SetGE: opCode = BRGEZ; break;
61 case Instruction::SetLT: opCode = BRLZ; break;
62 case Instruction::SetGT: opCode = BRGZ; break;
63 default:
64 assert(0 && "Unrecognized VM instruction!");
65 opCode = INVALID_OPCODE;
66 break;
67 }
68
69 return opCode;
70}
71
72
73static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +000074ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000075 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +000076{
77 MachineOpCode opCode = INVALID_OPCODE;
78
79 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
80
81 if (isSigned)
82 {
83 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000084 {
85 case Instruction::SetEQ: opCode = BE; break;
86 case Instruction::SetNE: opCode = BNE; break;
87 case Instruction::SetLE: opCode = BLE; break;
88 case Instruction::SetGE: opCode = BGE; break;
89 case Instruction::SetLT: opCode = BL; break;
90 case Instruction::SetGT: opCode = BG; break;
91 default:
92 assert(0 && "Unrecognized VM instruction!");
93 break;
94 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000095 }
96 else
97 {
98 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000099 {
100 case Instruction::SetEQ: opCode = BE; break;
101 case Instruction::SetNE: opCode = BNE; break;
102 case Instruction::SetLE: opCode = BLEU; break;
103 case Instruction::SetGE: opCode = BCC; break;
104 case Instruction::SetLT: opCode = BCS; break;
105 case Instruction::SetGT: opCode = BGU; break;
106 default:
107 assert(0 && "Unrecognized VM instruction!");
108 break;
109 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000110 }
111
112 return opCode;
113}
114
115static inline MachineOpCode
116ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000117 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000118{
119 MachineOpCode opCode = INVALID_OPCODE;
120
121 switch(setCCInstr->getOpcode())
122 {
123 case Instruction::SetEQ: opCode = FBE; break;
124 case Instruction::SetNE: opCode = FBNE; break;
125 case Instruction::SetLE: opCode = FBLE; break;
126 case Instruction::SetGE: opCode = FBGE; break;
127 case Instruction::SetLT: opCode = FBL; break;
128 case Instruction::SetGT: opCode = FBG; break;
129 default:
130 assert(0 && "Unrecognized VM instruction!");
131 break;
132 }
133
134 return opCode;
135}
136
137
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000138// Create a unique TmpInstruction for a boolean value,
139// representing the CC register used by a branch on that value.
140// For now, hack this using a little static cache of TmpInstructions.
141// Eventually the entire BURG instruction selection should be put
142// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000143// The static cache is not too bad because the memory for these
144// TmpInstructions will be freed along with the rest of the Method anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000145//
146static TmpInstruction*
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000147GetTmpForCC(Value* boolVal, const Method* method, const Type* ccType)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000148{
Chris Lattner697954c2002-01-20 22:54:45 +0000149 typedef std::hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000150 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
151 static const Method* lastMethod = NULL; // Use to flush cache between methods
152
153 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
154
155 if (lastMethod != method)
156 {
157 lastMethod = method;
158 boolToTmpCache.clear();
159 }
160
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000161 // Look for tmpI and create a new one otherwise. The new value is
162 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000163 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
164 if (tmpI == NULL)
Chris Lattner9c461082002-02-03 07:50:56 +0000165 tmpI = new TmpInstruction(ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000166
167 return tmpI;
168}
169
170
Chris Lattner20b1ea02001-09-14 03:47:57 +0000171static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000172ChooseBccInstruction(const InstructionNode* instrNode,
173 bool& isFPBranch)
174{
175 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
176 BinaryOperator* setCCInstr = (BinaryOperator*) setCCNode->getInstruction();
177 const Type* setCCType = setCCInstr->getOperand(0)->getType();
178
179 isFPBranch = (setCCType == Type::FloatTy || setCCType == Type::DoubleTy);
180
181 if (isFPBranch)
182 return ChooseBFpccInstruction(instrNode, setCCInstr);
183 else
184 return ChooseBpccInstruction(instrNode, setCCInstr);
185}
186
187
188static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000189ChooseMovFpccInstruction(const InstructionNode* instrNode)
190{
191 MachineOpCode opCode = INVALID_OPCODE;
192
193 switch(instrNode->getInstruction()->getOpcode())
194 {
195 case Instruction::SetEQ: opCode = MOVFE; break;
196 case Instruction::SetNE: opCode = MOVFNE; break;
197 case Instruction::SetLE: opCode = MOVFLE; break;
198 case Instruction::SetGE: opCode = MOVFGE; break;
199 case Instruction::SetLT: opCode = MOVFL; break;
200 case Instruction::SetGT: opCode = MOVFG; break;
201 default:
202 assert(0 && "Unrecognized VM instruction!");
203 break;
204 }
205
206 return opCode;
207}
208
209
210// Assumes that SUBcc v1, v2 -> v3 has been executed.
211// In most cases, we want to clear v3 and then follow it by instruction
212// MOVcc 1 -> v3.
213// Set mustClearReg=false if v3 need not be cleared before conditional move.
214// Set valueToMove=0 if we want to conditionally move 0 instead of 1
215// (i.e., we want to test inverse of a condition)
Vikram S. Adve243dd452001-09-18 13:03:13 +0000216// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000217//
218static MachineOpCode
219ChooseMovpccAfterSub(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000220 bool& mustClearReg,
221 int& valueToMove)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000222{
223 MachineOpCode opCode = INVALID_OPCODE;
224 mustClearReg = true;
225 valueToMove = 1;
226
227 switch(instrNode->getInstruction()->getOpcode())
228 {
Vikram S. Adve243dd452001-09-18 13:03:13 +0000229 case Instruction::SetEQ: opCode = MOVE; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000230 case Instruction::SetLE: opCode = MOVLE; break;
231 case Instruction::SetGE: opCode = MOVGE; break;
232 case Instruction::SetLT: opCode = MOVL; break;
233 case Instruction::SetGT: opCode = MOVG; break;
Vikram S. Adve243dd452001-09-18 13:03:13 +0000234 case Instruction::SetNE: assert(0 && "No move required!"); break;
235 default: assert(0 && "Unrecognized VM instr!"); break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000236 }
237
238 return opCode;
239}
240
Chris Lattner20b1ea02001-09-14 03:47:57 +0000241static inline MachineOpCode
242ChooseConvertToFloatInstr(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000243 const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000244{
245 MachineOpCode opCode = INVALID_OPCODE;
246
247 switch(instrNode->getOpLabel())
248 {
249 case ToFloatTy:
250 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000251 opCode = FITOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000252 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000253 opCode = FXTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000254 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000255 opCode = FDTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000256 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000257 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000258 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000259 assert(0 && "Cannot convert this type to FLOAT on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000260 break;
261
262 case ToDoubleTy:
Vikram S. Adve74825322002-03-18 03:15:35 +0000263 // This is usually used in conjunction with CreateCodeToCopyIntToFloat().
264 // Both functions should treat the integer as a 32-bit value for types
265 // of 4 bytes or less, and as a 64-bit value otherwise.
266 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
267 opCode = FITOD;
268 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000269 opCode = FXTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000270 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000271 opCode = FSTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000272 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000273 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000274 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000275 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000276 break;
277
278 default:
279 break;
280 }
281
282 return opCode;
283}
284
285static inline MachineOpCode
286ChooseConvertToIntInstr(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000287 const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000288{
289 MachineOpCode opCode = INVALID_OPCODE;;
290
291 int instrType = (int) instrNode->getOpLabel();
292
293 if (instrType == ToSByteTy || instrType == ToShortTy || instrType == ToIntTy)
294 {
295 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000296 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000297 case Type::FloatTyID: opCode = FSTOI; break;
298 case Type::DoubleTyID: opCode = FDTOI; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000299 default:
300 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
301 break;
302 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000303 }
304 else if (instrType == ToLongTy)
305 {
306 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000307 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000308 case Type::FloatTyID: opCode = FSTOX; break;
309 case Type::DoubleTyID: opCode = FDTOX; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000310 default:
311 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
312 break;
313 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000314 }
315 else
316 assert(0 && "Should not get here, Mo!");
317
318 return opCode;
319}
320
321
322static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000323ChooseAddInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000324{
325 MachineOpCode opCode = INVALID_OPCODE;
326
Chris Lattner20b1ea02001-09-14 03:47:57 +0000327 if (resultType->isIntegral() ||
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000328 resultType->isPointerType() ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000329 resultType->isLabelType() ||
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000330 isa<MethodType>(resultType) ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000331 resultType == Type::BoolTy)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000332 {
333 opCode = ADD;
334 }
335 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000336 switch(resultType->getPrimitiveID())
337 {
338 case Type::FloatTyID: opCode = FADDS; break;
339 case Type::DoubleTyID: opCode = FADDD; break;
340 default: assert(0 && "Invalid type for ADD instruction"); break;
341 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000342
343 return opCode;
344}
345
346
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000347static inline MachineOpCode
348ChooseAddInstruction(const InstructionNode* instrNode)
349{
350 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
351}
352
353
Chris Lattner20b1ea02001-09-14 03:47:57 +0000354static inline MachineInstr*
355CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000356 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000357{
358 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000359 ? FMOVS : FMOVD);
Vikram S. Adve74825322002-03-18 03:15:35 +0000360 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
361 instrNode->leftChild()->getValue());
362 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
363 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000364 return minstr;
365}
366
367static inline MachineInstr*
368CreateAddConstInstruction(const InstructionNode* instrNode)
369{
370 MachineInstr* minstr = NULL;
371
372 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000373 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000374
375 // Cases worth optimizing are:
376 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
377 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
378 //
379 const Type* resultType = instrNode->getInstruction()->getType();
380
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000381 if (resultType == Type::FloatTy ||
382 resultType == Type::DoubleTy)
383 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000384 double dval = cast<ConstantFP>(constOp)->getValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000385 if (dval == 0.0)
386 minstr = CreateMovFloatInstruction(instrNode, resultType);
387 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000388
389 return minstr;
390}
391
392
393static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000394ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000395{
396 MachineOpCode opCode = INVALID_OPCODE;
397
Chris Lattner20b1ea02001-09-14 03:47:57 +0000398 if (resultType->isIntegral() ||
399 resultType->isPointerType())
400 {
401 opCode = SUB;
402 }
403 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000404 switch(resultType->getPrimitiveID())
405 {
406 case Type::FloatTyID: opCode = FSUBS; break;
407 case Type::DoubleTyID: opCode = FSUBD; break;
408 default: assert(0 && "Invalid type for SUB instruction"); break;
409 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000410
411 return opCode;
412}
413
414
415static inline MachineInstr*
416CreateSubConstInstruction(const InstructionNode* instrNode)
417{
418 MachineInstr* minstr = NULL;
419
420 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000421 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000422
423 // Cases worth optimizing are:
424 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
425 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
426 //
427 const Type* resultType = instrNode->getInstruction()->getType();
428
429 if (resultType == Type::FloatTy ||
430 resultType == Type::DoubleTy)
431 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000432 double dval = cast<ConstantFP>(constOp)->getValue();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000433 if (dval == 0.0)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000434 minstr = CreateMovFloatInstruction(instrNode, resultType);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000435 }
436
437 return minstr;
438}
439
440
441static inline MachineOpCode
442ChooseFcmpInstruction(const InstructionNode* instrNode)
443{
444 MachineOpCode opCode = INVALID_OPCODE;
445
446 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
447 switch(operand->getType()->getPrimitiveID()) {
448 case Type::FloatTyID: opCode = FCMPS; break;
449 case Type::DoubleTyID: opCode = FCMPD; break;
450 default: assert(0 && "Invalid type for FCMP instruction"); break;
451 }
452
453 return opCode;
454}
455
456
457// Assumes that leftArg and rightArg are both cast instructions.
458//
459static inline bool
460BothFloatToDouble(const InstructionNode* instrNode)
461{
462 InstrTreeNode* leftArg = instrNode->leftChild();
463 InstrTreeNode* rightArg = instrNode->rightChild();
464 InstrTreeNode* leftArgArg = leftArg->leftChild();
465 InstrTreeNode* rightArgArg = rightArg->leftChild();
466 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
467
468 // Check if both arguments are floats cast to double
469 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000470 leftArgArg->getValue()->getType() == Type::FloatTy &&
471 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000472}
473
474
475static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000476ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000477{
478 MachineOpCode opCode = INVALID_OPCODE;
479
Chris Lattner20b1ea02001-09-14 03:47:57 +0000480 if (resultType->isIntegral())
Vikram S. Adve510eec72001-11-04 21:59:14 +0000481 opCode = MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000482 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000483 switch(resultType->getPrimitiveID())
484 {
485 case Type::FloatTyID: opCode = FMULS; break;
486 case Type::DoubleTyID: opCode = FMULD; break;
487 default: assert(0 && "Invalid type for MUL instruction"); break;
488 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000489
490 return opCode;
491}
492
493
Vikram S. Adve510eec72001-11-04 21:59:14 +0000494
Chris Lattner20b1ea02001-09-14 03:47:57 +0000495static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000496CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000497 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000498{
499 MachineInstr* minstr = new MachineInstr(SUB);
Vikram S. Adve74825322002-03-18 03:15:35 +0000500 minstr->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
501 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, vreg);
502 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000503 return minstr;
504}
505
506
Vikram S. Adve74825322002-03-18 03:15:35 +0000507// Does not create any instructions if we cannot exploit constant to
508// create a cheaper instruction
509static inline void
510CreateMulConstInstruction(const TargetMachine &target,
511 Value* lval, Value* rval, Value* destVal,
512 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000513{
Vikram S. Adve74825322002-03-18 03:15:35 +0000514 MachineInstr* minstr1 = NULL;
515 MachineInstr* minstr2 = NULL;
516
517 Value* constOp = rval;
518 if (! isa<Constant>(constOp))
519 return;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000520
521 // Cases worth optimizing are:
522 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
523 // (2) Multiply by 2^x for integer types: replace with Shift
524 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000525 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000526
Vikram S. Adve243dd452001-09-18 13:03:13 +0000527 if (resultType->isIntegral() || resultType->isPointerType())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000528 {
529 unsigned pow;
530 bool isValidConst;
531 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
532 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000533 {
534 bool needNeg = false;
535 if (C < 0)
536 {
537 needNeg = true;
538 C = -C;
539 }
540
541 if (C == 0 || C == 1)
542 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000543 minstr1 = new MachineInstr(ADD);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000544
545 if (C == 0)
Vikram S. Adve74825322002-03-18 03:15:35 +0000546 minstr1->SetMachineOperandReg(0,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000547 target.getRegInfo().getZeroRegNum());
548 else
Vikram S. Adve74825322002-03-18 03:15:35 +0000549 minstr1->SetMachineOperandVal(0,MachineOperand::MO_VirtualRegister,
550 lval);
551 minstr1->SetMachineOperandReg(1,target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000552 }
553 else if (IsPowerOf2(C, pow))
554 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000555 minstr1 = new MachineInstr((resultType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000556 ? SLLX : SLL);
Vikram S. Adve74825322002-03-18 03:15:35 +0000557 minstr1->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
558 lval);
559 minstr1->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
560 pow);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000561 }
562
Vikram S. Adve74825322002-03-18 03:15:35 +0000563 if (minstr1 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000564 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve74825322002-03-18 03:15:35 +0000565 minstr2 = CreateIntNegInstruction(target, destVal);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000566 }
567 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000568 }
569 else
570 {
571 if (resultType == Type::FloatTy ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000572 resultType == Type::DoubleTy)
573 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000574 double dval = cast<ConstantFP>(constOp)->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000575 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000576 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000577 bool needNeg = (dval < 0);
578
579 MachineOpCode opCode = needNeg
580 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
581 : (resultType == Type::FloatTy? FMOVS : FMOVD);
582
Vikram S. Adve74825322002-03-18 03:15:35 +0000583 minstr1 = new MachineInstr(opCode);
584 minstr1->SetMachineOperandVal(0,
585 MachineOperand::MO_VirtualRegister,
586 lval);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000587 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000588 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000589 }
590
Vikram S. Adve74825322002-03-18 03:15:35 +0000591 if (minstr1 != NULL)
592 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
593 destVal);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000594
Vikram S. Adve74825322002-03-18 03:15:35 +0000595 if (minstr1)
596 mvec.push_back(minstr1);
597 if (minstr2)
598 mvec.push_back(minstr2);
599}
600
601
602// Return NULL if we cannot exploit constant to create a cheaper instruction
603static inline void
604CreateMulInstruction(const TargetMachine &target,
605 Value* lval, Value* rval, Value* destVal,
606 vector<MachineInstr*>& mvec,
607 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
608{
609 unsigned int L = mvec.size();
610 CreateMulConstInstruction(target, lval, rval, destVal, mvec);
611 if (mvec.size() == L)
612 { // no instructions were added so create MUL reg, reg, reg.
613 // Use FSMULD if both operands are actually floats cast to doubles.
614 // Otherwise, use the default opcode for the appropriate type.
615 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
616 ? forceMulOp
617 : ChooseMulInstructionByType(destVal->getType()));
618 MachineInstr* M = new MachineInstr(mulOp);
619 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, lval);
620 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, rval);
621 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, destVal);
622 mvec.push_back(M);
623 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000624}
625
626
Vikram S. Adve510eec72001-11-04 21:59:14 +0000627// Generate a divide instruction for Div or Rem.
628// For Rem, this assumes that the operand type will be signed if the result
629// type is signed. This is correct because they must have the same sign.
630//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000631static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000632ChooseDivInstruction(TargetMachine &target,
633 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000634{
635 MachineOpCode opCode = INVALID_OPCODE;
636
637 const Type* resultType = instrNode->getInstruction()->getType();
638
639 if (resultType->isIntegral())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000640 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000641 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000642 switch(resultType->getPrimitiveID())
643 {
644 case Type::FloatTyID: opCode = FDIVS; break;
645 case Type::DoubleTyID: opCode = FDIVD; break;
646 default: assert(0 && "Invalid type for DIV instruction"); break;
647 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000648
649 return opCode;
650}
651
652
Vikram S. Adve74825322002-03-18 03:15:35 +0000653// Return NULL if we cannot exploit constant to create a cheaper instruction
654static inline void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000655CreateDivConstInstruction(TargetMachine &target,
656 const InstructionNode* instrNode,
Vikram S. Adve74825322002-03-18 03:15:35 +0000657 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000658{
Vikram S. Adve74825322002-03-18 03:15:35 +0000659 MachineInstr* minstr1 = NULL;
660 MachineInstr* minstr2 = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000661
662 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Vikram S. Adve74825322002-03-18 03:15:35 +0000663 if (! isa<Constant>(constOp))
664 return;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000665
666 // Cases worth optimizing are:
667 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
668 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
669 //
670 const Type* resultType = instrNode->getInstruction()->getType();
671
672 if (resultType->isIntegral())
673 {
674 unsigned pow;
675 bool isValidConst;
676 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
677 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000678 {
679 bool needNeg = false;
680 if (C < 0)
681 {
682 needNeg = true;
683 C = -C;
684 }
685
686 if (C == 1)
687 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000688 minstr1 = new MachineInstr(ADD);
689 minstr1->SetMachineOperandVal(0,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000690 instrNode->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +0000691 minstr1->SetMachineOperandReg(1,target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000692 }
693 else if (IsPowerOf2(C, pow))
694 {
695 MachineOpCode opCode= ((resultType->isSigned())
696 ? (resultType==Type::LongTy)? SRAX : SRA
697 : (resultType==Type::LongTy)? SRLX : SRL);
Vikram S. Adve74825322002-03-18 03:15:35 +0000698 minstr1 = new MachineInstr(opCode);
699 minstr1->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000700 instrNode->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +0000701 minstr1->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000702 pow);
703 }
704
Vikram S. Adve74825322002-03-18 03:15:35 +0000705 if (minstr1 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000706 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve74825322002-03-18 03:15:35 +0000707 minstr2 = CreateIntNegInstruction(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000708 instrNode->getValue());
709 }
710 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000711 }
712 else
713 {
714 if (resultType == Type::FloatTy ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000715 resultType == Type::DoubleTy)
716 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000717 double dval = cast<ConstantFP>(constOp)->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000718 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000719 {
720 bool needNeg = (dval < 0);
721
722 MachineOpCode opCode = needNeg
723 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
724 : (resultType == Type::FloatTy? FMOVS : FMOVD);
725
Vikram S. Adve74825322002-03-18 03:15:35 +0000726 minstr1 = new MachineInstr(opCode);
727 minstr1->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000728 instrNode->leftChild()->getValue());
729 }
730 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000731 }
732
Vikram S. Adve74825322002-03-18 03:15:35 +0000733 if (minstr1 != NULL)
734 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
735 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000736
Vikram S. Adve74825322002-03-18 03:15:35 +0000737 if (minstr1)
738 mvec.push_back(minstr1);
739 if (minstr2)
740 mvec.push_back(minstr2);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000741}
742
743
Vikram S. Adve74825322002-03-18 03:15:35 +0000744static void
745CreateCodeForVariableSizeAlloca(const TargetMachine& target,
746 Instruction* result,
747 unsigned int tsize,
748 Value* numElementsVal,
749 vector<MachineInstr*>& getMvec)
750{
751 MachineInstr* M;
752
753 // Create a Value to hold the (constant) element size
754 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
755
756 // Get the constant offset from SP for dynamically allocated storage
757 // and create a temporary Value to hold it.
758 assert(result && result->getParent() && "Result value is not part of a method?");
759 Method* method = result->getParent()->getParent();
760 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(method);
761 bool growUp;
762 ConstantSInt* dynamicAreaOffset =
763 ConstantSInt::get(Type::IntTy,
764 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
765 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
766
767 // Create a temporary value to hold the result of MUL
768 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
769 MachineCodeForInstruction::get(result).addTemp(tmpProd);
770
771 // Instruction 1: mul numElements, typeSize -> tmpProd
772 M = new MachineInstr(MULX);
773 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, numElementsVal);
774 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tsizeVal);
775 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, tmpProd);
776 getMvec.push_back(M);
777
778 // Instruction 2: sub %sp, tmpProd -> %sp
779 M = new MachineInstr(SUB);
780 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
781 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tmpProd);
782 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
783 getMvec.push_back(M);
784
785 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
786 M = new MachineInstr(ADD);
787 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
788 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, dynamicAreaOffset);
789 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
790 getMvec.push_back(M);
791}
792
793
794static void
795CreateCodeForFixedSizeAlloca(const TargetMachine& target,
796 Instruction* result,
797 unsigned int tsize,
798 unsigned int numElements,
799 vector<MachineInstr*>& getMvec)
800{
801 assert(result && result->getParent() && "Result value is not part of a method?");
802 Method* method = result->getParent()->getParent();
803 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(method);
804
805 // Check if the offset would small enough to use as an immediate in load/stores
806 // (check LDX because all load/stores have the same-size immediate field).
807 // If not, put the variable in the dynamically sized area of the frame.
808 int offsetFromFP = mcInfo.computeOffsetforLocalVar(target, result,
809 tsize * numElements);
810 if (! target.getInstrInfo().constantFitsInImmedField(LDX, offsetFromFP))
811 {
812 CreateCodeForVariableSizeAlloca(target, result, tsize,
813 ConstantSInt::get(Type::IntTy,numElements),
814 getMvec);
815 return;
816 }
817
818 // else offset fits in immediate field so go ahead and allocate it.
819 offsetFromFP = mcInfo.allocateLocalVar(target, result, tsize * numElements);
820
821 // Create a temporary Value to hold the constant offset.
822 // This is needed because it may not fit in the immediate field.
823 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
824
825 // Instruction 1: add %fp, offsetFromFP -> result
826 MachineInstr* M = new MachineInstr(ADD);
827 M->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
828 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, offsetVal);
829 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
830
831 getMvec.push_back(M);
832}
833
834
835
Chris Lattner20b1ea02001-09-14 03:47:57 +0000836//------------------------------------------------------------------------
837// Function SetOperandsForMemInstr
838//
839// Choose addressing mode for the given load or store instruction.
840// Use [reg+reg] if it is an indexed reference, and the index offset is
841// not a constant or if it cannot fit in the offset field.
842// Use [reg+offset] in all other cases.
843//
844// This assumes that all array refs are "lowered" to one of these forms:
845// %x = load (subarray*) ptr, constant ; single constant offset
846// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
847// Generally, this should happen via strength reduction + LICM.
848// Also, strength reduction should take care of using the same register for
849// the loop index variable and an array index, when that is profitable.
850//------------------------------------------------------------------------
851
852static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000853SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
854 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000855 const InstructionNode* vmInstrNode,
856 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000857{
858 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
859
860 // Variables to hold the index vector, ptr value, and offset value.
861 // The major work here is to extract these for all 3 instruction types
862 // and then call the common function SetMemOperands_Internal().
863 //
Vikram S. Advefa248972001-12-15 00:36:32 +0000864 vector<Value*> idxVec;
Vikram S. Adve74825322002-03-18 03:15:35 +0000865 Value* ptrVal = memInst->getPointerOperand();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000866
867 // Test if a GetElemPtr instruction is being folded into this mem instrn.
868 // If so, it will be in the left child for Load and GetElemPtr,
869 // and in the right child for Store instructions.
870 //
871 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000872 ? vmInstrNode->rightChild()
873 : vmInstrNode->leftChild());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000874
Vikram S. Adve74825322002-03-18 03:15:35 +0000875 // We can only fold a chain of GetElemPtr instructions for structure references
876 //
877 if (isa<StructType>(cast<PointerType>(ptrVal->getType())->getElementType())
878 && (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
879 ptrChild->getOpLabel() == GetElemPtrIdx))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000880 {
881 // There is a GetElemPtr instruction and there may be a chain of
882 // more than one. Use the pointer value of the last one in the chain.
883 // Fold the index vectors from the entire chain and from the mem
884 // instruction into one single index vector.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000885 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000886 ptrVal = FoldGetElemChain((InstructionNode*) ptrChild, idxVec);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000887
Vikram S. Adve74825322002-03-18 03:15:35 +0000888 assert (! cast<PointerType>(ptrVal->getType())->getElementType()->isArrayType()
889 && "GetElemPtr cannot be folded into array refs in selection");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000890 }
891
Vikram S. Adve74825322002-03-18 03:15:35 +0000892 // Append the index vector of this instruction (may be none) to the indexes
893 // folded in previous getElementPtr's (may be none)
894 idxVec.insert(idxVec.end(), memInst->idx_begin(), memInst->idx_end());
895
896 SetMemOperands_Internal(mvec, mvecI, vmInstrNode, ptrVal, idxVec, target);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000897}
898
899
Vikram S. Adve74825322002-03-18 03:15:35 +0000900// Generate the correct operands (and additional instructions if needed)
901// for the given pointer and given index vector.
902//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000903static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000904SetMemOperands_Internal(vector<MachineInstr*>& mvec,
905 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000906 const InstructionNode* vmInstrNode,
907 Value* ptrVal,
Vikram S. Advefa248972001-12-15 00:36:32 +0000908 const vector<Value*>& idxVec,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000909 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000910{
911 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
912
913 // Initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000914 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000915 Value* valueForRegOffset = NULL;
916 MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister;
917
Vikram S. Adve74825322002-03-18 03:15:35 +0000918 // Check if there is an index vector and if so, compute the
919 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +0000920 //
921 if (idxVec.size() > 0)
922 {
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000923 unsigned offset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000924
Vikram S. Adve74825322002-03-18 03:15:35 +0000925 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000926
Chris Lattner7a176752001-12-04 00:03:30 +0000927 if (ptrType->getElementType()->isStructType())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000928 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000929 // Compute the offset value using the index vector,
930 // and create a virtual register for it.
931 unsigned offset = target.DataLayout.getIndexedOffset(ptrType,idxVec);
932 valueForRegOffset = ConstantSInt::get(Type::IntTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000933 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000934 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000935 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000936 // It must be an array ref. Check that the indexing has been
937 // lowered to a single offset.
938 assert((memInst->getNumOperands()
939 == (unsigned) 1 + memInst->getFirstIndexOperandNumber())
940 && "Array refs must be lowered before Instruction Selection");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000941
Vikram S. Adve74825322002-03-18 03:15:35 +0000942 Value* arrayOffsetVal = * memInst->idx_begin();
943
944 // Generate a MUL instruction to compute address from index
945 // The call to getTypeSize() will fail if size is not constant
946 vector<MachineInstr*> mulVec;
947 Instruction* addr = new TmpInstruction(Type::UIntTy, memInst);
948 MachineCodeForInstruction::get(memInst).addTemp(addr);
949 unsigned int eltSize =
950 target.DataLayout.getTypeSize(ptrType->getElementType());
951 assert(eltSize > 0 && "Invalid or non-constant array element size");
952 ConstantUInt* eltVal = ConstantUInt::get(Type::UIntTy, eltSize);
953
954 CreateMulInstruction(target,
955 arrayOffsetVal, /* lval, not likely constant */
956 eltVal, /* rval, likely constant */
957 addr, /* result*/
958 mulVec, INVALID_MACHINE_OPCODE);
959 assert(mulVec.size() > 0 && "No multiply instruction created?");
960 for (vector<MachineInstr*>::const_iterator I = mulVec.begin();
961 I != mulVec.end(); ++I)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000962 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000963 mvecI = mvec.insert(mvecI, *I); // get ptr to inserted value
964 ++mvecI; // get ptr to mem. instr.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000965 }
Vikram S. Adve74825322002-03-18 03:15:35 +0000966
967 valueForRegOffset = addr;
968
969 // Check if the offset is a constant,
970 // if (Constant *CPV = dyn_cast<Constant>(arrayOffsetVal))
971 // {
972 // isConstantOffset = true; // always constant for structs
973 // assert(arrayOffsetVal->getType()->isIntegral());
974 // offset = (CPV->getType()->isSigned()
975 // ? cast<ConstantSInt>(CPV)->getValue()
976 // : (int64_t) cast<ConstantUInt>(CPV)->getValue());
977 // }
978 // else
979 // {
980 // valueForRegOffset = arrayOffsetVal;
981 // }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000982 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000983 }
984 else
985 {
986 offsetOpType = MachineOperand::MO_SignExtendedImmed;
987 smallConstOffset = 0;
988 }
989
990 // Operand 0 is value for STORE, ptr for LOAD or GET_ELEMENT_PTR
991 // It is the left child in the instruction tree in all cases.
992 Value* leftVal = vmInstrNode->leftChild()->getValue();
Vikram S. Adve74825322002-03-18 03:15:35 +0000993 (*mvecI)->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
994 leftVal);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000995
996 // Operand 1 is ptr for STORE, offset for LOAD or GET_ELEMENT_PTR
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000997 // Operand 2 is offset for STORE, result reg for LOAD or GET_ELEMENT_PTR
Chris Lattner20b1ea02001-09-14 03:47:57 +0000998 //
999 unsigned offsetOpNum = (memInst->getOpcode() == Instruction::Store)? 2 : 1;
1000 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1001 {
1002 assert(valueForRegOffset != NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001003 (*mvecI)->SetMachineOperandVal(offsetOpNum, offsetOpType,
1004 valueForRegOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001005 }
1006 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001007 (*mvecI)->SetMachineOperandConst(offsetOpNum, offsetOpType,
1008 smallConstOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001009
1010 if (memInst->getOpcode() == Instruction::Store)
Vikram S. Adve74825322002-03-18 03:15:35 +00001011 (*mvecI)->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1012 ptrVal);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001013 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001014 (*mvecI)->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1015 vmInstrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +00001016}
1017
1018
Chris Lattner20b1ea02001-09-14 03:47:57 +00001019//
1020// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001021// in place of the use(s) of that instruction in node `parent'.
1022// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001023// Also make sure to skip over a parent who:
1024// (1) is a list node in the Burg tree, or
1025// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001026//
1027static void
1028ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001029 InstrTreeNode* parent,
1030 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001031{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001032 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1033
Chris Lattner20b1ea02001-09-14 03:47:57 +00001034 Instruction* unusedOp = treeNode->getInstruction();
1035 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001036
1037 // The parent itself may be a list node, so find the real parent instruction
1038 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1039 {
1040 parent = parent->parent();
1041 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1042 }
1043 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1044
1045 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001046 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001047
1048 // The parent's mvec would be empty if it was itself forwarded.
1049 // Recursively call ForwardOperand in that case...
1050 //
1051 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001052 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001053 assert(parent->parent() != NULL &&
1054 "Parent could not have been forwarded, yet has no instructions?");
1055 ForwardOperand(treeNode, parent->parent(), operandNum);
1056 }
1057 else
1058 {
1059 bool fwdSuccessful = false;
1060 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001061 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001062 MachineInstr* minstr = mvec[i];
1063 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001064 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001065 const MachineOperand& mop = minstr->getOperand(i);
1066 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1067 mop.getVRegValue() == unusedOp)
1068 {
1069 minstr->SetMachineOperandVal(i,
1070 MachineOperand::MO_VirtualRegister, fwdOp);
1071 fwdSuccessful = true;
1072 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001073 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001074
1075 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1076 if (minstr->getImplicitRef(i) == unusedOp)
1077 {
1078 minstr->setImplicitRef(i, fwdOp,
1079 minstr->implicitRefIsDefined(i));
1080 fwdSuccessful = true;
1081 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001082 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001083 assert(fwdSuccessful && "Value to be forwarded is never used!");
Chris Lattner20b1ea02001-09-14 03:47:57 +00001084 }
1085}
1086
1087
Ruchira Sasanka67a463a2001-11-12 14:45:33 +00001088void UltraSparcInstrInfo::
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001089CreateCopyInstructionsByType(const TargetMachine& target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001090 Method* method,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001091 Value* src,
1092 Instruction* dest,
Ruchira Sasanka67a463a2001-11-12 14:45:33 +00001093 vector<MachineInstr*>& minstrVec) const
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001094{
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001095 bool loadConstantToReg = false;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001096
1097 const Type* resultType = dest->getType();
1098
1099 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
1100 if (opCode == INVALID_OPCODE)
1101 {
1102 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001103 return;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001104 }
1105
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001106 // if `src' is a constant that doesn't fit in the immed field or if it is
1107 // a global variable (i.e., a constant address), generate a load
1108 // instruction instead of an add
1109 //
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001110 if (isa<Constant>(src))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001111 {
1112 unsigned int machineRegNum;
1113 int64_t immedValue;
1114 MachineOperand::MachineOperandType opType =
1115 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
1116 machineRegNum, immedValue);
1117
1118 if (opType == MachineOperand::MO_VirtualRegister)
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001119 loadConstantToReg = true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001120 }
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001121 else if (isa<GlobalValue>(src))
1122 loadConstantToReg = true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001123
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001124 if (loadConstantToReg)
1125 { // `src' is constant and cannot fit in immed field for the ADD
1126 // Insert instructions to "load" the constant into a register
1127 vector<TmpInstruction*> tempVec;
Vikram S. Adve74825322002-03-18 03:15:35 +00001128 target.getInstrInfo().CreateCodeToLoadConst(method, src, dest,minstrVec,tempVec);
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001129 for (unsigned i=0; i < tempVec.size(); i++)
Chris Lattner9c461082002-02-03 07:50:56 +00001130 MachineCodeForInstruction::get(dest).addTemp(tempVec[i]);
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001131 }
1132 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001133 { // Create an add-with-0 instruction of the appropriate type.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001134 // Make `src' the second operand, in case it is a constant
1135 // Use (unsigned long) 0 for a NULL pointer value.
1136 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001137 const Type* zeroValueType =
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001138 (resultType->getPrimitiveID() == Type::PointerTyID)? Type::ULongTy
1139 : resultType;
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001140 MachineInstr* minstr = new MachineInstr(opCode);
Vikram S. Adve74825322002-03-18 03:15:35 +00001141 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1142 Constant::getNullConstant(zeroValueType));
1143 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, src);
1144 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,dest);
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001145 minstrVec.push_back(minstr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001146 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001147}
1148
1149
Ruchira Sasanka67a463a2001-11-12 14:45:33 +00001150
Vikram S. Advefb361122001-10-22 13:36:31 +00001151//******************* Externally Visible Functions *************************/
1152
1153
1154//------------------------------------------------------------------------
1155// External Function: GetInstructionsForProlog
1156// External Function: GetInstructionsForEpilog
1157//
1158// Purpose:
1159// Create prolog and epilog code for procedure entry and exit
1160//------------------------------------------------------------------------
1161
1162extern unsigned
1163GetInstructionsForProlog(BasicBlock* entryBB,
1164 TargetMachine &target,
1165 MachineInstr** mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001166{
Vikram S. Adve74825322002-03-18 03:15:35 +00001167 MachineInstr* M;
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001168 const MachineFrameInfo& frameInfo = target.getFrameInfo();
Vikram S. Adve74825322002-03-18 03:15:35 +00001169 unsigned int N = 0;
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001170
Vikram S. Advefb361122001-10-22 13:36:31 +00001171 // The second operand is the stack size. If it does not fit in the
Vikram S. Adve74825322002-03-18 03:15:35 +00001172 // immediate field, we have to use a free register to hold the size.
1173 // We will assume that local register `l0' is unused since the SAVE
1174 // instruction must be the first instruction in each procedure.
1175 //
Vikram S. Advefb361122001-10-22 13:36:31 +00001176 Method* method = entryBB->getParent();
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001177 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(method);
1178 unsigned int staticStackSize = mcInfo.getStaticStackSize();
1179
1180 if (staticStackSize < (unsigned) frameInfo.getMinStackFrameSize())
1181 staticStackSize = (unsigned) frameInfo.getMinStackFrameSize();
1182
1183 if (unsigned padsz = (staticStackSize %
1184 (unsigned) frameInfo.getStackFrameSizeAlignment()))
Vikram S. Advefd9b7dc2001-11-12 05:16:39 +00001185 staticStackSize += frameInfo.getStackFrameSizeAlignment() - padsz;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001186
Vikram S. Adve74825322002-03-18 03:15:35 +00001187 if (target.getInstrInfo().constantFitsInImmedField(SAVE, staticStackSize))
1188 {
1189 M = new MachineInstr(SAVE);
1190 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
1191 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
1192 - (int) staticStackSize);
1193 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
1194 mvec[N++] = M;
1195 }
1196 else
1197 {
1198 M = new MachineInstr(SETSW);
1199 M->SetMachineOperandReg(0, MachineOperand::MO_SignExtendedImmed,
1200 - staticStackSize);
1201 M->SetMachineOperandReg(1, MachineOperand::MO_MachineRegister,
1202 target.getRegInfo().getUnifiedRegNum(
1203 target.getRegInfo().getRegClassIDOfType(Type::IntTy),
1204 SparcIntRegOrder::l0));
1205 mvec[N++] = M;
1206
1207 M = new MachineInstr(SAVE);
1208 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
1209 M->SetMachineOperandReg(1, MachineOperand::MO_MachineRegister,
1210 target.getRegInfo().getUnifiedRegNum(
1211 target.getRegInfo().getRegClassIDOfType(Type::IntTy),
1212 SparcIntRegOrder::l0));
1213 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
1214 mvec[N++] = M;
1215 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001216
Vikram S. Adve74825322002-03-18 03:15:35 +00001217 return N;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001218}
1219
1220
Vikram S. Advefb361122001-10-22 13:36:31 +00001221extern unsigned
1222GetInstructionsForEpilog(BasicBlock* anExitBB,
1223 TargetMachine &target,
1224 MachineInstr** mvec)
1225{
Vikram S. Advefb361122001-10-22 13:36:31 +00001226 mvec[0] = new MachineInstr(RESTORE);
Vikram S. Adve74825322002-03-18 03:15:35 +00001227 mvec[0]->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
1228 mvec[0]->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
Chris Lattner697954c2002-01-20 22:54:45 +00001229 (int64_t)0);
Vikram S. Adve74825322002-03-18 03:15:35 +00001230 mvec[0]->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Advefb361122001-10-22 13:36:31 +00001231
1232 return 1;
1233}
1234
1235
1236//------------------------------------------------------------------------
1237// External Function: ThisIsAChainRule
1238//
1239// Purpose:
1240// Check if a given BURG rule is a chain rule.
1241//------------------------------------------------------------------------
1242
1243extern bool
1244ThisIsAChainRule(int eruleno)
1245{
1246 switch(eruleno)
1247 {
1248 case 111: // stmt: reg
1249 case 113: // stmt: bool
1250 case 123:
1251 case 124:
1252 case 125:
1253 case 126:
1254 case 127:
1255 case 128:
1256 case 129:
1257 case 130:
1258 case 131:
1259 case 132:
1260 case 133:
1261 case 155:
1262 case 221:
1263 case 222:
1264 case 241:
1265 case 242:
1266 case 243:
1267 case 244:
1268 return true; break;
1269
1270 default:
1271 return false; break;
1272 }
1273}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001274
1275
1276//------------------------------------------------------------------------
1277// External Function: GetInstructionsByRule
1278//
1279// Purpose:
1280// Choose machine instructions for the SPARC according to the
1281// patterns chosen by the BURG-generated parser.
1282//------------------------------------------------------------------------
1283
Vikram S. Adve74825322002-03-18 03:15:35 +00001284void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001285GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001286 int ruleForNode,
1287 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001288 TargetMachine &target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001289 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001290{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001291 bool checkCast = false; // initialize here to use fall-through
Chris Lattner20b1ea02001-09-14 03:47:57 +00001292 int nextRule;
1293 int forwardOperandNum = -1;
Vikram S. Adve74825322002-03-18 03:15:35 +00001294 unsigned int allocaSize = 0;
1295 MachineInstr* M, *M2;
1296 unsigned int L;
1297
1298 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001299
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001300 //
1301 // Let's check for chain rules outside the switch so that we don't have
1302 // to duplicate the list of chain rule production numbers here again
1303 //
1304 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001305 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001306 // Chain rules have a single nonterminal on the RHS.
1307 // Get the rule that matches the RHS non-terminal and use that instead.
1308 //
1309 assert(nts[0] && ! nts[1]
1310 && "A chain rule should have only one RHS non-terminal!");
1311 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1312 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001313 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001314 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001315 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001316 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001317 switch(ruleForNode) {
1318 case 1: // stmt: Ret
1319 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001320 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001321 // for moving return value to appropriate register.
1322 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001323 // Mark the return value register as an implicit ref of
1324 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001325 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001326 ReturnInst *returnInstr =
1327 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001328 assert(returnInstr->getOpcode() == Instruction::Ret);
1329
Chris Lattner9c461082002-02-03 07:50:56 +00001330 Instruction* returnReg = new TmpInstruction(returnInstr);
1331 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001332
Vikram S. Adve74825322002-03-18 03:15:35 +00001333 M = new MachineInstr(JMPLRET);
1334 M->SetMachineOperandReg(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001335 returnReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001336 M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
Chris Lattner697954c2002-01-20 22:54:45 +00001337 (int64_t)8);
Vikram S. Adve74825322002-03-18 03:15:35 +00001338 M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001339
Vikram S. Advea995e602001-10-11 04:23:19 +00001340 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001341 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001342
Vikram S. Adve74825322002-03-18 03:15:35 +00001343 mvec.push_back(M);
1344 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001345
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001346 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001347 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001348
1349 case 3: // stmt: Store(reg,reg)
1350 case 4: // stmt: Store(reg,ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001351 mvec.push_back(new MachineInstr(
1352 ChooseStoreInstruction(
1353 subtreeRoot->leftChild()->getValue()->getType())));
1354 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001355 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001356
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001357 case 5: // stmt: BrUncond
Vikram S. Adve74825322002-03-18 03:15:35 +00001358 M = new MachineInstr(BA);
1359 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001360 (Value*)NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001361 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001362 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001363 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001364
1365 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001366 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001367 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001368
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001369 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001370 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001371 // If the constant is ZERO, we can use the branch-on-integer-register
1372 // instructions and avoid the SUBcc instruction entirely.
1373 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001374 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001375 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1376 assert(constNode &&
1377 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001378 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001379 bool isValidConst;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001380
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001381 if ((constVal->getType()->isIntegral()
1382 || constVal->getType()->isPointerType())
1383 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1384 && isValidConst)
1385 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001386 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1387
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001388 // That constant is a zero after all...
1389 // Use the left child of setCC as the first argument!
Vikram S. Adve74825322002-03-18 03:15:35 +00001390 M = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1391 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001392 subtreeRoot->leftChild()->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001393 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1394 brInst->getSuccessor(0));
1395 mvec.push_back(M);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001396
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001397 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001398 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001399
1400 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001401 M = new MachineInstr(BA);
1402 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1403 (Value*) NULL);
1404 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001405 brInst->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001406 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001407
1408 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001409 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001410
1411 break;
1412 }
1413 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001414 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001415
1416 case 6: // stmt: BrCond(bool)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001417 { // bool => boolean was computed with some boolean operator
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001418 // (SetCC, Not, ...). We need to check whether the type was a FP,
1419 // signed int or unsigned int, and check the branching condition in
1420 // order to choose the branch to use.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001421 // If it is an integer CC, we also need to find the unique
1422 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001423 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001424 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001425 bool isFPBranch;
Vikram S. Adve74825322002-03-18 03:15:35 +00001426 M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001427
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001428 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1429 brInst->getParent()->getParent(),
1430 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001431
Vikram S. Adve74825322002-03-18 03:15:35 +00001432 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue);
1433 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1434 brInst->getSuccessor(0));
1435 mvec.push_back(M);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001436
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001437 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001438 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001439
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001440 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001441 M = new MachineInstr(BA);
1442 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1443 (Value*) NULL);
1444 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1445 brInst->getSuccessor(1));
1446 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001447
1448 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001449 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001450 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001451 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001452
1453 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001454 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001455 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001456 Constant* constVal =
1457 cast<Constant>(subtreeRoot->leftChild()->getValue());
1458 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001459
Vikram S. Adve74825322002-03-18 03:15:35 +00001460 M = new MachineInstr(BA);
1461 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1462 (Value*) NULL);
1463 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001464 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001465 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001466
1467 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001468 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001469 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001470 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001471
1472 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001473 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001474 // Just use the branch-on-integer-register instruction!
1475 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001476 M = new MachineInstr(BRNZ);
1477 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001478 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001479 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001480 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001481 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001482
1483 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001484 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001485
1486 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001487 M = new MachineInstr(BA);
1488 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1489 (Value*) NULL);
1490 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001491 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001492 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001493
1494 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001495 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001496 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001497 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001498
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001499 case 9: // stmt: Switch(reg)
1500 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001501 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001502
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001503 case 10: // reg: VRegList(reg, reg)
1504 assert(0 && "VRegList should never be the topmost non-chain rule");
1505 break;
1506
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001507 case 21: // bool: Not(bool): Both these are implemented as:
1508 case 321: // reg: BNot(reg) : reg = reg XOR-NOT 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001509 M = new MachineInstr(XNOR);
1510 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1511 subtreeRoot->leftChild()->getValue());
1512 M->SetMachineOperandReg(1, target.getRegInfo().getZeroRegNum());
1513 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1514 subtreeRoot->getValue());
1515 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001516 break;
1517
1518 case 322: // reg: ToBoolTy(bool):
1519 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001520 {
1521 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
1522 assert(opType->isIntegral() || opType->isPointerType()
1523 || opType == Type::BoolTy);
Vikram S. Adve74825322002-03-18 03:15:35 +00001524 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001525 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001526 }
1527
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001528 case 23: // reg: ToUByteTy(reg)
1529 case 25: // reg: ToUShortTy(reg)
1530 case 27: // reg: ToUIntTy(reg)
1531 case 29: // reg: ToULongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001532 {
1533 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001534 assert(opType->isIntegral() ||
1535 opType->isPointerType() ||
1536 opType == Type::BoolTy && "Cast is illegal for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00001537 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001538 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001539 }
1540
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001541 case 24: // reg: ToSByteTy(reg)
1542 case 26: // reg: ToShortTy(reg)
1543 case 28: // reg: ToIntTy(reg)
1544 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001545 {
1546 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
1547 if (opType->isIntegral()
1548 || opType->isPointerType()
1549 || opType == Type::BoolTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001550 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001551 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001552 }
1553 else
1554 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001555 // If the source operand is an FP type, the int result must be
1556 // copied from float to int register via memory!
1557 Instruction *dest = subtreeRoot->getInstruction();
1558 Value* leftVal = subtreeRoot->leftChild()->getValue();
1559 Value* destForCast;
1560 vector<MachineInstr*> minstrVec;
1561
1562 if (opType == Type::FloatTy || opType == Type::DoubleTy)
1563 {
1564 // Create a temporary to represent the INT register
1565 // into which the FP value will be copied via memory.
1566 // The type of this temporary will determine the FP
1567 // register used: single-prec for a 32-bit int or smaller,
1568 // double-prec for a 64-bit int.
1569 //
1570 const Type* destTypeToUse =
1571 (dest->getType() == Type::LongTy)? Type::DoubleTy
1572 : Type::FloatTy;
Chris Lattner9c461082002-02-03 07:50:56 +00001573 destForCast = new TmpInstruction(destTypeToUse, leftVal);
1574 MachineCodeForInstruction &MCFI =
1575 MachineCodeForInstruction::get(dest);
1576 MCFI.addTemp(destForCast);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001577
1578 vector<TmpInstruction*> tempVec;
1579 target.getInstrInfo().CreateCodeToCopyFloatToInt(
1580 dest->getParent()->getParent(),
1581 (TmpInstruction*) destForCast, dest,
1582 minstrVec, tempVec, target);
1583
1584 for (unsigned i=0; i < tempVec.size(); ++i)
Chris Lattner9c461082002-02-03 07:50:56 +00001585 MCFI.addTemp(tempVec[i]);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001586 }
1587 else
1588 destForCast = leftVal;
1589
1590 MachineOpCode opCode=ChooseConvertToIntInstr(subtreeRoot, opType);
1591 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
1592
Vikram S. Adve74825322002-03-18 03:15:35 +00001593 M = new MachineInstr(opCode);
1594 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1595 leftVal);
1596 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1597 destForCast);
1598 mvec.push_back(M);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001599
Vikram S. Adve74825322002-03-18 03:15:35 +00001600 // Append the copy code, if any, after the conversion instr.
1601 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001602 }
1603 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001604 }
1605
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001606 case 31: // reg: ToFloatTy(reg):
1607 case 32: // reg: ToDoubleTy(reg):
1608 case 232: // reg: ToDoubleTy(Constant):
1609
1610 // If this instruction has a parent (a user) in the tree
1611 // and the user is translated as an FsMULd instruction,
1612 // then the cast is unnecessary. So check that first.
1613 // In the future, we'll want to do the same for the FdMULq instruction,
1614 // so do the check here instead of only for ToFloatTy(reg).
1615 //
1616 if (subtreeRoot->parent() != NULL &&
Chris Lattner9c461082002-02-03 07:50:56 +00001617 MachineCodeForInstruction::get(((InstructionNode*)subtreeRoot->parent())->getInstruction())[0]->getOpCode() == FSMULD)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001618 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001619 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001620 }
1621 else
1622 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001623 Value* leftVal = subtreeRoot->leftChild()->getValue();
1624 const Type* opType = leftVal->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001625 MachineOpCode opCode=ChooseConvertToFloatInstr(subtreeRoot,opType);
1626 if (opCode == INVALID_OPCODE) // no conversion needed
1627 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001628 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001629 }
1630 else
1631 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001632 // If the source operand is a non-FP type it must be
1633 // first copied from int to float register via memory!
1634 Instruction *dest = subtreeRoot->getInstruction();
1635 Value* srcForCast;
1636 int n = 0;
1637 if (opType != Type::FloatTy && opType != Type::DoubleTy)
1638 {
1639 // Create a temporary to represent the FP register
1640 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001641 // The type of this temporary will determine the FP
1642 // register used: single-prec for a 32-bit int or smaller,
1643 // double-prec for a 64-bit int.
1644 //
1645 const Type* srcTypeToUse =
1646 (leftVal->getType() == Type::LongTy)? Type::DoubleTy
1647 : Type::FloatTy;
1648
Chris Lattner9c461082002-02-03 07:50:56 +00001649 srcForCast = new TmpInstruction(srcTypeToUse, dest);
1650 MachineCodeForInstruction &DestMCFI =
1651 MachineCodeForInstruction::get(dest);
1652 DestMCFI.addTemp(srcForCast);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001653
1654 vector<MachineInstr*> minstrVec;
1655 vector<TmpInstruction*> tempVec;
1656 target.getInstrInfo().CreateCodeToCopyIntToFloat(
1657 dest->getParent()->getParent(),
1658 leftVal, (TmpInstruction*) srcForCast,
1659 minstrVec, tempVec, target);
1660
Vikram S. Adve74825322002-03-18 03:15:35 +00001661 mvec.insert(mvec.end(), minstrVec.begin(),minstrVec.end());
1662
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001663 for (unsigned i=0; i < tempVec.size(); ++i)
Chris Lattner9c461082002-02-03 07:50:56 +00001664 DestMCFI.addTemp(tempVec[i]);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001665 }
1666 else
1667 srcForCast = leftVal;
1668
Vikram S. Adve74825322002-03-18 03:15:35 +00001669 M = new MachineInstr(opCode);
1670 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1671 srcForCast);
1672 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1673 dest);
1674 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001675 }
1676 }
1677 break;
1678
1679 case 19: // reg: ToArrayTy(reg):
1680 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001681 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001682 break;
1683
1684 case 233: // reg: Add(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001685 M = CreateAddConstInstruction(subtreeRoot);
1686 if (M != NULL)
1687 {
1688 mvec.push_back(M);
1689 break;
1690 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001691 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001692
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001693 case 33: // reg: Add(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001694 mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot)));
1695 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001696 break;
1697
1698 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001699 M = CreateSubConstInstruction(subtreeRoot);
1700 if (M != NULL)
1701 {
1702 mvec.push_back(M);
1703 break;
1704 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001705 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001706
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001707 case 34: // reg: Sub(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001708 mvec.push_back(new MachineInstr(ChooseSubInstructionByType(
1709 subtreeRoot->getInstruction()->getType())));
1710 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001711 break;
1712
1713 case 135: // reg: Mul(todouble, todouble)
1714 checkCast = true;
1715 // FALL THROUGH
1716
1717 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001718 {
1719 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1720 ? FSMULD
1721 : INVALID_MACHINE_OPCODE);
1722 CreateMulInstruction(target,
1723 subtreeRoot->leftChild()->getValue(),
1724 subtreeRoot->rightChild()->getValue(),
1725 subtreeRoot->getInstruction(),
1726 mvec, forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001727 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001728 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001729 case 335: // reg: Mul(todouble, todoubleConst)
1730 checkCast = true;
1731 // FALL THROUGH
1732
1733 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001734 {
1735 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1736 ? FSMULD
1737 : INVALID_MACHINE_OPCODE);
1738 CreateMulInstruction(target,
1739 subtreeRoot->leftChild()->getValue(),
1740 subtreeRoot->rightChild()->getValue(),
1741 subtreeRoot->getInstruction(),
1742 mvec, forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001743 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001744 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001745 case 236: // reg: Div(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001746 L = mvec.size();
1747 CreateDivConstInstruction(target, subtreeRoot, mvec);
1748 if (mvec.size() > L)
1749 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001750 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001751
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001752 case 36: // reg: Div(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001753 mvec.push_back(new MachineInstr(ChooseDivInstruction(target, subtreeRoot)));
1754 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001755 break;
1756
1757 case 37: // reg: Rem(reg, reg)
1758 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001759 {
1760 Instruction* remInstr = subtreeRoot->getInstruction();
1761
Chris Lattner9c461082002-02-03 07:50:56 +00001762 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001763 subtreeRoot->leftChild()->getValue(),
1764 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001765 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001766 quot,
1767 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001768 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001769
Vikram S. Adve74825322002-03-18 03:15:35 +00001770 M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1771 Set3OperandsFromInstr(M, subtreeRoot, target);
1772 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot);
1773 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001774
Vikram S. Adve74825322002-03-18 03:15:35 +00001775 M = new MachineInstr(ChooseMulInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001776 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001777 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,quot);
1778 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
Vikram S. Adve510eec72001-11-04 21:59:14 +00001779 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001780 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,prod);
1781 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001782
Vikram S. Adve74825322002-03-18 03:15:35 +00001783 M = new MachineInstr(ChooseSubInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001784 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001785 Set3OperandsFromInstr(M, subtreeRoot, target);
1786 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod);
1787 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001788
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001789 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001790 }
1791
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001792 case 38: // bool: And(bool, bool)
1793 case 238: // bool: And(bool, boolconst)
1794 case 338: // reg : BAnd(reg, reg)
1795 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001796 mvec.push_back(new MachineInstr(AND));
1797 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001798 break;
1799
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001800 case 138: // bool: And(bool, not)
1801 case 438: // bool: BAnd(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001802 mvec.push_back(new MachineInstr(ANDN));
1803 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001804 break;
1805
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001806 case 39: // bool: Or(bool, bool)
1807 case 239: // bool: Or(bool, boolconst)
1808 case 339: // reg : BOr(reg, reg)
1809 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001810 mvec.push_back(new MachineInstr(ORN));
1811 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001812 break;
1813
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001814 case 139: // bool: Or(bool, not)
1815 case 439: // bool: BOr(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001816 mvec.push_back(new MachineInstr(ORN));
1817 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001818 break;
1819
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001820 case 40: // bool: Xor(bool, bool)
1821 case 240: // bool: Xor(bool, boolconst)
1822 case 340: // reg : BXor(reg, reg)
1823 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001824 mvec.push_back(new MachineInstr(XOR));
1825 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001826 break;
1827
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001828 case 140: // bool: Xor(bool, not)
1829 case 440: // bool: BXor(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001830 mvec.push_back(new MachineInstr(XNOR));
1831 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001832 break;
1833
1834 case 41: // boolconst: SetCC(reg, Constant)
1835 // Check if this is an integer comparison, and
1836 // there is a parent, and the parent decided to use
1837 // a branch-on-integer-register instead of branch-on-condition-code.
1838 // If so, the SUBcc instruction is not required.
1839 // (However, we must still check for constants to be loaded from
1840 // the constant pool so that such a load can be associated with
1841 // this instruction.)
1842 //
1843 // Otherwise this is just the same as case 42, so just fall through.
1844 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001845 if ((subtreeRoot->leftChild()->getValue()->getType()->isIntegral() ||
1846 subtreeRoot->leftChild()->getValue()->getType()->isPointerType())
1847 && subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001848 {
1849 InstructionNode* parent = (InstructionNode*) subtreeRoot->parent();
1850 assert(parent->getNodeType() == InstrTreeNode::NTInstructionNode);
Chris Lattner9c461082002-02-03 07:50:56 +00001851 const MachineCodeForInstruction &minstrVec =
1852 MachineCodeForInstruction::get(parent->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001853 MachineOpCode parentOpCode;
1854 if (parent->getInstruction()->getOpcode() == Instruction::Br &&
1855 (parentOpCode = minstrVec[0]->getOpCode()) >= BRZ &&
1856 parentOpCode <= BRGEZ)
1857 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001858 break; // don't forward the operand!
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001859 }
1860 }
1861 // ELSE FALL THROUGH
1862
1863 case 42: // bool: SetCC(reg, reg):
1864 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001865 // This generates a SUBCC instruction, putting the difference in
1866 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001867 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001868 // If the boolean result of the SetCC is used by anything other
1869 // than a single branch instruction, the boolean must be
1870 // computed and stored in the result register. Otherwise, discard
1871 // the difference (by using %g0) and keep only the condition code.
1872 //
1873 // To compute the boolean result in a register we use a conditional
1874 // move, unless the result of the SUBCC instruction can be used as
1875 // the bool! This assumes that zero is FALSE and any non-zero
1876 // integer is TRUE.
1877 //
1878 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1879 Instruction* setCCInstr = subtreeRoot->getInstruction();
1880 bool keepBoolVal = (parentNode == NULL ||
1881 parentNode->getInstruction()->getOpcode()
1882 != Instruction::Br);
1883 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001884 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1885 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1886
1887 bool mustClearReg;
1888 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001889 MachineOpCode movOpCode = 0;
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001890
1891 // Mark the 4th operand as being a CC register, and as a def
1892 // A TmpInstruction is created to represent the CC "result".
1893 // Unlike other instances of TmpInstruction, this one is used
1894 // by machine code of multiple LLVM instructions, viz.,
1895 // the SetCC and the branch. Make sure to get the same one!
1896 // Note that we do this even for FP CC registers even though they
1897 // are explicit operands, because the type of the operand
1898 // needs to be a floating point condition code, not an integer
1899 // condition code. Think of this as casting the bool result to
1900 // a FP condition code register.
1901 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001902 Value* leftVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001903 bool isFPCompare = (leftVal->getType() == Type::FloatTy ||
1904 leftVal->getType() == Type::DoubleTy);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001905
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001906 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1907 setCCInstr->getParent()->getParent(),
1908 isFPCompare? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001909 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001910
1911 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001912 {
1913 // Integer condition: dest. should be %g0 or an integer register.
1914 // If result must be saved but condition is not SetEQ then we need
1915 // a separate instruction to compute the bool result, so discard
1916 // result of SUBcc instruction anyway.
1917 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001918 M = new MachineInstr(SUBcc);
1919 Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal);
1920 M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister,
1921 tmpForCC, /*def*/true);
1922 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001923
1924 if (computeBoolVal)
1925 { // recompute bool using the integer condition codes
1926 movOpCode =
1927 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1928 }
1929 }
1930 else
1931 {
1932 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve74825322002-03-18 03:15:35 +00001933 M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1934 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001935 tmpForCC);
Vikram S. Adve74825322002-03-18 03:15:35 +00001936 M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001937 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001938 M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001939 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001940 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001941
1942 if (computeBoolVal)
1943 {// recompute bool using the FP condition codes
1944 mustClearReg = true;
1945 valueToMove = 1;
1946 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1947 }
1948 }
1949
1950 if (computeBoolVal)
1951 {
1952 if (mustClearReg)
1953 {// Unconditionally set register to 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001954 M = new MachineInstr(SETHI);
1955 M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed,
1956 (int64_t)0);
1957 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1958 setCCInstr);
1959 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001960 }
1961
1962 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve74825322002-03-18 03:15:35 +00001963 M = new MachineInstr(movOpCode);
1964 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1965 tmpForCC);
1966 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
1967 valueToMove);
1968 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1969 setCCInstr);
1970 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001971 }
1972 break;
1973 }
1974
1975 case 43: // boolreg: VReg
1976 case 44: // boolreg: Constant
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001977 break;
1978
1979 case 51: // reg: Load(reg)
1980 case 52: // reg: Load(ptrreg)
1981 case 53: // reg: LoadIdx(reg,reg)
1982 case 54: // reg: LoadIdx(ptrreg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001983 mvec.push_back(new MachineInstr(ChooseLoadInstruction(
1984 subtreeRoot->getValue()->getType())));
1985 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001986 break;
1987
1988 case 55: // reg: GetElemPtr(reg)
1989 case 56: // reg: GetElemPtrIdx(reg,reg)
1990 if (subtreeRoot->parent() != NULL)
1991 {
Vikram S. Adve671b16d2001-11-10 01:05:26 +00001992 // If the parent was a memory operation and not an array access,
1993 // the parent will fold this instruction in so generate nothing.
1994 //
1995 Instruction* parent =
1996 cast<Instruction>(subtreeRoot->parent()->getValue());
1997 if (parent->getOpcode() == Instruction::Load ||
1998 parent->getOpcode() == Instruction::Store ||
1999 parent->getOpcode() == Instruction::GetElementPtr)
2000 {
2001 // Check if the parent is an array access,
2002 // If so, we still need to generate this instruction.
2003 GetElementPtrInst* getElemInst =
2004 cast<GetElementPtrInst>(subtreeRoot->getInstruction());
2005 const PointerType* ptrType =
Chris Lattner65ea1712001-11-14 11:27:58 +00002006 cast<PointerType>(getElemInst->getPointerOperand()->getType());
Chris Lattner7a176752001-12-04 00:03:30 +00002007 if (! ptrType->getElementType()->isArrayType())
Vikram S. Adve671b16d2001-11-10 01:05:26 +00002008 {// we don't need a separate instr
Vikram S. Adve74825322002-03-18 03:15:35 +00002009 break; // don't forward operand!
Vikram S. Adve671b16d2001-11-10 01:05:26 +00002010 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002011 }
2012 }
2013 // else in all other cases we need to a separate ADD instruction
Vikram S. Adve74825322002-03-18 03:15:35 +00002014 mvec.push_back(new MachineInstr(ADD));
2015 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002016 break;
2017
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002018 case 57: // reg: Alloca: Implement as 1 instruction:
2019 { // add %fp, offsetFromFP -> result
Vikram S. Adve74825322002-03-18 03:15:35 +00002020 AllocationInst* instr = cast<AllocationInst>(subtreeRoot->getInstruction());
2021 unsigned int tsize =target.findOptimalStorageSize(instr->getAllocatedType());
2022 assert(tsize != 0);
2023 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002024 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002025 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002026
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002027 case 58: // reg: Alloca(reg): Implement as 3 instructions:
2028 // mul num, typeSz -> tmp
2029 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002030 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Adve74825322002-03-18 03:15:35 +00002031 AllocationInst* instr = cast<AllocationInst>(subtreeRoot->getInstruction());
2032 const Type* eltType = instr->getAllocatedType();
2033
2034 // If the #elements is a constant, use simpler code for fixed-size allocas
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002035 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Adve74825322002-03-18 03:15:35 +00002036 if (isa<Constant>(instr->getArraySize()))
2037 // total size is constant: generate code for fixed-size alloca
2038 CreateCodeForFixedSizeAlloca(target, instr, tsize,
2039 cast<ConstantUInt>(instr->getArraySize())->getValue(), mvec);
2040 else // total size is not constant.
2041 CreateCodeForVariableSizeAlloca(target, instr, tsize,
2042 instr->getArraySize(), mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002043 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002044 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002045
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002046 case 61: // reg: Call
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002047 { // Generate a call-indirect (i.e., jmpl) for now to expose
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002048 // the potential need for registers. If an absolute address
2049 // is available, replace this with a CALL instruction.
2050 // Mark both the indirection register and the return-address
2051 // register as hidden virtual registers.
Vikram S. Advea995e602001-10-11 04:23:19 +00002052 // Also, mark the operands of the Call and return value (if
2053 // any) as implicit operands of the CALL machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002054 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002055 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002056 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002057
Chris Lattner9c461082002-02-03 07:50:56 +00002058 Instruction* retAddrReg = new TmpInstruction(callInstr);
Vikram S. Adve8557b222001-10-10 20:56:33 +00002059
Vikram S. Advea995e602001-10-11 04:23:19 +00002060 // Note temporary values in the machineInstrVec for the VM instr.
Vikram S. Adve8557b222001-10-10 20:56:33 +00002061 //
2062 // WARNING: Operands 0..N-1 must go in slots 0..N-1 of implicitUses.
2063 // The result value must go in slot N. This is assumed
2064 // in register allocation.
2065 //
Chris Lattner9c461082002-02-03 07:50:56 +00002066 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002067
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002068
2069 // Generate the machine instruction and its operands.
2070 // Use CALL for direct function calls; this optimistically assumes
2071 // the PC-relative address fits in the CALL address field (22 bits).
2072 // Use JMPL for indirect calls.
2073 //
2074 if (callee->getValueType() == Value::MethodVal)
2075 { // direct function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002076 M = new MachineInstr(CALL);
2077 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
2078 callee);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002079 }
2080 else
2081 { // indirect function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002082 M = new MachineInstr(JMPLCALL);
2083 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
2084 callee);
2085 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
2086 (int64_t) 0);
2087 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
2088 retAddrReg);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002089 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002090
Vikram S. Adve74825322002-03-18 03:15:35 +00002091 mvec.push_back(M);
2092
Vikram S. Advea995e602001-10-11 04:23:19 +00002093 // Add the call operands and return value as implicit refs
2094 for (unsigned i=0, N=callInstr->getNumOperands(); i < N; ++i)
2095 if (callInstr->getOperand(i) != callee)
Vikram S. Adve74825322002-03-18 03:15:35 +00002096 mvec.back()->addImplicitRef(callInstr->getOperand(i));
Vikram S. Advea995e602001-10-11 04:23:19 +00002097
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002098 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002099 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002100
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002101 // For the CALL instruction, the ret. addr. reg. is also implicit
2102 if (callee->getValueType() == Value::MethodVal)
Vikram S. Adve74825322002-03-18 03:15:35 +00002103 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002104
Vikram S. Adve74825322002-03-18 03:15:35 +00002105 // delay slot
2106 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002107 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002108 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002109
2110 case 62: // reg: Shl(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002111 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002112 assert(opType->isIntegral()
2113 || opType == Type::BoolTy
2114 || opType->isPointerType()&& "Shl unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002115 mvec.push_back(new MachineInstr((opType == Type::LongTy)? SLLX : SLL));
2116 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002117 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002118 }
2119
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002120 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002121 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002122 assert(opType->isIntegral()
2123 || opType == Type::BoolTy
2124 || opType->isPointerType() &&"Shr unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002125 mvec.push_back(new MachineInstr((opType->isSigned()
2126 ? ((opType == Type::LongTy)? SRAX : SRA)
2127 : ((opType == Type::LongTy)? SRLX : SRL))));
2128 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002129 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002130 }
2131
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002132 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002133 break; // don't forward the value
2134
Vikram S. Adve3438b212001-11-12 18:54:11 +00002135#undef NEED_PHI_MACHINE_INSTRS
2136#ifdef NEED_PHI_MACHINE_INSTRS
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002137 { // This instruction has variable #operands, so resultPos is 0.
2138 Instruction* phi = subtreeRoot->getInstruction();
Vikram S. Adve74825322002-03-18 03:15:35 +00002139 M = new MachineInstr(PHI, 1 + phi->getNumOperands());
2140 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002141 subtreeRoot->getValue());
2142 for (unsigned i=0, N=phi->getNumOperands(); i < N; i++)
Vikram S. Adve74825322002-03-18 03:15:35 +00002143 M->SetMachineOperandVal(i+1, MachineOperand::MO_VirtualRegister,
2144 phi->getOperand(i));
2145 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002146 break;
2147 }
Chris Lattner697954c2002-01-20 22:54:45 +00002148#endif // NEED_PHI_MACHINE_INSTRS
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002149
Vikram S. Adve74825322002-03-18 03:15:35 +00002150
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002151 case 71: // reg: VReg
2152 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002153 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002154
2155 default:
2156 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002157 break;
2158 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002159 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002160
2161 if (forwardOperandNum >= 0)
2162 { // We did not generate a machine instruction but need to use operand.
2163 // If user is in the same tree, replace Value in its machine operand.
2164 // If not, insert a copy instruction which should get coalesced away
2165 // by register allocation.
2166 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002167 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002168 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002169 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002170 vector<MachineInstr*> minstrVec;
Vikram S. Adve74825322002-03-18 03:15:35 +00002171 target.getInstrInfo().CreateCopyInstructionsByType(target,
2172 subtreeRoot->getInstruction()->getParent()->getParent(),
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002173 subtreeRoot->getInstruction()->getOperand(forwardOperandNum),
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002174 subtreeRoot->getInstruction(), minstrVec);
2175 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002176 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002177 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002178 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002179}
2180
2181