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/Support/MathExtras.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/iTerminators.h"
22 #include "llvm/iMemory.h"
23 #include "llvm/iOther.h"
24 #include "llvm/BasicBlock.h"
25 #include "llvm/Method.h"
26 #include "llvm/ConstPoolVals.h"
29 //******************** Internal Data Declarations ************************/
32 //************************* Forward Declarations ***************************/
35 static void SetMemOperands_Internal (MachineInstr* minstr,
36 const InstructionNode* vmInstrNode,
38 Value* arrayOffsetVal,
39 const vector<ConstPoolVal*>& idxVec,
40 const TargetMachine& target);
43 //************************ Internal Functions ******************************/
46 static inline MachineOpCode
47 ChooseBprInstruction(const InstructionNode* instrNode)
51 Instruction* setCCInstr =
52 ((InstructionNode*) instrNode->leftChild())->getInstruction();
54 switch(setCCInstr->getOpcode())
56 case Instruction::SetEQ: opCode = BRZ; break;
57 case Instruction::SetNE: opCode = BRNZ; break;
58 case Instruction::SetLE: opCode = BRLEZ; break;
59 case Instruction::SetGE: opCode = BRGEZ; break;
60 case Instruction::SetLT: opCode = BRLZ; break;
61 case Instruction::SetGT: opCode = BRGZ; break;
63 assert(0 && "Unrecognized VM instruction!");
64 opCode = INVALID_OPCODE;
72 static inline MachineOpCode
73 ChooseBpccInstruction(const InstructionNode* instrNode,
74 const BinaryOperator* setCCInstr)
76 MachineOpCode opCode = INVALID_OPCODE;
78 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
82 switch(setCCInstr->getOpcode())
84 case Instruction::SetEQ: opCode = BE; break;
85 case Instruction::SetNE: opCode = BNE; break;
86 case Instruction::SetLE: opCode = BLE; break;
87 case Instruction::SetGE: opCode = BGE; break;
88 case Instruction::SetLT: opCode = BL; break;
89 case Instruction::SetGT: opCode = BG; break;
91 assert(0 && "Unrecognized VM instruction!");
97 switch(setCCInstr->getOpcode())
99 case Instruction::SetEQ: opCode = BE; break;
100 case Instruction::SetNE: opCode = BNE; break;
101 case Instruction::SetLE: opCode = BLEU; break;
102 case Instruction::SetGE: opCode = BCC; break;
103 case Instruction::SetLT: opCode = BCS; break;
104 case Instruction::SetGT: opCode = BGU; break;
106 assert(0 && "Unrecognized VM instruction!");
114 static inline MachineOpCode
115 ChooseBFpccInstruction(const InstructionNode* instrNode,
116 const BinaryOperator* setCCInstr)
118 MachineOpCode opCode = INVALID_OPCODE;
120 switch(setCCInstr->getOpcode())
122 case Instruction::SetEQ: opCode = FBE; break;
123 case Instruction::SetNE: opCode = FBNE; break;
124 case Instruction::SetLE: opCode = FBLE; break;
125 case Instruction::SetGE: opCode = FBGE; break;
126 case Instruction::SetLT: opCode = FBL; break;
127 case Instruction::SetGT: opCode = FBG; break;
129 assert(0 && "Unrecognized VM instruction!");
137 // Create a unique TmpInstruction for a boolean value,
138 // representing the CC register used by a branch on that value.
139 // For now, hack this using a little static cache of TmpInstructions.
140 // Eventually the entire BURG instruction selection should be put
141 // into a separate class that can hold such information.
142 // The static cache is not too bad because that memory for these
143 // TmpInstructions will be freed elsewhere in any case.
145 static TmpInstruction*
146 GetTmpForCC(Value* boolVal, const Method* method)
148 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
149 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
150 static const Method* lastMethod = NULL; // Use to flush cache between methods
152 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
154 if (lastMethod != method)
157 boolToTmpCache.clear();
160 // Look for tmpI and create a new one otherswise.
161 // new value is directly written to map using
162 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
164 tmpI = new TmpInstruction(TMP_INSTRUCTION_OPCODE, boolVal, NULL);
170 static inline MachineOpCode
171 ChooseBccInstruction(const InstructionNode* instrNode,
174 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
175 BinaryOperator* setCCInstr = (BinaryOperator*) setCCNode->getInstruction();
176 const Type* setCCType = setCCInstr->getOperand(0)->getType();
178 isFPBranch = (setCCType == Type::FloatTy || setCCType == Type::DoubleTy);
181 return ChooseBFpccInstruction(instrNode, setCCInstr);
183 return ChooseBpccInstruction(instrNode, setCCInstr);
187 static inline MachineOpCode
188 ChooseMovFpccInstruction(const InstructionNode* instrNode)
190 MachineOpCode opCode = INVALID_OPCODE;
192 switch(instrNode->getInstruction()->getOpcode())
194 case Instruction::SetEQ: opCode = MOVFE; break;
195 case Instruction::SetNE: opCode = MOVFNE; break;
196 case Instruction::SetLE: opCode = MOVFLE; break;
197 case Instruction::SetGE: opCode = MOVFGE; break;
198 case Instruction::SetLT: opCode = MOVFL; break;
199 case Instruction::SetGT: opCode = MOVFG; break;
201 assert(0 && "Unrecognized VM instruction!");
209 // Assumes that SUBcc v1, v2 -> v3 has been executed.
210 // In most cases, we want to clear v3 and then follow it by instruction
212 // Set mustClearReg=false if v3 need not be cleared before conditional move.
213 // Set valueToMove=0 if we want to conditionally move 0 instead of 1
214 // (i.e., we want to test inverse of a condition)
215 // (The latter two cases do not seem to arise because SetNE needs nothing.)
218 ChooseMovpccAfterSub(const InstructionNode* instrNode,
222 MachineOpCode opCode = INVALID_OPCODE;
226 switch(instrNode->getInstruction()->getOpcode())
228 case Instruction::SetEQ: opCode = MOVE; break;
229 case Instruction::SetLE: opCode = MOVLE; break;
230 case Instruction::SetGE: opCode = MOVGE; break;
231 case Instruction::SetLT: opCode = MOVL; break;
232 case Instruction::SetGT: opCode = MOVG; break;
233 case Instruction::SetNE: assert(0 && "No move required!"); break;
234 default: assert(0 && "Unrecognized VM instr!"); break;
240 static inline MachineOpCode
241 ChooseConvertToFloatInstr(const InstructionNode* instrNode,
244 MachineOpCode opCode = INVALID_OPCODE;
246 switch(instrNode->getOpLabel())
249 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
251 else if (opType == Type::LongTy)
253 else if (opType == Type::DoubleTy)
255 else if (opType == Type::FloatTy)
258 assert(0 && "Cannot convert this type to FLOAT on SPARC");
262 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
264 else if (opType == Type::LongTy)
266 else if (opType == Type::FloatTy)
268 else if (opType == Type::DoubleTy)
271 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
281 static inline MachineOpCode
282 ChooseConvertToIntInstr(const InstructionNode* instrNode,
285 MachineOpCode opCode = INVALID_OPCODE;;
287 int instrType = (int) instrNode->getOpLabel();
289 if (instrType == ToSByteTy || instrType == ToShortTy || instrType == ToIntTy)
291 switch (opType->getPrimitiveID())
293 case Type::FloatTyID: opCode = FSTOI; break;
294 case Type::DoubleTyID: opCode = FDTOI; break;
296 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
300 else if (instrType == ToLongTy)
302 switch (opType->getPrimitiveID())
304 case Type::FloatTyID: opCode = FSTOX; break;
305 case Type::DoubleTyID: opCode = FDTOX; break;
307 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
312 assert(0 && "Should not get here, Mo!");
318 static inline MachineOpCode
319 ChooseAddInstructionByType(const Type* resultType)
321 MachineOpCode opCode = INVALID_OPCODE;
323 if (resultType->isIntegral() ||
324 isa<PointerType>(resultType) ||
325 isa<MethodType>(resultType) ||
326 resultType->isLabelType() ||
327 resultType == Type::BoolTy)
332 switch(resultType->getPrimitiveID())
334 case Type::FloatTyID: opCode = FADDS; break;
335 case Type::DoubleTyID: opCode = FADDD; break;
336 default: assert(0 && "Invalid type for ADD instruction"); break;
343 static inline MachineOpCode
344 ChooseAddInstruction(const InstructionNode* instrNode)
346 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
350 static inline MachineInstr*
351 CreateMovFloatInstruction(const InstructionNode* instrNode,
352 const Type* resultType)
354 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
356 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
357 instrNode->leftChild()->getValue());
358 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
359 instrNode->getValue());
363 static inline MachineInstr*
364 CreateAddConstInstruction(const InstructionNode* instrNode)
366 MachineInstr* minstr = NULL;
368 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
369 assert(isa<ConstPoolVal>(constOp));
371 // Cases worth optimizing are:
372 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
373 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
375 const Type* resultType = instrNode->getInstruction()->getType();
377 if (resultType == Type::FloatTy ||
378 resultType == Type::DoubleTy)
380 double dval = ((ConstPoolFP*) constOp)->getValue();
382 minstr = CreateMovFloatInstruction(instrNode, resultType);
389 static inline MachineOpCode
390 ChooseSubInstructionByType(const Type* resultType)
392 MachineOpCode opCode = INVALID_OPCODE;
394 if (resultType->isIntegral() ||
395 resultType->isPointerType())
400 switch(resultType->getPrimitiveID())
402 case Type::FloatTyID: opCode = FSUBS; break;
403 case Type::DoubleTyID: opCode = FSUBD; break;
404 default: assert(0 && "Invalid type for SUB instruction"); break;
411 static inline MachineInstr*
412 CreateSubConstInstruction(const InstructionNode* instrNode)
414 MachineInstr* minstr = NULL;
416 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
417 assert(isa<ConstPoolVal>(constOp));
419 // Cases worth optimizing are:
420 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
421 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
423 const Type* resultType = instrNode->getInstruction()->getType();
425 if (resultType == Type::FloatTy ||
426 resultType == Type::DoubleTy)
428 double dval = ((ConstPoolFP*) constOp)->getValue();
430 minstr = CreateMovFloatInstruction(instrNode, resultType);
437 static inline MachineOpCode
438 ChooseFcmpInstruction(const InstructionNode* instrNode)
440 MachineOpCode opCode = INVALID_OPCODE;
442 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
443 switch(operand->getType()->getPrimitiveID()) {
444 case Type::FloatTyID: opCode = FCMPS; break;
445 case Type::DoubleTyID: opCode = FCMPD; break;
446 default: assert(0 && "Invalid type for FCMP instruction"); break;
453 // Assumes that leftArg and rightArg are both cast instructions.
456 BothFloatToDouble(const InstructionNode* instrNode)
458 InstrTreeNode* leftArg = instrNode->leftChild();
459 InstrTreeNode* rightArg = instrNode->rightChild();
460 InstrTreeNode* leftArgArg = leftArg->leftChild();
461 InstrTreeNode* rightArgArg = rightArg->leftChild();
462 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
464 // Check if both arguments are floats cast to double
465 return (leftArg->getValue()->getType() == Type::DoubleTy &&
466 leftArgArg->getValue()->getType() == Type::FloatTy &&
467 rightArgArg->getValue()->getType() == Type::FloatTy);
471 static inline MachineOpCode
472 ChooseMulInstructionByType(const Type* resultType)
474 MachineOpCode opCode = INVALID_OPCODE;
476 if (resultType->isIntegral())
479 switch(resultType->getPrimitiveID())
481 case Type::FloatTyID: opCode = FMULS; break;
482 case Type::DoubleTyID: opCode = FMULD; break;
483 default: assert(0 && "Invalid type for MUL instruction"); break;
490 static inline MachineOpCode
491 ChooseMulInstruction(const InstructionNode* instrNode,
494 if (checkCasts && BothFloatToDouble(instrNode))
497 // else use the regular multiply instructions
498 return ChooseMulInstructionByType(instrNode->getInstruction()->getType());
502 static inline MachineInstr*
503 CreateIntNegInstruction(TargetMachine& target,
506 MachineInstr* minstr = new MachineInstr(SUB);
507 minstr->SetMachineOperand(0, target.getRegInfo().getZeroRegNum());
508 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, vreg);
509 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, vreg);
514 static inline MachineInstr*
515 CreateMulConstInstruction(TargetMachine &target,
516 const InstructionNode* instrNode,
517 MachineInstr*& getMinstr2)
519 MachineInstr* minstr = NULL;
521 bool needNeg = false;
523 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
524 assert(isa<ConstPoolVal>(constOp));
526 // Cases worth optimizing are:
527 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
528 // (2) Multiply by 2^x for integer types: replace with Shift
530 const Type* resultType = instrNode->getInstruction()->getType();
532 if (resultType->isIntegral() || resultType->isPointerType())
536 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
539 bool needNeg = false;
546 if (C == 0 || C == 1)
548 minstr = new MachineInstr(ADD);
551 minstr->SetMachineOperand(0,
552 target.getRegInfo().getZeroRegNum());
554 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
555 instrNode->leftChild()->getValue());
556 minstr->SetMachineOperand(1,target.getRegInfo().getZeroRegNum());
558 else if (IsPowerOf2(C, pow))
560 minstr = new MachineInstr((resultType == Type::LongTy)
562 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
563 instrNode->leftChild()->getValue());
564 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
568 if (minstr && needNeg)
569 { // insert <reg = SUB 0, reg> after the instr to flip the sign
570 getMinstr2 = CreateIntNegInstruction(target,
571 instrNode->getValue());
577 if (resultType == Type::FloatTy ||
578 resultType == Type::DoubleTy)
581 double dval = ((ConstPoolFP*) constOp)->getValue();
587 minstr = new MachineInstr((resultType == Type::FloatTy)
589 minstr->SetMachineOperand(0,
590 target.getRegInfo().getZeroRegNum());
592 else if (fabs(dval) == 1)
594 bool needNeg = (dval < 0);
596 MachineOpCode opCode = needNeg
597 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
598 : (resultType == Type::FloatTy? FMOVS : FMOVD);
600 minstr = new MachineInstr(opCode);
601 minstr->SetMachineOperand(0,
602 MachineOperand::MO_VirtualRegister,
603 instrNode->leftChild()->getValue());
610 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
611 instrNode->getValue());
617 // Generate a divide instruction for Div or Rem.
618 // For Rem, this assumes that the operand type will be signed if the result
619 // type is signed. This is correct because they must have the same sign.
621 static inline MachineOpCode
622 ChooseDivInstruction(TargetMachine &target,
623 const InstructionNode* instrNode)
625 MachineOpCode opCode = INVALID_OPCODE;
627 const Type* resultType = instrNode->getInstruction()->getType();
629 if (resultType->isIntegral())
630 opCode = resultType->isSigned()? SDIVX : UDIVX;
632 switch(resultType->getPrimitiveID())
634 case Type::FloatTyID: opCode = FDIVS; break;
635 case Type::DoubleTyID: opCode = FDIVD; break;
636 default: assert(0 && "Invalid type for DIV instruction"); break;
643 static inline MachineInstr*
644 CreateDivConstInstruction(TargetMachine &target,
645 const InstructionNode* instrNode,
646 MachineInstr*& getMinstr2)
648 MachineInstr* minstr = NULL;
651 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
652 assert(isa<ConstPoolVal>(constOp));
654 // Cases worth optimizing are:
655 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
656 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
658 const Type* resultType = instrNode->getInstruction()->getType();
660 if (resultType->isIntegral())
664 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
667 bool needNeg = false;
676 minstr = new MachineInstr(ADD);
677 minstr->SetMachineOperand(0,MachineOperand::MO_VirtualRegister,
678 instrNode->leftChild()->getValue());
679 minstr->SetMachineOperand(1,target.getRegInfo().getZeroRegNum());
681 else if (IsPowerOf2(C, pow))
683 MachineOpCode opCode= ((resultType->isSigned())
684 ? (resultType==Type::LongTy)? SRAX : SRA
685 : (resultType==Type::LongTy)? SRLX : SRL);
686 minstr = new MachineInstr(opCode);
687 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
688 instrNode->leftChild()->getValue());
689 minstr->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
693 if (minstr && needNeg)
694 { // insert <reg = SUB 0, reg> after the instr to flip the sign
695 getMinstr2 = CreateIntNegInstruction(target,
696 instrNode->getValue());
702 if (resultType == Type::FloatTy ||
703 resultType == Type::DoubleTy)
706 double dval = ((ConstPoolFP*) constOp)->getValue();
708 if (isValidConst && fabs(dval) == 1)
710 bool needNeg = (dval < 0);
712 MachineOpCode opCode = needNeg
713 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
714 : (resultType == Type::FloatTy? FMOVS : FMOVD);
716 minstr = new MachineInstr(opCode);
717 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
718 instrNode->leftChild()->getValue());
724 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
725 instrNode->getValue());
731 //------------------------------------------------------------------------
732 // Function SetOperandsForMemInstr
734 // Choose addressing mode for the given load or store instruction.
735 // Use [reg+reg] if it is an indexed reference, and the index offset is
736 // not a constant or if it cannot fit in the offset field.
737 // Use [reg+offset] in all other cases.
739 // This assumes that all array refs are "lowered" to one of these forms:
740 // %x = load (subarray*) ptr, constant ; single constant offset
741 // %x = load (subarray*) ptr, offsetVal ; single non-constant offset
742 // Generally, this should happen via strength reduction + LICM.
743 // Also, strength reduction should take care of using the same register for
744 // the loop index variable and an array index, when that is profitable.
745 //------------------------------------------------------------------------
748 SetOperandsForMemInstr(MachineInstr* minstr,
749 const InstructionNode* vmInstrNode,
750 const TargetMachine& target)
752 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
754 // Variables to hold the index vector, ptr value, and offset value.
755 // The major work here is to extract these for all 3 instruction types
756 // and then call the common function SetMemOperands_Internal().
758 const vector<ConstPoolVal*>* idxVec = &memInst->getIndices();
759 vector<ConstPoolVal*>* newIdxVec = NULL;
761 Value* arrayOffsetVal = NULL;
763 // Test if a GetElemPtr instruction is being folded into this mem instrn.
764 // If so, it will be in the left child for Load and GetElemPtr,
765 // and in the right child for Store instructions.
767 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
768 ? vmInstrNode->rightChild()
769 : vmInstrNode->leftChild());
771 if (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
772 ptrChild->getOpLabel() == GetElemPtrIdx)
774 // There is a GetElemPtr instruction and there may be a chain of
775 // more than one. Use the pointer value of the last one in the chain.
776 // Fold the index vectors from the entire chain and from the mem
777 // instruction into one single index vector.
778 // Finally, we never fold for an array instruction so make that NULL.
780 newIdxVec = new vector<ConstPoolVal*>;
781 ptrVal = FoldGetElemChain((InstructionNode*) ptrChild, *newIdxVec);
783 newIdxVec->insert(newIdxVec->end(), idxVec->begin(), idxVec->end());
786 assert(! ((PointerType*)ptrVal->getType())->getValueType()->isArrayType()
787 && "GetElemPtr cannot be folded into array refs in selection");
791 // There is no GetElemPtr instruction.
792 // Use the pointer value and the index vector from the Mem instruction.
793 // If it is an array reference, get the array offset value.
795 ptrVal = memInst->getPtrOperand();
798 ((const PointerType*) ptrVal->getType())->getValueType();
799 if (opType->isArrayType())
801 assert((memInst->getNumOperands()
802 == (unsigned) 1 + memInst->getFirstOffsetIdx())
803 && "Array refs must be lowered before Instruction Selection");
805 arrayOffsetVal = memInst->getOperand(memInst->getFirstOffsetIdx());
809 SetMemOperands_Internal(minstr, vmInstrNode, ptrVal, arrayOffsetVal,
812 if (newIdxVec != NULL)
818 SetMemOperands_Internal(MachineInstr* minstr,
819 const InstructionNode* vmInstrNode,
821 Value* arrayOffsetVal,
822 const vector<ConstPoolVal*>& idxVec,
823 const TargetMachine& target)
825 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
827 // Initialize so we default to storing the offset in a register.
828 int64_t smallConstOffset;
829 Value* valueForRegOffset = NULL;
830 MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister;
832 // Check if there is an index vector and if so, if it translates to
833 // a small enough constant to fit in the immediate-offset field.
835 if (idxVec.size() > 0)
837 bool isConstantOffset = false;
840 const PointerType* ptrType = (PointerType*) ptrVal->getType();
842 if (ptrType->getValueType()->isStructType())
844 // the offset is always constant for structs
845 isConstantOffset = true;
847 // Compute the offset value using the index vector
848 offset = target.DataLayout.getIndexedOffset(ptrType, idxVec);
852 // It must be an array ref. Check if the offset is a constant,
853 // and that the indexing has been lowered to a single offset.
855 assert(ptrType->getValueType()->isArrayType());
856 assert(arrayOffsetVal != NULL
857 && "Expect to be given Value* for array offsets");
859 if (ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(arrayOffsetVal))
861 isConstantOffset = true; // always constant for structs
862 assert(arrayOffsetVal->getType()->isIntegral());
863 offset = (CPV->getType()->isSigned()
864 ? ((ConstPoolSInt*)CPV)->getValue()
865 : (int64_t) ((ConstPoolUInt*)CPV)->getValue());
869 valueForRegOffset = arrayOffsetVal;
873 if (isConstantOffset)
875 // create a virtual register for the constant
876 valueForRegOffset = ConstPoolSInt::get(Type::IntTy, offset);
881 offsetOpType = MachineOperand::MO_SignExtendedImmed;
882 smallConstOffset = 0;
885 // Operand 0 is value for STORE, ptr for LOAD or GET_ELEMENT_PTR
886 // It is the left child in the instruction tree in all cases.
887 Value* leftVal = vmInstrNode->leftChild()->getValue();
888 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, leftVal);
890 // Operand 1 is ptr for STORE, offset for LOAD or GET_ELEMENT_PTR
891 // Operand 2 is offset for STORE, result reg for LOAD or GET_ELEMENT_PTR
893 unsigned offsetOpNum = (memInst->getOpcode() == Instruction::Store)? 2 : 1;
894 if (offsetOpType == MachineOperand::MO_VirtualRegister)
896 assert(valueForRegOffset != NULL);
897 minstr->SetMachineOperand(offsetOpNum, offsetOpType, valueForRegOffset);
900 minstr->SetMachineOperand(offsetOpNum, offsetOpType, smallConstOffset);
902 if (memInst->getOpcode() == Instruction::Store)
903 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, ptrVal);
905 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
906 vmInstrNode->getValue());
911 // Substitute operand `operandNum' of the instruction in node `treeNode'
912 // in place of the use(s) of that instruction in node `parent'.
913 // Check both explicit and implicit operands!
916 ForwardOperand(InstructionNode* treeNode,
917 InstrTreeNode* parent,
920 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
922 Instruction* unusedOp = treeNode->getInstruction();
923 Value* fwdOp = unusedOp->getOperand(operandNum);
925 // The parent itself may be a list node, so find the real parent instruction
926 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
928 parent = parent->parent();
929 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
931 InstructionNode* parentInstrNode = (InstructionNode*) parent;
933 Instruction* userInstr = parentInstrNode->getInstruction();
934 MachineCodeForVMInstr& mvec = userInstr->getMachineInstrVec();
935 for (unsigned i=0, N=mvec.size(); i < N; i++)
937 MachineInstr* minstr = mvec[i];
939 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
941 const MachineOperand& mop = minstr->getOperand(i);
942 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
943 mop.getVRegValue() == unusedOp)
945 minstr->SetMachineOperand(i, MachineOperand::MO_VirtualRegister,
950 for (unsigned i=0, numOps=minstr->getNumImplicitRefs(); i < numOps; ++i)
951 if (minstr->getImplicitRef(i) == unusedOp)
952 minstr->setImplicitRef(i, fwdOp, minstr->implicitRefIsDefined(i));
958 CreateCopyInstructionsByType(const TargetMachine& target,
961 vector<MachineInstr*>& minstrVec)
963 bool loadConstantToReg = false;
965 const Type* resultType = dest->getType();
967 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
968 if (opCode == INVALID_OPCODE)
970 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
974 // if `src' is a constant that doesn't fit in the immed field or if it is
975 // a global variable (i.e., a constant address), generate a load
976 // instruction instead of an add
978 if (isa<ConstPoolVal>(src))
980 unsigned int machineRegNum;
982 MachineOperand::MachineOperandType opType =
983 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
984 machineRegNum, immedValue);
986 if (opType == MachineOperand::MO_VirtualRegister)
987 loadConstantToReg = true;
989 else if (isa<GlobalValue>(src))
990 loadConstantToReg = true;
992 if (loadConstantToReg)
993 { // `src' is constant and cannot fit in immed field for the ADD
994 // Insert instructions to "load" the constant into a register
995 vector<TmpInstruction*> tempVec;
996 target.getInstrInfo().CreateCodeToLoadConst(src,dest,minstrVec,tempVec);
997 for (unsigned i=0; i < tempVec.size(); i++)
998 dest->getMachineInstrVec().addTempValue(tempVec[i]);
1001 { // Create the appropriate add instruction.
1002 // Make `src' the second operand, in case it is a constant
1003 // Use (unsigned long) 0 for a NULL pointer value.
1005 const Type* nullValueType =
1006 (resultType->getPrimitiveID() == Type::PointerTyID)? Type::ULongTy
1008 MachineInstr* minstr = new MachineInstr(opCode);
1009 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1010 ConstPoolVal::getNullConstant(nullValueType));
1011 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, src);
1012 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, dest);
1013 minstrVec.push_back(minstr);
1018 //******************* Externally Visible Functions *************************/
1021 //------------------------------------------------------------------------
1022 // External Function: GetInstructionsForProlog
1023 // External Function: GetInstructionsForEpilog
1026 // Create prolog and epilog code for procedure entry and exit
1027 //------------------------------------------------------------------------
1030 GetInstructionsForProlog(BasicBlock* entryBB,
1031 TargetMachine &target,
1032 MachineInstr** mvec)
1034 int64_t s0=0; // used to avoid overloading ambiguity below
1036 // The second operand is the stack size. If it does not fit in the
1037 // immediate field, we either have to find an unused register in the
1038 // caller's window or move some elements to the dynamically allocated
1039 // area of the stack frame (just above save area and method args).
1040 Method* method = entryBB->getParent();
1041 MachineCodeForMethod& mcodeInfo = method->getMachineCode();
1042 unsigned int staticStackSize = mcodeInfo.getStaticStackSize();
1044 assert(target.getInstrInfo().constantFitsInImmedField(SAVE, staticStackSize)
1045 && "Stack size too large for immediate field of SAVE instruction. Need additional work as described in the comment above");
1047 mvec[0] = new MachineInstr(SAVE);
1048 mvec[0]->SetMachineOperand(0, target.getRegInfo().getStackPointer());
1049 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1051 mvec[0]->SetMachineOperand(2, target.getRegInfo().getStackPointer());
1058 GetInstructionsForEpilog(BasicBlock* anExitBB,
1059 TargetMachine &target,
1060 MachineInstr** mvec)
1062 int64_t s0=0; // used to avoid overloading ambiguity below
1064 mvec[0] = new MachineInstr(RESTORE);
1065 mvec[0]->SetMachineOperand(0, target.getRegInfo().getZeroRegNum());
1066 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed, s0);
1067 mvec[0]->SetMachineOperand(2, target.getRegInfo().getZeroRegNum());
1073 //------------------------------------------------------------------------
1074 // External Function: ThisIsAChainRule
1077 // Check if a given BURG rule is a chain rule.
1078 //------------------------------------------------------------------------
1081 ThisIsAChainRule(int eruleno)
1085 case 111: // stmt: reg
1086 case 113: // stmt: bool
1108 return false; break;
1113 //------------------------------------------------------------------------
1114 // External Function: GetInstructionsByRule
1117 // Choose machine instructions for the SPARC according to the
1118 // patterns chosen by the BURG-generated parser.
1119 //------------------------------------------------------------------------
1122 GetInstructionsByRule(InstructionNode* subtreeRoot,
1126 MachineInstr** mvec)
1128 int numInstr = 1; // initialize for common case
1129 bool checkCast = false; // initialize here to use fall-through
1130 Value *leftVal, *rightVal;
1133 int forwardOperandNum = -1;
1134 int64_t s0=0, s8=8; // variables holding constants to avoid
1135 uint64_t u0=0; // overloading ambiguities below
1137 UltraSparc& target = (UltraSparc&) tgt;
1139 for (unsigned i=0; i < MAX_INSTR_PER_VMINSTR; i++)
1143 // Let's check for chain rules outside the switch so that we don't have
1144 // to duplicate the list of chain rule production numbers here again
1146 if (ThisIsAChainRule(ruleForNode))
1148 // Chain rules have a single nonterminal on the RHS.
1149 // Get the rule that matches the RHS non-terminal and use that instead.
1151 assert(nts[0] && ! nts[1]
1152 && "A chain rule should have only one RHS non-terminal!");
1153 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1154 nts = burm_nts[nextRule];
1155 numInstr = GetInstructionsByRule(subtreeRoot, nextRule, nts,target,mvec);
1159 switch(ruleForNode) {
1160 case 1: // stmt: Ret
1161 case 2: // stmt: RetValue(reg)
1162 { // NOTE: Prepass of register allocation is responsible
1163 // for moving return value to appropriate register.
1164 // Mark the return-address register as a hidden virtual reg.
1165 // Mark the return value register as an implicit ref of
1166 // the machine instruction.
1167 // Finally put a NOP in the delay slot.
1168 ReturnInst* returnInstr = (ReturnInst*) subtreeRoot->getInstruction();
1169 assert(returnInstr->getOpcode() == Instruction::Ret);
1170 Method* method = returnInstr->getParent()->getParent();
1172 Instruction* returnReg = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
1174 returnInstr->getMachineInstrVec().addTempValue(returnReg);
1176 mvec[0] = new MachineInstr(JMPLRET);
1177 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1179 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,s8);
1180 mvec[0]->SetMachineOperand(2, target.getRegInfo().getZeroRegNum());
1182 if (returnInstr->getReturnValue() != NULL)
1183 mvec[0]->addImplicitRef(returnInstr->getReturnValue());
1185 unsigned n = numInstr++; // delay slot
1186 mvec[n] = new MachineInstr(NOP);
1191 case 3: // stmt: Store(reg,reg)
1192 case 4: // stmt: Store(reg,ptrreg)
1193 mvec[0] = new MachineInstr(
1194 ChooseStoreInstruction(
1195 subtreeRoot->leftChild()->getValue()->getType()));
1196 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1199 case 5: // stmt: BrUncond
1200 mvec[0] = new MachineInstr(BA);
1201 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1203 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1204 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1207 mvec[numInstr++] = new MachineInstr(NOP);
1210 case 206: // stmt: BrCond(setCCconst)
1211 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
1212 // If the constant is ZERO, we can use the branch-on-integer-register
1213 // instructions and avoid the SUBcc instruction entirely.
1214 // Otherwise this is just the same as case 5, so just fall through.
1216 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1218 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
1219 ConstPoolVal* constVal = (ConstPoolVal*) constNode->getValue();
1222 if ((constVal->getType()->isIntegral()
1223 || constVal->getType()->isPointerType())
1224 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1227 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1229 // That constant is a zero after all...
1230 // Use the left child of setCC as the first argument!
1231 mvec[0] = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1232 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1233 subtreeRoot->leftChild()->leftChild()->getValue());
1234 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1235 brInst->getSuccessor(0));
1238 mvec[numInstr++] = new MachineInstr(NOP);
1242 mvec[n] = new MachineInstr(BA);
1243 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1245 mvec[n]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1246 brInst->getSuccessor(1));
1249 mvec[numInstr++] = new MachineInstr(NOP);
1253 // ELSE FALL THROUGH
1256 case 6: // stmt: BrCond(bool)
1257 { // bool => boolean was computed with some boolean operator
1258 // (SetCC, Not, ...). We need to check whether the type was a FP,
1259 // signed int or unsigned int, and check the branching condition in
1260 // order to choose the branch to use.
1261 // If it is an integer CC, we also need to find the unique
1262 // TmpInstruction representing that CC.
1264 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
1266 mvec[0] = new MachineInstr(ChooseBccInstruction(subtreeRoot,
1269 Value* ccValue = isFPBranch? subtreeRoot->leftChild()->getValue()
1270 : GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1271 brInst->getParent()->getParent());
1273 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister, ccValue);
1274 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1275 brInst->getSuccessor(0));
1278 mvec[numInstr++] = new MachineInstr(NOP);
1282 mvec[n] = new MachineInstr(BA);
1283 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1285 mvec[n]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1286 brInst->getSuccessor(1));
1289 mvec[numInstr++] = new MachineInstr(NOP);
1293 case 208: // stmt: BrCond(boolconst)
1295 // boolconst => boolean is a constant; use BA to first or second label
1296 ConstPoolVal* constVal =
1297 cast<ConstPoolVal>(subtreeRoot->leftChild()->getValue());
1298 unsigned dest = ((ConstPoolBool*) constVal)->getValue()? 0 : 1;
1300 mvec[0] = new MachineInstr(BA);
1301 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1303 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1304 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(dest));
1307 mvec[numInstr++] = new MachineInstr(NOP);
1311 case 8: // stmt: BrCond(boolreg)
1312 { // boolreg => boolean is stored in an existing register.
1313 // Just use the branch-on-integer-register instruction!
1315 mvec[0] = new MachineInstr(BRNZ);
1316 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1317 subtreeRoot->leftChild()->getValue());
1318 mvec[0]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1319 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
1322 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1326 mvec[n] = new MachineInstr(BA);
1327 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1329 mvec[n]->SetMachineOperand(1, MachineOperand::MO_PCRelativeDisp,
1330 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
1333 mvec[numInstr++] = new MachineInstr(NOP);
1337 case 9: // stmt: Switch(reg)
1338 assert(0 && "*** SWITCH instruction is not implemented yet.");
1342 case 10: // reg: VRegList(reg, reg)
1343 assert(0 && "VRegList should never be the topmost non-chain rule");
1346 case 21: // reg: Not(reg): Implemented as reg = reg XOR-NOT 0
1347 mvec[0] = new MachineInstr(XNOR);
1348 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1349 subtreeRoot->leftChild()->getValue());
1350 mvec[0]->SetMachineOperand(1, target.getRegInfo().getZeroRegNum());
1351 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1352 subtreeRoot->getValue());
1355 case 322: // reg: ToBoolTy(bool):
1356 case 22: // reg: ToBoolTy(reg):
1357 opType = subtreeRoot->leftChild()->getValue()->getType();
1358 assert(opType->isIntegral() || opType == Type::BoolTy);
1360 forwardOperandNum = 0;
1363 case 23: // reg: ToUByteTy(reg)
1364 case 25: // reg: ToUShortTy(reg)
1365 case 27: // reg: ToUIntTy(reg)
1366 case 29: // reg: ToULongTy(reg)
1367 opType = subtreeRoot->leftChild()->getValue()->getType();
1368 assert(opType->isIntegral() ||
1369 opType->isPointerType() ||
1370 opType == Type::BoolTy && "Cast is illegal for other types");
1372 forwardOperandNum = 0;
1375 case 24: // reg: ToSByteTy(reg)
1376 case 26: // reg: ToShortTy(reg)
1377 case 28: // reg: ToIntTy(reg)
1378 case 30: // reg: ToLongTy(reg)
1379 opType = subtreeRoot->leftChild()->getValue()->getType();
1380 if (opType->isIntegral() || opType == Type::BoolTy)
1383 forwardOperandNum = 0;
1387 mvec[0] = new MachineInstr(ChooseConvertToIntInstr(subtreeRoot,
1389 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
1393 case 31: // reg: ToFloatTy(reg):
1394 case 32: // reg: ToDoubleTy(reg):
1395 case 232: // reg: ToDoubleTy(Constant):
1397 // If this instruction has a parent (a user) in the tree
1398 // and the user is translated as an FsMULd instruction,
1399 // then the cast is unnecessary. So check that first.
1400 // In the future, we'll want to do the same for the FdMULq instruction,
1401 // so do the check here instead of only for ToFloatTy(reg).
1403 if (subtreeRoot->parent() != NULL &&
1404 ((InstructionNode*) subtreeRoot->parent())->getInstruction()->getMachineInstrVec()[0]->getOpCode() == FSMULD)
1407 forwardOperandNum = 0;
1411 opType = subtreeRoot->leftChild()->getValue()->getType();
1412 MachineOpCode opCode=ChooseConvertToFloatInstr(subtreeRoot,opType);
1413 if (opCode == INVALID_OPCODE) // no conversion needed
1416 forwardOperandNum = 0;
1420 mvec[0] = new MachineInstr(opCode);
1421 Set2OperandsFromInstr(mvec[0], subtreeRoot, target);
1426 case 19: // reg: ToArrayTy(reg):
1427 case 20: // reg: ToPointerTy(reg):
1429 forwardOperandNum = 0;
1432 case 233: // reg: Add(reg, Constant)
1433 mvec[0] = CreateAddConstInstruction(subtreeRoot);
1434 if (mvec[0] != NULL)
1436 // ELSE FALL THROUGH
1438 case 33: // reg: Add(reg, reg)
1439 mvec[0] = new MachineInstr(ChooseAddInstruction(subtreeRoot));
1440 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1443 case 234: // reg: Sub(reg, Constant)
1444 mvec[0] = CreateSubConstInstruction(subtreeRoot);
1445 if (mvec[0] != NULL)
1447 // ELSE FALL THROUGH
1449 case 34: // reg: Sub(reg, reg)
1450 mvec[0] = new MachineInstr(ChooseSubInstructionByType(
1451 subtreeRoot->getInstruction()->getType()));
1452 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1455 case 135: // reg: Mul(todouble, todouble)
1459 case 35: // reg: Mul(reg, reg)
1460 mvec[0] =new MachineInstr(ChooseMulInstruction(subtreeRoot,checkCast));
1461 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1464 case 335: // reg: Mul(todouble, todoubleConst)
1468 case 235: // reg: Mul(reg, Constant)
1469 mvec[0] = CreateMulConstInstruction(target, subtreeRoot, mvec[1]);
1470 if (mvec[0] == NULL)
1472 mvec[0] = new MachineInstr(ChooseMulInstruction(subtreeRoot,
1474 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1477 if (mvec[1] != NULL)
1481 case 236: // reg: Div(reg, Constant)
1482 mvec[0] = CreateDivConstInstruction(target, subtreeRoot, mvec[1]);
1483 if (mvec[0] != NULL)
1485 if (mvec[1] != NULL)
1489 // ELSE FALL THROUGH
1491 case 36: // reg: Div(reg, reg)
1492 mvec[0] = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1493 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1496 case 37: // reg: Rem(reg, reg)
1497 case 237: // reg: Rem(reg, Constant)
1499 Instruction* remInstr = subtreeRoot->getInstruction();
1501 TmpInstruction* quot = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
1502 subtreeRoot->leftChild()->getValue(),
1503 subtreeRoot->rightChild()->getValue());
1504 TmpInstruction* prod = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
1506 subtreeRoot->rightChild()->getValue());
1507 remInstr->getMachineInstrVec().addTempValue(quot);
1508 remInstr->getMachineInstrVec().addTempValue(prod);
1510 mvec[0] = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1511 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1512 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,quot);
1515 mvec[n] = new MachineInstr(ChooseMulInstructionByType(
1516 subtreeRoot->getInstruction()->getType()));
1517 mvec[n]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,quot);
1518 mvec[n]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1519 subtreeRoot->rightChild()->getValue());
1520 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,prod);
1523 mvec[n] = new MachineInstr(ChooseSubInstructionByType(
1524 subtreeRoot->getInstruction()->getType()));
1525 Set3OperandsFromInstr(mvec[n], subtreeRoot, target);
1526 mvec[n]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,prod);
1531 case 38: // reg: And(reg, reg)
1532 case 238: // reg: And(reg, Constant)
1533 mvec[0] = new MachineInstr(AND);
1534 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1537 case 138: // reg: And(reg, not)
1538 mvec[0] = new MachineInstr(ANDN);
1539 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1542 case 39: // reg: Or(reg, reg)
1543 case 239: // reg: Or(reg, Constant)
1544 mvec[0] = new MachineInstr(ORN);
1545 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1548 case 139: // reg: Or(reg, not)
1549 mvec[0] = new MachineInstr(ORN);
1550 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1553 case 40: // reg: Xor(reg, reg)
1554 case 240: // reg: Xor(reg, Constant)
1555 mvec[0] = new MachineInstr(XOR);
1556 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1559 case 140: // reg: Xor(reg, not)
1560 mvec[0] = new MachineInstr(XNOR);
1561 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1564 case 41: // boolconst: SetCC(reg, Constant)
1565 // Check if this is an integer comparison, and
1566 // there is a parent, and the parent decided to use
1567 // a branch-on-integer-register instead of branch-on-condition-code.
1568 // If so, the SUBcc instruction is not required.
1569 // (However, we must still check for constants to be loaded from
1570 // the constant pool so that such a load can be associated with
1571 // this instruction.)
1573 // Otherwise this is just the same as case 42, so just fall through.
1575 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral() &&
1576 subtreeRoot->parent() != NULL)
1578 InstructionNode* parent = (InstructionNode*) subtreeRoot->parent();
1579 assert(parent->getNodeType() == InstrTreeNode::NTInstructionNode);
1580 const vector<MachineInstr*>&
1581 minstrVec = parent->getInstruction()->getMachineInstrVec();
1582 MachineOpCode parentOpCode;
1583 if (parent->getInstruction()->getOpcode() == Instruction::Br &&
1584 (parentOpCode = minstrVec[0]->getOpCode()) >= BRZ &&
1585 parentOpCode <= BRGEZ)
1587 numInstr = 0; // don't forward the operand!
1591 // ELSE FALL THROUGH
1593 case 42: // bool: SetCC(reg, reg):
1595 // This generates a SUBCC instruction, putting the difference in
1596 // a result register, and setting a condition code.
1598 // If the boolean result of the SetCC is used by anything other
1599 // than a single branch instruction, the boolean must be
1600 // computed and stored in the result register. Otherwise, discard
1601 // the difference (by using %g0) and keep only the condition code.
1603 // To compute the boolean result in a register we use a conditional
1604 // move, unless the result of the SUBCC instruction can be used as
1605 // the bool! This assumes that zero is FALSE and any non-zero
1608 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1609 Instruction* setCCInstr = subtreeRoot->getInstruction();
1610 bool keepBoolVal = (parentNode == NULL ||
1611 parentNode->getInstruction()->getOpcode()
1612 != Instruction::Br);
1613 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
1614 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1615 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1619 MachineOpCode movOpCode;
1620 Value* ccValue = NULL;
1622 if (subtreeRoot->leftChild()->getValue()->getType()->isIntegral() ||
1623 subtreeRoot->leftChild()->getValue()->getType()->isPointerType())
1625 // Integer condition: dest. should be %g0 or an integer register.
1626 // If result must be saved but condition is not SetEQ then we need
1627 // a separate instruction to compute the bool result, so discard
1628 // result of SUBcc instruction anyway.
1630 mvec[0] = new MachineInstr(SUBcc);
1631 Set3OperandsFromInstr(mvec[0], subtreeRoot, target, ! keepSubVal);
1633 // Mark the 4th operand as being a CC register, and as a def
1634 // A TmpInstruction is created to represent the int CC "result".
1635 // Unlike other instances of TmpInstruction, this one is used by
1636 // used by machine code of multiple LLVM instructions, viz.,
1637 // the SetCC and the branch. Make sure to get the same one!
1639 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1640 setCCInstr->getParent()->getParent());
1641 setCCInstr->getMachineInstrVec().addTempValue(tmpForCC);
1643 mvec[0]->SetMachineOperand(3, MachineOperand::MO_CCRegister,
1644 tmpForCC, /*def*/true);
1647 { // recompute bool using the integer condition codes
1649 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1655 // FP condition: dest of FCMP should be some FCCn register
1656 mvec[0] = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1657 mvec[0]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1659 mvec[0]->SetMachineOperand(1,MachineOperand::MO_VirtualRegister,
1660 subtreeRoot->leftChild()->getValue());
1661 mvec[0]->SetMachineOperand(2,MachineOperand::MO_VirtualRegister,
1662 subtreeRoot->rightChild()->getValue());
1665 {// recompute bool using the FP condition codes
1666 mustClearReg = true;
1668 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1669 ccValue = setCCInstr;
1675 assert(ccValue && "Inconsistent logic above and here");
1678 {// Unconditionally set register to 0
1680 mvec[n] = new MachineInstr(SETHI);
1681 mvec[n]->SetMachineOperand(0,MachineOperand::MO_UnextendedImmed,
1683 mvec[n]->SetMachineOperand(1,MachineOperand::MO_VirtualRegister,
1687 // Now conditionally move `valueToMove' (0 or 1) into the register
1689 mvec[n] = new MachineInstr(movOpCode);
1690 mvec[n]->SetMachineOperand(0, MachineOperand::MO_CCRegister,
1692 mvec[n]->SetMachineOperand(1, MachineOperand::MO_UnextendedImmed,
1694 mvec[n]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1700 case 43: // boolreg: VReg
1701 case 44: // boolreg: Constant
1705 case 51: // reg: Load(reg)
1706 case 52: // reg: Load(ptrreg)
1707 case 53: // reg: LoadIdx(reg,reg)
1708 case 54: // reg: LoadIdx(ptrreg,reg)
1709 mvec[0] = new MachineInstr(ChooseLoadInstruction(
1710 subtreeRoot->getValue()->getType()));
1711 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1714 case 55: // reg: GetElemPtr(reg)
1715 case 56: // reg: GetElemPtrIdx(reg,reg)
1716 if (subtreeRoot->parent() != NULL)
1718 // Check if the parent was an array access.
1719 // If so, we still need to generate this instruction.
1720 MemAccessInst* memInst = (MemAccessInst*)
1721 subtreeRoot->getInstruction();
1722 const PointerType* ptrType =
1723 (const PointerType*) memInst->getPtrOperand()->getType();
1724 if (! ptrType->getValueType()->isArrayType())
1725 {// we don't need a separate instr
1726 numInstr = 0; // don't forward operand!
1730 // else in all other cases we need to a separate ADD instruction
1731 mvec[0] = new MachineInstr(ADD);
1732 SetOperandsForMemInstr(mvec[0], subtreeRoot, target);
1735 case 57: // reg: Alloca: Implement as 1 instruction:
1736 { // add %fp, offsetFromFP -> result
1737 Instruction* instr = subtreeRoot->getInstruction();
1738 const PointerType* instrType = (const PointerType*) instr->getType();
1739 assert(instrType->isPointerType());
1741 target.findOptimalStorageSize(instrType->getValueType());
1742 assert(tsize != 0 && "Just to check when this can happen");
1744 Method* method = instr->getParent()->getParent();
1745 MachineCodeForMethod& mcode = method->getMachineCode();
1747 target.getFrameInfo().getFirstAutomaticVarOffsetFromFP(method)
1748 - (tsize + mcode.getAutomaticVarsSize());
1750 mcode.putLocalVarAtOffsetFromFP(instr, offsetFromFP, tsize);
1752 // Create a temporary Value to hold the constant offset.
1753 // This is needed because it may not fit in the immediate field.
1754 ConstPoolSInt* offsetVal=ConstPoolSInt::get(Type::IntTy, offsetFromFP);
1756 // Instruction 1: add %fp, offsetFromFP -> result
1757 mvec[0] = new MachineInstr(ADD);
1758 mvec[0]->SetMachineOperand(0, target.getRegInfo().getFramePointer());
1759 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1761 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1766 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1767 // mul num, typeSz -> tmp
1768 // sub %sp, tmp -> %sp
1769 { // add %sp, frameSizeBelowDynamicArea -> result
1770 Instruction* instr = subtreeRoot->getInstruction();
1771 const PointerType* instrType = (const PointerType*) instr->getType();
1772 assert(instrType->isPointerType() &&
1773 instrType->getValueType()->isArrayType());
1774 const Type* eltType =
1775 ((ArrayType*) instrType->getValueType())->getElementType();
1776 int tsize = (int) target.findOptimalStorageSize(eltType);
1778 assert(tsize != 0 && "Just to check when this can happen");
1780 // Create a temporary Value to hold the constant type-size
1781 ConstPoolSInt* tsizeVal = ConstPoolSInt::get(Type::IntTy, tsize);
1783 // Create a temporary Value to hold the constant offset from SP
1784 Method* method = instr->getParent()->getParent();
1785 MachineCodeForMethod& mcode = method->getMachineCode();
1786 int frameSizeBelowDynamicArea =
1787 target.getFrameInfo().getFrameSizeBelowDynamicArea(method);
1788 ConstPoolSInt* lowerAreaSizeVal = ConstPoolSInt::get(Type::IntTy,
1789 frameSizeBelowDynamicArea);
1790 cerr << "***" << endl
1791 << "*** Variable-size ALLOCA operation needs more work:" << endl
1792 << "*** We have to precompute the size of "
1793 << " optional arguments in the stack frame" << endl
1795 assert(0 && "SEE MESSAGE ABOVE");
1797 // Create a temporary value to hold `tmp'
1798 Instruction* tmpInstr = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
1799 subtreeRoot->leftChild()->getValue(),
1800 NULL /*could insert tsize here*/);
1801 subtreeRoot->getInstruction()->getMachineInstrVec().addTempValue(tmpInstr);
1803 // Instruction 1: mul numElements, typeSize -> tmp
1804 mvec[0] = new MachineInstr(MULX);
1805 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1806 subtreeRoot->leftChild()->getValue());
1807 mvec[0]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1809 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1812 // Instruction 2: sub %sp, tmp -> %sp
1814 mvec[1] = new MachineInstr(SUB);
1815 mvec[1]->SetMachineOperand(0, target.getRegInfo().getStackPointer());
1816 mvec[1]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1818 mvec[1]->SetMachineOperand(2, target.getRegInfo().getStackPointer());
1820 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
1822 mvec[2] = new MachineInstr(ADD);
1823 mvec[2]->SetMachineOperand(0, target.getRegInfo().getStackPointer());
1824 mvec[2]->SetMachineOperand(1, MachineOperand::MO_VirtualRegister,
1826 mvec[2]->SetMachineOperand(2,MachineOperand::MO_VirtualRegister,instr);
1830 case 61: // reg: Call
1831 { // Generate a call-indirect (i.e., jmpl) for now to expose
1832 // the potential need for registers. If an absolute address
1833 // is available, replace this with a CALL instruction.
1834 // Mark both the indirection register and the return-address
1835 // register as hidden virtual registers.
1836 // Also, mark the operands of the Call and return value (if
1837 // any) as implicit operands of the CALL machine instruction.
1839 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
1840 Value *callee = callInstr->getCalledValue();
1842 Instruction* retAddrReg = new TmpInstruction(TMP_INSTRUCTION_OPCODE,
1845 // Note temporary values in the machineInstrVec for the VM instr.
1847 // WARNING: Operands 0..N-1 must go in slots 0..N-1 of implicitUses.
1848 // The result value must go in slot N. This is assumed
1849 // in register allocation.
1851 callInstr->getMachineInstrVec().addTempValue(retAddrReg);
1854 // Generate the machine instruction and its operands.
1855 // Use CALL for direct function calls; this optimistically assumes
1856 // the PC-relative address fits in the CALL address field (22 bits).
1857 // Use JMPL for indirect calls.
1859 if (callee->getValueType() == Value::MethodVal)
1860 { // direct function call
1861 mvec[0] = new MachineInstr(CALL);
1862 mvec[0]->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp,
1866 { // indirect function call
1867 mvec[0] = new MachineInstr(JMPLCALL);
1868 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1870 mvec[0]->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
1872 mvec[0]->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
1876 // Add the call operands and return value as implicit refs
1877 for (unsigned i=0, N=callInstr->getNumOperands(); i < N; ++i)
1878 if (callInstr->getOperand(i) != callee)
1879 mvec[0]->addImplicitRef(callInstr->getOperand(i));
1881 if (callInstr->getType() != Type::VoidTy)
1882 mvec[0]->addImplicitRef(callInstr, /*isDef*/ true);
1884 // For the CALL instruction, the ret. addr. reg. is also implicit
1885 if (callee->getValueType() == Value::MethodVal)
1886 mvec[0]->addImplicitRef(retAddrReg, /*isDef*/ true);
1888 mvec[numInstr++] = new MachineInstr(NOP); // delay slot
1892 case 62: // reg: Shl(reg, reg)
1893 opType = subtreeRoot->leftChild()->getValue()->getType();
1894 assert(opType->isIntegral()
1895 || opType == Type::BoolTy
1896 || opType->isPointerType()&& "Shl unsupported for other types");
1897 mvec[0] = new MachineInstr((opType == Type::LongTy)? SLLX : SLL);
1898 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1901 case 63: // reg: Shr(reg, reg)
1902 opType = subtreeRoot->leftChild()->getValue()->getType();
1903 assert(opType->isIntegral()
1904 || opType == Type::BoolTy
1905 || opType->isPointerType() &&"Shr unsupported for other types");
1906 mvec[0] = new MachineInstr((opType->isSigned()
1907 ? ((opType == Type::LongTy)? SRAX : SRA)
1908 : ((opType == Type::LongTy)? SRLX : SRL)));
1909 Set3OperandsFromInstr(mvec[0], subtreeRoot, target);
1912 case 64: // reg: Phi(reg,reg)
1913 { // This instruction has variable #operands, so resultPos is 0.
1914 Instruction* phi = subtreeRoot->getInstruction();
1915 mvec[0] = new MachineInstr(PHI, 1 + phi->getNumOperands());
1916 mvec[0]->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
1917 subtreeRoot->getValue());
1918 for (unsigned i=0, N=phi->getNumOperands(); i < N; i++)
1919 mvec[0]->SetMachineOperand(i+1, MachineOperand::MO_VirtualRegister,
1920 phi->getOperand(i));
1923 case 71: // reg: VReg
1924 case 72: // reg: Constant
1925 numInstr = 0; // don't forward the value
1929 assert(0 && "Unrecognized BURG rule");
1935 if (forwardOperandNum >= 0)
1936 { // We did not generate a machine instruction but need to use operand.
1937 // If user is in the same tree, replace Value in its machine operand.
1938 // If not, insert a copy instruction which should get coalesced away
1939 // by register allocation.
1940 if (subtreeRoot->parent() != NULL)
1941 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
1944 vector<MachineInstr*> minstrVec;
1945 CreateCopyInstructionsByType(target,
1946 subtreeRoot->getInstruction()->getOperand(forwardOperandNum),
1947 subtreeRoot->getInstruction(), minstrVec);
1948 assert(minstrVec.size() > 0);
1949 for (unsigned i=0; i < minstrVec.size(); ++i)
1950 mvec[numInstr++] = minstrVec[i];