Added support for caller saving
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9Internals.h
1 //===-- SparcInternals.h - Header file for Sparc backend ---------*- C++ -*--=//
2 //
3 // This file defines stuff that is to be private to the Sparc backend, but is 
4 // shared among different portions of the backend.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef SPARC_INTERNALS_H
9 #define SPARC_INTERNALS_H
10
11
12 #include "SparcRegClassInfo.h"
13 #include "llvm/Target/TargetMachine.h"
14 #include "llvm/Target/MachineInstrInfo.h"
15
16 #include "llvm/Target/MachineSchedInfo.h"
17 #include "llvm/CodeGen/RegClass.h"
18 #include "llvm/Type.h"
19
20 #include <sys/types.h>
21
22 class UltraSparc;
23
24 // OpCodeMask definitions for the Sparc V9
25 // 
26 const OpCodeMask        Immed           = 0x00002000; // immed or reg operand?
27 const OpCodeMask        Annul           = 0x20000000; // annul delay instr?
28 const OpCodeMask        PredictTaken    = 0x00080000; // predict branch taken?
29
30
31 enum SparcInstrSchedClass {
32   SPARC_NONE,           /* Instructions with no scheduling restrictions */
33   SPARC_IEUN,           /* Integer class that can use IEU0 or IEU1 */
34   SPARC_IEU0,           /* Integer class IEU0 */
35   SPARC_IEU1,           /* Integer class IEU1 */
36   SPARC_FPM,            /* FP Multiply or Divide instructions */
37   SPARC_FPA,            /* All other FP instructions */ 
38   SPARC_CTI,            /* Control-transfer instructions */
39   SPARC_LD,             /* Load instructions */
40   SPARC_ST,             /* Store instructions */
41   SPARC_SINGLE,         /* Instructions that must issue by themselves */
42   
43   SPARC_INV,            /* This should stay at the end for the next value */
44   SPARC_NUM_SCHED_CLASSES = SPARC_INV
45 };
46
47
48 //---------------------------------------------------------------------------
49 // enum SparcMachineOpCode. 
50 // const MachineInstrDescriptor SparcMachineInstrDesc[]
51 // 
52 // Purpose:
53 //   Description of UltraSparc machine instructions.
54 // 
55 //---------------------------------------------------------------------------
56
57 enum SparcMachineOpCode {
58 #define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
59           NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS)             \
60    ENUM,
61 #include "SparcInstr.def"
62
63   // End-of-array marker
64   INVALID_OPCODE,
65   NUM_REAL_OPCODES = RETURN+1,          // number of valid opcodes
66   NUM_TOTAL_OPCODES = INVALID_OPCODE
67 };
68
69
70 // Array of machine instruction descriptions...
71 extern const MachineInstrDescriptor SparcMachineInstrDesc[];
72
73
74 //---------------------------------------------------------------------------
75 // class UltraSparcInstrInfo 
76 // 
77 // Purpose:
78 //   Information about individual instructions.
79 //   Most information is stored in the SparcMachineInstrDesc array above.
80 //   Other information is computed on demand, and most such functions
81 //   default to member functions in base class MachineInstrInfo. 
82 //---------------------------------------------------------------------------
83
84 class UltraSparcInstrInfo : public MachineInstrInfo {
85 public:
86   /*ctor*/      UltraSparcInstrInfo();
87   
88   virtual bool          hasResultInterlock      (MachineOpCode opCode)
89   {
90     // All UltraSPARC instructions have interlocks (note that delay slots
91     // are not considered here).
92     // However, instructions that use the result of an FCMP produce a
93     // 9-cycle stall if they are issued less than 3 cycles after the FCMP.
94     // Force the compiler to insert a software interlock (i.e., gap of
95     // 2 other groups, including NOPs if necessary).
96     return (opCode == FCMPS || opCode == FCMPD || opCode == FCMPQ);
97   }
98
99
100
101
102   
103 };
104
105
106
107 class LiveRange;
108 class UltraSparc;
109
110
111
112 class UltraSparcRegInfo : public MachineRegInfo
113 {
114
115  private:
116
117   // The actual register classes in the Sparc
118
119   enum RegClassIDs { 
120     IntRegClassID, 
121     FloatRegClassID, 
122     IntCCRegClassID,
123     FloatCCRegClassID 
124   };
125
126
127   // Type of registers available in Sparc. There can be several reg types
128   // in the same class. For instace, the float reg class has Single/Double
129   // types
130   enum RegTypes {
131     IntRegType,
132     FPSingleRegType,
133     FPDoubleRegType,
134     IntCCRegType,
135     FloatCCRegType
136   };
137
138
139   // WARNING: If the above enum order must be changed, also modify 
140   // getRegisterClassOfValue method below since it assumes this particular 
141   // order for efficiency.
142
143
144   // reverse pointer to get info about the ultra sparc machine
145   const UltraSparc *const UltraSparcInfo;
146
147   // Both int and float rguments can be passed in 6 int regs - 
148   // %o0 to %o5 (cannot be changed)
149   unsigned const NumOfIntArgRegs;
150   unsigned const NumOfFloatArgRegs;
151   int const InvalidRegNum;
152
153   //void setCallArgColor(LiveRange *const LR, const unsigned RegNo) const;
154
155   void setCallOrRetArgCol(LiveRange *const LR, const unsigned RegNo,
156                          const MachineInstr *MI,AddedInstrMapType &AIMap)const;
157
158   MachineInstr * getCopy2RegMI(const Value *SrcVal, const unsigned Reg,
159                                unsigned RegClassID) const ;
160
161
162   void suggestReg4RetAddr(const MachineInstr * RetMI, 
163                           LiveRangeInfo& LRI) const;
164
165   void suggestReg4CallAddr(const MachineInstr * CallMI) const;
166
167
168   Value *getValue4ReturnAddr( const MachineInstr * MInst ) const ;
169
170   int getRegType(const LiveRange *const LR) const {
171
172     unsigned Typ;
173
174     switch(  (LR->getRegClass())->getID() ) {
175
176     case IntRegClassID: return IntRegType; 
177
178     case FloatRegClassID: 
179                           Typ =  LR->getTypeID();
180                           if( Typ == Type::FloatTyID ) 
181                             return FPSingleRegType;
182                           else if( Typ == Type::DoubleTyID )
183                             return FPDoubleRegType;
184                           else assert(0 && "Unknown type in FloatRegClass");
185
186     case IntCCRegClassID: return IntCCRegType; 
187       
188     case FloatCCRegClassID: return FloatCCRegType ; 
189
190     default: assert( 0 && "Unknown reg class ID");
191
192     }
193
194   }
195
196   int getRegType(const Value *const Val) const {
197
198     unsigned Typ;
199
200     switch( getRegClassIDOfValue(Val)  ) {
201
202     case IntRegClassID: return IntRegType; 
203
204     case FloatRegClassID: 
205                           Typ =  (Val->getType())->getPrimitiveID();
206                           if( Typ == Type::FloatTyID ) 
207                             return FPSingleRegType;
208                           else if( Typ == Type::DoubleTyID )
209                             return FPDoubleRegType;
210                           else assert(0 && "Unknown type in FloatRegClass");
211
212     case IntCCRegClassID: return IntCCRegType; 
213       
214     case FloatCCRegClassID: return FloatCCRegType ; 
215
216     default: assert( 0 && "Unknown reg class ID");
217
218     }
219
220   }
221
222
223
224   // ***TODO: See this method is necessary
225
226   MachineInstr * cpValue2RegMI(Value * Val,  const unsigned DestReg,
227                                const int RegType) const;
228
229
230  public:
231
232
233   UltraSparcRegInfo(const UltraSparc *const USI ) : UltraSparcInfo(USI), 
234                                                     NumOfIntArgRegs(6), 
235                                                     NumOfFloatArgRegs(32),
236                                                     InvalidRegNum(1000)
237   {    
238     MachineRegClassArr.push_back( new SparcIntRegClass(IntRegClassID) );
239     MachineRegClassArr.push_back( new SparcFloatRegClass(FloatRegClassID) );
240     MachineRegClassArr.push_back( new SparcIntCCRegClass(IntCCRegClassID) );
241     MachineRegClassArr.push_back( new SparcFloatCCRegClass(FloatCCRegClassID));
242
243     assert( SparcFloatRegOrder::StartOfNonVolatileRegs == 32 && 
244             "32 Float regs are used for float arg passing");
245   }
246
247   // ***** TODO  Delete
248   ~UltraSparcRegInfo(void) { }              // empty destructor 
249
250
251   inline const UltraSparc & getUltraSparcInfo() const { 
252     return *UltraSparcInfo;
253   }
254
255
256
257   inline unsigned getRegClassIDOfValue (const Value *const Val,
258                                         bool isCCReg = false) const {
259
260     Type::PrimitiveID ty = (Val->getType())->getPrimitiveID();
261
262     unsigned res;
263     
264     if( (ty && ty <= Type::LongTyID) || (ty == Type::LabelTyID) ||
265         (ty == Type::MethodTyID) ||  (ty == Type::PointerTyID) )
266       res =  IntRegClassID;             // sparc int reg (ty=0: void)
267     else if( ty <= Type::DoubleTyID)
268       res = FloatRegClassID;           // sparc float reg class
269     else { 
270       cerr << "TypeID: " << ty << endl;
271       assert(0 && "Cannot resolve register class for type");
272     }
273
274     if(isCCReg)
275       return res + 2;      // corresponidng condition code regiser 
276     else 
277       return res;
278   }
279
280   // returns the register tha contains always zero
281   // this is the unified register number
282   inline int getZeroRegNum() const { return SparcIntRegOrder::g0; }
283
284   // returns the reg used for pushing the address when a method is called.
285   // This can be used for other purposes between calls
286   unsigned getCallAddressReg() const  { return SparcIntRegOrder::o7; }
287
288   
289   // and when we return from a method. It should be made sure that this 
290   // register contains the return value when a return instruction is reached.
291   unsigned getReturnAddressReg()  const { return SparcIntRegOrder::i7; }
292
293   void suggestRegs4MethodArgs(const Method *const Meth, 
294                               LiveRangeInfo& LRI) const;
295
296   void suggestRegs4CallArgs(const MachineInstr *const CallMI, 
297                             LiveRangeInfo& LRI, vector<RegClass *> RCL) const; 
298
299   void suggestReg4RetValue(const MachineInstr *const RetMI, 
300                             LiveRangeInfo& LRI ) const;
301
302
303   void colorMethodArgs(const Method *const Meth,  LiveRangeInfo& LRI,
304                        AddedInstrns *const FirstAI) const;
305
306   void colorCallArgs(const MachineInstr *const CallMI, LiveRangeInfo& LRI,
307                      AddedInstrns *const CallAI) const;
308
309   void colorRetValue(const MachineInstr *const RetI,   LiveRangeInfo& LRI,
310                      AddedInstrns *const RetAI) const;
311
312
313   // bool handleSpecialMInstr(const MachineInstr * MInst, 
314   //                       LiveRangeInfo& LRI, vector<RegClass *> RCL) const;
315
316
317     static void printReg(const LiveRange *const LR)  ;
318
319   // this method provides a unique number for each register 
320   inline int getUnifiedRegNum(int RegClassID, int reg) const {
321
322     if( RegClassID == IntRegClassID && reg < 32 ) 
323       return reg;
324     else if ( RegClassID == FloatRegClassID && reg < 64)
325       return reg + 32;                  // we have 32 int regs
326     else if( RegClassID == FloatCCRegClassID && reg < 4)
327       return reg + 32 + 64;             // 32 int, 64 float
328     else if( RegClassID == IntCCRegClassID ) 
329       return 4+ 32 + 64;                // only int cc reg
330     else if (reg==InvalidRegNum)                
331       return InvalidRegNum;
332     else  
333       assert(0 && "Invalid register class or reg number");
334
335   }
336
337   // given the unified register number, this gives the name
338   inline const string getUnifiedRegName(int reg) const {
339     if( reg < 32 ) 
340       return SparcIntRegOrder::getRegName(reg);
341     else if ( reg < (64 + 32) )
342       return SparcFloatRegOrder::getRegName( reg  - 32);                  
343     else if( reg < (64+32+4) )
344       return SparcFloatCCRegOrder::getRegName( reg -32 - 64);
345     else if ( reg == 64+32+4)
346       return "xcc";                     // only integer cc reg
347
348     else if (reg== InvalidRegNum)       //****** TODO: Remove
349       return "<*NoReg*>";
350     else 
351       assert(0 && "Invalid register number");
352   }
353
354
355   MachineInstr * cpReg2RegMI(const unsigned SrcReg, const unsigned DestReg,
356                              const int RegType) const;
357
358   MachineInstr * cpReg2MemMI(const unsigned SrcReg,  const unsigned DestPtrReg,
359                              const int Offset, const int RegType) const;
360
361   MachineInstr * cpMem2RegMI(const unsigned SrcPtrReg, const int Offset,
362                              const unsigned DestReg, const int RegType) const;
363
364   inline bool isRegVolatile(const int RegClassID, const int Reg) const {
365     return  (MachineRegClassArr[RegClassID])->isRegVolatile(Reg);
366   }
367
368
369   inline unsigned getFramePointer() const {
370     return SparcIntRegOrder::i6;
371   }
372
373   inline unsigned getStackPointer() const {
374     return SparcIntRegOrder::o6;
375   }
376
377   inline int getInvalidRegNum() const {
378     return InvalidRegNum;
379   }
380
381 };
382
383
384
385 /*---------------------------------------------------------------------------
386 Scheduling guidelines for SPARC IIi:
387
388 I-Cache alignment rules (pg 326)
389 -- Align a branch target instruction so that it's entire group is within
390    the same cache line (may be 1-4 instructions).
391 ** Don't let a branch that is predicted taken be the last instruction
392    on an I-cache line: delay slot will need an entire line to be fetched
393 -- Make a FP instruction or a branch be the 4th instruction in a group.
394    For branches, there are tradeoffs in reordering to make this happen
395    (see pg. 327).
396 ** Don't put a branch in a group that crosses a 32-byte boundary!
397    An artificial branch is inserted after every 32 bytes, and having
398    another branch will force the group to be broken into 2 groups. 
399
400 iTLB rules:
401 -- Don't let a loop span two memory pages, if possible
402
403 Branch prediction performance:
404 -- Don't make the branch in a delay slot the target of a branch
405 -- Try not to have 2 predicted branches within a group of 4 instructions
406    (because each such group has a single branch target field).
407 -- Try to align branches in slots 0, 2, 4 or 6 of a cache line (to avoid
408    the wrong prediction bits being used in some cases).
409
410 D-Cache timing constraints:
411 -- Signed int loads of less than 64 bits have 3 cycle latency, not 2
412 -- All other loads that hit in D-Cache have 2 cycle latency
413 -- All loads are returned IN ORDER, so a D-Cache miss will delay a later hit
414 -- Mis-aligned loads or stores cause a trap.  In particular, replace
415    mis-aligned FP double precision l/s with 2 single-precision l/s.
416 -- Simulations of integer codes show increase in avg. group size of
417    33% when code (including esp. non-faulting loads) is moved across
418    one branch, and 50% across 2 branches.
419
420 E-Cache timing constraints:
421 -- Scheduling for E-cache (D-Cache misses) is effective (due to load buffering)
422
423 Store buffer timing constraints:
424 -- Stores can be executed in same cycle as instruction producing the value
425 -- Stores are buffered and have lower priority for E-cache until
426    highwater mark is reached in the store buffer (5 stores)
427
428 Pipeline constraints:
429 -- Shifts can only use IEU0.
430 -- CC setting instructions can only use IEU1.
431 -- Several other instructions must only use IEU1:
432    EDGE(?), ARRAY(?), CALL, JMPL, BPr, PST, and FCMP.
433 -- Two instructions cannot store to the same register file in a single cycle
434    (single write port per file).
435
436 Issue and grouping constraints:
437 -- FP and branch instructions must use slot 4.
438 -- Shift instructions cannot be grouped with other IEU0-specific instructions.
439 -- CC setting instructions cannot be grouped with other IEU1-specific instrs.
440 -- Several instructions must be issued in a single-instruction group:
441         MOVcc or MOVr, MULs/x and DIVs/x, SAVE/RESTORE, many others
442 -- A CALL or JMPL breaks a group, ie, is not combined with subsequent instrs.
443 -- 
444 -- 
445
446 Branch delay slot scheduling rules:
447 -- A CTI couple (two back-to-back CTI instructions in the dynamic stream)
448    has a 9-instruction penalty: the entire pipeline is flushed when the
449    second instruction reaches stage 9 (W-Writeback).
450 -- Avoid putting multicycle instructions, and instructions that may cause
451    load misses, in the delay slot of an annulling branch.
452 -- Avoid putting WR, SAVE..., RESTORE and RETURN instructions in the
453    delay slot of an annulling branch.
454
455  *--------------------------------------------------------------------------- */
456
457 //---------------------------------------------------------------------------
458 // List of CPUResources for UltraSPARC IIi.
459 //---------------------------------------------------------------------------
460
461 const CPUResource  AllIssueSlots(   "All Instr Slots", 4);
462 const CPUResource  IntIssueSlots(   "Int Instr Slots", 3);
463 const CPUResource  First3IssueSlots("Instr Slots 0-3", 3);
464 const CPUResource  LSIssueSlots(    "Load-Store Instr Slot", 1);
465 const CPUResource  CTIIssueSlots(   "Ctrl Transfer Instr Slot", 1);
466 const CPUResource  FPAIssueSlots(   "Int Instr Slot 1", 1);
467 const CPUResource  FPMIssueSlots(   "Int Instr Slot 1", 1);
468
469 // IEUN instructions can use either Alu and should use IAluN.
470 // IEU0 instructions must use Alu 1 and should use both IAluN and IAlu0. 
471 // IEU1 instructions must use Alu 2 and should use both IAluN and IAlu1. 
472 const CPUResource  IAluN("Int ALU 1or2", 2);
473 const CPUResource  IAlu0("Int ALU 1",    1);
474 const CPUResource  IAlu1("Int ALU 2",    1);
475
476 const CPUResource  LSAluC1("Load/Store Unit Addr Cycle", 1);
477 const CPUResource  LSAluC2("Load/Store Unit Issue Cycle", 1);
478 const CPUResource  LdReturn("Load Return Unit", 1);
479
480 const CPUResource  FPMAluC1("FP Mul/Div Alu Cycle 1", 1);
481 const CPUResource  FPMAluC2("FP Mul/Div Alu Cycle 2", 1);
482 const CPUResource  FPMAluC3("FP Mul/Div Alu Cycle 3", 1);
483
484 const CPUResource  FPAAluC1("FP Other Alu Cycle 1", 1);
485 const CPUResource  FPAAluC2("FP Other Alu Cycle 2", 1);
486 const CPUResource  FPAAluC3("FP Other Alu Cycle 3", 1);
487
488 const CPUResource  IRegReadPorts("Int Reg ReadPorts", INT_MAX);  // CHECK
489 const CPUResource  IRegWritePorts("Int Reg WritePorts", 2);      // CHECK
490 const CPUResource  FPRegReadPorts("FP Reg Read Ports", INT_MAX); // CHECK
491 const CPUResource  FPRegWritePorts("FP Reg Write Ports", 1);     // CHECK
492
493 const CPUResource  CTIDelayCycle( "CTI  delay cycle", 1);
494 const CPUResource  FCMPDelayCycle("FCMP delay cycle", 1);
495
496
497 //---------------------------------------------------------------------------
498 // const InstrClassRUsage SparcRUsageDesc[]
499 // 
500 // Purpose:
501 //   Resource usage information for instruction in each scheduling class.
502 //   The InstrRUsage Objects for individual classes are specified first.
503 //   Note that fetch and decode are decoupled from the execution pipelines
504 //   via an instr buffer, so they are not included in the cycles below.
505 //---------------------------------------------------------------------------
506
507 const InstrClassRUsage NoneClassRUsage = {
508   SPARC_NONE,
509   /*totCycles*/ 7,
510   
511   /* maxIssueNum */ 4,
512   /* isSingleIssue */ false,
513   /* breaksGroup */ false,
514   /* numBubbles */ 0,
515   
516   /*numSlots*/ 4,
517   /* feasibleSlots[] */ { 0, 1, 2, 3 },
518   
519   /*numEntries*/ 0,
520   /* V[] */ {
521     /*Cycle G */
522     /*Ccle E */
523     /*Cycle C */
524     /*Cycle N1*/
525     /*Cycle N1*/
526     /*Cycle N1*/
527     /*Cycle W */
528   }
529 };
530
531 const InstrClassRUsage IEUNClassRUsage = {
532   SPARC_IEUN,
533   /*totCycles*/ 7,
534   
535   /* maxIssueNum */ 3,
536   /* isSingleIssue */ false,
537   /* breaksGroup */ false,
538   /* numBubbles */ 0,
539   
540   /*numSlots*/ 3,
541   /* feasibleSlots[] */ { 0, 1, 2 },
542   
543   /*numEntries*/ 4,
544   /* V[] */ {
545     /*Cycle G */ { AllIssueSlots.rid, 0, 1 },
546                  { IntIssueSlots.rid, 0, 1 },
547     /*Cycle E */ { IAluN.rid, 1, 1 },
548     /*Cycle C */
549     /*Cycle N1*/
550     /*Cycle N1*/
551     /*Cycle N1*/
552     /*Cycle W */ { IRegWritePorts.rid, 6, 1  }
553   }
554 };
555
556 const InstrClassRUsage IEU0ClassRUsage = {
557   SPARC_IEU0,
558   /*totCycles*/ 7,
559   
560   /* maxIssueNum */ 1,
561   /* isSingleIssue */ false,
562   /* breaksGroup */ false,
563   /* numBubbles */ 0,
564   
565   /*numSlots*/ 3,
566   /* feasibleSlots[] */ { 0, 1, 2 },
567   
568   /*numEntries*/ 5,
569   /* V[] */ {
570     /*Cycle G */ { AllIssueSlots.rid, 0, 1 },
571                  { IntIssueSlots.rid, 0, 1 },
572     /*Cycle E */ { IAluN.rid, 1, 1 },
573                  { IAlu0.rid, 1, 1 },
574     /*Cycle C */
575     /*Cycle N1*/
576     /*Cycle N1*/
577     /*Cycle N1*/
578     /*Cycle W */ { IRegWritePorts.rid, 6, 1 }
579   }
580 };
581
582 const InstrClassRUsage IEU1ClassRUsage = {
583   SPARC_IEU1,
584   /*totCycles*/ 7,
585   
586   /* maxIssueNum */ 1,
587   /* isSingleIssue */ false,
588   /* breaksGroup */ false,
589   /* numBubbles */ 0,
590   
591   /*numSlots*/ 3,
592   /* feasibleSlots[] */ { 0, 1, 2 },
593   
594   /*numEntries*/ 5,
595   /* V[] */ {
596     /*Cycle G */ { AllIssueSlots.rid, 0, 1 },
597                { IntIssueSlots.rid, 0, 1 },
598     /*Cycle E */ { IAluN.rid, 1, 1 },
599                { IAlu1.rid, 1, 1 },
600     /*Cycle C */
601     /*Cycle N1*/
602     /*Cycle N1*/
603     /*Cycle N1*/
604     /*Cycle W */ { IRegWritePorts.rid, 6, 1 }
605   }
606 };
607
608 const InstrClassRUsage FPMClassRUsage = {
609   SPARC_FPM,
610   /*totCycles*/ 7,
611   
612   /* maxIssueNum */ 1,
613   /* isSingleIssue */ false,
614   /* breaksGroup */ false,
615   /* numBubbles */ 0,
616   
617   /*numSlots*/ 4,
618   /* feasibleSlots[] */ { 0, 1, 2, 3 },
619   
620   /*numEntries*/ 7,
621   /* V[] */ {
622     /*Cycle G */ { AllIssueSlots.rid,   0, 1 },
623                  { FPMIssueSlots.rid,   0, 1 },
624     /*Cycle E */ { FPRegReadPorts.rid,  1, 1 },
625     /*Cycle C */ { FPMAluC1.rid,        2, 1 },
626     /*Cycle N1*/ { FPMAluC2.rid,        3, 1 },
627     /*Cycle N1*/ { FPMAluC3.rid,        4, 1 },
628     /*Cycle N1*/
629     /*Cycle W */ { FPRegWritePorts.rid, 6, 1 }
630   }
631 };
632
633 const InstrClassRUsage FPAClassRUsage = {
634   SPARC_FPA,
635   /*totCycles*/ 7,
636   
637   /* maxIssueNum */ 1,
638   /* isSingleIssue */ false,
639   /* breaksGroup */ false,
640   /* numBubbles */ 0,
641   
642   /*numSlots*/ 4,
643   /* feasibleSlots[] */ { 0, 1, 2, 3 },
644   
645   /*numEntries*/ 7,
646   /* V[] */ {
647     /*Cycle G */ { AllIssueSlots.rid,   0, 1 },
648                  { FPAIssueSlots.rid,   0, 1 },
649     /*Cycle E */ { FPRegReadPorts.rid,  1, 1 },
650     /*Cycle C */ { FPAAluC1.rid,        2, 1 },
651     /*Cycle N1*/ { FPAAluC2.rid,        3, 1 },
652     /*Cycle N1*/ { FPAAluC3.rid,        4, 1 },
653     /*Cycle N1*/
654     /*Cycle W */ { FPRegWritePorts.rid, 6, 1 }
655   }
656 };
657
658 const InstrClassRUsage LDClassRUsage = {
659   SPARC_LD,
660   /*totCycles*/ 7,
661   
662   /* maxIssueNum */ 1,
663   /* isSingleIssue */ false,
664   /* breaksGroup */ false,
665   /* numBubbles */ 0,
666   
667   /*numSlots*/ 3,
668   /* feasibleSlots[] */ { 0, 1, 2, },
669   
670   /*numEntries*/ 6,
671   /* V[] */ {
672     /*Cycle G */ { AllIssueSlots.rid,    0, 1 },
673                  { First3IssueSlots.rid, 0, 1 },
674                  { LSIssueSlots.rid,     0, 1 },
675     /*Cycle E */ { LSAluC1.rid,          1, 1 },
676     /*Cycle C */ { LSAluC2.rid,          2, 1 },
677                  { LdReturn.rid,         2, 1 },
678     /*Cycle N1*/
679     /*Cycle N1*/
680     /*Cycle N1*/
681     /*Cycle W */ { IRegWritePorts.rid,   6, 1 }
682   }
683 };
684
685 const InstrClassRUsage STClassRUsage = {
686   SPARC_ST,
687   /*totCycles*/ 7,
688   
689   /* maxIssueNum */ 1,
690   /* isSingleIssue */ false,
691   /* breaksGroup */ false,
692   /* numBubbles */ 0,
693   
694   /*numSlots*/ 3,
695   /* feasibleSlots[] */ { 0, 1, 2 },
696   
697   /*numEntries*/ 4,
698   /* V[] */ {
699     /*Cycle G */ { AllIssueSlots.rid,    0, 1 },
700                  { First3IssueSlots.rid, 0, 1 },
701                  { LSIssueSlots.rid,     0, 1 },
702     /*Cycle E */ { LSAluC1.rid,          1, 1 },
703     /*Cycle C */ { LSAluC2.rid,          2, 1 }
704     /*Cycle N1*/
705     /*Cycle N1*/
706     /*Cycle N1*/
707     /*Cycle W */
708   }
709 };
710
711 const InstrClassRUsage CTIClassRUsage = {
712   SPARC_CTI,
713   /*totCycles*/ 7,
714   
715   /* maxIssueNum */ 1,
716   /* isSingleIssue */ false,
717   /* breaksGroup */ false,
718   /* numBubbles */ 0,
719   
720   /*numSlots*/ 4,
721   /* feasibleSlots[] */ { 0, 1, 2, 3 },
722   
723   /*numEntries*/ 4,
724   /* V[] */ {
725     /*Cycle G */ { AllIssueSlots.rid,    0, 1 },
726                  { CTIIssueSlots.rid,    0, 1 },
727     /*Cycle E */ { IAlu0.rid,            1, 1 },
728     /*Cycles E-C */ { CTIDelayCycle.rid, 1, 2 }
729     /*Cycle C */             
730     /*Cycle N1*/
731     /*Cycle N1*/
732     /*Cycle N1*/
733     /*Cycle W */
734   }
735 };
736
737 const InstrClassRUsage SingleClassRUsage = {
738   SPARC_SINGLE,
739   /*totCycles*/ 7,
740   
741   /* maxIssueNum */ 1,
742   /* isSingleIssue */ true,
743   /* breaksGroup */ false,
744   /* numBubbles */ 0,
745   
746   /*numSlots*/ 1,
747   /* feasibleSlots[] */ { 0 },
748   
749   /*numEntries*/ 5,
750   /* V[] */ {
751     /*Cycle G */ { AllIssueSlots.rid,    0, 1 },
752                  { AllIssueSlots.rid,    0, 1 },
753                  { AllIssueSlots.rid,    0, 1 },
754                  { AllIssueSlots.rid,    0, 1 },
755     /*Cycle E */ { IAlu0.rid,            1, 1 }
756     /*Cycle C */
757     /*Cycle N1*/
758     /*Cycle N1*/
759     /*Cycle N1*/
760     /*Cycle W */
761   }
762 };
763
764
765 const InstrClassRUsage SparcRUsageDesc[] = {
766   NoneClassRUsage,
767   IEUNClassRUsage,
768   IEU0ClassRUsage,
769   IEU1ClassRUsage,
770   FPMClassRUsage,
771   FPAClassRUsage,
772   CTIClassRUsage,
773   LDClassRUsage,
774   STClassRUsage,
775   SingleClassRUsage
776 };
777
778
779 //---------------------------------------------------------------------------
780 // const InstrIssueDelta  SparcInstrIssueDeltas[]
781 // 
782 // Purpose:
783 //   Changes to issue restrictions information in InstrClassRUsage for
784 //   instructions that differ from other instructions in their class.
785 //---------------------------------------------------------------------------
786
787 const InstrIssueDelta  SparcInstrIssueDeltas[] = {
788
789   // opCode,  isSingleIssue,  breaksGroup,  numBubbles
790
791                                 // Special cases for single-issue only
792                                 // Other single issue cases are below.
793 //{ LDDA,       true,   true,   0 },
794 //{ STDA,       true,   true,   0 },
795 //{ LDDF,       true,   true,   0 },
796 //{ LDDFA,      true,   true,   0 },
797   { ADDC,       true,   true,   0 },
798   { ADDCcc,     true,   true,   0 },
799   { SUBC,       true,   true,   0 },
800   { SUBCcc,     true,   true,   0 },
801 //{ SAVE,       true,   true,   0 },
802 //{ RESTORE,    true,   true,   0 },
803 //{ LDSTUB,     true,   true,   0 },
804 //{ SWAP,       true,   true,   0 },
805 //{ SWAPA,      true,   true,   0 },
806 //{ CAS,        true,   true,   0 },
807 //{ CASA,       true,   true,   0 },
808 //{ CASX,       true,   true,   0 },
809 //{ CASXA,      true,   true,   0 },
810 //{ LDFSR,      true,   true,   0 },
811 //{ LDFSRA,     true,   true,   0 },
812 //{ LDXFSR,     true,   true,   0 },
813 //{ LDXFSRA,    true,   true,   0 },
814 //{ STFSR,      true,   true,   0 },
815 //{ STFSRA,     true,   true,   0 },
816 //{ STXFSR,     true,   true,   0 },
817 //{ STXFSRA,    true,   true,   0 },
818 //{ SAVED,      true,   true,   0 },
819 //{ RESTORED,   true,   true,   0 },
820 //{ FLUSH,      true,   true,   9 },
821 //{ FLUSHW,     true,   true,   9 },
822 //{ ALIGNADDR,  true,   true,   0 },
823   { RETURN,     true,   true,   0 },
824 //{ DONE,       true,   true,   0 },
825 //{ RETRY,      true,   true,   0 },
826 //{ WR,         true,   true,   0 },
827 //{ WRPR,       true,   true,   4 },
828 //{ RD,         true,   true,   0 },
829 //{ RDPR,       true,   true,   0 },
830 //{ TCC,        true,   true,   0 },
831 //{ SHUTDOWN,   true,   true,   0 },
832   
833                                 // Special cases for breaking group *before*
834                                 // CURRENTLY NOT SUPPORTED!
835   { CALL,       false,  false,  0 },
836   { JMPL,       false,  false,  0 },
837   
838                                 // Special cases for breaking the group *after*
839   { MULX,       true,   true,   (4+34)/2 },
840   { FDIVS,      false,  true,   0 },
841   { FDIVD,      false,  true,   0 },
842   { FDIVQ,      false,  true,   0 },
843   { FSQRTS,     false,  true,   0 },
844   { FSQRTD,     false,  true,   0 },
845   { FSQRTQ,     false,  true,   0 },
846 //{ FCMP{LE,GT,NE,EQ}, false, true, 0 },
847   
848                                 // Instructions that introduce bubbles
849 //{ MULScc,     true,   true,   2 },
850 //{ SMULcc,     true,   true,   (4+18)/2 },
851 //{ UMULcc,     true,   true,   (4+19)/2 },
852   { SDIVX,      true,   true,   68 },
853   { UDIVX,      true,   true,   68 },
854 //{ SDIVcc,     true,   true,   36 },
855 //{ UDIVcc,     true,   true,   37 },
856 //{ WR,         false,  false,  4 },
857 //{ WRPR,       false,  false,  4 },
858 };
859
860
861 //---------------------------------------------------------------------------
862 // const InstrRUsageDelta SparcInstrUsageDeltas[]
863 // 
864 // Purpose:
865 //   Changes to resource usage information in InstrClassRUsage for
866 //   instructions that differ from other instructions in their class.
867 //---------------------------------------------------------------------------
868
869 const InstrRUsageDelta SparcInstrUsageDeltas[] = {
870
871   // MachineOpCode, Resource, Start cycle, Num cycles
872
873   // 
874   // JMPL counts as a load/store instruction for issue!
875   //
876   { JMPL,     LSIssueSlots.rid,  0,  1 },
877   
878   // 
879   // Many instructions cannot issue for the next 2 cycles after an FCMP
880   // We model that with a fake resource FCMPDelayCycle.
881   // 
882   { FCMPS,    FCMPDelayCycle.rid, 1, 3 },
883   { FCMPD,    FCMPDelayCycle.rid, 1, 3 },
884   { FCMPQ,    FCMPDelayCycle.rid, 1, 3 },
885   
886   { MULX,     FCMPDelayCycle.rid, 1, 1 },
887   { SDIVX,    FCMPDelayCycle.rid, 1, 1 },
888   { UDIVX,    FCMPDelayCycle.rid, 1, 1 },
889 //{ SMULcc,   FCMPDelayCycle.rid, 1, 1 },
890 //{ UMULcc,   FCMPDelayCycle.rid, 1, 1 },
891 //{ SDIVcc,   FCMPDelayCycle.rid, 1, 1 },
892 //{ UDIVcc,   FCMPDelayCycle.rid, 1, 1 },
893   { STD,      FCMPDelayCycle.rid, 1, 1 },
894   { FMOVRSZ,  FCMPDelayCycle.rid, 1, 1 },
895   { FMOVRSLEZ,FCMPDelayCycle.rid, 1, 1 },
896   { FMOVRSLZ, FCMPDelayCycle.rid, 1, 1 },
897   { FMOVRSNZ, FCMPDelayCycle.rid, 1, 1 },
898   { FMOVRSGZ, FCMPDelayCycle.rid, 1, 1 },
899   { FMOVRSGEZ,FCMPDelayCycle.rid, 1, 1 },
900   
901   // 
902   // Some instructions are stalled in the GROUP stage if a CTI is in
903   // the E or C stage
904   // 
905   { LDD,      CTIDelayCycle.rid,  1, 1 },
906 //{ LDDA,     CTIDelayCycle.rid,  1, 1 },
907 //{ LDDSTUB,  CTIDelayCycle.rid,  1, 1 },
908 //{ LDDSTUBA, CTIDelayCycle.rid,  1, 1 },
909 //{ SWAP,     CTIDelayCycle.rid,  1, 1 },
910 //{ SWAPA,    CTIDelayCycle.rid,  1, 1 },
911 //{ CAS,      CTIDelayCycle.rid,  1, 1 },
912 //{ CASA,     CTIDelayCycle.rid,  1, 1 },
913 //{ CASX,     CTIDelayCycle.rid,  1, 1 },
914 //{ CASXA,    CTIDelayCycle.rid,  1, 1 },
915   
916   //
917   // Signed int loads of less than dword size return data in cycle N1 (not C)
918   // and put all loads in consecutive cycles into delayed load return mode.
919   //
920   { LDSB,    LdReturn.rid,  2, -1 },
921   { LDSB,    LdReturn.rid,  3,  1 },
922   
923   { LDSH,    LdReturn.rid,  2, -1 },
924   { LDSH,    LdReturn.rid,  3,  1 },
925   
926   { LDSW,    LdReturn.rid,  2, -1 },
927   { LDSW,    LdReturn.rid,  3,  1 },
928
929
930 #undef EXPLICIT_BUBBLES_NEEDED
931 #ifdef EXPLICIT_BUBBLES_NEEDED
932   // 
933   // MULScc inserts one bubble.
934   // This means it breaks the current group (captured in UltraSparcSchedInfo)
935   // *and occupies all issue slots for the next cycle
936   // 
937 //{ MULScc,  AllIssueSlots.rid, 2, 2-1 },
938 //{ MULScc,  AllIssueSlots.rid, 2, 2-1 },
939 //{ MULScc,  AllIssueSlots.rid, 2, 2-1 },
940 //{ MULScc,  AllIssueSlots.rid,  2, 2-1 },
941   
942   // 
943   // SMULcc inserts between 4 and 18 bubbles, depending on #leading 0s in rs1.
944   // We just model this with a simple average.
945   // 
946 //{ SMULcc,  AllIssueSlots.rid, 2, ((4+18)/2)-1 },
947 //{ SMULcc,  AllIssueSlots.rid, 2, ((4+18)/2)-1 },
948 //{ SMULcc,  AllIssueSlots.rid, 2, ((4+18)/2)-1 },
949 //{ SMULcc,  AllIssueSlots.rid,  2, ((4+18)/2)-1 },
950   
951   // SMULcc inserts between 4 and 19 bubbles, depending on #leading 0s in rs1.
952 //{ UMULcc,  AllIssueSlots.rid, 2, ((4+19)/2)-1 },
953 //{ UMULcc,  AllIssueSlots.rid, 2, ((4+19)/2)-1 },
954 //{ UMULcc,  AllIssueSlots.rid, 2, ((4+19)/2)-1 },
955 //{ UMULcc,  AllIssueSlots.rid,  2, ((4+19)/2)-1 },
956   
957   // 
958   // MULX inserts between 4 and 34 bubbles, depending on #leading 0s in rs1.
959   // 
960   { MULX,    AllIssueSlots.rid, 2, ((4+34)/2)-1 },
961   { MULX,    AllIssueSlots.rid, 2, ((4+34)/2)-1 },
962   { MULX,    AllIssueSlots.rid, 2, ((4+34)/2)-1 },
963   { MULX,    AllIssueSlots.rid,  2, ((4+34)/2)-1 },
964   
965   // 
966   // SDIVcc inserts 36 bubbles.
967   // 
968 //{ SDIVcc,  AllIssueSlots.rid, 2, 36-1 },
969 //{ SDIVcc,  AllIssueSlots.rid, 2, 36-1 },
970 //{ SDIVcc,  AllIssueSlots.rid, 2, 36-1 },
971 //{ SDIVcc,  AllIssueSlots.rid,  2, 36-1 },
972   
973   // UDIVcc inserts 37 bubbles.
974 //{ UDIVcc,  AllIssueSlots.rid, 2, 37-1 },
975 //{ UDIVcc,  AllIssueSlots.rid, 2, 37-1 },
976 //{ UDIVcc,  AllIssueSlots.rid, 2, 37-1 },
977 //{ UDIVcc,  AllIssueSlots.rid,  2, 37-1 },
978   
979   // 
980   // SDIVX inserts 68 bubbles.
981   // 
982   { SDIVX,   AllIssueSlots.rid, 2, 68-1 },
983   { SDIVX,   AllIssueSlots.rid, 2, 68-1 },
984   { SDIVX,   AllIssueSlots.rid, 2, 68-1 },
985   { SDIVX,   AllIssueSlots.rid,  2, 68-1 },
986   
987   // 
988   // UDIVX inserts 68 bubbles.
989   // 
990   { UDIVX,   AllIssueSlots.rid, 2, 68-1 },
991   { UDIVX,   AllIssueSlots.rid, 2, 68-1 },
992   { UDIVX,   AllIssueSlots.rid, 2, 68-1 },
993   { UDIVX,   AllIssueSlots.rid,  2, 68-1 },
994   
995   // 
996   // WR inserts 4 bubbles.
997   // 
998 //{ WR,     AllIssueSlots.rid, 2, 68-1 },
999 //{ WR,     AllIssueSlots.rid, 2, 68-1 },
1000 //{ WR,     AllIssueSlots.rid, 2, 68-1 },
1001 //{ WR,     AllIssueSlots.rid,  2, 68-1 },
1002   
1003   // 
1004   // WRPR inserts 4 bubbles.
1005   // 
1006 //{ WRPR,   AllIssueSlots.rid, 2, 68-1 },
1007 //{ WRPR,   AllIssueSlots.rid, 2, 68-1 },
1008 //{ WRPR,   AllIssueSlots.rid, 2, 68-1 },
1009 //{ WRPR,   AllIssueSlots.rid,  2, 68-1 },
1010   
1011   // 
1012   // DONE inserts 9 bubbles.
1013   // 
1014 //{ DONE,   AllIssueSlots.rid, 2, 9-1 },
1015 //{ DONE,   AllIssueSlots.rid, 2, 9-1 },
1016 //{ DONE,   AllIssueSlots.rid, 2, 9-1 },
1017 //{ DONE,   AllIssueSlots.rid, 2, 9-1 },
1018   
1019   // 
1020   // RETRY inserts 9 bubbles.
1021   // 
1022 //{ RETRY,   AllIssueSlots.rid, 2, 9-1 },
1023 //{ RETRY,   AllIssueSlots.rid, 2, 9-1 },
1024 //{ RETRY,   AllIssueSlots.rid, 2, 9-1 },
1025 //{ RETRY,   AllIssueSlots.rid,  2, 9-1 },
1026
1027 #endif  /*EXPLICIT_BUBBLES_NEEDED */
1028 };
1029
1030
1031
1032 // Additional delays to be captured in code:
1033 // 1. RDPR from several state registers (page 349)
1034 // 2. RD   from *any* register (page 349)
1035 // 3. Writes to TICK, PSTATE, TL registers and FLUSH{W} instr (page 349)
1036 // 4. Integer store can be in same group as instr producing value to store.
1037 // 5. BICC and BPICC can be in the same group as instr producing CC (pg 350)
1038 // 6. FMOVr cannot be in the same or next group as an IEU instr (pg 351).
1039 // 7. The second instr. of a CTI group inserts 9 bubbles (pg 351)
1040 // 8. WR{PR}, SVAE, SAVED, RESTORE, RESTORED, RETURN, RETRY, and DONE that
1041 //    follow an annulling branch cannot be issued in the same group or in
1042 //    the 3 groups following the branch.
1043 // 9. A predicted annulled load does not stall dependent instructions.
1044 //    Other annulled delay slot instructions *do* stall dependents, so
1045 //    nothing special needs to be done for them during scheduling.
1046 //10. Do not put a load use that may be annulled in the same group as the
1047 //    branch.  The group will stall until the load returns.
1048 //11. Single-prec. FP loads lock 2 registers, for dependency checking.
1049 //
1050 // 
1051 // Additional delays we cannot or will not capture:
1052 // 1. If DCTI is last word of cache line, it is delayed until next line can be
1053 //    fetched.  Also, other DCTI alignment-related delays (pg 352)
1054 // 2. Load-after-store is delayed by 7 extra cycles if load hits in D-Cache.
1055 //    Also, several other store-load and load-store conflicts (pg 358)
1056 // 3. MEMBAR, LD{X}FSR, LDD{A} and a bunch of other load stalls (pg 358)
1057 // 4. There can be at most 8 outstanding buffered store instructions
1058 //     (including some others like MEMBAR, LDSTUB, CAS{AX}, and FLUSH)
1059
1060
1061
1062 //---------------------------------------------------------------------------
1063 // class UltraSparcSchedInfo
1064 // 
1065 // Purpose:
1066 //   Interface to instruction scheduling information for UltraSPARC.
1067 //   The parameter values above are based on UltraSPARC IIi.
1068 //---------------------------------------------------------------------------
1069
1070
1071 class UltraSparcSchedInfo: public MachineSchedInfo {
1072 public:
1073   /*ctor*/         UltraSparcSchedInfo  (const MachineInstrInfo* mii);
1074   /*dtor*/ virtual ~UltraSparcSchedInfo () {}
1075 protected:
1076   virtual void  initializeResources     ();
1077 };
1078
1079
1080 //---------------------------------------------------------------------------
1081 // class UltraSparcMachine 
1082 // 
1083 // Purpose:
1084 //   Primary interface to machine description for the UltraSPARC.
1085 //   Primarily just initializes machine-dependent parameters in
1086 //   class TargetMachine, and creates machine-dependent subclasses
1087 //   for classes such as InstrInfo, SchedInfo and RegInfo. 
1088 //---------------------------------------------------------------------------
1089
1090 class UltraSparc : public TargetMachine {
1091 private:
1092   UltraSparcInstrInfo instrInfo;
1093   UltraSparcSchedInfo schedInfo;
1094   UltraSparcRegInfo   regInfo;
1095 public:
1096   UltraSparc();
1097   virtual ~UltraSparc() {}
1098   
1099   virtual const MachineInstrInfo &getInstrInfo() const { return instrInfo; }
1100   virtual const MachineSchedInfo &getSchedInfo() const { return schedInfo; }
1101   virtual const MachineRegInfo   &getRegInfo()   const { return regInfo; }
1102   
1103   // compileMethod - For the sparc, we do instruction selection, followed by
1104   // delay slot scheduling, then register allocation.
1105   //
1106   virtual bool compileMethod(Method *M);
1107
1108   //
1109   // emitAssembly - Output assembly language code (a .s file) for the specified
1110   // module. The specified module must have been compiled before this may be
1111   // used.
1112   //
1113   virtual void emitAssembly(const Module *M, ostream &OutStr) const;
1114 };
1115
1116
1117 #endif