blob: dc8ab1b761f7c103365057888a0f0f6d7032a59d [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
137static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000138ChooseBccInstruction(const InstructionNode* instrNode,
139 bool& isFPBranch)
140{
141 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
142 BinaryOperator* setCCInstr = (BinaryOperator*) setCCNode->getInstruction();
143 const Type* setCCType = setCCInstr->getOperand(0)->getType();
144
145 isFPBranch = (setCCType == Type::FloatTy || setCCType == Type::DoubleTy);
146
147 if (isFPBranch)
148 return ChooseBFpccInstruction(instrNode, setCCInstr);
149 else
150 return ChooseBpccInstruction(instrNode, setCCInstr);
151}
152
153
154static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000155ChooseMovFpccInstruction(const InstructionNode* instrNode)
156{
157 MachineOpCode opCode = INVALID_OPCODE;
158
159 switch(instrNode->getInstruction()->getOpcode())
160 {
161 case Instruction::SetEQ: opCode = MOVFE; break;
162 case Instruction::SetNE: opCode = MOVFNE; break;
163 case Instruction::SetLE: opCode = MOVFLE; break;
164 case Instruction::SetGE: opCode = MOVFGE; break;
165 case Instruction::SetLT: opCode = MOVFL; break;
166 case Instruction::SetGT: opCode = MOVFG; break;
167 default:
168 assert(0 && "Unrecognized VM instruction!");
169 break;
170 }
171
172 return opCode;
173}
174
175
176// Assumes that SUBcc v1, v2 -> v3 has been executed.
177// In most cases, we want to clear v3 and then follow it by instruction
178// MOVcc 1 -> v3.
179// Set mustClearReg=false if v3 need not be cleared before conditional move.
180// Set valueToMove=0 if we want to conditionally move 0 instead of 1
181// (i.e., we want to test inverse of a condition)
Vikram S. Adve243dd452001-09-18 13:03:13 +0000182// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000183//
184static MachineOpCode
185ChooseMovpccAfterSub(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000186 bool& mustClearReg,
187 int& valueToMove)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000188{
189 MachineOpCode opCode = INVALID_OPCODE;
190 mustClearReg = true;
191 valueToMove = 1;
192
193 switch(instrNode->getInstruction()->getOpcode())
194 {
Vikram S. Adve243dd452001-09-18 13:03:13 +0000195 case Instruction::SetEQ: opCode = MOVE; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000196 case Instruction::SetLE: opCode = MOVLE; break;
197 case Instruction::SetGE: opCode = MOVGE; break;
198 case Instruction::SetLT: opCode = MOVL; break;
199 case Instruction::SetGT: opCode = MOVG; break;
Vikram S. Adve243dd452001-09-18 13:03:13 +0000200 case Instruction::SetNE: assert(0 && "No move required!"); break;
201 default: assert(0 && "Unrecognized VM instr!"); break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000202 }
203
204 return opCode;
205}
206
Chris Lattner20b1ea02001-09-14 03:47:57 +0000207static inline MachineOpCode
208ChooseConvertToFloatInstr(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000209 const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000210{
211 MachineOpCode opCode = INVALID_OPCODE;
212
213 switch(instrNode->getOpLabel())
214 {
215 case ToFloatTy:
216 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000217 opCode = FITOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000218 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000219 opCode = FXTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000220 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000221 opCode = FDTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000222 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000223 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000224 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000225 assert(0 && "Cannot convert this type to FLOAT on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000226 break;
227
228 case ToDoubleTy:
229 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000230 opCode = FITOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000231 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000232 opCode = FXTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000233 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000234 opCode = FSTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000235 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000236 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000237 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000238 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000239 break;
240
241 default:
242 break;
243 }
244
245 return opCode;
246}
247
248static inline MachineOpCode
249ChooseConvertToIntInstr(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000250 const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000251{
252 MachineOpCode opCode = INVALID_OPCODE;;
253
254 int instrType = (int) instrNode->getOpLabel();
255
256 if (instrType == ToSByteTy || instrType == ToShortTy || instrType == ToIntTy)
257 {
258 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000259 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000260 case Type::FloatTyID: opCode = FSTOI; break;
261 case Type::DoubleTyID: opCode = FDTOI; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000262 default:
263 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
264 break;
265 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000266 }
267 else if (instrType == ToLongTy)
268 {
269 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000270 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000271 case Type::FloatTyID: opCode = FSTOX; break;
272 case Type::DoubleTyID: opCode = FDTOX; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000273 default:
274 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
275 break;
276 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000277 }
278 else
279 assert(0 && "Should not get here, Mo!");
280
281 return opCode;
282}
283
284
285static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000286ChooseAddInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000287{
288 MachineOpCode opCode = INVALID_OPCODE;
289
Chris Lattner20b1ea02001-09-14 03:47:57 +0000290 if (resultType->isIntegral() ||
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000291 isa<PointerType>(resultType) ||
292 isa<MethodType>(resultType) ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000293 resultType->isLabelType() ||
294 resultType == Type::BoolTy)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000295 {
296 opCode = ADD;
297 }
298 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000299 switch(resultType->getPrimitiveID())
300 {
301 case Type::FloatTyID: opCode = FADDS; break;
302 case Type::DoubleTyID: opCode = FADDD; break;
303 default: assert(0 && "Invalid type for ADD instruction"); break;
304 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000305
306 return opCode;
307}
308
309
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000310static inline MachineOpCode
311ChooseAddInstruction(const InstructionNode* instrNode)
312{
313 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
314}
315
316
Chris Lattner20b1ea02001-09-14 03:47:57 +0000317static inline MachineInstr*
318CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000319 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000320{
321 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000322 ? FMOVS : FMOVD);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000323 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000324 instrNode->leftChild()->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000325 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000326 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000327 return minstr;
328}
329
330static inline MachineInstr*
331CreateAddConstInstruction(const InstructionNode* instrNode)
332{
333 MachineInstr* minstr = NULL;
334
335 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000336 assert(isa<ConstPoolVal>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000337
338 // Cases worth optimizing are:
339 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
340 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
341 //
342 const Type* resultType = instrNode->getInstruction()->getType();
343
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000344 if (resultType == Type::FloatTy ||
345 resultType == Type::DoubleTy)
346 {
347 double dval = ((ConstPoolFP*) constOp)->getValue();
348 if (dval == 0.0)
349 minstr = CreateMovFloatInstruction(instrNode, resultType);
350 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000351
352 return minstr;
353}
354
355
356static inline MachineOpCode
357ChooseSubInstruction(const InstructionNode* instrNode)
358{
359 MachineOpCode opCode = INVALID_OPCODE;
360
361 const Type* resultType = instrNode->getInstruction()->getType();
362
363 if (resultType->isIntegral() ||
364 resultType->isPointerType())
365 {
366 opCode = SUB;
367 }
368 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000369 switch(resultType->getPrimitiveID())
370 {
371 case Type::FloatTyID: opCode = FSUBS; break;
372 case Type::DoubleTyID: opCode = FSUBD; break;
373 default: assert(0 && "Invalid type for SUB instruction"); break;
374 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000375
376 return opCode;
377}
378
379
380static inline MachineInstr*
381CreateSubConstInstruction(const InstructionNode* instrNode)
382{
383 MachineInstr* minstr = NULL;
384
385 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000386 assert(isa<ConstPoolVal>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000387
388 // Cases worth optimizing are:
389 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
390 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
391 //
392 const Type* resultType = instrNode->getInstruction()->getType();
393
394 if (resultType == Type::FloatTy ||
395 resultType == Type::DoubleTy)
396 {
397 double dval = ((ConstPoolFP*) constOp)->getValue();
398 if (dval == 0.0)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000399 minstr = CreateMovFloatInstruction(instrNode, resultType);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000400 }
401
402 return minstr;
403}
404
405
406static inline MachineOpCode
407ChooseFcmpInstruction(const InstructionNode* instrNode)
408{
409 MachineOpCode opCode = INVALID_OPCODE;
410
411 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
412 switch(operand->getType()->getPrimitiveID()) {
413 case Type::FloatTyID: opCode = FCMPS; break;
414 case Type::DoubleTyID: opCode = FCMPD; break;
415 default: assert(0 && "Invalid type for FCMP instruction"); break;
416 }
417
418 return opCode;
419}
420
421
422// Assumes that leftArg and rightArg are both cast instructions.
423//
424static inline bool
425BothFloatToDouble(const InstructionNode* instrNode)
426{
427 InstrTreeNode* leftArg = instrNode->leftChild();
428 InstrTreeNode* rightArg = instrNode->rightChild();
429 InstrTreeNode* leftArgArg = leftArg->leftChild();
430 InstrTreeNode* rightArgArg = rightArg->leftChild();
431 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
432
433 // Check if both arguments are floats cast to double
434 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000435 leftArgArg->getValue()->getType() == Type::FloatTy &&
436 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000437}
438
439
440static inline MachineOpCode
441ChooseMulInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000442 bool checkCasts)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000443{
444 MachineOpCode opCode = INVALID_OPCODE;
445
446 if (checkCasts && BothFloatToDouble(instrNode))
447 {
448 return opCode = FSMULD;
449 }
450 // else fall through and use the regular multiply instructions
451
452 const Type* resultType = instrNode->getInstruction()->getType();
453
454 if (resultType->isIntegral())
455 {
456 opCode = MULX;
457 }
458 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000459 switch(resultType->getPrimitiveID())
460 {
461 case Type::FloatTyID: opCode = FMULS; break;
462 case Type::DoubleTyID: opCode = FMULD; break;
463 default: assert(0 && "Invalid type for MUL instruction"); break;
464 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000465
466 return opCode;
467}
468
469
470static inline MachineInstr*
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000471CreateIntNegInstruction(TargetMachine& target,
472 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000473{
474 MachineInstr* minstr = new MachineInstr(SUB);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000475 minstr->SetMachineOperand(0, target.getRegInfo().getZeroRegNum());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000476 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, vreg);
477 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, vreg);
478 return minstr;
479}
480
481
482static inline MachineInstr*
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000483CreateMulConstInstruction(TargetMachine &target,
484 const InstructionNode* instrNode,
485 MachineInstr*& getMinstr2)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000486{
487 MachineInstr* minstr = NULL;
488 getMinstr2 = NULL;
489 bool needNeg = false;
490
491 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000492 assert(isa<ConstPoolVal>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000493
494 // Cases worth optimizing are:
495 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
496 // (2) Multiply by 2^x for integer types: replace with Shift
497 //
498 const Type* resultType = instrNode->getInstruction()->getType();
499
Vikram S. Adve243dd452001-09-18 13:03:13 +0000500 if (resultType->isIntegral() || resultType->isPointerType())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000501 {
502 unsigned pow;
503 bool isValidConst;
504 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
505 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000506 {
507 bool needNeg = false;
508 if (C < 0)
509 {
510 needNeg = true;
511 C = -C;
512 }
513
514 if (C == 0 || C == 1)
515 {
516 minstr = new MachineInstr(ADD);
517
518 if (C == 0)
519 minstr->SetMachineOperand(0,
520 target.getRegInfo().getZeroRegNum());
521 else
522 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
523 instrNode->leftChild()->getValue());
524 minstr->SetMachineOperand(1,target.getRegInfo().getZeroRegNum());
525 }
526 else if (IsPowerOf2(C, pow))
527 {
528 minstr = new MachineInstr((resultType == Type::LongTy)
529 ? SLLX : SLL);
530 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
531 instrNode->leftChild()->getValue());
532 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
533 pow);
534 }
535
536 if (minstr && needNeg)
537 { // insert <reg = SUB 0, reg> after the instr to flip the sign
538 getMinstr2 = CreateIntNegInstruction(target,
539 instrNode->getValue());
540 }
541 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000542 }
543 else
544 {
545 if (resultType == Type::FloatTy ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000546 resultType == Type::DoubleTy)
547 {
548 bool isValidConst;
549 double dval = ((ConstPoolFP*) constOp)->getValue();
550
551 if (isValidConst)
552 {
553 if (dval == 0)
554 {
555 minstr = new MachineInstr((resultType == Type::FloatTy)
556 ? FITOS : FITOD);
557 minstr->SetMachineOperand(0,
558 target.getRegInfo().getZeroRegNum());
559 }
560 else if (fabs(dval) == 1)
561 {
562 bool needNeg = (dval < 0);
563
564 MachineOpCode opCode = needNeg
565 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
566 : (resultType == Type::FloatTy? FMOVS : FMOVD);
567
568 minstr = new MachineInstr(opCode);
569 minstr->SetMachineOperand(0,
570 MachineOperand::MO_VirtualRegister,
571 instrNode->leftChild()->getValue());
572 }
573 }
574 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000575 }
576
577 if (minstr != NULL)
578 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000579 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000580
581 return minstr;
582}
583
584
585static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000586ChooseDivInstruction(TargetMachine &target,
587 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000588{
589 MachineOpCode opCode = INVALID_OPCODE;
590
591 const Type* resultType = instrNode->getInstruction()->getType();
592
593 if (resultType->isIntegral())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000594 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000595 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000596 switch(resultType->getPrimitiveID())
597 {
598 case Type::FloatTyID: opCode = FDIVS; break;
599 case Type::DoubleTyID: opCode = FDIVD; break;
600 default: assert(0 && "Invalid type for DIV instruction"); break;
601 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000602
603 return opCode;
604}
605
606
607static inline MachineInstr*
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000608CreateDivConstInstruction(TargetMachine &target,
609 const InstructionNode* instrNode,
610 MachineInstr*& getMinstr2)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000611{
612 MachineInstr* minstr = NULL;
613 getMinstr2 = NULL;
614
615 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000616 assert(isa<ConstPoolVal>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000617
618 // Cases worth optimizing are:
619 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
620 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
621 //
622 const Type* resultType = instrNode->getInstruction()->getType();
623
624 if (resultType->isIntegral())
625 {
626 unsigned pow;
627 bool isValidConst;
628 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
629 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000630 {
631 bool needNeg = false;
632 if (C < 0)
633 {
634 needNeg = true;
635 C = -C;
636 }
637
638 if (C == 1)
639 {
640 minstr = new MachineInstr(ADD);
641 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
642 instrNode->leftChild()->getValue());
643 minstr->SetMachineOperand(1,target.getRegInfo().getZeroRegNum());
644 }
645 else if (IsPowerOf2(C, pow))
646 {
647 MachineOpCode opCode= ((resultType->isSigned())
648 ? (resultType==Type::LongTy)? SRAX : SRA
649 : (resultType==Type::LongTy)? SRLX : SRL);
650 minstr = new MachineInstr(opCode);
651 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
652 instrNode->leftChild()->getValue());
653 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
654 pow);
655 }
656
657 if (minstr && needNeg)
658 { // insert <reg = SUB 0, reg> after the instr to flip the sign
659 getMinstr2 = CreateIntNegInstruction(target,
660 instrNode->getValue());
661 }
662 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000663 }
664 else
665 {
666 if (resultType == Type::FloatTy ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000667 resultType == Type::DoubleTy)
668 {
669 bool isValidConst;
670 double dval = ((ConstPoolFP*) constOp)->getValue();
671
672 if (isValidConst && fabs(dval) == 1)
673 {
674 bool needNeg = (dval < 0);
675
676 MachineOpCode opCode = needNeg
677 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
678 : (resultType == Type::FloatTy? FMOVS : FMOVD);
679
680 minstr = new MachineInstr(opCode);
681 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
682 instrNode->leftChild()->getValue());
683 }
684 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000685 }
686
687 if (minstr != NULL)
688 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000689 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000690
691 return minstr;
692}
693
694
Chris Lattner20b1ea02001-09-14 03:47:57 +0000695//------------------------------------------------------------------------
696// Function SetOperandsForMemInstr
697//
698// Choose addressing mode for the given load or store instruction.
699// Use [reg+reg] if it is an indexed reference, and the index offset is
700// not a constant or if it cannot fit in the offset field.
701// Use [reg+offset] in all other cases.
702//
703// This assumes that all array refs are "lowered" to one of these forms:
704// %x = load (subarray*) ptr, constant ; single constant offset
705// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
706// Generally, this should happen via strength reduction + LICM.
707// Also, strength reduction should take care of using the same register for
708// the loop index variable and an array index, when that is profitable.
709//------------------------------------------------------------------------
710
711static void
712SetOperandsForMemInstr(MachineInstr* minstr,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000713 const InstructionNode* vmInstrNode,
714 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000715{
716 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
717
718 // Variables to hold the index vector, ptr value, and offset value.
719 // The major work here is to extract these for all 3 instruction types
720 // and then call the common function SetMemOperands_Internal().
721 //
722 const vector<ConstPoolVal*>* idxVec = & memInst->getIndexVec();
723 vector<ConstPoolVal*>* newIdxVec = NULL;
724 Value* ptrVal;
725 Value* arrayOffsetVal = NULL;
726
727 // Test if a GetElemPtr instruction is being folded into this mem instrn.
728 // If so, it will be in the left child for Load and GetElemPtr,
729 // and in the right child for Store instructions.
730 //
731 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000732 ? vmInstrNode->rightChild()
733 : vmInstrNode->leftChild());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000734
735 if (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
736 ptrChild->getOpLabel() == GetElemPtrIdx)
737 {
738 // There is a GetElemPtr instruction and there may be a chain of
739 // more than one. Use the pointer value of the last one in the chain.
740 // Fold the index vectors from the entire chain and from the mem
741 // instruction into one single index vector.
742 // Finally, we never fold for an array instruction so make that NULL.
743
744 newIdxVec = new vector<ConstPoolVal*>;
745 ptrVal = FoldGetElemChain((InstructionNode*) ptrChild, *newIdxVec);
746
747 newIdxVec->insert(newIdxVec->end(), idxVec->begin(), idxVec->end());
748 idxVec = newIdxVec;
749
750 assert(! ((PointerType*)ptrVal->getType())->getValueType()->isArrayType()
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000751 && "GetElemPtr cannot be folded into array refs in selection");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000752 }
753 else
754 {
755 // There is no GetElemPtr instruction.
756 // Use the pointer value and the index vector from the Mem instruction.
757 // If it is an array reference, get the array offset value.
758 //
759 ptrVal = memInst->getPtrOperand();
760
761 const Type* opType =
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000762 ((const PointerType*) ptrVal->getType())->getValueType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000763 if (opType->isArrayType())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000764 {
765 assert((memInst->getNumOperands()
766 == (unsigned) 1 + memInst->getFirstOffsetIdx())
767 && "Array refs must be lowered before Instruction Selection");
768
769 arrayOffsetVal = memInst->getOperand(memInst->getFirstOffsetIdx());
770 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000771 }
772
773 SetMemOperands_Internal(minstr, vmInstrNode, ptrVal, arrayOffsetVal,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000774 *idxVec, target);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000775
776 if (newIdxVec != NULL)
777 delete newIdxVec;
778}
779
780
781static void
782SetMemOperands_Internal(MachineInstr* minstr,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000783 const InstructionNode* vmInstrNode,
784 Value* ptrVal,
785 Value* arrayOffsetVal,
786 const vector<ConstPoolVal*>& idxVec,
787 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000788{
789 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
790
791 // Initialize so we default to storing the offset in a register.
792 int64_t smallConstOffset;
793 Value* valueForRegOffset = NULL;
794 MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister;
795
796 // Check if there is an index vector and if so, if it translates to
797 // a small enough constant to fit in the immediate-offset field.
798 //
799 if (idxVec.size() > 0)
800 {
801 bool isConstantOffset = false;
802 unsigned offset;
803
804 const PointerType* ptrType = (PointerType*) ptrVal->getType();
805
806 if (ptrType->getValueType()->isStructType())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000807 {
808 // the offset is always constant for structs
809 isConstantOffset = true;
810
811 // Compute the offset value using the index vector
812 offset = target.DataLayout.getIndexedOffset(ptrType, idxVec);
813 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000814 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000815 {
816 // It must be an array ref. Check if the offset is a constant,
817 // and that the indexing has been lowered to a single offset.
818 //
819 assert(ptrType->getValueType()->isArrayType());
820 assert(arrayOffsetVal != NULL
821 && "Expect to be given Value* for array offsets");
822
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000823 if (ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(arrayOffsetVal))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000824 {
825 isConstantOffset = true; // always constant for structs
826 assert(arrayOffsetVal->getType()->isIntegral());
827 offset = (CPV->getType()->isSigned()
828 ? ((ConstPoolSInt*)CPV)->getValue()
829 : (int64_t) ((ConstPoolUInt*)CPV)->getValue());
830 }
831 else
832 {
833 valueForRegOffset = arrayOffsetVal;
834 }
835 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000836
837 if (isConstantOffset)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000838 {
839 // create a virtual register for the constant
840 valueForRegOffset = ConstPoolSInt::get(Type::IntTy, offset);
841 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000842 }
843 else
844 {
845 offsetOpType = MachineOperand::MO_SignExtendedImmed;
846 smallConstOffset = 0;
847 }
848
849 // Operand 0 is value for STORE, ptr for LOAD or GET_ELEMENT_PTR
850 // It is the left child in the instruction tree in all cases.
851 Value* leftVal = vmInstrNode->leftChild()->getValue();
852 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, leftVal);
853
854 // Operand 1 is ptr for STORE, offset for LOAD or GET_ELEMENT_PTR
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000855 // Operand 2 is offset for STORE, result reg for LOAD or GET_ELEMENT_PTR
Chris Lattner20b1ea02001-09-14 03:47:57 +0000856 //
857 unsigned offsetOpNum = (memInst->getOpcode() == Instruction::Store)? 2 : 1;
858 if (offsetOpType == MachineOperand::MO_VirtualRegister)
859 {
860 assert(valueForRegOffset != NULL);
861 minstr->SetMachineOperand(offsetOpNum, offsetOpType, valueForRegOffset);
862 }
863 else
864 minstr->SetMachineOperand(offsetOpNum, offsetOpType, smallConstOffset);
865
866 if (memInst->getOpcode() == Instruction::Store)
867 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, ptrVal);
868 else
869 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000870 vmInstrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000871}
872
873
Chris Lattner20b1ea02001-09-14 03:47:57 +0000874//
875// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +0000876// in place of the use(s) of that instruction in node `parent'.
877// Check both explicit and implicit operands!
Chris Lattner20b1ea02001-09-14 03:47:57 +0000878//
879static void
880ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000881 InstrTreeNode* parent,
882 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000883{
Vikram S. Adve243dd452001-09-18 13:03:13 +0000884 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
885
Chris Lattner20b1ea02001-09-14 03:47:57 +0000886 Instruction* unusedOp = treeNode->getInstruction();
887 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +0000888
889 // The parent itself may be a list node, so find the real parent instruction
890 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
891 {
892 parent = parent->parent();
893 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
894 }
895 InstructionNode* parentInstrNode = (InstructionNode*) parent;
896
897 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000898 MachineCodeForVMInstr& mvec = userInstr->getMachineInstrVec();
899 for (unsigned i=0, N=mvec.size(); i < N; i++)
900 {
901 MachineInstr* minstr = mvec[i];
Vikram S. Advec025fc12001-10-14 23:28:43 +0000902
903 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000904 {
905 const MachineOperand& mop = minstr->getOperand(i);
906 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
907 mop.getVRegValue() == unusedOp)
908 {
909 minstr->SetMachineOperand(i, MachineOperand::MO_VirtualRegister,
910 fwdOp);
911 }
912 }
Vikram S. Advec025fc12001-10-14 23:28:43 +0000913
914 for (unsigned i=0, numOps=minstr->getNumImplicitRefs(); i < numOps; ++i)
915 if (minstr->getImplicitRef(i) == unusedOp)
916 minstr->setImplicitRef(i, fwdOp, minstr->implicitRefIsDefined(i));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000917 }
918}
919
920
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000921void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000922CreateCopyInstructionsByType(const TargetMachine& target,
923 Value* src,
924 Instruction* dest,
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000925 vector<MachineInstr*>& minstrVec)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000926{
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000927 bool loadConstantToReg = false;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000928
929 const Type* resultType = dest->getType();
930
931 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
932 if (opCode == INVALID_OPCODE)
933 {
934 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000935 return;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000936 }
937
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000938 // if `src' is a constant that doesn't fit in the immed field or if it is
939 // a global variable (i.e., a constant address), generate a load
940 // instruction instead of an add
941 //
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000942 if (isa<ConstPoolVal>(src))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000943 {
944 unsigned int machineRegNum;
945 int64_t immedValue;
946 MachineOperand::MachineOperandType opType =
947 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
948 machineRegNum, immedValue);
949
950 if (opType == MachineOperand::MO_VirtualRegister)
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000951 loadConstantToReg = true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000952 }
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000953 else if (isa<GlobalValue>(src))
954 loadConstantToReg = true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000955
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000956 if (loadConstantToReg)
957 { // `src' is constant and cannot fit in immed field for the ADD
958 // Insert instructions to "load" the constant into a register
959 vector<TmpInstruction*> tempVec;
960 target.getInstrInfo().CreateCodeToLoadConst(src,dest,minstrVec,tempVec);
961 for (unsigned i=0; i < tempVec.size(); i++)
962 dest->getMachineInstrVec().addTempValue(tempVec[i]);
963 }
964 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000965 { // Create the appropriate add instruction.
966 // Make `src' the second operand, in case it is a constant
967 // Use (unsigned long) 0 for a NULL pointer value.
968 //
969 const Type* nullValueType =
970 (resultType->getPrimitiveID() == Type::PointerTyID)? Type::ULongTy
971 : resultType;
Vikram S. Adve7fe27872001-10-18 00:26:20 +0000972 MachineInstr* minstr = new MachineInstr(opCode);
973 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
974 ConstPoolVal::getNullConstant(nullValueType));
975 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, src);
976 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, dest);
977 minstrVec.push_back(minstr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000978 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000979}
980
981
Vikram S. Advefb361122001-10-22 13:36:31 +0000982//******************* Externally Visible Functions *************************/
983
984
985//------------------------------------------------------------------------
986// External Function: GetInstructionsForProlog
987// External Function: GetInstructionsForEpilog
988//
989// Purpose:
990// Create prolog and epilog code for procedure entry and exit
991//------------------------------------------------------------------------
992
993extern unsigned
994GetInstructionsForProlog(BasicBlock* entryBB,
995 TargetMachine &target,
996 MachineInstr** mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000997{
Vikram S. Advefb361122001-10-22 13:36:31 +0000998 int64_t s0=0; // used to avoid overloading ambiguity below
Chris Lattner20b1ea02001-09-14 03:47:57 +0000999
Vikram S. Advefb361122001-10-22 13:36:31 +00001000 // The second operand is the stack size. If it does not fit in the
1001 // immediate field, we either have to find an unused register in the
1002 // caller's window or move some elements to the dynamically allocated
1003 // area of the stack frame (just above save area and method args).
1004 Method* method = entryBB->getParent();
1005 MachineCodeForMethod& mcodeInfo = method->getMachineCode();
1006 unsigned int staticStackSize = mcodeInfo.getStaticStackSize();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001007
Vikram S. Advefb361122001-10-22 13:36:31 +00001008 assert(target.getInstrInfo().constantFitsInImmedField(SAVE, staticStackSize)
1009 && "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 +00001010
Vikram S. Advefb361122001-10-22 13:36:31 +00001011 mvec[0] = new MachineInstr(SAVE);
1012 mvec[0]->SetMachineOperand(0, target.getRegInfo().getStackPointer());
1013 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1014 staticStackSize);
1015 mvec[0]->SetMachineOperand(2, target.getRegInfo().getStackPointer());
1016
1017 return 1;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001018}
1019
1020
Vikram S. Advefb361122001-10-22 13:36:31 +00001021extern unsigned
1022GetInstructionsForEpilog(BasicBlock* anExitBB,
1023 TargetMachine &target,
1024 MachineInstr** mvec)
1025{
1026 int64_t s0=0; // used to avoid overloading ambiguity below
1027
1028 mvec[0] = new MachineInstr(RESTORE);
1029 mvec[0]->SetMachineOperand(0, target.getRegInfo().getZeroRegNum());
1030 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed, s0);
1031 mvec[0]->SetMachineOperand(2, target.getRegInfo().getZeroRegNum());
1032
1033 return 1;
1034}
1035
1036
1037//------------------------------------------------------------------------
1038// External Function: ThisIsAChainRule
1039//
1040// Purpose:
1041// Check if a given BURG rule is a chain rule.
1042//------------------------------------------------------------------------
1043
1044extern bool
1045ThisIsAChainRule(int eruleno)
1046{
1047 switch(eruleno)
1048 {
1049 case 111: // stmt: reg
1050 case 113: // stmt: bool
1051 case 123:
1052 case 124:
1053 case 125:
1054 case 126:
1055 case 127:
1056 case 128:
1057 case 129:
1058 case 130:
1059 case 131:
1060 case 132:
1061 case 133:
1062 case 155:
1063 case 221:
1064 case 222:
1065 case 241:
1066 case 242:
1067 case 243:
1068 case 244:
1069 return true; break;
1070
1071 default:
1072 return false; break;
1073 }
1074}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001075
1076
1077//------------------------------------------------------------------------
1078// External Function: GetInstructionsByRule
1079//
1080// Purpose:
1081// Choose machine instructions for the SPARC according to the
1082// patterns chosen by the BURG-generated parser.
1083//------------------------------------------------------------------------
1084
1085unsigned
1086GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001087 int ruleForNode,
1088 short* nts,
Vikram S. Advefb361122001-10-22 13:36:31 +00001089 TargetMachine &tgt,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001090 MachineInstr** mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001091{
1092 int numInstr = 1; // initialize for common case
1093 bool checkCast = false; // initialize here to use fall-through
1094 Value *leftVal, *rightVal;
1095 const Type* opType;
1096 int nextRule;
1097 int forwardOperandNum = -1;
Vikram S. Adve8557b222001-10-10 20:56:33 +00001098 int64_t s0=0, s8=8; // variables holding constants to avoid
1099 uint64_t u0=0; // overloading ambiguities below
Chris Lattner20b1ea02001-09-14 03:47:57 +00001100
Vikram S. Advefb361122001-10-22 13:36:31 +00001101 UltraSparc& target = (UltraSparc&) tgt;
1102
1103 for (unsigned i=0; i < MAX_INSTR_PER_VMINSTR; i++)
1104 mvec[i] = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001105
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001106 //
1107 // Let's check for chain rules outside the switch so that we don't have
1108 // to duplicate the list of chain rule production numbers here again
1109 //
1110 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001111 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001112 // Chain rules have a single nonterminal on the RHS.
1113 // Get the rule that matches the RHS non-terminal and use that instead.
1114 //
1115 assert(nts[0] && ! nts[1]
1116 && "A chain rule should have only one RHS non-terminal!");
1117 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1118 nts = burm_nts[nextRule];
1119 numInstr = GetInstructionsByRule(subtreeRoot, nextRule, nts,target,mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001120 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001121 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001122 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001123 switch(ruleForNode) {
1124 case 1: // stmt: Ret
1125 case 2: // stmt: RetValue(reg)
1126 // NOTE: Prepass of register allocation is responsible
1127 // for moving return value to appropriate register.
1128 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001129 // Mark the return value register as an implicit ref of
1130 // the machine instruction.
Vikram S. Advefb361122001-10-22 13:36:31 +00001131 { // Finally put a NOP in the delay slot.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001132 ReturnInst* returnInstr = (ReturnInst*) subtreeRoot->getInstruction();
1133 assert(returnInstr->getOpcode() == Instruction::Ret);
Vikram S. Advefb361122001-10-22 13:36:31 +00001134 Method* method = returnInstr->getParent()->getParent();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001135
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001136 Instruction* returnReg = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001137 returnInstr, NULL);
1138 returnInstr->getMachineInstrVec().addTempValue(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001139
1140 mvec[0] = new MachineInstr(JMPLRET);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001141 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1142 returnReg);
Vikram S. Adve8557b222001-10-10 20:56:33 +00001143 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,s8);
Vikram S. Advefb361122001-10-22 13:36:31 +00001144 mvec[0]->SetMachineOperand(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001145
Vikram S. Advea995e602001-10-11 04:23:19 +00001146 if (returnInstr->getReturnValue() != NULL)
1147 mvec[0]->addImplicitRef(returnInstr->getReturnValue());
1148
Vikram S. Advefb361122001-10-22 13:36:31 +00001149 unsigned n = numInstr++; // delay slot
1150 mvec[n] = new MachineInstr(NOP);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001151
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001152 break;
1153 }
1154
1155 case 3: // stmt: Store(reg,reg)
1156 case 4: // stmt: Store(reg,ptrreg)
1157 mvec[0] = new MachineInstr(
1158 ChooseStoreInstruction(
1159 subtreeRoot->leftChild()->getValue()->getType()));
1160 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1161 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001162
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001163 case 5: // stmt: BrUncond
1164 mvec[0] = new MachineInstr(BA);
1165 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1166 (Value*)NULL);
1167 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1168 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1169
1170 // delay slot
1171 mvec[numInstr++] = new MachineInstr(NOP);
1172 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001173
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001174 case 206: // stmt: BrCond(setCCconst)
1175 // setCCconst => boolean was computed with `%b = setCC type reg1 const'
1176 // If the constant is ZERO, we can use the branch-on-integer-register
1177 // instructions and avoid the SUBcc instruction entirely.
1178 // Otherwise this is just the same as case 5, so just fall through.
1179 {
1180 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1181 assert(constNode &&
1182 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
1183 ConstPoolVal* constVal = (ConstPoolVal*) constNode->getValue();
1184 bool isValidConst;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001185
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001186 if ((constVal->getType()->isIntegral()
1187 || constVal->getType()->isPointerType())
1188 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1189 && isValidConst)
1190 {
1191 // That constant is a zero after all...
1192 // Use the left child of setCC as the first argument!
1193 mvec[0] = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1194 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1195 subtreeRoot->leftChild()->leftChild()->getValue());
1196 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1197 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
Chris Lattner20b1ea02001-09-14 03:47:57 +00001198
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001199 // delay slot
1200 mvec[numInstr++] = new MachineInstr(NOP);
1201
1202 // false branch
1203 int n = numInstr++;
1204 mvec[n] = new MachineInstr(BA);
1205 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1206 (Value*) NULL);
1207 mvec[n]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1208 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
1209
1210 // delay slot
1211 mvec[numInstr++] = new MachineInstr(NOP);
1212
1213 break;
1214 }
1215 // ELSE FALL THROUGH
1216 }
1217
1218 case 6: // stmt: BrCond(bool)
1219 // bool => boolean was computed with some boolean operator
1220 // (SetCC, Not, ...). We need to check whether the type was a FP,
1221 // signed int or unsigned int, and check the branching condition in
1222 // order to choose the branch to use.
1223 //
1224 {
1225 bool isFPBranch;
1226 mvec[0] = new MachineInstr(ChooseBccInstruction(subtreeRoot,
1227 isFPBranch));
1228 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1229 subtreeRoot->leftChild()->getValue());
1230 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1231 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1232
1233 // 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,
1242 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
1243
1244 // delay slot
1245 mvec[numInstr++] = new MachineInstr(NOP);
1246 break;
1247 }
1248
1249 case 208: // stmt: BrCond(boolconst)
1250 {
1251 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnercfe26c92001-10-01 18:26:53 +00001252 ConstPoolVal* constVal =
1253 cast<ConstPoolVal>(subtreeRoot->leftChild()->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001254 unsigned dest = ((ConstPoolBool*) constVal)->getValue()? 0 : 1;
1255
1256 mvec[0] = new MachineInstr(BA);
1257 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1258 (Value*) NULL);
1259 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1260 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(dest));
1261
1262 // delay slot
1263 mvec[numInstr++] = new MachineInstr(NOP);
1264 break;
1265 }
1266
1267 case 8: // stmt: BrCond(boolreg)
1268 // boolreg => boolean is stored in an existing register.
1269 // Just use the branch-on-integer-register instruction!
1270 //
1271 {
1272 mvec[0] = new MachineInstr(BRNZ);
1273 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1274 subtreeRoot->leftChild()->getValue());
1275 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1276 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1277
1278 // delay slot
1279 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1280
1281 // false branch
1282 int n = numInstr++;
1283 mvec[n] = new MachineInstr(BA);
1284 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1285 (Value*) NULL);
1286 mvec[n]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1287 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
1288
1289 // delay slot
1290 mvec[numInstr++] = new MachineInstr(NOP);
1291 break;
1292 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001293
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001294 case 9: // stmt: Switch(reg)
1295 assert(0 && "*** SWITCH instruction is not implemented yet.");
1296 numInstr = 0;
1297 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001298
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001299 case 10: // reg: VRegList(reg, reg)
1300 assert(0 && "VRegList should never be the topmost non-chain rule");
1301 break;
1302
1303 case 21: // reg: Not(reg): Implemented as reg = reg XOR-NOT 0
1304 mvec[0] = new MachineInstr(XNOR);
1305 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1306 subtreeRoot->leftChild()->getValue());
1307 mvec[0]->SetMachineOperand(1, target.getRegInfo().getZeroRegNum());
1308 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1309 subtreeRoot->getValue());
1310 break;
1311
1312 case 322: // reg: ToBoolTy(bool):
1313 case 22: // reg: ToBoolTy(reg):
1314 opType = subtreeRoot->leftChild()->getValue()->getType();
1315 assert(opType->isIntegral() || opType == Type::BoolTy);
1316 numInstr = 0;
1317 forwardOperandNum = 0;
1318 break;
1319
1320 case 23: // reg: ToUByteTy(reg)
1321 case 25: // reg: ToUShortTy(reg)
1322 case 27: // reg: ToUIntTy(reg)
1323 case 29: // reg: ToULongTy(reg)
1324 opType = subtreeRoot->leftChild()->getValue()->getType();
1325 assert(opType->isIntegral() ||
1326 opType->isPointerType() ||
1327 opType == Type::BoolTy && "Cast is illegal for other types");
1328 numInstr = 0;
1329 forwardOperandNum = 0;
1330 break;
1331
1332 case 24: // reg: ToSByteTy(reg)
1333 case 26: // reg: ToShortTy(reg)
1334 case 28: // reg: ToIntTy(reg)
1335 case 30: // reg: ToLongTy(reg)
1336 opType = subtreeRoot->leftChild()->getValue()->getType();
1337 if (opType->isIntegral() || opType == Type::BoolTy)
1338 {
1339 numInstr = 0;
1340 forwardOperandNum = 0;
1341 }
1342 else
1343 {
1344 mvec[0] = new MachineInstr(ChooseConvertToIntInstr(subtreeRoot,
1345 opType));
1346 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
1347 }
1348 break;
1349
1350 case 31: // reg: ToFloatTy(reg):
1351 case 32: // reg: ToDoubleTy(reg):
1352 case 232: // reg: ToDoubleTy(Constant):
1353
1354 // If this instruction has a parent (a user) in the tree
1355 // and the user is translated as an FsMULd instruction,
1356 // then the cast is unnecessary. So check that first.
1357 // In the future, we'll want to do the same for the FdMULq instruction,
1358 // so do the check here instead of only for ToFloatTy(reg).
1359 //
1360 if (subtreeRoot->parent() != NULL &&
1361 ((InstructionNode*) subtreeRoot->parent())->getInstruction()->getMachineInstrVec()[0]->getOpCode() == FSMULD)
1362 {
1363 numInstr = 0;
1364 forwardOperandNum = 0;
1365 }
1366 else
1367 {
1368 opType = subtreeRoot->leftChild()->getValue()->getType();
1369 MachineOpCode opCode=ChooseConvertToFloatInstr(subtreeRoot,opType);
1370 if (opCode == INVALID_OPCODE) // no conversion needed
1371 {
1372 numInstr = 0;
1373 forwardOperandNum = 0;
1374 }
1375 else
1376 {
1377 mvec[0] = new MachineInstr(opCode);
1378 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
1379 }
1380 }
1381 break;
1382
1383 case 19: // reg: ToArrayTy(reg):
1384 case 20: // reg: ToPointerTy(reg):
1385 numInstr = 0;
1386 forwardOperandNum = 0;
1387 break;
1388
1389 case 233: // reg: Add(reg, Constant)
1390 mvec[0] = CreateAddConstInstruction(subtreeRoot);
1391 if (mvec[0] != NULL)
1392 break;
1393 // ELSE FALL THROUGH
1394
1395 case 33: // reg: Add(reg, reg)
1396 mvec[0] = new MachineInstr(ChooseAddInstruction(subtreeRoot));
1397 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1398 break;
1399
1400 case 234: // reg: Sub(reg, Constant)
1401 mvec[0] = CreateSubConstInstruction(subtreeRoot);
1402 if (mvec[0] != NULL)
1403 break;
1404 // ELSE FALL THROUGH
1405
1406 case 34: // reg: Sub(reg, reg)
1407 mvec[0] = new MachineInstr(ChooseSubInstruction(subtreeRoot));
1408 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1409 break;
1410
1411 case 135: // reg: Mul(todouble, todouble)
1412 checkCast = true;
1413 // FALL THROUGH
1414
1415 case 35: // reg: Mul(reg, reg)
1416 mvec[0] =new MachineInstr(ChooseMulInstruction(subtreeRoot,checkCast));
1417 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1418 break;
1419
1420 case 335: // reg: Mul(todouble, todoubleConst)
1421 checkCast = true;
1422 // FALL THROUGH
1423
1424 case 235: // reg: Mul(reg, Constant)
1425 mvec[0] = CreateMulConstInstruction(target, subtreeRoot, mvec[1]);
1426 if (mvec[0] == NULL)
1427 {
1428 mvec[0] = new MachineInstr(ChooseMulInstruction(subtreeRoot,
1429 checkCast));
1430 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1431 }
1432 else
1433 if (mvec[1] != NULL)
1434 ++numInstr;
1435 break;
1436
1437 case 236: // reg: Div(reg, Constant)
1438 mvec[0] = CreateDivConstInstruction(target, subtreeRoot, mvec[1]);
1439 if (mvec[0] != NULL)
1440 {
1441 if (mvec[1] != NULL)
1442 ++numInstr;
1443 }
1444 else
1445 // ELSE FALL THROUGH
1446
1447 case 36: // reg: Div(reg, reg)
1448 mvec[0] = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1449 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1450 break;
1451
1452 case 37: // reg: Rem(reg, reg)
1453 case 237: // reg: Rem(reg, Constant)
1454 assert(0 && "REM instruction unimplemented for the SPARC.");
1455 break;
1456
1457 case 38: // reg: And(reg, reg)
1458 case 238: // reg: And(reg, Constant)
1459 mvec[0] = new MachineInstr(AND);
1460 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1461 break;
1462
1463 case 138: // reg: And(reg, not)
1464 mvec[0] = new MachineInstr(ANDN);
1465 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1466 break;
1467
1468 case 39: // reg: Or(reg, reg)
1469 case 239: // reg: Or(reg, Constant)
1470 mvec[0] = new MachineInstr(ORN);
1471 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1472 break;
1473
1474 case 139: // reg: Or(reg, not)
1475 mvec[0] = new MachineInstr(ORN);
1476 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1477 break;
1478
1479 case 40: // reg: Xor(reg, reg)
1480 case 240: // reg: Xor(reg, Constant)
1481 mvec[0] = new MachineInstr(XOR);
1482 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1483 break;
1484
1485 case 140: // reg: Xor(reg, not)
1486 mvec[0] = new MachineInstr(XNOR);
1487 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1488 break;
1489
1490 case 41: // boolconst: SetCC(reg, Constant)
1491 // Check if this is an integer comparison, and
1492 // there is a parent, and the parent decided to use
1493 // a branch-on-integer-register instead of branch-on-condition-code.
1494 // If so, the SUBcc instruction is not required.
1495 // (However, we must still check for constants to be loaded from
1496 // the constant pool so that such a load can be associated with
1497 // this instruction.)
1498 //
1499 // Otherwise this is just the same as case 42, so just fall through.
1500 //
1501 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral() &&
1502 subtreeRoot->parent() != NULL)
1503 {
1504 InstructionNode* parent = (InstructionNode*) subtreeRoot->parent();
1505 assert(parent->getNodeType() == InstrTreeNode::NTInstructionNode);
1506 const vector<MachineInstr*>&
1507 minstrVec = parent->getInstruction()->getMachineInstrVec();
1508 MachineOpCode parentOpCode;
1509 if (parent->getInstruction()->getOpcode() == Instruction::Br &&
1510 (parentOpCode = minstrVec[0]->getOpCode()) >= BRZ &&
1511 parentOpCode <= BRGEZ)
1512 {
1513 numInstr = 0; // don't forward the operand!
1514 break;
1515 }
1516 }
1517 // ELSE FALL THROUGH
1518
1519 case 42: // bool: SetCC(reg, reg):
1520 {
1521 // If result of the SetCC is only used for a single branch, we can
1522 // discard the result. Otherwise, the boolean value must go into
1523 // an integer register.
1524 //
1525 bool keepBoolVal = (subtreeRoot->parent() == NULL ||
1526 ((InstructionNode*) subtreeRoot->parent())
1527 ->getInstruction()->getOpcode() !=Instruction::Br);
1528 bool subValIsBoolVal =
1529 subtreeRoot->getInstruction()->getOpcode() == Instruction::SetNE;
1530 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1531 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1532
1533 bool mustClearReg;
1534 int valueToMove;
1535 MachineOpCode movOpCode;
1536
1537 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral() ||
1538 subtreeRoot->leftChild()->getValue()->getType()->isPointerType())
1539 {
1540 // Integer condition: dest. should be %g0 or an integer register.
1541 // If result must be saved but condition is not SetEQ then we need
1542 // a separate instruction to compute the bool result, so discard
1543 // result of SUBcc instruction anyway.
1544 //
1545 mvec[0] = new MachineInstr(SUBcc);
1546 Set3OperandsFromInstr(mvec[0], subtreeRoot, target, ! keepSubVal);
1547
1548 // mark the 4th operand as being a CC register, and a "result"
1549 mvec[0]->SetMachineOperand(3, MachineOperand::MO_CCRegister,
1550 subtreeRoot->getValue(),/*def*/true);
1551
1552 if (computeBoolVal)
1553 { // recompute bool using the integer condition codes
1554 movOpCode =
1555 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1556 }
1557 }
1558 else
1559 {
1560 // FP condition: dest of FCMP should be some FCCn register
1561 mvec[0] = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1562 mvec[0]->SetMachineOperand(0,MachineOperand::MO_CCRegister,
1563 subtreeRoot->getValue());
1564 mvec[0]->SetMachineOperand(1,MachineOperand::MO_VirtualRegister,
1565 subtreeRoot->leftChild()->getValue());
1566 mvec[0]->SetMachineOperand(2,MachineOperand::MO_VirtualRegister,
1567 subtreeRoot->rightChild()->getValue());
1568
1569 if (computeBoolVal)
1570 {// recompute bool using the FP condition codes
1571 mustClearReg = true;
1572 valueToMove = 1;
1573 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1574 }
1575 }
1576
1577 if (computeBoolVal)
1578 {
1579 if (mustClearReg)
1580 {// Unconditionally set register to 0
1581 int n = numInstr++;
1582 mvec[n] = new MachineInstr(SETHI);
1583 mvec[n]->SetMachineOperand(0,MachineOperand::MO_UnextendedImmed,
1584 s0);
1585 mvec[n]->SetMachineOperand(1,MachineOperand::MO_VirtualRegister,
1586 subtreeRoot->getValue());
1587 }
1588
1589 // Now conditionally move `valueToMove' (0 or 1) into the register
1590 int n = numInstr++;
1591 mvec[n] = new MachineInstr(movOpCode);
1592 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1593 subtreeRoot->getValue());
1594 mvec[n]->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
1595 valueToMove);
1596 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1597 subtreeRoot->getValue());
1598 }
1599 break;
1600 }
1601
1602 case 43: // boolreg: VReg
1603 case 44: // boolreg: Constant
1604 numInstr = 0;
1605 break;
1606
1607 case 51: // reg: Load(reg)
1608 case 52: // reg: Load(ptrreg)
1609 case 53: // reg: LoadIdx(reg,reg)
1610 case 54: // reg: LoadIdx(ptrreg,reg)
1611 mvec[0] = new MachineInstr(ChooseLoadInstruction(
1612 subtreeRoot->getValue()->getType()));
1613 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1614 break;
1615
1616 case 55: // reg: GetElemPtr(reg)
1617 case 56: // reg: GetElemPtrIdx(reg,reg)
1618 if (subtreeRoot->parent() != NULL)
1619 {
1620 // Check if the parent was an array access.
1621 // If so, we still need to generate this instruction.
1622 MemAccessInst* memInst = (MemAccessInst*)
1623 subtreeRoot->getInstruction();
1624 const PointerType* ptrType =
1625 (const PointerType*) memInst->getPtrOperand()->getType();
1626 if (! ptrType->getValueType()->isArrayType())
1627 {// we don't need a separate instr
1628 numInstr = 0; // don't forward operand!
1629 break;
1630 }
1631 }
1632 // else in all other cases we need to a separate ADD instruction
1633 mvec[0] = new MachineInstr(ADD);
1634 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1635 break;
1636
Vikram S. Advefb361122001-10-22 13:36:31 +00001637 case 57: // reg: Alloca: Implement as 1 instruction:
1638 { // add %fp, offsetFromFP -> result
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001639 Instruction* instr = subtreeRoot->getInstruction();
1640 const PointerType* instrType = (const PointerType*) instr->getType();
1641 assert(instrType->isPointerType());
1642 int tsize = (int)
1643 target.findOptimalStorageSize(instrType->getValueType());
1644 assert(tsize != 0 && "Just to check when this can happen");
1645
Vikram S. Advefb361122001-10-22 13:36:31 +00001646 Method* method = instr->getParent()->getParent();
1647 MachineCodeForMethod& mcode = method->getMachineCode();
1648 int offsetFromFP =
1649 target.getFrameInfo().getFirstAutomaticVarOffsetFromFP(method)
1650 - (tsize + mcode.getAutomaticVarsSize());
1651
1652 mcode.putLocalVarAtOffsetFromFP(instr, offsetFromFP, tsize);
1653
1654 // Create a temporary Value to hold the constant offset.
1655 // This is needed because it may not fit in the immediate field.
1656 ConstPoolSInt* offsetVal=ConstPoolSInt::get(Type::IntTy, offsetFromFP);
1657
1658 // Instruction 1: add %fp, offsetFromFP -> result
1659 mvec[0] = new MachineInstr(ADD);
1660 mvec[0]->SetMachineOperand(0, target.getRegInfo().getFramePointer());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001661 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
Vikram S. Advefb361122001-10-22 13:36:31 +00001662 offsetVal);
1663 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001664 instr);
1665 break;
1666 }
1667
1668 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1669 // mul num, typeSz -> tmp
1670 // sub %sp, tmp -> %sp
Vikram S. Advefb361122001-10-22 13:36:31 +00001671 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001672 Instruction* instr = subtreeRoot->getInstruction();
1673 const PointerType* instrType = (const PointerType*) instr->getType();
1674 assert(instrType->isPointerType() &&
1675 instrType->getValueType()->isArrayType());
1676 const Type* eltType =
1677 ((ArrayType*) instrType->getValueType())->getElementType();
1678 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advefb361122001-10-22 13:36:31 +00001679
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001680 assert(tsize != 0 && "Just to check when this can happen");
Vikram S. Advefb361122001-10-22 13:36:31 +00001681
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001682 // Create a temporary Value to hold the constant type-size
Vikram S. Advefb361122001-10-22 13:36:31 +00001683 ConstPoolSInt* tsizeVal = ConstPoolSInt::get(Type::IntTy, tsize);
1684
1685 // Create a temporary Value to hold the constant offset from SP
1686 Method* method = instr->getParent()->getParent();
1687 MachineCodeForMethod& mcode = method->getMachineCode();
1688 int frameSizeBelowDynamicArea =
1689 target.getFrameInfo().getFrameSizeBelowDynamicArea(method);
1690 ConstPoolSInt* lowerAreaSizeVal = ConstPoolSInt::get(Type::IntTy,
1691 frameSizeBelowDynamicArea);
1692 cerr << "***" << endl
1693 << "*** Variable-size ALLOCA operation needs more work:" << endl
1694 << "*** We have to precompute the size of "
1695 << " optional arguments in the stack frame" << endl
1696 << "***" << endl;
1697 assert(0 && "SEE MESSAGE ABOVE");
1698
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001699 // Create a temporary value to hold `tmp'
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001700 Instruction* tmpInstr = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001701 subtreeRoot->leftChild()->getValue(),
1702 NULL /*could insert tsize here*/);
1703 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(tmpInstr);
1704
1705 // Instruction 1: mul numElements, typeSize -> tmp
1706 mvec[0] = new MachineInstr(MULX);
1707 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefb361122001-10-22 13:36:31 +00001708 subtreeRoot->leftChild()->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001709 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
Vikram S. Advefb361122001-10-22 13:36:31 +00001710 tsizeVal);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001711 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1712 tmpInstr);
Vikram S. Advefb361122001-10-22 13:36:31 +00001713
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001714 // Instruction 2: sub %sp, tmp -> %sp
1715 numInstr++;
1716 mvec[1] = new MachineInstr(SUB);
Vikram S. Advefb361122001-10-22 13:36:31 +00001717 mvec[1]->SetMachineOperand(0, target.getRegInfo().getStackPointer());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001718 mvec[1]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1719 tmpInstr);
Vikram S. Advefb361122001-10-22 13:36:31 +00001720 mvec[1]->SetMachineOperand(2, target.getRegInfo().getStackPointer());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001721
Vikram S. Advefb361122001-10-22 13:36:31 +00001722 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001723 numInstr++;
1724 mvec[2] = new MachineInstr(ADD);
Vikram S. Advefb361122001-10-22 13:36:31 +00001725 mvec[2]->SetMachineOperand(0, target.getRegInfo().getStackPointer());
1726 mvec[2]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1727 lowerAreaSizeVal);
1728 mvec[2]->SetMachineOperand(2,MachineOperand::MO_VirtualRegister,instr);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001729 break;
1730 }
1731
1732 case 61: // reg: Call
Vikram S. Advefb361122001-10-22 13:36:31 +00001733 // Generate a call-indirect (i.e., jmpl) for now to expose
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001734 // the potential need for registers. If an absolute address
1735 // is available, replace this with a CALL instruction.
1736 // Mark both the indirection register and the return-address
1737 // register as hidden virtual registers.
Vikram S. Advea995e602001-10-11 04:23:19 +00001738 // Also, mark the operands of the Call and return value (if
1739 // any) as implicit operands of the CALL machine instruction.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001740 {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001741 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00001742 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001743
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001744 Instruction* retAddrReg = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001745 callInstr, NULL);
Vikram S. Adve8557b222001-10-10 20:56:33 +00001746
Vikram S. Advea995e602001-10-11 04:23:19 +00001747 // Note temporary values in the machineInstrVec for the VM instr.
Vikram S. Adve8557b222001-10-10 20:56:33 +00001748 //
1749 // WARNING: Operands 0..N-1 must go in slots 0..N-1 of implicitUses.
1750 // The result value must go in slot N. This is assumed
1751 // in register allocation.
1752 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001753 callInstr->getMachineInstrVec().addTempValue(retAddrReg);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001754
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001755
1756 // Generate the machine instruction and its operands.
1757 // Use CALL for direct function calls; this optimistically assumes
1758 // the PC-relative address fits in the CALL address field (22 bits).
1759 // Use JMPL for indirect calls.
1760 //
1761 if (callee->getValueType() == Value::MethodVal)
1762 { // direct function call
1763 mvec[0] = new MachineInstr(CALL);
1764 mvec[0]->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp,
1765 callee);
1766 }
1767 else
1768 { // indirect function call
Vikram S. Advefb361122001-10-22 13:36:31 +00001769 mvec[0] = new MachineInstr(JMPLCALL);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001770 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1771 callee);
1772 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1773 (int64_t) 0);
1774 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1775 retAddrReg);
1776 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001777
Vikram S. Advea995e602001-10-11 04:23:19 +00001778 // Add the call operands and return value as implicit refs
1779 for (unsigned i=0, N=callInstr->getNumOperands(); i < N; ++i)
1780 if (callInstr->getOperand(i) != callee)
1781 mvec[0]->addImplicitRef(callInstr->getOperand(i));
1782
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001783 if (callInstr->getType() != Type::VoidTy)
Vikram S. Advea995e602001-10-11 04:23:19 +00001784 mvec[0]->addImplicitRef(callInstr, /*isDef*/ true);
1785
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001786 // For the CALL instruction, the ret. addr. reg. is also implicit
1787 if (callee->getValueType() == Value::MethodVal)
1788 mvec[0]->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001789
1790 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1791 break;
1792 }
1793
1794 case 62: // reg: Shl(reg, reg)
1795 opType = subtreeRoot->leftChild()->getValue()->getType();
1796 assert(opType->isIntegral()
1797 || opType == Type::BoolTy
1798 || opType->isPointerType()&& "Shl unsupported for other types");
1799 mvec[0] = new MachineInstr((opType == Type::LongTy)? SLLX : SLL);
1800 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1801 break;
1802
1803 case 63: // reg: Shr(reg, reg)
1804 opType = subtreeRoot->leftChild()->getValue()->getType();
1805 assert(opType->isIntegral()
1806 || opType == Type::BoolTy
1807 || opType->isPointerType() &&"Shr unsupported for other types");
1808 mvec[0] = new MachineInstr((opType->isSigned()
1809 ? ((opType == Type::LongTy)? SRAX : SRA)
1810 : ((opType == Type::LongTy)? SRLX : SRL)));
1811 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1812 break;
1813
1814 case 64: // reg: Phi(reg,reg)
1815 { // This instruction has variable #operands, so resultPos is 0.
1816 Instruction* phi = subtreeRoot->getInstruction();
1817 mvec[0] = new MachineInstr(PHI, 1 + phi->getNumOperands());
1818 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1819 subtreeRoot->getValue());
1820 for (unsigned i=0, N=phi->getNumOperands(); i < N; i++)
1821 mvec[0]->SetMachineOperand(i+1, MachineOperand::MO_VirtualRegister,
1822 phi->getOperand(i));
1823 break;
1824 }
1825 case 71: // reg: VReg
1826 case 72: // reg: Constant
1827 numInstr = 0; // don't forward the value
1828 break;
1829
1830 default:
1831 assert(0 && "Unrecognized BURG rule");
1832 numInstr = 0;
1833 break;
1834 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001835 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001836
1837 if (forwardOperandNum >= 0)
1838 { // We did not generate a machine instruction but need to use operand.
1839 // If user is in the same tree, replace Value in its machine operand.
1840 // If not, insert a copy instruction which should get coalesced away
1841 // by register allocation.
1842 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001843 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001844 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001845 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001846 vector<MachineInstr*> minstrVec;
1847 CreateCopyInstructionsByType(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001848 subtreeRoot->getInstruction()->getOperand(forwardOperandNum),
Vikram S. Adve7fe27872001-10-18 00:26:20 +00001849 subtreeRoot->getInstruction(), minstrVec);
1850 assert(minstrVec.size() > 0);
1851 for (unsigned i=0; i < minstrVec.size(); ++i)
1852 mvec[numInstr++] = minstrVec[i];
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001853 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001854 }
1855
Chris Lattner20b1ea02001-09-14 03:47:57 +00001856 return numInstr;
1857}
1858
1859