2 //***************************************************************************
4 // SparcInstrSelection.cpp
7 // BURS instruction selection for SPARC V9 architecture.
10 // 7/02/01 - Vikram Adve - Created
11 //**************************************************************************/
13 #include "SparcInternals.h"
14 #include "SparcInstrSelectionSupport.h"
15 #include "llvm/CodeGen/InstrSelectionSupport.h"
16 #include "llvm/CodeGen/MachineInstr.h"
17 #include "llvm/CodeGen/InstrForest.h"
18 #include "llvm/CodeGen/InstrSelection.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/iTerminators.h"
21 #include "llvm/iMemory.h"
22 #include "llvm/iOther.h"
23 #include "llvm/BasicBlock.h"
24 #include "llvm/Method.h"
25 #include "llvm/ConstantVals.h"
26 #include "Support/MathExtras.h"
30 //************************* Forward Declarations ***************************/
33 static void SetMemOperands_Internal (MachineInstr* minstr,
34 const InstructionNode* vmInstrNode,
36 Value* arrayOffsetVal,
37 const vector<Constant*>& idxVec,
38 const TargetMachine& target);
41 //************************ Internal Functions ******************************/
44 static inline MachineOpCode
45 ChooseBprInstruction(const InstructionNode* instrNode)
49 Instruction* setCCInstr =
50 ((InstructionNode*) instrNode->leftChild())->getInstruction();
52 switch(setCCInstr->getOpcode())
54 case Instruction::SetEQ: opCode = BRZ; break;
55 case Instruction::SetNE: opCode = BRNZ; break;
56 case Instruction::SetLE: opCode = BRLEZ; break;
57 case Instruction::SetGE: opCode = BRGEZ; break;
58 case Instruction::SetLT: opCode = BRLZ; break;
59 case Instruction::SetGT: opCode = BRGZ; break;
61 assert(0 && "Unrecognized VM instruction!");
62 opCode = INVALID_OPCODE;
70 static inline MachineOpCode
71 ChooseBpccInstruction(const InstructionNode* instrNode,
72 const BinaryOperator* setCCInstr)
74 MachineOpCode opCode = INVALID_OPCODE;
76 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
80 switch(setCCInstr->getOpcode())
82 case Instruction::SetEQ: opCode = BE; break;
83 case Instruction::SetNE: opCode = BNE; break;
84 case Instruction::SetLE: opCode = BLE; break;
85 case Instruction::SetGE: opCode = BGE; break;
86 case Instruction::SetLT: opCode = BL; break;
87 case Instruction::SetGT: opCode = BG; break;
89 assert(0 && "Unrecognized VM instruction!");
95 switch(setCCInstr->getOpcode())
97 case Instruction::SetEQ: opCode = BE; break;
98 case Instruction::SetNE: opCode = BNE; break;
99 case Instruction::SetLE: opCode = BLEU; break;
100 case Instruction::SetGE: opCode = BCC; break;
101 case Instruction::SetLT: opCode = BCS; break;
102 case Instruction::SetGT: opCode = BGU; break;
104 assert(0 && "Unrecognized VM instruction!");
112 static inline MachineOpCode
113 ChooseBFpccInstruction(const InstructionNode* instrNode,
114 const BinaryOperator* setCCInstr)
116 MachineOpCode opCode = INVALID_OPCODE;
118 switch(setCCInstr->getOpcode())
120 case Instruction::SetEQ: opCode = FBE; break;
121 case Instruction::SetNE: opCode = FBNE; break;
122 case Instruction::SetLE: opCode = FBLE; break;
123 case Instruction::SetGE: opCode = FBGE; break;
124 case Instruction::SetLT: opCode = FBL; break;
125 case Instruction::SetGT: opCode = FBG; break;
127 assert(0 && "Unrecognized VM instruction!");
135 // Create a unique TmpInstruction for a boolean value,
136 // representing the CC register used by a branch on that value.
137 // For now, hack this using a little static cache of TmpInstructions.
138 // Eventually the entire BURG instruction selection should be put
139 // into a separate class that can hold such information.
140 // The static cache is not too bad because the memory for these
141 // TmpInstructions will be freed along with the rest of the Method anyway.
143 static TmpInstruction*
144 GetTmpForCC(Value* boolVal, const Method* method, const Type* ccType)
146 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
147 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
148 static const Method* lastMethod = NULL; // Use to flush cache between methods
150 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
152 if (lastMethod != method)
155 boolToTmpCache.clear();
158 // Look for tmpI and create a new one otherwise. The new value is
159 // directly written to map using the ref returned by operator[].
160 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
162 tmpI = new TmpInstruction(TMP_INSTRUCTION_OPCODE, ccType, boolVal, NULL);
168 static inline MachineOpCode
169 ChooseBccInstruction(const InstructionNode* instrNode,
172 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
173 BinaryOperator* setCCInstr = (BinaryOperator*) setCCNode->getInstruction();
174 const Type* setCCType = setCCInstr->getOperand(0)->getType();
176 isFPBranch = (setCCType == Type::FloatTy || setCCType == Type::DoubleTy);
179 return ChooseBFpccInstruction(instrNode, setCCInstr);
181 return ChooseBpccInstruction(instrNode, setCCInstr);
185 static inline MachineOpCode
186 ChooseMovFpccInstruction(const InstructionNode* instrNode)
188 MachineOpCode opCode = INVALID_OPCODE;
190 switch(instrNode->getInstruction()->getOpcode())
192 case Instruction::SetEQ: opCode = MOVFE; break;
193 case Instruction::SetNE: opCode = MOVFNE; break;
194 case Instruction::SetLE: opCode = MOVFLE; break;
195 case Instruction::SetGE: opCode = MOVFGE; break;
196 case Instruction::SetLT: opCode = MOVFL; break;
197 case Instruction::SetGT: opCode = MOVFG; break;
199 assert(0 && "Unrecognized VM instruction!");
207 // Assumes that SUBcc v1, v2 -> v3 has been executed.
208 // In most cases, we want to clear v3 and then follow it by instruction
210 // Set mustClearReg=false if v3 need not be cleared before conditional move.
211 // Set valueToMove=0 if we want to conditionally move 0 instead of 1
212 // (i.e., we want to test inverse of a condition)
213 // (The latter two cases do not seem to arise because SetNE needs nothing.)
216 ChooseMovpccAfterSub(const InstructionNode* instrNode,
220 MachineOpCode opCode = INVALID_OPCODE;
224 switch(instrNode->getInstruction()->getOpcode())
226 case Instruction::SetEQ: opCode = MOVE; break;
227 case Instruction::SetLE: opCode = MOVLE; break;
228 case Instruction::SetGE: opCode = MOVGE; break;
229 case Instruction::SetLT: opCode = MOVL; break;
230 case Instruction::SetGT: opCode = MOVG; break;
231 case Instruction::SetNE: assert(0 && "No move required!"); break;
232 default: assert(0 && "Unrecognized VM instr!"); break;
238 static inline MachineOpCode
239 ChooseConvertToFloatInstr(const InstructionNode* instrNode,
242 MachineOpCode opCode = INVALID_OPCODE;
244 switch(instrNode->getOpLabel())
247 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
249 else if (opType == Type::LongTy)
251 else if (opType == Type::DoubleTy)
253 else if (opType == Type::FloatTy)
256 assert(0 && "Cannot convert this type to FLOAT on SPARC");
260 // Use FXTOD for all integer-to-double conversions. This has to be
261 // consistent with the code in CreateCodeToCopyIntToFloat() since
262 // that will be used to load the integer into an FP register.
264 if (opType == Type::SByteTy || opType == Type::ShortTy ||
265 opType == Type::IntTy || opType == Type::LongTy)
267 else if (opType == Type::FloatTy)
269 else if (opType == Type::DoubleTy)
272 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
282 static inline MachineOpCode
283 ChooseConvertToIntInstr(const InstructionNode* instrNode,
286 MachineOpCode opCode = INVALID_OPCODE;;
288 int instrType = (int) instrNode->getOpLabel();
290 if (instrType == ToSByteTy || instrType == ToShortTy || instrType == ToIntTy)
292 switch (opType->getPrimitiveID())
294 case Type::FloatTyID: opCode = FSTOI; break;
295 case Type::DoubleTyID: opCode = FDTOI; break;
297 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
301 else if (instrType == ToLongTy)
303 switch (opType->getPrimitiveID())
305 case Type::FloatTyID: opCode = FSTOX; break;
306 case Type::DoubleTyID: opCode = FDTOX; break;
308 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
313 assert(0 && "Should not get here, Mo!");
319 static inline MachineOpCode
320 ChooseAddInstructionByType(const Type* resultType)
322 MachineOpCode opCode = INVALID_OPCODE;
324 if (resultType->isIntegral() ||
325 resultType->isPointerType() ||
326 resultType->isLabelType() ||
327 isa<MethodType>(resultType) ||
328 resultType == Type::BoolTy)
333 switch(resultType->getPrimitiveID())
335 case Type::FloatTyID: opCode = FADDS; break;
336 case Type::DoubleTyID: opCode = FADDD; break;
337 default: assert(0 && "Invalid type for ADD instruction"); break;
344 static inline MachineOpCode
345 ChooseAddInstruction(const InstructionNode* instrNode)
347 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
351 static inline MachineInstr*
352 CreateMovFloatInstruction(const InstructionNode* instrNode,
353 const Type* resultType)
355 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
357 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
358 instrNode->leftChild()->getValue());
359 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
360 instrNode->getValue());
364 static inline MachineInstr*
365 CreateAddConstInstruction(const InstructionNode* instrNode)
367 MachineInstr* minstr = NULL;
369 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
370 assert(isa<Constant>(constOp));
372 // Cases worth optimizing are:
373 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
374 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
376 const Type* resultType = instrNode->getInstruction()->getType();
378 if (resultType == Type::FloatTy ||
379 resultType == Type::DoubleTy)
381 double dval = cast<ConstantFP>(constOp)->getValue();
383 minstr = CreateMovFloatInstruction(instrNode, resultType);
390 static inline MachineOpCode
391 ChooseSubInstructionByType(const Type* resultType)
393 MachineOpCode opCode = INVALID_OPCODE;
395 if (resultType->isIntegral() ||
396 resultType->isPointerType())
401 switch(resultType->getPrimitiveID())
403 case Type::FloatTyID: opCode = FSUBS; break;
404 case Type::DoubleTyID: opCode = FSUBD; break;
405 default: assert(0 && "Invalid type for SUB instruction"); break;
412 static inline MachineInstr*
413 CreateSubConstInstruction(const InstructionNode* instrNode)
415 MachineInstr* minstr = NULL;
417 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
418 assert(isa<Constant>(constOp));
420 // Cases worth optimizing are:
421 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
422 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
424 const Type* resultType = instrNode->getInstruction()->getType();
426 if (resultType == Type::FloatTy ||
427 resultType == Type::DoubleTy)
429 double dval = cast<ConstantFP>(constOp)->getValue();
431 minstr = CreateMovFloatInstruction(instrNode, resultType);
438 static inline MachineOpCode
439 ChooseFcmpInstruction(const InstructionNode* instrNode)
441 MachineOpCode opCode = INVALID_OPCODE;
443 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
444 switch(operand->getType()->getPrimitiveID()) {
445 case Type::FloatTyID: opCode = FCMPS; break;
446 case Type::DoubleTyID: opCode = FCMPD; break;
447 default: assert(0 && "Invalid type for FCMP instruction"); break;
454 // Assumes that leftArg and rightArg are both cast instructions.
457 BothFloatToDouble(const InstructionNode* instrNode)
459 InstrTreeNode* leftArg = instrNode->leftChild();
460 InstrTreeNode* rightArg = instrNode->rightChild();
461 InstrTreeNode* leftArgArg = leftArg->leftChild();
462 InstrTreeNode* rightArgArg = rightArg->leftChild();
463 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
465 // Check if both arguments are floats cast to double
466 return (leftArg->getValue()->getType() == Type::DoubleTy &&
467 leftArgArg->getValue()->getType() == Type::FloatTy &&
468 rightArgArg->getValue()->getType() == Type::FloatTy);
472 static inline MachineOpCode
473 ChooseMulInstructionByType(const Type* resultType)
475 MachineOpCode opCode = INVALID_OPCODE;
477 if (resultType->isIntegral())
480 switch(resultType->getPrimitiveID())
482 case Type::FloatTyID: opCode = FMULS; break;
483 case Type::DoubleTyID: opCode = FMULD; break;
484 default: assert(0 && "Invalid type for MUL instruction"); break;
491 static inline MachineOpCode
492 ChooseMulInstruction(const InstructionNode* instrNode,
495 if (checkCasts && BothFloatToDouble(instrNode))
498 // else use the regular multiply instructions
499 return ChooseMulInstructionByType(instrNode->getInstruction()->getType());
503 static inline MachineInstr*
504 CreateIntNegInstruction(TargetMachine& target,
507 MachineInstr* minstr = new MachineInstr(SUB);
508 minstr->SetMachineOperand(0, target.getRegInfo().getZeroRegNum());
509 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, vreg);
510 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, vreg);
515 static inline MachineInstr*
516 CreateMulConstInstruction(TargetMachine &target,
517 const InstructionNode* instrNode,
518 MachineInstr*& getMinstr2)
520 MachineInstr* minstr = NULL; // return NULL if we cannot exploit constant
521 getMinstr2 = NULL; // to create a cheaper instruction
522 bool needNeg = false;
524 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
525 assert(isa<Constant>(constOp));
527 // Cases worth optimizing are:
528 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
529 // (2) Multiply by 2^x for integer types: replace with Shift
531 const Type* resultType = instrNode->getInstruction()->getType();
533 if (resultType->isIntegral() || resultType->isPointerType())
537 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
540 bool needNeg = false;
547 if (C == 0 || C == 1)
549 minstr = new MachineInstr(ADD);
552 minstr->SetMachineOperand(0,
553 target.getRegInfo().getZeroRegNum());
555 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
556 instrNode->leftChild()->getValue());
557 minstr->SetMachineOperand(1,target.getRegInfo().getZeroRegNum());
559 else if (IsPowerOf2(C, pow))
561 minstr = new MachineInstr((resultType == Type::LongTy)
563 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
564 instrNode->leftChild()->getValue());
565 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
569 if (minstr && needNeg)
570 { // insert <reg = SUB 0, reg> after the instr to flip the sign
571 getMinstr2 = CreateIntNegInstruction(target,
572 instrNode->getValue());
578 if (resultType == Type::FloatTy ||
579 resultType == Type::DoubleTy)
581 double dval = cast<ConstantFP>(constOp)->getValue();
584 bool needNeg = (dval < 0);
586 MachineOpCode opCode = needNeg
587 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
588 : (resultType == Type::FloatTy? FMOVS : FMOVD);
590 minstr = new MachineInstr(opCode);
591 minstr->SetMachineOperand(0,
592 MachineOperand::MO_VirtualRegister,
593 instrNode->leftChild()->getValue());
599 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
600 instrNode->getValue());
606 // Generate a divide instruction for Div or Rem.
607 // For Rem, this assumes that the operand type will be signed if the result
608 // type is signed. This is correct because they must have the same sign.
610 static inline MachineOpCode
611 ChooseDivInstruction(TargetMachine &target,
612 const InstructionNode* instrNode)
614 MachineOpCode opCode = INVALID_OPCODE;
616 const Type* resultType = instrNode->getInstruction()->getType();
618 if (resultType->isIntegral())
619 opCode = resultType->isSigned()? SDIVX : UDIVX;
621 switch(resultType->getPrimitiveID())
623 case Type::FloatTyID: opCode = FDIVS; break;
624 case Type::DoubleTyID: opCode = FDIVD; break;
625 default: assert(0 && "Invalid type for DIV instruction"); break;
632 static inline MachineInstr*
633 CreateDivConstInstruction(TargetMachine &target,
634 const InstructionNode* instrNode,
635 MachineInstr*& getMinstr2)
637 MachineInstr* minstr = NULL;
640 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
641 assert(isa<Constant>(constOp));
643 // Cases worth optimizing are:
644 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
645 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
647 const Type* resultType = instrNode->getInstruction()->getType();
649 if (resultType->isIntegral())
653 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
656 bool needNeg = false;
665 minstr = new MachineInstr(ADD);
666 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
667 instrNode->leftChild()->getValue());
668 minstr->SetMachineOperand(1,target.getRegInfo().getZeroRegNum());
670 else if (IsPowerOf2(C, pow))
672 MachineOpCode opCode= ((resultType->isSigned())
673 ? (resultType==Type::LongTy)? SRAX : SRA
674 : (resultType==Type::LongTy)? SRLX : SRL);
675 minstr = new MachineInstr(opCode);
676 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
677 instrNode->leftChild()->getValue());
678 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
682 if (minstr && needNeg)
683 { // insert <reg = SUB 0, reg> after the instr to flip the sign
684 getMinstr2 = CreateIntNegInstruction(target,
685 instrNode->getValue());
691 if (resultType == Type::FloatTy ||
692 resultType == Type::DoubleTy)
694 double dval = cast<ConstantFP>(constOp)->getValue();
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, MachineOperand::MO_VirtualRegister,
705 instrNode->leftChild()->getValue());
711 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
712 instrNode->getValue());
718 //------------------------------------------------------------------------
719 // Function SetOperandsForMemInstr
721 // Choose addressing mode for the given load or store instruction.
722 // Use [reg+reg] if it is an indexed reference, and the index offset is
723 // not a constant or if it cannot fit in the offset field.
724 // Use [reg+offset] in all other cases.
726 // This assumes that all array refs are "lowered" to one of these forms:
727 // %x = load (subarray*) ptr, constant ; single constant offset
728 // %x = load (subarray*) ptr, offsetVal ; single non-constant offset
729 // Generally, this should happen via strength reduction + LICM.
730 // Also, strength reduction should take care of using the same register for
731 // the loop index variable and an array index, when that is profitable.
732 //------------------------------------------------------------------------
735 SetOperandsForMemInstr(MachineInstr* minstr,
736 const InstructionNode* vmInstrNode,
737 const TargetMachine& target)
739 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
741 // Variables to hold the index vector, ptr value, and offset value.
742 // The major work here is to extract these for all 3 instruction types
743 // and then call the common function SetMemOperands_Internal().
745 const vector<Constant*> OLDIDXVEC = memInst->getIndicesBROKEN();
746 const vector<Constant*>* idxVec = &OLDIDXVEC; //FIXME
747 vector<Constant*>* newIdxVec = NULL;
749 Value* arrayOffsetVal = NULL;
751 // Test if a GetElemPtr instruction is being folded into this mem instrn.
752 // If so, it will be in the left child for Load and GetElemPtr,
753 // and in the right child for Store instructions.
755 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
756 ? vmInstrNode->rightChild()
757 : vmInstrNode->leftChild());
759 if (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
760 ptrChild->getOpLabel() == GetElemPtrIdx)
762 // There is a GetElemPtr instruction and there may be a chain of
763 // more than one. Use the pointer value of the last one in the chain.
764 // Fold the index vectors from the entire chain and from the mem
765 // instruction into one single index vector.
766 // Finally, we never fold for an array instruction so make that NULL.
768 newIdxVec = new vector<Constant*>;
769 ptrVal = FoldGetElemChain((InstructionNode*) ptrChild, *newIdxVec);
771 newIdxVec->insert(newIdxVec->end(), idxVec->begin(), idxVec->end());
774 assert(!((PointerType*)ptrVal->getType())->getElementType()->isArrayType()
775 && "GetElemPtr cannot be folded into array refs in selection");
779 // There is no GetElemPtr instruction.
780 // Use the pointer value and the index vector from the Mem instruction.
781 // If it is an array reference, get the array offset value.
783 ptrVal = memInst->getPointerOperand();
785 const Type* opType = cast<PointerType>(ptrVal->getType())->getElementType();
786 if (opType->isArrayType())
788 assert((memInst->getNumOperands()
789 == (unsigned) 1 + memInst->getFirstIndexOperandNumber())
790 && "Array refs must be lowered before Instruction Selection");
792 arrayOffsetVal = memInst->getOperand(memInst->getFirstIndexOperandNumber());
796 SetMemOperands_Internal(minstr, vmInstrNode, ptrVal, arrayOffsetVal,
799 if (newIdxVec != NULL)
805 SetMemOperands_Internal(MachineInstr* minstr,
806 const InstructionNode* vmInstrNode,
808 Value* arrayOffsetVal,
809 const vector<Constant*>& idxVec,
810 const TargetMachine& target)
812 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
814 // Initialize so we default to storing the offset in a register.
815 int64_t smallConstOffset = 0;
816 Value* valueForRegOffset = NULL;
817 MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister;
819 // Check if there is an index vector and if so, if it translates to
820 // a small enough constant to fit in the immediate-offset field.
822 if (idxVec.size() > 0)
824 bool isConstantOffset = false;
827 const PointerType* ptrType = (PointerType*) ptrVal->getType();
829 if (ptrType->getElementType()->isStructType())
831 // the offset is always constant for structs
832 isConstantOffset = true;
834 // Compute the offset value using the index vector
835 offset = target.DataLayout.getIndexedOffset(ptrType, idxVec);
839 // It must be an array ref. Check if the offset is a constant,
840 // and that the indexing has been lowered to a single offset.
842 assert(ptrType->getElementType()->isArrayType());
843 assert(arrayOffsetVal != NULL
844 && "Expect to be given Value* for array offsets");
846 if (Constant *CPV = dyn_cast<Constant>(arrayOffsetVal))
848 isConstantOffset = true; // always constant for structs
849 assert(arrayOffsetVal->getType()->isIntegral());
850 offset = (CPV->getType()->isSigned()
851 ? cast<ConstantSInt>(CPV)->getValue()
852 : (int64_t) cast<ConstantUInt>(CPV)->getValue());
856 valueForRegOffset = arrayOffsetVal;
860 if (isConstantOffset)
862 // create a virtual register for the constant
863 valueForRegOffset = ConstantSInt::get(Type::IntTy, offset);
868 offsetOpType = MachineOperand::MO_SignExtendedImmed;
869 smallConstOffset = 0;
872 // Operand 0 is value for STORE, ptr for LOAD or GET_ELEMENT_PTR
873 // It is the left child in the instruction tree in all cases.
874 Value* leftVal = vmInstrNode->leftChild()->getValue();
875 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, leftVal);
877 // Operand 1 is ptr for STORE, offset for LOAD or GET_ELEMENT_PTR
878 // Operand 2 is offset for STORE, result reg for LOAD or GET_ELEMENT_PTR
880 unsigned offsetOpNum = (memInst->getOpcode() == Instruction::Store)? 2 : 1;
881 if (offsetOpType == MachineOperand::MO_VirtualRegister)
883 assert(valueForRegOffset != NULL);
884 minstr->SetMachineOperand(offsetOpNum, offsetOpType, valueForRegOffset);
887 minstr->SetMachineOperand(offsetOpNum, offsetOpType, smallConstOffset);
889 if (memInst->getOpcode() == Instruction::Store)
890 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, ptrVal);
892 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
893 vmInstrNode->getValue());
898 // Substitute operand `operandNum' of the instruction in node `treeNode'
899 // in place of the use(s) of that instruction in node `parent'.
900 // Check both explicit and implicit operands!
903 ForwardOperand(InstructionNode* treeNode,
904 InstrTreeNode* parent,
907 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
909 Instruction* unusedOp = treeNode->getInstruction();
910 Value* fwdOp = unusedOp->getOperand(operandNum);
912 // The parent itself may be a list node, so find the real parent instruction
913 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
915 parent = parent->parent();
916 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
918 InstructionNode* parentInstrNode = (InstructionNode*) parent;
920 Instruction* userInstr = parentInstrNode->getInstruction();
921 MachineCodeForVMInstr& mvec = userInstr->getMachineInstrVec();
922 for (unsigned i=0, N=mvec.size(); i < N; i++)
924 MachineInstr* minstr = mvec[i];
926 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
928 const MachineOperand& mop = minstr->getOperand(i);
929 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
930 mop.getVRegValue() == unusedOp)
932 minstr->SetMachineOperand(i, MachineOperand::MO_VirtualRegister,
937 for (unsigned i=0, numOps=minstr->getNumImplicitRefs(); i < numOps; ++i)
938 if (minstr->getImplicitRef(i) == unusedOp)
939 minstr->setImplicitRef(i, fwdOp, minstr->implicitRefIsDefined(i));
945 void UltraSparcInstrInfo::
946 CreateCopyInstructionsByType(const TargetMachine& target,
949 vector<MachineInstr*>& minstrVec) const
951 bool loadConstantToReg = false;
953 const Type* resultType = dest->getType();
955 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
956 if (opCode == INVALID_OPCODE)
958 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
962 // if `src' is a constant that doesn't fit in the immed field or if it is
963 // a global variable (i.e., a constant address), generate a load
964 // instruction instead of an add
966 if (isa<Constant>(src))
968 unsigned int machineRegNum;
970 MachineOperand::MachineOperandType opType =
971 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
972 machineRegNum, immedValue);
974 if (opType == MachineOperand::MO_VirtualRegister)
975 loadConstantToReg = true;
977 else if (isa<GlobalValue>(src))
978 loadConstantToReg = true;
980 if (loadConstantToReg)
981 { // `src' is constant and cannot fit in immed field for the ADD
982 // Insert instructions to "load" the constant into a register
983 vector<TmpInstruction*> tempVec;
984 target.getInstrInfo().CreateCodeToLoadConst(src,dest,minstrVec,tempVec);
985 for (unsigned i=0; i < tempVec.size(); i++)
986 dest->getMachineInstrVec().addTempValue(tempVec[i]);
989 { // Create the appropriate add instruction.
990 // Make `src' the second operand, in case it is a constant
991 // Use (unsigned long) 0 for a NULL pointer value.
993 const Type* nullValueType =
994 (resultType->getPrimitiveID() == Type::PointerTyID)? Type::ULongTy
996 MachineInstr* minstr = new MachineInstr(opCode);
997 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
998 Constant::getNullConstant(nullValueType));
999 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, src);
1000 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, dest);
1001 minstrVec.push_back(minstr);
1007 //******************* Externally Visible Functions *************************/
1010 //------------------------------------------------------------------------
1011 // External Function: GetInstructionsForProlog
1012 // External Function: GetInstructionsForEpilog
1015 // Create prolog and epilog code for procedure entry and exit
1016 //------------------------------------------------------------------------
1019 GetInstructionsForProlog(BasicBlock* entryBB,
1020 TargetMachine &target,
1021 MachineInstr** mvec)
1023 int64_t s0=0; // used to avoid overloading ambiguity below
1025 const MachineFrameInfo& frameInfo = target.getFrameInfo();
1027 // The second operand is the stack size. If it does not fit in the
1028 // immediate field, we either have to find an unused register in the
1029 // caller's window or move some elements to the dynamically allocated
1030 // area of the stack frame (just above save area and method args).
1031 Method* method = entryBB->getParent();
1032 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(method);
1033 unsigned int staticStackSize = mcInfo.getStaticStackSize();
1035 if (staticStackSize < (unsigned) frameInfo.getMinStackFrameSize())
1036 staticStackSize = (unsigned) frameInfo.getMinStackFrameSize();
1038 if (unsigned padsz = (staticStackSize %
1039 (unsigned) frameInfo.getStackFrameSizeAlignment()))
1040 staticStackSize += frameInfo.getStackFrameSizeAlignment() - padsz;
1042 assert(target.getInstrInfo().constantFitsInImmedField(SAVE, staticStackSize)
1043 && "Stack size too large for immediate field of SAVE instruction. Need additional work as described in the comment above");
1045 mvec[0] = new MachineInstr(SAVE);
1046 mvec[0]->SetMachineOperand(0, target.getRegInfo().getStackPointer());
1047 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1048 - (int) staticStackSize);
1049 mvec[0]->SetMachineOperand(2, target.getRegInfo().getStackPointer());
1056 GetInstructionsForEpilog(BasicBlock* anExitBB,
1057 TargetMachine &target,
1058 MachineInstr** mvec)
1060 int64_t s0=0; // used to avoid overloading ambiguity below
1062 mvec[0] = new MachineInstr(RESTORE);
1063 mvec[0]->SetMachineOperand(0, target.getRegInfo().getZeroRegNum());
1064 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed, s0);
1065 mvec[0]->SetMachineOperand(2, target.getRegInfo().getZeroRegNum());
1071 //------------------------------------------------------------------------
1072 // External Function: ThisIsAChainRule
1075 // Check if a given BURG rule is a chain rule.
1076 //------------------------------------------------------------------------
1079 ThisIsAChainRule(int eruleno)
1083 case 111: // stmt: reg
1084 case 113: // stmt: bool
1106 return false; break;
1111 //------------------------------------------------------------------------
1112 // External Function: GetInstructionsByRule
1115 // Choose machine instructions for the SPARC according to the
1116 // patterns chosen by the BURG-generated parser.
1117 //------------------------------------------------------------------------
1120 GetInstructionsByRule(InstructionNode* subtreeRoot,
1123 TargetMachine &target,
1124 MachineInstr** mvec)
1126 int numInstr = 1; // initialize for common case
1127 bool checkCast = false; // initialize here to use fall-through
1129 int forwardOperandNum = -1;
1130 int64_t s0=0, s8=8; // variables holding constants to avoid
1131 uint64_t u0=0; // overloading ambiguities below
1133 for (unsigned i=0; i < MAX_INSTR_PER_VMINSTR; i++)
1137 // Let's check for chain rules outside the switch so that we don't have
1138 // to duplicate the list of chain rule production numbers here again
1140 if (ThisIsAChainRule(ruleForNode))
1142 // Chain rules have a single nonterminal on the RHS.
1143 // Get the rule that matches the RHS non-terminal and use that instead.
1145 assert(nts[0] && ! nts[1]
1146 && "A chain rule should have only one RHS non-terminal!");
1147 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1148 nts = burm_nts[nextRule];
1149 numInstr = GetInstructionsByRule(subtreeRoot, nextRule, nts,target,mvec);
1153 switch(ruleForNode) {
1154 case 1: // stmt: Ret
1155 case 2: // stmt: RetValue(reg)
1156 { // NOTE: Prepass of register allocation is responsible
1157 // for moving return value to appropriate register.
1158 // Mark the return-address register as a hidden virtual reg.
1159 // Mark the return value register as an implicit ref of
1160 // the machine instruction.
1161 // Finally put a NOP in the delay slot.
1162 ReturnInst *returnInstr =
1163 cast<ReturnInst>(subtreeRoot->getInstruction());
1164 assert(returnInstr->getOpcode() == Instruction::Ret);
1165 Method* method = returnInstr->getParent()->getParent();
1167 Instruction* returnReg = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
1169 returnInstr->getMachineInstrVec().addTempValue(returnReg);
1171 mvec[0] = new MachineInstr(JMPLRET);
1172 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1174 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,s8);
1175 mvec[0]->SetMachineOperand(2, target.getRegInfo().getZeroRegNum());
1177 if (returnInstr->getReturnValue() != NULL)
1178 mvec[0]->addImplicitRef(returnInstr->getReturnValue());
1180 unsigned n = numInstr++; // delay slot
1181 mvec[n] = new MachineInstr(NOP);
1186 case 3: // stmt: Store(reg,reg)
1187 case 4: // stmt: Store(reg,ptrreg)
1188 mvec[0] = new MachineInstr(
1189 ChooseStoreInstruction(
1190 subtreeRoot->leftChild()->getValue()->getType()));
1191 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1194 case 5: // stmt: BrUncond
1195 mvec[0] = new MachineInstr(BA);
1196 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1198 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1199 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
1202 mvec[numInstr++] = new MachineInstr(NOP);
1205 case 206: // stmt: BrCond(setCCconst)
1206 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
1207 // If the constant is ZERO, we can use the branch-on-integer-register
1208 // instructions and avoid the SUBcc instruction entirely.
1209 // Otherwise this is just the same as case 5, so just fall through.
1211 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1213 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
1214 Constant *constVal = cast<Constant>(constNode->getValue());
1217 if ((constVal->getType()->isIntegral()
1218 || constVal->getType()->isPointerType())
1219 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1222 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1224 // That constant is a zero after all...
1225 // Use the left child of setCC as the first argument!
1226 mvec[0] = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1227 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1228 subtreeRoot->leftChild()->leftChild()->getValue());
1229 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1230 brInst->getSuccessor(0));
1233 mvec[numInstr++] = new MachineInstr(NOP);
1237 mvec[n] = new MachineInstr(BA);
1238 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1240 mvec[n]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1241 brInst->getSuccessor(1));
1244 mvec[numInstr++] = new MachineInstr(NOP);
1248 // ELSE FALL THROUGH
1251 case 6: // stmt: BrCond(bool)
1252 { // bool => boolean was computed with some boolean operator
1253 // (SetCC, Not, ...). We need to check whether the type was a FP,
1254 // signed int or unsigned int, and check the branching condition in
1255 // order to choose the branch to use.
1256 // If it is an integer CC, we also need to find the unique
1257 // TmpInstruction representing that CC.
1259 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
1261 mvec[0] = new MachineInstr(ChooseBccInstruction(subtreeRoot,
1264 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1265 brInst->getParent()->getParent(),
1266 isFPBranch? Type::FloatTy : Type::IntTy);
1268 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister, ccValue);
1269 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1270 brInst->getSuccessor(0));
1273 mvec[numInstr++] = new MachineInstr(NOP);
1277 mvec[n] = new MachineInstr(BA);
1278 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1280 mvec[n]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1281 brInst->getSuccessor(1));
1284 mvec[numInstr++] = new MachineInstr(NOP);
1288 case 208: // stmt: BrCond(boolconst)
1290 // boolconst => boolean is a constant; use BA to first or second label
1291 Constant* constVal =
1292 cast<Constant>(subtreeRoot->leftChild()->getValue());
1293 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
1295 mvec[0] = new MachineInstr(BA);
1296 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1298 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1299 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(dest));
1302 mvec[numInstr++] = new MachineInstr(NOP);
1306 case 8: // stmt: BrCond(boolreg)
1307 { // boolreg => boolean is stored in an existing register.
1308 // Just use the branch-on-integer-register instruction!
1310 mvec[0] = new MachineInstr(BRNZ);
1311 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1312 subtreeRoot->leftChild()->getValue());
1313 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1314 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1317 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1321 mvec[n] = new MachineInstr(BA);
1322 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1324 mvec[n]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1325 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
1328 mvec[numInstr++] = new MachineInstr(NOP);
1332 case 9: // stmt: Switch(reg)
1333 assert(0 && "*** SWITCH instruction is not implemented yet.");
1337 case 10: // reg: VRegList(reg, reg)
1338 assert(0 && "VRegList should never be the topmost non-chain rule");
1341 case 21: // bool: Not(bool): Both these are implemented as:
1342 case 321: // reg: BNot(reg) : reg = reg XOR-NOT 0
1343 mvec[0] = new MachineInstr(XNOR);
1344 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1345 subtreeRoot->leftChild()->getValue());
1346 mvec[0]->SetMachineOperand(1, target.getRegInfo().getZeroRegNum());
1347 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1348 subtreeRoot->getValue());
1351 case 322: // reg: ToBoolTy(bool):
1352 case 22: // reg: ToBoolTy(reg):
1354 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
1355 assert(opType->isIntegral() || opType->isPointerType()
1356 || opType == Type::BoolTy);
1358 forwardOperandNum = 0;
1362 case 23: // reg: ToUByteTy(reg)
1363 case 25: // reg: ToUShortTy(reg)
1364 case 27: // reg: ToUIntTy(reg)
1365 case 29: // reg: ToULongTy(reg)
1367 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
1368 assert(opType->isIntegral() ||
1369 opType->isPointerType() ||
1370 opType == Type::BoolTy && "Cast is illegal for other types");
1372 forwardOperandNum = 0;
1376 case 24: // reg: ToSByteTy(reg)
1377 case 26: // reg: ToShortTy(reg)
1378 case 28: // reg: ToIntTy(reg)
1379 case 30: // reg: ToLongTy(reg)
1381 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
1382 if (opType->isIntegral()
1383 || opType->isPointerType()
1384 || opType == Type::BoolTy)
1387 forwardOperandNum = 0;
1391 // If the source operand is an FP type, the int result must be
1392 // copied from float to int register via memory!
1393 Instruction *dest = subtreeRoot->getInstruction();
1394 Value* leftVal = subtreeRoot->leftChild()->getValue();
1396 vector<MachineInstr*> minstrVec;
1398 if (opType == Type::FloatTy || opType == Type::DoubleTy)
1400 // Create a temporary to represent the INT register
1401 // into which the FP value will be copied via memory.
1402 // The type of this temporary will determine the FP
1403 // register used: single-prec for a 32-bit int or smaller,
1404 // double-prec for a 64-bit int.
1406 const Type* destTypeToUse =
1407 (dest->getType() == Type::LongTy)? Type::DoubleTy
1409 destForCast = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
1410 destTypeToUse, leftVal, NULL);
1411 dest->getMachineInstrVec().addTempValue(destForCast);
1413 vector<TmpInstruction*> tempVec;
1414 target.getInstrInfo().CreateCodeToCopyFloatToInt(
1415 dest->getParent()->getParent(),
1416 (TmpInstruction*) destForCast, dest,
1417 minstrVec, tempVec, target);
1419 for (unsigned i=0; i < tempVec.size(); ++i)
1420 dest->getMachineInstrVec().addTempValue(tempVec[i]);
1423 destForCast = leftVal;
1425 MachineOpCode opCode=ChooseConvertToIntInstr(subtreeRoot, opType);
1426 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
1428 mvec[0] = new MachineInstr(opCode);
1429 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1431 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1434 assert(numInstr == 1 && "Should be initialized to 1 at the top");
1435 for (unsigned i=0; i < minstrVec.size(); ++i)
1436 mvec[numInstr++] = minstrVec[i];
1441 case 31: // reg: ToFloatTy(reg):
1442 case 32: // reg: ToDoubleTy(reg):
1443 case 232: // reg: ToDoubleTy(Constant):
1445 // If this instruction has a parent (a user) in the tree
1446 // and the user is translated as an FsMULd instruction,
1447 // then the cast is unnecessary. So check that first.
1448 // In the future, we'll want to do the same for the FdMULq instruction,
1449 // so do the check here instead of only for ToFloatTy(reg).
1451 if (subtreeRoot->parent() != NULL &&
1452 ((InstructionNode*) subtreeRoot->parent())->getInstruction()->getMachineInstrVec()[0]->getOpCode() == FSMULD)
1455 forwardOperandNum = 0;
1459 Value* leftVal = subtreeRoot->leftChild()->getValue();
1460 const Type* opType = leftVal->getType();
1461 MachineOpCode opCode=ChooseConvertToFloatInstr(subtreeRoot,opType);
1462 if (opCode == INVALID_OPCODE) // no conversion needed
1465 forwardOperandNum = 0;
1469 // If the source operand is a non-FP type it must be
1470 // first copied from int to float register via memory!
1471 Instruction *dest = subtreeRoot->getInstruction();
1474 if (opType != Type::FloatTy && opType != Type::DoubleTy)
1476 // Create a temporary to represent the FP register
1477 // into which the integer will be copied via memory.
1478 // The type of this temporary will determine the FP
1479 // register used: single-prec for a 32-bit int or smaller,
1480 // double-prec for a 64-bit int.
1482 const Type* srcTypeToUse =
1483 (leftVal->getType() == Type::LongTy)? Type::DoubleTy
1486 srcForCast = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
1487 srcTypeToUse, dest, NULL);
1488 dest->getMachineInstrVec().addTempValue(srcForCast);
1490 vector<MachineInstr*> minstrVec;
1491 vector<TmpInstruction*> tempVec;
1492 target.getInstrInfo().CreateCodeToCopyIntToFloat(
1493 dest->getParent()->getParent(),
1494 leftVal, (TmpInstruction*) srcForCast,
1495 minstrVec, tempVec, target);
1497 for (unsigned i=0; i < minstrVec.size(); ++i)
1498 mvec[n++] = minstrVec[i];
1500 for (unsigned i=0; i < tempVec.size(); ++i)
1501 dest->getMachineInstrVec().addTempValue(tempVec[i]);
1504 srcForCast = leftVal;
1506 MachineInstr* castI = new MachineInstr(opCode);
1507 castI->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1509 castI->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1517 case 19: // reg: ToArrayTy(reg):
1518 case 20: // reg: ToPointerTy(reg):
1520 forwardOperandNum = 0;
1523 case 233: // reg: Add(reg, Constant)
1524 mvec[0] = CreateAddConstInstruction(subtreeRoot);
1525 if (mvec[0] != NULL)
1527 // ELSE FALL THROUGH
1529 case 33: // reg: Add(reg, reg)
1530 mvec[0] = new MachineInstr(ChooseAddInstruction(subtreeRoot));
1531 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1534 case 234: // reg: Sub(reg, Constant)
1535 mvec[0] = CreateSubConstInstruction(subtreeRoot);
1536 if (mvec[0] != NULL)
1538 // ELSE FALL THROUGH
1540 case 34: // reg: Sub(reg, reg)
1541 mvec[0] = new MachineInstr(ChooseSubInstructionByType(
1542 subtreeRoot->getInstruction()->getType()));
1543 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1546 case 135: // reg: Mul(todouble, todouble)
1550 case 35: // reg: Mul(reg, reg)
1551 mvec[0] =new MachineInstr(ChooseMulInstruction(subtreeRoot,checkCast));
1552 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1555 case 335: // reg: Mul(todouble, todoubleConst)
1559 case 235: // reg: Mul(reg, Constant)
1560 mvec[0] = CreateMulConstInstruction(target, subtreeRoot, mvec[1]);
1561 if (mvec[0] == NULL)
1563 mvec[0] = new MachineInstr(ChooseMulInstruction(subtreeRoot,
1565 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1568 if (mvec[1] != NULL)
1572 case 236: // reg: Div(reg, Constant)
1573 mvec[0] = CreateDivConstInstruction(target, subtreeRoot, mvec[1]);
1574 if (mvec[0] != NULL)
1576 if (mvec[1] != NULL)
1580 // ELSE FALL THROUGH
1582 case 36: // reg: Div(reg, reg)
1583 mvec[0] = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1584 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1587 case 37: // reg: Rem(reg, reg)
1588 case 237: // reg: Rem(reg, Constant)
1590 Instruction* remInstr = subtreeRoot->getInstruction();
1592 TmpInstruction* quot = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
1593 subtreeRoot->leftChild()->getValue(),
1594 subtreeRoot->rightChild()->getValue());
1595 TmpInstruction* prod = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
1597 subtreeRoot->rightChild()->getValue());
1598 remInstr->getMachineInstrVec().addTempValue(quot);
1599 remInstr->getMachineInstrVec().addTempValue(prod);
1601 mvec[0] = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1602 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1603 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,quot);
1606 mvec[n] = new MachineInstr(ChooseMulInstructionByType(
1607 subtreeRoot->getInstruction()->getType()));
1608 mvec[n]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,quot);
1609 mvec[n]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1610 subtreeRoot->rightChild()->getValue());
1611 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,prod);
1614 mvec[n] = new MachineInstr(ChooseSubInstructionByType(
1615 subtreeRoot->getInstruction()->getType()));
1616 Set3OperandsFromInstr(mvec[n], subtreeRoot, target);
1617 mvec[n]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,prod);
1622 case 38: // bool: And(bool, bool)
1623 case 238: // bool: And(bool, boolconst)
1624 case 338: // reg : BAnd(reg, reg)
1625 case 538: // reg : BAnd(reg, Constant)
1626 mvec[0] = new MachineInstr(AND);
1627 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1630 case 138: // bool: And(bool, not)
1631 case 438: // bool: BAnd(bool, not)
1632 mvec[0] = new MachineInstr(ANDN);
1633 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1636 case 39: // bool: Or(bool, bool)
1637 case 239: // bool: Or(bool, boolconst)
1638 case 339: // reg : BOr(reg, reg)
1639 case 539: // reg : BOr(reg, Constant)
1640 mvec[0] = new MachineInstr(ORN);
1641 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1644 case 139: // bool: Or(bool, not)
1645 case 439: // bool: BOr(bool, not)
1646 mvec[0] = new MachineInstr(ORN);
1647 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1650 case 40: // bool: Xor(bool, bool)
1651 case 240: // bool: Xor(bool, boolconst)
1652 case 340: // reg : BXor(reg, reg)
1653 case 540: // reg : BXor(reg, Constant)
1654 mvec[0] = new MachineInstr(XOR);
1655 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1658 case 140: // bool: Xor(bool, not)
1659 case 440: // bool: BXor(bool, not)
1660 mvec[0] = new MachineInstr(XNOR);
1661 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1664 case 41: // boolconst: SetCC(reg, Constant)
1665 // Check if this is an integer comparison, and
1666 // there is a parent, and the parent decided to use
1667 // a branch-on-integer-register instead of branch-on-condition-code.
1668 // If so, the SUBcc instruction is not required.
1669 // (However, we must still check for constants to be loaded from
1670 // the constant pool so that such a load can be associated with
1671 // this instruction.)
1673 // Otherwise this is just the same as case 42, so just fall through.
1675 if ((subtreeRoot->leftChild()->getValue()->getType()->isIntegral() ||
1676 subtreeRoot->leftChild()->getValue()->getType()->isPointerType())
1677 && subtreeRoot->parent() != NULL)
1679 InstructionNode* parent = (InstructionNode*) subtreeRoot->parent();
1680 assert(parent->getNodeType() == InstrTreeNode::NTInstructionNode);
1681 const vector<MachineInstr*>&
1682 minstrVec = parent->getInstruction()->getMachineInstrVec();
1683 MachineOpCode parentOpCode;
1684 if (parent->getInstruction()->getOpcode() == Instruction::Br &&
1685 (parentOpCode = minstrVec[0]->getOpCode()) >= BRZ &&
1686 parentOpCode <= BRGEZ)
1688 numInstr = 0; // don't forward the operand!
1692 // ELSE FALL THROUGH
1694 case 42: // bool: SetCC(reg, reg):
1696 // This generates a SUBCC instruction, putting the difference in
1697 // a result register, and setting a condition code.
1699 // If the boolean result of the SetCC is used by anything other
1700 // than a single branch instruction, the boolean must be
1701 // computed and stored in the result register. Otherwise, discard
1702 // the difference (by using %g0) and keep only the condition code.
1704 // To compute the boolean result in a register we use a conditional
1705 // move, unless the result of the SUBCC instruction can be used as
1706 // the bool! This assumes that zero is FALSE and any non-zero
1709 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1710 Instruction* setCCInstr = subtreeRoot->getInstruction();
1711 bool keepBoolVal = (parentNode == NULL ||
1712 parentNode->getInstruction()->getOpcode()
1713 != Instruction::Br);
1714 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
1715 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1716 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1720 MachineOpCode movOpCode = 0;
1722 // Mark the 4th operand as being a CC register, and as a def
1723 // A TmpInstruction is created to represent the CC "result".
1724 // Unlike other instances of TmpInstruction, this one is used
1725 // by machine code of multiple LLVM instructions, viz.,
1726 // the SetCC and the branch. Make sure to get the same one!
1727 // Note that we do this even for FP CC registers even though they
1728 // are explicit operands, because the type of the operand
1729 // needs to be a floating point condition code, not an integer
1730 // condition code. Think of this as casting the bool result to
1731 // a FP condition code register.
1733 Value* leftVal = subtreeRoot->leftChild()->getValue();
1734 bool isFPCompare = (leftVal->getType() == Type::FloatTy ||
1735 leftVal->getType() == Type::DoubleTy);
1737 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1738 setCCInstr->getParent()->getParent(),
1739 isFPCompare? Type::FloatTy : Type::IntTy);
1740 setCCInstr->getMachineInstrVec().addTempValue(tmpForCC);
1744 // Integer condition: dest. should be %g0 or an integer register.
1745 // If result must be saved but condition is not SetEQ then we need
1746 // a separate instruction to compute the bool result, so discard
1747 // result of SUBcc instruction anyway.
1749 mvec[0] = new MachineInstr(SUBcc);
1750 Set3OperandsFromInstr(mvec[0], subtreeRoot, target, ! keepSubVal);
1752 mvec[0]->SetMachineOperand(3, MachineOperand::MO_CCRegister,
1753 tmpForCC, /*def*/true);
1756 { // recompute bool using the integer condition codes
1758 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1763 // FP condition: dest of FCMP should be some FCCn register
1764 mvec[0] = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1765 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1767 mvec[0]->SetMachineOperand(1,MachineOperand::MO_VirtualRegister,
1768 subtreeRoot->leftChild()->getValue());
1769 mvec[0]->SetMachineOperand(2,MachineOperand::MO_VirtualRegister,
1770 subtreeRoot->rightChild()->getValue());
1773 {// recompute bool using the FP condition codes
1774 mustClearReg = true;
1776 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1783 {// Unconditionally set register to 0
1785 mvec[n] = new MachineInstr(SETHI);
1786 mvec[n]->SetMachineOperand(0,MachineOperand::MO_UnextendedImmed,
1788 mvec[n]->SetMachineOperand(1,MachineOperand::MO_VirtualRegister,
1792 // Now conditionally move `valueToMove' (0 or 1) into the register
1794 mvec[n] = new MachineInstr(movOpCode);
1795 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1797 mvec[n]->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
1799 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1805 case 43: // boolreg: VReg
1806 case 44: // boolreg: Constant
1810 case 51: // reg: Load(reg)
1811 case 52: // reg: Load(ptrreg)
1812 case 53: // reg: LoadIdx(reg,reg)
1813 case 54: // reg: LoadIdx(ptrreg,reg)
1814 mvec[0] = new MachineInstr(ChooseLoadInstruction(
1815 subtreeRoot->getValue()->getType()));
1816 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1819 case 55: // reg: GetElemPtr(reg)
1820 case 56: // reg: GetElemPtrIdx(reg,reg)
1821 if (subtreeRoot->parent() != NULL)
1823 // If the parent was a memory operation and not an array access,
1824 // the parent will fold this instruction in so generate nothing.
1826 Instruction* parent =
1827 cast<Instruction>(subtreeRoot->parent()->getValue());
1828 if (parent->getOpcode() == Instruction::Load ||
1829 parent->getOpcode() == Instruction::Store ||
1830 parent->getOpcode() == Instruction::GetElementPtr)
1832 // Check if the parent is an array access,
1833 // If so, we still need to generate this instruction.
1834 GetElementPtrInst* getElemInst =
1835 cast<GetElementPtrInst>(subtreeRoot->getInstruction());
1836 const PointerType* ptrType =
1837 cast<PointerType>(getElemInst->getPointerOperand()->getType());
1838 if (! ptrType->getElementType()->isArrayType())
1839 {// we don't need a separate instr
1840 numInstr = 0; // don't forward operand!
1845 // else in all other cases we need to a separate ADD instruction
1846 mvec[0] = new MachineInstr(ADD);
1847 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1850 case 57: // reg: Alloca: Implement as 1 instruction:
1851 { // add %fp, offsetFromFP -> result
1852 Instruction* instr = subtreeRoot->getInstruction();
1853 const PointerType* instrType = (const PointerType*) instr->getType();
1854 assert(instrType->isPointerType());
1856 target.findOptimalStorageSize(instrType->getElementType());
1857 assert(tsize != 0 && "Just to check when this can happen");
1859 Method* method = instr->getParent()->getParent();
1860 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(method);
1861 int offsetFromFP = mcInfo.allocateLocalVar(target, instr, (unsigned int) tsize);
1863 // Create a temporary Value to hold the constant offset.
1864 // This is needed because it may not fit in the immediate field.
1865 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
1867 // Instruction 1: add %fp, offsetFromFP -> result
1868 mvec[0] = new MachineInstr(ADD);
1869 mvec[0]->SetMachineOperand(0, target.getRegInfo().getFramePointer());
1870 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1872 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1877 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1878 // mul num, typeSz -> tmp
1879 // sub %sp, tmp -> %sp
1880 { // add %sp, frameSizeBelowDynamicArea -> result
1881 Instruction* instr = subtreeRoot->getInstruction();
1882 const PointerType* instrType = (const PointerType*) instr->getType();
1883 assert(instrType->isPointerType() &&
1884 instrType->getElementType()->isArrayType());
1885 const Type* eltType =
1886 ((ArrayType*) instrType->getElementType())->getElementType();
1887 int tsize = (int) target.findOptimalStorageSize(eltType);
1889 assert(tsize != 0 && "Just to check when this can happen");
1891 // Create a temporary Value to hold the constant type-size
1892 ConstantSInt* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
1894 // Create a temporary Value to hold the constant offset from SP
1895 Method* method = instr->getParent()->getParent();
1896 bool ignore; // we don't need this
1897 ConstantSInt* dynamicAreaOffset = ConstantSInt::get(Type::IntTy,
1898 target.getFrameInfo().getDynamicAreaOffset(MachineCodeForMethod::get(method),
1901 // Create a temporary value to hold `tmp'
1902 Instruction* tmpInstr = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
1903 subtreeRoot->leftChild()->getValue(),
1904 NULL /*could insert tsize here*/);
1905 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(tmpInstr);
1907 // Instruction 1: mul numElements, typeSize -> tmp
1908 mvec[0] = new MachineInstr(MULX);
1909 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1910 subtreeRoot->leftChild()->getValue());
1911 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1913 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1916 // Instruction 2: sub %sp, tmp -> %sp
1918 mvec[1] = new MachineInstr(SUB);
1919 mvec[1]->SetMachineOperand(0, target.getRegInfo().getStackPointer());
1920 mvec[1]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1922 mvec[1]->SetMachineOperand(2, target.getRegInfo().getStackPointer());
1924 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
1926 mvec[2] = new MachineInstr(ADD);
1927 mvec[2]->SetMachineOperand(0, target.getRegInfo().getStackPointer());
1928 mvec[2]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1930 mvec[2]->SetMachineOperand(2,MachineOperand::MO_VirtualRegister,instr);
1934 case 61: // reg: Call
1935 { // Generate a call-indirect (i.e., jmpl) for now to expose
1936 // the potential need for registers. If an absolute address
1937 // is available, replace this with a CALL instruction.
1938 // Mark both the indirection register and the return-address
1939 // register as hidden virtual registers.
1940 // Also, mark the operands of the Call and return value (if
1941 // any) as implicit operands of the CALL machine instruction.
1943 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
1944 Value *callee = callInstr->getCalledValue();
1946 Instruction* retAddrReg = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
1949 // Note temporary values in the machineInstrVec for the VM instr.
1951 // WARNING: Operands 0..N-1 must go in slots 0..N-1 of implicitUses.
1952 // The result value must go in slot N. This is assumed
1953 // in register allocation.
1955 callInstr->getMachineInstrVec().addTempValue(retAddrReg);
1958 // Generate the machine instruction and its operands.
1959 // Use CALL for direct function calls; this optimistically assumes
1960 // the PC-relative address fits in the CALL address field (22 bits).
1961 // Use JMPL for indirect calls.
1963 if (callee->getValueType() == Value::MethodVal)
1964 { // direct function call
1965 mvec[0] = new MachineInstr(CALL);
1966 mvec[0]->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp,
1970 { // indirect function call
1971 mvec[0] = new MachineInstr(JMPLCALL);
1972 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1974 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1976 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1980 // Add the call operands and return value as implicit refs
1981 for (unsigned i=0, N=callInstr->getNumOperands(); i < N; ++i)
1982 if (callInstr->getOperand(i) != callee)
1983 mvec[0]->addImplicitRef(callInstr->getOperand(i));
1985 if (callInstr->getType() != Type::VoidTy)
1986 mvec[0]->addImplicitRef(callInstr, /*isDef*/ true);
1988 // For the CALL instruction, the ret. addr. reg. is also implicit
1989 if (callee->getValueType() == Value::MethodVal)
1990 mvec[0]->addImplicitRef(retAddrReg, /*isDef*/ true);
1992 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1996 case 62: // reg: Shl(reg, reg)
1997 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
1998 assert(opType->isIntegral()
1999 || opType == Type::BoolTy
2000 || opType->isPointerType()&& "Shl unsupported for other types");
2001 mvec[0] = new MachineInstr((opType == Type::LongTy)? SLLX : SLL);
2002 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
2006 case 63: // reg: Shr(reg, reg)
2007 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
2008 assert(opType->isIntegral()
2009 || opType == Type::BoolTy
2010 || opType->isPointerType() &&"Shr unsupported for other types");
2011 mvec[0] = new MachineInstr((opType->isSigned()
2012 ? ((opType == Type::LongTy)? SRAX : SRA)
2013 : ((opType == Type::LongTy)? SRLX : SRL)));
2014 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
2018 case 64: // reg: Phi(reg,reg)
2019 numInstr = 0; // don't forward the value
2021 #undef NEED_PHI_MACHINE_INSTRS
2022 #ifdef NEED_PHI_MACHINE_INSTRS
2023 { // This instruction has variable #operands, so resultPos is 0.
2024 Instruction* phi = subtreeRoot->getInstruction();
2025 mvec[0] = new MachineInstr(PHI, 1 + phi->getNumOperands());
2026 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
2027 subtreeRoot->getValue());
2028 for (unsigned i=0, N=phi->getNumOperands(); i < N; i++)
2029 mvec[0]->SetMachineOperand(i+1, MachineOperand::MO_VirtualRegister,
2030 phi->getOperand(i));
2033 #endif NEED_PHI_MACHINE_INSTRS
2035 case 71: // reg: VReg
2036 case 72: // reg: Constant
2037 numInstr = 0; // don't forward the value
2041 assert(0 && "Unrecognized BURG rule");
2047 if (forwardOperandNum >= 0)
2048 { // We did not generate a machine instruction but need to use operand.
2049 // If user is in the same tree, replace Value in its machine operand.
2050 // If not, insert a copy instruction which should get coalesced away
2051 // by register allocation.
2052 if (subtreeRoot->parent() != NULL)
2053 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
2056 vector<MachineInstr*> minstrVec;
2057 target.getInstrInfo().CreateCopyInstructionsByType(target,
2058 subtreeRoot->getInstruction()->getOperand(forwardOperandNum),
2059 subtreeRoot->getInstruction(), minstrVec);
2060 assert(minstrVec.size() > 0);
2061 for (unsigned i=0; i < minstrVec.size(); ++i)
2062 mvec[numInstr++] = minstrVec[i];