blob: 533d74c15f91bf3c31f2d003ebf8d8052ee26080 [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. Adve8557b222001-10-10 20:56:33 +000015#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000016#include "llvm/CodeGen/MachineInstr.h"
17#include "llvm/CodeGen/InstrForest.h"
18#include "llvm/CodeGen/InstrSelection.h"
19#include "llvm/Support/MathExtras.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/iTerminators.h"
22#include "llvm/iMemory.h"
23#include "llvm/iOther.h"
24#include "llvm/BasicBlock.h"
25#include "llvm/Method.h"
26#include "llvm/ConstPoolVals.h"
Chris Lattner749655f2001-10-13 06:54:30 +000027#include <math.h>
Chris Lattner20b1ea02001-09-14 03:47:57 +000028
29//******************** Internal Data Declarations ************************/
30
Chris Lattner20b1ea02001-09-14 03:47:57 +000031
32//************************* Forward Declarations ***************************/
33
34
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000035static void SetMemOperands_Internal (MachineInstr* minstr,
36 const InstructionNode* vmInstrNode,
37 Value* ptrVal,
38 Value* arrayOffsetVal,
39 const vector<ConstPoolVal*>& idxVec,
40 const TargetMachine& target);
Chris Lattner20b1ea02001-09-14 03:47:57 +000041
42
43//************************ Internal Functions ******************************/
44
Chris Lattner20b1ea02001-09-14 03:47:57 +000045
Chris Lattner20b1ea02001-09-14 03:47:57 +000046static inline MachineOpCode
47ChooseBprInstruction(const InstructionNode* instrNode)
48{
49 MachineOpCode opCode;
50
51 Instruction* setCCInstr =
52 ((InstructionNode*) instrNode->leftChild())->getInstruction();
53
54 switch(setCCInstr->getOpcode())
55 {
56 case Instruction::SetEQ: opCode = BRZ; break;
57 case Instruction::SetNE: opCode = BRNZ; break;
58 case Instruction::SetLE: opCode = BRLEZ; break;
59 case Instruction::SetGE: opCode = BRGEZ; break;
60 case Instruction::SetLT: opCode = BRLZ; break;
61 case Instruction::SetGT: opCode = BRGZ; break;
62 default:
63 assert(0 && "Unrecognized VM instruction!");
64 opCode = INVALID_OPCODE;
65 break;
66 }
67
68 return opCode;
69}
70
71
72static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +000073ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000074 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +000075{
76 MachineOpCode opCode = INVALID_OPCODE;
77
78 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
79
80 if (isSigned)
81 {
82 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000083 {
84 case Instruction::SetEQ: opCode = BE; break;
85 case Instruction::SetNE: opCode = BNE; break;
86 case Instruction::SetLE: opCode = BLE; break;
87 case Instruction::SetGE: opCode = BGE; break;
88 case Instruction::SetLT: opCode = BL; break;
89 case Instruction::SetGT: opCode = BG; break;
90 default:
91 assert(0 && "Unrecognized VM instruction!");
92 break;
93 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000094 }
95 else
96 {
97 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000098 {
99 case Instruction::SetEQ: opCode = BE; break;
100 case Instruction::SetNE: opCode = BNE; break;
101 case Instruction::SetLE: opCode = BLEU; break;
102 case Instruction::SetGE: opCode = BCC; break;
103 case Instruction::SetLT: opCode = BCS; break;
104 case Instruction::SetGT: opCode = BGU; break;
105 default:
106 assert(0 && "Unrecognized VM instruction!");
107 break;
108 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000109 }
110
111 return opCode;
112}
113
114static inline MachineOpCode
115ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000116 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000117{
118 MachineOpCode opCode = INVALID_OPCODE;
119
120 switch(setCCInstr->getOpcode())
121 {
122 case Instruction::SetEQ: opCode = FBE; break;
123 case Instruction::SetNE: opCode = FBNE; break;
124 case Instruction::SetLE: opCode = FBLE; break;
125 case Instruction::SetGE: opCode = FBGE; break;
126 case Instruction::SetLT: opCode = FBL; break;
127 case Instruction::SetGT: opCode = FBG; break;
128 default:
129 assert(0 && "Unrecognized VM instruction!");
130 break;
131 }
132
133 return opCode;
134}
135
136
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000137// Create a unique TmpInstruction for a boolean value,
138// representing the CC register used by a branch on that value.
139// For now, hack this using a little static cache of TmpInstructions.
140// Eventually the entire BURG instruction selection should be put
141// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000142// The static cache is not too bad because the memory for these
143// TmpInstructions will be freed along with the rest of the Method anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000144//
145static TmpInstruction*
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000146GetTmpForCC(Value* boolVal, const Method* method, const Type* ccType)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000147{
148 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
149 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
150 static const Method* lastMethod = NULL; // Use to flush cache between methods
151
152 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
153
154 if (lastMethod != method)
155 {
156 lastMethod = method;
157 boolToTmpCache.clear();
158 }
159
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000160 // Look for tmpI and create a new one otherwise. The new value is
161 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000162 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
163 if (tmpI == NULL)
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000164 tmpI = new TmpInstruction(TMP_INSTRUCTION_OPCODE, ccType, boolVal, NULL);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000165
166 return tmpI;
167}
168
169
Chris Lattner20b1ea02001-09-14 03:47:57 +0000170static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000171ChooseBccInstruction(const InstructionNode* instrNode,
172 bool& isFPBranch)
173{
174 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
175 BinaryOperator* setCCInstr = (BinaryOperator*) setCCNode->getInstruction();
176 const Type* setCCType = setCCInstr->getOperand(0)->getType();
177
178 isFPBranch = (setCCType == Type::FloatTy || setCCType == Type::DoubleTy);
179
180 if (isFPBranch)
181 return ChooseBFpccInstruction(instrNode, setCCInstr);
182 else
183 return ChooseBpccInstruction(instrNode, setCCInstr);
184}
185
186
187static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000188ChooseMovFpccInstruction(const InstructionNode* instrNode)
189{
190 MachineOpCode opCode = INVALID_OPCODE;
191
192 switch(instrNode->getInstruction()->getOpcode())
193 {
194 case Instruction::SetEQ: opCode = MOVFE; break;
195 case Instruction::SetNE: opCode = MOVFNE; break;
196 case Instruction::SetLE: opCode = MOVFLE; break;
197 case Instruction::SetGE: opCode = MOVFGE; break;
198 case Instruction::SetLT: opCode = MOVFL; break;
199 case Instruction::SetGT: opCode = MOVFG; break;
200 default:
201 assert(0 && "Unrecognized VM instruction!");
202 break;
203 }
204
205 return opCode;
206}
207
208
209// Assumes that SUBcc v1, v2 -> v3 has been executed.
210// In most cases, we want to clear v3 and then follow it by instruction
211// MOVcc 1 -> v3.
212// Set mustClearReg=false if v3 need not be cleared before conditional move.
213// Set valueToMove=0 if we want to conditionally move 0 instead of 1
214// (i.e., we want to test inverse of a condition)
Vikram S. Adve243dd452001-09-18 13:03:13 +0000215// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000216//
217static MachineOpCode
218ChooseMovpccAfterSub(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000219 bool& mustClearReg,
220 int& valueToMove)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000221{
222 MachineOpCode opCode = INVALID_OPCODE;
223 mustClearReg = true;
224 valueToMove = 1;
225
226 switch(instrNode->getInstruction()->getOpcode())
227 {
Vikram S. Adve243dd452001-09-18 13:03:13 +0000228 case Instruction::SetEQ: opCode = MOVE; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000229 case Instruction::SetLE: opCode = MOVLE; break;
230 case Instruction::SetGE: opCode = MOVGE; break;
231 case Instruction::SetLT: opCode = MOVL; break;
232 case Instruction::SetGT: opCode = MOVG; break;
Vikram S. Adve243dd452001-09-18 13:03:13 +0000233 case Instruction::SetNE: assert(0 && "No move required!"); break;
234 default: assert(0 && "Unrecognized VM instr!"); break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000235 }
236
237 return opCode;
238}
239
Chris Lattner20b1ea02001-09-14 03:47:57 +0000240static inline MachineOpCode
241ChooseConvertToFloatInstr(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000242 const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000243{
244 MachineOpCode opCode = INVALID_OPCODE;
245
246 switch(instrNode->getOpLabel())
247 {
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. Adveff5a09e2001-11-08 05:04:09 +0000262 // Use FXTOD for all integer-to-double conversions. This has to be
263 // consistent with the code in CreateCodeToCopyIntToFloat() since
264 // that will be used to load the integer into an FP register.
265 //
266 if (opType == Type::SByteTy || opType == Type::ShortTy ||
267 opType == Type::IntTy || opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000268 opCode = FXTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000269 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000270 opCode = FSTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000271 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000272 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000273 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000274 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000275 break;
276
277 default:
278 break;
279 }
280
281 return opCode;
282}
283
284static inline MachineOpCode
285ChooseConvertToIntInstr(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000286 const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000287{
288 MachineOpCode opCode = INVALID_OPCODE;;
289
290 int instrType = (int) instrNode->getOpLabel();
291
292 if (instrType == ToSByteTy || instrType == ToShortTy || instrType == ToIntTy)
293 {
294 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000295 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000296 case Type::FloatTyID: opCode = FSTOI; break;
297 case Type::DoubleTyID: opCode = FDTOI; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000298 default:
299 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
300 break;
301 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000302 }
303 else if (instrType == ToLongTy)
304 {
305 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000306 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000307 case Type::FloatTyID: opCode = FSTOX; break;
308 case Type::DoubleTyID: opCode = FDTOX; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000309 default:
310 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
311 break;
312 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000313 }
314 else
315 assert(0 && "Should not get here, Mo!");
316
317 return opCode;
318}
319
320
321static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000322ChooseAddInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000323{
324 MachineOpCode opCode = INVALID_OPCODE;
325
Chris Lattner20b1ea02001-09-14 03:47:57 +0000326 if (resultType->isIntegral() ||
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000327 resultType->isPointerType() ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000328 resultType->isLabelType() ||
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000329 isa<MethodType>(resultType) ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000330 resultType == Type::BoolTy)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000331 {
332 opCode = ADD;
333 }
334 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000335 switch(resultType->getPrimitiveID())
336 {
337 case Type::FloatTyID: opCode = FADDS; break;
338 case Type::DoubleTyID: opCode = FADDD; break;
339 default: assert(0 && "Invalid type for ADD instruction"); break;
340 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000341
342 return opCode;
343}
344
345
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000346static inline MachineOpCode
347ChooseAddInstruction(const InstructionNode* instrNode)
348{
349 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
350}
351
352
Chris Lattner20b1ea02001-09-14 03:47:57 +0000353static inline MachineInstr*
354CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000355 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000356{
357 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000358 ? FMOVS : FMOVD);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000359 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000360 instrNode->leftChild()->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000361 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000362 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000363 return minstr;
364}
365
366static inline MachineInstr*
367CreateAddConstInstruction(const InstructionNode* instrNode)
368{
369 MachineInstr* minstr = NULL;
370
371 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000372 assert(isa<ConstPoolVal>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000373
374 // Cases worth optimizing are:
375 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
376 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
377 //
378 const Type* resultType = instrNode->getInstruction()->getType();
379
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000380 if (resultType == Type::FloatTy ||
381 resultType == Type::DoubleTy)
382 {
383 double dval = ((ConstPoolFP*) constOp)->getValue();
384 if (dval == 0.0)
385 minstr = CreateMovFloatInstruction(instrNode, resultType);
386 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000387
388 return minstr;
389}
390
391
392static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000393ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000394{
395 MachineOpCode opCode = INVALID_OPCODE;
396
Chris Lattner20b1ea02001-09-14 03:47:57 +0000397 if (resultType->isIntegral() ||
398 resultType->isPointerType())
399 {
400 opCode = SUB;
401 }
402 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000403 switch(resultType->getPrimitiveID())
404 {
405 case Type::FloatTyID: opCode = FSUBS; break;
406 case Type::DoubleTyID: opCode = FSUBD; break;
407 default: assert(0 && "Invalid type for SUB instruction"); break;
408 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000409
410 return opCode;
411}
412
413
414static inline MachineInstr*
415CreateSubConstInstruction(const InstructionNode* instrNode)
416{
417 MachineInstr* minstr = NULL;
418
419 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000420 assert(isa<ConstPoolVal>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000421
422 // Cases worth optimizing are:
423 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
424 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
425 //
426 const Type* resultType = instrNode->getInstruction()->getType();
427
428 if (resultType == Type::FloatTy ||
429 resultType == Type::DoubleTy)
430 {
431 double dval = ((ConstPoolFP*) constOp)->getValue();
432 if (dval == 0.0)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000433 minstr = CreateMovFloatInstruction(instrNode, resultType);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000434 }
435
436 return minstr;
437}
438
439
440static inline MachineOpCode
441ChooseFcmpInstruction(const InstructionNode* instrNode)
442{
443 MachineOpCode opCode = INVALID_OPCODE;
444
445 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
446 switch(operand->getType()->getPrimitiveID()) {
447 case Type::FloatTyID: opCode = FCMPS; break;
448 case Type::DoubleTyID: opCode = FCMPD; break;
449 default: assert(0 && "Invalid type for FCMP instruction"); break;
450 }
451
452 return opCode;
453}
454
455
456// Assumes that leftArg and rightArg are both cast instructions.
457//
458static inline bool
459BothFloatToDouble(const InstructionNode* instrNode)
460{
461 InstrTreeNode* leftArg = instrNode->leftChild();
462 InstrTreeNode* rightArg = instrNode->rightChild();
463 InstrTreeNode* leftArgArg = leftArg->leftChild();
464 InstrTreeNode* rightArgArg = rightArg->leftChild();
465 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
466
467 // Check if both arguments are floats cast to double
468 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000469 leftArgArg->getValue()->getType() == Type::FloatTy &&
470 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000471}
472
473
474static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000475ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000476{
477 MachineOpCode opCode = INVALID_OPCODE;
478
Chris Lattner20b1ea02001-09-14 03:47:57 +0000479 if (resultType->isIntegral())
Vikram S. Adve510eec72001-11-04 21:59:14 +0000480 opCode = MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000481 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000482 switch(resultType->getPrimitiveID())
483 {
484 case Type::FloatTyID: opCode = FMULS; break;
485 case Type::DoubleTyID: opCode = FMULD; break;
486 default: assert(0 && "Invalid type for MUL instruction"); break;
487 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000488
489 return opCode;
490}
491
492
Vikram S. Adve510eec72001-11-04 21:59:14 +0000493static inline MachineOpCode
494ChooseMulInstruction(const InstructionNode* instrNode,
495 bool checkCasts)
496{
497 if (checkCasts && BothFloatToDouble(instrNode))
498 return FSMULD;
499
500 // else use the regular multiply instructions
501 return ChooseMulInstructionByType(instrNode->getInstruction()->getType());
502}
503
504
Chris Lattner20b1ea02001-09-14 03:47:57 +0000505static inline MachineInstr*
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000506CreateIntNegInstruction(TargetMachine& target,
507 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000508{
509 MachineInstr* minstr = new MachineInstr(SUB);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000510 minstr->SetMachineOperand(0, target.getRegInfo().getZeroRegNum());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000511 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, vreg);
512 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, vreg);
513 return minstr;
514}
515
516
517static inline MachineInstr*
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000518CreateMulConstInstruction(TargetMachine &target,
519 const InstructionNode* instrNode,
520 MachineInstr*& getMinstr2)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000521{
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000522 MachineInstr* minstr = NULL; // return NULL if we cannot exploit constant
523 getMinstr2 = NULL; // to create a cheaper instruction
Chris Lattner20b1ea02001-09-14 03:47:57 +0000524 bool needNeg = false;
525
526 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000527 assert(isa<ConstPoolVal>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000528
529 // Cases worth optimizing are:
530 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
531 // (2) Multiply by 2^x for integer types: replace with Shift
532 //
533 const Type* resultType = instrNode->getInstruction()->getType();
534
Vikram S. Adve243dd452001-09-18 13:03:13 +0000535 if (resultType->isIntegral() || resultType->isPointerType())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000536 {
537 unsigned pow;
538 bool isValidConst;
539 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
540 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000541 {
542 bool needNeg = false;
543 if (C < 0)
544 {
545 needNeg = true;
546 C = -C;
547 }
548
549 if (C == 0 || C == 1)
550 {
551 minstr = new MachineInstr(ADD);
552
553 if (C == 0)
554 minstr->SetMachineOperand(0,
555 target.getRegInfo().getZeroRegNum());
556 else
557 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
558 instrNode->leftChild()->getValue());
559 minstr->SetMachineOperand(1,target.getRegInfo().getZeroRegNum());
560 }
561 else if (IsPowerOf2(C, pow))
562 {
563 minstr = new MachineInstr((resultType == Type::LongTy)
564 ? SLLX : SLL);
565 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
566 instrNode->leftChild()->getValue());
567 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
568 pow);
569 }
570
571 if (minstr && needNeg)
572 { // insert <reg = SUB 0, reg> after the instr to flip the sign
573 getMinstr2 = CreateIntNegInstruction(target,
574 instrNode->getValue());
575 }
576 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000577 }
578 else
579 {
580 if (resultType == Type::FloatTy ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000581 resultType == Type::DoubleTy)
582 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000583 double dval = ((ConstPoolFP*) constOp)->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000584 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000585 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000586 bool needNeg = (dval < 0);
587
588 MachineOpCode opCode = needNeg
589 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
590 : (resultType == Type::FloatTy? FMOVS : FMOVD);
591
592 minstr = new MachineInstr(opCode);
593 minstr->SetMachineOperand(0,
594 MachineOperand::MO_VirtualRegister,
595 instrNode->leftChild()->getValue());
596 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000597 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000598 }
599
600 if (minstr != NULL)
601 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000602 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000603
604 return minstr;
605}
606
607
Vikram S. Adve510eec72001-11-04 21:59:14 +0000608// Generate a divide instruction for Div or Rem.
609// For Rem, this assumes that the operand type will be signed if the result
610// type is signed. This is correct because they must have the same sign.
611//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000612static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000613ChooseDivInstruction(TargetMachine &target,
614 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000615{
616 MachineOpCode opCode = INVALID_OPCODE;
617
618 const Type* resultType = instrNode->getInstruction()->getType();
619
620 if (resultType->isIntegral())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000621 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000622 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000623 switch(resultType->getPrimitiveID())
624 {
625 case Type::FloatTyID: opCode = FDIVS; break;
626 case Type::DoubleTyID: opCode = FDIVD; break;
627 default: assert(0 && "Invalid type for DIV instruction"); break;
628 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000629
630 return opCode;
631}
632
633
634static inline MachineInstr*
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000635CreateDivConstInstruction(TargetMachine &target,
636 const InstructionNode* instrNode,
637 MachineInstr*& getMinstr2)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000638{
639 MachineInstr* minstr = NULL;
640 getMinstr2 = NULL;
641
642 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000643 assert(isa<ConstPoolVal>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000644
645 // Cases worth optimizing are:
646 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
647 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
648 //
649 const Type* resultType = instrNode->getInstruction()->getType();
650
651 if (resultType->isIntegral())
652 {
653 unsigned pow;
654 bool isValidConst;
655 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
656 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000657 {
658 bool needNeg = false;
659 if (C < 0)
660 {
661 needNeg = true;
662 C = -C;
663 }
664
665 if (C == 1)
666 {
667 minstr = new MachineInstr(ADD);
668 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
669 instrNode->leftChild()->getValue());
670 minstr->SetMachineOperand(1,target.getRegInfo().getZeroRegNum());
671 }
672 else if (IsPowerOf2(C, pow))
673 {
674 MachineOpCode opCode= ((resultType->isSigned())
675 ? (resultType==Type::LongTy)? SRAX : SRA
676 : (resultType==Type::LongTy)? SRLX : SRL);
677 minstr = new MachineInstr(opCode);
678 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
679 instrNode->leftChild()->getValue());
680 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
681 pow);
682 }
683
684 if (minstr && needNeg)
685 { // insert <reg = SUB 0, reg> after the instr to flip the sign
686 getMinstr2 = CreateIntNegInstruction(target,
687 instrNode->getValue());
688 }
689 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000690 }
691 else
692 {
693 if (resultType == Type::FloatTy ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000694 resultType == Type::DoubleTy)
695 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000696 double dval = ((ConstPoolFP*) constOp)->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000697 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000698 {
699 bool needNeg = (dval < 0);
700
701 MachineOpCode opCode = needNeg
702 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
703 : (resultType == Type::FloatTy? FMOVS : FMOVD);
704
705 minstr = new MachineInstr(opCode);
706 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
707 instrNode->leftChild()->getValue());
708 }
709 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000710 }
711
712 if (minstr != NULL)
713 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000714 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000715
716 return minstr;
717}
718
719
Chris Lattner20b1ea02001-09-14 03:47:57 +0000720//------------------------------------------------------------------------
721// Function SetOperandsForMemInstr
722//
723// Choose addressing mode for the given load or store instruction.
724// Use [reg+reg] if it is an indexed reference, and the index offset is
725// not a constant or if it cannot fit in the offset field.
726// Use [reg+offset] in all other cases.
727//
728// This assumes that all array refs are "lowered" to one of these forms:
729// %x = load (subarray*) ptr, constant ; single constant offset
730// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
731// Generally, this should happen via strength reduction + LICM.
732// Also, strength reduction should take care of using the same register for
733// the loop index variable and an array index, when that is profitable.
734//------------------------------------------------------------------------
735
736static void
737SetOperandsForMemInstr(MachineInstr* minstr,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000738 const InstructionNode* vmInstrNode,
739 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000740{
741 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
742
743 // Variables to hold the index vector, ptr value, and offset value.
744 // The major work here is to extract these for all 3 instruction types
745 // and then call the common function SetMemOperands_Internal().
746 //
Chris Lattner8e7f4092001-11-04 08:08:34 +0000747 const vector<ConstPoolVal*>* idxVec = &memInst->getIndices();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000748 vector<ConstPoolVal*>* newIdxVec = NULL;
749 Value* ptrVal;
750 Value* arrayOffsetVal = NULL;
751
752 // Test if a GetElemPtr instruction is being folded into this mem instrn.
753 // If so, it will be in the left child for Load and GetElemPtr,
754 // and in the right child for Store instructions.
755 //
756 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000757 ? vmInstrNode->rightChild()
758 : vmInstrNode->leftChild());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000759
760 if (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
761 ptrChild->getOpLabel() == GetElemPtrIdx)
762 {
763 // There is a GetElemPtr instruction and there may be a chain of
764 // more than one. Use the pointer value of the last one in the chain.
765 // Fold the index vectors from the entire chain and from the mem
766 // instruction into one single index vector.
767 // Finally, we never fold for an array instruction so make that NULL.
768
769 newIdxVec = new vector<ConstPoolVal*>;
770 ptrVal = FoldGetElemChain((InstructionNode*) ptrChild, *newIdxVec);
771
772 newIdxVec->insert(newIdxVec->end(), idxVec->begin(), idxVec->end());
773 idxVec = newIdxVec;
774
775 assert(! ((PointerType*)ptrVal->getType())->getValueType()->isArrayType()
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000776 && "GetElemPtr cannot be folded into array refs in selection");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000777 }
778 else
779 {
780 // There is no GetElemPtr instruction.
781 // Use the pointer value and the index vector from the Mem instruction.
782 // If it is an array reference, get the array offset value.
783 //
784 ptrVal = memInst->getPtrOperand();
785
786 const Type* opType =
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000787 ((const PointerType*) ptrVal->getType())->getValueType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000788 if (opType->isArrayType())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000789 {
790 assert((memInst->getNumOperands()
791 == (unsigned) 1 + memInst->getFirstOffsetIdx())
792 && "Array refs must be lowered before Instruction Selection");
793
794 arrayOffsetVal = memInst->getOperand(memInst->getFirstOffsetIdx());
795 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000796 }
797
798 SetMemOperands_Internal(minstr, vmInstrNode, ptrVal, arrayOffsetVal,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000799 *idxVec, target);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000800
801 if (newIdxVec != NULL)
802 delete newIdxVec;
803}
804
805
806static void
807SetMemOperands_Internal(MachineInstr* minstr,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000808 const InstructionNode* vmInstrNode,
809 Value* ptrVal,
810 Value* arrayOffsetVal,
811 const vector<ConstPoolVal*>& idxVec,
812 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000813{
814 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
815
816 // Initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000817 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000818 Value* valueForRegOffset = NULL;
819 MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister;
820
821 // Check if there is an index vector and if so, if it translates to
822 // a small enough constant to fit in the immediate-offset field.
823 //
824 if (idxVec.size() > 0)
825 {
826 bool isConstantOffset = false;
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000827 unsigned offset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000828
829 const PointerType* ptrType = (PointerType*) ptrVal->getType();
830
831 if (ptrType->getValueType()->isStructType())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000832 {
833 // the offset is always constant for structs
834 isConstantOffset = true;
835
836 // Compute the offset value using the index vector
837 offset = target.DataLayout.getIndexedOffset(ptrType, idxVec);
838 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000839 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000840 {
841 // It must be an array ref. Check if the offset is a constant,
842 // and that the indexing has been lowered to a single offset.
843 //
844 assert(ptrType->getValueType()->isArrayType());
845 assert(arrayOffsetVal != NULL
846 && "Expect to be given Value* for array offsets");
847
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000848 if (ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(arrayOffsetVal))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000849 {
850 isConstantOffset = true; // always constant for structs
851 assert(arrayOffsetVal->getType()->isIntegral());
852 offset = (CPV->getType()->isSigned()
853 ? ((ConstPoolSInt*)CPV)->getValue()
854 : (int64_t) ((ConstPoolUInt*)CPV)->getValue());
855 }
856 else
857 {
858 valueForRegOffset = arrayOffsetVal;
859 }
860 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000861
862 if (isConstantOffset)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000863 {
864 // create a virtual register for the constant
865 valueForRegOffset = ConstPoolSInt::get(Type::IntTy, offset);
866 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000867 }
868 else
869 {
870 offsetOpType = MachineOperand::MO_SignExtendedImmed;
871 smallConstOffset = 0;
872 }
873
874 // Operand 0 is value for STORE, ptr for LOAD or GET_ELEMENT_PTR
875 // It is the left child in the instruction tree in all cases.
876 Value* leftVal = vmInstrNode->leftChild()->getValue();
877 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, leftVal);
878
879 // Operand 1 is ptr for STORE, offset for LOAD or GET_ELEMENT_PTR
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000880 // Operand 2 is offset for STORE, result reg for LOAD or GET_ELEMENT_PTR
Chris Lattner20b1ea02001-09-14 03:47:57 +0000881 //
882 unsigned offsetOpNum = (memInst->getOpcode() == Instruction::Store)? 2 : 1;
883 if (offsetOpType == MachineOperand::MO_VirtualRegister)
884 {
885 assert(valueForRegOffset != NULL);
886 minstr->SetMachineOperand(offsetOpNum, offsetOpType, valueForRegOffset);
887 }
888 else
889 minstr->SetMachineOperand(offsetOpNum, offsetOpType, smallConstOffset);
890
891 if (memInst->getOpcode() == Instruction::Store)
892 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, ptrVal);
893 else
894 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000895 vmInstrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000896}
897
898
Chris Lattner20b1ea02001-09-14 03:47:57 +0000899//
900// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +0000901// in place of the use(s) of that instruction in node `parent'.
902// Check both explicit and implicit operands!
Chris Lattner20b1ea02001-09-14 03:47:57 +0000903//
904static void
905ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000906 InstrTreeNode* parent,
907 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000908{
Vikram S. Adve243dd452001-09-18 13:03:13 +0000909 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
910
Chris Lattner20b1ea02001-09-14 03:47:57 +0000911 Instruction* unusedOp = treeNode->getInstruction();
912 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +0000913
914 // The parent itself may be a list node, so find the real parent instruction
915 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
916 {
917 parent = parent->parent();
918 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
919 }
920 InstructionNode* parentInstrNode = (InstructionNode*) parent;
921
922 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000923 MachineCodeForVMInstr& mvec = userInstr->getMachineInstrVec();
924 for (unsigned i=0, N=mvec.size(); i < N; i++)
925 {
926 MachineInstr* minstr = mvec[i];
Vikram S. Advec025fc12001-10-14 23:28:43 +0000927
928 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000929 {
930 const MachineOperand& mop = minstr->getOperand(i);
931 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
932 mop.getVRegValue() == unusedOp)
933 {
934 minstr->SetMachineOperand(i, MachineOperand::MO_VirtualRegister,
935 fwdOp);
936 }
937 }
Vikram S. Advec025fc12001-10-14 23:28:43 +0000938
939 for (unsigned i=0, numOps=minstr->getNumImplicitRefs(); i < numOps; ++i)
940 if (minstr->getImplicitRef(i) == unusedOp)
941 minstr->setImplicitRef(i, fwdOp, minstr->implicitRefIsDefined(i));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000942 }
943}
944
945
Ruchira Sasanka67a463a2001-11-12 14:45:33 +0000946
947void UltraSparcInstrInfo::
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000948CreateCopyInstructionsByType(const TargetMachine& target,
949 Value* src,
950 Instruction* dest,
Ruchira Sasanka67a463a2001-11-12 14:45:33 +0000951 vector<MachineInstr*>& minstrVec) const
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000952{
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000953 bool loadConstantToReg = false;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000954
955 const Type* resultType = dest->getType();
956
957 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
958 if (opCode == INVALID_OPCODE)
959 {
960 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000961 return;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000962 }
963
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000964 // if `src' is a constant that doesn't fit in the immed field or if it is
965 // a global variable (i.e., a constant address), generate a load
966 // instruction instead of an add
967 //
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000968 if (isa<ConstPoolVal>(src))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000969 {
970 unsigned int machineRegNum;
971 int64_t immedValue;
972 MachineOperand::MachineOperandType opType =
973 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
974 machineRegNum, immedValue);
975
976 if (opType == MachineOperand::MO_VirtualRegister)
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000977 loadConstantToReg = true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000978 }
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000979 else if (isa<GlobalValue>(src))
980 loadConstantToReg = true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000981
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000982 if (loadConstantToReg)
983 { // `src' is constant and cannot fit in immed field for the ADD
984 // Insert instructions to "load" the constant into a register
985 vector<TmpInstruction*> tempVec;
986 target.getInstrInfo().CreateCodeToLoadConst(src,dest,minstrVec,tempVec);
987 for (unsigned i=0; i < tempVec.size(); i++)
988 dest->getMachineInstrVec().addTempValue(tempVec[i]);
989 }
990 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000991 { // Create the appropriate add instruction.
992 // Make `src' the second operand, in case it is a constant
993 // Use (unsigned long) 0 for a NULL pointer value.
994 //
995 const Type* nullValueType =
996 (resultType->getPrimitiveID() == Type::PointerTyID)? Type::ULongTy
997 : resultType;
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000998 MachineInstr* minstr = new MachineInstr(opCode);
999 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1000 ConstPoolVal::getNullConstant(nullValueType));
1001 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, src);
1002 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, dest);
1003 minstrVec.push_back(minstr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001004 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001005}
1006
1007
Ruchira Sasanka67a463a2001-11-12 14:45:33 +00001008
Vikram S. Advefb361122001-10-22 13:36:31 +00001009//******************* Externally Visible Functions *************************/
1010
1011
1012//------------------------------------------------------------------------
1013// External Function: GetInstructionsForProlog
1014// External Function: GetInstructionsForEpilog
1015//
1016// Purpose:
1017// Create prolog and epilog code for procedure entry and exit
1018//------------------------------------------------------------------------
1019
1020extern unsigned
1021GetInstructionsForProlog(BasicBlock* entryBB,
1022 TargetMachine &target,
1023 MachineInstr** mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001024{
Vikram S. Advefb361122001-10-22 13:36:31 +00001025 int64_t s0=0; // used to avoid overloading ambiguity below
Chris Lattner20b1ea02001-09-14 03:47:57 +00001026
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001027 const MachineFrameInfo& frameInfo = target.getFrameInfo();
1028
Vikram S. Advefb361122001-10-22 13:36:31 +00001029 // The second operand is the stack size. If it does not fit in the
1030 // immediate field, we either have to find an unused register in the
1031 // caller's window or move some elements to the dynamically allocated
1032 // area of the stack frame (just above save area and method args).
1033 Method* method = entryBB->getParent();
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001034 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(method);
1035 unsigned int staticStackSize = mcInfo.getStaticStackSize();
1036
1037 if (staticStackSize < (unsigned) frameInfo.getMinStackFrameSize())
1038 staticStackSize = (unsigned) frameInfo.getMinStackFrameSize();
1039
1040 if (unsigned padsz = (staticStackSize %
1041 (unsigned) frameInfo.getStackFrameSizeAlignment()))
Vikram S. Advefd9b7dc2001-11-12 05:16:39 +00001042 staticStackSize += frameInfo.getStackFrameSizeAlignment() - padsz;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001043
Vikram S. Advefb361122001-10-22 13:36:31 +00001044 assert(target.getInstrInfo().constantFitsInImmedField(SAVE, staticStackSize)
1045 && "Stack size too large for immediate field of SAVE instruction. Need additional work as described in the comment above");
Chris Lattner20b1ea02001-09-14 03:47:57 +00001046
Vikram S. Advefb361122001-10-22 13:36:31 +00001047 mvec[0] = new MachineInstr(SAVE);
1048 mvec[0]->SetMachineOperand(0, target.getRegInfo().getStackPointer());
1049 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001050 - (int) staticStackSize);
Vikram S. Advefb361122001-10-22 13:36:31 +00001051 mvec[0]->SetMachineOperand(2, target.getRegInfo().getStackPointer());
1052
1053 return 1;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001054}
1055
1056
Vikram S. Advefb361122001-10-22 13:36:31 +00001057extern unsigned
1058GetInstructionsForEpilog(BasicBlock* anExitBB,
1059 TargetMachine &target,
1060 MachineInstr** mvec)
1061{
1062 int64_t s0=0; // used to avoid overloading ambiguity below
1063
1064 mvec[0] = new MachineInstr(RESTORE);
1065 mvec[0]->SetMachineOperand(0, target.getRegInfo().getZeroRegNum());
1066 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed, s0);
1067 mvec[0]->SetMachineOperand(2, target.getRegInfo().getZeroRegNum());
1068
1069 return 1;
1070}
1071
1072
1073//------------------------------------------------------------------------
1074// External Function: ThisIsAChainRule
1075//
1076// Purpose:
1077// Check if a given BURG rule is a chain rule.
1078//------------------------------------------------------------------------
1079
1080extern bool
1081ThisIsAChainRule(int eruleno)
1082{
1083 switch(eruleno)
1084 {
1085 case 111: // stmt: reg
1086 case 113: // stmt: bool
1087 case 123:
1088 case 124:
1089 case 125:
1090 case 126:
1091 case 127:
1092 case 128:
1093 case 129:
1094 case 130:
1095 case 131:
1096 case 132:
1097 case 133:
1098 case 155:
1099 case 221:
1100 case 222:
1101 case 241:
1102 case 242:
1103 case 243:
1104 case 244:
1105 return true; break;
1106
1107 default:
1108 return false; break;
1109 }
1110}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001111
1112
1113//------------------------------------------------------------------------
1114// External Function: GetInstructionsByRule
1115//
1116// Purpose:
1117// Choose machine instructions for the SPARC according to the
1118// patterns chosen by the BURG-generated parser.
1119//------------------------------------------------------------------------
1120
1121unsigned
1122GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001123 int ruleForNode,
1124 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001125 TargetMachine &target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001126 MachineInstr** mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001127{
1128 int numInstr = 1; // initialize for common case
1129 bool checkCast = false; // initialize here to use fall-through
Chris Lattner20b1ea02001-09-14 03:47:57 +00001130 int nextRule;
1131 int forwardOperandNum = -1;
Vikram S. Adve8557b222001-10-10 20:56:33 +00001132 int64_t s0=0, s8=8; // variables holding constants to avoid
1133 uint64_t u0=0; // overloading ambiguities below
Chris Lattner20b1ea02001-09-14 03:47:57 +00001134
Vikram S. Advefb361122001-10-22 13:36:31 +00001135 for (unsigned i=0; i < MAX_INSTR_PER_VMINSTR; i++)
1136 mvec[i] = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001137
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001138 //
1139 // Let's check for chain rules outside the switch so that we don't have
1140 // to duplicate the list of chain rule production numbers here again
1141 //
1142 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001143 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001144 // Chain rules have a single nonterminal on the RHS.
1145 // Get the rule that matches the RHS non-terminal and use that instead.
1146 //
1147 assert(nts[0] && ! nts[1]
1148 && "A chain rule should have only one RHS non-terminal!");
1149 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1150 nts = burm_nts[nextRule];
1151 numInstr = GetInstructionsByRule(subtreeRoot, nextRule, nts,target,mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001152 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001153 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001154 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001155 switch(ruleForNode) {
1156 case 1: // stmt: Ret
1157 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001158 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001159 // for moving return value to appropriate register.
1160 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001161 // Mark the return value register as an implicit ref of
1162 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001163 // Finally put a NOP in the delay slot.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001164 ReturnInst* returnInstr = (ReturnInst*) subtreeRoot->getInstruction();
1165 assert(returnInstr->getOpcode() == Instruction::Ret);
Vikram S. Advefb361122001-10-22 13:36:31 +00001166 Method* method = returnInstr->getParent()->getParent();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001167
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001168 Instruction* returnReg = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001169 returnInstr, NULL);
1170 returnInstr->getMachineInstrVec().addTempValue(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001171
1172 mvec[0] = new MachineInstr(JMPLRET);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001173 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1174 returnReg);
Vikram S. Adve8557b222001-10-10 20:56:33 +00001175 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,s8);
Vikram S. Advefb361122001-10-22 13:36:31 +00001176 mvec[0]->SetMachineOperand(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001177
Vikram S. Advea995e602001-10-11 04:23:19 +00001178 if (returnInstr->getReturnValue() != NULL)
1179 mvec[0]->addImplicitRef(returnInstr->getReturnValue());
1180
Vikram S. Advefb361122001-10-22 13:36:31 +00001181 unsigned n = numInstr++; // delay slot
1182 mvec[n] = new MachineInstr(NOP);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001183
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001184 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001185 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001186
1187 case 3: // stmt: Store(reg,reg)
1188 case 4: // stmt: Store(reg,ptrreg)
1189 mvec[0] = new MachineInstr(
1190 ChooseStoreInstruction(
1191 subtreeRoot->leftChild()->getValue()->getType()));
1192 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1193 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001194
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001195 case 5: // stmt: BrUncond
1196 mvec[0] = new MachineInstr(BA);
1197 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1198 (Value*)NULL);
1199 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1200 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1201
1202 // delay slot
1203 mvec[numInstr++] = new MachineInstr(NOP);
1204 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001205
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001206 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001207 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001208 // If the constant is ZERO, we can use the branch-on-integer-register
1209 // instructions and avoid the SUBcc instruction entirely.
1210 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001211 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001212 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1213 assert(constNode &&
1214 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
1215 ConstPoolVal* constVal = (ConstPoolVal*) constNode->getValue();
1216 bool isValidConst;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001217
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001218 if ((constVal->getType()->isIntegral()
1219 || constVal->getType()->isPointerType())
1220 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1221 && isValidConst)
1222 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001223 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1224
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001225 // That constant is a zero after all...
1226 // Use the left child of setCC as the first argument!
1227 mvec[0] = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1228 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1229 subtreeRoot->leftChild()->leftChild()->getValue());
1230 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001231 brInst->getSuccessor(0));
Chris Lattner20b1ea02001-09-14 03:47:57 +00001232
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001233 // delay slot
1234 mvec[numInstr++] = new MachineInstr(NOP);
1235
1236 // false branch
1237 int n = numInstr++;
1238 mvec[n] = new MachineInstr(BA);
1239 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1240 (Value*) NULL);
1241 mvec[n]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001242 brInst->getSuccessor(1));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001243
1244 // delay slot
1245 mvec[numInstr++] = new MachineInstr(NOP);
1246
1247 break;
1248 }
1249 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001250 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001251
1252 case 6: // stmt: BrCond(bool)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001253 { // bool => boolean was computed with some boolean operator
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001254 // (SetCC, Not, ...). We need to check whether the type was a FP,
1255 // signed int or unsigned int, and check the branching condition in
1256 // order to choose the branch to use.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001257 // If it is an integer CC, we also need to find the unique
1258 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001259 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001260 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001261 bool isFPBranch;
1262 mvec[0] = new MachineInstr(ChooseBccInstruction(subtreeRoot,
1263 isFPBranch));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001264
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001265 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1266 brInst->getParent()->getParent(),
1267 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001268
1269 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister, ccValue);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001270 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001271 brInst->getSuccessor(0));
1272
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001273 // delay slot
1274 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001275
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001276 // false branch
1277 int n = numInstr++;
1278 mvec[n] = new MachineInstr(BA);
1279 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1280 (Value*) NULL);
1281 mvec[n]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001282 brInst->getSuccessor(1));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001283
1284 // delay slot
1285 mvec[numInstr++] = new MachineInstr(NOP);
1286 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001287 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001288
1289 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001290 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001291 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnercfe26c92001-10-01 18:26:53 +00001292 ConstPoolVal* constVal =
1293 cast<ConstPoolVal>(subtreeRoot->leftChild()->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001294 unsigned dest = ((ConstPoolBool*) constVal)->getValue()? 0 : 1;
1295
1296 mvec[0] = new MachineInstr(BA);
1297 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1298 (Value*) NULL);
1299 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1300 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(dest));
1301
1302 // delay slot
1303 mvec[numInstr++] = new MachineInstr(NOP);
1304 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001305 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001306
1307 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001308 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001309 // Just use the branch-on-integer-register instruction!
1310 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001311 mvec[0] = new MachineInstr(BRNZ);
1312 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1313 subtreeRoot->leftChild()->getValue());
1314 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1315 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1316
1317 // delay slot
1318 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1319
1320 // false branch
1321 int n = numInstr++;
1322 mvec[n] = new MachineInstr(BA);
1323 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1324 (Value*) NULL);
1325 mvec[n]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1326 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
1327
1328 // delay slot
1329 mvec[numInstr++] = new MachineInstr(NOP);
1330 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001331 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001332
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001333 case 9: // stmt: Switch(reg)
1334 assert(0 && "*** SWITCH instruction is not implemented yet.");
1335 numInstr = 0;
1336 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001337
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001338 case 10: // reg: VRegList(reg, reg)
1339 assert(0 && "VRegList should never be the topmost non-chain rule");
1340 break;
1341
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001342 case 21: // bool: Not(bool): Both these are implemented as:
1343 case 321: // reg: BNot(reg) : reg = reg XOR-NOT 0
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001344 mvec[0] = new MachineInstr(XNOR);
1345 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1346 subtreeRoot->leftChild()->getValue());
1347 mvec[0]->SetMachineOperand(1, target.getRegInfo().getZeroRegNum());
1348 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1349 subtreeRoot->getValue());
1350 break;
1351
1352 case 322: // reg: ToBoolTy(bool):
1353 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001354 {
1355 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
1356 assert(opType->isIntegral() || opType->isPointerType()
1357 || opType == Type::BoolTy);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001358 numInstr = 0;
1359 forwardOperandNum = 0;
1360 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001361 }
1362
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001363 case 23: // reg: ToUByteTy(reg)
1364 case 25: // reg: ToUShortTy(reg)
1365 case 27: // reg: ToUIntTy(reg)
1366 case 29: // reg: ToULongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001367 {
1368 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001369 assert(opType->isIntegral() ||
1370 opType->isPointerType() ||
1371 opType == Type::BoolTy && "Cast is illegal for other types");
1372 numInstr = 0;
1373 forwardOperandNum = 0;
1374 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001375 }
1376
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001377 case 24: // reg: ToSByteTy(reg)
1378 case 26: // reg: ToShortTy(reg)
1379 case 28: // reg: ToIntTy(reg)
1380 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001381 {
1382 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
1383 if (opType->isIntegral()
1384 || opType->isPointerType()
1385 || opType == Type::BoolTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001386 {
1387 numInstr = 0;
1388 forwardOperandNum = 0;
1389 }
1390 else
1391 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001392 // If the source operand is an FP type, the int result must be
1393 // copied from float to int register via memory!
1394 Instruction *dest = subtreeRoot->getInstruction();
1395 Value* leftVal = subtreeRoot->leftChild()->getValue();
1396 Value* destForCast;
1397 vector<MachineInstr*> minstrVec;
1398
1399 if (opType == Type::FloatTy || opType == Type::DoubleTy)
1400 {
1401 // Create a temporary to represent the INT register
1402 // into which the FP value will be copied via memory.
1403 // The type of this temporary will determine the FP
1404 // register used: single-prec for a 32-bit int or smaller,
1405 // double-prec for a 64-bit int.
1406 //
1407 const Type* destTypeToUse =
1408 (dest->getType() == Type::LongTy)? Type::DoubleTy
1409 : Type::FloatTy;
1410 destForCast = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
1411 destTypeToUse, leftVal, NULL);
1412 dest->getMachineInstrVec().addTempValue(destForCast);
1413
1414 vector<TmpInstruction*> tempVec;
1415 target.getInstrInfo().CreateCodeToCopyFloatToInt(
1416 dest->getParent()->getParent(),
1417 (TmpInstruction*) destForCast, dest,
1418 minstrVec, tempVec, target);
1419
1420 for (unsigned i=0; i < tempVec.size(); ++i)
1421 dest->getMachineInstrVec().addTempValue(tempVec[i]);
1422 }
1423 else
1424 destForCast = leftVal;
1425
1426 MachineOpCode opCode=ChooseConvertToIntInstr(subtreeRoot, opType);
1427 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
1428
1429 mvec[0] = new MachineInstr(opCode);
1430 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1431 leftVal);
1432 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1433 destForCast);
1434
1435 assert(numInstr == 1 && "Should be initialized to 1 at the top");
1436 for (unsigned i=0; i < minstrVec.size(); ++i)
1437 mvec[numInstr++] = minstrVec[i];
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001438 }
1439 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001440 }
1441
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001442 case 31: // reg: ToFloatTy(reg):
1443 case 32: // reg: ToDoubleTy(reg):
1444 case 232: // reg: ToDoubleTy(Constant):
1445
1446 // If this instruction has a parent (a user) in the tree
1447 // and the user is translated as an FsMULd instruction,
1448 // then the cast is unnecessary. So check that first.
1449 // In the future, we'll want to do the same for the FdMULq instruction,
1450 // so do the check here instead of only for ToFloatTy(reg).
1451 //
1452 if (subtreeRoot->parent() != NULL &&
1453 ((InstructionNode*) subtreeRoot->parent())->getInstruction()->getMachineInstrVec()[0]->getOpCode() == FSMULD)
1454 {
1455 numInstr = 0;
1456 forwardOperandNum = 0;
1457 }
1458 else
1459 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001460 Value* leftVal = subtreeRoot->leftChild()->getValue();
1461 const Type* opType = leftVal->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001462 MachineOpCode opCode=ChooseConvertToFloatInstr(subtreeRoot,opType);
1463 if (opCode == INVALID_OPCODE) // no conversion needed
1464 {
1465 numInstr = 0;
1466 forwardOperandNum = 0;
1467 }
1468 else
1469 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001470 // If the source operand is a non-FP type it must be
1471 // first copied from int to float register via memory!
1472 Instruction *dest = subtreeRoot->getInstruction();
1473 Value* srcForCast;
1474 int n = 0;
1475 if (opType != Type::FloatTy && opType != Type::DoubleTy)
1476 {
1477 // Create a temporary to represent the FP register
1478 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001479 // The type of this temporary will determine the FP
1480 // register used: single-prec for a 32-bit int or smaller,
1481 // double-prec for a 64-bit int.
1482 //
1483 const Type* srcTypeToUse =
1484 (leftVal->getType() == Type::LongTy)? Type::DoubleTy
1485 : Type::FloatTy;
1486
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001487 srcForCast = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001488 srcTypeToUse, dest, NULL);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001489 dest->getMachineInstrVec().addTempValue(srcForCast);
1490
1491 vector<MachineInstr*> minstrVec;
1492 vector<TmpInstruction*> tempVec;
1493 target.getInstrInfo().CreateCodeToCopyIntToFloat(
1494 dest->getParent()->getParent(),
1495 leftVal, (TmpInstruction*) srcForCast,
1496 minstrVec, tempVec, target);
1497
1498 for (unsigned i=0; i < minstrVec.size(); ++i)
1499 mvec[n++] = minstrVec[i];
1500
1501 for (unsigned i=0; i < tempVec.size(); ++i)
1502 dest->getMachineInstrVec().addTempValue(tempVec[i]);
1503 }
1504 else
1505 srcForCast = leftVal;
1506
1507 MachineInstr* castI = new MachineInstr(opCode);
1508 castI->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1509 srcForCast);
1510 castI->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1511 dest);
1512 mvec[n++] = castI;
1513 numInstr = n;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001514 }
1515 }
1516 break;
1517
1518 case 19: // reg: ToArrayTy(reg):
1519 case 20: // reg: ToPointerTy(reg):
1520 numInstr = 0;
1521 forwardOperandNum = 0;
1522 break;
1523
1524 case 233: // reg: Add(reg, Constant)
1525 mvec[0] = CreateAddConstInstruction(subtreeRoot);
1526 if (mvec[0] != NULL)
1527 break;
1528 // ELSE FALL THROUGH
1529
1530 case 33: // reg: Add(reg, reg)
1531 mvec[0] = new MachineInstr(ChooseAddInstruction(subtreeRoot));
1532 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1533 break;
1534
1535 case 234: // reg: Sub(reg, Constant)
1536 mvec[0] = CreateSubConstInstruction(subtreeRoot);
1537 if (mvec[0] != NULL)
1538 break;
1539 // ELSE FALL THROUGH
1540
1541 case 34: // reg: Sub(reg, reg)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001542 mvec[0] = new MachineInstr(ChooseSubInstructionByType(
1543 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001544 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1545 break;
1546
1547 case 135: // reg: Mul(todouble, todouble)
1548 checkCast = true;
1549 // FALL THROUGH
1550
1551 case 35: // reg: Mul(reg, reg)
1552 mvec[0] =new MachineInstr(ChooseMulInstruction(subtreeRoot,checkCast));
1553 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1554 break;
1555
1556 case 335: // reg: Mul(todouble, todoubleConst)
1557 checkCast = true;
1558 // FALL THROUGH
1559
1560 case 235: // reg: Mul(reg, Constant)
1561 mvec[0] = CreateMulConstInstruction(target, subtreeRoot, mvec[1]);
1562 if (mvec[0] == NULL)
1563 {
1564 mvec[0] = new MachineInstr(ChooseMulInstruction(subtreeRoot,
1565 checkCast));
1566 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1567 }
1568 else
1569 if (mvec[1] != NULL)
1570 ++numInstr;
1571 break;
1572
1573 case 236: // reg: Div(reg, Constant)
1574 mvec[0] = CreateDivConstInstruction(target, subtreeRoot, mvec[1]);
1575 if (mvec[0] != NULL)
1576 {
1577 if (mvec[1] != NULL)
1578 ++numInstr;
1579 }
1580 else
1581 // ELSE FALL THROUGH
1582
1583 case 36: // reg: Div(reg, reg)
1584 mvec[0] = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1585 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1586 break;
1587
1588 case 37: // reg: Rem(reg, reg)
1589 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001590 {
1591 Instruction* remInstr = subtreeRoot->getInstruction();
1592
1593 TmpInstruction* quot = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
1594 subtreeRoot->leftChild()->getValue(),
1595 subtreeRoot->rightChild()->getValue());
1596 TmpInstruction* prod = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
1597 quot,
1598 subtreeRoot->rightChild()->getValue());
1599 remInstr->getMachineInstrVec().addTempValue(quot);
1600 remInstr->getMachineInstrVec().addTempValue(prod);
1601
1602 mvec[0] = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1603 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1604 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,quot);
1605
1606 int n = numInstr++;
1607 mvec[n] = new MachineInstr(ChooseMulInstructionByType(
1608 subtreeRoot->getInstruction()->getType()));
1609 mvec[n]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,quot);
1610 mvec[n]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1611 subtreeRoot->rightChild()->getValue());
1612 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,prod);
1613
1614 n = numInstr++;
1615 mvec[n] = new MachineInstr(ChooseSubInstructionByType(
1616 subtreeRoot->getInstruction()->getType()));
1617 Set3OperandsFromInstr(mvec[n], subtreeRoot, target);
1618 mvec[n]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,prod);
1619
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001620 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001621 }
1622
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001623 case 38: // bool: And(bool, bool)
1624 case 238: // bool: And(bool, boolconst)
1625 case 338: // reg : BAnd(reg, reg)
1626 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001627 mvec[0] = new MachineInstr(AND);
1628 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1629 break;
1630
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001631 case 138: // bool: And(bool, not)
1632 case 438: // bool: BAnd(bool, not)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001633 mvec[0] = new MachineInstr(ANDN);
1634 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1635 break;
1636
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001637 case 39: // bool: Or(bool, bool)
1638 case 239: // bool: Or(bool, boolconst)
1639 case 339: // reg : BOr(reg, reg)
1640 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001641 mvec[0] = new MachineInstr(ORN);
1642 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1643 break;
1644
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001645 case 139: // bool: Or(bool, not)
1646 case 439: // bool: BOr(bool, not)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001647 mvec[0] = new MachineInstr(ORN);
1648 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1649 break;
1650
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001651 case 40: // bool: Xor(bool, bool)
1652 case 240: // bool: Xor(bool, boolconst)
1653 case 340: // reg : BXor(reg, reg)
1654 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001655 mvec[0] = new MachineInstr(XOR);
1656 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1657 break;
1658
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001659 case 140: // bool: Xor(bool, not)
1660 case 440: // bool: BXor(bool, not)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001661 mvec[0] = new MachineInstr(XNOR);
1662 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1663 break;
1664
1665 case 41: // boolconst: SetCC(reg, Constant)
1666 // Check if this is an integer comparison, and
1667 // there is a parent, and the parent decided to use
1668 // a branch-on-integer-register instead of branch-on-condition-code.
1669 // If so, the SUBcc instruction is not required.
1670 // (However, we must still check for constants to be loaded from
1671 // the constant pool so that such a load can be associated with
1672 // this instruction.)
1673 //
1674 // Otherwise this is just the same as case 42, so just fall through.
1675 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001676 if ((subtreeRoot->leftChild()->getValue()->getType()->isIntegral() ||
1677 subtreeRoot->leftChild()->getValue()->getType()->isPointerType())
1678 && subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001679 {
1680 InstructionNode* parent = (InstructionNode*) subtreeRoot->parent();
1681 assert(parent->getNodeType() == InstrTreeNode::NTInstructionNode);
1682 const vector<MachineInstr*>&
1683 minstrVec = parent->getInstruction()->getMachineInstrVec();
1684 MachineOpCode parentOpCode;
1685 if (parent->getInstruction()->getOpcode() == Instruction::Br &&
1686 (parentOpCode = minstrVec[0]->getOpCode()) >= BRZ &&
1687 parentOpCode <= BRGEZ)
1688 {
1689 numInstr = 0; // don't forward the operand!
1690 break;
1691 }
1692 }
1693 // ELSE FALL THROUGH
1694
1695 case 42: // bool: SetCC(reg, reg):
1696 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001697 // This generates a SUBCC instruction, putting the difference in
1698 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001699 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001700 // If the boolean result of the SetCC is used by anything other
1701 // than a single branch instruction, the boolean must be
1702 // computed and stored in the result register. Otherwise, discard
1703 // the difference (by using %g0) and keep only the condition code.
1704 //
1705 // To compute the boolean result in a register we use a conditional
1706 // move, unless the result of the SUBCC instruction can be used as
1707 // the bool! This assumes that zero is FALSE and any non-zero
1708 // integer is TRUE.
1709 //
1710 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1711 Instruction* setCCInstr = subtreeRoot->getInstruction();
1712 bool keepBoolVal = (parentNode == NULL ||
1713 parentNode->getInstruction()->getOpcode()
1714 != Instruction::Br);
1715 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001716 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1717 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1718
1719 bool mustClearReg;
1720 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001721 MachineOpCode movOpCode = 0;
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001722
1723 // Mark the 4th operand as being a CC register, and as a def
1724 // A TmpInstruction is created to represent the CC "result".
1725 // Unlike other instances of TmpInstruction, this one is used
1726 // by machine code of multiple LLVM instructions, viz.,
1727 // the SetCC and the branch. Make sure to get the same one!
1728 // Note that we do this even for FP CC registers even though they
1729 // are explicit operands, because the type of the operand
1730 // needs to be a floating point condition code, not an integer
1731 // condition code. Think of this as casting the bool result to
1732 // a FP condition code register.
1733 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001734 Value* leftVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001735 bool isFPCompare = (leftVal->getType() == Type::FloatTy ||
1736 leftVal->getType() == Type::DoubleTy);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001737
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001738 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1739 setCCInstr->getParent()->getParent(),
1740 isFPCompare? Type::FloatTy : Type::IntTy);
1741 setCCInstr->getMachineInstrVec().addTempValue(tmpForCC);
1742
1743 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001744 {
1745 // Integer condition: dest. should be %g0 or an integer register.
1746 // If result must be saved but condition is not SetEQ then we need
1747 // a separate instruction to compute the bool result, so discard
1748 // result of SUBcc instruction anyway.
1749 //
1750 mvec[0] = new MachineInstr(SUBcc);
1751 Set3OperandsFromInstr(mvec[0], subtreeRoot, target, ! keepSubVal);
1752
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001753 mvec[0]->SetMachineOperand(3, MachineOperand::MO_CCRegister,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001754 tmpForCC, /*def*/true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001755
1756 if (computeBoolVal)
1757 { // recompute bool using the integer condition codes
1758 movOpCode =
1759 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1760 }
1761 }
1762 else
1763 {
1764 // FP condition: dest of FCMP should be some FCCn register
1765 mvec[0] = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001766 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001767 tmpForCC);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001768 mvec[0]->SetMachineOperand(1,MachineOperand::MO_VirtualRegister,
1769 subtreeRoot->leftChild()->getValue());
1770 mvec[0]->SetMachineOperand(2,MachineOperand::MO_VirtualRegister,
1771 subtreeRoot->rightChild()->getValue());
1772
1773 if (computeBoolVal)
1774 {// recompute bool using the FP condition codes
1775 mustClearReg = true;
1776 valueToMove = 1;
1777 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1778 }
1779 }
1780
1781 if (computeBoolVal)
1782 {
1783 if (mustClearReg)
1784 {// Unconditionally set register to 0
1785 int n = numInstr++;
1786 mvec[n] = new MachineInstr(SETHI);
1787 mvec[n]->SetMachineOperand(0,MachineOperand::MO_UnextendedImmed,
1788 s0);
1789 mvec[n]->SetMachineOperand(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001790 setCCInstr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001791 }
1792
1793 // Now conditionally move `valueToMove' (0 or 1) into the register
1794 int n = numInstr++;
1795 mvec[n] = new MachineInstr(movOpCode);
1796 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001797 tmpForCC);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001798 mvec[n]->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
1799 valueToMove);
1800 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001801 setCCInstr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001802 }
1803 break;
1804 }
1805
1806 case 43: // boolreg: VReg
1807 case 44: // boolreg: Constant
1808 numInstr = 0;
1809 break;
1810
1811 case 51: // reg: Load(reg)
1812 case 52: // reg: Load(ptrreg)
1813 case 53: // reg: LoadIdx(reg,reg)
1814 case 54: // reg: LoadIdx(ptrreg,reg)
1815 mvec[0] = new MachineInstr(ChooseLoadInstruction(
1816 subtreeRoot->getValue()->getType()));
1817 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1818 break;
1819
1820 case 55: // reg: GetElemPtr(reg)
1821 case 56: // reg: GetElemPtrIdx(reg,reg)
1822 if (subtreeRoot->parent() != NULL)
1823 {
Vikram S. Adve671b16d2001-11-10 01:05:26 +00001824 // If the parent was a memory operation and not an array access,
1825 // the parent will fold this instruction in so generate nothing.
1826 //
1827 Instruction* parent =
1828 cast<Instruction>(subtreeRoot->parent()->getValue());
1829 if (parent->getOpcode() == Instruction::Load ||
1830 parent->getOpcode() == Instruction::Store ||
1831 parent->getOpcode() == Instruction::GetElementPtr)
1832 {
1833 // Check if the parent is an array access,
1834 // If so, we still need to generate this instruction.
1835 GetElementPtrInst* getElemInst =
1836 cast<GetElementPtrInst>(subtreeRoot->getInstruction());
1837 const PointerType* ptrType =
1838 (const PointerType*) getElemInst->getPtrOperand()->getType();
1839 if (! ptrType->getValueType()->isArrayType())
1840 {// we don't need a separate instr
1841 numInstr = 0; // don't forward operand!
1842 break;
1843 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001844 }
1845 }
1846 // else in all other cases we need to a separate ADD instruction
1847 mvec[0] = new MachineInstr(ADD);
1848 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1849 break;
1850
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001851 case 57: // reg: Alloca: Implement as 1 instruction:
1852 { // add %fp, offsetFromFP -> result
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001853 Instruction* instr = subtreeRoot->getInstruction();
1854 const PointerType* instrType = (const PointerType*) instr->getType();
1855 assert(instrType->isPointerType());
1856 int tsize = (int)
1857 target.findOptimalStorageSize(instrType->getValueType());
1858 assert(tsize != 0 && "Just to check when this can happen");
1859
Vikram S. Advefb361122001-10-22 13:36:31 +00001860 Method* method = instr->getParent()->getParent();
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001861 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(method);
1862 int offsetFromFP = mcInfo.allocateLocalVar(target, instr);
Vikram S. Advefb361122001-10-22 13:36:31 +00001863
1864 // Create a temporary Value to hold the constant offset.
1865 // This is needed because it may not fit in the immediate field.
1866 ConstPoolSInt* offsetVal=ConstPoolSInt::get(Type::IntTy, offsetFromFP);
1867
1868 // Instruction 1: add %fp, offsetFromFP -> result
1869 mvec[0] = new MachineInstr(ADD);
1870 mvec[0]->SetMachineOperand(0, target.getRegInfo().getFramePointer());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001871 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
Vikram S. Advefb361122001-10-22 13:36:31 +00001872 offsetVal);
1873 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001874 instr);
1875 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001876 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001877
1878 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1879 // mul num, typeSz -> tmp
1880 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001881 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001882 Instruction* instr = subtreeRoot->getInstruction();
1883 const PointerType* instrType = (const PointerType*) instr->getType();
1884 assert(instrType->isPointerType() &&
1885 instrType->getValueType()->isArrayType());
1886 const Type* eltType =
1887 ((ArrayType*) instrType->getValueType())->getElementType();
1888 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advefb361122001-10-22 13:36:31 +00001889
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001890 assert(tsize != 0 && "Just to check when this can happen");
Vikram S. Advefb361122001-10-22 13:36:31 +00001891
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001892 // Create a temporary Value to hold the constant type-size
Vikram S. Advefb361122001-10-22 13:36:31 +00001893 ConstPoolSInt* tsizeVal = ConstPoolSInt::get(Type::IntTy, tsize);
1894
1895 // Create a temporary Value to hold the constant offset from SP
1896 Method* method = instr->getParent()->getParent();
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001897 bool ignore; // we don't need this
1898 ConstPoolSInt* dynamicAreaOffset = ConstPoolSInt::get(Type::IntTy,
1899 target.getFrameInfo().getDynamicAreaOffset(MachineCodeForMethod::get(method),
1900 ignore));
Vikram S. Advefb361122001-10-22 13:36:31 +00001901
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001902 // Create a temporary value to hold `tmp'
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001903 Instruction* tmpInstr = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001904 subtreeRoot->leftChild()->getValue(),
1905 NULL /*could insert tsize here*/);
1906 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(tmpInstr);
1907
1908 // Instruction 1: mul numElements, typeSize -> tmp
1909 mvec[0] = new MachineInstr(MULX);
1910 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefb361122001-10-22 13:36:31 +00001911 subtreeRoot->leftChild()->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001912 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
Vikram S. Advefb361122001-10-22 13:36:31 +00001913 tsizeVal);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001914 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1915 tmpInstr);
Vikram S. Advefb361122001-10-22 13:36:31 +00001916
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001917 // Instruction 2: sub %sp, tmp -> %sp
1918 numInstr++;
1919 mvec[1] = new MachineInstr(SUB);
Vikram S. Advefb361122001-10-22 13:36:31 +00001920 mvec[1]->SetMachineOperand(0, target.getRegInfo().getStackPointer());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001921 mvec[1]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1922 tmpInstr);
Vikram S. Advefb361122001-10-22 13:36:31 +00001923 mvec[1]->SetMachineOperand(2, target.getRegInfo().getStackPointer());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001924
Vikram S. Advefb361122001-10-22 13:36:31 +00001925 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001926 numInstr++;
1927 mvec[2] = new MachineInstr(ADD);
Vikram S. Advefb361122001-10-22 13:36:31 +00001928 mvec[2]->SetMachineOperand(0, target.getRegInfo().getStackPointer());
1929 mvec[2]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001930 dynamicAreaOffset);
Vikram S. Advefb361122001-10-22 13:36:31 +00001931 mvec[2]->SetMachineOperand(2,MachineOperand::MO_VirtualRegister,instr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001932 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001933 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001934
1935 case 61: // reg: Call
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001936 { // Generate a call-indirect (i.e., jmpl) for now to expose
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001937 // the potential need for registers. If an absolute address
1938 // is available, replace this with a CALL instruction.
1939 // Mark both the indirection register and the return-address
1940 // register as hidden virtual registers.
Vikram S. Advea995e602001-10-11 04:23:19 +00001941 // Also, mark the operands of the Call and return value (if
1942 // any) as implicit operands of the CALL machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001943 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00001944 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00001945 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001946
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001947 Instruction* retAddrReg = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001948 callInstr, NULL);
Vikram S. Adve8557b222001-10-10 20:56:33 +00001949
Vikram S. Advea995e602001-10-11 04:23:19 +00001950 // Note temporary values in the machineInstrVec for the VM instr.
Vikram S. Adve8557b222001-10-10 20:56:33 +00001951 //
1952 // WARNING: Operands 0..N-1 must go in slots 0..N-1 of implicitUses.
1953 // The result value must go in slot N. This is assumed
1954 // in register allocation.
1955 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001956 callInstr->getMachineInstrVec().addTempValue(retAddrReg);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001957
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001958
1959 // Generate the machine instruction and its operands.
1960 // Use CALL for direct function calls; this optimistically assumes
1961 // the PC-relative address fits in the CALL address field (22 bits).
1962 // Use JMPL for indirect calls.
1963 //
1964 if (callee->getValueType() == Value::MethodVal)
1965 { // direct function call
1966 mvec[0] = new MachineInstr(CALL);
1967 mvec[0]->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp,
1968 callee);
1969 }
1970 else
1971 { // indirect function call
Vikram S. Advefb361122001-10-22 13:36:31 +00001972 mvec[0] = new MachineInstr(JMPLCALL);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001973 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1974 callee);
1975 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1976 (int64_t) 0);
1977 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1978 retAddrReg);
1979 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001980
Vikram S. Advea995e602001-10-11 04:23:19 +00001981 // Add the call operands and return value as implicit refs
1982 for (unsigned i=0, N=callInstr->getNumOperands(); i < N; ++i)
1983 if (callInstr->getOperand(i) != callee)
1984 mvec[0]->addImplicitRef(callInstr->getOperand(i));
1985
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001986 if (callInstr->getType() != Type::VoidTy)
Vikram S. Advea995e602001-10-11 04:23:19 +00001987 mvec[0]->addImplicitRef(callInstr, /*isDef*/ true);
1988
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001989 // For the CALL instruction, the ret. addr. reg. is also implicit
1990 if (callee->getValueType() == Value::MethodVal)
1991 mvec[0]->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001992
1993 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1994 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001995 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001996
1997 case 62: // reg: Shl(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001998 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001999 assert(opType->isIntegral()
2000 || opType == Type::BoolTy
2001 || opType->isPointerType()&& "Shl unsupported for other types");
2002 mvec[0] = new MachineInstr((opType == Type::LongTy)? SLLX : SLL);
2003 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
2004 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002005 }
2006
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002007 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002008 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002009 assert(opType->isIntegral()
2010 || opType == Type::BoolTy
2011 || opType->isPointerType() &&"Shr unsupported for other types");
2012 mvec[0] = new MachineInstr((opType->isSigned()
2013 ? ((opType == Type::LongTy)? SRAX : SRA)
2014 : ((opType == Type::LongTy)? SRLX : SRL)));
2015 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
2016 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002017 }
2018
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002019 case 64: // reg: Phi(reg,reg)
2020 { // This instruction has variable #operands, so resultPos is 0.
2021 Instruction* phi = subtreeRoot->getInstruction();
2022 mvec[0] = new MachineInstr(PHI, 1 + phi->getNumOperands());
2023 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
2024 subtreeRoot->getValue());
2025 for (unsigned i=0, N=phi->getNumOperands(); i < N; i++)
2026 mvec[0]->SetMachineOperand(i+1, MachineOperand::MO_VirtualRegister,
2027 phi->getOperand(i));
2028 break;
2029 }
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002030
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002031 case 71: // reg: VReg
2032 case 72: // reg: Constant
2033 numInstr = 0; // don't forward the value
2034 break;
2035
2036 default:
2037 assert(0 && "Unrecognized BURG rule");
2038 numInstr = 0;
2039 break;
2040 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002041 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002042
2043 if (forwardOperandNum >= 0)
2044 { // We did not generate a machine instruction but need to use operand.
2045 // If user is in the same tree, replace Value in its machine operand.
2046 // If not, insert a copy instruction which should get coalesced away
2047 // by register allocation.
2048 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002049 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002050 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002051 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002052 vector<MachineInstr*> minstrVec;
Ruchira Sasanka67a463a2001-11-12 14:45:33 +00002053 target.getInstrInfo().CreateCopyInstructionsByType(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002054 subtreeRoot->getInstruction()->getOperand(forwardOperandNum),
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002055 subtreeRoot->getInstruction(), minstrVec);
2056 assert(minstrVec.size() > 0);
2057 for (unsigned i=0; i < minstrVec.size(); ++i)
2058 mvec[numInstr++] = minstrVec[i];
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002059 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002060 }
2061
Chris Lattner20b1ea02001-09-14 03:47:57 +00002062 return numInstr;
2063}
2064
2065