2 //***************************************************************************
7 // This file defines stuff that is to be private to the Sparc
8 // backend, but is shared among different portions of the backend.
9 //**************************************************************************/
12 #ifndef SPARC_INTERNALS_H
13 #define SPARC_INTERNALS_H
16 #include "SparcRegClassInfo.h"
17 #include "llvm/Target/TargetMachine.h"
18 #include "llvm/Target/MachineInstrInfo.h"
19 #include "llvm/Target/MachineSchedInfo.h"
20 #include "llvm/Target/MachineFrameInfo.h"
21 #include "llvm/Target/MachineCacheInfo.h"
22 #include "llvm/CodeGen/RegClass.h"
23 #include "llvm/Type.h"
25 #include <sys/types.h>
29 // OpCodeMask definitions for the Sparc V9
31 const OpCodeMask Immed = 0x00002000; // immed or reg operand?
32 const OpCodeMask Annul = 0x20000000; // annul delay instr?
33 const OpCodeMask PredictTaken = 0x00080000; // predict branch taken?
36 enum SparcInstrSchedClass {
37 SPARC_NONE, /* Instructions with no scheduling restrictions */
38 SPARC_IEUN, /* Integer class that can use IEU0 or IEU1 */
39 SPARC_IEU0, /* Integer class IEU0 */
40 SPARC_IEU1, /* Integer class IEU1 */
41 SPARC_FPM, /* FP Multiply or Divide instructions */
42 SPARC_FPA, /* All other FP instructions */
43 SPARC_CTI, /* Control-transfer instructions */
44 SPARC_LD, /* Load instructions */
45 SPARC_ST, /* Store instructions */
46 SPARC_SINGLE, /* Instructions that must issue by themselves */
48 SPARC_INV, /* This should stay at the end for the next value */
49 SPARC_NUM_SCHED_CLASSES = SPARC_INV
53 //---------------------------------------------------------------------------
54 // enum SparcMachineOpCode.
55 // const MachineInstrDescriptor SparcMachineInstrDesc[]
58 // Description of UltraSparc machine instructions.
60 //---------------------------------------------------------------------------
62 enum SparcMachineOpCode {
63 #define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
64 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
66 #include "SparcInstr.def"
68 // End-of-array marker
70 NUM_REAL_OPCODES = PHI, // number of valid opcodes
71 NUM_TOTAL_OPCODES = INVALID_OPCODE
75 // Array of machine instruction descriptions...
76 extern const MachineInstrDescriptor SparcMachineInstrDesc[];
79 //---------------------------------------------------------------------------
80 // class UltraSparcInstrInfo
83 // Information about individual instructions.
84 // Most information is stored in the SparcMachineInstrDesc array above.
85 // Other information is computed on demand, and most such functions
86 // default to member functions in base class MachineInstrInfo.
87 //---------------------------------------------------------------------------
89 class UltraSparcInstrInfo : public MachineInstrInfo {
91 /*ctor*/ UltraSparcInstrInfo(const TargetMachine& tgt);
94 // All immediate constants are in position 0 except the
95 // store instructions.
97 virtual int getImmmedConstantPos(MachineOpCode opCode) const {
99 if (this->maxImmedConstant(opCode, ignore) != 0)
101 assert(! this->isStore((MachineOpCode) STB - 1)); // first store is STB
102 assert(! this->isStore((MachineOpCode) STD + 1)); // last store is STD
103 return (opCode >= STB || opCode <= STD)? 2 : 1;
109 virtual bool hasResultInterlock (MachineOpCode opCode) const
111 // All UltraSPARC instructions have interlocks (note that delay slots
112 // are not considered here).
113 // However, instructions that use the result of an FCMP produce a
114 // 9-cycle stall if they are issued less than 3 cycles after the FCMP.
115 // Force the compiler to insert a software interlock (i.e., gap of
116 // 2 other groups, including NOPs if necessary).
117 return (opCode == FCMPS || opCode == FCMPD || opCode == FCMPQ);
120 //-------------------------------------------------------------------------
121 // Code generation support for creating individual machine instructions
122 //-------------------------------------------------------------------------
124 // Create an instruction sequence to put the constant `val' into
125 // the virtual register `dest'. The generated instructions are
126 // returned in `minstrVec'. Any temporary registers (TmpInstruction)
127 // created are returned in `tempVec'.
129 virtual void CreateCodeToLoadConst(Value* val,
131 vector<MachineInstr*>& minstrVec,
132 vector<TmpInstruction*>& tempVec) const;
135 // Create an instruction sequence to copy an integer value `val'
136 // to a floating point value `dest' by copying to memory and back.
137 // val must be an integral type. dest must be a Float or Double.
138 // The generated instructions are returned in `minstrVec'.
139 // Any temp. registers (TmpInstruction) created are returned in `tempVec'.
141 virtual void CreateCodeToCopyIntToFloat(Method* method,
144 vector<MachineInstr*>& minstrVec,
145 vector<TmpInstruction*>& tempVec,
146 TargetMachine& target) const;
148 // Similarly, create an instruction sequence to copy an FP value
149 // `val' to an integer value `dest' by copying to memory and back.
150 // See the previous function for information about return values.
152 virtual void CreateCodeToCopyFloatToInt(Method* method,
155 vector<MachineInstr*>& minstrVec,
156 vector<TmpInstruction*>& tempVec,
157 TargetMachine& target) const;
159 // create copy instruction(s)
161 CreateCopyInstructionsByType(const TargetMachine& target,
164 vector<MachineInstr*>& minstrVec) const;
170 //----------------------------------------------------------------------------
171 // class UltraSparcRegInfo
173 //----------------------------------------------------------------------------
181 class UltraSparcRegInfo : public MachineRegInfo
185 // The actual register classes in the Sparc
195 // Type of registers available in Sparc. There can be several reg types
196 // in the same class. For instace, the float reg class has Single/Double
206 // the size of a value (int, float, etc..) stored in the stack frame
210 // WARNING: If the above enum order must be changed, also modify
211 // getRegisterClassOfValue method below since it assumes this particular
212 // order for efficiency.
215 // reverse pointer to get info about the ultra sparc machine
216 const UltraSparc *const UltraSparcInfo;
218 // Both int and float rguments can be passed in 6 int regs -
219 // %o0 to %o5 (cannot be changed)
220 unsigned const NumOfIntArgRegs;
221 unsigned const NumOfFloatArgRegs;
222 int const InvalidRegNum;
223 int SizeOfOperandOnStack;
227 //void setCallArgColor(LiveRange *const LR, const unsigned RegNo) const;
229 void setCallOrRetArgCol(LiveRange *const LR, const unsigned RegNo,
230 const MachineInstr *MI,AddedInstrMapType &AIMap)const;
232 MachineInstr * getCopy2RegMI(const Value *SrcVal, const unsigned Reg,
233 unsigned RegClassID) const ;
236 void suggestReg4RetAddr(const MachineInstr * RetMI,
237 LiveRangeInfo& LRI) const;
239 void suggestReg4CallAddr(const MachineInstr * CallMI, LiveRangeInfo& LRI,
240 vector<RegClass *> RCList) const;
243 Value *getValue4ReturnAddr( const MachineInstr * MInst ) const ;
245 int getRegType(const LiveRange *const LR) const {
249 switch( (LR->getRegClass())->getID() ) {
251 case IntRegClassID: return IntRegType;
253 case FloatRegClassID:
254 Typ = LR->getTypeID();
255 if( Typ == Type::FloatTyID )
256 return FPSingleRegType;
257 else if( Typ == Type::DoubleTyID )
258 return FPDoubleRegType;
259 else assert(0 && "Unknown type in FloatRegClass");
261 case IntCCRegClassID: return IntCCRegType;
263 case FloatCCRegClassID: return FloatCCRegType ;
265 default: assert( 0 && "Unknown reg class ID");
271 int getRegType(const Value *const Val) const {
275 switch( getRegClassIDOfValue(Val) ) {
277 case IntRegClassID: return IntRegType;
279 case FloatRegClassID:
280 Typ = (Val->getType())->getPrimitiveID();
281 if( Typ == Type::FloatTyID )
282 return FPSingleRegType;
283 else if( Typ == Type::DoubleTyID )
284 return FPDoubleRegType;
285 else assert(0 && "Unknown type in FloatRegClass");
287 case IntCCRegClassID: return IntCCRegType;
289 case FloatCCRegClassID: return FloatCCRegType ;
291 default: assert( 0 && "Unknown reg class ID");
298 int getRegType(int reg) const {
301 else if ( reg < (32 + 32) )
302 return FPSingleRegType;
303 else if ( reg < (64 + 32) )
304 return FPDoubleRegType;
305 else if( reg < (64+32+4) )
306 return FloatCCRegType;
307 else if( reg < (64+32+4+2) )
310 assert(0 && "Invalid register number in getRegType");
315 // ***TODO: See this method is necessary
317 MachineInstr * cpValue2RegMI(Value * Val, const unsigned DestReg,
318 const int RegType) const;
320 const Value *getCallInstRetAddr(const MachineInstr *CallMI) const;
321 const unsigned getCallInstNumArgs(const MachineInstr *CallMI) const;
324 MachineInstr * cpCCR2IntMI(const unsigned IntReg) const;
325 MachineInstr * cpInt2CCRMI(const unsigned IntReg) const;
329 void moveInst2OrdVec(vector<MachineInstr *> &OrdVec, MachineInstr *UnordInst,
330 PhyRegAlloc &PRA ) const;
332 void OrderAddedInstrns( vector<MachineInstr *> &UnordVec,
333 vector<MachineInstr *> &OrdVec,
334 PhyRegAlloc &PRA) const;
338 bool isVarArgCall(const MachineInstr *CallMI) const;
345 UltraSparcRegInfo(const TargetMachine& tgt ) :
347 UltraSparcInfo(& (const UltraSparc&) tgt),
349 NumOfFloatArgRegs(32),
351 SizeOfOperandOnStack(8) {
353 MachineRegClassArr.push_back( new SparcIntRegClass(IntRegClassID) );
354 MachineRegClassArr.push_back( new SparcFloatRegClass(FloatRegClassID) );
355 MachineRegClassArr.push_back( new SparcIntCCRegClass(IntCCRegClassID) );
356 MachineRegClassArr.push_back( new SparcFloatCCRegClass(FloatCCRegClassID));
358 assert( SparcFloatRegOrder::StartOfNonVolatileRegs == 32 &&
359 "32 Float regs are used for float arg passing");
364 ~UltraSparcRegInfo(void) { } // empty destructor
367 inline const UltraSparc & getUltraSparcInfo() const {
368 return *UltraSparcInfo;
373 inline unsigned getRegClassIDOfValue (const Value *const Val,
374 bool isCCReg = false) const {
376 Type::PrimitiveID ty = (Val->getType())->getPrimitiveID();
380 if( (ty && ty <= Type::LongTyID) || (ty == Type::LabelTyID) ||
381 (ty == Type::MethodTyID) || (ty == Type::PointerTyID) )
382 res = IntRegClassID; // sparc int reg (ty=0: void)
383 else if( ty <= Type::DoubleTyID)
384 res = FloatRegClassID; // sparc float reg class
386 cerr << "TypeID: " << ty << endl;
387 assert(0 && "Cannot resolve register class for type");
392 return res + 2; // corresponidng condition code regiser
397 // returns the register tha contains always zero
398 // this is the unified register number
399 inline int getZeroRegNum() const { return SparcIntRegOrder::g0; }
401 // returns the reg used for pushing the address when a method is called.
402 // This can be used for other purposes between calls
403 unsigned getCallAddressReg() const { return SparcIntRegOrder::o7; }
406 // and when we return from a method. It should be made sure that this
407 // register contains the return value when a return instruction is reached.
408 unsigned getReturnAddressReg() const { return SparcIntRegOrder::i7; }
410 void suggestRegs4MethodArgs(const Method *const Meth,
411 LiveRangeInfo& LRI) const;
413 void suggestRegs4CallArgs(const MachineInstr *const CallMI,
414 LiveRangeInfo& LRI, vector<RegClass *> RCL) const;
416 void suggestReg4RetValue(const MachineInstr *const RetMI,
417 LiveRangeInfo& LRI ) const;
420 void colorMethodArgs(const Method *const Meth, LiveRangeInfo& LRI,
421 AddedInstrns *const FirstAI) const;
423 void colorCallArgs(const MachineInstr *const CallMI, LiveRangeInfo& LRI,
424 AddedInstrns *const CallAI, PhyRegAlloc &PRA,
425 const BasicBlock *BB) const;
427 void colorRetValue(const MachineInstr *const RetI, LiveRangeInfo& LRI,
428 AddedInstrns *const RetAI) const;
431 // bool handleSpecialMInstr(const MachineInstr * MInst,
432 // LiveRangeInfo& LRI, vector<RegClass *> RCL) const;
435 static void printReg(const LiveRange *const LR) ;
437 // this method provides a unique number for each register
438 inline int getUnifiedRegNum(int RegClassID, int reg) const {
440 if( RegClassID == IntRegClassID && reg < 32 )
442 else if ( RegClassID == FloatRegClassID && reg < 64)
443 return reg + 32; // we have 32 int regs
444 else if( RegClassID == FloatCCRegClassID && reg < 4)
445 return reg + 32 + 64; // 32 int, 64 float
446 else if( RegClassID == IntCCRegClassID )
447 return 4+ 32 + 64; // only int cc reg
448 else if (reg==InvalidRegNum)
449 return InvalidRegNum;
451 assert(0 && "Invalid register class or reg number");
455 // given the unified register number, this gives the name
456 inline const string getUnifiedRegName(int reg) const {
458 return SparcIntRegOrder::getRegName(reg);
459 else if ( reg < (64 + 32) )
460 return SparcFloatRegOrder::getRegName( reg - 32);
461 else if( reg < (64+32+4) )
462 return SparcFloatCCRegOrder::getRegName( reg -32 - 64);
463 else if( reg < (64+32+4+2) ) // two names: %xcc and %ccr
464 return SparcIntCCRegOrder::getRegName( reg -32 - 64 - 4);
465 else if (reg== InvalidRegNum) //****** TODO: Remove */
468 assert(0 && "Invalid register number");
472 inline unsigned int getRegNumInCallersWindow(int reg) {
473 if (reg == InvalidRegNum || reg >= 32)
475 return SparcIntRegOrder::getRegNumInCallersWindow(reg);
478 inline bool mustBeRemappedInCallersWindow(int reg) {
479 return (reg != InvalidRegNum && reg < 32);
484 inline int getSpilledRegSize(const int RegType) const {
487 // for Sparc, we allocate 8 bytes on stack for all register types
490 const Value * getCallInstRetVal(const MachineInstr *CallMI) const;
492 MachineInstr * cpReg2RegMI(const unsigned SrcReg, const unsigned DestReg,
493 const int RegType) const;
495 MachineInstr * cpReg2MemMI(const unsigned SrcReg, const unsigned DestPtrReg,
496 const int Offset, const int RegType) const;
498 MachineInstr * cpMem2RegMI(const unsigned SrcPtrReg, const int Offset,
499 const unsigned DestReg, const int RegType) const;
501 MachineInstr* cpValue2Value(Value *Src, Value *Dest) const;
504 inline bool isRegVolatile(const int RegClassID, const int Reg) const {
505 return (MachineRegClassArr[RegClassID])->isRegVolatile(Reg);
509 inline unsigned getFramePointer() const {
510 return SparcIntRegOrder::i6;
513 inline unsigned getStackPointer() const {
514 return SparcIntRegOrder::o6;
517 inline int getInvalidRegNum() const {
518 return InvalidRegNum;
522 void insertCallerSavingCode(const MachineInstr *MInst,
523 const BasicBlock *BB, PhyRegAlloc &PRA ) const;
530 /*---------------------------------------------------------------------------
531 Scheduling guidelines for SPARC IIi:
533 I-Cache alignment rules (pg 326)
534 -- Align a branch target instruction so that it's entire group is within
535 the same cache line (may be 1-4 instructions).
536 ** Don't let a branch that is predicted taken be the last instruction
537 on an I-cache line: delay slot will need an entire line to be fetched
538 -- Make a FP instruction or a branch be the 4th instruction in a group.
539 For branches, there are tradeoffs in reordering to make this happen
541 ** Don't put a branch in a group that crosses a 32-byte boundary!
542 An artificial branch is inserted after every 32 bytes, and having
543 another branch will force the group to be broken into 2 groups.
546 -- Don't let a loop span two memory pages, if possible
548 Branch prediction performance:
549 -- Don't make the branch in a delay slot the target of a branch
550 -- Try not to have 2 predicted branches within a group of 4 instructions
551 (because each such group has a single branch target field).
552 -- Try to align branches in slots 0, 2, 4 or 6 of a cache line (to avoid
553 the wrong prediction bits being used in some cases).
555 D-Cache timing constraints:
556 -- Signed int loads of less than 64 bits have 3 cycle latency, not 2
557 -- All other loads that hit in D-Cache have 2 cycle latency
558 -- All loads are returned IN ORDER, so a D-Cache miss will delay a later hit
559 -- Mis-aligned loads or stores cause a trap. In particular, replace
560 mis-aligned FP double precision l/s with 2 single-precision l/s.
561 -- Simulations of integer codes show increase in avg. group size of
562 33% when code (including esp. non-faulting loads) is moved across
563 one branch, and 50% across 2 branches.
565 E-Cache timing constraints:
566 -- Scheduling for E-cache (D-Cache misses) is effective (due to load buffering)
568 Store buffer timing constraints:
569 -- Stores can be executed in same cycle as instruction producing the value
570 -- Stores are buffered and have lower priority for E-cache until
571 highwater mark is reached in the store buffer (5 stores)
573 Pipeline constraints:
574 -- Shifts can only use IEU0.
575 -- CC setting instructions can only use IEU1.
576 -- Several other instructions must only use IEU1:
577 EDGE(?), ARRAY(?), CALL, JMPL, BPr, PST, and FCMP.
578 -- Two instructions cannot store to the same register file in a single cycle
579 (single write port per file).
581 Issue and grouping constraints:
582 -- FP and branch instructions must use slot 4.
583 -- Shift instructions cannot be grouped with other IEU0-specific instructions.
584 -- CC setting instructions cannot be grouped with other IEU1-specific instrs.
585 -- Several instructions must be issued in a single-instruction group:
586 MOVcc or MOVr, MULs/x and DIVs/x, SAVE/RESTORE, many others
587 -- A CALL or JMPL breaks a group, ie, is not combined with subsequent instrs.
591 Branch delay slot scheduling rules:
592 -- A CTI couple (two back-to-back CTI instructions in the dynamic stream)
593 has a 9-instruction penalty: the entire pipeline is flushed when the
594 second instruction reaches stage 9 (W-Writeback).
595 -- Avoid putting multicycle instructions, and instructions that may cause
596 load misses, in the delay slot of an annulling branch.
597 -- Avoid putting WR, SAVE..., RESTORE and RETURN instructions in the
598 delay slot of an annulling branch.
600 *--------------------------------------------------------------------------- */
602 //---------------------------------------------------------------------------
603 // List of CPUResources for UltraSPARC IIi.
604 //---------------------------------------------------------------------------
606 const CPUResource AllIssueSlots( "All Instr Slots", 4);
607 const CPUResource IntIssueSlots( "Int Instr Slots", 3);
608 const CPUResource First3IssueSlots("Instr Slots 0-3", 3);
609 const CPUResource LSIssueSlots( "Load-Store Instr Slot", 1);
610 const CPUResource CTIIssueSlots( "Ctrl Transfer Instr Slot", 1);
611 const CPUResource FPAIssueSlots( "Int Instr Slot 1", 1);
612 const CPUResource FPMIssueSlots( "Int Instr Slot 1", 1);
614 // IEUN instructions can use either Alu and should use IAluN.
615 // IEU0 instructions must use Alu 1 and should use both IAluN and IAlu0.
616 // IEU1 instructions must use Alu 2 and should use both IAluN and IAlu1.
617 const CPUResource IAluN("Int ALU 1or2", 2);
618 const CPUResource IAlu0("Int ALU 1", 1);
619 const CPUResource IAlu1("Int ALU 2", 1);
621 const CPUResource LSAluC1("Load/Store Unit Addr Cycle", 1);
622 const CPUResource LSAluC2("Load/Store Unit Issue Cycle", 1);
623 const CPUResource LdReturn("Load Return Unit", 1);
625 const CPUResource FPMAluC1("FP Mul/Div Alu Cycle 1", 1);
626 const CPUResource FPMAluC2("FP Mul/Div Alu Cycle 2", 1);
627 const CPUResource FPMAluC3("FP Mul/Div Alu Cycle 3", 1);
629 const CPUResource FPAAluC1("FP Other Alu Cycle 1", 1);
630 const CPUResource FPAAluC2("FP Other Alu Cycle 2", 1);
631 const CPUResource FPAAluC3("FP Other Alu Cycle 3", 1);
633 const CPUResource IRegReadPorts("Int Reg ReadPorts", INT_MAX); // CHECK
634 const CPUResource IRegWritePorts("Int Reg WritePorts", 2); // CHECK
635 const CPUResource FPRegReadPorts("FP Reg Read Ports", INT_MAX); // CHECK
636 const CPUResource FPRegWritePorts("FP Reg Write Ports", 1); // CHECK
638 const CPUResource CTIDelayCycle( "CTI delay cycle", 1);
639 const CPUResource FCMPDelayCycle("FCMP delay cycle", 1);
642 //---------------------------------------------------------------------------
643 // const InstrClassRUsage SparcRUsageDesc[]
646 // Resource usage information for instruction in each scheduling class.
647 // The InstrRUsage Objects for individual classes are specified first.
648 // Note that fetch and decode are decoupled from the execution pipelines
649 // via an instr buffer, so they are not included in the cycles below.
650 //---------------------------------------------------------------------------
652 const InstrClassRUsage NoneClassRUsage = {
657 /* isSingleIssue */ false,
658 /* breaksGroup */ false,
662 /* feasibleSlots[] */ { 0, 1, 2, 3 },
676 const InstrClassRUsage IEUNClassRUsage = {
681 /* isSingleIssue */ false,
682 /* breaksGroup */ false,
686 /* feasibleSlots[] */ { 0, 1, 2 },
690 /*Cycle G */ { AllIssueSlots.rid, 0, 1 },
691 { IntIssueSlots.rid, 0, 1 },
692 /*Cycle E */ { IAluN.rid, 1, 1 },
697 /*Cycle W */ { IRegWritePorts.rid, 6, 1 }
701 const InstrClassRUsage IEU0ClassRUsage = {
706 /* isSingleIssue */ false,
707 /* breaksGroup */ false,
711 /* feasibleSlots[] */ { 0, 1, 2 },
715 /*Cycle G */ { AllIssueSlots.rid, 0, 1 },
716 { IntIssueSlots.rid, 0, 1 },
717 /*Cycle E */ { IAluN.rid, 1, 1 },
723 /*Cycle W */ { IRegWritePorts.rid, 6, 1 }
727 const InstrClassRUsage IEU1ClassRUsage = {
732 /* isSingleIssue */ false,
733 /* breaksGroup */ false,
737 /* feasibleSlots[] */ { 0, 1, 2 },
741 /*Cycle G */ { AllIssueSlots.rid, 0, 1 },
742 { IntIssueSlots.rid, 0, 1 },
743 /*Cycle E */ { IAluN.rid, 1, 1 },
749 /*Cycle W */ { IRegWritePorts.rid, 6, 1 }
753 const InstrClassRUsage FPMClassRUsage = {
758 /* isSingleIssue */ false,
759 /* breaksGroup */ false,
763 /* feasibleSlots[] */ { 0, 1, 2, 3 },
767 /*Cycle G */ { AllIssueSlots.rid, 0, 1 },
768 { FPMIssueSlots.rid, 0, 1 },
769 /*Cycle E */ { FPRegReadPorts.rid, 1, 1 },
770 /*Cycle C */ { FPMAluC1.rid, 2, 1 },
771 /*Cycle N1*/ { FPMAluC2.rid, 3, 1 },
772 /*Cycle N1*/ { FPMAluC3.rid, 4, 1 },
774 /*Cycle W */ { FPRegWritePorts.rid, 6, 1 }
778 const InstrClassRUsage FPAClassRUsage = {
783 /* isSingleIssue */ false,
784 /* breaksGroup */ false,
788 /* feasibleSlots[] */ { 0, 1, 2, 3 },
792 /*Cycle G */ { AllIssueSlots.rid, 0, 1 },
793 { FPAIssueSlots.rid, 0, 1 },
794 /*Cycle E */ { FPRegReadPorts.rid, 1, 1 },
795 /*Cycle C */ { FPAAluC1.rid, 2, 1 },
796 /*Cycle N1*/ { FPAAluC2.rid, 3, 1 },
797 /*Cycle N1*/ { FPAAluC3.rid, 4, 1 },
799 /*Cycle W */ { FPRegWritePorts.rid, 6, 1 }
803 const InstrClassRUsage LDClassRUsage = {
808 /* isSingleIssue */ false,
809 /* breaksGroup */ false,
813 /* feasibleSlots[] */ { 0, 1, 2, },
817 /*Cycle G */ { AllIssueSlots.rid, 0, 1 },
818 { First3IssueSlots.rid, 0, 1 },
819 { LSIssueSlots.rid, 0, 1 },
820 /*Cycle E */ { LSAluC1.rid, 1, 1 },
821 /*Cycle C */ { LSAluC2.rid, 2, 1 },
822 { LdReturn.rid, 2, 1 },
826 /*Cycle W */ { IRegWritePorts.rid, 6, 1 }
830 const InstrClassRUsage STClassRUsage = {
835 /* isSingleIssue */ false,
836 /* breaksGroup */ false,
840 /* feasibleSlots[] */ { 0, 1, 2 },
844 /*Cycle G */ { AllIssueSlots.rid, 0, 1 },
845 { First3IssueSlots.rid, 0, 1 },
846 { LSIssueSlots.rid, 0, 1 },
847 /*Cycle E */ { LSAluC1.rid, 1, 1 },
848 /*Cycle C */ { LSAluC2.rid, 2, 1 }
856 const InstrClassRUsage CTIClassRUsage = {
861 /* isSingleIssue */ false,
862 /* breaksGroup */ false,
866 /* feasibleSlots[] */ { 0, 1, 2, 3 },
870 /*Cycle G */ { AllIssueSlots.rid, 0, 1 },
871 { CTIIssueSlots.rid, 0, 1 },
872 /*Cycle E */ { IAlu0.rid, 1, 1 },
873 /*Cycles E-C */ { CTIDelayCycle.rid, 1, 2 }
882 const InstrClassRUsage SingleClassRUsage = {
887 /* isSingleIssue */ true,
888 /* breaksGroup */ false,
892 /* feasibleSlots[] */ { 0 },
896 /*Cycle G */ { AllIssueSlots.rid, 0, 1 },
897 { AllIssueSlots.rid, 0, 1 },
898 { AllIssueSlots.rid, 0, 1 },
899 { AllIssueSlots.rid, 0, 1 },
900 /*Cycle E */ { IAlu0.rid, 1, 1 }
910 const InstrClassRUsage SparcRUsageDesc[] = {
924 //---------------------------------------------------------------------------
925 // const InstrIssueDelta SparcInstrIssueDeltas[]
928 // Changes to issue restrictions information in InstrClassRUsage for
929 // instructions that differ from other instructions in their class.
930 //---------------------------------------------------------------------------
932 const InstrIssueDelta SparcInstrIssueDeltas[] = {
934 // opCode, isSingleIssue, breaksGroup, numBubbles
936 // Special cases for single-issue only
937 // Other single issue cases are below.
938 //{ LDDA, true, true, 0 },
939 //{ STDA, true, true, 0 },
940 //{ LDDF, true, true, 0 },
941 //{ LDDFA, true, true, 0 },
942 { ADDC, true, true, 0 },
943 { ADDCcc, true, true, 0 },
944 { SUBC, true, true, 0 },
945 { SUBCcc, true, true, 0 },
946 //{ LDSTUB, true, true, 0 },
947 //{ SWAP, true, true, 0 },
948 //{ SWAPA, true, true, 0 },
949 //{ CAS, true, true, 0 },
950 //{ CASA, true, true, 0 },
951 //{ CASX, true, true, 0 },
952 //{ CASXA, true, true, 0 },
953 //{ LDFSR, true, true, 0 },
954 //{ LDFSRA, true, true, 0 },
955 //{ LDXFSR, true, true, 0 },
956 //{ LDXFSRA, true, true, 0 },
957 //{ STFSR, true, true, 0 },
958 //{ STFSRA, true, true, 0 },
959 //{ STXFSR, true, true, 0 },
960 //{ STXFSRA, true, true, 0 },
961 //{ SAVED, true, true, 0 },
962 //{ RESTORED, true, true, 0 },
963 //{ FLUSH, true, true, 9 },
964 //{ FLUSHW, true, true, 9 },
965 //{ ALIGNADDR, true, true, 0 },
966 { RETURN, true, true, 0 },
967 //{ DONE, true, true, 0 },
968 //{ RETRY, true, true, 0 },
969 //{ TCC, true, true, 0 },
970 //{ SHUTDOWN, true, true, 0 },
972 // Special cases for breaking group *before*
973 // CURRENTLY NOT SUPPORTED!
974 { CALL, false, false, 0 },
975 { JMPLCALL, false, false, 0 },
976 { JMPLRET, false, false, 0 },
978 // Special cases for breaking the group *after*
979 { MULX, true, true, (4+34)/2 },
980 { FDIVS, false, true, 0 },
981 { FDIVD, false, true, 0 },
982 { FDIVQ, false, true, 0 },
983 { FSQRTS, false, true, 0 },
984 { FSQRTD, false, true, 0 },
985 { FSQRTQ, false, true, 0 },
986 //{ FCMP{LE,GT,NE,EQ}, false, true, 0 },
988 // Instructions that introduce bubbles
989 //{ MULScc, true, true, 2 },
990 //{ SMULcc, true, true, (4+18)/2 },
991 //{ UMULcc, true, true, (4+19)/2 },
992 { SDIVX, true, true, 68 },
993 { UDIVX, true, true, 68 },
994 //{ SDIVcc, true, true, 36 },
995 //{ UDIVcc, true, true, 37 },
996 { WRCCR, true, true, 4 },
997 //{ WRPR, true, true, 4 },
998 //{ RDCCR, true, true, 0 }, // no bubbles after, but see below
999 //{ RDPR, true, true, 0 },
1003 //---------------------------------------------------------------------------
1004 // const InstrRUsageDelta SparcInstrUsageDeltas[]
1007 // Changes to resource usage information in InstrClassRUsage for
1008 // instructions that differ from other instructions in their class.
1009 //---------------------------------------------------------------------------
1011 const InstrRUsageDelta SparcInstrUsageDeltas[] = {
1013 // MachineOpCode, Resource, Start cycle, Num cycles
1016 // JMPL counts as a load/store instruction for issue!
1018 { JMPLCALL, LSIssueSlots.rid, 0, 1 },
1019 { JMPLRET, LSIssueSlots.rid, 0, 1 },
1022 // Many instructions cannot issue for the next 2 cycles after an FCMP
1023 // We model that with a fake resource FCMPDelayCycle.
1025 { FCMPS, FCMPDelayCycle.rid, 1, 3 },
1026 { FCMPD, FCMPDelayCycle.rid, 1, 3 },
1027 { FCMPQ, FCMPDelayCycle.rid, 1, 3 },
1029 { MULX, FCMPDelayCycle.rid, 1, 1 },
1030 { SDIVX, FCMPDelayCycle.rid, 1, 1 },
1031 { UDIVX, FCMPDelayCycle.rid, 1, 1 },
1032 //{ SMULcc, FCMPDelayCycle.rid, 1, 1 },
1033 //{ UMULcc, FCMPDelayCycle.rid, 1, 1 },
1034 //{ SDIVcc, FCMPDelayCycle.rid, 1, 1 },
1035 //{ UDIVcc, FCMPDelayCycle.rid, 1, 1 },
1036 { STD, FCMPDelayCycle.rid, 1, 1 },
1037 { FMOVRSZ, FCMPDelayCycle.rid, 1, 1 },
1038 { FMOVRSLEZ,FCMPDelayCycle.rid, 1, 1 },
1039 { FMOVRSLZ, FCMPDelayCycle.rid, 1, 1 },
1040 { FMOVRSNZ, FCMPDelayCycle.rid, 1, 1 },
1041 { FMOVRSGZ, FCMPDelayCycle.rid, 1, 1 },
1042 { FMOVRSGEZ,FCMPDelayCycle.rid, 1, 1 },
1045 // Some instructions are stalled in the GROUP stage if a CTI is in
1046 // the E or C stage. We model that with a fake resource CTIDelayCycle.
1048 { LDD, CTIDelayCycle.rid, 1, 1 },
1049 //{ LDDA, CTIDelayCycle.rid, 1, 1 },
1050 //{ LDDSTUB, CTIDelayCycle.rid, 1, 1 },
1051 //{ LDDSTUBA, CTIDelayCycle.rid, 1, 1 },
1052 //{ SWAP, CTIDelayCycle.rid, 1, 1 },
1053 //{ SWAPA, CTIDelayCycle.rid, 1, 1 },
1054 //{ CAS, CTIDelayCycle.rid, 1, 1 },
1055 //{ CASA, CTIDelayCycle.rid, 1, 1 },
1056 //{ CASX, CTIDelayCycle.rid, 1, 1 },
1057 //{ CASXA, CTIDelayCycle.rid, 1, 1 },
1060 // Signed int loads of less than dword size return data in cycle N1 (not C)
1061 // and put all loads in consecutive cycles into delayed load return mode.
1063 { LDSB, LdReturn.rid, 2, -1 },
1064 { LDSB, LdReturn.rid, 3, 1 },
1066 { LDSH, LdReturn.rid, 2, -1 },
1067 { LDSH, LdReturn.rid, 3, 1 },
1069 { LDSW, LdReturn.rid, 2, -1 },
1070 { LDSW, LdReturn.rid, 3, 1 },
1073 // RDPR from certain registers and RD from any register are not dispatchable
1074 // until four clocks after they reach the head of the instr. buffer.
1075 // Together with their single-issue requirement, this means all four issue
1076 // slots are effectively blocked for those cycles, plus the issue cycle.
1077 // This does not increase the latency of the instruction itself.
1079 { RDCCR, AllIssueSlots.rid, 0, 5 },
1080 { RDCCR, AllIssueSlots.rid, 0, 5 },
1081 { RDCCR, AllIssueSlots.rid, 0, 5 },
1082 { RDCCR, AllIssueSlots.rid, 0, 5 },
1084 #undef EXPLICIT_BUBBLES_NEEDED
1085 #ifdef EXPLICIT_BUBBLES_NEEDED
1087 // MULScc inserts one bubble.
1088 // This means it breaks the current group (captured in UltraSparcSchedInfo)
1089 // *and occupies all issue slots for the next cycle
1091 //{ MULScc, AllIssueSlots.rid, 2, 2-1 },
1092 //{ MULScc, AllIssueSlots.rid, 2, 2-1 },
1093 //{ MULScc, AllIssueSlots.rid, 2, 2-1 },
1094 //{ MULScc, AllIssueSlots.rid, 2, 2-1 },
1097 // SMULcc inserts between 4 and 18 bubbles, depending on #leading 0s in rs1.
1098 // We just model this with a simple average.
1100 //{ SMULcc, AllIssueSlots.rid, 2, ((4+18)/2)-1 },
1101 //{ SMULcc, AllIssueSlots.rid, 2, ((4+18)/2)-1 },
1102 //{ SMULcc, AllIssueSlots.rid, 2, ((4+18)/2)-1 },
1103 //{ SMULcc, AllIssueSlots.rid, 2, ((4+18)/2)-1 },
1105 // SMULcc inserts between 4 and 19 bubbles, depending on #leading 0s in rs1.
1106 //{ UMULcc, AllIssueSlots.rid, 2, ((4+19)/2)-1 },
1107 //{ UMULcc, AllIssueSlots.rid, 2, ((4+19)/2)-1 },
1108 //{ UMULcc, AllIssueSlots.rid, 2, ((4+19)/2)-1 },
1109 //{ UMULcc, AllIssueSlots.rid, 2, ((4+19)/2)-1 },
1112 // MULX inserts between 4 and 34 bubbles, depending on #leading 0s in rs1.
1114 { MULX, AllIssueSlots.rid, 2, ((4+34)/2)-1 },
1115 { MULX, AllIssueSlots.rid, 2, ((4+34)/2)-1 },
1116 { MULX, AllIssueSlots.rid, 2, ((4+34)/2)-1 },
1117 { MULX, AllIssueSlots.rid, 2, ((4+34)/2)-1 },
1120 // SDIVcc inserts 36 bubbles.
1122 //{ SDIVcc, AllIssueSlots.rid, 2, 36-1 },
1123 //{ SDIVcc, AllIssueSlots.rid, 2, 36-1 },
1124 //{ SDIVcc, AllIssueSlots.rid, 2, 36-1 },
1125 //{ SDIVcc, AllIssueSlots.rid, 2, 36-1 },
1127 // UDIVcc inserts 37 bubbles.
1128 //{ UDIVcc, AllIssueSlots.rid, 2, 37-1 },
1129 //{ UDIVcc, AllIssueSlots.rid, 2, 37-1 },
1130 //{ UDIVcc, AllIssueSlots.rid, 2, 37-1 },
1131 //{ UDIVcc, AllIssueSlots.rid, 2, 37-1 },
1134 // SDIVX inserts 68 bubbles.
1136 { SDIVX, AllIssueSlots.rid, 2, 68-1 },
1137 { SDIVX, AllIssueSlots.rid, 2, 68-1 },
1138 { SDIVX, AllIssueSlots.rid, 2, 68-1 },
1139 { SDIVX, AllIssueSlots.rid, 2, 68-1 },
1142 // UDIVX inserts 68 bubbles.
1144 { UDIVX, AllIssueSlots.rid, 2, 68-1 },
1145 { UDIVX, AllIssueSlots.rid, 2, 68-1 },
1146 { UDIVX, AllIssueSlots.rid, 2, 68-1 },
1147 { UDIVX, AllIssueSlots.rid, 2, 68-1 },
1150 // WR inserts 4 bubbles.
1152 //{ WR, AllIssueSlots.rid, 2, 68-1 },
1153 //{ WR, AllIssueSlots.rid, 2, 68-1 },
1154 //{ WR, AllIssueSlots.rid, 2, 68-1 },
1155 //{ WR, AllIssueSlots.rid, 2, 68-1 },
1158 // WRPR inserts 4 bubbles.
1160 //{ WRPR, AllIssueSlots.rid, 2, 68-1 },
1161 //{ WRPR, AllIssueSlots.rid, 2, 68-1 },
1162 //{ WRPR, AllIssueSlots.rid, 2, 68-1 },
1163 //{ WRPR, AllIssueSlots.rid, 2, 68-1 },
1166 // DONE inserts 9 bubbles.
1168 //{ DONE, AllIssueSlots.rid, 2, 9-1 },
1169 //{ DONE, AllIssueSlots.rid, 2, 9-1 },
1170 //{ DONE, AllIssueSlots.rid, 2, 9-1 },
1171 //{ DONE, AllIssueSlots.rid, 2, 9-1 },
1174 // RETRY inserts 9 bubbles.
1176 //{ RETRY, AllIssueSlots.rid, 2, 9-1 },
1177 //{ RETRY, AllIssueSlots.rid, 2, 9-1 },
1178 //{ RETRY, AllIssueSlots.rid, 2, 9-1 },
1179 //{ RETRY, AllIssueSlots.rid, 2, 9-1 },
1181 #endif /*EXPLICIT_BUBBLES_NEEDED */
1186 // Additional delays to be captured in code:
1187 // 1. RDPR from several state registers (page 349)
1188 // 2. RD from *any* register (page 349)
1189 // 3. Writes to TICK, PSTATE, TL registers and FLUSH{W} instr (page 349)
1190 // 4. Integer store can be in same group as instr producing value to store.
1191 // 5. BICC and BPICC can be in the same group as instr producing CC (pg 350)
1192 // 6. FMOVr cannot be in the same or next group as an IEU instr (pg 351).
1193 // 7. The second instr. of a CTI group inserts 9 bubbles (pg 351)
1194 // 8. WR{PR}, SVAE, SAVED, RESTORE, RESTORED, RETURN, RETRY, and DONE that
1195 // follow an annulling branch cannot be issued in the same group or in
1196 // the 3 groups following the branch.
1197 // 9. A predicted annulled load does not stall dependent instructions.
1198 // Other annulled delay slot instructions *do* stall dependents, so
1199 // nothing special needs to be done for them during scheduling.
1200 //10. Do not put a load use that may be annulled in the same group as the
1201 // branch. The group will stall until the load returns.
1202 //11. Single-prec. FP loads lock 2 registers, for dependency checking.
1205 // Additional delays we cannot or will not capture:
1206 // 1. If DCTI is last word of cache line, it is delayed until next line can be
1207 // fetched. Also, other DCTI alignment-related delays (pg 352)
1208 // 2. Load-after-store is delayed by 7 extra cycles if load hits in D-Cache.
1209 // Also, several other store-load and load-store conflicts (pg 358)
1210 // 3. MEMBAR, LD{X}FSR, LDD{A} and a bunch of other load stalls (pg 358)
1211 // 4. There can be at most 8 outstanding buffered store instructions
1212 // (including some others like MEMBAR, LDSTUB, CAS{AX}, and FLUSH)
1216 //---------------------------------------------------------------------------
1217 // class UltraSparcSchedInfo
1220 // Interface to instruction scheduling information for UltraSPARC.
1221 // The parameter values above are based on UltraSPARC IIi.
1222 //---------------------------------------------------------------------------
1225 class UltraSparcSchedInfo: public MachineSchedInfo {
1227 /*ctor*/ UltraSparcSchedInfo (const TargetMachine& tgt);
1228 /*dtor*/ virtual ~UltraSparcSchedInfo () {}
1230 virtual void initializeResources ();
1234 //---------------------------------------------------------------------------
1235 // class UltraSparcFrameInfo
1238 // Interface to stack frame layout info for the UltraSPARC.
1239 // Starting offsets for each area of the stack frame are aligned at
1240 // a multiple of getStackFrameSizeAlignment().
1241 //---------------------------------------------------------------------------
1243 class UltraSparcFrameInfo: public MachineFrameInfo {
1245 /*ctor*/ UltraSparcFrameInfo(const TargetMachine& tgt) : MachineFrameInfo(tgt) {}
1248 int getStackFrameSizeAlignment () const { return StackFrameSizeAlignment;}
1249 int getMinStackFrameSize () const { return MinStackFrameSize; }
1250 int getNumFixedOutgoingArgs () const { return NumFixedOutgoingArgs; }
1251 int getSizeOfEachArgOnStack () const { return SizeOfEachArgOnStack; }
1252 bool argsOnStackHaveFixedSize () const { return true; }
1255 // These methods compute offsets using the frame contents for a
1256 // particular method. The frame contents are obtained from the
1257 // MachineCodeInfoForMethod object for the given method.
1259 int getFirstIncomingArgOffset (MachineCodeForMethod& mcInfo,
1262 pos = true; // arguments area grows upwards
1263 return FirstIncomingArgOffsetFromFP;
1265 int getFirstOutgoingArgOffset (MachineCodeForMethod& mcInfo,
1268 pos = true; // arguments area grows upwards
1269 return FirstOutgoingArgOffsetFromSP;
1271 int getFirstOptionalOutgoingArgOffset(MachineCodeForMethod& mcInfo,
1274 pos = true; // arguments area grows upwards
1275 return FirstOptionalOutgoingArgOffsetFromSP;
1278 int getFirstAutomaticVarOffset (MachineCodeForMethod& mcInfo,
1280 int getRegSpillAreaOffset (MachineCodeForMethod& mcInfo,
1282 int getTmpAreaOffset (MachineCodeForMethod& mcInfo,
1284 int getDynamicAreaOffset (MachineCodeForMethod& mcInfo,
1288 // These methods specify the base register used for each stack area
1289 // (generally FP or SP)
1291 virtual int getIncomingArgBaseRegNum() const {
1292 return (int) target.getRegInfo().getFramePointer();
1294 virtual int getOutgoingArgBaseRegNum() const {
1295 return (int) target.getRegInfo().getStackPointer();
1297 virtual int getOptionalOutgoingArgBaseRegNum() const {
1298 return (int) target.getRegInfo().getStackPointer();
1300 virtual int getAutomaticVarBaseRegNum() const {
1301 return (int) target.getRegInfo().getFramePointer();
1303 virtual int getRegSpillAreaBaseRegNum() const {
1304 return (int) target.getRegInfo().getFramePointer();
1306 virtual int getDynamicAreaBaseRegNum() const {
1307 return (int) target.getRegInfo().getStackPointer();
1311 // All stack addresses must be offset by 0x7ff (2047) on Sparc V9.
1312 static const int OFFSET = (int) 0x7ff;
1313 static const int StackFrameSizeAlignment = 16;
1314 static const int MinStackFrameSize = 176;
1315 static const int NumFixedOutgoingArgs = 6;
1316 static const int SizeOfEachArgOnStack = 8;
1317 static const int StaticAreaOffsetFromFP = 0 + OFFSET;
1318 static const int FirstIncomingArgOffsetFromFP = 128 + OFFSET;
1319 static const int FirstOptionalIncomingArgOffsetFromFP = 176 + OFFSET;
1320 static const int FirstOutgoingArgOffsetFromSP = 128 + OFFSET;
1321 static const int FirstOptionalOutgoingArgOffsetFromSP = 176 + OFFSET;
1325 //---------------------------------------------------------------------------
1326 // class UltraSparcCacheInfo
1329 // Interface to cache parameters for the UltraSPARC.
1330 // Just use defaults for now.
1331 //---------------------------------------------------------------------------
1333 class UltraSparcCacheInfo: public MachineCacheInfo {
1335 /*ctor*/ UltraSparcCacheInfo (const TargetMachine& target) :
1336 MachineCacheInfo(target) {}
1340 //---------------------------------------------------------------------------
1341 // class UltraSparcMachine
1344 // Primary interface to machine description for the UltraSPARC.
1345 // Primarily just initializes machine-dependent parameters in
1346 // class TargetMachine, and creates machine-dependent subclasses
1347 // for classes such as InstrInfo, SchedInfo and RegInfo.
1348 //---------------------------------------------------------------------------
1350 class UltraSparc : public TargetMachine {
1352 UltraSparcInstrInfo instrInfo;
1353 UltraSparcSchedInfo schedInfo;
1354 UltraSparcRegInfo regInfo;
1355 UltraSparcFrameInfo frameInfo;
1356 UltraSparcCacheInfo cacheInfo;
1359 virtual ~UltraSparc() {}
1361 virtual const MachineInstrInfo &getInstrInfo() const { return instrInfo; }
1362 virtual const MachineSchedInfo &getSchedInfo() const { return schedInfo; }
1363 virtual const MachineRegInfo &getRegInfo() const { return regInfo; }
1364 virtual const MachineFrameInfo &getFrameInfo() const { return frameInfo; }
1365 virtual const MachineCacheInfo &getCacheInfo() const { return cacheInfo; }
1367 // compileMethod - For the sparc, we do instruction selection, followed by
1368 // delay slot scheduling, then register allocation.
1370 virtual bool compileMethod(Method *M);
1373 // emitAssembly - Output assembly language code (a .s file) for the specified
1374 // module. The specified module must have been compiled before this may be
1377 virtual void emitAssembly(const Module *M, ostream &OutStr) const;