blob: 088d089d80c360e356401f8da9227709bc0ae0e1 [file] [log] [blame]
Vikram S. Advea21cf202001-07-21 12:42:19 +00001// $Id$
2//***************************************************************************
3// File:
4// SparcInstrSelection.cpp
5//
6// Purpose:
7//
8// History:
9// 7/02/01 - Vikram Adve - Created
Vikram S. Adved4228a52001-08-28 23:12:57 +000010//**************************************************************************/
Vikram S. Advea21cf202001-07-21 12:42:19 +000011
Vikram S. Adved4228a52001-08-28 23:12:57 +000012#include "llvm/Support/MathExtras.h"
Vikram S. Advea21cf202001-07-21 12:42:19 +000013#include "llvm/Type.h"
14#include "llvm/DerivedTypes.h"
15#include "llvm/SymbolTable.h"
16#include "llvm/Value.h"
17#include "llvm/Instruction.h"
18#include "llvm/InstrTypes.h"
19#include "llvm/iTerminators.h"
20#include "llvm/iMemory.h"
21#include "llvm/iOther.h"
22#include "llvm/BasicBlock.h"
23#include "llvm/Method.h"
24#include "llvm/ConstPoolVals.h"
Chris Lattner7e583cf2001-07-21 20:58:30 +000025#include "llvm/CodeGen/Sparc.h"
26#include "llvm/CodeGen/MachineInstr.h"
27#include "llvm/CodeGen/InstrForest.h"
28#include "llvm/CodeGen/InstrSelection.h"
Vikram S. Advea21cf202001-07-21 12:42:19 +000029
30
31//******************** Internal Data Declarations ************************/
32
33// to be used later
34struct BranchPattern {
35 bool flipCondition; // should the sense of the test be reversed
36 BasicBlock* targetBB; // which basic block to branch to
37 MachineInstr* extraBranch; // if neither branch is fall-through, then this
38 // BA must be inserted after the cond'l one
39};
40
41//************************* Forward Declarations ***************************/
42
43
Vikram S. Adve4f231662001-07-28 04:15:15 +000044static MachineOpCode ChooseBprInstruction (const InstructionNode* instrNode);
Vikram S. Advea21cf202001-07-21 12:42:19 +000045
Vikram S. Adve4f231662001-07-28 04:15:15 +000046static MachineOpCode ChooseBccInstruction (const InstructionNode* instrNode,
47 bool& isFPBranch);
Vikram S. Advea21cf202001-07-21 12:42:19 +000048
Vikram S. Adve4f231662001-07-28 04:15:15 +000049static MachineOpCode ChooseBpccInstruction (const InstructionNode* instrNode,
50 const BinaryOperator* setCCInst);
Vikram S. Advea21cf202001-07-21 12:42:19 +000051
Vikram S. Adve4f231662001-07-28 04:15:15 +000052static MachineOpCode ChooseBFpccInstruction (const InstructionNode* instrNode,
53 const BinaryOperator* setCCInst);
Vikram S. Advea21cf202001-07-21 12:42:19 +000054
Vikram S. Adve4f231662001-07-28 04:15:15 +000055static MachineOpCode ChooseMovFpccInstruction(const InstructionNode*);
56
57static MachineOpCode ChooseMovpccAfterSub (const InstructionNode* instrNode,
58 bool& mustClearReg,
59 int& valueToMove);
60
61static MachineOpCode ChooseConvertToFloatInstr(const InstructionNode*,
Chris Lattner51a9ad92001-07-21 22:57:05 +000062 const Type* opType);
Vikram S. Advea21cf202001-07-21 12:42:19 +000063
Vikram S. Adve4f231662001-07-28 04:15:15 +000064static MachineOpCode ChooseConvertToIntInstr(const InstructionNode* instrNode,
65 const Type* opType);
Vikram S. Advea21cf202001-07-21 12:42:19 +000066
Vikram S. Adve4f231662001-07-28 04:15:15 +000067static MachineOpCode ChooseAddInstruction (const InstructionNode* instrNode);
Vikram S. Advea21cf202001-07-21 12:42:19 +000068
Vikram S. Adve4f231662001-07-28 04:15:15 +000069static MachineOpCode ChooseSubInstruction (const InstructionNode* instrNode);
Vikram S. Advea21cf202001-07-21 12:42:19 +000070
Vikram S. Adve4f231662001-07-28 04:15:15 +000071static MachineOpCode ChooseFcmpInstruction (const InstructionNode* instrNode);
Vikram S. Advea21cf202001-07-21 12:42:19 +000072
Vikram S. Adve4f231662001-07-28 04:15:15 +000073static MachineOpCode ChooseMulInstruction (const InstructionNode* instrNode,
74 bool checkCasts);
Vikram S. Advea21cf202001-07-21 12:42:19 +000075
Vikram S. Adve4f231662001-07-28 04:15:15 +000076static MachineOpCode ChooseDivInstruction (const InstructionNode* instrNode);
Vikram S. Advea21cf202001-07-21 12:42:19 +000077
Vikram S. Adve4f231662001-07-28 04:15:15 +000078static MachineOpCode ChooseLoadInstruction (const Type* resultType);
Vikram S. Advea21cf202001-07-21 12:42:19 +000079
Vikram S. Adve4f231662001-07-28 04:15:15 +000080static MachineOpCode ChooseStoreInstruction (const Type* valueType);
Vikram S. Advea21cf202001-07-21 12:42:19 +000081
Vikram S. Adve4f231662001-07-28 04:15:15 +000082static void SetOperandsForMemInstr(MachineInstr* minstr,
Vikram S. Advea21cf202001-07-21 12:42:19 +000083 const InstructionNode* vmInstrNode,
Vikram S. Adve4f231662001-07-28 04:15:15 +000084 const TargetMachine& target);
Vikram S. Advea21cf202001-07-21 12:42:19 +000085
86static void SetMemOperands_Internal (MachineInstr* minstr,
87 const InstructionNode* vmInstrNode,
88 Value* ptrVal,
89 Value* arrayOffsetVal,
90 const vector<ConstPoolVal*>& idxVec,
Vikram S. Adve4f231662001-07-28 04:15:15 +000091 const TargetMachine& target);
Vikram S. Advea21cf202001-07-21 12:42:19 +000092
93static unsigned FixConstantOperands(const InstructionNode* vmInstrNode,
Vikram S. Adve4f231662001-07-28 04:15:15 +000094 MachineInstr** mvec,
95 unsigned numInstr,
96 TargetMachine& target);
Vikram S. Advea21cf202001-07-21 12:42:19 +000097
Vikram S. Adved4228a52001-08-28 23:12:57 +000098static MachineInstr* MakeLoadConstInstr(Instruction* vmInstr,
99 Value* val,
100 TmpInstruction*& tmpReg,
101 MachineInstr*& getMinstr2);
Vikram S. Adve4f231662001-07-28 04:15:15 +0000102
103static void ForwardOperand (InstructionNode* treeNode,
104 InstructionNode* parent,
105 int operandNum);
Vikram S. Advea21cf202001-07-21 12:42:19 +0000106
107
Vikram S. Adved4228a52001-08-28 23:12:57 +0000108//************************ Internal Functions ******************************/
Vikram S. Advea21cf202001-07-21 12:42:19 +0000109
110
111//------------------------------------------------------------------------
112// External Function: ThisIsAChainRule
113//
114// Purpose:
115// Check if a given BURG rule is a chain rule.
116//------------------------------------------------------------------------
117
118extern bool
119ThisIsAChainRule(int eruleno)
120{
121 switch(eruleno)
122 {
123 case 111: // stmt: reg
124 case 112: // stmt: boolconst
125 case 113: // stmt: bool
126 case 121:
127 case 122:
128 case 123:
129 case 124:
130 case 125:
131 case 126:
132 case 127:
133 case 128:
134 case 129:
135 case 130:
136 case 131:
137 case 132:
Vikram S. Adve74f4a132001-07-31 21:46:57 +0000138 case 153:
139 case 155: return true; break;
Vikram S. Advea21cf202001-07-21 12:42:19 +0000140
141 default: return false; break;
142 }
143}
144
Vikram S. Adved4228a52001-08-28 23:12:57 +0000145
146static inline MachineOpCode
147ChooseBprInstruction(const InstructionNode* instrNode)
148{
149 MachineOpCode opCode;
150
151 Instruction* setCCInstr =
152 ((InstructionNode*) instrNode->leftChild())->getInstruction();
153
154 switch(setCCInstr->getOpcode())
155 {
156 case Instruction::SetEQ: opCode = BRZ; break;
157 case Instruction::SetNE: opCode = BRNZ; break;
158 case Instruction::SetLE: opCode = BRLEZ; break;
159 case Instruction::SetGE: opCode = BRGEZ; break;
160 case Instruction::SetLT: opCode = BRLZ; break;
161 case Instruction::SetGT: opCode = BRGZ; break;
162 default:
163 assert(0 && "Unrecognized VM instruction!");
164 opCode = INVALID_OPCODE;
165 break;
166 }
167
168 return opCode;
169}
170
171
172static inline MachineOpCode
173ChooseBccInstruction(const InstructionNode* instrNode,
174 bool& isFPBranch)
175{
176 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
177 BinaryOperator* setCCInstr = (BinaryOperator*) setCCNode->getInstruction();
178 const Type* setCCType = setCCInstr->getOperand(0)->getType();
179
180 isFPBranch = (setCCType == Type::FloatTy || setCCType == Type::DoubleTy);
181
182 if (isFPBranch)
183 return ChooseBFpccInstruction(instrNode, setCCInstr);
184 else
185 return ChooseBpccInstruction(instrNode, setCCInstr);
186}
187
188
189static inline MachineOpCode
190ChooseBpccInstruction(const InstructionNode* instrNode,
191 const BinaryOperator* setCCInstr)
192{
193 MachineOpCode opCode = INVALID_OPCODE;
194
195 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
196
197 if (isSigned)
198 {
199 switch(setCCInstr->getOpcode())
200 {
201 case Instruction::SetEQ: opCode = BE; break;
202 case Instruction::SetNE: opCode = BNE; break;
203 case Instruction::SetLE: opCode = BLE; break;
204 case Instruction::SetGE: opCode = BGE; break;
205 case Instruction::SetLT: opCode = BL; break;
206 case Instruction::SetGT: opCode = BG; break;
207 default:
208 assert(0 && "Unrecognized VM instruction!");
209 break;
210 }
211 }
212 else
213 {
214 switch(setCCInstr->getOpcode())
215 {
216 case Instruction::SetEQ: opCode = BE; break;
217 case Instruction::SetNE: opCode = BNE; break;
218 case Instruction::SetLE: opCode = BLEU; break;
219 case Instruction::SetGE: opCode = BCC; break;
220 case Instruction::SetLT: opCode = BCS; break;
221 case Instruction::SetGT: opCode = BGU; break;
222 default:
223 assert(0 && "Unrecognized VM instruction!");
224 break;
225 }
226 }
227
228 return opCode;
229}
230
231static inline MachineOpCode
232ChooseBFpccInstruction(const InstructionNode* instrNode,
233 const BinaryOperator* setCCInstr)
234{
235 MachineOpCode opCode = INVALID_OPCODE;
236
237 switch(setCCInstr->getOpcode())
238 {
239 case Instruction::SetEQ: opCode = FBE; break;
240 case Instruction::SetNE: opCode = FBNE; break;
241 case Instruction::SetLE: opCode = FBLE; break;
242 case Instruction::SetGE: opCode = FBGE; break;
243 case Instruction::SetLT: opCode = FBL; break;
244 case Instruction::SetGT: opCode = FBG; break;
245 default:
246 assert(0 && "Unrecognized VM instruction!");
247 break;
248 }
249
250 return opCode;
251}
252
253
254static inline MachineOpCode
255ChooseMovFpccInstruction(const InstructionNode* instrNode)
256{
257 MachineOpCode opCode = INVALID_OPCODE;
258
259 switch(instrNode->getInstruction()->getOpcode())
260 {
261 case Instruction::SetEQ: opCode = MOVFE; break;
262 case Instruction::SetNE: opCode = MOVFNE; break;
263 case Instruction::SetLE: opCode = MOVFLE; break;
264 case Instruction::SetGE: opCode = MOVFGE; break;
265 case Instruction::SetLT: opCode = MOVFL; break;
266 case Instruction::SetGT: opCode = MOVFG; break;
267 default:
268 assert(0 && "Unrecognized VM instruction!");
269 break;
270 }
271
272 return opCode;
273}
274
275
276// Assumes that SUBcc v1, v2 -> v3 has been executed.
277// In most cases, we want to clear v3 and then follow it by instruction
278// MOVcc 1 -> v3.
279// Set mustClearReg=false if v3 need not be cleared before conditional move.
280// Set valueToMove=0 if we want to conditionally move 0 instead of 1
281// (i.e., we want to test inverse of a condition)
282//
283//
284static MachineOpCode
285ChooseMovpccAfterSub(const InstructionNode* instrNode,
286 bool& mustClearReg,
287 int& valueToMove)
288{
289 MachineOpCode opCode = INVALID_OPCODE;
290 mustClearReg = true;
291 valueToMove = 1;
292
293 switch(instrNode->getInstruction()->getOpcode())
294 {
295 case Instruction::SetEQ: opCode = MOVNE; mustClearReg = false;
296 valueToMove = 0; break;
297 case Instruction::SetLE: opCode = MOVLE; break;
298 case Instruction::SetGE: opCode = MOVGE; break;
299 case Instruction::SetLT: opCode = MOVL; break;
300 case Instruction::SetGT: opCode = MOVG; break;
301
302 case Instruction::SetNE: assert(0 && "No move required!");
303
304 default:
305 assert(0 && "Unrecognized VM instruction!");
306 break;
307 }
308
309 return opCode;
310}
311
312
313static inline MachineOpCode
314ChooseConvertToFloatInstr(const InstructionNode* instrNode,
315 const Type* opType)
316{
317 MachineOpCode opCode = INVALID_OPCODE;
318
319 switch(instrNode->getOpLabel())
320 {
321 case ToFloatTy:
322 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
323 opCode = FITOS;
324 else if (opType == Type::LongTy)
325 opCode = FXTOS;
326 else if (opType == Type::DoubleTy)
327 opCode = FDTOS;
328 else
329 assert(0 && "Cannot convert this type to FLOAT on SPARC");
330 break;
331
332 case ToDoubleTy:
333 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
334 opCode = FITOD;
335 else if (opType == Type::LongTy)
336 opCode = FXTOD;
337 else if (opType == Type::FloatTy)
338 opCode = FSTOD;
339 else
340 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
341 break;
342
343 default:
344 break;
345 }
346
347 return opCode;
348}
349
350static inline MachineOpCode
351ChooseConvertToIntInstr(const InstructionNode* instrNode,
352 const Type* opType)
353{
354 MachineOpCode opCode = INVALID_OPCODE;;
355
356 int instrType = (int) instrNode->getOpLabel();
357
358 if (instrType == ToSByteTy || instrType == ToShortTy || instrType == ToIntTy)
359 {
360 switch (opType->getPrimitiveID())
361 {
362 case Type::FloatTyID: opCode = FSTOI; break;
363 case Type::DoubleTyID: opCode = FDTOI; break;
364 default:
365 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
366 break;
367 }
368 }
369 else if (instrType == ToLongTy)
370 {
371 switch (opType->getPrimitiveID())
372 {
373 case Type::FloatTyID: opCode = FSTOX; break;
374 case Type::DoubleTyID: opCode = FDTOX; break;
375 default:
376 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
377 break;
378 }
379 }
380 else
381 assert(0 && "Should not get here, Mo!");
382
383 return opCode;
384}
385
386
387static inline MachineOpCode
388ChooseAddInstruction(const InstructionNode* instrNode)
389{
390 MachineOpCode opCode = INVALID_OPCODE;
391
392 const Type* resultType = instrNode->getInstruction()->getType();
393
394 if (resultType->isIntegral() ||
395 resultType->isPointerType() ||
396 resultType->isMethodType() ||
397 resultType->isLabelType())
398 {
399 opCode = ADD;
400 }
401 else
402 {
403 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
404 switch(operand->getType()->getPrimitiveID())
405 {
406 case Type::FloatTyID: opCode = FADDS; break;
407 case Type::DoubleTyID: opCode = FADDD; break;
408 default: assert(0 && "Invalid type for ADD instruction"); break;
409 }
410 }
411
412 return opCode;
413}
414
415
416static inline MachineInstr*
417CreateMovFloatInstruction(const InstructionNode* instrNode,
418 const Type* resultType)
419{
420 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
421 ? FMOVS : FMOVD);
422 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
423 instrNode->leftChild()->getValue());
424 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
425 instrNode->getValue());
426 return minstr;
427}
428
429static inline MachineInstr*
430CreateAddConstInstruction(const InstructionNode* instrNode)
431{
432 MachineInstr* minstr = NULL;
433
434 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
435 assert(constOp->getValueType() == Value::ConstantVal);
436
437 // Cases worth optimizing are:
438 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
439 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
440 //
441 const Type* resultType = instrNode->getInstruction()->getType();
442
443 if (resultType == Type::FloatTy ||
444 resultType == Type::DoubleTy)
445 {
446 double dval = ((ConstPoolFP*) constOp)->getValue();
447 if (dval == 0.0)
448 minstr = CreateMovFloatInstruction(instrNode, resultType);
449 }
450
451 return minstr;
452}
453
454
455static inline MachineOpCode
456ChooseSubInstruction(const InstructionNode* instrNode)
457{
458 MachineOpCode opCode = INVALID_OPCODE;
459
460 const Type* resultType = instrNode->getInstruction()->getType();
461
462 if (resultType->isIntegral() ||
463 resultType->isPointerType())
464 {
465 opCode = SUB;
466 }
467 else
468 {
469 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
470 switch(operand->getType()->getPrimitiveID())
471 {
472 case Type::FloatTyID: opCode = FSUBS; break;
473 case Type::DoubleTyID: opCode = FSUBD; break;
474 default: assert(0 && "Invalid type for SUB instruction"); break;
475 }
476 }
477
478 return opCode;
479}
480
481
482static inline MachineInstr*
483CreateSubConstInstruction(const InstructionNode* instrNode)
484{
485 MachineInstr* minstr = NULL;
486
487 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
488 assert(constOp->getValueType() == Value::ConstantVal);
489
490 // Cases worth optimizing are:
491 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
492 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
493 //
494 const Type* resultType = instrNode->getInstruction()->getType();
495
496 if (resultType == Type::FloatTy ||
497 resultType == Type::DoubleTy)
498 {
499 double dval = ((ConstPoolFP*) constOp)->getValue();
500 if (dval == 0.0)
501 minstr = CreateMovFloatInstruction(instrNode, resultType);
502 }
503
504 return minstr;
505}
506
507
508static inline MachineOpCode
509ChooseFcmpInstruction(const InstructionNode* instrNode)
510{
511 MachineOpCode opCode = INVALID_OPCODE;
512
513 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
514 switch(operand->getType()->getPrimitiveID())
515 {
516 case Type::FloatTyID: opCode = FCMPS; break;
517 case Type::DoubleTyID: opCode = FCMPD; break;
518 default: assert(0 && "Invalid type for FCMP instruction"); break;
519 }
520
521 return opCode;
522}
523
524
525// Assumes that leftArg and rightArg are both cast instructions.
526//
527static inline bool
528BothFloatToDouble(const InstructionNode* instrNode)
529{
530 InstrTreeNode* leftArg = instrNode->leftChild();
531 InstrTreeNode* rightArg = instrNode->rightChild();
532 InstrTreeNode* leftArgArg = leftArg->leftChild();
533 InstrTreeNode* rightArgArg = rightArg->leftChild();
534 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
535
536 // Check if both arguments are floats cast to double
537 return (leftArg->getValue()->getType() == Type::DoubleTy &&
538 leftArgArg->getValue()->getType() == Type::FloatTy &&
539 rightArgArg->getValue()->getType() == Type::FloatTy);
540}
541
542
543static inline MachineOpCode
544ChooseMulInstruction(const InstructionNode* instrNode,
545 bool checkCasts)
546{
547 MachineOpCode opCode = INVALID_OPCODE;
548
549 if (checkCasts && BothFloatToDouble(instrNode))
550 {
551 return opCode = FSMULD;
552 }
553 // else fall through and use the regular multiply instructions
554
555 const Type* resultType = instrNode->getInstruction()->getType();
556
557 if (resultType->isIntegral())
558 {
559 opCode = MULX;
560 }
561 else
562 {
563 switch(instrNode->leftChild()->getValue()->getType()->getPrimitiveID())
564 {
565 case Type::FloatTyID: opCode = FMULS; break;
566 case Type::DoubleTyID: opCode = FMULD; break;
567 default: assert(0 && "Invalid type for MUL instruction"); break;
568 }
569 }
570
571 return opCode;
572}
573
574
575static inline MachineInstr*
576CreateIntNegInstruction(Value* vreg)
577{
578 MachineInstr* minstr = new MachineInstr(SUB);
579 minstr->SetMachineOperand(0, /*regNum %g0*/(unsigned int) 0);
580 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, vreg);
581 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, vreg);
582 return minstr;
583}
584
585
586static inline MachineInstr*
587CreateMulConstInstruction(const InstructionNode* instrNode,
588 MachineInstr*& getMinstr2)
589{
590 MachineInstr* minstr = NULL;
591 getMinstr2 = NULL;
592 bool needNeg = false;
593
594 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
595 assert(constOp->getValueType() == Value::ConstantVal);
596
597 // Cases worth optimizing are:
598 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
599 // (2) Multiply by 2^x for integer types: replace with Shift
600 //
601 const Type* resultType = instrNode->getInstruction()->getType();
602
603 if (resultType->isIntegral())
604 {
605 unsigned pow;
606 bool isValidConst;
607 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
608 if (isValidConst)
609 {
610 bool needNeg = false;
611 if (C < 0)
612 {
613 needNeg = true;
614 C = -C;
615 }
616
617 if (C == 0 || C == 1)
618 {
619 minstr = new MachineInstr(ADD);
620
621 if (C == 0)
622 minstr->SetMachineOperand(0, /*regNum %g0*/ (unsigned int) 0);
623 else
624 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
625 instrNode->leftChild()->getValue());
626 minstr->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
627 }
628 else if (IsPowerOf2(C, pow))
629 {
630 minstr = new MachineInstr((resultType == Type::LongTy)
631 ? SLLX : SLL);
632 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
633 instrNode->leftChild()->getValue());
634 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
635 pow);
636 }
637
638 if (minstr && needNeg)
639 { // insert <reg = SUB 0, reg> after the instr to flip the sign
640 getMinstr2 = CreateIntNegInstruction(instrNode->getValue());
641 }
642 }
643 }
644 else
645 {
646 if (resultType == Type::FloatTy ||
647 resultType == Type::DoubleTy)
648 {
649 bool isValidConst;
650 double dval = ((ConstPoolFP*) constOp)->getValue();
651
652 if (isValidConst)
653 {
654 if (dval == 0)
655 {
656 minstr = new MachineInstr((resultType == Type::FloatTy)
657 ? FITOS : FITOD);
658 minstr->SetMachineOperand(0, /*regNum %g0*/(unsigned int) 0);
659 }
660 else if (fabs(dval) == 1)
661 {
662 bool needNeg = (dval < 0);
663
664 MachineOpCode opCode = needNeg
665 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
666 : (resultType == Type::FloatTy? FMOVS : FMOVD);
667
668 minstr = new MachineInstr(opCode);
669 minstr->SetMachineOperand(0,
670 MachineOperand::MO_VirtualRegister,
671 instrNode->leftChild()->getValue());
672 }
673 }
674 }
675 }
676
677 if (minstr != NULL)
678 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
679 instrNode->getValue());
680
681 return minstr;
682}
683
684
685static inline MachineOpCode
686ChooseDivInstruction(const InstructionNode* instrNode)
687{
688 MachineOpCode opCode = INVALID_OPCODE;
689
690 const Type* resultType = instrNode->getInstruction()->getType();
691
692 if (resultType->isIntegral())
693 {
694 opCode = resultType->isSigned()? SDIVX : UDIVX;
695 }
696 else
697 {
698 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
699 switch(operand->getType()->getPrimitiveID())
700 {
701 case Type::FloatTyID: opCode = FDIVS; break;
702 case Type::DoubleTyID: opCode = FDIVD; break;
703 default: assert(0 && "Invalid type for DIV instruction"); break;
704 }
705 }
706
707 return opCode;
708}
709
710
711static inline MachineInstr*
712CreateDivConstInstruction(const InstructionNode* instrNode,
713 MachineInstr*& getMinstr2)
714{
715 MachineInstr* minstr = NULL;
716 getMinstr2 = NULL;
717
718 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
719 assert(constOp->getValueType() == Value::ConstantVal);
720
721 // Cases worth optimizing are:
722 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
723 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
724 //
725 const Type* resultType = instrNode->getInstruction()->getType();
726
727 if (resultType->isIntegral())
728 {
729 unsigned pow;
730 bool isValidConst;
731 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
732 if (isValidConst)
733 {
734 bool needNeg = false;
735 if (C < 0)
736 {
737 needNeg = true;
738 C = -C;
739 }
740
741 if (C == 1)
742 {
743 minstr = new MachineInstr(ADD);
744 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
745 instrNode->leftChild()->getValue());
746 minstr->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
747 }
748 else if (IsPowerOf2(C, pow))
749 {
750 MachineOpCode opCode= ((resultType->isSigned())
751 ? (resultType==Type::LongTy)? SRAX : SRA
752 : (resultType==Type::LongTy)? SRLX : SRL);
753 minstr = new MachineInstr(opCode);
754 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
755 instrNode->leftChild()->getValue());
756 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
757 pow);
758 }
759
760 if (minstr && needNeg)
761 { // insert <reg = SUB 0, reg> after the instr to flip the sign
762 getMinstr2 = CreateIntNegInstruction(instrNode->getValue());
763 }
764 }
765 }
766 else
767 {
768 if (resultType == Type::FloatTy ||
769 resultType == Type::DoubleTy)
770 {
771 bool isValidConst;
772 double dval = ((ConstPoolFP*) constOp)->getValue();
773
774 if (isValidConst && fabs(dval) == 1)
775 {
776 bool needNeg = (dval < 0);
777
778 MachineOpCode opCode = needNeg
779 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
780 : (resultType == Type::FloatTy? FMOVS : FMOVD);
781
782 minstr = new MachineInstr(opCode);
783 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
784 instrNode->leftChild()->getValue());
785 }
786 }
787 }
788
789 if (minstr != NULL)
790 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
791 instrNode->getValue());
792
793 return minstr;
794}
795
796
797static inline MachineOpCode
798ChooseLoadInstruction(const Type* resultType)
799{
800 MachineOpCode opCode = INVALID_OPCODE;
801
802 switch (resultType->getPrimitiveID())
803 {
804 case Type::BoolTyID: opCode = LDUB; break;
805 case Type::UByteTyID: opCode = LDUB; break;
806 case Type::SByteTyID: opCode = LDSB; break;
807 case Type::UShortTyID: opCode = LDUH; break;
808 case Type::ShortTyID: opCode = LDSH; break;
809 case Type::UIntTyID: opCode = LDUW; break;
810 case Type::IntTyID: opCode = LDSW; break;
811 case Type::ULongTyID:
812 case Type::LongTyID: opCode = LDX; break;
813 case Type::FloatTyID: opCode = LD; break;
814 case Type::DoubleTyID: opCode = LDD; break;
815 default: assert(0 && "Invalid type for Load instruction"); break;
816 }
817
818 return opCode;
819}
820
821
822static inline MachineOpCode
823ChooseStoreInstruction(const Type* valueType)
824{
825 MachineOpCode opCode = INVALID_OPCODE;
826
827 switch (valueType->getPrimitiveID())
828 {
829 case Type::BoolTyID:
830 case Type::UByteTyID:
831 case Type::SByteTyID: opCode = STB; break;
832 case Type::UShortTyID:
833 case Type::ShortTyID: opCode = STH; break;
834 case Type::UIntTyID:
835 case Type::IntTyID: opCode = STW; break;
836 case Type::ULongTyID:
837 case Type::LongTyID: opCode = STX; break;
838 case Type::FloatTyID: opCode = ST; break;
839 case Type::DoubleTyID: opCode = STD; break;
840 default: assert(0 && "Invalid type for Store instruction"); break;
841 }
842
843 return opCode;
844}
845
846
847//------------------------------------------------------------------------
848// Function SetOperandsForMemInstr
849//
850// Choose addressing mode for the given load or store instruction.
851// Use [reg+reg] if it is an indexed reference, and the index offset is
852// not a constant or if it cannot fit in the offset field.
853// Use [reg+offset] in all other cases.
854//
855// This assumes that all array refs are "lowered" to one of these forms:
856// %x = load (subarray*) ptr, constant ; single constant offset
857// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
858// Generally, this should happen via strength reduction + LICM.
859// Also, strength reduction should take care of using the same register for
860// the loop index variable and an array index, when that is profitable.
861//------------------------------------------------------------------------
862
863static void
864SetOperandsForMemInstr(MachineInstr* minstr,
865 const InstructionNode* vmInstrNode,
866 const TargetMachine& target)
867{
868 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
869
870 // Variables to hold the index vector, ptr value, and offset value.
871 // The major work here is to extract these for all 3 instruction types
872 // and then call the common function SetMemOperands_Internal().
873 //
874 const vector<ConstPoolVal*>* idxVec = & memInst->getIndexVec();
875 vector<ConstPoolVal*>* newIdxVec = NULL;
876 Value* ptrVal;
877 Value* arrayOffsetVal = NULL;
878
879 // Test if a GetElemPtr instruction is being folded into this mem instrn.
880 // If so, it will be in the left child for Load and GetElemPtr,
881 // and in the right child for Store instructions.
882 //
883 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
884 ? vmInstrNode->rightChild()
885 : vmInstrNode->leftChild());
886
887 if (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
888 ptrChild->getOpLabel() == GetElemPtrIdx)
889 {
890 // There is a GetElemPtr instruction and there may be a chain of
891 // more than one. Use the pointer value of the last one in the chain.
892 // Fold the index vectors from the entire chain and from the mem
893 // instruction into one single index vector.
894 // Finally, we never fold for an array instruction so make that NULL.
895
896 newIdxVec = new vector<ConstPoolVal*>;
897 ptrVal = FoldGetElemChain((InstructionNode*) ptrChild, *newIdxVec);
898
899 newIdxVec->insert(newIdxVec->end(), idxVec->begin(), idxVec->end());
900 idxVec = newIdxVec;
901
902 assert(! ((PointerType*)ptrVal->getType())->getValueType()->isArrayType()
903 && "GetElemPtr cannot be folded into array refs in selection");
904 }
905 else
906 {
907 // There is no GetElemPtr instruction.
908 // Use the pointer value and the index vector from the Mem instruction.
909 // If it is an array reference, get the array offset value.
910 //
911 ptrVal = memInst->getPtrOperand();
912
913 const Type* opType =
914 ((const PointerType*) ptrVal->getType())->getValueType();
915 if (opType->isArrayType())
916 {
917 assert((memInst->getNumOperands()
918 == (unsigned) 1 + memInst->getFirstOffsetIdx())
919 && "Array refs must be lowered before Instruction Selection");
920
921 arrayOffsetVal = memInst->getOperand(memInst->getFirstOffsetIdx());
922 }
923 }
924
925 SetMemOperands_Internal(minstr, vmInstrNode, ptrVal, arrayOffsetVal,
926 *idxVec, target);
927
928 if (newIdxVec != NULL)
929 delete newIdxVec;
930}
931
932
933static void
934SetMemOperands_Internal(MachineInstr* minstr,
935 const InstructionNode* vmInstrNode,
936 Value* ptrVal,
937 Value* arrayOffsetVal,
938 const vector<ConstPoolVal*>& idxVec,
939 const TargetMachine& target)
940{
941 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
942
943 // Initialize so we default to storing the offset in a register.
944 int64_t smallConstOffset;
945 Value* valueForRegOffset = NULL;
946 MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister;
947
948 // Check if there is an index vector and if so, if it translates to
949 // a small enough constant to fit in the immediate-offset field.
950 //
951 if (idxVec.size() > 0)
952 {
953 bool isConstantOffset = false;
954 unsigned offset;
955
956 const PointerType* ptrType = (PointerType*) ptrVal->getType();
957
958 if (ptrType->getValueType()->isStructType())
959 {
960 // the offset is always constant for structs
961 isConstantOffset = true;
962
963 // Compute the offset value using the index vector
964 offset = target.DataLayout.getIndexedOffset(ptrType, idxVec);
965 }
966 else
967 {
968 // It must be an array ref. Check if the offset is a constant,
969 // and that the indexing has been lowered to a single offset.
970 //
971 assert(ptrType->getValueType()->isArrayType());
972 assert(arrayOffsetVal != NULL
973 && "Expect to be given Value* for array offsets");
974
975 if (ConstPoolVal *CPV = arrayOffsetVal->castConstant())
976 {
977 isConstantOffset = true; // always constant for structs
978 assert(arrayOffsetVal->getType()->isIntegral());
979 offset = (CPV->getType()->isSigned()
980 ? ((ConstPoolSInt*)CPV)->getValue()
981 : (int64_t) ((ConstPoolUInt*)CPV)->getValue());
982 }
983 else
984 {
985 valueForRegOffset = arrayOffsetVal;
986 }
987 }
988
989 if (isConstantOffset)
990 {
991 // create a virtual register for the constant
992 valueForRegOffset = new ConstPoolSInt(Type::IntTy, offset);
993 }
994 }
995 else
996 {
997 offsetOpType = MachineOperand::MO_SignExtendedImmed;
998 smallConstOffset = 0;
999 }
1000
1001 // Operand 0 is value for STORE, ptr for LOAD or GET_ELEMENT_PTR
1002 // It is the left child in the instruction tree in all cases.
1003 Value* leftVal = vmInstrNode->leftChild()->getValue();
1004 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, leftVal);
1005
1006 // Operand 1 is ptr for STORE, offset for LOAD or GET_ELEMENT_PTR
1007 // Operand 3 is offset for STORE, result reg for LOAD or GET_ELEMENT_PTR
1008 //
1009 unsigned offsetOpNum = (memInst->getOpcode() == Instruction::Store)? 2 : 1;
1010 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1011 {
1012 assert(valueForRegOffset != NULL);
1013 minstr->SetMachineOperand(offsetOpNum, offsetOpType, valueForRegOffset);
1014 }
1015 else
1016 minstr->SetMachineOperand(offsetOpNum, offsetOpType, smallConstOffset);
1017
1018 if (memInst->getOpcode() == Instruction::Store)
1019 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, ptrVal);
1020 else
1021 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1022 vmInstrNode->getValue());
1023}
1024
1025
1026// Special handling for constant operands:
1027// -- if the constant is 0, use the hardwired 0 register, if any;
1028// -- if the constant is of float or double type but has an integer value,
1029// use int-to-float conversion instruction instead of generating a load;
1030// -- if the constant fits in the IMMEDIATE field, use that field;
1031// -- else insert instructions to put the constant into a register, either
1032// directly or by loading explicitly from the constant pool.
1033//
1034static unsigned
1035FixConstantOperands(const InstructionNode* vmInstrNode,
1036 MachineInstr** mvec,
1037 unsigned numInstr,
1038 TargetMachine& target)
1039{
1040 static MachineInstr* loadConstVec[MAX_INSTR_PER_VMINSTR];
1041
1042 unsigned numNew = 0;
1043 Instruction* vmInstr = vmInstrNode->getInstruction();
1044
1045 for (unsigned i=0; i < numInstr; i++)
1046 {
1047 MachineInstr* minstr = mvec[i];
1048 const MachineInstrDescriptor& instrDesc =
1049 target.getInstrInfo().getDescriptor(minstr->getOpCode());
1050
1051 for (unsigned op=0; op < minstr->getNumOperands(); op++)
1052 {
1053 const MachineOperand& mop = minstr->getOperand(op);
1054
1055 // skip the result position (for efficiency below) and any other
1056 // positions already marked as not a virtual register
1057 if (instrDesc.resultPos == (int) op ||
1058 mop.getOperandType() != MachineOperand::MO_VirtualRegister ||
1059 mop.getVRegValue() == NULL)
1060 {
1061 break;
1062 }
1063
1064 Value* opValue = mop.getVRegValue();
1065
1066 if (opValue->getValueType() == Value::ConstantVal)
1067 {
1068 unsigned int machineRegNum;
1069 int64_t immedValue;
1070 MachineOperand::MachineOperandType opType =
1071 ChooseRegOrImmed(opValue, minstr->getOpCode(), target,
1072 /*canUseImmed*/ (op == 1),
1073 machineRegNum, immedValue);
1074
1075 if (opType == MachineOperand::MO_MachineRegister)
1076 minstr->SetMachineOperand(op, machineRegNum);
1077 else if (opType == MachineOperand::MO_VirtualRegister)
1078 {
1079 // value is constant and must be loaded into a register
1080 TmpInstruction* tmpReg;
1081 MachineInstr* minstr2;
1082 loadConstVec[numNew++] = MakeLoadConstInstr(vmInstr, opValue,
1083 tmpReg, minstr2);
1084 minstr->SetMachineOperand(op, opType, tmpReg);
1085 if (minstr2 != NULL)
1086 loadConstVec[numNew++] = minstr2;
1087 }
1088 else
1089 minstr->SetMachineOperand(op, opType, immedValue);
1090 }
1091 }
1092 }
1093
1094 if (numNew > 0)
1095 {
1096 // Insert the new instructions *before* the old ones by moving
1097 // the old ones over `numNew' positions (last-to-first, of course!).
1098 // We do check *after* returning that we did not exceed the vector mvec.
1099 for (int i=numInstr-1; i >= 0; i--)
1100 mvec[i+numNew] = mvec[i];
1101
1102 for (unsigned i=0; i < numNew; i++)
1103 mvec[i] = loadConstVec[i];
1104 }
1105
1106 return (numInstr + numNew);
1107}
1108
1109
1110static inline MachineInstr*
1111MakeIntSetInstruction(int64_t C, bool isSigned, Value* dest)
1112{
1113 MachineInstr* minstr;
1114 if (isSigned)
1115 {
1116 minstr = new MachineInstr(SETSW);
1117 minstr->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, C);
1118 }
1119 else
1120 {
1121 minstr = new MachineInstr(SETUW);
1122 minstr->SetMachineOperand(0, MachineOperand::MO_UnextendedImmed, C);
1123 }
1124
1125 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, dest);
1126
1127 return minstr;
1128}
1129
1130
1131static MachineInstr*
1132MakeLoadConstInstr(Instruction* vmInstr,
1133 Value* val,
1134 TmpInstruction*& tmpReg,
1135 MachineInstr*& getMinstr2)
1136{
1137 assert(val->getValueType() == Value::ConstantVal);
1138
1139 MachineInstr* minstr;
1140
1141 getMinstr2 = NULL;
1142
1143 // Create a TmpInstruction to mark the hidden register used for the constant
1144 tmpReg = new TmpInstruction(Instruction::UserOp1, val, NULL);
1145 vmInstr->getMachineInstrVec().addTempValue(tmpReg);
1146
1147 // Use a "set" instruction for known constants that can go in an integer reg.
1148 // Use a "set" instruction followed by a int-to-float conversion for known
1149 // constants that must go in a floating point reg but have an integer value.
1150 // Use a "load" instruction for all other constants, in particular,
1151 // floating point constants.
1152 //
1153 const Type* valType = val->getType();
1154
1155 if (valType->isIntegral() ||
1156 valType->isPointerType() ||
1157 valType == Type::BoolTy)
1158 {
1159 bool isValidConstant;
1160 int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
1161 assert(isValidConstant && "Unrecognized constant");
1162
1163 minstr = MakeIntSetInstruction(C, valType->isSigned(), tmpReg);
1164 }
1165 else
1166 {
1167 assert(valType == Type::FloatTy || valType == Type::DoubleTy);
1168 double dval = ((ConstPoolFP*) val)->getValue();
1169 if (dval == (int64_t) dval)
1170 {
1171 // The constant actually has an integer value, so use a
1172 // [set; int-to-float] sequence instead of a load instruction.
1173 //
1174 TmpInstruction* tmpReg2 = NULL;
1175 if (dval != 0.0)
1176 { // First, create an integer constant of the same value as dval
1177 ConstPoolSInt* ival = new ConstPoolSInt(Type::IntTy,
1178 (int64_t) dval);
1179 ConstantPool& cpool =
1180 vmInstr->getParent()->getParent()->getConstantPool();
1181 ConstPoolVal* prev = cpool.find(ival);
1182 if (prev == NULL)
1183 cpool.insert(ival);
1184 else
1185 {
1186 delete ival;
1187 ival = (ConstPoolSInt*) prev;
1188 }
1189
1190 // Create another TmpInstruction for the hidden integer register
1191 TmpInstruction* tmpReg2 =
1192 new TmpInstruction(Instruction::UserOp1, ival, NULL);
1193 vmInstr->getMachineInstrVec().addTempValue(tmpReg2);
1194
1195 // Create the `SET' instruction
1196 minstr = MakeIntSetInstruction((int64_t)dval, true, tmpReg2);
1197 }
1198
1199 // In which variable do we put the second instruction?
1200 MachineInstr*& instr2 = (minstr)? getMinstr2 : minstr;
1201
1202 // Create the int-to-float instruction
1203 instr2 = new MachineInstr(valType == Type::FloatTy? FITOS : FITOD);
1204
1205 if (dval == 0.0)
1206 instr2->SetMachineOperand(0, /*regNum %g0*/ (unsigned int) 0);
1207 else
1208 instr2->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
1209 tmpReg2);
1210
1211 instr2->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1212 tmpReg);
1213 }
1214 else
1215 {
1216 // Make a Load instruction, and make `val' both the ptr value *and*
1217 // the result value, and set the offset field to 0. Final code
1218 // generation will have to generate the base+offset for the constant.
1219 //
1220 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
1221 minstr = new MachineInstr(ChooseLoadInstruction(val->getType()));
1222 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,val);
1223 minstr->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1224 zeroOffset);
1225 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1226 tmpReg);
1227 }
1228 }
1229
1230 tmpReg->addMachineInstruction(minstr);
1231
1232 assert(minstr);
1233 return minstr;
1234}
1235
1236//
1237// Substitute operand `operandNum' of the instruction in node `treeNode'
1238// in place the use(s) of that instruction in node `parent'.
1239//
1240static void
1241ForwardOperand(InstructionNode* treeNode,
1242 InstructionNode* parent,
1243 int operandNum)
1244{
1245 Instruction* unusedOp = treeNode->getInstruction();
1246 Value* fwdOp = unusedOp->getOperand(operandNum);
1247 Instruction* userInstr = parent->getInstruction();
1248 MachineCodeForVMInstr& mvec = userInstr->getMachineInstrVec();
1249 for (unsigned i=0, N=mvec.size(); i < N; i++)
1250 {
1251 MachineInstr* minstr = mvec[i];
1252 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; i++)
1253 {
1254 const MachineOperand& mop = minstr->getOperand(i);
1255 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1256 mop.getVRegValue() == unusedOp)
1257 {
1258 minstr->SetMachineOperand(i, MachineOperand::MO_VirtualRegister,
1259 fwdOp);
1260 }
1261 }
1262 }
1263}
1264
1265
1266// This function is currently unused and incomplete but will be
1267// used if we have a linear layout of basic blocks in LLVM code.
1268// It decides which branch should fall-through, and whether an
1269// extra unconditional branch is needed (when neither falls through).
1270//
1271void
1272ChooseBranchPattern(Instruction* vmInstr, BranchPattern& brPattern)
1273{
1274 BranchInst* brInstr = (BranchInst*) vmInstr;
1275
1276 brPattern.flipCondition = false;
1277 brPattern.targetBB = brInstr->getSuccessor(0);
1278 brPattern.extraBranch = NULL;
1279
1280 assert(brInstr->getNumSuccessors() > 1 &&
1281 "Unnecessary analysis for unconditional branch");
1282
1283 assert(0 && "Fold branches in peephole optimization");
1284}
1285
1286
1287//******************* Externally Visible Functions *************************/
1288
1289
Vikram S. Advea21cf202001-07-21 12:42:19 +00001290//------------------------------------------------------------------------
1291// External Function: GetInstructionsByRule
1292//
1293// Purpose:
1294// Choose machine instructions for the SPARC according to the
1295// patterns chosen by the BURG-generated parser.
1296//------------------------------------------------------------------------
1297
1298unsigned
1299GetInstructionsByRule(InstructionNode* subtreeRoot,
1300 int ruleForNode,
1301 short* nts,
Vikram S. Adve4f231662001-07-28 04:15:15 +00001302 TargetMachine &target,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001303 MachineInstr** mvec)
1304{
1305 int numInstr = 1; // initialize for common case
1306 bool checkCast = false; // initialize here to use fall-through
1307 Value *leftVal, *rightVal;
1308 const Type* opType;
1309 int nextRule;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001310 int forwardOperandNum = -1;
Vikram S. Advea21cf202001-07-21 12:42:19 +00001311 BranchPattern brPattern;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001312 int64_t s0 = 0; // variables holding zero to avoid
1313 uint64_t u0 = 0; // overloading ambiguities below
Vikram S. Advea21cf202001-07-21 12:42:19 +00001314
1315 mvec[0] = mvec[1] = mvec[2] = mvec[3] = NULL; // just for safety
1316
1317 switch(ruleForNode) {
1318 case 1: // stmt: Ret
1319 case 2: // stmt: RetValue(reg)
1320 // NOTE: Prepass of register allocation is responsible
1321 // for moving return value to appropriate register.
1322 // Mark the return-address register as a hidden virtual reg.
1323 {
1324 Instruction* returnReg = new TmpInstruction(Instruction::UserOp1,
1325 subtreeRoot->getInstruction(), NULL);
1326 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(returnReg);
1327
1328 mvec[0] = new MachineInstr(RETURN);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001329 mvec[0]->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,returnReg);
1330 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed, s0);
1331
1332 returnReg->addMachineInstruction(mvec[0]);
1333
Vikram S. Advea21cf202001-07-21 12:42:19 +00001334 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1335 break;
1336 }
1337
1338 case 3: // stmt: Store(reg,reg)
1339 case 4: // stmt: Store(reg,ptrreg)
1340 mvec[0] = new MachineInstr(ChooseStoreInstruction(subtreeRoot->leftChild()->getValue()->getType()));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001341 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001342 break;
1343
1344 case 5: // stmt: BrUncond
1345 mvec[0] = new MachineInstr(BA);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001346 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister, (Value*)NULL);
1347 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001348 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1349
Vikram S. Adved4228a52001-08-28 23:12:57 +00001350 // delay slot
1351 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001352 break;
1353
1354 case 6: // stmt: BrCond(boolconst)
1355 // boolconst => boolean was computed with `%b = setCC type reg1 constant'
1356 // If the constant is ZERO, we can use the branch-on-integer-register
1357 // instructions and avoid the SUBcc instruction entirely.
1358 // Otherwise this is just the same as case 5, so just fall through.
1359 {
1360 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1361 assert(constNode && constNode->getNodeType() ==InstrTreeNode::NTConstNode);
1362 ConstPoolVal* constVal = (ConstPoolVal*) constNode->getValue();
Vikram S. Adve4f231662001-07-28 04:15:15 +00001363 bool isValidConst;
Vikram S. Advea21cf202001-07-21 12:42:19 +00001364
1365 if (constVal->getType()->isIntegral()
Vikram S. Adve4f231662001-07-28 04:15:15 +00001366 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1367 && isValidConst)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001368 {
Vikram S. Adve4f231662001-07-28 04:15:15 +00001369 // That constant ia a zero after all...
Vikram S. Advea21cf202001-07-21 12:42:19 +00001370 // Use the left child of the setCC instruction as the first argument!
1371 mvec[0] = new MachineInstr(ChooseBprInstruction(subtreeRoot));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001372 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001373 subtreeRoot->leftChild()->leftChild()->getValue());
1374 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1375 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1376
Vikram S. Adve4f231662001-07-28 04:15:15 +00001377 // delay slot
1378 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001379
Vikram S. Adve4f231662001-07-28 04:15:15 +00001380 // false branch
1381 mvec[numInstr++] = new MachineInstr(BA);
1382 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1383 (Value*) NULL);
1384 mvec[numInstr-1]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp, ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adved4228a52001-08-28 23:12:57 +00001385
1386 // delay slot
1387 mvec[numInstr++] = new MachineInstr(NOP);
1388
Vikram S. Advea21cf202001-07-21 12:42:19 +00001389 break;
1390 }
1391 // ELSE FALL THROUGH
1392 }
1393
1394 case 7: // stmt: BrCond(bool)
1395 // bool => boolean was computed with `%b = setcc type reg1 reg2'
1396 // Need to check whether the type was a FP, signed int or unsigned int,
Vikram S. Adve4f231662001-07-28 04:15:15 +00001397 // and check the branching condition in order to choose the branch to use.
Vikram S. Advea21cf202001-07-21 12:42:19 +00001398 //
1399 {
1400 bool isFPBranch;
1401 mvec[0] = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001402 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1403 subtreeRoot->leftChild()->getValue());
1404 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001405 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1406
Vikram S. Adve4f231662001-07-28 04:15:15 +00001407 // delay slot
1408 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001409
Vikram S. Adve4f231662001-07-28 04:15:15 +00001410 // false branch
1411 mvec[numInstr++] = new MachineInstr(BA);
1412 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1413 (Value*) NULL);
1414 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp,
1415 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adved4228a52001-08-28 23:12:57 +00001416
1417 // delay slot
1418 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001419 break;
1420 }
Vikram S. Adve4f231662001-07-28 04:15:15 +00001421
Vikram S. Advea21cf202001-07-21 12:42:19 +00001422 case 8: // stmt: BrCond(boolreg)
1423 // bool => boolean is stored in an existing register.
1424 // Just use the branch-on-integer-register instruction!
1425 //
1426 mvec[0] = new MachineInstr(BRNZ);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001427 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1428 subtreeRoot->leftChild()->getValue());
Vikram S. Advea21cf202001-07-21 12:42:19 +00001429 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1430 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adved4228a52001-08-28 23:12:57 +00001431
1432 // delay slot
Vikram S. Advea21cf202001-07-21 12:42:19 +00001433 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
Vikram S. Adved4228a52001-08-28 23:12:57 +00001434
1435 // false branch
1436 mvec[numInstr++] = new MachineInstr(BA);
1437 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1438 (Value*) NULL);
1439 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp,
1440 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
1441
1442 // delay slot
1443 mvec[numInstr++] = new MachineInstr(NOP);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001444 break;
1445
1446 case 9: // stmt: Switch(reg)
1447 assert(0 && "*** SWITCH instruction is not implemented yet.");
1448 numInstr = 0;
1449 break;
1450
1451 case 10: // reg: VRegList(reg, reg)
1452 assert(0 && "VRegList should never be the topmost non-chain rule");
1453 break;
1454
1455 case 21: // reg: Not(reg): Implemented as reg = reg XOR-NOT 0
1456 mvec[0] = new MachineInstr(XNOR);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001457 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001458 subtreeRoot->leftChild()->getValue());
1459 mvec[0]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001460 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001461 subtreeRoot->getValue());
1462 break;
1463
1464 case 22: // reg: ToBoolTy(reg):
1465 opType = subtreeRoot->leftChild()->getValue()->getType();
1466 assert(opType->isIntegral() || opType == Type::BoolTy);
1467 numInstr = 0;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001468 forwardOperandNum = 0;
Vikram S. Advea21cf202001-07-21 12:42:19 +00001469 break;
1470
1471 case 23: // reg: ToUByteTy(reg)
1472 case 25: // reg: ToUShortTy(reg)
1473 case 27: // reg: ToUIntTy(reg)
1474 case 29: // reg: ToULongTy(reg)
1475 opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve74f4a132001-07-31 21:46:57 +00001476 assert(opType->isIntegral() ||
1477 opType->isPointerType() ||
1478 opType == Type::BoolTy && "Ignoring cast: illegal for other types");
Vikram S. Advea21cf202001-07-21 12:42:19 +00001479 numInstr = 0;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001480 forwardOperandNum = 0;
Vikram S. Advea21cf202001-07-21 12:42:19 +00001481 break;
1482
1483 case 24: // reg: ToSByteTy(reg)
1484 case 26: // reg: ToShortTy(reg)
1485 case 28: // reg: ToIntTy(reg)
1486 case 30: // reg: ToLongTy(reg)
1487 opType = subtreeRoot->leftChild()->getValue()->getType();
1488 if (opType->isIntegral() || opType == Type::BoolTy)
Vikram S. Adve4f231662001-07-28 04:15:15 +00001489 {
1490 numInstr = 0;
1491 forwardOperandNum = 0;
1492 }
Vikram S. Advea21cf202001-07-21 12:42:19 +00001493 else
1494 {
1495 mvec[0] =new MachineInstr(ChooseConvertToIntInstr(subtreeRoot,opType));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001496 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001497 }
1498 break;
1499
1500 case 31: // reg: ToFloatTy(reg):
1501 case 32: // reg: ToDoubleTy(reg):
1502
1503 // If this instruction has a parent (a user) in the tree
1504 // and the user is translated as an FsMULd instruction,
1505 // then the cast is unnecessary. So check that first.
1506 // In the future, we'll want to do the same for the FdMULq instruction,
1507 // so do the check here instead of only for ToFloatTy(reg).
1508 //
1509 if (subtreeRoot->parent() != NULL &&
1510 ((InstructionNode*) subtreeRoot->parent())->getInstruction()->getMachineInstrVec()[0]->getOpCode() == FSMULD)
1511 {
1512 numInstr = 0;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001513 forwardOperandNum = 0;
Vikram S. Advea21cf202001-07-21 12:42:19 +00001514 }
1515 else
1516 {
1517 opType = subtreeRoot->leftChild()->getValue()->getType();
1518 mvec[0] = new MachineInstr(ChooseConvertToFloatInstr(subtreeRoot, opType));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001519 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001520 }
1521 break;
1522
1523 case 19: // reg: ToArrayTy(reg):
1524 case 20: // reg: ToPointerTy(reg):
1525 numInstr = 0;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001526 forwardOperandNum = 0;
Vikram S. Advea21cf202001-07-21 12:42:19 +00001527 break;
1528
Vikram S. Adved4228a52001-08-28 23:12:57 +00001529 case 233: // reg: Add(reg, Constant)
1530 mvec[0] = CreateAddConstInstruction(subtreeRoot);
1531 if (mvec[0] != NULL)
1532 break;
1533 // ELSE FALL THROUGH
1534
Vikram S. Advea21cf202001-07-21 12:42:19 +00001535 case 33: // reg: Add(reg, reg)
1536 mvec[0] = new MachineInstr(ChooseAddInstruction(subtreeRoot));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001537 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001538 break;
1539
Vikram S. Adved4228a52001-08-28 23:12:57 +00001540 case 234: // reg: Sub(reg, Constant)
1541 mvec[0] = CreateSubConstInstruction(subtreeRoot);
1542 if (mvec[0] != NULL)
1543 break;
1544 // ELSE FALL THROUGH
1545
Vikram S. Advea21cf202001-07-21 12:42:19 +00001546 case 34: // reg: Sub(reg, reg)
1547 mvec[0] = new MachineInstr(ChooseSubInstruction(subtreeRoot));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001548 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001549 break;
1550
1551 case 135: // reg: Mul(todouble, todouble)
1552 checkCast = true;
1553 // FALL THROUGH
1554
1555 case 35: // reg: Mul(reg, reg)
1556 mvec[0] = new MachineInstr(ChooseMulInstruction(subtreeRoot, checkCast));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001557 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001558 break;
Vikram S. Adved4228a52001-08-28 23:12:57 +00001559
1560 case 335: // reg: Mul(todouble, todoubleConst)
1561 checkCast = true;
1562 // FALL THROUGH
1563
1564 case 235: // reg: Mul(reg, Constant)
1565 mvec[0] = CreateMulConstInstruction(subtreeRoot, mvec[1]);
1566 if (mvec[0] == NULL)
1567 {
1568 mvec[0]=new MachineInstr(ChooseMulInstruction(subtreeRoot, checkCast));
1569 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1570 }
1571 else
1572 if (mvec[1] != NULL)
1573 ++numInstr;
1574 break;
1575
1576 case 236: // reg: Div(reg, Constant)
1577 mvec[0] = CreateDivConstInstruction(subtreeRoot, mvec[1]);
1578 if (mvec[0] != NULL)
1579 {
1580 if (mvec[1] != NULL)
1581 ++numInstr;
1582 }
1583 else
1584 // ELSE FALL THROUGH
Vikram S. Advea21cf202001-07-21 12:42:19 +00001585
1586 case 36: // reg: Div(reg, reg)
1587 mvec[0] = new MachineInstr(ChooseDivInstruction(subtreeRoot));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001588 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001589 break;
1590
Vikram S. Adved4228a52001-08-28 23:12:57 +00001591 case 37: // reg: Rem(reg, reg)
1592 case 237: // reg: Rem(reg, Constant)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001593 assert(0 && "REM instruction unimplemented for the SPARC.");
1594 break;
1595
Vikram S. Adved4228a52001-08-28 23:12:57 +00001596 case 38: // reg: And(reg, reg)
1597 case 238: // reg: And(reg, Constant)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001598 mvec[0] = new MachineInstr(AND);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001599 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001600 break;
1601
1602 case 138: // reg: And(reg, not)
1603 mvec[0] = new MachineInstr(ANDN);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001604 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001605 break;
1606
Vikram S. Adved4228a52001-08-28 23:12:57 +00001607 case 39: // reg: Or(reg, reg)
1608 case 239: // reg: Or(reg, Constant)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001609 mvec[0] = new MachineInstr(ORN);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001610 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001611 break;
1612
1613 case 139: // reg: Or(reg, not)
1614 mvec[0] = new MachineInstr(ORN);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001615 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001616 break;
1617
Vikram S. Adved4228a52001-08-28 23:12:57 +00001618 case 40: // reg: Xor(reg, reg)
1619 case 240: // reg: Xor(reg, Constant)
Vikram S. Advea21cf202001-07-21 12:42:19 +00001620 mvec[0] = new MachineInstr(XOR);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001621 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001622 break;
1623
1624 case 140: // reg: Xor(reg, not)
1625 mvec[0] = new MachineInstr(XNOR);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001626 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001627 break;
1628
1629 case 41: // boolconst: SetCC(reg, Constant)
1630 // Check if this is an integer comparison, and
1631 // there is a parent, and the parent decided to use
1632 // a branch-on-integer-register instead of branch-on-condition-code.
1633 // If so, the SUBcc instruction is not required.
1634 // (However, we must still check for constants to be loaded from
1635 // the constant pool so that such a load can be associated with
1636 // this instruction.)
1637 //
Vikram S. Adve4f231662001-07-28 04:15:15 +00001638 // Otherwise this is just the same as case 42, so just fall through.
Vikram S. Advea21cf202001-07-21 12:42:19 +00001639 //
1640 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral() &&
1641 subtreeRoot->parent() != NULL)
1642 {
1643 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1644 assert(parentNode->getNodeType() == InstrTreeNode::NTInstructionNode);
1645 const vector<MachineInstr*>&
1646 minstrVec = parentNode->getInstruction()->getMachineInstrVec();
1647 MachineOpCode parentOpCode;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001648 if (parentNode->getInstruction()->getOpcode() == Instruction::Br &&
Vikram S. Advea21cf202001-07-21 12:42:19 +00001649 (parentOpCode = minstrVec[0]->getOpCode()) >= BRZ &&
1650 parentOpCode <= BRGEZ)
1651 {
Vikram S. Adve4f231662001-07-28 04:15:15 +00001652 numInstr = 0; // don't forward the operand!
Vikram S. Advea21cf202001-07-21 12:42:19 +00001653 break;
1654 }
1655 }
1656 // ELSE FALL THROUGH
1657
1658 case 42: // bool: SetCC(reg, reg):
Vikram S. Adve4f231662001-07-28 04:15:15 +00001659 {
1660 // If result of the SetCC is only used for a branch, we can
1661 // discard the result. otherwise, it must go into an integer register.
1662 // Note that the user may or may not be in the same tree, so we have
1663 // to follow SSA def-use edges here, not BURG tree edges.
1664 //
1665 Instruction* result = subtreeRoot->getInstruction();
1666 Value* firstUse = (Value*) * result->use_begin();
1667 bool discardResult =
1668 (result->use_size() == 1
1669 && firstUse->getValueType() == Value::InstructionVal
1670 && ((Instruction*) firstUse)->getOpcode() == Instruction::Br);
1671
1672 bool mustClearReg;
1673 int valueToMove;
1674 MachineOpCode movOpCode;
1675
Vikram S. Advea21cf202001-07-21 12:42:19 +00001676 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral())
1677 {
Vikram S. Adve4f231662001-07-28 04:15:15 +00001678 // integer condition: destination should be %g0 or integer register
1679 // if result must be saved but condition is not SetEQ then we need
1680 // a separate instruction to compute the bool result, so discard
1681 // result of SUBcc instruction anyway.
1682 //
Vikram S. Advea21cf202001-07-21 12:42:19 +00001683 mvec[0] = new MachineInstr(SUBcc);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001684 Set3OperandsFromInstr(mvec[0], subtreeRoot, target, discardResult);
1685
Vikram S. Adve98a9c972001-08-06 21:06:10 +00001686 // mark the 4th operand as being a CC register, and a "result"
1687 mvec[0]->SetMachineOperand(3, MachineOperand::MO_CCRegister,
1688 subtreeRoot->getValue(), /*def*/ true);
1689
1690 if (!discardResult)
Vikram S. Adve4f231662001-07-28 04:15:15 +00001691 { // recompute bool if needed, using the integer condition codes
1692 if (result->getOpcode() == Instruction::SetNE)
1693 discardResult = true;
1694 else
1695 movOpCode =
1696 ChooseMovpccAfterSub(subtreeRoot, mustClearReg, valueToMove);
1697 }
Vikram S. Advea21cf202001-07-21 12:42:19 +00001698 }
1699 else
1700 {
Vikram S. Adve4f231662001-07-28 04:15:15 +00001701 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Advea21cf202001-07-21 12:42:19 +00001702 mvec[0] = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
Vikram S. Advea21cf202001-07-21 12:42:19 +00001703 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
Vikram S. Adve4f231662001-07-28 04:15:15 +00001704 subtreeRoot->getValue());
1705 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1706 subtreeRoot->leftChild()->getValue());
1707 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1708 subtreeRoot->rightChild()->getValue());
1709
1710 if (!discardResult)
1711 {// recompute bool using the FP condition codes
1712 mustClearReg = true;
1713 valueToMove = 1;
1714 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1715 }
1716 }
1717
1718 if (!discardResult)
1719 {
1720 if (mustClearReg)
1721 {// Unconditionally set register to 0
1722 int n = numInstr++;
1723 mvec[n] = new MachineInstr(SETHI);
1724 mvec[n]->SetMachineOperand(0,MachineOperand::MO_UnextendedImmed,s0);
1725 mvec[n]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1726 subtreeRoot->getValue());
1727 }
1728
1729 // Now conditionally move `valueToMove' (0 or 1) into the register
1730 int n = numInstr++;
1731 mvec[n] = new MachineInstr(movOpCode);
1732 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1733 subtreeRoot->getValue());
1734 mvec[n]->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
1735 valueToMove);
1736 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1737 subtreeRoot->getValue());
Vikram S. Advea21cf202001-07-21 12:42:19 +00001738 }
1739 break;
Vikram S. Adve4f231662001-07-28 04:15:15 +00001740 }
1741
Vikram S. Advea21cf202001-07-21 12:42:19 +00001742 case 43: // boolreg: VReg
Vikram S. Adve74f4a132001-07-31 21:46:57 +00001743 case 44: // boolreg: Constant
Vikram S. Advea21cf202001-07-21 12:42:19 +00001744 numInstr = 0;
1745 break;
1746
1747 case 51: // reg: Load(reg)
1748 case 52: // reg: Load(ptrreg)
1749 case 53: // reg: LoadIdx(reg,reg)
1750 case 54: // reg: LoadIdx(ptrreg,reg)
1751 mvec[0] = new MachineInstr(ChooseLoadInstruction(subtreeRoot->getValue()->getType()));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001752 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001753 break;
1754
1755 case 55: // reg: GetElemPtr(reg)
1756 case 56: // reg: GetElemPtrIdx(reg,reg)
1757 if (subtreeRoot->parent() != NULL)
1758 {
1759 // Check if the parent was an array access.
1760 // If so, we still need to generate this instruction.
1761 MemAccessInst* memInst =(MemAccessInst*) subtreeRoot->getInstruction();
1762 const PointerType* ptrType =
1763 (const PointerType*) memInst->getPtrOperand()->getType();
1764 if (! ptrType->getValueType()->isArrayType())
1765 {// we don't need a separate instr
Vikram S. Adve4f231662001-07-28 04:15:15 +00001766 numInstr = 0; // don't forward operand!
Vikram S. Advea21cf202001-07-21 12:42:19 +00001767 break;
1768 }
1769 }
1770 // else in all other cases we need to a separate ADD instruction
1771 mvec[0] = new MachineInstr(ADD);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001772 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001773 break;
1774
1775 case 57: // reg: Alloca: Implement as 2 instructions:
1776 // sub %sp, tmp -> %sp
1777 { // add %sp, 0 -> result
1778 Instruction* instr = subtreeRoot->getInstruction();
1779 const PointerType* instrType = (const PointerType*) instr->getType();
1780 assert(instrType->isPointerType());
Vikram S. Adve4f231662001-07-28 04:15:15 +00001781 int tsize = (int) target.findOptimalStorageSize(instrType->getValueType());
1782 assert(tsize != 0 && "Just to check when this can happen");
1783 // if (tsize == 0)
1784 // {
1785 // numInstr = 0;
1786 // break;
1787 // }
Vikram S. Advea21cf202001-07-21 12:42:19 +00001788 //else go on to create the instructions needed...
1789
1790 // Create a temporary Value to hold the constant type-size
1791 ConstPoolSInt* valueForTSize = new ConstPoolSInt(Type::IntTy, tsize);
1792 ConstantPool &cpool = instr->getParent()->getParent()->getConstantPool();
1793 if (cpool.find(valueForTSize) == 0)
1794 cpool.insert(valueForTSize);
1795
1796 // Instruction 1: sub %sp, tsize -> %sp
1797 // tsize is always constant, but it may have to be put into a
1798 // register if it doesn't fit in the immediate field.
1799 //
1800 mvec[0] = new MachineInstr(SUB);
1801 mvec[0]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001802 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, valueForTSize);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001803 mvec[0]->SetMachineOperand(2, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1804
1805 // Instruction 2: add %sp, 0 -> result
1806 numInstr++;
1807 mvec[1] = new MachineInstr(ADD);
1808 mvec[1]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1809 mvec[1]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001810 mvec[1]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, instr);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001811 break;
1812 }
1813
1814 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1815 // mul num, typeSz -> tmp
1816 // sub %sp, tmp -> %sp
1817 { // add %sp, 0 -> result
1818 Instruction* instr = subtreeRoot->getInstruction();
1819 const PointerType* instrType = (const PointerType*) instr->getType();
1820 assert(instrType->isPointerType() &&
1821 instrType->getValueType()->isArrayType());
1822 const Type* eltType =
1823 ((ArrayType*) instrType->getValueType())->getElementType();
Vikram S. Adve4f231662001-07-28 04:15:15 +00001824 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001825
Vikram S. Adve4f231662001-07-28 04:15:15 +00001826 assert(tsize != 0 && "Just to check when this can happen");
1827 // if (tsize == 0)
1828 // {
1829 // numInstr = 0;
1830 // break;
1831 // }
Vikram S. Advea21cf202001-07-21 12:42:19 +00001832 //else go on to create the instructions needed...
1833
1834 // Create a temporary Value to hold the constant type-size
1835 ConstPoolSInt* valueForTSize = new ConstPoolSInt(Type::IntTy, tsize);
1836 ConstantPool &cpool = instr->getParent()->getParent()->getConstantPool();
1837 if (cpool.find(valueForTSize) == 0)
1838 cpool.insert(valueForTSize);
1839
1840 // Create a temporary value to hold `tmp'
1841 Instruction* tmpInstr = new TmpInstruction(Instruction::UserOp1,
1842 subtreeRoot->leftChild()->getValue(),
1843 NULL /*could insert tsize here*/);
1844 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(tmpInstr);
1845
1846 // Instruction 1: mul numElements, typeSize -> tmp
1847 mvec[0] = new MachineInstr(MULX);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001848 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001849 subtreeRoot->leftChild()->getValue());
Vikram S. Adve4f231662001-07-28 04:15:15 +00001850 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, valueForTSize);
1851 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,tmpInstr);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001852
Vikram S. Adve4f231662001-07-28 04:15:15 +00001853 tmpInstr->addMachineInstruction(mvec[0]);
1854
Vikram S. Advea21cf202001-07-21 12:42:19 +00001855 // Instruction 2: sub %sp, tmp -> %sp
1856 numInstr++;
1857 mvec[1] = new MachineInstr(SUB);
1858 mvec[1]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001859 mvec[1]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,tmpInstr);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001860 mvec[1]->SetMachineOperand(2, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1861
1862 // Instruction 3: add %sp, 0 -> result
1863 numInstr++;
1864 mvec[2] = new MachineInstr(ADD);
1865 mvec[2]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1866 mvec[2]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001867 mvec[2]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, instr);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001868 break;
1869 }
1870
1871 case 61: // reg: Call
1872 // Generate a call-indirect (i.e., JMPL) for now to expose
1873 // the potential need for registers. If an absolute address
1874 // is available, replace this with a CALL instruction.
1875 // Mark both the indirection register and the return-address
1876 { // register as hidden virtual registers.
1877
Vikram S. Adve4f231662001-07-28 04:15:15 +00001878 Instruction* jmpAddrReg = new TmpInstruction(Instruction::UserOp1,
Vikram S. Advea21cf202001-07-21 12:42:19 +00001879 ((CallInst*) subtreeRoot->getInstruction())->getCalledMethod(), NULL);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001880 Instruction* retAddrReg = new TmpInstruction(Instruction::UserOp1,
1881 subtreeRoot->getValue(), NULL);
1882 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(jmpAddrReg);
1883 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(retAddrReg);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001884
1885 mvec[0] = new MachineInstr(JMPL);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001886 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, jmpAddrReg);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001887 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1888 (int64_t) 0);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001889 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, retAddrReg);
1890
1891 // NOTE: jmpAddrReg will be loaded by a different instruction generated
1892 // by the final code generator, so we just mark the CALL instruction
1893 // as computing that value.
1894 // The retAddrReg is actually computed by the CALL instruction.
1895 //
1896 jmpAddrReg->addMachineInstruction(mvec[0]);
1897 retAddrReg->addMachineInstruction(mvec[0]);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001898
1899 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1900 break;
1901 }
1902
1903 case 62: // reg: Shl(reg, reg)
1904 opType = subtreeRoot->leftChild()->getValue()->getType();
1905 assert(opType->isIntegral() || opType == Type::BoolTy);
1906 mvec[0] = new MachineInstr((opType == Type::LongTy)? SLLX : SLL);
Vikram S. Adve4f231662001-07-28 04:15:15 +00001907 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001908 break;
1909
1910 case 63: // reg: Shr(reg, reg)
1911 opType = subtreeRoot->leftChild()->getValue()->getType();
1912 assert(opType->isIntegral() || opType == Type::BoolTy);
1913 mvec[0] = new MachineInstr((opType->isSigned()
1914 ? ((opType == Type::LongTy)? SRAX : SRA)
1915 : ((opType == Type::LongTy)? SRLX : SRL)));
Vikram S. Adve4f231662001-07-28 04:15:15 +00001916 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001917 break;
1918
Vikram S. Adve74f4a132001-07-31 21:46:57 +00001919 case 64: // reg: Phi(reg,reg)
1920 { // This instruction has variable #operands, so resultPos is 0.
1921 Instruction* phi = subtreeRoot->getInstruction();
1922 mvec[0] = new MachineInstr(PHI, 1 + phi->getNumOperands());
1923 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adved4228a52001-08-28 23:12:57 +00001924 subtreeRoot->getValue());
Vikram S. Adve74f4a132001-07-31 21:46:57 +00001925 for (unsigned i=0, N=phi->getNumOperands(); i < N; i++)
1926 mvec[0]->SetMachineOperand(i+1, MachineOperand::MO_VirtualRegister,
1927 phi->getOperand(i));
1928 break;
1929 }
Vikram S. Advea21cf202001-07-21 12:42:19 +00001930 case 71: // reg: VReg
1931 case 72: // reg: Constant
Vikram S. Adve4f231662001-07-28 04:15:15 +00001932 numInstr = 0; // don't forward the value
Vikram S. Advea21cf202001-07-21 12:42:19 +00001933 break;
1934
1935 case 111: // stmt: reg
1936 case 112: // stmt: boolconst
1937 case 113: // stmt: bool
1938 case 121:
1939 case 122:
1940 case 123:
1941 case 124:
1942 case 125:
1943 case 126:
1944 case 127:
1945 case 128:
1946 case 129:
1947 case 130:
1948 case 131:
1949 case 132:
1950 case 153:
Vikram S. Adve74f4a132001-07-31 21:46:57 +00001951 case 155:
Vikram S. Advea21cf202001-07-21 12:42:19 +00001952 //
1953 // These are all chain rules, which have a single nonterminal on the RHS.
1954 // Get the rule that matches the RHS non-terminal and use that instead.
1955 //
1956 assert(ThisIsAChainRule(ruleForNode));
1957 assert(nts[0] && ! nts[1]
1958 && "A chain rule should have only one RHS non-terminal!");
1959 nextRule = burm_rule(subtreeRoot->getBasicNode()->state, nts[0]);
1960 nts = burm_nts[nextRule];
Vikram S. Adve4f231662001-07-28 04:15:15 +00001961 numInstr = GetInstructionsByRule(subtreeRoot, nextRule, nts,target,mvec);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001962 break;
1963
1964 default:
Vikram S. Adve4f231662001-07-28 04:15:15 +00001965 assert(0 && "Unrecognized BURG rule");
Vikram S. Advea21cf202001-07-21 12:42:19 +00001966 numInstr = 0;
1967 break;
1968 }
1969
Vikram S. Adve4f231662001-07-28 04:15:15 +00001970 if (forwardOperandNum >= 0)
1971 { // We did not generate a machine instruction but need to use operand.
1972 // If user is in the same tree, replace Value in its machine operand.
1973 // If not, insert a copy instruction which should get coalesced away
1974 // by register allocation.
1975 if (subtreeRoot->parent() != NULL)
1976 ForwardOperand(subtreeRoot, (InstructionNode*) subtreeRoot->parent(),
1977 forwardOperandNum);
1978 else
1979 {
1980 int n = numInstr++;
1981 mvec[n] = new MachineInstr(ADD);
1982 mvec[n]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1983 subtreeRoot->getInstruction()->getOperand(forwardOperandNum));
1984 mvec[n]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
1985 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1986 subtreeRoot->getInstruction());
1987 }
1988 }
1989
1990 if (! ThisIsAChainRule(ruleForNode))
1991 numInstr = FixConstantOperands(subtreeRoot, mvec, numInstr, target);
Vikram S. Advea21cf202001-07-21 12:42:19 +00001992
1993 return numInstr;
1994}
1995
1996