blob: bf27a2abd5f064228edc7967eaaea35765a5a82e [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.
142// The static cache is not too bad because that memory for these
143// TmpInstructions will be freed elsewhere in any case.
144//
145static TmpInstruction*
146GetTmpForCC(Value* boolVal, const Method* method)
147{
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
160 // Look for tmpI and create a new one otherswise.
161 // new value is directly written to map using
162 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
163 if (tmpI == NULL)
164 tmpI = new TmpInstruction(TMP_INSTRUCTION_OPCODE, boolVal, NULL);
165
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:
262 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000263 opCode = FITOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000264 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000265 opCode = FXTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000266 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000267 opCode = FSTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000268 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000269 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000270 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000271 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000272 break;
273
274 default:
275 break;
276 }
277
278 return opCode;
279}
280
281static inline MachineOpCode
282ChooseConvertToIntInstr(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000283 const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000284{
285 MachineOpCode opCode = INVALID_OPCODE;;
286
287 int instrType = (int) instrNode->getOpLabel();
288
289 if (instrType == ToSByteTy || instrType == ToShortTy || instrType == ToIntTy)
290 {
291 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000292 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000293 case Type::FloatTyID: opCode = FSTOI; break;
294 case Type::DoubleTyID: opCode = FDTOI; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000295 default:
296 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
297 break;
298 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000299 }
300 else if (instrType == ToLongTy)
301 {
302 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000303 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000304 case Type::FloatTyID: opCode = FSTOX; break;
305 case Type::DoubleTyID: opCode = FDTOX; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000306 default:
307 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
308 break;
309 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000310 }
311 else
312 assert(0 && "Should not get here, Mo!");
313
314 return opCode;
315}
316
317
318static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000319ChooseAddInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000320{
321 MachineOpCode opCode = INVALID_OPCODE;
322
Chris Lattner20b1ea02001-09-14 03:47:57 +0000323 if (resultType->isIntegral() ||
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000324 isa<PointerType>(resultType) ||
325 isa<MethodType>(resultType) ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000326 resultType->isLabelType() ||
327 resultType == Type::BoolTy)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000328 {
329 opCode = ADD;
330 }
331 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000332 switch(resultType->getPrimitiveID())
333 {
334 case Type::FloatTyID: opCode = FADDS; break;
335 case Type::DoubleTyID: opCode = FADDD; break;
336 default: assert(0 && "Invalid type for ADD instruction"); break;
337 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000338
339 return opCode;
340}
341
342
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000343static inline MachineOpCode
344ChooseAddInstruction(const InstructionNode* instrNode)
345{
346 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
347}
348
349
Chris Lattner20b1ea02001-09-14 03:47:57 +0000350static inline MachineInstr*
351CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000352 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000353{
354 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000355 ? FMOVS : FMOVD);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000356 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000357 instrNode->leftChild()->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000358 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000359 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000360 return minstr;
361}
362
363static inline MachineInstr*
364CreateAddConstInstruction(const InstructionNode* instrNode)
365{
366 MachineInstr* minstr = NULL;
367
368 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000369 assert(isa<ConstPoolVal>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000370
371 // Cases worth optimizing are:
372 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
373 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
374 //
375 const Type* resultType = instrNode->getInstruction()->getType();
376
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000377 if (resultType == Type::FloatTy ||
378 resultType == Type::DoubleTy)
379 {
380 double dval = ((ConstPoolFP*) constOp)->getValue();
381 if (dval == 0.0)
382 minstr = CreateMovFloatInstruction(instrNode, resultType);
383 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000384
385 return minstr;
386}
387
388
389static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000390ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000391{
392 MachineOpCode opCode = INVALID_OPCODE;
393
Chris Lattner20b1ea02001-09-14 03:47:57 +0000394 if (resultType->isIntegral() ||
395 resultType->isPointerType())
396 {
397 opCode = SUB;
398 }
399 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000400 switch(resultType->getPrimitiveID())
401 {
402 case Type::FloatTyID: opCode = FSUBS; break;
403 case Type::DoubleTyID: opCode = FSUBD; break;
404 default: assert(0 && "Invalid type for SUB instruction"); break;
405 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000406
407 return opCode;
408}
409
410
411static inline MachineInstr*
412CreateSubConstInstruction(const InstructionNode* instrNode)
413{
414 MachineInstr* minstr = NULL;
415
416 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000417 assert(isa<ConstPoolVal>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000418
419 // Cases worth optimizing are:
420 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
421 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
422 //
423 const Type* resultType = instrNode->getInstruction()->getType();
424
425 if (resultType == Type::FloatTy ||
426 resultType == Type::DoubleTy)
427 {
428 double dval = ((ConstPoolFP*) constOp)->getValue();
429 if (dval == 0.0)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000430 minstr = CreateMovFloatInstruction(instrNode, resultType);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000431 }
432
433 return minstr;
434}
435
436
437static inline MachineOpCode
438ChooseFcmpInstruction(const InstructionNode* instrNode)
439{
440 MachineOpCode opCode = INVALID_OPCODE;
441
442 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
443 switch(operand->getType()->getPrimitiveID()) {
444 case Type::FloatTyID: opCode = FCMPS; break;
445 case Type::DoubleTyID: opCode = FCMPD; break;
446 default: assert(0 && "Invalid type for FCMP instruction"); break;
447 }
448
449 return opCode;
450}
451
452
453// Assumes that leftArg and rightArg are both cast instructions.
454//
455static inline bool
456BothFloatToDouble(const InstructionNode* instrNode)
457{
458 InstrTreeNode* leftArg = instrNode->leftChild();
459 InstrTreeNode* rightArg = instrNode->rightChild();
460 InstrTreeNode* leftArgArg = leftArg->leftChild();
461 InstrTreeNode* rightArgArg = rightArg->leftChild();
462 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
463
464 // Check if both arguments are floats cast to double
465 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000466 leftArgArg->getValue()->getType() == Type::FloatTy &&
467 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000468}
469
470
471static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000472ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000473{
474 MachineOpCode opCode = INVALID_OPCODE;
475
Chris Lattner20b1ea02001-09-14 03:47:57 +0000476 if (resultType->isIntegral())
Vikram S. Adve510eec72001-11-04 21:59:14 +0000477 opCode = MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000478 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000479 switch(resultType->getPrimitiveID())
480 {
481 case Type::FloatTyID: opCode = FMULS; break;
482 case Type::DoubleTyID: opCode = FMULD; break;
483 default: assert(0 && "Invalid type for MUL instruction"); break;
484 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000485
486 return opCode;
487}
488
489
Vikram S. Adve510eec72001-11-04 21:59:14 +0000490static inline MachineOpCode
491ChooseMulInstruction(const InstructionNode* instrNode,
492 bool checkCasts)
493{
494 if (checkCasts && BothFloatToDouble(instrNode))
495 return FSMULD;
496
497 // else use the regular multiply instructions
498 return ChooseMulInstructionByType(instrNode->getInstruction()->getType());
499}
500
501
Chris Lattner20b1ea02001-09-14 03:47:57 +0000502static inline MachineInstr*
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000503CreateIntNegInstruction(TargetMachine& target,
504 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000505{
506 MachineInstr* minstr = new MachineInstr(SUB);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000507 minstr->SetMachineOperand(0, target.getRegInfo().getZeroRegNum());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000508 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, vreg);
509 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, vreg);
510 return minstr;
511}
512
513
514static inline MachineInstr*
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000515CreateMulConstInstruction(TargetMachine &target,
516 const InstructionNode* instrNode,
517 MachineInstr*& getMinstr2)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000518{
519 MachineInstr* minstr = NULL;
520 getMinstr2 = NULL;
521 bool needNeg = false;
522
523 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000524 assert(isa<ConstPoolVal>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000525
526 // Cases worth optimizing are:
527 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
528 // (2) Multiply by 2^x for integer types: replace with Shift
529 //
530 const Type* resultType = instrNode->getInstruction()->getType();
531
Vikram S. Adve243dd452001-09-18 13:03:13 +0000532 if (resultType->isIntegral() || resultType->isPointerType())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000533 {
534 unsigned pow;
535 bool isValidConst;
536 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
537 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000538 {
539 bool needNeg = false;
540 if (C < 0)
541 {
542 needNeg = true;
543 C = -C;
544 }
545
546 if (C == 0 || C == 1)
547 {
548 minstr = new MachineInstr(ADD);
549
550 if (C == 0)
551 minstr->SetMachineOperand(0,
552 target.getRegInfo().getZeroRegNum());
553 else
554 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
555 instrNode->leftChild()->getValue());
556 minstr->SetMachineOperand(1,target.getRegInfo().getZeroRegNum());
557 }
558 else if (IsPowerOf2(C, pow))
559 {
560 minstr = new MachineInstr((resultType == Type::LongTy)
561 ? SLLX : SLL);
562 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
563 instrNode->leftChild()->getValue());
564 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
565 pow);
566 }
567
568 if (minstr && needNeg)
569 { // insert <reg = SUB 0, reg> after the instr to flip the sign
570 getMinstr2 = CreateIntNegInstruction(target,
571 instrNode->getValue());
572 }
573 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000574 }
575 else
576 {
577 if (resultType == Type::FloatTy ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000578 resultType == Type::DoubleTy)
579 {
580 bool isValidConst;
581 double dval = ((ConstPoolFP*) constOp)->getValue();
582
583 if (isValidConst)
584 {
585 if (dval == 0)
586 {
587 minstr = new MachineInstr((resultType == Type::FloatTy)
588 ? FITOS : FITOD);
589 minstr->SetMachineOperand(0,
590 target.getRegInfo().getZeroRegNum());
591 }
592 else if (fabs(dval) == 1)
593 {
594 bool needNeg = (dval < 0);
595
596 MachineOpCode opCode = needNeg
597 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
598 : (resultType == Type::FloatTy? FMOVS : FMOVD);
599
600 minstr = new MachineInstr(opCode);
601 minstr->SetMachineOperand(0,
602 MachineOperand::MO_VirtualRegister,
603 instrNode->leftChild()->getValue());
604 }
605 }
606 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000607 }
608
609 if (minstr != NULL)
610 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000611 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000612
613 return minstr;
614}
615
616
Vikram S. Adve510eec72001-11-04 21:59:14 +0000617// Generate a divide instruction for Div or Rem.
618// For Rem, this assumes that the operand type will be signed if the result
619// type is signed. This is correct because they must have the same sign.
620//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000621static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000622ChooseDivInstruction(TargetMachine &target,
623 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000624{
625 MachineOpCode opCode = INVALID_OPCODE;
626
627 const Type* resultType = instrNode->getInstruction()->getType();
628
629 if (resultType->isIntegral())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000630 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000631 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000632 switch(resultType->getPrimitiveID())
633 {
634 case Type::FloatTyID: opCode = FDIVS; break;
635 case Type::DoubleTyID: opCode = FDIVD; break;
636 default: assert(0 && "Invalid type for DIV instruction"); break;
637 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000638
639 return opCode;
640}
641
642
643static inline MachineInstr*
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000644CreateDivConstInstruction(TargetMachine &target,
645 const InstructionNode* instrNode,
646 MachineInstr*& getMinstr2)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000647{
648 MachineInstr* minstr = NULL;
649 getMinstr2 = NULL;
650
651 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000652 assert(isa<ConstPoolVal>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000653
654 // Cases worth optimizing are:
655 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
656 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
657 //
658 const Type* resultType = instrNode->getInstruction()->getType();
659
660 if (resultType->isIntegral())
661 {
662 unsigned pow;
663 bool isValidConst;
664 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
665 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000666 {
667 bool needNeg = false;
668 if (C < 0)
669 {
670 needNeg = true;
671 C = -C;
672 }
673
674 if (C == 1)
675 {
676 minstr = new MachineInstr(ADD);
677 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
678 instrNode->leftChild()->getValue());
679 minstr->SetMachineOperand(1,target.getRegInfo().getZeroRegNum());
680 }
681 else if (IsPowerOf2(C, pow))
682 {
683 MachineOpCode opCode= ((resultType->isSigned())
684 ? (resultType==Type::LongTy)? SRAX : SRA
685 : (resultType==Type::LongTy)? SRLX : SRL);
686 minstr = new MachineInstr(opCode);
687 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
688 instrNode->leftChild()->getValue());
689 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
690 pow);
691 }
692
693 if (minstr && needNeg)
694 { // insert <reg = SUB 0, reg> after the instr to flip the sign
695 getMinstr2 = CreateIntNegInstruction(target,
696 instrNode->getValue());
697 }
698 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000699 }
700 else
701 {
702 if (resultType == Type::FloatTy ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000703 resultType == Type::DoubleTy)
704 {
705 bool isValidConst;
706 double dval = ((ConstPoolFP*) constOp)->getValue();
707
708 if (isValidConst && fabs(dval) == 1)
709 {
710 bool needNeg = (dval < 0);
711
712 MachineOpCode opCode = needNeg
713 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
714 : (resultType == Type::FloatTy? FMOVS : FMOVD);
715
716 minstr = new MachineInstr(opCode);
717 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
718 instrNode->leftChild()->getValue());
719 }
720 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000721 }
722
723 if (minstr != NULL)
724 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000725 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000726
727 return minstr;
728}
729
730
Chris Lattner20b1ea02001-09-14 03:47:57 +0000731//------------------------------------------------------------------------
732// Function SetOperandsForMemInstr
733//
734// Choose addressing mode for the given load or store instruction.
735// Use [reg+reg] if it is an indexed reference, and the index offset is
736// not a constant or if it cannot fit in the offset field.
737// Use [reg+offset] in all other cases.
738//
739// This assumes that all array refs are "lowered" to one of these forms:
740// %x = load (subarray*) ptr, constant ; single constant offset
741// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
742// Generally, this should happen via strength reduction + LICM.
743// Also, strength reduction should take care of using the same register for
744// the loop index variable and an array index, when that is profitable.
745//------------------------------------------------------------------------
746
747static void
748SetOperandsForMemInstr(MachineInstr* minstr,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000749 const InstructionNode* vmInstrNode,
750 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000751{
752 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
753
754 // Variables to hold the index vector, ptr value, and offset value.
755 // The major work here is to extract these for all 3 instruction types
756 // and then call the common function SetMemOperands_Internal().
757 //
Chris Lattner8e7f4092001-11-04 08:08:34 +0000758 const vector<ConstPoolVal*>* idxVec = &memInst->getIndices();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000759 vector<ConstPoolVal*>* newIdxVec = NULL;
760 Value* ptrVal;
761 Value* arrayOffsetVal = NULL;
762
763 // Test if a GetElemPtr instruction is being folded into this mem instrn.
764 // If so, it will be in the left child for Load and GetElemPtr,
765 // and in the right child for Store instructions.
766 //
767 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000768 ? vmInstrNode->rightChild()
769 : vmInstrNode->leftChild());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000770
771 if (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
772 ptrChild->getOpLabel() == GetElemPtrIdx)
773 {
774 // There is a GetElemPtr instruction and there may be a chain of
775 // more than one. Use the pointer value of the last one in the chain.
776 // Fold the index vectors from the entire chain and from the mem
777 // instruction into one single index vector.
778 // Finally, we never fold for an array instruction so make that NULL.
779
780 newIdxVec = new vector<ConstPoolVal*>;
781 ptrVal = FoldGetElemChain((InstructionNode*) ptrChild, *newIdxVec);
782
783 newIdxVec->insert(newIdxVec->end(), idxVec->begin(), idxVec->end());
784 idxVec = newIdxVec;
785
786 assert(! ((PointerType*)ptrVal->getType())->getValueType()->isArrayType()
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000787 && "GetElemPtr cannot be folded into array refs in selection");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000788 }
789 else
790 {
791 // There is no GetElemPtr instruction.
792 // Use the pointer value and the index vector from the Mem instruction.
793 // If it is an array reference, get the array offset value.
794 //
795 ptrVal = memInst->getPtrOperand();
796
797 const Type* opType =
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000798 ((const PointerType*) ptrVal->getType())->getValueType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000799 if (opType->isArrayType())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000800 {
801 assert((memInst->getNumOperands()
802 == (unsigned) 1 + memInst->getFirstOffsetIdx())
803 && "Array refs must be lowered before Instruction Selection");
804
805 arrayOffsetVal = memInst->getOperand(memInst->getFirstOffsetIdx());
806 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000807 }
808
809 SetMemOperands_Internal(minstr, vmInstrNode, ptrVal, arrayOffsetVal,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000810 *idxVec, target);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000811
812 if (newIdxVec != NULL)
813 delete newIdxVec;
814}
815
816
817static void
818SetMemOperands_Internal(MachineInstr* minstr,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000819 const InstructionNode* vmInstrNode,
820 Value* ptrVal,
821 Value* arrayOffsetVal,
822 const vector<ConstPoolVal*>& idxVec,
823 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000824{
825 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
826
827 // Initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000828 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000829 Value* valueForRegOffset = NULL;
830 MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister;
831
832 // Check if there is an index vector and if so, if it translates to
833 // a small enough constant to fit in the immediate-offset field.
834 //
835 if (idxVec.size() > 0)
836 {
837 bool isConstantOffset = false;
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000838 unsigned offset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000839
840 const PointerType* ptrType = (PointerType*) ptrVal->getType();
841
842 if (ptrType->getValueType()->isStructType())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000843 {
844 // the offset is always constant for structs
845 isConstantOffset = true;
846
847 // Compute the offset value using the index vector
848 offset = target.DataLayout.getIndexedOffset(ptrType, idxVec);
849 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000850 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000851 {
852 // It must be an array ref. Check if the offset is a constant,
853 // and that the indexing has been lowered to a single offset.
854 //
855 assert(ptrType->getValueType()->isArrayType());
856 assert(arrayOffsetVal != NULL
857 && "Expect to be given Value* for array offsets");
858
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000859 if (ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(arrayOffsetVal))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000860 {
861 isConstantOffset = true; // always constant for structs
862 assert(arrayOffsetVal->getType()->isIntegral());
863 offset = (CPV->getType()->isSigned()
864 ? ((ConstPoolSInt*)CPV)->getValue()
865 : (int64_t) ((ConstPoolUInt*)CPV)->getValue());
866 }
867 else
868 {
869 valueForRegOffset = arrayOffsetVal;
870 }
871 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000872
873 if (isConstantOffset)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000874 {
875 // create a virtual register for the constant
876 valueForRegOffset = ConstPoolSInt::get(Type::IntTy, offset);
877 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000878 }
879 else
880 {
881 offsetOpType = MachineOperand::MO_SignExtendedImmed;
882 smallConstOffset = 0;
883 }
884
885 // Operand 0 is value for STORE, ptr for LOAD or GET_ELEMENT_PTR
886 // It is the left child in the instruction tree in all cases.
887 Value* leftVal = vmInstrNode->leftChild()->getValue();
888 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, leftVal);
889
890 // Operand 1 is ptr for STORE, offset for LOAD or GET_ELEMENT_PTR
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000891 // Operand 2 is offset for STORE, result reg for LOAD or GET_ELEMENT_PTR
Chris Lattner20b1ea02001-09-14 03:47:57 +0000892 //
893 unsigned offsetOpNum = (memInst->getOpcode() == Instruction::Store)? 2 : 1;
894 if (offsetOpType == MachineOperand::MO_VirtualRegister)
895 {
896 assert(valueForRegOffset != NULL);
897 minstr->SetMachineOperand(offsetOpNum, offsetOpType, valueForRegOffset);
898 }
899 else
900 minstr->SetMachineOperand(offsetOpNum, offsetOpType, smallConstOffset);
901
902 if (memInst->getOpcode() == Instruction::Store)
903 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, ptrVal);
904 else
905 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000906 vmInstrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000907}
908
909
Chris Lattner20b1ea02001-09-14 03:47:57 +0000910//
911// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +0000912// in place of the use(s) of that instruction in node `parent'.
913// Check both explicit and implicit operands!
Chris Lattner20b1ea02001-09-14 03:47:57 +0000914//
915static void
916ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000917 InstrTreeNode* parent,
918 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000919{
Vikram S. Adve243dd452001-09-18 13:03:13 +0000920 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
921
Chris Lattner20b1ea02001-09-14 03:47:57 +0000922 Instruction* unusedOp = treeNode->getInstruction();
923 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +0000924
925 // The parent itself may be a list node, so find the real parent instruction
926 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
927 {
928 parent = parent->parent();
929 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
930 }
931 InstructionNode* parentInstrNode = (InstructionNode*) parent;
932
933 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000934 MachineCodeForVMInstr& mvec = userInstr->getMachineInstrVec();
935 for (unsigned i=0, N=mvec.size(); i < N; i++)
936 {
937 MachineInstr* minstr = mvec[i];
Vikram S. Advec025fc12001-10-14 23:28:43 +0000938
939 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000940 {
941 const MachineOperand& mop = minstr->getOperand(i);
942 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
943 mop.getVRegValue() == unusedOp)
944 {
945 minstr->SetMachineOperand(i, MachineOperand::MO_VirtualRegister,
946 fwdOp);
947 }
948 }
Vikram S. Advec025fc12001-10-14 23:28:43 +0000949
950 for (unsigned i=0, numOps=minstr->getNumImplicitRefs(); i < numOps; ++i)
951 if (minstr->getImplicitRef(i) == unusedOp)
952 minstr->setImplicitRef(i, fwdOp, minstr->implicitRefIsDefined(i));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000953 }
954}
955
956
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000957void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000958CreateCopyInstructionsByType(const TargetMachine& target,
959 Value* src,
960 Instruction* dest,
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000961 vector<MachineInstr*>& minstrVec)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000962{
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000963 bool loadConstantToReg = false;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000964
965 const Type* resultType = dest->getType();
966
967 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
968 if (opCode == INVALID_OPCODE)
969 {
970 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000971 return;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000972 }
973
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000974 // if `src' is a constant that doesn't fit in the immed field or if it is
975 // a global variable (i.e., a constant address), generate a load
976 // instruction instead of an add
977 //
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000978 if (isa<ConstPoolVal>(src))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000979 {
980 unsigned int machineRegNum;
981 int64_t immedValue;
982 MachineOperand::MachineOperandType opType =
983 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
984 machineRegNum, immedValue);
985
986 if (opType == MachineOperand::MO_VirtualRegister)
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000987 loadConstantToReg = true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000988 }
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000989 else if (isa<GlobalValue>(src))
990 loadConstantToReg = true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000991
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000992 if (loadConstantToReg)
993 { // `src' is constant and cannot fit in immed field for the ADD
994 // Insert instructions to "load" the constant into a register
995 vector<TmpInstruction*> tempVec;
996 target.getInstrInfo().CreateCodeToLoadConst(src,dest,minstrVec,tempVec);
997 for (unsigned i=0; i < tempVec.size(); i++)
998 dest->getMachineInstrVec().addTempValue(tempVec[i]);
999 }
1000 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001001 { // Create the appropriate add instruction.
1002 // Make `src' the second operand, in case it is a constant
1003 // Use (unsigned long) 0 for a NULL pointer value.
1004 //
1005 const Type* nullValueType =
1006 (resultType->getPrimitiveID() == Type::PointerTyID)? Type::ULongTy
1007 : resultType;
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001008 MachineInstr* minstr = new MachineInstr(opCode);
1009 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1010 ConstPoolVal::getNullConstant(nullValueType));
1011 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, src);
1012 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, dest);
1013 minstrVec.push_back(minstr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001014 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001015}
1016
1017
Vikram S. Advefb361122001-10-22 13:36:31 +00001018//******************* Externally Visible Functions *************************/
1019
1020
1021//------------------------------------------------------------------------
1022// External Function: GetInstructionsForProlog
1023// External Function: GetInstructionsForEpilog
1024//
1025// Purpose:
1026// Create prolog and epilog code for procedure entry and exit
1027//------------------------------------------------------------------------
1028
1029extern unsigned
1030GetInstructionsForProlog(BasicBlock* entryBB,
1031 TargetMachine &target,
1032 MachineInstr** mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001033{
Vikram S. Advefb361122001-10-22 13:36:31 +00001034 int64_t s0=0; // used to avoid overloading ambiguity below
Chris Lattner20b1ea02001-09-14 03:47:57 +00001035
Vikram S. Advefb361122001-10-22 13:36:31 +00001036 // The second operand is the stack size. If it does not fit in the
1037 // immediate field, we either have to find an unused register in the
1038 // caller's window or move some elements to the dynamically allocated
1039 // area of the stack frame (just above save area and method args).
1040 Method* method = entryBB->getParent();
1041 MachineCodeForMethod& mcodeInfo = method->getMachineCode();
1042 unsigned int staticStackSize = mcodeInfo.getStaticStackSize();
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. Adve0589b2c2001-10-28 21:39:47 +00001050 - 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. Advefb361122001-10-22 13:36:31 +00001125 TargetMachine &tgt,
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
1130 Value *leftVal, *rightVal;
1131 const Type* opType;
1132 int nextRule;
1133 int forwardOperandNum = -1;
Vikram S. Adve8557b222001-10-10 20:56:33 +00001134 int64_t s0=0, s8=8; // variables holding constants to avoid
1135 uint64_t u0=0; // overloading ambiguities below
Chris Lattner20b1ea02001-09-14 03:47:57 +00001136
Vikram S. Advefb361122001-10-22 13:36:31 +00001137 UltraSparc& target = (UltraSparc&) tgt;
1138
1139 for (unsigned i=0; i < MAX_INSTR_PER_VMINSTR; i++)
1140 mvec[i] = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001141
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001142 //
1143 // Let's check for chain rules outside the switch so that we don't have
1144 // to duplicate the list of chain rule production numbers here again
1145 //
1146 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001147 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001148 // Chain rules have a single nonterminal on the RHS.
1149 // Get the rule that matches the RHS non-terminal and use that instead.
1150 //
1151 assert(nts[0] && ! nts[1]
1152 && "A chain rule should have only one RHS non-terminal!");
1153 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1154 nts = burm_nts[nextRule];
1155 numInstr = GetInstructionsByRule(subtreeRoot, nextRule, nts,target,mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001156 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001157 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001158 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001159 switch(ruleForNode) {
1160 case 1: // stmt: Ret
1161 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001162 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001163 // for moving return value to appropriate register.
1164 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001165 // Mark the return value register as an implicit ref of
1166 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001167 // Finally put a NOP in the delay slot.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001168 ReturnInst* returnInstr = (ReturnInst*) subtreeRoot->getInstruction();
1169 assert(returnInstr->getOpcode() == Instruction::Ret);
Vikram S. Advefb361122001-10-22 13:36:31 +00001170 Method* method = returnInstr->getParent()->getParent();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001171
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001172 Instruction* returnReg = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001173 returnInstr, NULL);
1174 returnInstr->getMachineInstrVec().addTempValue(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001175
1176 mvec[0] = new MachineInstr(JMPLRET);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001177 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1178 returnReg);
Vikram S. Adve8557b222001-10-10 20:56:33 +00001179 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,s8);
Vikram S. Advefb361122001-10-22 13:36:31 +00001180 mvec[0]->SetMachineOperand(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001181
Vikram S. Advea995e602001-10-11 04:23:19 +00001182 if (returnInstr->getReturnValue() != NULL)
1183 mvec[0]->addImplicitRef(returnInstr->getReturnValue());
1184
Vikram S. Advefb361122001-10-22 13:36:31 +00001185 unsigned n = numInstr++; // delay slot
1186 mvec[n] = new MachineInstr(NOP);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001187
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001188 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001189 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001190
1191 case 3: // stmt: Store(reg,reg)
1192 case 4: // stmt: Store(reg,ptrreg)
1193 mvec[0] = new MachineInstr(
1194 ChooseStoreInstruction(
1195 subtreeRoot->leftChild()->getValue()->getType()));
1196 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1197 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001198
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001199 case 5: // stmt: BrUncond
1200 mvec[0] = new MachineInstr(BA);
1201 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1202 (Value*)NULL);
1203 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1204 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1205
1206 // delay slot
1207 mvec[numInstr++] = new MachineInstr(NOP);
1208 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001209
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001210 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001211 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001212 // If the constant is ZERO, we can use the branch-on-integer-register
1213 // instructions and avoid the SUBcc instruction entirely.
1214 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001215 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001216 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1217 assert(constNode &&
1218 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
1219 ConstPoolVal* constVal = (ConstPoolVal*) constNode->getValue();
1220 bool isValidConst;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001221
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001222 if ((constVal->getType()->isIntegral()
1223 || constVal->getType()->isPointerType())
1224 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1225 && isValidConst)
1226 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001227 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1228
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001229 // That constant is a zero after all...
1230 // Use the left child of setCC as the first argument!
1231 mvec[0] = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1232 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1233 subtreeRoot->leftChild()->leftChild()->getValue());
1234 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001235 brInst->getSuccessor(0));
Chris Lattner20b1ea02001-09-14 03:47:57 +00001236
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001237 // delay slot
1238 mvec[numInstr++] = new MachineInstr(NOP);
1239
1240 // false branch
1241 int n = numInstr++;
1242 mvec[n] = new MachineInstr(BA);
1243 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1244 (Value*) NULL);
1245 mvec[n]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001246 brInst->getSuccessor(1));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001247
1248 // delay slot
1249 mvec[numInstr++] = new MachineInstr(NOP);
1250
1251 break;
1252 }
1253 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001254 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001255
1256 case 6: // stmt: BrCond(bool)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001257 { // bool => boolean was computed with some boolean operator
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001258 // (SetCC, Not, ...). We need to check whether the type was a FP,
1259 // signed int or unsigned int, and check the branching condition in
1260 // order to choose the branch to use.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001261 // If it is an integer CC, we also need to find the unique
1262 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001263 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001264 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001265 bool isFPBranch;
1266 mvec[0] = new MachineInstr(ChooseBccInstruction(subtreeRoot,
1267 isFPBranch));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001268
1269 Value* ccValue = isFPBranch? subtreeRoot->leftChild()->getValue()
1270 : GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1271 brInst->getParent()->getParent());
1272
1273 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister, ccValue);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001274 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001275 brInst->getSuccessor(0));
1276
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001277 // delay slot
1278 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001279
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001280 // false branch
1281 int n = numInstr++;
1282 mvec[n] = new MachineInstr(BA);
1283 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1284 (Value*) NULL);
1285 mvec[n]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001286 brInst->getSuccessor(1));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001287
1288 // delay slot
1289 mvec[numInstr++] = new MachineInstr(NOP);
1290 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001291 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001292
1293 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001294 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001295 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnercfe26c92001-10-01 18:26:53 +00001296 ConstPoolVal* constVal =
1297 cast<ConstPoolVal>(subtreeRoot->leftChild()->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001298 unsigned dest = ((ConstPoolBool*) constVal)->getValue()? 0 : 1;
1299
1300 mvec[0] = new MachineInstr(BA);
1301 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1302 (Value*) NULL);
1303 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1304 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(dest));
1305
1306 // delay slot
1307 mvec[numInstr++] = new MachineInstr(NOP);
1308 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001309 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001310
1311 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001312 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001313 // Just use the branch-on-integer-register instruction!
1314 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001315 mvec[0] = new MachineInstr(BRNZ);
1316 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1317 subtreeRoot->leftChild()->getValue());
1318 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1319 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1320
1321 // delay slot
1322 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1323
1324 // false branch
1325 int n = numInstr++;
1326 mvec[n] = new MachineInstr(BA);
1327 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1328 (Value*) NULL);
1329 mvec[n]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1330 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
1331
1332 // delay slot
1333 mvec[numInstr++] = new MachineInstr(NOP);
1334 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001335 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001336
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001337 case 9: // stmt: Switch(reg)
1338 assert(0 && "*** SWITCH instruction is not implemented yet.");
1339 numInstr = 0;
1340 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001341
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001342 case 10: // reg: VRegList(reg, reg)
1343 assert(0 && "VRegList should never be the topmost non-chain rule");
1344 break;
1345
1346 case 21: // reg: Not(reg): Implemented as reg = reg XOR-NOT 0
1347 mvec[0] = new MachineInstr(XNOR);
1348 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1349 subtreeRoot->leftChild()->getValue());
1350 mvec[0]->SetMachineOperand(1, target.getRegInfo().getZeroRegNum());
1351 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1352 subtreeRoot->getValue());
1353 break;
1354
1355 case 322: // reg: ToBoolTy(bool):
1356 case 22: // reg: ToBoolTy(reg):
1357 opType = subtreeRoot->leftChild()->getValue()->getType();
1358 assert(opType->isIntegral() || opType == Type::BoolTy);
1359 numInstr = 0;
1360 forwardOperandNum = 0;
1361 break;
1362
1363 case 23: // reg: ToUByteTy(reg)
1364 case 25: // reg: ToUShortTy(reg)
1365 case 27: // reg: ToUIntTy(reg)
1366 case 29: // reg: ToULongTy(reg)
1367 opType = subtreeRoot->leftChild()->getValue()->getType();
1368 assert(opType->isIntegral() ||
1369 opType->isPointerType() ||
1370 opType == Type::BoolTy && "Cast is illegal for other types");
1371 numInstr = 0;
1372 forwardOperandNum = 0;
1373 break;
1374
1375 case 24: // reg: ToSByteTy(reg)
1376 case 26: // reg: ToShortTy(reg)
1377 case 28: // reg: ToIntTy(reg)
1378 case 30: // reg: ToLongTy(reg)
1379 opType = subtreeRoot->leftChild()->getValue()->getType();
1380 if (opType->isIntegral() || opType == Type::BoolTy)
1381 {
1382 numInstr = 0;
1383 forwardOperandNum = 0;
1384 }
1385 else
1386 {
1387 mvec[0] = new MachineInstr(ChooseConvertToIntInstr(subtreeRoot,
1388 opType));
1389 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
1390 }
1391 break;
1392
1393 case 31: // reg: ToFloatTy(reg):
1394 case 32: // reg: ToDoubleTy(reg):
1395 case 232: // reg: ToDoubleTy(Constant):
1396
1397 // If this instruction has a parent (a user) in the tree
1398 // and the user is translated as an FsMULd instruction,
1399 // then the cast is unnecessary. So check that first.
1400 // In the future, we'll want to do the same for the FdMULq instruction,
1401 // so do the check here instead of only for ToFloatTy(reg).
1402 //
1403 if (subtreeRoot->parent() != NULL &&
1404 ((InstructionNode*) subtreeRoot->parent())->getInstruction()->getMachineInstrVec()[0]->getOpCode() == FSMULD)
1405 {
1406 numInstr = 0;
1407 forwardOperandNum = 0;
1408 }
1409 else
1410 {
1411 opType = subtreeRoot->leftChild()->getValue()->getType();
1412 MachineOpCode opCode=ChooseConvertToFloatInstr(subtreeRoot,opType);
1413 if (opCode == INVALID_OPCODE) // no conversion needed
1414 {
1415 numInstr = 0;
1416 forwardOperandNum = 0;
1417 }
1418 else
1419 {
1420 mvec[0] = new MachineInstr(opCode);
1421 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
1422 }
1423 }
1424 break;
1425
1426 case 19: // reg: ToArrayTy(reg):
1427 case 20: // reg: ToPointerTy(reg):
1428 numInstr = 0;
1429 forwardOperandNum = 0;
1430 break;
1431
1432 case 233: // reg: Add(reg, Constant)
1433 mvec[0] = CreateAddConstInstruction(subtreeRoot);
1434 if (mvec[0] != NULL)
1435 break;
1436 // ELSE FALL THROUGH
1437
1438 case 33: // reg: Add(reg, reg)
1439 mvec[0] = new MachineInstr(ChooseAddInstruction(subtreeRoot));
1440 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1441 break;
1442
1443 case 234: // reg: Sub(reg, Constant)
1444 mvec[0] = CreateSubConstInstruction(subtreeRoot);
1445 if (mvec[0] != NULL)
1446 break;
1447 // ELSE FALL THROUGH
1448
1449 case 34: // reg: Sub(reg, reg)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001450 mvec[0] = new MachineInstr(ChooseSubInstructionByType(
1451 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001452 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1453 break;
1454
1455 case 135: // reg: Mul(todouble, todouble)
1456 checkCast = true;
1457 // FALL THROUGH
1458
1459 case 35: // reg: Mul(reg, reg)
1460 mvec[0] =new MachineInstr(ChooseMulInstruction(subtreeRoot,checkCast));
1461 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1462 break;
1463
1464 case 335: // reg: Mul(todouble, todoubleConst)
1465 checkCast = true;
1466 // FALL THROUGH
1467
1468 case 235: // reg: Mul(reg, Constant)
1469 mvec[0] = CreateMulConstInstruction(target, subtreeRoot, mvec[1]);
1470 if (mvec[0] == NULL)
1471 {
1472 mvec[0] = new MachineInstr(ChooseMulInstruction(subtreeRoot,
1473 checkCast));
1474 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1475 }
1476 else
1477 if (mvec[1] != NULL)
1478 ++numInstr;
1479 break;
1480
1481 case 236: // reg: Div(reg, Constant)
1482 mvec[0] = CreateDivConstInstruction(target, subtreeRoot, mvec[1]);
1483 if (mvec[0] != NULL)
1484 {
1485 if (mvec[1] != NULL)
1486 ++numInstr;
1487 }
1488 else
1489 // ELSE FALL THROUGH
1490
1491 case 36: // reg: Div(reg, reg)
1492 mvec[0] = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1493 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1494 break;
1495
1496 case 37: // reg: Rem(reg, reg)
1497 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001498 {
1499 Instruction* remInstr = subtreeRoot->getInstruction();
1500
1501 TmpInstruction* quot = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
1502 subtreeRoot->leftChild()->getValue(),
1503 subtreeRoot->rightChild()->getValue());
1504 TmpInstruction* prod = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
1505 quot,
1506 subtreeRoot->rightChild()->getValue());
1507 remInstr->getMachineInstrVec().addTempValue(quot);
1508 remInstr->getMachineInstrVec().addTempValue(prod);
1509
1510 mvec[0] = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1511 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1512 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,quot);
1513
1514 int n = numInstr++;
1515 mvec[n] = new MachineInstr(ChooseMulInstructionByType(
1516 subtreeRoot->getInstruction()->getType()));
1517 mvec[n]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,quot);
1518 mvec[n]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1519 subtreeRoot->rightChild()->getValue());
1520 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,prod);
1521
1522 n = numInstr++;
1523 mvec[n] = new MachineInstr(ChooseSubInstructionByType(
1524 subtreeRoot->getInstruction()->getType()));
1525 Set3OperandsFromInstr(mvec[n], subtreeRoot, target);
1526 mvec[n]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,prod);
1527
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001528 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001529 }
1530
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001531 case 38: // reg: And(reg, reg)
1532 case 238: // reg: And(reg, Constant)
1533 mvec[0] = new MachineInstr(AND);
1534 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1535 break;
1536
1537 case 138: // reg: And(reg, not)
1538 mvec[0] = new MachineInstr(ANDN);
1539 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1540 break;
1541
1542 case 39: // reg: Or(reg, reg)
1543 case 239: // reg: Or(reg, Constant)
1544 mvec[0] = new MachineInstr(ORN);
1545 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1546 break;
1547
1548 case 139: // reg: Or(reg, not)
1549 mvec[0] = new MachineInstr(ORN);
1550 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1551 break;
1552
1553 case 40: // reg: Xor(reg, reg)
1554 case 240: // reg: Xor(reg, Constant)
1555 mvec[0] = new MachineInstr(XOR);
1556 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1557 break;
1558
1559 case 140: // reg: Xor(reg, not)
1560 mvec[0] = new MachineInstr(XNOR);
1561 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1562 break;
1563
1564 case 41: // boolconst: SetCC(reg, Constant)
1565 // Check if this is an integer comparison, and
1566 // there is a parent, and the parent decided to use
1567 // a branch-on-integer-register instead of branch-on-condition-code.
1568 // If so, the SUBcc instruction is not required.
1569 // (However, we must still check for constants to be loaded from
1570 // the constant pool so that such a load can be associated with
1571 // this instruction.)
1572 //
1573 // Otherwise this is just the same as case 42, so just fall through.
1574 //
1575 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral() &&
1576 subtreeRoot->parent() != NULL)
1577 {
1578 InstructionNode* parent = (InstructionNode*) subtreeRoot->parent();
1579 assert(parent->getNodeType() == InstrTreeNode::NTInstructionNode);
1580 const vector<MachineInstr*>&
1581 minstrVec = parent->getInstruction()->getMachineInstrVec();
1582 MachineOpCode parentOpCode;
1583 if (parent->getInstruction()->getOpcode() == Instruction::Br &&
1584 (parentOpCode = minstrVec[0]->getOpCode()) >= BRZ &&
1585 parentOpCode <= BRGEZ)
1586 {
1587 numInstr = 0; // don't forward the operand!
1588 break;
1589 }
1590 }
1591 // ELSE FALL THROUGH
1592
1593 case 42: // bool: SetCC(reg, reg):
1594 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001595 // This generates a SUBCC instruction, putting the difference in
1596 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001597 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001598 // If the boolean result of the SetCC is used by anything other
1599 // than a single branch instruction, the boolean must be
1600 // computed and stored in the result register. Otherwise, discard
1601 // the difference (by using %g0) and keep only the condition code.
1602 //
1603 // To compute the boolean result in a register we use a conditional
1604 // move, unless the result of the SUBCC instruction can be used as
1605 // the bool! This assumes that zero is FALSE and any non-zero
1606 // integer is TRUE.
1607 //
1608 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1609 Instruction* setCCInstr = subtreeRoot->getInstruction();
1610 bool keepBoolVal = (parentNode == NULL ||
1611 parentNode->getInstruction()->getOpcode()
1612 != Instruction::Br);
1613 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001614 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1615 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1616
1617 bool mustClearReg;
1618 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001619 MachineOpCode movOpCode = 0;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001620 Value* ccValue = NULL;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001621
1622 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral() ||
1623 subtreeRoot->leftChild()->getValue()->getType()->isPointerType())
1624 {
1625 // Integer condition: dest. should be %g0 or an integer register.
1626 // If result must be saved but condition is not SetEQ then we need
1627 // a separate instruction to compute the bool result, so discard
1628 // result of SUBcc instruction anyway.
1629 //
1630 mvec[0] = new MachineInstr(SUBcc);
1631 Set3OperandsFromInstr(mvec[0], subtreeRoot, target, ! keepSubVal);
1632
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001633 // Mark the 4th operand as being a CC register, and as a def
1634 // A TmpInstruction is created to represent the int CC "result".
1635 // Unlike other instances of TmpInstruction, this one is used by
1636 // used by machine code of multiple LLVM instructions, viz.,
1637 // the SetCC and the branch. Make sure to get the same one!
1638 //
1639 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1640 setCCInstr->getParent()->getParent());
1641 setCCInstr->getMachineInstrVec().addTempValue(tmpForCC);
1642
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001643 mvec[0]->SetMachineOperand(3, MachineOperand::MO_CCRegister,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001644 tmpForCC, /*def*/true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001645
1646 if (computeBoolVal)
1647 { // recompute bool using the integer condition codes
1648 movOpCode =
1649 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001650 ccValue = tmpForCC;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001651 }
1652 }
1653 else
1654 {
1655 // FP condition: dest of FCMP should be some FCCn register
1656 mvec[0] = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001657 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1658 setCCInstr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001659 mvec[0]->SetMachineOperand(1,MachineOperand::MO_VirtualRegister,
1660 subtreeRoot->leftChild()->getValue());
1661 mvec[0]->SetMachineOperand(2,MachineOperand::MO_VirtualRegister,
1662 subtreeRoot->rightChild()->getValue());
1663
1664 if (computeBoolVal)
1665 {// recompute bool using the FP condition codes
1666 mustClearReg = true;
1667 valueToMove = 1;
1668 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001669 ccValue = setCCInstr;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001670 }
1671 }
1672
1673 if (computeBoolVal)
1674 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001675 assert(ccValue && "Inconsistent logic above and here");
1676
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001677 if (mustClearReg)
1678 {// Unconditionally set register to 0
1679 int n = numInstr++;
1680 mvec[n] = new MachineInstr(SETHI);
1681 mvec[n]->SetMachineOperand(0,MachineOperand::MO_UnextendedImmed,
1682 s0);
1683 mvec[n]->SetMachineOperand(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001684 setCCInstr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001685 }
1686
1687 // Now conditionally move `valueToMove' (0 or 1) into the register
1688 int n = numInstr++;
1689 mvec[n] = new MachineInstr(movOpCode);
1690 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001691 ccValue);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001692 mvec[n]->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
1693 valueToMove);
1694 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001695 setCCInstr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001696 }
1697 break;
1698 }
1699
1700 case 43: // boolreg: VReg
1701 case 44: // boolreg: Constant
1702 numInstr = 0;
1703 break;
1704
1705 case 51: // reg: Load(reg)
1706 case 52: // reg: Load(ptrreg)
1707 case 53: // reg: LoadIdx(reg,reg)
1708 case 54: // reg: LoadIdx(ptrreg,reg)
1709 mvec[0] = new MachineInstr(ChooseLoadInstruction(
1710 subtreeRoot->getValue()->getType()));
1711 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1712 break;
1713
1714 case 55: // reg: GetElemPtr(reg)
1715 case 56: // reg: GetElemPtrIdx(reg,reg)
1716 if (subtreeRoot->parent() != NULL)
1717 {
1718 // Check if the parent was an array access.
1719 // If so, we still need to generate this instruction.
1720 MemAccessInst* memInst = (MemAccessInst*)
1721 subtreeRoot->getInstruction();
1722 const PointerType* ptrType =
1723 (const PointerType*) memInst->getPtrOperand()->getType();
1724 if (! ptrType->getValueType()->isArrayType())
1725 {// we don't need a separate instr
1726 numInstr = 0; // don't forward operand!
1727 break;
1728 }
1729 }
1730 // else in all other cases we need to a separate ADD instruction
1731 mvec[0] = new MachineInstr(ADD);
1732 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1733 break;
1734
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001735 case 57: // reg: Alloca: Implement as 1 instruction:
1736 { // add %fp, offsetFromFP -> result
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001737 Instruction* instr = subtreeRoot->getInstruction();
1738 const PointerType* instrType = (const PointerType*) instr->getType();
1739 assert(instrType->isPointerType());
1740 int tsize = (int)
1741 target.findOptimalStorageSize(instrType->getValueType());
1742 assert(tsize != 0 && "Just to check when this can happen");
1743
Vikram S. Advefb361122001-10-22 13:36:31 +00001744 Method* method = instr->getParent()->getParent();
1745 MachineCodeForMethod& mcode = method->getMachineCode();
1746 int offsetFromFP =
1747 target.getFrameInfo().getFirstAutomaticVarOffsetFromFP(method)
1748 - (tsize + mcode.getAutomaticVarsSize());
1749
1750 mcode.putLocalVarAtOffsetFromFP(instr, offsetFromFP, tsize);
1751
1752 // Create a temporary Value to hold the constant offset.
1753 // This is needed because it may not fit in the immediate field.
1754 ConstPoolSInt* offsetVal=ConstPoolSInt::get(Type::IntTy, offsetFromFP);
1755
1756 // Instruction 1: add %fp, offsetFromFP -> result
1757 mvec[0] = new MachineInstr(ADD);
1758 mvec[0]->SetMachineOperand(0, target.getRegInfo().getFramePointer());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001759 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
Vikram S. Advefb361122001-10-22 13:36:31 +00001760 offsetVal);
1761 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001762 instr);
1763 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001764 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001765
1766 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1767 // mul num, typeSz -> tmp
1768 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001769 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001770 Instruction* instr = subtreeRoot->getInstruction();
1771 const PointerType* instrType = (const PointerType*) instr->getType();
1772 assert(instrType->isPointerType() &&
1773 instrType->getValueType()->isArrayType());
1774 const Type* eltType =
1775 ((ArrayType*) instrType->getValueType())->getElementType();
1776 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advefb361122001-10-22 13:36:31 +00001777
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001778 assert(tsize != 0 && "Just to check when this can happen");
Vikram S. Advefb361122001-10-22 13:36:31 +00001779
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001780 // Create a temporary Value to hold the constant type-size
Vikram S. Advefb361122001-10-22 13:36:31 +00001781 ConstPoolSInt* tsizeVal = ConstPoolSInt::get(Type::IntTy, tsize);
1782
1783 // Create a temporary Value to hold the constant offset from SP
1784 Method* method = instr->getParent()->getParent();
1785 MachineCodeForMethod& mcode = method->getMachineCode();
1786 int frameSizeBelowDynamicArea =
1787 target.getFrameInfo().getFrameSizeBelowDynamicArea(method);
1788 ConstPoolSInt* lowerAreaSizeVal = ConstPoolSInt::get(Type::IntTy,
1789 frameSizeBelowDynamicArea);
1790 cerr << "***" << endl
1791 << "*** Variable-size ALLOCA operation needs more work:" << endl
1792 << "*** We have to precompute the size of "
1793 << " optional arguments in the stack frame" << endl
1794 << "***" << endl;
1795 assert(0 && "SEE MESSAGE ABOVE");
1796
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001797 // Create a temporary value to hold `tmp'
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001798 Instruction* tmpInstr = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001799 subtreeRoot->leftChild()->getValue(),
1800 NULL /*could insert tsize here*/);
1801 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(tmpInstr);
1802
1803 // Instruction 1: mul numElements, typeSize -> tmp
1804 mvec[0] = new MachineInstr(MULX);
1805 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefb361122001-10-22 13:36:31 +00001806 subtreeRoot->leftChild()->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001807 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
Vikram S. Advefb361122001-10-22 13:36:31 +00001808 tsizeVal);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001809 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1810 tmpInstr);
Vikram S. Advefb361122001-10-22 13:36:31 +00001811
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001812 // Instruction 2: sub %sp, tmp -> %sp
1813 numInstr++;
1814 mvec[1] = new MachineInstr(SUB);
Vikram S. Advefb361122001-10-22 13:36:31 +00001815 mvec[1]->SetMachineOperand(0, target.getRegInfo().getStackPointer());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001816 mvec[1]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1817 tmpInstr);
Vikram S. Advefb361122001-10-22 13:36:31 +00001818 mvec[1]->SetMachineOperand(2, target.getRegInfo().getStackPointer());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001819
Vikram S. Advefb361122001-10-22 13:36:31 +00001820 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001821 numInstr++;
1822 mvec[2] = new MachineInstr(ADD);
Vikram S. Advefb361122001-10-22 13:36:31 +00001823 mvec[2]->SetMachineOperand(0, target.getRegInfo().getStackPointer());
1824 mvec[2]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1825 lowerAreaSizeVal);
1826 mvec[2]->SetMachineOperand(2,MachineOperand::MO_VirtualRegister,instr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001827 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001828 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001829
1830 case 61: // reg: Call
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001831 { // Generate a call-indirect (i.e., jmpl) for now to expose
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001832 // the potential need for registers. If an absolute address
1833 // is available, replace this with a CALL instruction.
1834 // Mark both the indirection register and the return-address
1835 // register as hidden virtual registers.
Vikram S. Advea995e602001-10-11 04:23:19 +00001836 // Also, mark the operands of the Call and return value (if
1837 // any) as implicit operands of the CALL machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001838 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00001839 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00001840 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001841
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001842 Instruction* retAddrReg = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001843 callInstr, NULL);
Vikram S. Adve8557b222001-10-10 20:56:33 +00001844
Vikram S. Advea995e602001-10-11 04:23:19 +00001845 // Note temporary values in the machineInstrVec for the VM instr.
Vikram S. Adve8557b222001-10-10 20:56:33 +00001846 //
1847 // WARNING: Operands 0..N-1 must go in slots 0..N-1 of implicitUses.
1848 // The result value must go in slot N. This is assumed
1849 // in register allocation.
1850 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001851 callInstr->getMachineInstrVec().addTempValue(retAddrReg);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001852
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001853
1854 // Generate the machine instruction and its operands.
1855 // Use CALL for direct function calls; this optimistically assumes
1856 // the PC-relative address fits in the CALL address field (22 bits).
1857 // Use JMPL for indirect calls.
1858 //
1859 if (callee->getValueType() == Value::MethodVal)
1860 { // direct function call
1861 mvec[0] = new MachineInstr(CALL);
1862 mvec[0]->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp,
1863 callee);
1864 }
1865 else
1866 { // indirect function call
Vikram S. Advefb361122001-10-22 13:36:31 +00001867 mvec[0] = new MachineInstr(JMPLCALL);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001868 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1869 callee);
1870 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1871 (int64_t) 0);
1872 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1873 retAddrReg);
1874 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001875
Vikram S. Advea995e602001-10-11 04:23:19 +00001876 // Add the call operands and return value as implicit refs
1877 for (unsigned i=0, N=callInstr->getNumOperands(); i < N; ++i)
1878 if (callInstr->getOperand(i) != callee)
1879 mvec[0]->addImplicitRef(callInstr->getOperand(i));
1880
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001881 if (callInstr->getType() != Type::VoidTy)
Vikram S. Advea995e602001-10-11 04:23:19 +00001882 mvec[0]->addImplicitRef(callInstr, /*isDef*/ true);
1883
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001884 // For the CALL instruction, the ret. addr. reg. is also implicit
1885 if (callee->getValueType() == Value::MethodVal)
1886 mvec[0]->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001887
1888 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1889 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001890 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001891
1892 case 62: // reg: Shl(reg, reg)
1893 opType = subtreeRoot->leftChild()->getValue()->getType();
1894 assert(opType->isIntegral()
1895 || opType == Type::BoolTy
1896 || opType->isPointerType()&& "Shl unsupported for other types");
1897 mvec[0] = new MachineInstr((opType == Type::LongTy)? SLLX : SLL);
1898 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1899 break;
1900
1901 case 63: // reg: Shr(reg, reg)
1902 opType = subtreeRoot->leftChild()->getValue()->getType();
1903 assert(opType->isIntegral()
1904 || opType == Type::BoolTy
1905 || opType->isPointerType() &&"Shr unsupported for other types");
1906 mvec[0] = new MachineInstr((opType->isSigned()
1907 ? ((opType == Type::LongTy)? SRAX : SRA)
1908 : ((opType == Type::LongTy)? SRLX : SRL)));
1909 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1910 break;
1911
1912 case 64: // reg: Phi(reg,reg)
1913 { // This instruction has variable #operands, so resultPos is 0.
1914 Instruction* phi = subtreeRoot->getInstruction();
1915 mvec[0] = new MachineInstr(PHI, 1 + phi->getNumOperands());
1916 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1917 subtreeRoot->getValue());
1918 for (unsigned i=0, N=phi->getNumOperands(); i < N; i++)
1919 mvec[0]->SetMachineOperand(i+1, MachineOperand::MO_VirtualRegister,
1920 phi->getOperand(i));
1921 break;
1922 }
1923 case 71: // reg: VReg
1924 case 72: // reg: Constant
1925 numInstr = 0; // don't forward the value
1926 break;
1927
1928 default:
1929 assert(0 && "Unrecognized BURG rule");
1930 numInstr = 0;
1931 break;
1932 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001933 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001934
1935 if (forwardOperandNum >= 0)
1936 { // We did not generate a machine instruction but need to use operand.
1937 // If user is in the same tree, replace Value in its machine operand.
1938 // If not, insert a copy instruction which should get coalesced away
1939 // by register allocation.
1940 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001941 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001942 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001943 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001944 vector<MachineInstr*> minstrVec;
1945 CreateCopyInstructionsByType(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001946 subtreeRoot->getInstruction()->getOperand(forwardOperandNum),
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001947 subtreeRoot->getInstruction(), minstrVec);
1948 assert(minstrVec.size() > 0);
1949 for (unsigned i=0; i < minstrVec.size(); ++i)
1950 mvec[numInstr++] = minstrVec[i];
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001951 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001952 }
1953
Chris Lattner20b1ea02001-09-14 03:47:57 +00001954 return numInstr;
1955}
1956
1957