2 //***************************************************************************
4 // SparcInstrSelection.cpp
9 // 7/02/01 - Vikram Adve - Created
10 //**************************************************************************/
12 #include "SparcInternals.h"
13 #include "llvm/CodeGen/MachineInstr.h"
14 #include "llvm/CodeGen/InstrForest.h"
15 #include "llvm/CodeGen/InstrSelection.h"
16 #include "llvm/Support/MathExtras.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/iTerminators.h"
19 #include "llvm/iMemory.h"
20 #include "llvm/iOther.h"
21 #include "llvm/BasicBlock.h"
22 #include "llvm/Method.h"
23 #include "llvm/ConstPoolVals.h"
26 //******************** Internal Data Declarations ************************/
29 struct BranchPattern {
30 bool flipCondition; // should the sense of the test be reversed
31 BasicBlock* targetBB; // which basic block to branch to
32 MachineInstr* extraBranch; // if neither branch is fall-through, then this
33 // BA must be inserted after the cond'l one
36 //************************* Forward Declarations ***************************/
39 static MachineOpCode ChooseBprInstruction (const InstructionNode* instrNode);
41 static MachineOpCode ChooseBccInstruction (const InstructionNode* instrNode,
44 static MachineOpCode ChooseBpccInstruction (const InstructionNode* instrNode,
45 const BinaryOperator* setCCInst);
47 static MachineOpCode ChooseBFpccInstruction (const InstructionNode* instrNode,
48 const BinaryOperator* setCCInst);
50 static MachineOpCode ChooseMovFpccInstruction(const InstructionNode*);
52 static MachineOpCode ChooseMovpccAfterSub (const InstructionNode* instrNode,
56 static MachineOpCode ChooseConvertToFloatInstr(const InstructionNode*,
59 static MachineOpCode ChooseConvertToIntInstr(const InstructionNode* instrNode,
62 static MachineOpCode ChooseAddInstruction (const InstructionNode* instrNode);
64 static MachineOpCode ChooseSubInstruction (const InstructionNode* instrNode);
66 static MachineOpCode ChooseFcmpInstruction (const InstructionNode* instrNode);
68 static MachineOpCode ChooseMulInstruction (const InstructionNode* instrNode,
71 static MachineOpCode ChooseDivInstruction (const InstructionNode* instrNode);
73 static MachineOpCode ChooseLoadInstruction (const Type* resultType);
75 static MachineOpCode ChooseStoreInstruction (const Type* valueType);
77 static void SetOperandsForMemInstr(MachineInstr* minstr,
78 const InstructionNode* vmInstrNode,
79 const TargetMachine& target);
81 static void SetMemOperands_Internal (MachineInstr* minstr,
82 const InstructionNode* vmInstrNode,
84 Value* arrayOffsetVal,
85 const vector<ConstPoolVal*>& idxVec,
86 const TargetMachine& target);
88 static unsigned FixConstantOperands(const InstructionNode* vmInstrNode,
91 TargetMachine& target);
93 static MachineInstr* MakeLoadConstInstr(Instruction* vmInstr,
95 TmpInstruction*& tmpReg,
96 MachineInstr*& getMinstr2);
98 static void ForwardOperand (InstructionNode* treeNode,
99 InstrTreeNode* parent,
103 //************************ Internal Functions ******************************/
105 // Convenience function to get the value of an integer constant, for an
106 // appropriate integer or non-integer type that can be held in an integer.
107 // The type of the argument must be the following:
108 // Signed or unsigned integer
112 // isValidConstant is set to true if a valid constant was found.
115 GetConstantValueAsSignedInt(const Value *V,
116 bool &isValidConstant)
118 if (!V->isConstant()) { isValidConstant = false; return 0; }
119 isValidConstant = true;
121 if (V->getType() == Type::BoolTy)
122 return ((ConstPoolBool*)V)->getValue();
123 if (V->getType()->isIntegral()) {
124 if (V->getType()->isSigned())
125 return ((ConstPoolSInt*)V)->getValue();
127 assert(V->getType()->isUnsigned());
128 uint64_t Val = ((ConstPoolUInt*)V)->getValue();
130 if (Val < INT64_MAX) // then safe to cast to signed
134 isValidConstant = false;
140 //------------------------------------------------------------------------
141 // External Function: ThisIsAChainRule
144 // Check if a given BURG rule is a chain rule.
145 //------------------------------------------------------------------------
148 ThisIsAChainRule(int eruleno)
152 case 111: // stmt: reg
153 case 112: // stmt: boolconst
154 case 113: // stmt: bool
185 static inline MachineOpCode
186 ChooseBprInstruction(const InstructionNode* instrNode)
188 MachineOpCode opCode;
190 Instruction* setCCInstr =
191 ((InstructionNode*) instrNode->leftChild())->getInstruction();
193 switch(setCCInstr->getOpcode())
195 case Instruction::SetEQ: opCode = BRZ; break;
196 case Instruction::SetNE: opCode = BRNZ; break;
197 case Instruction::SetLE: opCode = BRLEZ; break;
198 case Instruction::SetGE: opCode = BRGEZ; break;
199 case Instruction::SetLT: opCode = BRLZ; break;
200 case Instruction::SetGT: opCode = BRGZ; break;
202 assert(0 && "Unrecognized VM instruction!");
203 opCode = INVALID_OPCODE;
211 static inline MachineOpCode
212 ChooseBccInstruction(const InstructionNode* instrNode,
215 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
216 BinaryOperator* setCCInstr = (BinaryOperator*) setCCNode->getInstruction();
217 const Type* setCCType = setCCInstr->getOperand(0)->getType();
219 isFPBranch = (setCCType == Type::FloatTy || setCCType == Type::DoubleTy);
222 return ChooseBFpccInstruction(instrNode, setCCInstr);
224 return ChooseBpccInstruction(instrNode, setCCInstr);
228 static inline MachineOpCode
229 ChooseBpccInstruction(const InstructionNode* instrNode,
230 const BinaryOperator* setCCInstr)
232 MachineOpCode opCode = INVALID_OPCODE;
234 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
238 switch(setCCInstr->getOpcode())
240 case Instruction::SetEQ: opCode = BE; break;
241 case Instruction::SetNE: opCode = BNE; break;
242 case Instruction::SetLE: opCode = BLE; break;
243 case Instruction::SetGE: opCode = BGE; break;
244 case Instruction::SetLT: opCode = BL; break;
245 case Instruction::SetGT: opCode = BG; break;
247 assert(0 && "Unrecognized VM instruction!");
253 switch(setCCInstr->getOpcode())
255 case Instruction::SetEQ: opCode = BE; break;
256 case Instruction::SetNE: opCode = BNE; break;
257 case Instruction::SetLE: opCode = BLEU; break;
258 case Instruction::SetGE: opCode = BCC; break;
259 case Instruction::SetLT: opCode = BCS; break;
260 case Instruction::SetGT: opCode = BGU; break;
262 assert(0 && "Unrecognized VM instruction!");
270 static inline MachineOpCode
271 ChooseBFpccInstruction(const InstructionNode* instrNode,
272 const BinaryOperator* setCCInstr)
274 MachineOpCode opCode = INVALID_OPCODE;
276 switch(setCCInstr->getOpcode())
278 case Instruction::SetEQ: opCode = FBE; break;
279 case Instruction::SetNE: opCode = FBNE; break;
280 case Instruction::SetLE: opCode = FBLE; break;
281 case Instruction::SetGE: opCode = FBGE; break;
282 case Instruction::SetLT: opCode = FBL; break;
283 case Instruction::SetGT: opCode = FBG; break;
285 assert(0 && "Unrecognized VM instruction!");
293 static inline MachineOpCode
294 ChooseMovFpccInstruction(const InstructionNode* instrNode)
296 MachineOpCode opCode = INVALID_OPCODE;
298 switch(instrNode->getInstruction()->getOpcode())
300 case Instruction::SetEQ: opCode = MOVFE; break;
301 case Instruction::SetNE: opCode = MOVFNE; break;
302 case Instruction::SetLE: opCode = MOVFLE; break;
303 case Instruction::SetGE: opCode = MOVFGE; break;
304 case Instruction::SetLT: opCode = MOVFL; break;
305 case Instruction::SetGT: opCode = MOVFG; break;
307 assert(0 && "Unrecognized VM instruction!");
315 // Assumes that SUBcc v1, v2 -> v3 has been executed.
316 // In most cases, we want to clear v3 and then follow it by instruction
318 // Set mustClearReg=false if v3 need not be cleared before conditional move.
319 // Set valueToMove=0 if we want to conditionally move 0 instead of 1
320 // (i.e., we want to test inverse of a condition)
321 // (The latter two cases do not seem to arise because SetNE needs nothing.)
324 ChooseMovpccAfterSub(const InstructionNode* instrNode,
328 MachineOpCode opCode = INVALID_OPCODE;
332 switch(instrNode->getInstruction()->getOpcode())
334 case Instruction::SetEQ: opCode = MOVE; break;
335 case Instruction::SetLE: opCode = MOVLE; break;
336 case Instruction::SetGE: opCode = MOVGE; break;
337 case Instruction::SetLT: opCode = MOVL; break;
338 case Instruction::SetGT: opCode = MOVG; break;
339 case Instruction::SetNE: assert(0 && "No move required!"); break;
340 default: assert(0 && "Unrecognized VM instr!"); break;
347 static inline MachineOpCode
348 ChooseConvertToFloatInstr(const InstructionNode* instrNode,
351 MachineOpCode opCode = INVALID_OPCODE;
353 switch(instrNode->getOpLabel())
356 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
358 else if (opType == Type::LongTy)
360 else if (opType == Type::DoubleTy)
362 else if (opType == Type::FloatTy)
365 assert(0 && "Cannot convert this type to FLOAT on SPARC");
369 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
371 else if (opType == Type::LongTy)
373 else if (opType == Type::FloatTy)
375 else if (opType == Type::DoubleTy)
378 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
388 static inline MachineOpCode
389 ChooseConvertToIntInstr(const InstructionNode* instrNode,
392 MachineOpCode opCode = INVALID_OPCODE;;
394 int instrType = (int) instrNode->getOpLabel();
396 if (instrType == ToSByteTy || instrType == ToShortTy || instrType == ToIntTy)
398 switch (opType->getPrimitiveID())
400 case Type::FloatTyID: opCode = FSTOI; break;
401 case Type::DoubleTyID: opCode = FDTOI; break;
403 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
407 else if (instrType == ToLongTy)
409 switch (opType->getPrimitiveID())
411 case Type::FloatTyID: opCode = FSTOX; break;
412 case Type::DoubleTyID: opCode = FDTOX; break;
414 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
419 assert(0 && "Should not get here, Mo!");
425 static inline MachineOpCode
426 ChooseAddInstruction(const InstructionNode* instrNode)
428 MachineOpCode opCode = INVALID_OPCODE;
430 const Type* resultType = instrNode->getInstruction()->getType();
432 if (resultType->isIntegral() ||
433 resultType->isPointerType() ||
434 resultType->isMethodType() ||
435 resultType->isLabelType())
441 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
442 switch(operand->getType()->getPrimitiveID())
444 case Type::FloatTyID: opCode = FADDS; break;
445 case Type::DoubleTyID: opCode = FADDD; break;
446 default: assert(0 && "Invalid type for ADD instruction"); break;
454 static inline MachineInstr*
455 CreateMovFloatInstruction(const InstructionNode* instrNode,
456 const Type* resultType)
458 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
460 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
461 instrNode->leftChild()->getValue());
462 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
463 instrNode->getValue());
467 static inline MachineInstr*
468 CreateAddConstInstruction(const InstructionNode* instrNode)
470 MachineInstr* minstr = NULL;
472 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
473 assert(constOp->isConstant());
475 // Cases worth optimizing are:
476 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
477 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
479 const Type* resultType = instrNode->getInstruction()->getType();
481 if (resultType == Type::FloatTy || resultType == Type::DoubleTy) {
482 double dval = ((ConstPoolFP*) constOp)->getValue();
484 minstr = CreateMovFloatInstruction(instrNode, resultType);
491 static inline MachineOpCode
492 ChooseSubInstruction(const InstructionNode* instrNode)
494 MachineOpCode opCode = INVALID_OPCODE;
496 const Type* resultType = instrNode->getInstruction()->getType();
498 if (resultType->isIntegral() ||
499 resultType->isPointerType())
505 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
506 switch(operand->getType()->getPrimitiveID())
508 case Type::FloatTyID: opCode = FSUBS; break;
509 case Type::DoubleTyID: opCode = FSUBD; break;
510 default: assert(0 && "Invalid type for SUB instruction"); break;
518 static inline MachineInstr*
519 CreateSubConstInstruction(const InstructionNode* instrNode)
521 MachineInstr* minstr = NULL;
523 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
524 assert(constOp->isConstant());
526 // Cases worth optimizing are:
527 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
528 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
530 const Type* resultType = instrNode->getInstruction()->getType();
532 if (resultType == Type::FloatTy ||
533 resultType == Type::DoubleTy)
535 double dval = ((ConstPoolFP*) constOp)->getValue();
537 minstr = CreateMovFloatInstruction(instrNode, resultType);
544 static inline MachineOpCode
545 ChooseFcmpInstruction(const InstructionNode* instrNode)
547 MachineOpCode opCode = INVALID_OPCODE;
549 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
550 switch(operand->getType()->getPrimitiveID()) {
551 case Type::FloatTyID: opCode = FCMPS; break;
552 case Type::DoubleTyID: opCode = FCMPD; break;
553 default: assert(0 && "Invalid type for FCMP instruction"); break;
560 // Assumes that leftArg and rightArg are both cast instructions.
563 BothFloatToDouble(const InstructionNode* instrNode)
565 InstrTreeNode* leftArg = instrNode->leftChild();
566 InstrTreeNode* rightArg = instrNode->rightChild();
567 InstrTreeNode* leftArgArg = leftArg->leftChild();
568 InstrTreeNode* rightArgArg = rightArg->leftChild();
569 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
571 // Check if both arguments are floats cast to double
572 return (leftArg->getValue()->getType() == Type::DoubleTy &&
573 leftArgArg->getValue()->getType() == Type::FloatTy &&
574 rightArgArg->getValue()->getType() == Type::FloatTy);
578 static inline MachineOpCode
579 ChooseMulInstruction(const InstructionNode* instrNode,
582 MachineOpCode opCode = INVALID_OPCODE;
584 if (checkCasts && BothFloatToDouble(instrNode))
586 return opCode = FSMULD;
588 // else fall through and use the regular multiply instructions
590 const Type* resultType = instrNode->getInstruction()->getType();
592 if (resultType->isIntegral())
598 switch(instrNode->leftChild()->getValue()->getType()->getPrimitiveID())
600 case Type::FloatTyID: opCode = FMULS; break;
601 case Type::DoubleTyID: opCode = FMULD; break;
602 default: assert(0 && "Invalid type for MUL instruction"); break;
610 static inline MachineInstr*
611 CreateIntNegInstruction(Value* vreg)
613 MachineInstr* minstr = new MachineInstr(SUB);
614 minstr->SetMachineOperand(0, /*regNum %g0*/(unsigned int) 0);
615 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, vreg);
616 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, vreg);
621 static inline MachineInstr*
622 CreateMulConstInstruction(const InstructionNode* instrNode,
623 MachineInstr*& getMinstr2)
625 MachineInstr* minstr = NULL;
627 bool needNeg = false;
629 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
630 assert(constOp->isConstant());
632 // Cases worth optimizing are:
633 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
634 // (2) Multiply by 2^x for integer types: replace with Shift
636 const Type* resultType = instrNode->getInstruction()->getType();
638 if (resultType->isIntegral() || resultType->isPointerType())
642 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
645 bool needNeg = false;
652 if (C == 0 || C == 1)
654 minstr = new MachineInstr(ADD);
657 minstr->SetMachineOperand(0, /*regNum %g0*/ (unsigned int) 0);
659 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
660 instrNode->leftChild()->getValue());
661 minstr->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
663 else if (IsPowerOf2(C, pow))
665 minstr = new MachineInstr((resultType == Type::LongTy)
667 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
668 instrNode->leftChild()->getValue());
669 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
673 if (minstr && needNeg)
674 { // insert <reg = SUB 0, reg> after the instr to flip the sign
675 getMinstr2 = CreateIntNegInstruction(instrNode->getValue());
681 if (resultType == Type::FloatTy ||
682 resultType == Type::DoubleTy)
685 double dval = ((ConstPoolFP*) constOp)->getValue();
691 minstr = new MachineInstr((resultType == Type::FloatTy)
693 minstr->SetMachineOperand(0, /*regNum %g0*/(unsigned int) 0);
695 else if (fabs(dval) == 1)
697 bool needNeg = (dval < 0);
699 MachineOpCode opCode = needNeg
700 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
701 : (resultType == Type::FloatTy? FMOVS : FMOVD);
703 minstr = new MachineInstr(opCode);
704 minstr->SetMachineOperand(0,
705 MachineOperand::MO_VirtualRegister,
706 instrNode->leftChild()->getValue());
713 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
714 instrNode->getValue());
720 static inline MachineOpCode
721 ChooseDivInstruction(const InstructionNode* instrNode)
723 MachineOpCode opCode = INVALID_OPCODE;
725 const Type* resultType = instrNode->getInstruction()->getType();
727 if (resultType->isIntegral())
729 opCode = resultType->isSigned()? SDIVX : UDIVX;
733 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
734 switch(operand->getType()->getPrimitiveID())
736 case Type::FloatTyID: opCode = FDIVS; break;
737 case Type::DoubleTyID: opCode = FDIVD; break;
738 default: assert(0 && "Invalid type for DIV instruction"); break;
746 static inline MachineInstr*
747 CreateDivConstInstruction(const InstructionNode* instrNode,
748 MachineInstr*& getMinstr2)
750 MachineInstr* minstr = NULL;
753 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
754 assert(constOp->isConstant());
756 // Cases worth optimizing are:
757 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
758 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
760 const Type* resultType = instrNode->getInstruction()->getType();
762 if (resultType->isIntegral())
766 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
769 bool needNeg = false;
778 minstr = new MachineInstr(ADD);
779 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
780 instrNode->leftChild()->getValue());
781 minstr->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
783 else if (IsPowerOf2(C, pow))
785 MachineOpCode opCode= ((resultType->isSigned())
786 ? (resultType==Type::LongTy)? SRAX : SRA
787 : (resultType==Type::LongTy)? SRLX : SRL);
788 minstr = new MachineInstr(opCode);
789 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
790 instrNode->leftChild()->getValue());
791 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
795 if (minstr && needNeg)
796 { // insert <reg = SUB 0, reg> after the instr to flip the sign
797 getMinstr2 = CreateIntNegInstruction(instrNode->getValue());
803 if (resultType == Type::FloatTy ||
804 resultType == Type::DoubleTy)
807 double dval = ((ConstPoolFP*) constOp)->getValue();
809 if (isValidConst && fabs(dval) == 1)
811 bool needNeg = (dval < 0);
813 MachineOpCode opCode = needNeg
814 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
815 : (resultType == Type::FloatTy? FMOVS : FMOVD);
817 minstr = new MachineInstr(opCode);
818 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
819 instrNode->leftChild()->getValue());
825 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
826 instrNode->getValue());
832 static inline MachineOpCode ChooseLoadInstruction(const Type *DestTy) {
833 switch (DestTy->getPrimitiveID()) {
835 case Type::UByteTyID: return LDUB;
836 case Type::SByteTyID: return LDSB;
837 case Type::UShortTyID: return LDUH;
838 case Type::ShortTyID: return LDSH;
839 case Type::UIntTyID: return LDUW;
840 case Type::IntTyID: return LDSW;
841 case Type::PointerTyID:
842 case Type::ULongTyID:
843 case Type::LongTyID: return LDX;
844 case Type::FloatTyID: return LD;
845 case Type::DoubleTyID: return LDD;
846 default: assert(0 && "Invalid type for Load instruction");
853 static inline MachineOpCode ChooseStoreInstruction(const Type *DestTy) {
854 switch (DestTy->getPrimitiveID()) {
856 case Type::UByteTyID:
857 case Type::SByteTyID: return STB;
858 case Type::UShortTyID:
859 case Type::ShortTyID: return STH;
861 case Type::IntTyID: return STW;
862 case Type::PointerTyID:
863 case Type::ULongTyID:
864 case Type::LongTyID: return STX;
865 case Type::FloatTyID: return ST;
866 case Type::DoubleTyID: return STD;
867 default: assert(0 && "Invalid type for Store instruction");
874 //------------------------------------------------------------------------
875 // Function SetOperandsForMemInstr
877 // Choose addressing mode for the given load or store instruction.
878 // Use [reg+reg] if it is an indexed reference, and the index offset is
879 // not a constant or if it cannot fit in the offset field.
880 // Use [reg+offset] in all other cases.
882 // This assumes that all array refs are "lowered" to one of these forms:
883 // %x = load (subarray*) ptr, constant ; single constant offset
884 // %x = load (subarray*) ptr, offsetVal ; single non-constant offset
885 // Generally, this should happen via strength reduction + LICM.
886 // Also, strength reduction should take care of using the same register for
887 // the loop index variable and an array index, when that is profitable.
888 //------------------------------------------------------------------------
891 SetOperandsForMemInstr(MachineInstr* minstr,
892 const InstructionNode* vmInstrNode,
893 const TargetMachine& target)
895 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
897 // Variables to hold the index vector, ptr value, and offset value.
898 // The major work here is to extract these for all 3 instruction types
899 // and then call the common function SetMemOperands_Internal().
901 const vector<ConstPoolVal*>* idxVec = & memInst->getIndexVec();
902 vector<ConstPoolVal*>* newIdxVec = NULL;
904 Value* arrayOffsetVal = NULL;
906 // Test if a GetElemPtr instruction is being folded into this mem instrn.
907 // If so, it will be in the left child for Load and GetElemPtr,
908 // and in the right child for Store instructions.
910 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
911 ? vmInstrNode->rightChild()
912 : vmInstrNode->leftChild());
914 if (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
915 ptrChild->getOpLabel() == GetElemPtrIdx)
917 // There is a GetElemPtr instruction and there may be a chain of
918 // more than one. Use the pointer value of the last one in the chain.
919 // Fold the index vectors from the entire chain and from the mem
920 // instruction into one single index vector.
921 // Finally, we never fold for an array instruction so make that NULL.
923 newIdxVec = new vector<ConstPoolVal*>;
924 ptrVal = FoldGetElemChain((InstructionNode*) ptrChild, *newIdxVec);
926 newIdxVec->insert(newIdxVec->end(), idxVec->begin(), idxVec->end());
929 assert(! ((PointerType*)ptrVal->getType())->getValueType()->isArrayType()
930 && "GetElemPtr cannot be folded into array refs in selection");
934 // There is no GetElemPtr instruction.
935 // Use the pointer value and the index vector from the Mem instruction.
936 // If it is an array reference, get the array offset value.
938 ptrVal = memInst->getPtrOperand();
941 ((const PointerType*) ptrVal->getType())->getValueType();
942 if (opType->isArrayType())
944 assert((memInst->getNumOperands()
945 == (unsigned) 1 + memInst->getFirstOffsetIdx())
946 && "Array refs must be lowered before Instruction Selection");
948 arrayOffsetVal = memInst->getOperand(memInst->getFirstOffsetIdx());
952 SetMemOperands_Internal(minstr, vmInstrNode, ptrVal, arrayOffsetVal,
955 if (newIdxVec != NULL)
961 SetMemOperands_Internal(MachineInstr* minstr,
962 const InstructionNode* vmInstrNode,
964 Value* arrayOffsetVal,
965 const vector<ConstPoolVal*>& idxVec,
966 const TargetMachine& target)
968 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
970 // Initialize so we default to storing the offset in a register.
971 int64_t smallConstOffset;
972 Value* valueForRegOffset = NULL;
973 MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister;
975 // Check if there is an index vector and if so, if it translates to
976 // a small enough constant to fit in the immediate-offset field.
978 if (idxVec.size() > 0)
980 bool isConstantOffset = false;
983 const PointerType* ptrType = (PointerType*) ptrVal->getType();
985 if (ptrType->getValueType()->isStructType())
987 // the offset is always constant for structs
988 isConstantOffset = true;
990 // Compute the offset value using the index vector
991 offset = target.DataLayout.getIndexedOffset(ptrType, idxVec);
995 // It must be an array ref. Check if the offset is a constant,
996 // and that the indexing has been lowered to a single offset.
998 assert(ptrType->getValueType()->isArrayType());
999 assert(arrayOffsetVal != NULL
1000 && "Expect to be given Value* for array offsets");
1002 if (ConstPoolVal *CPV = arrayOffsetVal->castConstant())
1004 isConstantOffset = true; // always constant for structs
1005 assert(arrayOffsetVal->getType()->isIntegral());
1006 offset = (CPV->getType()->isSigned()
1007 ? ((ConstPoolSInt*)CPV)->getValue()
1008 : (int64_t) ((ConstPoolUInt*)CPV)->getValue());
1012 valueForRegOffset = arrayOffsetVal;
1016 if (isConstantOffset)
1018 // create a virtual register for the constant
1019 valueForRegOffset = ConstPoolSInt::get(Type::IntTy, offset);
1024 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1025 smallConstOffset = 0;
1028 // Operand 0 is value for STORE, ptr for LOAD or GET_ELEMENT_PTR
1029 // It is the left child in the instruction tree in all cases.
1030 Value* leftVal = vmInstrNode->leftChild()->getValue();
1031 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, leftVal);
1033 // Operand 1 is ptr for STORE, offset for LOAD or GET_ELEMENT_PTR
1034 // Operand 3 is offset for STORE, result reg for LOAD or GET_ELEMENT_PTR
1036 unsigned offsetOpNum = (memInst->getOpcode() == Instruction::Store)? 2 : 1;
1037 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1039 assert(valueForRegOffset != NULL);
1040 minstr->SetMachineOperand(offsetOpNum, offsetOpType, valueForRegOffset);
1043 minstr->SetMachineOperand(offsetOpNum, offsetOpType, smallConstOffset);
1045 if (memInst->getOpcode() == Instruction::Store)
1046 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, ptrVal);
1048 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1049 vmInstrNode->getValue());
1053 // Special handling for constant operands:
1054 // -- if the constant is 0, use the hardwired 0 register, if any;
1055 // -- if the constant is of float or double type but has an integer value,
1056 // use int-to-float conversion instruction instead of generating a load;
1057 // -- if the constant fits in the IMMEDIATE field, use that field;
1058 // -- else insert instructions to put the constant into a register, either
1059 // directly or by loading explicitly from the constant pool.
1062 FixConstantOperands(const InstructionNode* vmInstrNode,
1063 MachineInstr** mvec,
1065 TargetMachine& target)
1067 static MachineInstr* loadConstVec[MAX_INSTR_PER_VMINSTR];
1069 unsigned numNew = 0;
1070 Instruction* vmInstr = vmInstrNode->getInstruction();
1072 for (unsigned i=0; i < numInstr; i++)
1074 MachineInstr* minstr = mvec[i];
1075 const MachineInstrDescriptor& instrDesc =
1076 target.getInstrInfo().getDescriptor(minstr->getOpCode());
1078 for (unsigned op=0; op < minstr->getNumOperands(); op++)
1080 const MachineOperand& mop = minstr->getOperand(op);
1082 // skip the result position (for efficiency below) and any other
1083 // positions already marked as not a virtual register
1084 if (instrDesc.resultPos == (int) op ||
1085 mop.getOperandType() != MachineOperand::MO_VirtualRegister ||
1086 mop.getVRegValue() == NULL)
1091 Value* opValue = mop.getVRegValue();
1093 if (opValue->isConstant())
1095 unsigned int machineRegNum;
1097 MachineOperand::MachineOperandType opType =
1098 ChooseRegOrImmed(opValue, minstr->getOpCode(), target,
1099 /*canUseImmed*/ (op == 1),
1100 machineRegNum, immedValue);
1102 if (opType == MachineOperand::MO_MachineRegister)
1103 minstr->SetMachineOperand(op, machineRegNum);
1104 else if (opType == MachineOperand::MO_VirtualRegister)
1106 // value is constant and must be loaded into a register
1107 TmpInstruction* tmpReg;
1108 MachineInstr* minstr2;
1109 loadConstVec[numNew++] = MakeLoadConstInstr(vmInstr, opValue,
1111 minstr->SetMachineOperand(op, opType, tmpReg);
1112 if (minstr2 != NULL)
1113 loadConstVec[numNew++] = minstr2;
1116 minstr->SetMachineOperand(op, opType, immedValue);
1123 // Insert the new instructions *before* the old ones by moving
1124 // the old ones over `numNew' positions (last-to-first, of course!).
1125 // We do check *after* returning that we did not exceed the vector mvec.
1126 for (int i=numInstr-1; i >= 0; i--)
1127 mvec[i+numNew] = mvec[i];
1129 for (unsigned i=0; i < numNew; i++)
1130 mvec[i] = loadConstVec[i];
1133 return (numInstr + numNew);
1137 static inline MachineInstr*
1138 MakeIntSetInstruction(int64_t C, bool isSigned, Value* dest)
1140 MachineInstr* minstr;
1143 minstr = new MachineInstr(SETSW);
1144 minstr->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, C);
1148 minstr = new MachineInstr(SETUW);
1149 minstr->SetMachineOperand(0, MachineOperand::MO_UnextendedImmed, C);
1152 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, dest);
1158 static MachineInstr*
1159 MakeLoadConstInstr(Instruction* vmInstr,
1161 TmpInstruction*& tmpReg,
1162 MachineInstr*& getMinstr2)
1164 assert(val->isConstant());
1166 MachineInstr* minstr;
1170 // Create a TmpInstruction to mark the hidden register used for the constant
1171 tmpReg = new TmpInstruction(Instruction::UserOp1, val, NULL);
1172 vmInstr->getMachineInstrVec().addTempValue(tmpReg);
1174 // Use a "set" instruction for known constants that can go in an integer reg.
1175 // Use a "set" instruction followed by a int-to-float conversion for known
1176 // constants that must go in a floating point reg but have an integer value.
1177 // Use a "load" instruction for all other constants, in particular,
1178 // floating point constants.
1180 const Type* valType = val->getType();
1182 if (valType->isIntegral() ||
1183 valType->isPointerType() ||
1184 valType == Type::BoolTy)
1186 bool isValidConstant;
1187 int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
1188 assert(isValidConstant && "Unrecognized constant");
1190 minstr = MakeIntSetInstruction(C, valType->isSigned(), tmpReg);
1194 assert(valType == Type::FloatTy || valType == Type::DoubleTy);
1195 double dval = ((ConstPoolFP*) val)->getValue();
1196 if (dval == (int64_t) dval)
1198 // The constant actually has an integer value, so use a
1199 // [set; int-to-float] sequence instead of a load instruction.
1201 TmpInstruction* tmpReg2 = NULL;
1203 { // First, create an integer constant of the same value as dval
1204 ConstPoolSInt* ival = ConstPoolSInt::get(Type::IntTy,
1206 // Create another TmpInstruction for the hidden integer register
1207 TmpInstruction* tmpReg2 =
1208 new TmpInstruction(Instruction::UserOp1, ival, NULL);
1209 vmInstr->getMachineInstrVec().addTempValue(tmpReg2);
1211 // Create the `SET' instruction
1212 minstr = MakeIntSetInstruction((int64_t)dval, true, tmpReg2);
1215 // In which variable do we put the second instruction?
1216 MachineInstr*& instr2 = (minstr)? getMinstr2 : minstr;
1218 // Create the int-to-float instruction
1219 instr2 = new MachineInstr(valType == Type::FloatTy? FITOS : FITOD);
1222 instr2->SetMachineOperand(0, /*regNum %g0*/ (unsigned int) 0);
1224 instr2->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
1227 instr2->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1232 // Make a Load instruction, and make `val' both the ptr value *and*
1233 // the result value, and set the offset field to 0. Final code
1234 // generation will have to generate the base+offset for the constant.
1236 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
1237 minstr = new MachineInstr(ChooseLoadInstruction(val->getType()));
1238 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,val);
1239 minstr->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1241 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1246 tmpReg->addMachineInstruction(minstr);
1253 // Substitute operand `operandNum' of the instruction in node `treeNode'
1254 // in place the use(s) of that instruction in node `parent'.
1257 ForwardOperand(InstructionNode* treeNode,
1258 InstrTreeNode* parent,
1261 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1263 Instruction* unusedOp = treeNode->getInstruction();
1264 Value* fwdOp = unusedOp->getOperand(operandNum);
1266 // The parent itself may be a list node, so find the real parent instruction
1267 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1269 parent = parent->parent();
1270 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1272 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1274 Instruction* userInstr = parentInstrNode->getInstruction();
1275 MachineCodeForVMInstr& mvec = userInstr->getMachineInstrVec();
1276 for (unsigned i=0, N=mvec.size(); i < N; i++)
1278 MachineInstr* minstr = mvec[i];
1279 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; i++)
1281 const MachineOperand& mop = minstr->getOperand(i);
1282 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1283 mop.getVRegValue() == unusedOp)
1285 minstr->SetMachineOperand(i, MachineOperand::MO_VirtualRegister,
1293 // This function is currently unused and incomplete but will be
1294 // used if we have a linear layout of basic blocks in LLVM code.
1295 // It decides which branch should fall-through, and whether an
1296 // extra unconditional branch is needed (when neither falls through).
1299 ChooseBranchPattern(Instruction* vmInstr, BranchPattern& brPattern)
1301 BranchInst* brInstr = (BranchInst*) vmInstr;
1303 brPattern.flipCondition = false;
1304 brPattern.targetBB = brInstr->getSuccessor(0);
1305 brPattern.extraBranch = NULL;
1307 assert(brInstr->getNumSuccessors() > 1 &&
1308 "Unnecessary analysis for unconditional branch");
1310 assert(0 && "Fold branches in peephole optimization");
1314 //******************* Externally Visible Functions *************************/
1317 //------------------------------------------------------------------------
1318 // External Function: GetInstructionsByRule
1321 // Choose machine instructions for the SPARC according to the
1322 // patterns chosen by the BURG-generated parser.
1323 //------------------------------------------------------------------------
1326 GetInstructionsByRule(InstructionNode* subtreeRoot,
1329 TargetMachine &target,
1330 MachineInstr** mvec)
1332 int numInstr = 1; // initialize for common case
1333 bool checkCast = false; // initialize here to use fall-through
1334 Value *leftVal, *rightVal;
1337 int forwardOperandNum = -1;
1338 int64_t s0 = 0; // variables holding zero to avoid
1339 uint64_t u0 = 0; // overloading ambiguities below
1341 mvec[0] = mvec[1] = mvec[2] = mvec[3] = NULL; // just for safety
1343 switch(ruleForNode) {
1344 case 1: // stmt: Ret
1345 case 2: // stmt: RetValue(reg)
1346 // NOTE: Prepass of register allocation is responsible
1347 // for moving return value to appropriate register.
1348 // Mark the return-address register as a hidden virtual reg.
1350 Instruction* returnReg = new TmpInstruction(Instruction::UserOp1,
1351 subtreeRoot->getInstruction(), NULL);
1352 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(returnReg);
1354 mvec[0] = new MachineInstr(RETURN);
1355 mvec[0]->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,returnReg);
1356 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed, s0);
1358 returnReg->addMachineInstruction(mvec[0]);
1360 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1364 case 3: // stmt: Store(reg,reg)
1365 case 4: // stmt: Store(reg,ptrreg)
1366 mvec[0] = new MachineInstr(ChooseStoreInstruction(subtreeRoot->leftChild()->getValue()->getType()));
1367 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1370 case 5: // stmt: BrUncond
1371 mvec[0] = new MachineInstr(BA);
1372 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister, (Value*)NULL);
1373 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1374 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1377 mvec[numInstr++] = new MachineInstr(NOP);
1380 case 206: // stmt: BrCond(setCCconst)
1381 // setCCconst => boolean was computed with `%b = setCC type reg1 constant'
1382 // If the constant is ZERO, we can use the branch-on-integer-register
1383 // instructions and avoid the SUBcc instruction entirely.
1384 // Otherwise this is just the same as case 5, so just fall through.
1386 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1387 assert(constNode && constNode->getNodeType() ==InstrTreeNode::NTConstNode);
1388 ConstPoolVal* constVal = (ConstPoolVal*) constNode->getValue();
1391 if ((constVal->getType()->isIntegral()
1392 || constVal->getType()->isPointerType())
1393 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1396 // That constant is a zero after all...
1397 // Use the left child of the setCC instruction as the first argument!
1398 mvec[0] = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1399 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1400 subtreeRoot->leftChild()->leftChild()->getValue());
1401 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1402 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1405 mvec[numInstr++] = new MachineInstr(NOP);
1408 mvec[numInstr++] = new MachineInstr(BA);
1409 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1411 mvec[numInstr-1]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp, ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
1414 mvec[numInstr++] = new MachineInstr(NOP);
1418 // ELSE FALL THROUGH
1421 case 6: // stmt: BrCond(bool)
1422 // bool => boolean was computed with some boolean operator (setCC,Not,...)
1423 // Need to check whether the type was a FP, signed int or unsigned int,
1424 // and check the branching condition in order to choose the branch to use.
1428 mvec[0] = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
1429 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1430 subtreeRoot->leftChild()->getValue());
1431 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1432 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1435 mvec[numInstr++] = new MachineInstr(NOP);
1438 mvec[numInstr++] = new MachineInstr(BA);
1439 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1441 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp,
1442 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
1445 mvec[numInstr++] = new MachineInstr(NOP);
1449 case 8: // stmt: BrCond(boolreg)
1450 case 208: // stmt: BrCond(boolconst)
1451 // boolreg => boolean is stored in an existing register.
1452 // boolconst => boolean is a constant; can be loaded into a register.
1453 // Just use the branch-on-integer-register instruction!
1455 mvec[0] = new MachineInstr(BRNZ);
1456 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1457 subtreeRoot->leftChild()->getValue());
1458 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1459 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1462 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1465 mvec[numInstr++] = new MachineInstr(BA);
1466 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1468 mvec[numInstr-1]->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp,
1469 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
1472 mvec[numInstr++] = new MachineInstr(NOP);
1475 case 9: // stmt: Switch(reg)
1476 assert(0 && "*** SWITCH instruction is not implemented yet.");
1480 case 10: // reg: VRegList(reg, reg)
1481 assert(0 && "VRegList should never be the topmost non-chain rule");
1484 case 21: // reg: Not(reg): Implemented as reg = reg XOR-NOT 0
1485 mvec[0] = new MachineInstr(XNOR);
1486 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1487 subtreeRoot->leftChild()->getValue());
1488 mvec[0]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
1489 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1490 subtreeRoot->getValue());
1493 case 322: // reg: ToBoolTy(bool):
1494 case 22: // reg: ToBoolTy(reg):
1495 opType = subtreeRoot->leftChild()->getValue()->getType();
1496 assert(opType->isIntegral() || opType == Type::BoolTy);
1498 forwardOperandNum = 0;
1501 case 23: // reg: ToUByteTy(reg)
1502 case 25: // reg: ToUShortTy(reg)
1503 case 27: // reg: ToUIntTy(reg)
1504 case 29: // reg: ToULongTy(reg)
1505 opType = subtreeRoot->leftChild()->getValue()->getType();
1506 assert(opType->isIntegral() ||
1507 opType->isPointerType() ||
1508 opType == Type::BoolTy && "Ignoring cast: illegal for other types");
1510 forwardOperandNum = 0;
1513 case 24: // reg: ToSByteTy(reg)
1514 case 26: // reg: ToShortTy(reg)
1515 case 28: // reg: ToIntTy(reg)
1516 case 30: // reg: ToLongTy(reg)
1517 opType = subtreeRoot->leftChild()->getValue()->getType();
1518 if (opType->isIntegral() || opType == Type::BoolTy)
1521 forwardOperandNum = 0;
1525 mvec[0] =new MachineInstr(ChooseConvertToIntInstr(subtreeRoot,opType));
1526 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
1530 case 31: // reg: ToFloatTy(reg):
1531 case 32: // reg: ToDoubleTy(reg):
1533 // If this instruction has a parent (a user) in the tree
1534 // and the user is translated as an FsMULd instruction,
1535 // then the cast is unnecessary. So check that first.
1536 // In the future, we'll want to do the same for the FdMULq instruction,
1537 // so do the check here instead of only for ToFloatTy(reg).
1539 if (subtreeRoot->parent() != NULL &&
1540 ((InstructionNode*) subtreeRoot->parent())->getInstruction()->getMachineInstrVec()[0]->getOpCode() == FSMULD)
1543 forwardOperandNum = 0;
1547 opType = subtreeRoot->leftChild()->getValue()->getType();
1548 MachineOpCode opCode = ChooseConvertToFloatInstr(subtreeRoot, opType);
1549 if (opCode == INVALID_OPCODE) // no conversion needed
1552 forwardOperandNum = 0;
1556 mvec[0] = new MachineInstr(opCode);
1557 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
1562 case 19: // reg: ToArrayTy(reg):
1563 case 20: // reg: ToPointerTy(reg):
1565 forwardOperandNum = 0;
1568 case 233: // reg: Add(reg, Constant)
1569 mvec[0] = CreateAddConstInstruction(subtreeRoot);
1570 if (mvec[0] != NULL)
1572 // ELSE FALL THROUGH
1574 case 33: // reg: Add(reg, reg)
1575 mvec[0] = new MachineInstr(ChooseAddInstruction(subtreeRoot));
1576 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1579 case 234: // reg: Sub(reg, Constant)
1580 mvec[0] = CreateSubConstInstruction(subtreeRoot);
1581 if (mvec[0] != NULL)
1583 // ELSE FALL THROUGH
1585 case 34: // reg: Sub(reg, reg)
1586 mvec[0] = new MachineInstr(ChooseSubInstruction(subtreeRoot));
1587 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1590 case 135: // reg: Mul(todouble, todouble)
1594 case 35: // reg: Mul(reg, reg)
1595 mvec[0] = new MachineInstr(ChooseMulInstruction(subtreeRoot, checkCast));
1596 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1599 case 335: // reg: Mul(todouble, todoubleConst)
1603 case 235: // reg: Mul(reg, Constant)
1604 mvec[0] = CreateMulConstInstruction(subtreeRoot, mvec[1]);
1605 if (mvec[0] == NULL)
1607 mvec[0]=new MachineInstr(ChooseMulInstruction(subtreeRoot, checkCast));
1608 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1611 if (mvec[1] != NULL)
1615 case 236: // reg: Div(reg, Constant)
1616 mvec[0] = CreateDivConstInstruction(subtreeRoot, mvec[1]);
1617 if (mvec[0] != NULL)
1619 if (mvec[1] != NULL)
1623 // ELSE FALL THROUGH
1625 case 36: // reg: Div(reg, reg)
1626 mvec[0] = new MachineInstr(ChooseDivInstruction(subtreeRoot));
1627 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1630 case 37: // reg: Rem(reg, reg)
1631 case 237: // reg: Rem(reg, Constant)
1632 assert(0 && "REM instruction unimplemented for the SPARC.");
1635 case 38: // reg: And(reg, reg)
1636 case 238: // reg: And(reg, Constant)
1637 mvec[0] = new MachineInstr(AND);
1638 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1641 case 138: // reg: And(reg, not)
1642 mvec[0] = new MachineInstr(ANDN);
1643 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1646 case 39: // reg: Or(reg, reg)
1647 case 239: // reg: Or(reg, Constant)
1648 mvec[0] = new MachineInstr(ORN);
1649 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1652 case 139: // reg: Or(reg, not)
1653 mvec[0] = new MachineInstr(ORN);
1654 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1657 case 40: // reg: Xor(reg, reg)
1658 case 240: // reg: Xor(reg, Constant)
1659 mvec[0] = new MachineInstr(XOR);
1660 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1663 case 140: // reg: Xor(reg, not)
1664 mvec[0] = new MachineInstr(XNOR);
1665 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1668 case 41: // boolconst: SetCC(reg, Constant)
1669 // Check if this is an integer comparison, and
1670 // there is a parent, and the parent decided to use
1671 // a branch-on-integer-register instead of branch-on-condition-code.
1672 // If so, the SUBcc instruction is not required.
1673 // (However, we must still check for constants to be loaded from
1674 // the constant pool so that such a load can be associated with
1675 // this instruction.)
1677 // Otherwise this is just the same as case 42, so just fall through.
1679 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral() &&
1680 subtreeRoot->parent() != NULL)
1682 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1683 assert(parentNode->getNodeType() == InstrTreeNode::NTInstructionNode);
1684 const vector<MachineInstr*>&
1685 minstrVec = parentNode->getInstruction()->getMachineInstrVec();
1686 MachineOpCode parentOpCode;
1687 if (parentNode->getInstruction()->getOpcode() == Instruction::Br &&
1688 (parentOpCode = minstrVec[0]->getOpCode()) >= BRZ &&
1689 parentOpCode <= BRGEZ)
1691 numInstr = 0; // don't forward the operand!
1695 // ELSE FALL THROUGH
1697 case 42: // bool: SetCC(reg, reg):
1699 // If result of the SetCC is only used for a branch, we can
1700 // discard the result. otherwise, it must go into an integer register.
1701 // Note that the user may or may not be in the same tree, so we have
1702 // to follow SSA def-use edges here, not BURG tree edges.
1704 Instruction* result = subtreeRoot->getInstruction();
1705 Value* firstUse = result->use_empty() ? 0 : *result->use_begin();
1706 bool discardResult =
1707 (result->use_size() == 1
1708 && firstUse->isInstruction()
1709 && ((Instruction*) firstUse)->getOpcode() == Instruction::Br);
1713 MachineOpCode movOpCode;
1715 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral() ||
1716 subtreeRoot->leftChild()->getValue()->getType()->isPointerType())
1718 // Integer condition: destination should be %g0 or integer register.
1719 // If result must be saved but condition is not SetEQ then we need
1720 // a separate instruction to compute the bool result, so discard
1721 // result of SUBcc instruction anyway.
1723 mvec[0] = new MachineInstr(SUBcc);
1724 Set3OperandsFromInstr(mvec[0], subtreeRoot, target, discardResult);
1726 // mark the 4th operand as being a CC register, and a "result"
1727 mvec[0]->SetMachineOperand(3, MachineOperand::MO_CCRegister,
1728 subtreeRoot->getValue(), /*def*/ true);
1731 { // recompute bool if needed, using the integer condition codes
1732 if (result->getOpcode() == Instruction::SetNE)
1733 discardResult = true;
1736 ChooseMovpccAfterSub(subtreeRoot, mustClearReg, valueToMove);
1741 // FP condition: dest of FCMP should be some FCCn register
1742 mvec[0] = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1743 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1744 subtreeRoot->getValue());
1745 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1746 subtreeRoot->leftChild()->getValue());
1747 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1748 subtreeRoot->rightChild()->getValue());
1751 {// recompute bool using the FP condition codes
1752 mustClearReg = true;
1754 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1761 {// Unconditionally set register to 0
1763 mvec[n] = new MachineInstr(SETHI);
1764 mvec[n]->SetMachineOperand(0,MachineOperand::MO_UnextendedImmed,s0);
1765 mvec[n]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1766 subtreeRoot->getValue());
1769 // Now conditionally move `valueToMove' (0 or 1) into the register
1771 mvec[n] = new MachineInstr(movOpCode);
1772 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1773 subtreeRoot->getValue());
1774 mvec[n]->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
1776 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1777 subtreeRoot->getValue());
1782 case 43: // boolreg: VReg
1783 case 44: // boolreg: Constant
1787 case 51: // reg: Load(reg)
1788 case 52: // reg: Load(ptrreg)
1789 case 53: // reg: LoadIdx(reg,reg)
1790 case 54: // reg: LoadIdx(ptrreg,reg)
1791 mvec[0] = new MachineInstr(ChooseLoadInstruction(subtreeRoot->getValue()->getType()));
1792 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1795 case 55: // reg: GetElemPtr(reg)
1796 case 56: // reg: GetElemPtrIdx(reg,reg)
1797 if (subtreeRoot->parent() != NULL)
1799 // Check if the parent was an array access.
1800 // If so, we still need to generate this instruction.
1801 MemAccessInst* memInst =(MemAccessInst*) subtreeRoot->getInstruction();
1802 const PointerType* ptrType =
1803 (const PointerType*) memInst->getPtrOperand()->getType();
1804 if (! ptrType->getValueType()->isArrayType())
1805 {// we don't need a separate instr
1806 numInstr = 0; // don't forward operand!
1810 // else in all other cases we need to a separate ADD instruction
1811 mvec[0] = new MachineInstr(ADD);
1812 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1815 case 57: // reg: Alloca: Implement as 2 instructions:
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 int tsize = (int) target.findOptimalStorageSize(instrType->getValueType());
1822 assert(tsize != 0 && "Just to check when this can happen");
1828 //else go on to create the instructions needed...
1830 // Create a temporary Value to hold the constant type-size
1831 ConstPoolSInt* valueForTSize = ConstPoolSInt::get(Type::IntTy, tsize);
1833 // Instruction 1: sub %sp, tsize -> %sp
1834 // tsize is always constant, but it may have to be put into a
1835 // register if it doesn't fit in the immediate field.
1837 mvec[0] = new MachineInstr(SUB);
1838 mvec[0]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1839 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, valueForTSize);
1840 mvec[0]->SetMachineOperand(2, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1842 // Instruction 2: add %sp, 0 -> result
1844 mvec[1] = new MachineInstr(ADD);
1845 mvec[1]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1846 mvec[1]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
1847 mvec[1]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, instr);
1851 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1852 // mul num, typeSz -> tmp
1853 // sub %sp, tmp -> %sp
1854 { // add %sp, 0 -> result
1855 Instruction* instr = subtreeRoot->getInstruction();
1856 const PointerType* instrType = (const PointerType*) instr->getType();
1857 assert(instrType->isPointerType() &&
1858 instrType->getValueType()->isArrayType());
1859 const Type* eltType =
1860 ((ArrayType*) instrType->getValueType())->getElementType();
1861 int tsize = (int) target.findOptimalStorageSize(eltType);
1863 assert(tsize != 0 && "Just to check when this can happen");
1869 //else go on to create the instructions needed...
1871 // Create a temporary Value to hold the constant type-size
1872 ConstPoolSInt* valueForTSize = ConstPoolSInt::get(Type::IntTy, tsize);
1874 // Create a temporary value to hold `tmp'
1875 Instruction* tmpInstr = new TmpInstruction(Instruction::UserOp1,
1876 subtreeRoot->leftChild()->getValue(),
1877 NULL /*could insert tsize here*/);
1878 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(tmpInstr);
1880 // Instruction 1: mul numElements, typeSize -> tmp
1881 mvec[0] = new MachineInstr(MULX);
1882 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1883 subtreeRoot->leftChild()->getValue());
1884 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, valueForTSize);
1885 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,tmpInstr);
1887 tmpInstr->addMachineInstruction(mvec[0]);
1889 // Instruction 2: sub %sp, tmp -> %sp
1891 mvec[1] = new MachineInstr(SUB);
1892 mvec[1]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1893 mvec[1]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,tmpInstr);
1894 mvec[1]->SetMachineOperand(2, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1896 // Instruction 3: add %sp, 0 -> result
1898 mvec[2] = new MachineInstr(ADD);
1899 mvec[2]->SetMachineOperand(0, /*regNum %sp = o6 = r[14]*/(unsigned int)14);
1900 mvec[2]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
1901 mvec[2]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, instr);
1905 case 61: // reg: Call
1906 // Generate a call-indirect (i.e., JMPL) for now to expose
1907 // the potential need for registers. If an absolute address
1908 // is available, replace this with a CALL instruction.
1909 // Mark both the indirection register and the return-address
1910 { // register as hidden virtual registers.
1912 Instruction* jmpAddrReg = new TmpInstruction(Instruction::UserOp1,
1913 ((CallInst*) subtreeRoot->getInstruction())->getCalledMethod(), NULL);
1914 Instruction* retAddrReg = new TmpInstruction(Instruction::UserOp1,
1915 subtreeRoot->getValue(), NULL);
1916 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(jmpAddrReg);
1917 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(retAddrReg);
1919 mvec[0] = new MachineInstr(JMPL);
1920 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, jmpAddrReg);
1921 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1923 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, retAddrReg);
1925 // NOTE: jmpAddrReg will be loaded by a different instruction generated
1926 // by the final code generator, so we just mark the CALL instruction
1927 // as computing that value.
1928 // The retAddrReg is actually computed by the CALL instruction.
1930 jmpAddrReg->addMachineInstruction(mvec[0]);
1931 retAddrReg->addMachineInstruction(mvec[0]);
1933 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1937 case 62: // reg: Shl(reg, reg)
1938 opType = subtreeRoot->leftChild()->getValue()->getType();
1939 assert(opType->isIntegral()
1940 || opType == Type::BoolTy
1941 || opType->isPointerType() && "Shl unsupported for other types");
1942 mvec[0] = new MachineInstr((opType == Type::LongTy)? SLLX : SLL);
1943 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1946 case 63: // reg: Shr(reg, reg)
1947 opType = subtreeRoot->leftChild()->getValue()->getType();
1948 assert(opType->isIntegral()
1949 || opType == Type::BoolTy
1950 || opType->isPointerType() && "Shr unsupported for other types");
1951 mvec[0] = new MachineInstr((opType->isSigned()
1952 ? ((opType == Type::LongTy)? SRAX : SRA)
1953 : ((opType == Type::LongTy)? SRLX : SRL)));
1954 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1957 case 64: // reg: Phi(reg,reg)
1958 { // This instruction has variable #operands, so resultPos is 0.
1959 Instruction* phi = subtreeRoot->getInstruction();
1960 mvec[0] = new MachineInstr(PHI, 1 + phi->getNumOperands());
1961 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1962 subtreeRoot->getValue());
1963 for (unsigned i=0, N=phi->getNumOperands(); i < N; i++)
1964 mvec[0]->SetMachineOperand(i+1, MachineOperand::MO_VirtualRegister,
1965 phi->getOperand(i));
1968 case 71: // reg: VReg
1969 case 72: // reg: Constant
1970 numInstr = 0; // don't forward the value
1973 case 111: // stmt: reg
1974 case 112: // stmt: boolconst
1975 case 113: // stmt: bool
1999 // These are all chain rules, which have a single nonterminal on the RHS.
2000 // Get the rule that matches the RHS non-terminal and use that instead.
2002 assert(ThisIsAChainRule(ruleForNode));
2003 assert(nts[0] && ! nts[1]
2004 && "A chain rule should have only one RHS non-terminal!");
2005 nextRule = burm_rule(subtreeRoot->state, nts[0]);
2006 nts = burm_nts[nextRule];
2007 numInstr = GetInstructionsByRule(subtreeRoot, nextRule, nts,target,mvec);
2011 assert(0 && "Unrecognized BURG rule");
2016 if (forwardOperandNum >= 0)
2017 { // We did not generate a machine instruction but need to use operand.
2018 // If user is in the same tree, replace Value in its machine operand.
2019 // If not, insert a copy instruction which should get coalesced away
2020 // by register allocation.
2021 if (subtreeRoot->parent() != NULL)
2022 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
2026 mvec[n] = new MachineInstr(ADD);
2027 mvec[n]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
2028 subtreeRoot->getInstruction()->getOperand(forwardOperandNum));
2029 mvec[n]->SetMachineOperand(1, /*regNum %g0*/ (unsigned int) 0);
2030 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
2031 subtreeRoot->getInstruction());
2035 if (! ThisIsAChainRule(ruleForNode))
2036 numInstr = FixConstantOperands(subtreeRoot, mvec, numInstr, target);