Reapply r79127. It was fixed by d0k.
[oota-llvm.git] / include / llvm / Target / TargetInstrItineraries.h
1 //===-- llvm/Target/TargetInstrItineraries.h - Scheduling -------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file describes the structures used for instruction itineraries and
11 // stages.  This is used by schedulers to determine instruction stages and
12 // latencies.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_TARGET_TARGETINSTRITINERARIES_H
17 #define LLVM_TARGET_TARGETINSTRITINERARIES_H
18
19 #include <algorithm>
20
21 namespace llvm {
22
23 //===----------------------------------------------------------------------===//
24 /// Instruction stage - These values represent a non-pipelined step in
25 /// the execution of an instruction.  Cycles represents the number of
26 /// discrete time slots needed to complete the stage.  Units represent
27 /// the choice of functional units that can be used to complete the
28 /// stage.  Eg. IntUnit1, IntUnit2. NextCycles indicates how many
29 /// cycles should elapse from the start of this stage to the start of
30 /// the next stage in the itinerary. A value of -1 indicates that the
31 /// next stage should start immediately after the current one.
32 /// For example:
33 ///
34 ///   { 1, x, -1 }
35 ///      indicates that the stage occupies FU x for 1 cycle and that
36 ///      the next stage starts immediately after this one.
37 ///
38 ///   { 2, x|y, 1 }
39 ///      indicates that the stage occupies either FU x or FU y for 2
40 ///      consecuative cycles and that the next stage starts one cycle
41 ///      after this stage starts. That is, the stage requirements
42 ///      overlap in time.
43 ///
44 ///   { 1, x, 0 }
45 ///      indicates that the stage occupies FU x for 1 cycle and that
46 ///      the next stage starts in this same cycle. This can be used to
47 ///      indicate that the instruction requires multiple stages at the
48 ///      same time.
49 ///
50 struct InstrStage {
51   unsigned Cycles_;  ///< Length of stage in machine cycles
52   unsigned Units_;   ///< Choice of functional units
53   int NextCycles_;   ///< Number of machine cycles to next stage 
54
55   /// getCycles - returns the number of cycles the stage is occupied
56   unsigned getCycles() const {
57     return Cycles_;
58   }
59
60   /// getUnits - returns the choice of FUs
61   unsigned getUnits() const {
62     return Units_;
63   }
64
65   /// getNextCycles - returns the number of cycles from the start of
66   /// this stage to the start of the next stage in the itinerary
67   unsigned getNextCycles() const {
68     return (NextCycles_ >= 0) ? (unsigned)NextCycles_ : Cycles_;
69   }
70 };
71
72
73 //===----------------------------------------------------------------------===//
74 /// Instruction itinerary - An itinerary represents a sequential series of steps
75 /// required to complete an instruction.  Itineraries are represented as
76 /// sequences of instruction stages.
77 ///
78 struct InstrItinerary {
79   unsigned First;    ///< Index of first stage in itinerary
80   unsigned Last;     ///< Index of last + 1 stage in itinerary
81 };
82
83
84
85 //===----------------------------------------------------------------------===//
86 /// Instruction itinerary Data - Itinerary data supplied by a subtarget to be
87 /// used by a target.
88 ///
89 struct InstrItineraryData {
90   const InstrStage     *Stages;         ///< Array of stages selected
91   const InstrItinerary *Itineratries;   ///< Array of itineraries selected
92
93   /// Ctors.
94   ///
95   InstrItineraryData() : Stages(0), Itineratries(0) {}
96   InstrItineraryData(const InstrStage *S, const InstrItinerary *I)
97     : Stages(S), Itineratries(I) {}
98   
99   /// isEmpty - Returns true if there are no itineraries.
100   ///
101   bool isEmpty() const { return Itineratries == 0; }
102   
103   /// begin - Return the first stage of the itinerary.
104   /// 
105   const InstrStage *begin(unsigned ItinClassIndx) const {
106     unsigned StageIdx = Itineratries[ItinClassIndx].First;
107     return Stages + StageIdx;
108   }
109
110   /// end - Return the last+1 stage of the itinerary.
111   /// 
112   const InstrStage *end(unsigned ItinClassIndx) const {
113     unsigned StageIdx = Itineratries[ItinClassIndx].Last;
114     return Stages + StageIdx;
115   }
116
117   /// getLatency - Return the scheduling latency of the given class.  A
118   /// simple latency value for an instruction is an over-simplification
119   /// for some architectures, but it's a reasonable first approximation.
120   ///
121   unsigned getLatency(unsigned ItinClassIndx) const {
122     // If the target doesn't provide latency information, use a simple
123     // non-zero default value for all instructions.
124     if (isEmpty())
125       return 1;
126
127     // Caclulate the maximum completion time for any stage. The
128     // assumption is that all inputs are consumed at the start of the
129     // first stage and that all outputs are produced at the end of the
130     // latest completing last stage.
131     unsigned Latency = 0, StartCycle = 0;
132     for (const InstrStage *IS = begin(ItinClassIndx), *E = end(ItinClassIndx);
133          IS != E; ++IS) {
134       Latency = std::max(Latency, StartCycle + IS->getCycles());
135       StartCycle += IS->getNextCycles();
136     }
137
138     return Latency;
139   }
140 };
141
142
143 } // End llvm namespace
144
145 #endif