blob: 2ecae9e211742592354455fd79b8d9e8563c4183 [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,
Vikram S. Advefd3900a2002-03-24 03:33:02 +000040 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.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000266 if (opType == Type::SByteTy || opType == Type::UByteTy ||
267 opType == Type::ShortTy || opType == Type::UShortTy ||
268 opType == Type::IntTy || opType == Type::UIntTy)
Vikram S. Adve74825322002-03-18 03:15:35 +0000269 opCode = FITOD;
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000270 else if (opType == Type::LongTy || opType == Type::ULongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000271 opCode = FXTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000272 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000273 opCode = FSTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000274 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000275 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000276 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000277 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000278 break;
279
280 default:
281 break;
282 }
283
284 return opCode;
285}
286
287static inline MachineOpCode
288ChooseConvertToIntInstr(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000289 const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000290{
291 MachineOpCode opCode = INVALID_OPCODE;;
292
293 int instrType = (int) instrNode->getOpLabel();
294
295 if (instrType == ToSByteTy || instrType == ToShortTy || instrType == ToIntTy)
296 {
297 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000298 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000299 case Type::FloatTyID: opCode = FSTOI; break;
300 case Type::DoubleTyID: opCode = FDTOI; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000301 default:
302 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
303 break;
304 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000305 }
306 else if (instrType == ToLongTy)
307 {
308 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000309 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000310 case Type::FloatTyID: opCode = FSTOX; break;
311 case Type::DoubleTyID: opCode = FDTOX; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000312 default:
313 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
314 break;
315 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000316 }
317 else
318 assert(0 && "Should not get here, Mo!");
319
320 return opCode;
321}
322
323
324static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000325ChooseAddInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000326{
327 MachineOpCode opCode = INVALID_OPCODE;
328
Chris Lattner20b1ea02001-09-14 03:47:57 +0000329 if (resultType->isIntegral() ||
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000330 resultType->isPointerType() ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000331 resultType->isLabelType() ||
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000332 isa<MethodType>(resultType) ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000333 resultType == Type::BoolTy)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000334 {
335 opCode = ADD;
336 }
337 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000338 switch(resultType->getPrimitiveID())
339 {
340 case Type::FloatTyID: opCode = FADDS; break;
341 case Type::DoubleTyID: opCode = FADDD; break;
342 default: assert(0 && "Invalid type for ADD instruction"); break;
343 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000344
345 return opCode;
346}
347
348
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000349static inline MachineOpCode
350ChooseAddInstruction(const InstructionNode* instrNode)
351{
352 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
353}
354
355
Chris Lattner20b1ea02001-09-14 03:47:57 +0000356static inline MachineInstr*
357CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000358 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000359{
360 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000361 ? FMOVS : FMOVD);
Vikram S. Adve74825322002-03-18 03:15:35 +0000362 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
363 instrNode->leftChild()->getValue());
364 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
365 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000366 return minstr;
367}
368
369static inline MachineInstr*
370CreateAddConstInstruction(const InstructionNode* instrNode)
371{
372 MachineInstr* minstr = NULL;
373
374 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000375 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000376
377 // Cases worth optimizing are:
378 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
379 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
380 //
381 const Type* resultType = instrNode->getInstruction()->getType();
382
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000383 if (resultType == Type::FloatTy ||
384 resultType == Type::DoubleTy)
385 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000386 double dval = cast<ConstantFP>(constOp)->getValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000387 if (dval == 0.0)
388 minstr = CreateMovFloatInstruction(instrNode, resultType);
389 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000390
391 return minstr;
392}
393
394
395static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000396ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000397{
398 MachineOpCode opCode = INVALID_OPCODE;
399
Chris Lattner20b1ea02001-09-14 03:47:57 +0000400 if (resultType->isIntegral() ||
401 resultType->isPointerType())
402 {
403 opCode = SUB;
404 }
405 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000406 switch(resultType->getPrimitiveID())
407 {
408 case Type::FloatTyID: opCode = FSUBS; break;
409 case Type::DoubleTyID: opCode = FSUBD; break;
410 default: assert(0 && "Invalid type for SUB instruction"); break;
411 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000412
413 return opCode;
414}
415
416
417static inline MachineInstr*
418CreateSubConstInstruction(const InstructionNode* instrNode)
419{
420 MachineInstr* minstr = NULL;
421
422 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000423 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000424
425 // Cases worth optimizing are:
426 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
427 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
428 //
429 const Type* resultType = instrNode->getInstruction()->getType();
430
431 if (resultType == Type::FloatTy ||
432 resultType == Type::DoubleTy)
433 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000434 double dval = cast<ConstantFP>(constOp)->getValue();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000435 if (dval == 0.0)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000436 minstr = CreateMovFloatInstruction(instrNode, resultType);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000437 }
438
439 return minstr;
440}
441
442
443static inline MachineOpCode
444ChooseFcmpInstruction(const InstructionNode* instrNode)
445{
446 MachineOpCode opCode = INVALID_OPCODE;
447
448 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
449 switch(operand->getType()->getPrimitiveID()) {
450 case Type::FloatTyID: opCode = FCMPS; break;
451 case Type::DoubleTyID: opCode = FCMPD; break;
452 default: assert(0 && "Invalid type for FCMP instruction"); break;
453 }
454
455 return opCode;
456}
457
458
459// Assumes that leftArg and rightArg are both cast instructions.
460//
461static inline bool
462BothFloatToDouble(const InstructionNode* instrNode)
463{
464 InstrTreeNode* leftArg = instrNode->leftChild();
465 InstrTreeNode* rightArg = instrNode->rightChild();
466 InstrTreeNode* leftArgArg = leftArg->leftChild();
467 InstrTreeNode* rightArgArg = rightArg->leftChild();
468 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
469
470 // Check if both arguments are floats cast to double
471 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000472 leftArgArg->getValue()->getType() == Type::FloatTy &&
473 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000474}
475
476
477static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000478ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000479{
480 MachineOpCode opCode = INVALID_OPCODE;
481
Chris Lattner20b1ea02001-09-14 03:47:57 +0000482 if (resultType->isIntegral())
Vikram S. Adve510eec72001-11-04 21:59:14 +0000483 opCode = MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000484 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000485 switch(resultType->getPrimitiveID())
486 {
487 case Type::FloatTyID: opCode = FMULS; break;
488 case Type::DoubleTyID: opCode = FMULD; break;
489 default: assert(0 && "Invalid type for MUL instruction"); break;
490 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000491
492 return opCode;
493}
494
495
Vikram S. Adve510eec72001-11-04 21:59:14 +0000496
Chris Lattner20b1ea02001-09-14 03:47:57 +0000497static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000498CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000499 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000500{
501 MachineInstr* minstr = new MachineInstr(SUB);
Vikram S. Adve74825322002-03-18 03:15:35 +0000502 minstr->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
503 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, vreg);
504 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000505 return minstr;
506}
507
508
Vikram S. Adve74825322002-03-18 03:15:35 +0000509// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000510// create a cheaper instruction.
511// This returns the approximate cost of the instructions generated,
512// which is used to pick the cheapest when both operands are constant.
513static inline unsigned int
Vikram S. Adve74825322002-03-18 03:15:35 +0000514CreateMulConstInstruction(const TargetMachine &target,
515 Value* lval, Value* rval, Value* destVal,
516 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000517{
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000518 /* An integer multiply is generally more costly than FP multiply */
519 unsigned int cost = target.getInstrInfo().minLatency(MULX);
Vikram S. Adve74825322002-03-18 03:15:35 +0000520 MachineInstr* minstr1 = NULL;
521 MachineInstr* minstr2 = NULL;
522
523 Value* constOp = rval;
524 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000525 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000526
527 // Cases worth optimizing are:
528 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
529 // (2) Multiply by 2^x for integer types: replace with Shift
530 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000531 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000532
Vikram S. Adve243dd452001-09-18 13:03:13 +0000533 if (resultType->isIntegral() || resultType->isPointerType())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000534 {
535 unsigned pow;
536 bool isValidConst;
537 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
538 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000539 {
540 bool needNeg = false;
541 if (C < 0)
542 {
543 needNeg = true;
544 C = -C;
545 }
546
547 if (C == 0 || C == 1)
548 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000549 cost = target.getInstrInfo().minLatency(ADD);
Vikram S. Adve74825322002-03-18 03:15:35 +0000550 minstr1 = new MachineInstr(ADD);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000551 if (C == 0)
Vikram S. Adve74825322002-03-18 03:15:35 +0000552 minstr1->SetMachineOperandReg(0,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000553 target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000554 else
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000555 minstr1->SetMachineOperandVal(0,
556 MachineOperand::MO_VirtualRegister, lval);
557 minstr1->SetMachineOperandReg(1,
558 target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000559 }
560 else if (IsPowerOf2(C, pow))
561 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000562 minstr1 = new MachineInstr((resultType == Type::LongTy)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000563 ? SLLX : SLL);
564 minstr1->SetMachineOperandVal(0,
565 MachineOperand::MO_VirtualRegister, lval);
566 minstr1->SetMachineOperandConst(1,
567 MachineOperand::MO_UnextendedImmed, pow);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000568 }
569
Vikram S. Adve74825322002-03-18 03:15:35 +0000570 if (minstr1 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000571 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve74825322002-03-18 03:15:35 +0000572 minstr2 = CreateIntNegInstruction(target, destVal);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000573 cost += target.getInstrInfo().minLatency(minstr2->getOpCode());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000574 }
575 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000576 }
577 else
578 {
579 if (resultType == Type::FloatTy ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000580 resultType == Type::DoubleTy)
581 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000582 double dval = cast<ConstantFP>(constOp)->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000583 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000584 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000585 bool needNeg = (dval < 0);
586
587 MachineOpCode opCode = needNeg
588 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
589 : (resultType == Type::FloatTy? FMOVS : FMOVD);
590
Vikram S. Adve74825322002-03-18 03:15:35 +0000591 minstr1 = new MachineInstr(opCode);
592 minstr1->SetMachineOperandVal(0,
593 MachineOperand::MO_VirtualRegister,
594 lval);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000595 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000596 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000597 }
598
Vikram S. Adve74825322002-03-18 03:15:35 +0000599 if (minstr1 != NULL)
600 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
601 destVal);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000602
Vikram S. Adve74825322002-03-18 03:15:35 +0000603 if (minstr1)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000604 {
605 mvec.push_back(minstr1);
606 cost = target.getInstrInfo().minLatency(minstr1->getOpCode());
607 }
Vikram S. Adve74825322002-03-18 03:15:35 +0000608 if (minstr2)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000609 {
610 assert(minstr1 && "Otherwise cost needs to be initialized to 0");
611 cost += target.getInstrInfo().minLatency(minstr2->getOpCode());
612 mvec.push_back(minstr2);
613 }
614
615 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000616}
617
618
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000619// Does not create any instructions if we cannot exploit constant to
620// create a cheaper instruction.
621//
622static inline void
623CreateCheapestMulConstInstruction(const TargetMachine &target,
624 Value* lval, Value* rval, Value* destVal,
625 vector<MachineInstr*>& mvec)
626{
627 Value* constOp;
628 if (isa<Constant>(lval) && isa<Constant>(rval))
629 { // both operands are constant: try both orders!
630 vector<MachineInstr*> mvec1, mvec2;
631 unsigned int lcost = CreateMulConstInstruction(target, lval, rval,
632 destVal, mvec1);
633 unsigned int rcost = CreateMulConstInstruction(target, rval, lval,
634 destVal, mvec2);
635 vector<MachineInstr*>& mincostMvec = (lcost <= rcost)? mvec1 : mvec2;
636 vector<MachineInstr*>& maxcostMvec = (lcost <= rcost)? mvec2 : mvec1;
637 mvec.insert(mvec.end(), mincostMvec.begin(), mincostMvec.end());
638
639 for (unsigned int i=0; i < maxcostMvec.size(); ++i)
640 delete maxcostMvec[i];
641 }
642 else if (isa<Constant>(rval)) // rval is constant, but not lval
643 CreateMulConstInstruction(target, lval, rval, destVal, mvec);
644 else if (isa<Constant>(lval)) // lval is constant, but not rval
645 CreateMulConstInstruction(target, lval, rval, destVal, mvec);
646
647 // else neither is constant
648 return;
649}
650
Vikram S. Adve74825322002-03-18 03:15:35 +0000651// Return NULL if we cannot exploit constant to create a cheaper instruction
652static inline void
653CreateMulInstruction(const TargetMachine &target,
654 Value* lval, Value* rval, Value* destVal,
655 vector<MachineInstr*>& mvec,
656 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
657{
658 unsigned int L = mvec.size();
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000659 CreateCheapestMulConstInstruction(target, lval, rval, destVal, mvec);
Vikram S. Adve74825322002-03-18 03:15:35 +0000660 if (mvec.size() == L)
661 { // no instructions were added so create MUL reg, reg, reg.
662 // Use FSMULD if both operands are actually floats cast to doubles.
663 // Otherwise, use the default opcode for the appropriate type.
664 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
665 ? forceMulOp
666 : ChooseMulInstructionByType(destVal->getType()));
667 MachineInstr* M = new MachineInstr(mulOp);
668 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, lval);
669 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, rval);
670 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, destVal);
671 mvec.push_back(M);
672 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000673}
674
675
Vikram S. Adve510eec72001-11-04 21:59:14 +0000676// Generate a divide instruction for Div or Rem.
677// For Rem, this assumes that the operand type will be signed if the result
678// type is signed. This is correct because they must have the same sign.
679//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000680static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000681ChooseDivInstruction(TargetMachine &target,
682 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000683{
684 MachineOpCode opCode = INVALID_OPCODE;
685
686 const Type* resultType = instrNode->getInstruction()->getType();
687
688 if (resultType->isIntegral())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000689 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000690 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000691 switch(resultType->getPrimitiveID())
692 {
693 case Type::FloatTyID: opCode = FDIVS; break;
694 case Type::DoubleTyID: opCode = FDIVD; break;
695 default: assert(0 && "Invalid type for DIV instruction"); break;
696 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000697
698 return opCode;
699}
700
701
Vikram S. Adve74825322002-03-18 03:15:35 +0000702// Return NULL if we cannot exploit constant to create a cheaper instruction
703static inline void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000704CreateDivConstInstruction(TargetMachine &target,
705 const InstructionNode* instrNode,
Vikram S. Adve74825322002-03-18 03:15:35 +0000706 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000707{
Vikram S. Adve74825322002-03-18 03:15:35 +0000708 MachineInstr* minstr1 = NULL;
709 MachineInstr* minstr2 = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000710
711 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Vikram S. Adve74825322002-03-18 03:15:35 +0000712 if (! isa<Constant>(constOp))
713 return;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000714
715 // Cases worth optimizing are:
716 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
717 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
718 //
719 const Type* resultType = instrNode->getInstruction()->getType();
720
721 if (resultType->isIntegral())
722 {
723 unsigned pow;
724 bool isValidConst;
725 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
726 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000727 {
728 bool needNeg = false;
729 if (C < 0)
730 {
731 needNeg = true;
732 C = -C;
733 }
734
735 if (C == 1)
736 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000737 minstr1 = new MachineInstr(ADD);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000738 minstr1->SetMachineOperandVal(0,
739 MachineOperand::MO_VirtualRegister,
740 instrNode->leftChild()->getValue());
741 minstr1->SetMachineOperandReg(1,
742 target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000743 }
744 else if (IsPowerOf2(C, pow))
745 {
746 MachineOpCode opCode= ((resultType->isSigned())
747 ? (resultType==Type::LongTy)? SRAX : SRA
748 : (resultType==Type::LongTy)? SRLX : SRL);
Vikram S. Adve74825322002-03-18 03:15:35 +0000749 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000750 minstr1->SetMachineOperandVal(0,
751 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000752 instrNode->leftChild()->getValue());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000753 minstr1->SetMachineOperandConst(1,
754 MachineOperand::MO_UnextendedImmed,
755 pow);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000756 }
757
Vikram S. Adve74825322002-03-18 03:15:35 +0000758 if (minstr1 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000759 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve74825322002-03-18 03:15:35 +0000760 minstr2 = CreateIntNegInstruction(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000761 instrNode->getValue());
762 }
763 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000764 }
765 else
766 {
767 if (resultType == Type::FloatTy ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000768 resultType == Type::DoubleTy)
769 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000770 double dval = cast<ConstantFP>(constOp)->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000771 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000772 {
773 bool needNeg = (dval < 0);
774
775 MachineOpCode opCode = needNeg
776 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
777 : (resultType == Type::FloatTy? FMOVS : FMOVD);
778
Vikram S. Adve74825322002-03-18 03:15:35 +0000779 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000780 minstr1->SetMachineOperandVal(0,
781 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000782 instrNode->leftChild()->getValue());
783 }
784 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000785 }
786
Vikram S. Adve74825322002-03-18 03:15:35 +0000787 if (minstr1 != NULL)
788 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
789 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000790
Vikram S. Adve74825322002-03-18 03:15:35 +0000791 if (minstr1)
792 mvec.push_back(minstr1);
793 if (minstr2)
794 mvec.push_back(minstr2);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000795}
796
797
Vikram S. Adve74825322002-03-18 03:15:35 +0000798static void
799CreateCodeForVariableSizeAlloca(const TargetMachine& target,
800 Instruction* result,
801 unsigned int tsize,
802 Value* numElementsVal,
803 vector<MachineInstr*>& getMvec)
804{
805 MachineInstr* M;
806
807 // Create a Value to hold the (constant) element size
808 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
809
810 // Get the constant offset from SP for dynamically allocated storage
811 // and create a temporary Value to hold it.
812 assert(result && result->getParent() && "Result value is not part of a method?");
813 Method* method = result->getParent()->getParent();
814 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(method);
815 bool growUp;
816 ConstantSInt* dynamicAreaOffset =
817 ConstantSInt::get(Type::IntTy,
818 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
819 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
820
821 // Create a temporary value to hold the result of MUL
822 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
823 MachineCodeForInstruction::get(result).addTemp(tmpProd);
824
825 // Instruction 1: mul numElements, typeSize -> tmpProd
826 M = new MachineInstr(MULX);
827 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, numElementsVal);
828 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tsizeVal);
829 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, tmpProd);
830 getMvec.push_back(M);
831
832 // Instruction 2: sub %sp, tmpProd -> %sp
833 M = new MachineInstr(SUB);
834 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
835 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tmpProd);
836 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
837 getMvec.push_back(M);
838
839 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
840 M = new MachineInstr(ADD);
841 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
842 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, dynamicAreaOffset);
843 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
844 getMvec.push_back(M);
845}
846
847
848static void
849CreateCodeForFixedSizeAlloca(const TargetMachine& target,
850 Instruction* result,
851 unsigned int tsize,
852 unsigned int numElements,
853 vector<MachineInstr*>& getMvec)
854{
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000855 assert(result && result->getParent() &&
856 "Result value is not part of a method?");
Vikram S. Adve74825322002-03-18 03:15:35 +0000857 Method* method = result->getParent()->getParent();
858 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(method);
859
860 // Check if the offset would small enough to use as an immediate in load/stores
861 // (check LDX because all load/stores have the same-size immediate field).
862 // If not, put the variable in the dynamically sized area of the frame.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000863 unsigned int paddedSizeIgnored;
Vikram S. Adve74825322002-03-18 03:15:35 +0000864 int offsetFromFP = mcInfo.computeOffsetforLocalVar(target, result,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000865 paddedSizeIgnored,
Vikram S. Adve74825322002-03-18 03:15:35 +0000866 tsize * numElements);
867 if (! target.getInstrInfo().constantFitsInImmedField(LDX, offsetFromFP))
868 {
869 CreateCodeForVariableSizeAlloca(target, result, tsize,
870 ConstantSInt::get(Type::IntTy,numElements),
871 getMvec);
872 return;
873 }
874
875 // else offset fits in immediate field so go ahead and allocate it.
876 offsetFromFP = mcInfo.allocateLocalVar(target, result, tsize * numElements);
877
878 // Create a temporary Value to hold the constant offset.
879 // This is needed because it may not fit in the immediate field.
880 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
881
882 // Instruction 1: add %fp, offsetFromFP -> result
883 MachineInstr* M = new MachineInstr(ADD);
884 M->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
885 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, offsetVal);
886 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
887
888 getMvec.push_back(M);
889}
890
891
892
Chris Lattner20b1ea02001-09-14 03:47:57 +0000893//------------------------------------------------------------------------
894// Function SetOperandsForMemInstr
895//
896// Choose addressing mode for the given load or store instruction.
897// Use [reg+reg] if it is an indexed reference, and the index offset is
898// not a constant or if it cannot fit in the offset field.
899// Use [reg+offset] in all other cases.
900//
901// This assumes that all array refs are "lowered" to one of these forms:
902// %x = load (subarray*) ptr, constant ; single constant offset
903// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
904// Generally, this should happen via strength reduction + LICM.
905// Also, strength reduction should take care of using the same register for
906// the loop index variable and an array index, when that is profitable.
907//------------------------------------------------------------------------
908
909static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000910SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
911 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000912 const InstructionNode* vmInstrNode,
913 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000914{
915 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
916
917 // Variables to hold the index vector, ptr value, and offset value.
918 // The major work here is to extract these for all 3 instruction types
919 // and then call the common function SetMemOperands_Internal().
920 //
Vikram S. Advefa248972001-12-15 00:36:32 +0000921 vector<Value*> idxVec;
Vikram S. Adve74825322002-03-18 03:15:35 +0000922 Value* ptrVal = memInst->getPointerOperand();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000923
924 // Test if a GetElemPtr instruction is being folded into this mem instrn.
925 // If so, it will be in the left child for Load and GetElemPtr,
926 // and in the right child for Store instructions.
927 //
928 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000929 ? vmInstrNode->rightChild()
930 : vmInstrNode->leftChild());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000931
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000932 // Fold chains of GetElemPtr instructions for structure references.
Vikram S. Adve74825322002-03-18 03:15:35 +0000933 //
934 if (isa<StructType>(cast<PointerType>(ptrVal->getType())->getElementType())
935 && (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
936 ptrChild->getOpLabel() == GetElemPtrIdx))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000937 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000938 Value* newPtr = FoldGetElemChain((InstructionNode*) ptrChild, idxVec);
939 if (newPtr)
940 ptrVal = newPtr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000941 }
942
Vikram S. Adve74825322002-03-18 03:15:35 +0000943 // Append the index vector of this instruction (may be none) to the indexes
944 // folded in previous getElementPtr's (may be none)
945 idxVec.insert(idxVec.end(), memInst->idx_begin(), memInst->idx_end());
946
947 SetMemOperands_Internal(mvec, mvecI, vmInstrNode, ptrVal, idxVec, target);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000948}
949
950
Vikram S. Adve74825322002-03-18 03:15:35 +0000951// Generate the correct operands (and additional instructions if needed)
952// for the given pointer and given index vector.
953//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000954static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000955SetMemOperands_Internal(vector<MachineInstr*>& mvec,
956 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000957 const InstructionNode* vmInstrNode,
958 Value* ptrVal,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000959 vector<Value*>& idxVec,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000960 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000961{
962 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
963
964 // Initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000965 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000966 Value* valueForRegOffset = NULL;
967 MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister;
968
Vikram S. Adve74825322002-03-18 03:15:35 +0000969 // Check if there is an index vector and if so, compute the
970 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +0000971 //
972 if (idxVec.size() > 0)
973 {
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000974 unsigned offset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000975
Vikram S. Adve74825322002-03-18 03:15:35 +0000976 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000977
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000978 // Handle special common case of leading [0] index.
979 bool firstIndexIsZero =
980 bool(isa<ConstantUInt>(idxVec.front()) &&
981 cast<ConstantUInt>(idxVec.front())->getValue() == 0);
982
983 // This is a real structure reference if the ptr target is a
984 // structure type, and the first offset is [0] (eliminate that offset).
985 if (firstIndexIsZero && ptrType->getElementType()->isStructType())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000986 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000987 // Compute the offset value using the index vector. Create a
988 // virtual reg. for it since it may not fit in the immed field.
989 assert(idxVec.size() >= 2);
990 idxVec.erase(idxVec.begin());
Vikram S. Adve74825322002-03-18 03:15:35 +0000991 unsigned offset = target.DataLayout.getIndexedOffset(ptrType,idxVec);
992 valueForRegOffset = ConstantSInt::get(Type::IntTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000993 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000994 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000995 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000996 // It is an array ref, and must have been lowered to a single offset.
Vikram S. Adve74825322002-03-18 03:15:35 +0000997 assert((memInst->getNumOperands()
998 == (unsigned) 1 + memInst->getFirstIndexOperandNumber())
999 && "Array refs must be lowered before Instruction Selection");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001000
Vikram S. Adve74825322002-03-18 03:15:35 +00001001 Value* arrayOffsetVal = * memInst->idx_begin();
1002
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001003 // If index is 0, the offset value is just 0. Otherwise,
1004 // generate a MUL instruction to compute address from index.
1005 // The call to getTypeSize() will fail if size is not constant.
1006 // CreateMulInstruction() folds constants intelligently enough.
1007 //
1008 if (firstIndexIsZero)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001009 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001010 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1011 smallConstOffset = 0;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001012 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001013 else
1014 {
1015 vector<MachineInstr*> mulVec;
1016 Instruction* addr = new TmpInstruction(Type::UIntTy, memInst);
1017 MachineCodeForInstruction::get(memInst).addTemp(addr);
1018
1019 unsigned int eltSize =
1020 target.DataLayout.getTypeSize(ptrType->getElementType());
1021 assert(eltSize > 0 && "Invalid or non-const array element size");
1022 ConstantUInt* eltVal = ConstantUInt::get(Type::UIntTy, eltSize);
1023
1024 CreateMulInstruction(target,
1025 arrayOffsetVal, /* lval, not likely const */
1026 eltVal, /* rval, likely constant */
1027 addr, /* result*/
1028 mulVec, INVALID_MACHINE_OPCODE);
1029 assert(mulVec.size() > 0 && "No multiply instruction created?");
1030 for (vector<MachineInstr*>::const_iterator I = mulVec.begin();
1031 I != mulVec.end(); ++I)
1032 {
1033 mvecI = mvec.insert(mvecI, *I); // ptr to inserted value
1034 ++mvecI; // ptr to mem. instr.
1035 }
1036
1037 valueForRegOffset = addr;
1038 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001039 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001040 }
1041 else
1042 {
1043 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1044 smallConstOffset = 0;
1045 }
1046
1047 // Operand 0 is value for STORE, ptr for LOAD or GET_ELEMENT_PTR
1048 // It is the left child in the instruction tree in all cases.
1049 Value* leftVal = vmInstrNode->leftChild()->getValue();
Vikram S. Adve74825322002-03-18 03:15:35 +00001050 (*mvecI)->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1051 leftVal);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001052
1053 // Operand 1 is ptr for STORE, offset for LOAD or GET_ELEMENT_PTR
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001054 // Operand 2 is offset for STORE, result reg for LOAD or GET_ELEMENT_PTR
Chris Lattner20b1ea02001-09-14 03:47:57 +00001055 //
1056 unsigned offsetOpNum = (memInst->getOpcode() == Instruction::Store)? 2 : 1;
1057 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1058 {
1059 assert(valueForRegOffset != NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001060 (*mvecI)->SetMachineOperandVal(offsetOpNum, offsetOpType,
1061 valueForRegOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001062 }
1063 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001064 (*mvecI)->SetMachineOperandConst(offsetOpNum, offsetOpType,
1065 smallConstOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001066
1067 if (memInst->getOpcode() == Instruction::Store)
Vikram S. Adve74825322002-03-18 03:15:35 +00001068 (*mvecI)->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1069 ptrVal);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001070 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001071 (*mvecI)->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1072 vmInstrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +00001073}
1074
1075
Chris Lattner20b1ea02001-09-14 03:47:57 +00001076//
1077// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001078// in place of the use(s) of that instruction in node `parent'.
1079// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001080// Also make sure to skip over a parent who:
1081// (1) is a list node in the Burg tree, or
1082// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001083//
1084static void
1085ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001086 InstrTreeNode* parent,
1087 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001088{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001089 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1090
Chris Lattner20b1ea02001-09-14 03:47:57 +00001091 Instruction* unusedOp = treeNode->getInstruction();
1092 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001093
1094 // The parent itself may be a list node, so find the real parent instruction
1095 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1096 {
1097 parent = parent->parent();
1098 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1099 }
1100 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1101
1102 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001103 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001104
1105 // The parent's mvec would be empty if it was itself forwarded.
1106 // Recursively call ForwardOperand in that case...
1107 //
1108 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001109 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001110 assert(parent->parent() != NULL &&
1111 "Parent could not have been forwarded, yet has no instructions?");
1112 ForwardOperand(treeNode, parent->parent(), operandNum);
1113 }
1114 else
1115 {
1116 bool fwdSuccessful = false;
1117 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001118 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001119 MachineInstr* minstr = mvec[i];
1120 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001121 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001122 const MachineOperand& mop = minstr->getOperand(i);
1123 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1124 mop.getVRegValue() == unusedOp)
1125 {
1126 minstr->SetMachineOperandVal(i,
1127 MachineOperand::MO_VirtualRegister, fwdOp);
1128 fwdSuccessful = true;
1129 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001130 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001131
1132 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1133 if (minstr->getImplicitRef(i) == unusedOp)
1134 {
1135 minstr->setImplicitRef(i, fwdOp,
1136 minstr->implicitRefIsDefined(i));
1137 fwdSuccessful = true;
1138 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001139 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001140 assert(fwdSuccessful && "Value to be forwarded is never used!");
Chris Lattner20b1ea02001-09-14 03:47:57 +00001141 }
1142}
1143
1144
Ruchira Sasanka67a463a2001-11-12 14:45:33 +00001145void UltraSparcInstrInfo::
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001146CreateCopyInstructionsByType(const TargetMachine& target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001147 Method* method,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001148 Value* src,
1149 Instruction* dest,
Ruchira Sasanka67a463a2001-11-12 14:45:33 +00001150 vector<MachineInstr*>& minstrVec) const
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001151{
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001152 bool loadConstantToReg = false;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001153
1154 const Type* resultType = dest->getType();
1155
1156 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
1157 if (opCode == INVALID_OPCODE)
1158 {
1159 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001160 return;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001161 }
1162
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001163 // if `src' is a constant that doesn't fit in the immed field or if it is
1164 // a global variable (i.e., a constant address), generate a load
1165 // instruction instead of an add
1166 //
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001167 if (isa<Constant>(src))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001168 {
1169 unsigned int machineRegNum;
1170 int64_t immedValue;
1171 MachineOperand::MachineOperandType opType =
1172 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
1173 machineRegNum, immedValue);
1174
1175 if (opType == MachineOperand::MO_VirtualRegister)
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001176 loadConstantToReg = true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001177 }
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001178 else if (isa<GlobalValue>(src))
1179 loadConstantToReg = true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001180
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001181 if (loadConstantToReg)
1182 { // `src' is constant and cannot fit in immed field for the ADD
1183 // Insert instructions to "load" the constant into a register
1184 vector<TmpInstruction*> tempVec;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001185 target.getInstrInfo().CreateCodeToLoadConst(method, src, dest,
1186 minstrVec,tempVec);
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001187 for (unsigned i=0; i < tempVec.size(); i++)
Chris Lattner9c461082002-02-03 07:50:56 +00001188 MachineCodeForInstruction::get(dest).addTemp(tempVec[i]);
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001189 }
1190 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001191 { // Create an add-with-0 instruction of the appropriate type.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001192 // Make `src' the second operand, in case it is a constant
1193 // Use (unsigned long) 0 for a NULL pointer value.
1194 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001195 const Type* zeroValueType =
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001196 (resultType->getPrimitiveID() == Type::PointerTyID)? Type::ULongTy
1197 : resultType;
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001198 MachineInstr* minstr = new MachineInstr(opCode);
Vikram S. Adve74825322002-03-18 03:15:35 +00001199 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1200 Constant::getNullConstant(zeroValueType));
1201 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, src);
1202 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,dest);
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001203 minstrVec.push_back(minstr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001204 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001205}
1206
1207
Ruchira Sasanka67a463a2001-11-12 14:45:33 +00001208
Vikram S. Advefb361122001-10-22 13:36:31 +00001209//******************* Externally Visible Functions *************************/
1210
1211
1212//------------------------------------------------------------------------
1213// External Function: GetInstructionsForProlog
1214// External Function: GetInstructionsForEpilog
1215//
1216// Purpose:
1217// Create prolog and epilog code for procedure entry and exit
1218//------------------------------------------------------------------------
1219
1220extern unsigned
1221GetInstructionsForProlog(BasicBlock* entryBB,
1222 TargetMachine &target,
1223 MachineInstr** mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001224{
Vikram S. Adve74825322002-03-18 03:15:35 +00001225 MachineInstr* M;
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001226 const MachineFrameInfo& frameInfo = target.getFrameInfo();
Vikram S. Adve74825322002-03-18 03:15:35 +00001227 unsigned int N = 0;
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001228
Vikram S. Advefb361122001-10-22 13:36:31 +00001229 // The second operand is the stack size. If it does not fit in the
Vikram S. Adve74825322002-03-18 03:15:35 +00001230 // immediate field, we have to use a free register to hold the size.
1231 // We will assume that local register `l0' is unused since the SAVE
1232 // instruction must be the first instruction in each procedure.
1233 //
Vikram S. Advefb361122001-10-22 13:36:31 +00001234 Method* method = entryBB->getParent();
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001235 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(method);
1236 unsigned int staticStackSize = mcInfo.getStaticStackSize();
1237
1238 if (staticStackSize < (unsigned) frameInfo.getMinStackFrameSize())
1239 staticStackSize = (unsigned) frameInfo.getMinStackFrameSize();
1240
1241 if (unsigned padsz = (staticStackSize %
1242 (unsigned) frameInfo.getStackFrameSizeAlignment()))
Vikram S. Advefd9b7dc2001-11-12 05:16:39 +00001243 staticStackSize += frameInfo.getStackFrameSizeAlignment() - padsz;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001244
Vikram S. Adve74825322002-03-18 03:15:35 +00001245 if (target.getInstrInfo().constantFitsInImmedField(SAVE, staticStackSize))
1246 {
1247 M = new MachineInstr(SAVE);
1248 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
1249 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
1250 - (int) staticStackSize);
1251 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
1252 mvec[N++] = M;
1253 }
1254 else
1255 {
1256 M = new MachineInstr(SETSW);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001257 M->SetMachineOperandConst(0, MachineOperand::MO_SignExtendedImmed,
1258 - (int) staticStackSize);
Vikram S. Adve74825322002-03-18 03:15:35 +00001259 M->SetMachineOperandReg(1, MachineOperand::MO_MachineRegister,
1260 target.getRegInfo().getUnifiedRegNum(
1261 target.getRegInfo().getRegClassIDOfType(Type::IntTy),
1262 SparcIntRegOrder::l0));
1263 mvec[N++] = M;
1264
1265 M = new MachineInstr(SAVE);
1266 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
1267 M->SetMachineOperandReg(1, MachineOperand::MO_MachineRegister,
1268 target.getRegInfo().getUnifiedRegNum(
1269 target.getRegInfo().getRegClassIDOfType(Type::IntTy),
1270 SparcIntRegOrder::l0));
1271 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
1272 mvec[N++] = M;
1273 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001274
Vikram S. Adve74825322002-03-18 03:15:35 +00001275 return N;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001276}
1277
1278
Vikram S. Advefb361122001-10-22 13:36:31 +00001279extern unsigned
1280GetInstructionsForEpilog(BasicBlock* anExitBB,
1281 TargetMachine &target,
1282 MachineInstr** mvec)
1283{
Vikram S. Advefb361122001-10-22 13:36:31 +00001284 mvec[0] = new MachineInstr(RESTORE);
Vikram S. Adve74825322002-03-18 03:15:35 +00001285 mvec[0]->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
1286 mvec[0]->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
Chris Lattner697954c2002-01-20 22:54:45 +00001287 (int64_t)0);
Vikram S. Adve74825322002-03-18 03:15:35 +00001288 mvec[0]->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Advefb361122001-10-22 13:36:31 +00001289
1290 return 1;
1291}
1292
1293
1294//------------------------------------------------------------------------
1295// External Function: ThisIsAChainRule
1296//
1297// Purpose:
1298// Check if a given BURG rule is a chain rule.
1299//------------------------------------------------------------------------
1300
1301extern bool
1302ThisIsAChainRule(int eruleno)
1303{
1304 switch(eruleno)
1305 {
1306 case 111: // stmt: reg
1307 case 113: // stmt: bool
1308 case 123:
1309 case 124:
1310 case 125:
1311 case 126:
1312 case 127:
1313 case 128:
1314 case 129:
1315 case 130:
1316 case 131:
1317 case 132:
1318 case 133:
1319 case 155:
1320 case 221:
1321 case 222:
1322 case 241:
1323 case 242:
1324 case 243:
1325 case 244:
1326 return true; break;
1327
1328 default:
1329 return false; break;
1330 }
1331}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001332
1333
1334//------------------------------------------------------------------------
1335// External Function: GetInstructionsByRule
1336//
1337// Purpose:
1338// Choose machine instructions for the SPARC according to the
1339// patterns chosen by the BURG-generated parser.
1340//------------------------------------------------------------------------
1341
Vikram S. Adve74825322002-03-18 03:15:35 +00001342void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001343GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001344 int ruleForNode,
1345 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001346 TargetMachine &target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001347 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001348{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001349 bool checkCast = false; // initialize here to use fall-through
Chris Lattner20b1ea02001-09-14 03:47:57 +00001350 int nextRule;
1351 int forwardOperandNum = -1;
Vikram S. Adve74825322002-03-18 03:15:35 +00001352 unsigned int allocaSize = 0;
1353 MachineInstr* M, *M2;
1354 unsigned int L;
1355
1356 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001357
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001358 // If the code for this instruction was folded into the parent (user),
1359 // then do nothing!
1360 if (subtreeRoot->isFoldedIntoParent())
1361 return;
1362
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001363 //
1364 // Let's check for chain rules outside the switch so that we don't have
1365 // to duplicate the list of chain rule production numbers here again
1366 //
1367 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001368 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001369 // Chain rules have a single nonterminal on the RHS.
1370 // Get the rule that matches the RHS non-terminal and use that instead.
1371 //
1372 assert(nts[0] && ! nts[1]
1373 && "A chain rule should have only one RHS non-terminal!");
1374 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1375 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001376 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001377 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001378 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001379 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001380 switch(ruleForNode) {
1381 case 1: // stmt: Ret
1382 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001383 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001384 // for moving return value to appropriate register.
1385 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001386 // Mark the return value register as an implicit ref of
1387 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001388 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001389 ReturnInst *returnInstr =
1390 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001391 assert(returnInstr->getOpcode() == Instruction::Ret);
1392
Chris Lattner9c461082002-02-03 07:50:56 +00001393 Instruction* returnReg = new TmpInstruction(returnInstr);
1394 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001395
Vikram S. Adve74825322002-03-18 03:15:35 +00001396 M = new MachineInstr(JMPLRET);
1397 M->SetMachineOperandReg(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001398 returnReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001399 M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
Chris Lattner697954c2002-01-20 22:54:45 +00001400 (int64_t)8);
Vikram S. Adve74825322002-03-18 03:15:35 +00001401 M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001402
Vikram S. Advea995e602001-10-11 04:23:19 +00001403 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001404 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001405
Vikram S. Adve74825322002-03-18 03:15:35 +00001406 mvec.push_back(M);
1407 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001408
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001409 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001410 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001411
1412 case 3: // stmt: Store(reg,reg)
1413 case 4: // stmt: Store(reg,ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001414 mvec.push_back(new MachineInstr(
1415 ChooseStoreInstruction(
1416 subtreeRoot->leftChild()->getValue()->getType())));
1417 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001418 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001419
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001420 case 5: // stmt: BrUncond
Vikram S. Adve74825322002-03-18 03:15:35 +00001421 M = new MachineInstr(BA);
1422 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001423 (Value*)NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001424 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001425 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001426 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001427
1428 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001429 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001430 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001431
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001432 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001433 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001434 // If the constant is ZERO, we can use the branch-on-integer-register
1435 // instructions and avoid the SUBcc instruction entirely.
1436 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001437 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001438 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1439 assert(constNode &&
1440 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001441 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001442 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001443
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001444 if ((constVal->getType()->isIntegral()
1445 || constVal->getType()->isPointerType())
1446 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1447 && isValidConst)
1448 {
1449 // That constant is a zero after all...
1450 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001451 // Mark the setCC node so that no code is generated for it.
1452 InstructionNode* setCCNode = (InstructionNode*)
1453 subtreeRoot->leftChild();
1454 assert(setCCNode->getOpLabel() == SetCCOp);
1455 setCCNode->markFoldedIntoParent();
1456
1457 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1458
Vikram S. Adve74825322002-03-18 03:15:35 +00001459 M = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1460 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001461 setCCNode->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001462 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1463 brInst->getSuccessor(0));
1464 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001465
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001466 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001467 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001468
1469 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001470 M = new MachineInstr(BA);
1471 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1472 (Value*) NULL);
1473 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001474 brInst->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001475 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001476
1477 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001478 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001479
1480 break;
1481 }
1482 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001483 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001484
1485 case 6: // stmt: BrCond(bool)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001486 { // bool => boolean was computed with some boolean operator
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001487 // (SetCC, Not, ...). We need to check whether the type was a FP,
1488 // signed int or unsigned int, and check the branching condition in
1489 // order to choose the branch to use.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001490 // If it is an integer CC, we also need to find the unique
1491 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001492 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001493 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001494 bool isFPBranch;
Vikram S. Adve74825322002-03-18 03:15:35 +00001495 M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001496
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001497 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1498 brInst->getParent()->getParent(),
1499 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001500
Vikram S. Adve74825322002-03-18 03:15:35 +00001501 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue);
1502 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1503 brInst->getSuccessor(0));
1504 mvec.push_back(M);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001505
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001506 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001507 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001508
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001509 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001510 M = new MachineInstr(BA);
1511 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1512 (Value*) NULL);
1513 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1514 brInst->getSuccessor(1));
1515 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001516
1517 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001518 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001519 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001520 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001521
1522 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001523 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001524 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001525 Constant* constVal =
1526 cast<Constant>(subtreeRoot->leftChild()->getValue());
1527 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001528
Vikram S. Adve74825322002-03-18 03:15:35 +00001529 M = new MachineInstr(BA);
1530 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1531 (Value*) NULL);
1532 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001533 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001534 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001535
1536 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001537 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001538 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001539 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001540
1541 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001542 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001543 // Just use the branch-on-integer-register instruction!
1544 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001545 M = new MachineInstr(BRNZ);
1546 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001547 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001548 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001549 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001550 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001551
1552 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001553 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001554
1555 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001556 M = new MachineInstr(BA);
1557 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1558 (Value*) NULL);
1559 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001560 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001561 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001562
1563 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001564 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001565 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001566 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001567
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001568 case 9: // stmt: Switch(reg)
1569 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001570 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001571
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001572 case 10: // reg: VRegList(reg, reg)
1573 assert(0 && "VRegList should never be the topmost non-chain rule");
1574 break;
1575
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001576 case 21: // bool: Not(bool): Both these are implemented as:
1577 case 321: // reg: BNot(reg) : reg = reg XOR-NOT 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001578 M = new MachineInstr(XNOR);
1579 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1580 subtreeRoot->leftChild()->getValue());
1581 M->SetMachineOperandReg(1, target.getRegInfo().getZeroRegNum());
1582 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1583 subtreeRoot->getValue());
1584 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001585 break;
1586
1587 case 322: // reg: ToBoolTy(bool):
1588 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001589 {
1590 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
1591 assert(opType->isIntegral() || opType->isPointerType()
1592 || opType == Type::BoolTy);
Vikram S. Adve74825322002-03-18 03:15:35 +00001593 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001594 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001595 }
1596
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001597 case 23: // reg: ToUByteTy(reg)
1598 case 25: // reg: ToUShortTy(reg)
1599 case 27: // reg: ToUIntTy(reg)
1600 case 29: // reg: ToULongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001601 {
1602 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001603 assert(opType->isIntegral() ||
1604 opType->isPointerType() ||
1605 opType == Type::BoolTy && "Cast is illegal for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00001606 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001607 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001608 }
1609
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001610 case 24: // reg: ToSByteTy(reg)
1611 case 26: // reg: ToShortTy(reg)
1612 case 28: // reg: ToIntTy(reg)
1613 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001614 {
1615 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
1616 if (opType->isIntegral()
1617 || opType->isPointerType()
1618 || opType == Type::BoolTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001619 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001620 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001621 }
1622 else
1623 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001624 // If the source operand is an FP type, the int result must be
1625 // copied from float to int register via memory!
1626 Instruction *dest = subtreeRoot->getInstruction();
1627 Value* leftVal = subtreeRoot->leftChild()->getValue();
1628 Value* destForCast;
1629 vector<MachineInstr*> minstrVec;
1630
1631 if (opType == Type::FloatTy || opType == Type::DoubleTy)
1632 {
1633 // Create a temporary to represent the INT register
1634 // into which the FP value will be copied via memory.
1635 // The type of this temporary will determine the FP
1636 // register used: single-prec for a 32-bit int or smaller,
1637 // double-prec for a 64-bit int.
1638 //
1639 const Type* destTypeToUse =
1640 (dest->getType() == Type::LongTy)? Type::DoubleTy
1641 : Type::FloatTy;
Chris Lattner9c461082002-02-03 07:50:56 +00001642 destForCast = new TmpInstruction(destTypeToUse, leftVal);
1643 MachineCodeForInstruction &MCFI =
1644 MachineCodeForInstruction::get(dest);
1645 MCFI.addTemp(destForCast);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001646
1647 vector<TmpInstruction*> tempVec;
1648 target.getInstrInfo().CreateCodeToCopyFloatToInt(
1649 dest->getParent()->getParent(),
1650 (TmpInstruction*) destForCast, dest,
1651 minstrVec, tempVec, target);
1652
1653 for (unsigned i=0; i < tempVec.size(); ++i)
Chris Lattner9c461082002-02-03 07:50:56 +00001654 MCFI.addTemp(tempVec[i]);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001655 }
1656 else
1657 destForCast = leftVal;
1658
1659 MachineOpCode opCode=ChooseConvertToIntInstr(subtreeRoot, opType);
1660 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
1661
Vikram S. Adve74825322002-03-18 03:15:35 +00001662 M = new MachineInstr(opCode);
1663 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1664 leftVal);
1665 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1666 destForCast);
1667 mvec.push_back(M);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001668
Vikram S. Adve74825322002-03-18 03:15:35 +00001669 // Append the copy code, if any, after the conversion instr.
1670 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001671 }
1672 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001673 }
1674
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001675 case 31: // reg: ToFloatTy(reg):
1676 case 32: // reg: ToDoubleTy(reg):
1677 case 232: // reg: ToDoubleTy(Constant):
1678
1679 // If this instruction has a parent (a user) in the tree
1680 // and the user is translated as an FsMULd instruction,
1681 // then the cast is unnecessary. So check that first.
1682 // In the future, we'll want to do the same for the FdMULq instruction,
1683 // so do the check here instead of only for ToFloatTy(reg).
1684 //
1685 if (subtreeRoot->parent() != NULL &&
Chris Lattner9c461082002-02-03 07:50:56 +00001686 MachineCodeForInstruction::get(((InstructionNode*)subtreeRoot->parent())->getInstruction())[0]->getOpCode() == FSMULD)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001687 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001688 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001689 }
1690 else
1691 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001692 Value* leftVal = subtreeRoot->leftChild()->getValue();
1693 const Type* opType = leftVal->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001694 MachineOpCode opCode=ChooseConvertToFloatInstr(subtreeRoot,opType);
1695 if (opCode == INVALID_OPCODE) // no conversion needed
1696 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001697 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001698 }
1699 else
1700 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001701 // If the source operand is a non-FP type it must be
1702 // first copied from int to float register via memory!
1703 Instruction *dest = subtreeRoot->getInstruction();
1704 Value* srcForCast;
1705 int n = 0;
1706 if (opType != Type::FloatTy && opType != Type::DoubleTy)
1707 {
1708 // Create a temporary to represent the FP register
1709 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001710 // The type of this temporary will determine the FP
1711 // register used: single-prec for a 32-bit int or smaller,
1712 // double-prec for a 64-bit int.
1713 //
1714 const Type* srcTypeToUse =
1715 (leftVal->getType() == Type::LongTy)? Type::DoubleTy
1716 : Type::FloatTy;
1717
Chris Lattner9c461082002-02-03 07:50:56 +00001718 srcForCast = new TmpInstruction(srcTypeToUse, dest);
1719 MachineCodeForInstruction &DestMCFI =
1720 MachineCodeForInstruction::get(dest);
1721 DestMCFI.addTemp(srcForCast);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001722
1723 vector<MachineInstr*> minstrVec;
1724 vector<TmpInstruction*> tempVec;
1725 target.getInstrInfo().CreateCodeToCopyIntToFloat(
1726 dest->getParent()->getParent(),
1727 leftVal, (TmpInstruction*) srcForCast,
1728 minstrVec, tempVec, target);
1729
Vikram S. Adve74825322002-03-18 03:15:35 +00001730 mvec.insert(mvec.end(), minstrVec.begin(),minstrVec.end());
1731
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001732 for (unsigned i=0; i < tempVec.size(); ++i)
Chris Lattner9c461082002-02-03 07:50:56 +00001733 DestMCFI.addTemp(tempVec[i]);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001734 }
1735 else
1736 srcForCast = leftVal;
1737
Vikram S. Adve74825322002-03-18 03:15:35 +00001738 M = new MachineInstr(opCode);
1739 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1740 srcForCast);
1741 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1742 dest);
1743 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001744 }
1745 }
1746 break;
1747
1748 case 19: // reg: ToArrayTy(reg):
1749 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001750 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001751 break;
1752
1753 case 233: // reg: Add(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001754 M = CreateAddConstInstruction(subtreeRoot);
1755 if (M != NULL)
1756 {
1757 mvec.push_back(M);
1758 break;
1759 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001760 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001761
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001762 case 33: // reg: Add(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001763 mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot)));
1764 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001765 break;
1766
1767 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001768 M = CreateSubConstInstruction(subtreeRoot);
1769 if (M != NULL)
1770 {
1771 mvec.push_back(M);
1772 break;
1773 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001774 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001775
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001776 case 34: // reg: Sub(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001777 mvec.push_back(new MachineInstr(ChooseSubInstructionByType(
1778 subtreeRoot->getInstruction()->getType())));
1779 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001780 break;
1781
1782 case 135: // reg: Mul(todouble, todouble)
1783 checkCast = true;
1784 // FALL THROUGH
1785
1786 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001787 {
1788 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1789 ? FSMULD
1790 : INVALID_MACHINE_OPCODE);
1791 CreateMulInstruction(target,
1792 subtreeRoot->leftChild()->getValue(),
1793 subtreeRoot->rightChild()->getValue(),
1794 subtreeRoot->getInstruction(),
1795 mvec, forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001796 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001797 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001798 case 335: // reg: Mul(todouble, todoubleConst)
1799 checkCast = true;
1800 // FALL THROUGH
1801
1802 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001803 {
1804 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1805 ? FSMULD
1806 : INVALID_MACHINE_OPCODE);
1807 CreateMulInstruction(target,
1808 subtreeRoot->leftChild()->getValue(),
1809 subtreeRoot->rightChild()->getValue(),
1810 subtreeRoot->getInstruction(),
1811 mvec, forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001812 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001813 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001814 case 236: // reg: Div(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001815 L = mvec.size();
1816 CreateDivConstInstruction(target, subtreeRoot, mvec);
1817 if (mvec.size() > L)
1818 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001819 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001820
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001821 case 36: // reg: Div(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001822 mvec.push_back(new MachineInstr(ChooseDivInstruction(target, subtreeRoot)));
1823 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001824 break;
1825
1826 case 37: // reg: Rem(reg, reg)
1827 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001828 {
1829 Instruction* remInstr = subtreeRoot->getInstruction();
1830
Chris Lattner9c461082002-02-03 07:50:56 +00001831 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001832 subtreeRoot->leftChild()->getValue(),
1833 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001834 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001835 quot,
1836 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001837 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001838
Vikram S. Adve74825322002-03-18 03:15:35 +00001839 M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1840 Set3OperandsFromInstr(M, subtreeRoot, target);
1841 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot);
1842 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001843
Vikram S. Adve74825322002-03-18 03:15:35 +00001844 M = new MachineInstr(ChooseMulInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001845 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001846 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,quot);
1847 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
Vikram S. Adve510eec72001-11-04 21:59:14 +00001848 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001849 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,prod);
1850 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001851
Vikram S. Adve74825322002-03-18 03:15:35 +00001852 M = new MachineInstr(ChooseSubInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001853 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001854 Set3OperandsFromInstr(M, subtreeRoot, target);
1855 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod);
1856 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001857
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001858 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001859 }
1860
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001861 case 38: // bool: And(bool, bool)
1862 case 238: // bool: And(bool, boolconst)
1863 case 338: // reg : BAnd(reg, reg)
1864 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001865 mvec.push_back(new MachineInstr(AND));
1866 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001867 break;
1868
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001869 case 138: // bool: And(bool, not)
1870 case 438: // bool: BAnd(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001871 mvec.push_back(new MachineInstr(ANDN));
1872 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001873 break;
1874
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001875 case 39: // bool: Or(bool, bool)
1876 case 239: // bool: Or(bool, boolconst)
1877 case 339: // reg : BOr(reg, reg)
1878 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001879 mvec.push_back(new MachineInstr(ORN));
1880 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001881 break;
1882
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001883 case 139: // bool: Or(bool, not)
1884 case 439: // bool: BOr(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001885 mvec.push_back(new MachineInstr(ORN));
1886 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001887 break;
1888
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001889 case 40: // bool: Xor(bool, bool)
1890 case 240: // bool: Xor(bool, boolconst)
1891 case 340: // reg : BXor(reg, reg)
1892 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001893 mvec.push_back(new MachineInstr(XOR));
1894 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001895 break;
1896
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001897 case 140: // bool: Xor(bool, not)
1898 case 440: // bool: BXor(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001899 mvec.push_back(new MachineInstr(XNOR));
1900 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001901 break;
1902
1903 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001904 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001905 // If the SetCC was folded into the user (parent), it will be
1906 // caught above. All other cases are the same as case 42,
1907 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001908 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001909 case 42: // bool: SetCC(reg, reg):
1910 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001911 // This generates a SUBCC instruction, putting the difference in
1912 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001913 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001914 // If the boolean result of the SetCC is used by anything other
1915 // than a single branch instruction, the boolean must be
1916 // computed and stored in the result register. Otherwise, discard
1917 // the difference (by using %g0) and keep only the condition code.
1918 //
1919 // To compute the boolean result in a register we use a conditional
1920 // move, unless the result of the SUBCC instruction can be used as
1921 // the bool! This assumes that zero is FALSE and any non-zero
1922 // integer is TRUE.
1923 //
1924 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1925 Instruction* setCCInstr = subtreeRoot->getInstruction();
1926 bool keepBoolVal = (parentNode == NULL ||
1927 parentNode->getInstruction()->getOpcode()
1928 != Instruction::Br);
1929 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001930 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1931 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1932
1933 bool mustClearReg;
1934 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001935 MachineOpCode movOpCode = 0;
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001936
1937 // Mark the 4th operand as being a CC register, and as a def
1938 // A TmpInstruction is created to represent the CC "result".
1939 // Unlike other instances of TmpInstruction, this one is used
1940 // by machine code of multiple LLVM instructions, viz.,
1941 // the SetCC and the branch. Make sure to get the same one!
1942 // Note that we do this even for FP CC registers even though they
1943 // are explicit operands, because the type of the operand
1944 // needs to be a floating point condition code, not an integer
1945 // condition code. Think of this as casting the bool result to
1946 // a FP condition code register.
1947 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001948 Value* leftVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001949 bool isFPCompare = (leftVal->getType() == Type::FloatTy ||
1950 leftVal->getType() == Type::DoubleTy);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001951
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001952 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1953 setCCInstr->getParent()->getParent(),
1954 isFPCompare? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001955 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001956
1957 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001958 {
1959 // Integer condition: dest. should be %g0 or an integer register.
1960 // If result must be saved but condition is not SetEQ then we need
1961 // a separate instruction to compute the bool result, so discard
1962 // result of SUBcc instruction anyway.
1963 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001964 M = new MachineInstr(SUBcc);
1965 Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal);
1966 M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister,
1967 tmpForCC, /*def*/true);
1968 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001969
1970 if (computeBoolVal)
1971 { // recompute bool using the integer condition codes
1972 movOpCode =
1973 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1974 }
1975 }
1976 else
1977 {
1978 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve74825322002-03-18 03:15:35 +00001979 M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1980 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001981 tmpForCC);
Vikram S. Adve74825322002-03-18 03:15:35 +00001982 M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001983 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001984 M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001985 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001986 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001987
1988 if (computeBoolVal)
1989 {// recompute bool using the FP condition codes
1990 mustClearReg = true;
1991 valueToMove = 1;
1992 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1993 }
1994 }
1995
1996 if (computeBoolVal)
1997 {
1998 if (mustClearReg)
1999 {// Unconditionally set register to 0
Vikram S. Adve74825322002-03-18 03:15:35 +00002000 M = new MachineInstr(SETHI);
2001 M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed,
2002 (int64_t)0);
2003 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
2004 setCCInstr);
2005 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002006 }
2007
2008 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve74825322002-03-18 03:15:35 +00002009 M = new MachineInstr(movOpCode);
2010 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
2011 tmpForCC);
2012 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
2013 valueToMove);
2014 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
2015 setCCInstr);
2016 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002017 }
2018 break;
2019 }
2020
2021 case 43: // boolreg: VReg
2022 case 44: // boolreg: Constant
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002023 break;
2024
2025 case 51: // reg: Load(reg)
2026 case 52: // reg: Load(ptrreg)
2027 case 53: // reg: LoadIdx(reg,reg)
2028 case 54: // reg: LoadIdx(ptrreg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002029 mvec.push_back(new MachineInstr(ChooseLoadInstruction(
2030 subtreeRoot->getValue()->getType())));
2031 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002032 break;
2033
2034 case 55: // reg: GetElemPtr(reg)
2035 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002036 // If the GetElemPtr was folded into the user (parent), it will be
2037 // caught above. For other cases, we have to compute the address.
Vikram S. Adve74825322002-03-18 03:15:35 +00002038 mvec.push_back(new MachineInstr(ADD));
2039 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002040 break;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002041
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002042 case 57: // reg: Alloca: Implement as 1 instruction:
2043 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002044 AllocationInst* instr =
2045 cast<AllocationInst>(subtreeRoot->getInstruction());
2046 unsigned int tsize =
2047 target.findOptimalStorageSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00002048 assert(tsize != 0);
2049 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002050 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002051 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002052
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002053 case 58: // reg: Alloca(reg): Implement as 3 instructions:
2054 // mul num, typeSz -> tmp
2055 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002056 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002057 AllocationInst* instr =
2058 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00002059 const Type* eltType = instr->getAllocatedType();
2060
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002061 // If #elements is constant, use simpler code for fixed-size allocas
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002062 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002063 Value* numElementsVal = NULL;
2064 bool isArray = instr->isArrayAllocation();
2065
2066 if (!isArray ||
2067 isa<Constant>(numElementsVal = instr->getArraySize()))
2068 { // total size is constant: generate code for fixed-size alloca
2069 unsigned int numElements = isArray?
2070 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
2071 CreateCodeForFixedSizeAlloca(target, instr, tsize,
2072 numElements, mvec);
2073 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002074 else // total size is not constant.
2075 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002076 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002077 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002078 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002079
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002080 case 61: // reg: Call
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002081 { // Generate a call-indirect (i.e., jmpl) for now to expose
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002082 // the potential need for registers. If an absolute address
2083 // is available, replace this with a CALL instruction.
2084 // Mark both the indirection register and the return-address
2085 // register as hidden virtual registers.
Vikram S. Advea995e602001-10-11 04:23:19 +00002086 // Also, mark the operands of the Call and return value (if
2087 // any) as implicit operands of the CALL machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002088 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002089 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002090 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002091
Chris Lattner9c461082002-02-03 07:50:56 +00002092 Instruction* retAddrReg = new TmpInstruction(callInstr);
Vikram S. Adve8557b222001-10-10 20:56:33 +00002093
Vikram S. Advea995e602001-10-11 04:23:19 +00002094 // Note temporary values in the machineInstrVec for the VM instr.
Vikram S. Adve8557b222001-10-10 20:56:33 +00002095 //
2096 // WARNING: Operands 0..N-1 must go in slots 0..N-1 of implicitUses.
2097 // The result value must go in slot N. This is assumed
2098 // in register allocation.
2099 //
Chris Lattner9c461082002-02-03 07:50:56 +00002100 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002101
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002102
2103 // Generate the machine instruction and its operands.
2104 // Use CALL for direct function calls; this optimistically assumes
2105 // the PC-relative address fits in the CALL address field (22 bits).
2106 // Use JMPL for indirect calls.
2107 //
2108 if (callee->getValueType() == Value::MethodVal)
2109 { // direct function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002110 M = new MachineInstr(CALL);
2111 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
2112 callee);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002113 }
2114 else
2115 { // indirect function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002116 M = new MachineInstr(JMPLCALL);
2117 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
2118 callee);
2119 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
2120 (int64_t) 0);
2121 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
2122 retAddrReg);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002123 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002124
Vikram S. Adve74825322002-03-18 03:15:35 +00002125 mvec.push_back(M);
2126
Vikram S. Advea995e602001-10-11 04:23:19 +00002127 // Add the call operands and return value as implicit refs
2128 for (unsigned i=0, N=callInstr->getNumOperands(); i < N; ++i)
2129 if (callInstr->getOperand(i) != callee)
Vikram S. Adve74825322002-03-18 03:15:35 +00002130 mvec.back()->addImplicitRef(callInstr->getOperand(i));
Vikram S. Advea995e602001-10-11 04:23:19 +00002131
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002132 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002133 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002134
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002135 // For the CALL instruction, the ret. addr. reg. is also implicit
2136 if (callee->getValueType() == Value::MethodVal)
Vikram S. Adve74825322002-03-18 03:15:35 +00002137 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002138
Vikram S. Adve74825322002-03-18 03:15:35 +00002139 // delay slot
2140 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002141 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002142 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002143
2144 case 62: // reg: Shl(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002145 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002146 assert(opType->isIntegral()
2147 || opType == Type::BoolTy
2148 || opType->isPointerType()&& "Shl unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002149 mvec.push_back(new MachineInstr((opType == Type::LongTy)? SLLX : SLL));
2150 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002151 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002152 }
2153
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002154 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002155 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002156 assert(opType->isIntegral()
2157 || opType == Type::BoolTy
2158 || opType->isPointerType() &&"Shr unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002159 mvec.push_back(new MachineInstr((opType->isSigned()
2160 ? ((opType == Type::LongTy)? SRAX : SRA)
2161 : ((opType == Type::LongTy)? SRLX : SRL))));
2162 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002163 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002164 }
2165
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002166 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002167 break; // don't forward the value
2168
Vikram S. Adve3438b212001-11-12 18:54:11 +00002169#undef NEED_PHI_MACHINE_INSTRS
2170#ifdef NEED_PHI_MACHINE_INSTRS
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002171 { // This instruction has variable #operands, so resultPos is 0.
2172 Instruction* phi = subtreeRoot->getInstruction();
Vikram S. Adve74825322002-03-18 03:15:35 +00002173 M = new MachineInstr(PHI, 1 + phi->getNumOperands());
2174 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002175 subtreeRoot->getValue());
2176 for (unsigned i=0, N=phi->getNumOperands(); i < N; i++)
Vikram S. Adve74825322002-03-18 03:15:35 +00002177 M->SetMachineOperandVal(i+1, MachineOperand::MO_VirtualRegister,
2178 phi->getOperand(i));
2179 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002180 break;
2181 }
Chris Lattner697954c2002-01-20 22:54:45 +00002182#endif // NEED_PHI_MACHINE_INSTRS
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002183
Vikram S. Adve74825322002-03-18 03:15:35 +00002184
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002185 case 71: // reg: VReg
2186 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002187 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002188
2189 default:
2190 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002191 break;
2192 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002193 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002194
2195 if (forwardOperandNum >= 0)
2196 { // We did not generate a machine instruction but need to use operand.
2197 // If user is in the same tree, replace Value in its machine operand.
2198 // If not, insert a copy instruction which should get coalesced away
2199 // by register allocation.
2200 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002201 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002202 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002203 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002204 vector<MachineInstr*> minstrVec;
Vikram S. Adve74825322002-03-18 03:15:35 +00002205 target.getInstrInfo().CreateCopyInstructionsByType(target,
2206 subtreeRoot->getInstruction()->getParent()->getParent(),
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002207 subtreeRoot->getInstruction()->getOperand(forwardOperandNum),
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002208 subtreeRoot->getInstruction(), minstrVec);
2209 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002210 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002211 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002212 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002213}
2214
2215