blob: 5e6aafd9e28e5d78434c1727adceb6d90df3fc65 [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"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000027#include "llvm/Function.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
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000144// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000145//
146static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000147GetTmpForCC(Value* boolVal, const Function *F, 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*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000151 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000152
153 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
154
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000155 if (lastFunction != F)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000156 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000157 lastFunction = F;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000158 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
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000242ChooseConvertToFloatInstr(OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000243{
244 MachineOpCode opCode = INVALID_OPCODE;
245
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000246 switch(vopCode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000247 {
248 case ToFloatTy:
249 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000250 opCode = FITOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000251 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000252 opCode = FXTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000253 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000254 opCode = FDTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000255 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000256 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000257 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000258 assert(0 && "Cannot convert this type to FLOAT on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000259 break;
260
261 case ToDoubleTy:
Vikram S. Adve74825322002-03-18 03:15:35 +0000262 // This is usually used in conjunction with CreateCodeToCopyIntToFloat().
263 // Both functions should treat the integer as a 32-bit value for types
264 // of 4 bytes or less, and as a 64-bit value otherwise.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000265 if (opType == Type::SByteTy || opType == Type::UByteTy ||
266 opType == Type::ShortTy || opType == Type::UShortTy ||
267 opType == Type::IntTy || opType == Type::UIntTy)
Vikram S. Adve74825322002-03-18 03:15:35 +0000268 opCode = FITOD;
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000269 else if (opType == Type::LongTy || opType == Type::ULongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000270 opCode = FXTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000271 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000272 opCode = FSTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000273 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000274 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000275 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000276 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000277 break;
278
279 default:
280 break;
281 }
282
283 return opCode;
284}
285
286static inline MachineOpCode
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000287ChooseConvertToIntInstr(OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000288{
289 MachineOpCode opCode = INVALID_OPCODE;;
290
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000291 if (vopCode == ToSByteTy || vopCode == ToShortTy || vopCode == ToIntTy)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000292 {
293 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000294 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000295 case Type::FloatTyID: opCode = FSTOI; break;
296 case Type::DoubleTyID: opCode = FDTOI; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000297 default:
298 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
299 break;
300 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000301 }
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000302 else if (vopCode == ToLongTy)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000303 {
304 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000305 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000306 case Type::FloatTyID: opCode = FSTOX; break;
307 case Type::DoubleTyID: opCode = FDTOX; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000308 default:
309 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
310 break;
311 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000312 }
313 else
314 assert(0 && "Should not get here, Mo!");
315
316 return opCode;
317}
318
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000319MachineInstr*
320CreateConvertToIntInstr(OpLabel vopCode, Value* srcVal, Value* destVal)
321{
322 MachineOpCode opCode = ChooseConvertToIntInstr(vopCode, srcVal->getType());
323 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
324
325 MachineInstr* M = new MachineInstr(opCode);
326 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, srcVal);
327 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, destVal);
328 return M;
329}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000330
331static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000332ChooseAddInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000333{
334 MachineOpCode opCode = INVALID_OPCODE;
335
Chris Lattner20b1ea02001-09-14 03:47:57 +0000336 if (resultType->isIntegral() ||
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000337 isa<PointerType>(resultType) ||
338 isa<FunctionType>(resultType) ||
339 resultType == Type::LabelTy ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000340 resultType == Type::BoolTy)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000341 {
342 opCode = ADD;
343 }
344 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000345 switch(resultType->getPrimitiveID())
346 {
347 case Type::FloatTyID: opCode = FADDS; break;
348 case Type::DoubleTyID: opCode = FADDD; break;
349 default: assert(0 && "Invalid type for ADD instruction"); break;
350 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000351
352 return opCode;
353}
354
355
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000356static inline MachineOpCode
357ChooseAddInstruction(const InstructionNode* instrNode)
358{
359 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
360}
361
362
Chris Lattner20b1ea02001-09-14 03:47:57 +0000363static inline MachineInstr*
364CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000365 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000366{
367 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000368 ? FMOVS : FMOVD);
Vikram S. Adve74825322002-03-18 03:15:35 +0000369 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
370 instrNode->leftChild()->getValue());
371 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
372 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000373 return minstr;
374}
375
376static inline MachineInstr*
377CreateAddConstInstruction(const InstructionNode* instrNode)
378{
379 MachineInstr* minstr = NULL;
380
381 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000382 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000383
384 // Cases worth optimizing are:
385 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
386 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
387 //
388 const Type* resultType = instrNode->getInstruction()->getType();
389
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000390 if (resultType == Type::FloatTy ||
391 resultType == Type::DoubleTy)
392 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000393 double dval = cast<ConstantFP>(constOp)->getValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000394 if (dval == 0.0)
395 minstr = CreateMovFloatInstruction(instrNode, resultType);
396 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000397
398 return minstr;
399}
400
401
402static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000403ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000404{
405 MachineOpCode opCode = INVALID_OPCODE;
406
Chris Lattner20b1ea02001-09-14 03:47:57 +0000407 if (resultType->isIntegral() ||
408 resultType->isPointerType())
409 {
410 opCode = SUB;
411 }
412 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000413 switch(resultType->getPrimitiveID())
414 {
415 case Type::FloatTyID: opCode = FSUBS; break;
416 case Type::DoubleTyID: opCode = FSUBD; break;
417 default: assert(0 && "Invalid type for SUB instruction"); break;
418 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000419
420 return opCode;
421}
422
423
424static inline MachineInstr*
425CreateSubConstInstruction(const InstructionNode* instrNode)
426{
427 MachineInstr* minstr = NULL;
428
429 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000430 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000431
432 // Cases worth optimizing are:
433 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
434 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
435 //
436 const Type* resultType = instrNode->getInstruction()->getType();
437
438 if (resultType == Type::FloatTy ||
439 resultType == Type::DoubleTy)
440 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000441 double dval = cast<ConstantFP>(constOp)->getValue();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000442 if (dval == 0.0)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000443 minstr = CreateMovFloatInstruction(instrNode, resultType);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000444 }
445
446 return minstr;
447}
448
449
450static inline MachineOpCode
451ChooseFcmpInstruction(const InstructionNode* instrNode)
452{
453 MachineOpCode opCode = INVALID_OPCODE;
454
455 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
456 switch(operand->getType()->getPrimitiveID()) {
457 case Type::FloatTyID: opCode = FCMPS; break;
458 case Type::DoubleTyID: opCode = FCMPD; break;
459 default: assert(0 && "Invalid type for FCMP instruction"); break;
460 }
461
462 return opCode;
463}
464
465
466// Assumes that leftArg and rightArg are both cast instructions.
467//
468static inline bool
469BothFloatToDouble(const InstructionNode* instrNode)
470{
471 InstrTreeNode* leftArg = instrNode->leftChild();
472 InstrTreeNode* rightArg = instrNode->rightChild();
473 InstrTreeNode* leftArgArg = leftArg->leftChild();
474 InstrTreeNode* rightArgArg = rightArg->leftChild();
475 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
476
477 // Check if both arguments are floats cast to double
478 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000479 leftArgArg->getValue()->getType() == Type::FloatTy &&
480 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000481}
482
483
484static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000485ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000486{
487 MachineOpCode opCode = INVALID_OPCODE;
488
Chris Lattner20b1ea02001-09-14 03:47:57 +0000489 if (resultType->isIntegral())
Vikram S. Adve510eec72001-11-04 21:59:14 +0000490 opCode = MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000491 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000492 switch(resultType->getPrimitiveID())
493 {
494 case Type::FloatTyID: opCode = FMULS; break;
495 case Type::DoubleTyID: opCode = FMULD; break;
496 default: assert(0 && "Invalid type for MUL instruction"); break;
497 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000498
499 return opCode;
500}
501
502
Vikram S. Adve510eec72001-11-04 21:59:14 +0000503
Chris Lattner20b1ea02001-09-14 03:47:57 +0000504static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000505CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000506 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000507{
508 MachineInstr* minstr = new MachineInstr(SUB);
Vikram S. Adve74825322002-03-18 03:15:35 +0000509 minstr->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
510 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, vreg);
511 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000512 return minstr;
513}
514
515
Vikram S. Adve74825322002-03-18 03:15:35 +0000516// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000517// create a cheaper instruction.
518// This returns the approximate cost of the instructions generated,
519// which is used to pick the cheapest when both operands are constant.
520static inline unsigned int
Vikram S. Adve74825322002-03-18 03:15:35 +0000521CreateMulConstInstruction(const TargetMachine &target,
522 Value* lval, Value* rval, Value* destVal,
523 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000524{
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000525 /* An integer multiply is generally more costly than FP multiply */
526 unsigned int cost = target.getInstrInfo().minLatency(MULX);
Vikram S. Adve74825322002-03-18 03:15:35 +0000527 MachineInstr* minstr1 = NULL;
528 MachineInstr* minstr2 = NULL;
529
530 Value* constOp = rval;
531 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000532 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000533
534 // Cases worth optimizing are:
535 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
536 // (2) Multiply by 2^x for integer types: replace with Shift
537 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000538 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000539
Vikram S. Adve243dd452001-09-18 13:03:13 +0000540 if (resultType->isIntegral() || resultType->isPointerType())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000541 {
542 unsigned pow;
543 bool isValidConst;
544 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
545 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000546 {
547 bool needNeg = false;
548 if (C < 0)
549 {
550 needNeg = true;
551 C = -C;
552 }
553
554 if (C == 0 || C == 1)
555 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000556 cost = target.getInstrInfo().minLatency(ADD);
Vikram S. Adve74825322002-03-18 03:15:35 +0000557 minstr1 = new MachineInstr(ADD);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000558 if (C == 0)
Vikram S. Adve74825322002-03-18 03:15:35 +0000559 minstr1->SetMachineOperandReg(0,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000560 target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000561 else
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000562 minstr1->SetMachineOperandVal(0,
563 MachineOperand::MO_VirtualRegister, lval);
564 minstr1->SetMachineOperandReg(1,
565 target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000566 }
567 else if (IsPowerOf2(C, pow))
568 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000569 minstr1 = new MachineInstr((resultType == Type::LongTy)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000570 ? SLLX : SLL);
571 minstr1->SetMachineOperandVal(0,
572 MachineOperand::MO_VirtualRegister, lval);
573 minstr1->SetMachineOperandConst(1,
574 MachineOperand::MO_UnextendedImmed, pow);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000575 }
576
Vikram S. Adve74825322002-03-18 03:15:35 +0000577 if (minstr1 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000578 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve74825322002-03-18 03:15:35 +0000579 minstr2 = CreateIntNegInstruction(target, destVal);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000580 cost += target.getInstrInfo().minLatency(minstr2->getOpCode());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000581 }
582 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000583 }
584 else
585 {
586 if (resultType == Type::FloatTy ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000587 resultType == Type::DoubleTy)
588 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000589 double dval = cast<ConstantFP>(constOp)->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000590 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000591 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000592 bool needNeg = (dval < 0);
593
594 MachineOpCode opCode = needNeg
595 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
596 : (resultType == Type::FloatTy? FMOVS : FMOVD);
597
Vikram S. Adve74825322002-03-18 03:15:35 +0000598 minstr1 = new MachineInstr(opCode);
599 minstr1->SetMachineOperandVal(0,
600 MachineOperand::MO_VirtualRegister,
601 lval);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000602 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000603 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000604 }
605
Vikram S. Adve74825322002-03-18 03:15:35 +0000606 if (minstr1 != NULL)
607 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
608 destVal);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000609
Vikram S. Adve74825322002-03-18 03:15:35 +0000610 if (minstr1)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000611 {
612 mvec.push_back(minstr1);
613 cost = target.getInstrInfo().minLatency(minstr1->getOpCode());
614 }
Vikram S. Adve74825322002-03-18 03:15:35 +0000615 if (minstr2)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000616 {
617 assert(minstr1 && "Otherwise cost needs to be initialized to 0");
618 cost += target.getInstrInfo().minLatency(minstr2->getOpCode());
619 mvec.push_back(minstr2);
620 }
621
622 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000623}
624
625
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000626// Does not create any instructions if we cannot exploit constant to
627// create a cheaper instruction.
628//
629static inline void
630CreateCheapestMulConstInstruction(const TargetMachine &target,
631 Value* lval, Value* rval, Value* destVal,
632 vector<MachineInstr*>& mvec)
633{
634 Value* constOp;
635 if (isa<Constant>(lval) && isa<Constant>(rval))
636 { // both operands are constant: try both orders!
637 vector<MachineInstr*> mvec1, mvec2;
638 unsigned int lcost = CreateMulConstInstruction(target, lval, rval,
639 destVal, mvec1);
640 unsigned int rcost = CreateMulConstInstruction(target, rval, lval,
641 destVal, mvec2);
642 vector<MachineInstr*>& mincostMvec = (lcost <= rcost)? mvec1 : mvec2;
643 vector<MachineInstr*>& maxcostMvec = (lcost <= rcost)? mvec2 : mvec1;
644 mvec.insert(mvec.end(), mincostMvec.begin(), mincostMvec.end());
645
646 for (unsigned int i=0; i < maxcostMvec.size(); ++i)
647 delete maxcostMvec[i];
648 }
649 else if (isa<Constant>(rval)) // rval is constant, but not lval
650 CreateMulConstInstruction(target, lval, rval, destVal, mvec);
651 else if (isa<Constant>(lval)) // lval is constant, but not rval
652 CreateMulConstInstruction(target, lval, rval, destVal, mvec);
653
654 // else neither is constant
655 return;
656}
657
Vikram S. Adve74825322002-03-18 03:15:35 +0000658// Return NULL if we cannot exploit constant to create a cheaper instruction
659static inline void
660CreateMulInstruction(const TargetMachine &target,
661 Value* lval, Value* rval, Value* destVal,
662 vector<MachineInstr*>& mvec,
663 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
664{
665 unsigned int L = mvec.size();
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000666 CreateCheapestMulConstInstruction(target, lval, rval, destVal, mvec);
Vikram S. Adve74825322002-03-18 03:15:35 +0000667 if (mvec.size() == L)
668 { // no instructions were added so create MUL reg, reg, reg.
669 // Use FSMULD if both operands are actually floats cast to doubles.
670 // Otherwise, use the default opcode for the appropriate type.
671 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
672 ? forceMulOp
673 : ChooseMulInstructionByType(destVal->getType()));
674 MachineInstr* M = new MachineInstr(mulOp);
675 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, lval);
676 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, rval);
677 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, destVal);
678 mvec.push_back(M);
679 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000680}
681
682
Vikram S. Adve510eec72001-11-04 21:59:14 +0000683// Generate a divide instruction for Div or Rem.
684// For Rem, this assumes that the operand type will be signed if the result
685// type is signed. This is correct because they must have the same sign.
686//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000687static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000688ChooseDivInstruction(TargetMachine &target,
689 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000690{
691 MachineOpCode opCode = INVALID_OPCODE;
692
693 const Type* resultType = instrNode->getInstruction()->getType();
694
695 if (resultType->isIntegral())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000696 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000697 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000698 switch(resultType->getPrimitiveID())
699 {
700 case Type::FloatTyID: opCode = FDIVS; break;
701 case Type::DoubleTyID: opCode = FDIVD; break;
702 default: assert(0 && "Invalid type for DIV instruction"); break;
703 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000704
705 return opCode;
706}
707
708
Vikram S. Adve74825322002-03-18 03:15:35 +0000709// Return NULL if we cannot exploit constant to create a cheaper instruction
710static inline void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000711CreateDivConstInstruction(TargetMachine &target,
712 const InstructionNode* instrNode,
Vikram S. Adve74825322002-03-18 03:15:35 +0000713 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000714{
Vikram S. Adve74825322002-03-18 03:15:35 +0000715 MachineInstr* minstr1 = NULL;
716 MachineInstr* minstr2 = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000717
718 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Vikram S. Adve74825322002-03-18 03:15:35 +0000719 if (! isa<Constant>(constOp))
720 return;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000721
722 // Cases worth optimizing are:
723 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
724 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
725 //
726 const Type* resultType = instrNode->getInstruction()->getType();
727
728 if (resultType->isIntegral())
729 {
730 unsigned pow;
731 bool isValidConst;
732 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
733 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000734 {
735 bool needNeg = false;
736 if (C < 0)
737 {
738 needNeg = true;
739 C = -C;
740 }
741
742 if (C == 1)
743 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000744 minstr1 = new MachineInstr(ADD);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000745 minstr1->SetMachineOperandVal(0,
746 MachineOperand::MO_VirtualRegister,
747 instrNode->leftChild()->getValue());
748 minstr1->SetMachineOperandReg(1,
749 target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000750 }
751 else if (IsPowerOf2(C, pow))
752 {
753 MachineOpCode opCode= ((resultType->isSigned())
754 ? (resultType==Type::LongTy)? SRAX : SRA
755 : (resultType==Type::LongTy)? SRLX : SRL);
Vikram S. Adve74825322002-03-18 03:15:35 +0000756 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000757 minstr1->SetMachineOperandVal(0,
758 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000759 instrNode->leftChild()->getValue());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000760 minstr1->SetMachineOperandConst(1,
761 MachineOperand::MO_UnextendedImmed,
762 pow);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000763 }
764
Vikram S. Adve74825322002-03-18 03:15:35 +0000765 if (minstr1 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000766 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve74825322002-03-18 03:15:35 +0000767 minstr2 = CreateIntNegInstruction(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000768 instrNode->getValue());
769 }
770 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000771 }
772 else
773 {
774 if (resultType == Type::FloatTy ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000775 resultType == Type::DoubleTy)
776 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000777 double dval = cast<ConstantFP>(constOp)->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000778 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000779 {
780 bool needNeg = (dval < 0);
781
782 MachineOpCode opCode = needNeg
783 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
784 : (resultType == Type::FloatTy? FMOVS : FMOVD);
785
Vikram S. Adve74825322002-03-18 03:15:35 +0000786 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000787 minstr1->SetMachineOperandVal(0,
788 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000789 instrNode->leftChild()->getValue());
790 }
791 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000792 }
793
Vikram S. Adve74825322002-03-18 03:15:35 +0000794 if (minstr1 != NULL)
795 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
796 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000797
Vikram S. Adve74825322002-03-18 03:15:35 +0000798 if (minstr1)
799 mvec.push_back(minstr1);
800 if (minstr2)
801 mvec.push_back(minstr2);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000802}
803
804
Vikram S. Adve74825322002-03-18 03:15:35 +0000805static void
806CreateCodeForVariableSizeAlloca(const TargetMachine& target,
807 Instruction* result,
808 unsigned int tsize,
809 Value* numElementsVal,
810 vector<MachineInstr*>& getMvec)
811{
812 MachineInstr* M;
813
814 // Create a Value to hold the (constant) element size
815 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
816
817 // Get the constant offset from SP for dynamically allocated storage
818 // and create a temporary Value to hold it.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000819 assert(result && result->getParent() && "Result value is not part of a fn?");
820 Function *F = result->getParent()->getParent();
821 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000822 bool growUp;
823 ConstantSInt* dynamicAreaOffset =
824 ConstantSInt::get(Type::IntTy,
825 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
826 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
827
828 // Create a temporary value to hold the result of MUL
829 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
830 MachineCodeForInstruction::get(result).addTemp(tmpProd);
831
832 // Instruction 1: mul numElements, typeSize -> tmpProd
833 M = new MachineInstr(MULX);
834 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, numElementsVal);
835 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tsizeVal);
836 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, tmpProd);
837 getMvec.push_back(M);
838
839 // Instruction 2: sub %sp, tmpProd -> %sp
840 M = new MachineInstr(SUB);
841 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
842 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tmpProd);
843 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
844 getMvec.push_back(M);
845
846 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
847 M = new MachineInstr(ADD);
848 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
849 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, dynamicAreaOffset);
850 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
851 getMvec.push_back(M);
852}
853
854
855static void
856CreateCodeForFixedSizeAlloca(const TargetMachine& target,
857 Instruction* result,
858 unsigned int tsize,
859 unsigned int numElements,
860 vector<MachineInstr*>& getMvec)
861{
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000862 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000863 "Result value is not part of a function?");
864 Function *F = result->getParent()->getParent();
865 MachineCodeForMethod &mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000866
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000867 // Check if the offset would small enough to use as an immediate in
868 // load/stores (check LDX because all load/stores have the same-size immediate
869 // field). If not, put the variable in the dynamically sized area of the
870 // frame.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000871 unsigned int paddedSizeIgnored;
Vikram S. Adve74825322002-03-18 03:15:35 +0000872 int offsetFromFP = mcInfo.computeOffsetforLocalVar(target, result,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000873 paddedSizeIgnored,
Vikram S. Adve74825322002-03-18 03:15:35 +0000874 tsize * numElements);
875 if (! target.getInstrInfo().constantFitsInImmedField(LDX, offsetFromFP))
876 {
877 CreateCodeForVariableSizeAlloca(target, result, tsize,
878 ConstantSInt::get(Type::IntTy,numElements),
879 getMvec);
880 return;
881 }
882
883 // else offset fits in immediate field so go ahead and allocate it.
884 offsetFromFP = mcInfo.allocateLocalVar(target, result, tsize * numElements);
885
886 // Create a temporary Value to hold the constant offset.
887 // This is needed because it may not fit in the immediate field.
888 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
889
890 // Instruction 1: add %fp, offsetFromFP -> result
891 MachineInstr* M = new MachineInstr(ADD);
892 M->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
893 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, offsetVal);
894 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
895
896 getMvec.push_back(M);
897}
898
899
900
Chris Lattner20b1ea02001-09-14 03:47:57 +0000901//------------------------------------------------------------------------
902// Function SetOperandsForMemInstr
903//
904// Choose addressing mode for the given load or store instruction.
905// Use [reg+reg] if it is an indexed reference, and the index offset is
906// not a constant or if it cannot fit in the offset field.
907// Use [reg+offset] in all other cases.
908//
909// This assumes that all array refs are "lowered" to one of these forms:
910// %x = load (subarray*) ptr, constant ; single constant offset
911// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
912// Generally, this should happen via strength reduction + LICM.
913// Also, strength reduction should take care of using the same register for
914// the loop index variable and an array index, when that is profitable.
915//------------------------------------------------------------------------
916
917static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000918SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
919 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000920 const InstructionNode* vmInstrNode,
921 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000922{
923 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
924
925 // Variables to hold the index vector, ptr value, and offset value.
926 // The major work here is to extract these for all 3 instruction types
927 // and then call the common function SetMemOperands_Internal().
928 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000929 Value* ptrVal = memInst->getPointerOperand();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000930
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000931 // Start with the index vector of this instruction, if any.
932 vector<Value*> idxVec;
933 idxVec.insert(idxVec.end(), memInst->idx_begin(), memInst->idx_end());
934
935 // If there is a GetElemPtr instruction to fold in to this instr,
936 // it must be in the left child for Load and GetElemPtr, and in the
937 // right child for Store instructions.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000938 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000939 ? vmInstrNode->rightChild()
940 : vmInstrNode->leftChild());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000941
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000942 // Fold chains of GetElemPtr instructions for structure references.
Vikram S. Adve74825322002-03-18 03:15:35 +0000943 if (isa<StructType>(cast<PointerType>(ptrVal->getType())->getElementType())
944 && (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
945 ptrChild->getOpLabel() == GetElemPtrIdx))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000946 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000947 Value* newPtr = FoldGetElemChain((InstructionNode*) ptrChild, idxVec);
948 if (newPtr)
949 ptrVal = newPtr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000950 }
951
Vikram S. Adve74825322002-03-18 03:15:35 +0000952 SetMemOperands_Internal(mvec, mvecI, vmInstrNode, ptrVal, idxVec, target);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000953}
954
955
Vikram S. Adve74825322002-03-18 03:15:35 +0000956// Generate the correct operands (and additional instructions if needed)
957// for the given pointer and given index vector.
958//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000959static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000960SetMemOperands_Internal(vector<MachineInstr*>& mvec,
961 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000962 const InstructionNode* vmInstrNode,
963 Value* ptrVal,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000964 vector<Value*>& idxVec,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000965 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000966{
967 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
968
969 // Initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000970 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000971 Value* valueForRegOffset = NULL;
972 MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister;
973
Vikram S. Adve74825322002-03-18 03:15:35 +0000974 // Check if there is an index vector and if so, compute the
975 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +0000976 //
977 if (idxVec.size() > 0)
978 {
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000979 unsigned offset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000980
Vikram S. Adve74825322002-03-18 03:15:35 +0000981 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000982
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000983 // Handle special common case of leading [0] index.
984 bool firstIndexIsZero =
985 bool(isa<ConstantUInt>(idxVec.front()) &&
986 cast<ConstantUInt>(idxVec.front())->getValue() == 0);
987
988 // This is a real structure reference if the ptr target is a
989 // structure type, and the first offset is [0] (eliminate that offset).
990 if (firstIndexIsZero && ptrType->getElementType()->isStructType())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000991 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000992 // Compute the offset value using the index vector. Create a
993 // virtual reg. for it since it may not fit in the immed field.
994 assert(idxVec.size() >= 2);
995 idxVec.erase(idxVec.begin());
Vikram S. Adve74825322002-03-18 03:15:35 +0000996 unsigned offset = target.DataLayout.getIndexedOffset(ptrType,idxVec);
997 valueForRegOffset = ConstantSInt::get(Type::IntTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000998 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000999 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001000 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001001 // It is an array ref, and must have been lowered to a single offset.
Vikram S. Adve74825322002-03-18 03:15:35 +00001002 assert((memInst->getNumOperands()
1003 == (unsigned) 1 + memInst->getFirstIndexOperandNumber())
1004 && "Array refs must be lowered before Instruction Selection");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001005
Vikram S. Adve74825322002-03-18 03:15:35 +00001006 Value* arrayOffsetVal = * memInst->idx_begin();
1007
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001008 // If index is 0, the offset value is just 0. Otherwise,
1009 // generate a MUL instruction to compute address from index.
1010 // The call to getTypeSize() will fail if size is not constant.
1011 // CreateMulInstruction() folds constants intelligently enough.
1012 //
1013 if (firstIndexIsZero)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001014 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001015 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1016 smallConstOffset = 0;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001017 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001018 else
1019 {
1020 vector<MachineInstr*> mulVec;
1021 Instruction* addr = new TmpInstruction(Type::UIntTy, memInst);
1022 MachineCodeForInstruction::get(memInst).addTemp(addr);
1023
1024 unsigned int eltSize =
1025 target.DataLayout.getTypeSize(ptrType->getElementType());
1026 assert(eltSize > 0 && "Invalid or non-const array element size");
1027 ConstantUInt* eltVal = ConstantUInt::get(Type::UIntTy, eltSize);
1028
1029 CreateMulInstruction(target,
1030 arrayOffsetVal, /* lval, not likely const */
1031 eltVal, /* rval, likely constant */
1032 addr, /* result*/
1033 mulVec, INVALID_MACHINE_OPCODE);
1034 assert(mulVec.size() > 0 && "No multiply instruction created?");
1035 for (vector<MachineInstr*>::const_iterator I = mulVec.begin();
1036 I != mulVec.end(); ++I)
1037 {
1038 mvecI = mvec.insert(mvecI, *I); // ptr to inserted value
1039 ++mvecI; // ptr to mem. instr.
1040 }
1041
1042 valueForRegOffset = addr;
1043 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001044 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001045 }
1046 else
1047 {
1048 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1049 smallConstOffset = 0;
1050 }
1051
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001052 // For STORE:
1053 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1054 // For LOAD or GET_ELEMENT_PTR,
1055 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1056 //
1057 unsigned offsetOpNum, ptrOpNum;
1058 if (memInst->getOpcode() == Instruction::Store)
1059 {
1060 (*mvecI)->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1061 vmInstrNode->leftChild()->getValue());
1062 ptrOpNum = 1;
1063 offsetOpNum = 2;
1064 }
1065 else
1066 {
1067 ptrOpNum = 0;
1068 offsetOpNum = 1;
1069 (*mvecI)->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1070 memInst);
1071 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001072
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001073 (*mvecI)->SetMachineOperandVal(ptrOpNum, MachineOperand::MO_VirtualRegister,
1074 ptrVal);
1075
Chris Lattner20b1ea02001-09-14 03:47:57 +00001076 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1077 {
1078 assert(valueForRegOffset != NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001079 (*mvecI)->SetMachineOperandVal(offsetOpNum, offsetOpType,
1080 valueForRegOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001081 }
1082 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001083 (*mvecI)->SetMachineOperandConst(offsetOpNum, offsetOpType,
1084 smallConstOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001085}
1086
1087
Chris Lattner20b1ea02001-09-14 03:47:57 +00001088//
1089// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001090// in place of the use(s) of that instruction in node `parent'.
1091// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001092// Also make sure to skip over a parent who:
1093// (1) is a list node in the Burg tree, or
1094// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001095//
1096static void
1097ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001098 InstrTreeNode* parent,
1099 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001100{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001101 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1102
Chris Lattner20b1ea02001-09-14 03:47:57 +00001103 Instruction* unusedOp = treeNode->getInstruction();
1104 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001105
1106 // The parent itself may be a list node, so find the real parent instruction
1107 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1108 {
1109 parent = parent->parent();
1110 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1111 }
1112 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1113
1114 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001115 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001116
1117 // The parent's mvec would be empty if it was itself forwarded.
1118 // Recursively call ForwardOperand in that case...
1119 //
1120 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001121 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001122 assert(parent->parent() != NULL &&
1123 "Parent could not have been forwarded, yet has no instructions?");
1124 ForwardOperand(treeNode, parent->parent(), operandNum);
1125 }
1126 else
1127 {
1128 bool fwdSuccessful = false;
1129 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001130 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001131 MachineInstr* minstr = mvec[i];
1132 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001133 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001134 const MachineOperand& mop = minstr->getOperand(i);
1135 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1136 mop.getVRegValue() == unusedOp)
1137 {
1138 minstr->SetMachineOperandVal(i,
1139 MachineOperand::MO_VirtualRegister, fwdOp);
1140 fwdSuccessful = true;
1141 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001142 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001143
1144 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1145 if (minstr->getImplicitRef(i) == unusedOp)
1146 {
1147 minstr->setImplicitRef(i, fwdOp,
1148 minstr->implicitRefIsDefined(i));
1149 fwdSuccessful = true;
1150 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001151 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001152 assert(fwdSuccessful && "Value to be forwarded is never used!");
Chris Lattner20b1ea02001-09-14 03:47:57 +00001153 }
1154}
1155
1156
Ruchira Sasanka67a463a2001-11-12 14:45:33 +00001157void UltraSparcInstrInfo::
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001158CreateCopyInstructionsByType(const TargetMachine& target,
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001159 Function *F,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001160 Value* src,
1161 Instruction* dest,
Ruchira Sasanka67a463a2001-11-12 14:45:33 +00001162 vector<MachineInstr*>& minstrVec) const
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001163{
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001164 bool loadConstantToReg = false;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001165
1166 const Type* resultType = dest->getType();
1167
1168 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
1169 if (opCode == INVALID_OPCODE)
1170 {
1171 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001172 return;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001173 }
1174
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001175 // if `src' is a constant that doesn't fit in the immed field or if it is
1176 // a global variable (i.e., a constant address), generate a load
1177 // instruction instead of an add
1178 //
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001179 if (isa<Constant>(src))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001180 {
1181 unsigned int machineRegNum;
1182 int64_t immedValue;
1183 MachineOperand::MachineOperandType opType =
1184 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
1185 machineRegNum, immedValue);
1186
1187 if (opType == MachineOperand::MO_VirtualRegister)
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001188 loadConstantToReg = true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001189 }
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001190 else if (isa<GlobalValue>(src))
1191 loadConstantToReg = true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001192
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001193 if (loadConstantToReg)
1194 { // `src' is constant and cannot fit in immed field for the ADD
1195 // Insert instructions to "load" the constant into a register
1196 vector<TmpInstruction*> tempVec;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001197 target.getInstrInfo().CreateCodeToLoadConst(F, src, dest,
1198 minstrVec, tempVec);
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001199 for (unsigned i=0; i < tempVec.size(); i++)
Chris Lattner9c461082002-02-03 07:50:56 +00001200 MachineCodeForInstruction::get(dest).addTemp(tempVec[i]);
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001201 }
1202 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001203 { // Create an add-with-0 instruction of the appropriate type.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001204 // Make `src' the second operand, in case it is a constant
1205 // Use (unsigned long) 0 for a NULL pointer value.
1206 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001207 const Type* zeroValueType =
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001208 (resultType->getPrimitiveID() == Type::PointerTyID)? Type::ULongTy
1209 : resultType;
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001210 MachineInstr* minstr = new MachineInstr(opCode);
Vikram S. Adve74825322002-03-18 03:15:35 +00001211 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1212 Constant::getNullConstant(zeroValueType));
1213 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, src);
1214 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,dest);
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001215 minstrVec.push_back(minstr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001216 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001217}
1218
1219
Ruchira Sasanka67a463a2001-11-12 14:45:33 +00001220
Vikram S. Advefb361122001-10-22 13:36:31 +00001221//******************* Externally Visible Functions *************************/
1222
1223
Vikram S. Advefb361122001-10-22 13:36:31 +00001224
1225//------------------------------------------------------------------------
1226// External Function: ThisIsAChainRule
1227//
1228// Purpose:
1229// Check if a given BURG rule is a chain rule.
1230//------------------------------------------------------------------------
1231
1232extern bool
1233ThisIsAChainRule(int eruleno)
1234{
1235 switch(eruleno)
1236 {
1237 case 111: // stmt: reg
1238 case 113: // stmt: bool
1239 case 123:
1240 case 124:
1241 case 125:
1242 case 126:
1243 case 127:
1244 case 128:
1245 case 129:
1246 case 130:
1247 case 131:
1248 case 132:
1249 case 133:
1250 case 155:
1251 case 221:
1252 case 222:
1253 case 241:
1254 case 242:
1255 case 243:
1256 case 244:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001257 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001258 return true; break;
1259
1260 default:
1261 return false; break;
1262 }
1263}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001264
1265
1266//------------------------------------------------------------------------
1267// External Function: GetInstructionsByRule
1268//
1269// Purpose:
1270// Choose machine instructions for the SPARC according to the
1271// patterns chosen by the BURG-generated parser.
1272//------------------------------------------------------------------------
1273
Vikram S. Adve74825322002-03-18 03:15:35 +00001274void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001275GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001276 int ruleForNode,
1277 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001278 TargetMachine &target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001279 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001280{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001281 bool checkCast = false; // initialize here to use fall-through
Chris Lattner20b1ea02001-09-14 03:47:57 +00001282 int nextRule;
1283 int forwardOperandNum = -1;
Vikram S. Adve74825322002-03-18 03:15:35 +00001284 unsigned int allocaSize = 0;
1285 MachineInstr* M, *M2;
1286 unsigned int L;
1287
1288 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001289
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001290 // If the code for this instruction was folded into the parent (user),
1291 // then do nothing!
1292 if (subtreeRoot->isFoldedIntoParent())
1293 return;
1294
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001295 //
1296 // Let's check for chain rules outside the switch so that we don't have
1297 // to duplicate the list of chain rule production numbers here again
1298 //
1299 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001300 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001301 // Chain rules have a single nonterminal on the RHS.
1302 // Get the rule that matches the RHS non-terminal and use that instead.
1303 //
1304 assert(nts[0] && ! nts[1]
1305 && "A chain rule should have only one RHS non-terminal!");
1306 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1307 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001308 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001309 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001310 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001311 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001312 switch(ruleForNode) {
1313 case 1: // stmt: Ret
1314 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001315 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001316 // for moving return value to appropriate register.
1317 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001318 // Mark the return value register as an implicit ref of
1319 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001320 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001321 ReturnInst *returnInstr =
1322 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001323 assert(returnInstr->getOpcode() == Instruction::Ret);
1324
Chris Lattner9c461082002-02-03 07:50:56 +00001325 Instruction* returnReg = new TmpInstruction(returnInstr);
1326 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001327
Vikram S. Adve74825322002-03-18 03:15:35 +00001328 M = new MachineInstr(JMPLRET);
1329 M->SetMachineOperandReg(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001330 returnReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001331 M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
Chris Lattner697954c2002-01-20 22:54:45 +00001332 (int64_t)8);
Vikram S. Adve74825322002-03-18 03:15:35 +00001333 M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001334
Vikram S. Advea995e602001-10-11 04:23:19 +00001335 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001336 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001337
Vikram S. Adve74825322002-03-18 03:15:35 +00001338 mvec.push_back(M);
1339 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001340
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001341 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001342 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001343
1344 case 3: // stmt: Store(reg,reg)
1345 case 4: // stmt: Store(reg,ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001346 mvec.push_back(new MachineInstr(
1347 ChooseStoreInstruction(
1348 subtreeRoot->leftChild()->getValue()->getType())));
1349 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001350 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001351
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001352 case 5: // stmt: BrUncond
Vikram S. Adve74825322002-03-18 03:15:35 +00001353 M = new MachineInstr(BA);
1354 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001355 (Value*)NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001356 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001357 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001358 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001359
1360 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001361 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001362 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001363
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001364 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001365 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001366 // If the constant is ZERO, we can use the branch-on-integer-register
1367 // instructions and avoid the SUBcc instruction entirely.
1368 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001369 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001370 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1371 assert(constNode &&
1372 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001373 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001374 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001375
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001376 if ((constVal->getType()->isIntegral()
1377 || constVal->getType()->isPointerType())
1378 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1379 && isValidConst)
1380 {
1381 // That constant is a zero after all...
1382 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001383 // Mark the setCC node so that no code is generated for it.
1384 InstructionNode* setCCNode = (InstructionNode*)
1385 subtreeRoot->leftChild();
1386 assert(setCCNode->getOpLabel() == SetCCOp);
1387 setCCNode->markFoldedIntoParent();
1388
1389 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1390
Vikram S. Adve74825322002-03-18 03:15:35 +00001391 M = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1392 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001393 setCCNode->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001394 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1395 brInst->getSuccessor(0));
1396 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001397
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001398 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001399 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001400
1401 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001402 M = new MachineInstr(BA);
1403 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1404 (Value*) NULL);
1405 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001406 brInst->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001407 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001408
1409 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001410 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001411
1412 break;
1413 }
1414 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001415 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001416
1417 case 6: // stmt: BrCond(bool)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001418 { // bool => boolean was computed with some boolean operator
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001419 // (SetCC, Not, ...). We need to check whether the type was a FP,
1420 // signed int or unsigned int, and check the branching condition in
1421 // order to choose the branch to use.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001422 // If it is an integer CC, we also need to find the unique
1423 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001424 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001425 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001426 bool isFPBranch;
Vikram S. Adve74825322002-03-18 03:15:35 +00001427 M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001428
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001429 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1430 brInst->getParent()->getParent(),
1431 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001432
Vikram S. Adve74825322002-03-18 03:15:35 +00001433 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue);
1434 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1435 brInst->getSuccessor(0));
1436 mvec.push_back(M);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001437
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001438 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001439 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001440
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001441 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001442 M = new MachineInstr(BA);
1443 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1444 (Value*) NULL);
1445 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1446 brInst->getSuccessor(1));
1447 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001448
1449 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001450 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001451 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001452 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001453
1454 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001455 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001456 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001457 Constant* constVal =
1458 cast<Constant>(subtreeRoot->leftChild()->getValue());
1459 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001460
Vikram S. Adve74825322002-03-18 03:15:35 +00001461 M = new MachineInstr(BA);
1462 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1463 (Value*) NULL);
1464 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001465 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001466 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001467
1468 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001469 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001470 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001471 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001472
1473 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001474 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001475 // Just use the branch-on-integer-register instruction!
1476 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001477 M = new MachineInstr(BRNZ);
1478 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001479 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001480 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001481 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001482 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001483
1484 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001485 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001486
1487 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001488 M = new MachineInstr(BA);
1489 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1490 (Value*) NULL);
1491 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001492 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001493 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001494
1495 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001496 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001497 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001498 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001499
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001500 case 9: // stmt: Switch(reg)
1501 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001502 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001503
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001504 case 10: // reg: VRegList(reg, reg)
1505 assert(0 && "VRegList should never be the topmost non-chain rule");
1506 break;
1507
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001508 case 21: // bool: Not(bool): Both these are implemented as:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001509 case 421: // reg: BNot(reg) : reg = reg XOR-NOT 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001510 M = new MachineInstr(XNOR);
1511 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1512 subtreeRoot->leftChild()->getValue());
1513 M->SetMachineOperandReg(1, target.getRegInfo().getZeroRegNum());
1514 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1515 subtreeRoot->getValue());
1516 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001517 break;
1518
1519 case 322: // reg: ToBoolTy(bool):
1520 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001521 {
1522 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
1523 assert(opType->isIntegral() || opType->isPointerType()
1524 || opType == Type::BoolTy);
Vikram S. Adve74825322002-03-18 03:15:35 +00001525 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001526 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001527 }
1528
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001529 case 23: // reg: ToUByteTy(reg)
1530 case 25: // reg: ToUShortTy(reg)
1531 case 27: // reg: ToUIntTy(reg)
1532 case 29: // reg: ToULongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001533 {
1534 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001535 assert(opType->isIntegral() ||
1536 opType->isPointerType() ||
1537 opType == Type::BoolTy && "Cast is illegal for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00001538 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001539 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001540 }
1541
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001542 case 24: // reg: ToSByteTy(reg)
1543 case 26: // reg: ToShortTy(reg)
1544 case 28: // reg: ToIntTy(reg)
1545 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001546 {
1547 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
1548 if (opType->isIntegral()
1549 || opType->isPointerType()
1550 || opType == Type::BoolTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001551 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001552 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001553 }
1554 else
1555 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001556 // If the source operand is an FP type, the int result must be
1557 // copied from float to int register via memory!
1558 Instruction *dest = subtreeRoot->getInstruction();
1559 Value* leftVal = subtreeRoot->leftChild()->getValue();
1560 Value* destForCast;
1561 vector<MachineInstr*> minstrVec;
1562
1563 if (opType == Type::FloatTy || opType == Type::DoubleTy)
1564 {
1565 // Create a temporary to represent the INT register
1566 // into which the FP value will be copied via memory.
1567 // The type of this temporary will determine the FP
1568 // register used: single-prec for a 32-bit int or smaller,
1569 // double-prec for a 64-bit int.
1570 //
1571 const Type* destTypeToUse =
1572 (dest->getType() == Type::LongTy)? Type::DoubleTy
1573 : Type::FloatTy;
Chris Lattner9c461082002-02-03 07:50:56 +00001574 destForCast = new TmpInstruction(destTypeToUse, leftVal);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001575 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001576 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001577 destMCFI.addTemp(destForCast);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001578
1579 vector<TmpInstruction*> tempVec;
1580 target.getInstrInfo().CreateCodeToCopyFloatToInt(
1581 dest->getParent()->getParent(),
1582 (TmpInstruction*) destForCast, dest,
1583 minstrVec, tempVec, target);
1584
1585 for (unsigned i=0; i < tempVec.size(); ++i)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001586 destMCFI.addTemp(tempVec[i]);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001587 }
1588 else
1589 destForCast = leftVal;
1590
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001591 M = CreateConvertToIntInstr(subtreeRoot->getOpLabel(),
1592 leftVal, destForCast);
Vikram S. Adve74825322002-03-18 03:15:35 +00001593 mvec.push_back(M);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001594
Vikram S. Adve74825322002-03-18 03:15:35 +00001595 // Append the copy code, if any, after the conversion instr.
1596 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001597 }
1598 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001599 }
1600
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001601 case 31: // reg: ToFloatTy(reg):
1602 case 32: // reg: ToDoubleTy(reg):
1603 case 232: // reg: ToDoubleTy(Constant):
1604
1605 // If this instruction has a parent (a user) in the tree
1606 // and the user is translated as an FsMULd instruction,
1607 // then the cast is unnecessary. So check that first.
1608 // In the future, we'll want to do the same for the FdMULq instruction,
1609 // so do the check here instead of only for ToFloatTy(reg).
1610 //
1611 if (subtreeRoot->parent() != NULL &&
Chris Lattner9c461082002-02-03 07:50:56 +00001612 MachineCodeForInstruction::get(((InstructionNode*)subtreeRoot->parent())->getInstruction())[0]->getOpCode() == FSMULD)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001613 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001614 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001615 }
1616 else
1617 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001618 Value* leftVal = subtreeRoot->leftChild()->getValue();
1619 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001620 MachineOpCode opCode=ChooseConvertToFloatInstr(
1621 subtreeRoot->getOpLabel(), opType);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001622 if (opCode == INVALID_OPCODE) // no conversion needed
1623 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001624 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001625 }
1626 else
1627 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001628 // If the source operand is a non-FP type it must be
1629 // first copied from int to float register via memory!
1630 Instruction *dest = subtreeRoot->getInstruction();
1631 Value* srcForCast;
1632 int n = 0;
1633 if (opType != Type::FloatTy && opType != Type::DoubleTy)
1634 {
1635 // Create a temporary to represent the FP register
1636 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001637 // The type of this temporary will determine the FP
1638 // register used: single-prec for a 32-bit int or smaller,
1639 // double-prec for a 64-bit int.
1640 //
1641 const Type* srcTypeToUse =
1642 (leftVal->getType() == Type::LongTy)? Type::DoubleTy
1643 : Type::FloatTy;
1644
Chris Lattner9c461082002-02-03 07:50:56 +00001645 srcForCast = new TmpInstruction(srcTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001646 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001647 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001648 destMCFI.addTemp(srcForCast);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001649
1650 vector<MachineInstr*> minstrVec;
1651 vector<TmpInstruction*> tempVec;
1652 target.getInstrInfo().CreateCodeToCopyIntToFloat(
1653 dest->getParent()->getParent(),
1654 leftVal, (TmpInstruction*) srcForCast,
1655 minstrVec, tempVec, target);
1656
Vikram S. Adve74825322002-03-18 03:15:35 +00001657 mvec.insert(mvec.end(), minstrVec.begin(),minstrVec.end());
1658
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001659 for (unsigned i=0; i < tempVec.size(); ++i)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001660 destMCFI.addTemp(tempVec[i]);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001661 }
1662 else
1663 srcForCast = leftVal;
1664
Vikram S. Adve74825322002-03-18 03:15:35 +00001665 M = new MachineInstr(opCode);
1666 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1667 srcForCast);
1668 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1669 dest);
1670 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001671 }
1672 }
1673 break;
1674
1675 case 19: // reg: ToArrayTy(reg):
1676 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001677 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001678 break;
1679
1680 case 233: // reg: Add(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001681 M = CreateAddConstInstruction(subtreeRoot);
1682 if (M != NULL)
1683 {
1684 mvec.push_back(M);
1685 break;
1686 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001687 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001688
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001689 case 33: // reg: Add(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001690 mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot)));
1691 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001692 break;
1693
1694 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001695 M = CreateSubConstInstruction(subtreeRoot);
1696 if (M != NULL)
1697 {
1698 mvec.push_back(M);
1699 break;
1700 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001701 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001702
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001703 case 34: // reg: Sub(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001704 mvec.push_back(new MachineInstr(ChooseSubInstructionByType(
1705 subtreeRoot->getInstruction()->getType())));
1706 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001707 break;
1708
1709 case 135: // reg: Mul(todouble, todouble)
1710 checkCast = true;
1711 // FALL THROUGH
1712
1713 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001714 {
1715 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1716 ? FSMULD
1717 : INVALID_MACHINE_OPCODE);
1718 CreateMulInstruction(target,
1719 subtreeRoot->leftChild()->getValue(),
1720 subtreeRoot->rightChild()->getValue(),
1721 subtreeRoot->getInstruction(),
1722 mvec, forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001723 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001724 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001725 case 335: // reg: Mul(todouble, todoubleConst)
1726 checkCast = true;
1727 // FALL THROUGH
1728
1729 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001730 {
1731 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1732 ? FSMULD
1733 : INVALID_MACHINE_OPCODE);
1734 CreateMulInstruction(target,
1735 subtreeRoot->leftChild()->getValue(),
1736 subtreeRoot->rightChild()->getValue(),
1737 subtreeRoot->getInstruction(),
1738 mvec, forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001739 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001740 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001741 case 236: // reg: Div(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001742 L = mvec.size();
1743 CreateDivConstInstruction(target, subtreeRoot, mvec);
1744 if (mvec.size() > L)
1745 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001746 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001747
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001748 case 36: // reg: Div(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001749 mvec.push_back(new MachineInstr(ChooseDivInstruction(target, subtreeRoot)));
1750 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001751 break;
1752
1753 case 37: // reg: Rem(reg, reg)
1754 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001755 {
1756 Instruction* remInstr = subtreeRoot->getInstruction();
1757
Chris Lattner9c461082002-02-03 07:50:56 +00001758 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001759 subtreeRoot->leftChild()->getValue(),
1760 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001761 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001762 quot,
1763 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001764 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001765
Vikram S. Adve74825322002-03-18 03:15:35 +00001766 M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1767 Set3OperandsFromInstr(M, subtreeRoot, target);
1768 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot);
1769 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001770
Vikram S. Adve74825322002-03-18 03:15:35 +00001771 M = new MachineInstr(ChooseMulInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001772 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001773 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,quot);
1774 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
Vikram S. Adve510eec72001-11-04 21:59:14 +00001775 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001776 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,prod);
1777 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001778
Vikram S. Adve74825322002-03-18 03:15:35 +00001779 M = new MachineInstr(ChooseSubInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001780 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001781 Set3OperandsFromInstr(M, subtreeRoot, target);
1782 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod);
1783 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001784
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001785 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001786 }
1787
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001788 case 38: // bool: And(bool, bool)
1789 case 238: // bool: And(bool, boolconst)
1790 case 338: // reg : BAnd(reg, reg)
1791 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001792 mvec.push_back(new MachineInstr(AND));
1793 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001794 break;
1795
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001796 case 138: // bool: And(bool, not)
1797 case 438: // bool: BAnd(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001798 mvec.push_back(new MachineInstr(ANDN));
1799 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001800 break;
1801
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001802 case 39: // bool: Or(bool, bool)
1803 case 239: // bool: Or(bool, boolconst)
1804 case 339: // reg : BOr(reg, reg)
1805 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001806 mvec.push_back(new MachineInstr(ORN));
1807 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001808 break;
1809
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001810 case 139: // bool: Or(bool, not)
1811 case 439: // bool: BOr(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001812 mvec.push_back(new MachineInstr(ORN));
1813 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001814 break;
1815
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001816 case 40: // bool: Xor(bool, bool)
1817 case 240: // bool: Xor(bool, boolconst)
1818 case 340: // reg : BXor(reg, reg)
1819 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001820 mvec.push_back(new MachineInstr(XOR));
1821 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001822 break;
1823
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001824 case 140: // bool: Xor(bool, not)
1825 case 440: // bool: BXor(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001826 mvec.push_back(new MachineInstr(XNOR));
1827 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001828 break;
1829
1830 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001831 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001832 // If the SetCC was folded into the user (parent), it will be
1833 // caught above. All other cases are the same as case 42,
1834 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001835 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001836 case 42: // bool: SetCC(reg, reg):
1837 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001838 // This generates a SUBCC instruction, putting the difference in
1839 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001840 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001841 // If the boolean result of the SetCC is used by anything other
1842 // than a single branch instruction, the boolean must be
1843 // computed and stored in the result register. Otherwise, discard
1844 // the difference (by using %g0) and keep only the condition code.
1845 //
1846 // To compute the boolean result in a register we use a conditional
1847 // move, unless the result of the SUBCC instruction can be used as
1848 // the bool! This assumes that zero is FALSE and any non-zero
1849 // integer is TRUE.
1850 //
1851 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1852 Instruction* setCCInstr = subtreeRoot->getInstruction();
1853 bool keepBoolVal = (parentNode == NULL ||
1854 parentNode->getInstruction()->getOpcode()
1855 != Instruction::Br);
1856 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001857 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1858 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1859
1860 bool mustClearReg;
1861 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001862 MachineOpCode movOpCode = 0;
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001863
1864 // Mark the 4th operand as being a CC register, and as a def
1865 // A TmpInstruction is created to represent the CC "result".
1866 // Unlike other instances of TmpInstruction, this one is used
1867 // by machine code of multiple LLVM instructions, viz.,
1868 // the SetCC and the branch. Make sure to get the same one!
1869 // Note that we do this even for FP CC registers even though they
1870 // are explicit operands, because the type of the operand
1871 // needs to be a floating point condition code, not an integer
1872 // condition code. Think of this as casting the bool result to
1873 // a FP condition code register.
1874 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001875 Value* leftVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001876 bool isFPCompare = (leftVal->getType() == Type::FloatTy ||
1877 leftVal->getType() == Type::DoubleTy);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001878
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001879 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1880 setCCInstr->getParent()->getParent(),
1881 isFPCompare? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001882 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001883
1884 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001885 {
1886 // Integer condition: dest. should be %g0 or an integer register.
1887 // If result must be saved but condition is not SetEQ then we need
1888 // a separate instruction to compute the bool result, so discard
1889 // result of SUBcc instruction anyway.
1890 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001891 M = new MachineInstr(SUBcc);
1892 Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal);
1893 M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister,
1894 tmpForCC, /*def*/true);
1895 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001896
1897 if (computeBoolVal)
1898 { // recompute bool using the integer condition codes
1899 movOpCode =
1900 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1901 }
1902 }
1903 else
1904 {
1905 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve74825322002-03-18 03:15:35 +00001906 M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1907 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001908 tmpForCC);
Vikram S. Adve74825322002-03-18 03:15:35 +00001909 M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001910 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001911 M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001912 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001913 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001914
1915 if (computeBoolVal)
1916 {// recompute bool using the FP condition codes
1917 mustClearReg = true;
1918 valueToMove = 1;
1919 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1920 }
1921 }
1922
1923 if (computeBoolVal)
1924 {
1925 if (mustClearReg)
1926 {// Unconditionally set register to 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001927 M = new MachineInstr(SETHI);
1928 M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed,
1929 (int64_t)0);
1930 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1931 setCCInstr);
1932 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001933 }
1934
1935 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve74825322002-03-18 03:15:35 +00001936 M = new MachineInstr(movOpCode);
1937 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1938 tmpForCC);
1939 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
1940 valueToMove);
1941 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1942 setCCInstr);
1943 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001944 }
1945 break;
1946 }
1947
1948 case 43: // boolreg: VReg
1949 case 44: // boolreg: Constant
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001950 break;
1951
1952 case 51: // reg: Load(reg)
1953 case 52: // reg: Load(ptrreg)
1954 case 53: // reg: LoadIdx(reg,reg)
1955 case 54: // reg: LoadIdx(ptrreg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001956 mvec.push_back(new MachineInstr(ChooseLoadInstruction(
1957 subtreeRoot->getValue()->getType())));
1958 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001959 break;
1960
1961 case 55: // reg: GetElemPtr(reg)
1962 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001963 // If the GetElemPtr was folded into the user (parent), it will be
1964 // caught above. For other cases, we have to compute the address.
Vikram S. Adve74825322002-03-18 03:15:35 +00001965 mvec.push_back(new MachineInstr(ADD));
1966 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001967 break;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001968
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001969 case 57: // reg: Alloca: Implement as 1 instruction:
1970 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001971 AllocationInst* instr =
1972 cast<AllocationInst>(subtreeRoot->getInstruction());
1973 unsigned int tsize =
1974 target.findOptimalStorageSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00001975 assert(tsize != 0);
1976 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001977 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001978 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001979
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001980 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1981 // mul num, typeSz -> tmp
1982 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001983 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001984 AllocationInst* instr =
1985 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00001986 const Type* eltType = instr->getAllocatedType();
1987
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001988 // If #elements is constant, use simpler code for fixed-size allocas
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001989 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001990 Value* numElementsVal = NULL;
1991 bool isArray = instr->isArrayAllocation();
1992
1993 if (!isArray ||
1994 isa<Constant>(numElementsVal = instr->getArraySize()))
1995 { // total size is constant: generate code for fixed-size alloca
1996 unsigned int numElements = isArray?
1997 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
1998 CreateCodeForFixedSizeAlloca(target, instr, tsize,
1999 numElements, mvec);
2000 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002001 else // total size is not constant.
2002 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002003 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002004 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002005 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002006
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002007 case 61: // reg: Call
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002008 { // Generate a direct (CALL) or indirect (JMPL). depending
2009 // Mark the return-address register and the indirection
2010 // register (if any) as hidden virtual registers.
Vikram S. Advea995e602001-10-11 04:23:19 +00002011 // Also, mark the operands of the Call and return value (if
2012 // any) as implicit operands of the CALL machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002013 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002014 // If this is a varargs function, floating point arguments
2015 // have to passed in integer registers so insert
2016 // copy-float-to-int instructions for each float operand.
2017 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002018 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002019 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002020
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002021 // Create hidden virtual register for return address, with type void*.
2022 Instruction* retAddrReg =
2023 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner9c461082002-02-03 07:50:56 +00002024 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002025
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002026 // Generate the machine instruction and its operands.
2027 // Use CALL for direct function calls; this optimistically assumes
2028 // the PC-relative address fits in the CALL address field (22 bits).
2029 // Use JMPL for indirect calls.
2030 //
Chris Lattnerb0d04722002-03-26 17:58:12 +00002031 if (isa<Function>(callee))
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002032 { // direct function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002033 M = new MachineInstr(CALL);
2034 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
2035 callee);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002036 }
2037 else
2038 { // indirect function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002039 M = new MachineInstr(JMPLCALL);
2040 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
2041 callee);
2042 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
2043 (int64_t) 0);
2044 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
2045 retAddrReg);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002046 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002047
Vikram S. Adve74825322002-03-18 03:15:35 +00002048 mvec.push_back(M);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002049
2050 // WARNING: Operands 0..N-1 must go in slots 0..N-1 of implicitUses.
2051 // The result value must go in slot N. This is assumed
2052 // in register allocation.
2053 //
Vikram S. Advea995e602001-10-11 04:23:19 +00002054 // Add the call operands and return value as implicit refs
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002055 // const Type* funcType = isa<Function>(callee)? callee->getType()
2056 // : cast<PointerType>(callee->getType())->getElementType();
2057 const Type* funcType = callee->getType();
2058 bool isVarArgs = cast<FunctionType>(cast<PointerType>(funcType)
2059 ->getElementType())->isVarArg();
2060
Vikram S. Advea995e602001-10-11 04:23:19 +00002061 for (unsigned i=0, N=callInstr->getNumOperands(); i < N; ++i)
2062 if (callInstr->getOperand(i) != callee)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002063 {
2064 Value* argVal = callInstr->getOperand(i);
2065
2066 // Check for FP arguments to varargs functions
2067 if (isVarArgs && argVal->getType()->isFloatingPoint())
2068 { // Add a copy-float-to-int instruction
2069 MachineCodeForInstruction &destMCFI =
2070 MachineCodeForInstruction::get(callInstr);
2071 Instruction* intArgReg =
2072 new TmpInstruction(Type::IntTy, argVal);
2073 destMCFI.addTemp(intArgReg);
2074
2075 vector<MachineInstr*> minstrVec;
2076 vector<TmpInstruction*> tempVec;
2077 target.getInstrInfo().CreateCodeToCopyFloatToInt(
2078 callInstr->getParent()->getParent(),
2079 argVal, (TmpInstruction*) intArgReg,
2080 minstrVec, tempVec, target);
2081
2082 mvec.insert(mvec.begin(), minstrVec.begin(),minstrVec.end());
2083
2084 for (unsigned i=0; i < tempVec.size(); ++i)
2085 destMCFI.addTemp(tempVec[i]);
2086
2087 argVal = intArgReg;
2088 }
2089
2090 mvec.back()->addImplicitRef(argVal);
2091 }
Vikram S. Advea995e602001-10-11 04:23:19 +00002092
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002093 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002094 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002095
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002096 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattnerb0d04722002-03-26 17:58:12 +00002097 if (isa<Function>(callee))
Vikram S. Adve74825322002-03-18 03:15:35 +00002098 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002099
Vikram S. Adve74825322002-03-18 03:15:35 +00002100 // delay slot
2101 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002102 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002103 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002104
2105 case 62: // reg: Shl(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002106 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002107 assert(opType->isIntegral()
2108 || opType == Type::BoolTy
2109 || opType->isPointerType()&& "Shl unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002110 mvec.push_back(new MachineInstr((opType == Type::LongTy)? SLLX : SLL));
2111 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002112 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002113 }
2114
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002115 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002116 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002117 assert(opType->isIntegral()
2118 || opType == Type::BoolTy
2119 || opType->isPointerType() &&"Shr unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002120 mvec.push_back(new MachineInstr((opType->isSigned()
2121 ? ((opType == Type::LongTy)? SRAX : SRA)
2122 : ((opType == Type::LongTy)? SRLX : SRL))));
2123 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002124 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002125 }
2126
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002127 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002128 break; // don't forward the value
2129
Vikram S. Adve3438b212001-11-12 18:54:11 +00002130#undef NEED_PHI_MACHINE_INSTRS
2131#ifdef NEED_PHI_MACHINE_INSTRS
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002132 { // This instruction has variable #operands, so resultPos is 0.
2133 Instruction* phi = subtreeRoot->getInstruction();
Vikram S. Adve74825322002-03-18 03:15:35 +00002134 M = new MachineInstr(PHI, 1 + phi->getNumOperands());
2135 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002136 subtreeRoot->getValue());
2137 for (unsigned i=0, N=phi->getNumOperands(); i < N; i++)
Vikram S. Adve74825322002-03-18 03:15:35 +00002138 M->SetMachineOperandVal(i+1, MachineOperand::MO_VirtualRegister,
2139 phi->getOperand(i));
2140 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002141 break;
2142 }
Chris Lattner697954c2002-01-20 22:54:45 +00002143#endif // NEED_PHI_MACHINE_INSTRS
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002144
Vikram S. Adve74825322002-03-18 03:15:35 +00002145
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002146 case 71: // reg: VReg
2147 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002148 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002149
2150 default:
2151 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002152 break;
2153 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002154 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002155
2156 if (forwardOperandNum >= 0)
2157 { // We did not generate a machine instruction but need to use operand.
2158 // If user is in the same tree, replace Value in its machine operand.
2159 // If not, insert a copy instruction which should get coalesced away
2160 // by register allocation.
2161 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002162 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002163 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002164 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002165 vector<MachineInstr*> minstrVec;
Vikram S. Adve74825322002-03-18 03:15:35 +00002166 target.getInstrInfo().CreateCopyInstructionsByType(target,
2167 subtreeRoot->getInstruction()->getParent()->getParent(),
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002168 subtreeRoot->getInstruction()->getOperand(forwardOperandNum),
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002169 subtreeRoot->getInstruction(), minstrVec);
2170 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002171 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002172 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002173 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002174}
2175
2176