Coverage Report

Created: 2019-02-21 13:17

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/MC/MCInstrItineraries.h
Line
Count
Source (jump to first uncovered line)
1
//===- llvm/MC/MCInstrItineraries.h - Scheduling ----------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file describes the structures used for instruction
10
// itineraries, stages, and operand reads/writes.  This is used by
11
// schedulers to determine instruction stages and latencies.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#ifndef LLVM_MC_MCINSTRITINERARIES_H
16
#define LLVM_MC_MCINSTRITINERARIES_H
17
18
#include "llvm/MC/MCSchedule.h"
19
#include <algorithm>
20
21
namespace llvm {
22
23
//===----------------------------------------------------------------------===//
24
/// 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
///      consecutive 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
/// FU reservation can be of two different kinds:
51
///  - FUs which instruction actually requires
52
///  - FUs which instruction just reserves. Reserved unit is not available for
53
///    execution of other instruction. However, several instructions can reserve
54
///    the same unit several times.
55
/// Such two types of units reservation is used to model instruction domain
56
/// change stalls, FUs using the same resource (e.g. same register file), etc.
57
58
struct InstrStage {
59
  enum ReservationKinds {
60
    Required = 0,
61
    Reserved = 1
62
  };
63
64
  unsigned Cycles_;  ///< Length of stage in machine cycles
65
  unsigned Units_;   ///< Choice of functional units
66
  int NextCycles_;   ///< Number of machine cycles to next stage
67
  ReservationKinds Kind_; ///< Kind of the FU reservation
68
69
  /// Returns the number of cycles the stage is occupied.
70
193M
  unsigned getCycles() const {
71
193M
    return Cycles_;
72
193M
  }
73
74
  /// Returns the choice of FUs.
75
4.76M
  unsigned getUnits() const {
76
4.76M
    return Units_;
77
4.76M
  }
78
79
5.29M
  ReservationKinds getReservationKind() const {
80
5.29M
    return Kind_;
81
5.29M
  }
82
83
  /// Returns the number of cycles from the start of this stage to the
84
  /// start of the next stage in the itinerary
85
189M
  unsigned getNextCycles() const {
86
189M
    return (NextCycles_ >= 0) ? 
(unsigned)NextCycles_91.1M
:
Cycles_98.1M
;
87
189M
  }
88
};
89
90
//===----------------------------------------------------------------------===//
91
/// An itinerary represents the scheduling information for an instruction.
92
/// This includes a set of stages occupied by the instruction and the pipeline
93
/// cycle in which operands are read and written.
94
///
95
struct InstrItinerary {
96
  int16_t  NumMicroOps;        ///< # of micro-ops, -1 means it's variable
97
  uint16_t FirstStage;         ///< Index of first stage in itinerary
98
  uint16_t LastStage;          ///< Index of last + 1 stage in itinerary
99
  uint16_t FirstOperandCycle;  ///< Index of first operand rd/wr
100
  uint16_t LastOperandCycle;   ///< Index of last + 1 operand rd/wr
101
};
102
103
//===----------------------------------------------------------------------===//
104
/// Itinerary data supplied by a subtarget to be used by a target.
105
///
106
class InstrItineraryData {
107
public:
108
  MCSchedModel SchedModel =
109
      MCSchedModel::GetDefaultSchedModel(); ///< Basic machine properties.
110
  const InstrStage *Stages = nullptr;       ///< Array of stages selected
111
  const unsigned *OperandCycles = nullptr; ///< Array of operand cycles selected
112
  const unsigned *Forwardings = nullptr; ///< Array of pipeline forwarding paths
113
  const InstrItinerary *Itineraries =
114
      nullptr; ///< Array of itineraries selected
115
116
699k
  InstrItineraryData() = default;
117
  InstrItineraryData(const MCSchedModel &SM, const InstrStage *S,
118
                     const unsigned *OS, const unsigned *F)
119
    : SchedModel(SM), Stages(S), OperandCycles(OS), Forwardings(F),
120
3.33M
      Itineraries(SchedModel.InstrItineraries) {}
121
122
  /// Returns true if there are no itineraries.
123
137M
  bool isEmpty() const { return Itineraries == nullptr; }
124
125
  /// Returns true if the index is for the end marker itinerary.
126
101M
  bool isEndMarker(unsigned ItinClassIndx) const {
127
101M
    return ((Itineraries[ItinClassIndx].FirstStage == UINT16_MAX) &&
128
101M
            
(Itineraries[ItinClassIndx].LastStage == UINT16_MAX)128k
);
129
101M
  }
130
131
  /// Return the first stage of the itinerary.
132
105M
  const InstrStage *beginStage(unsigned ItinClassIndx) const {
133
105M
    unsigned StageIdx = Itineraries[ItinClassIndx].FirstStage;
134
105M
    return Stages + StageIdx;
135
105M
  }
136
137
  /// Return the last+1 stage of the itinerary.
138
105M
  const InstrStage *endStage(unsigned ItinClassIndx) const {
139
105M
    unsigned StageIdx = Itineraries[ItinClassIndx].LastStage;
140
105M
    return Stages + StageIdx;
141
105M
  }
142
143
  /// Return the total stage latency of the given class.  The latency is
144
  /// the maximum completion time for any stage in the itinerary.  If no stages
145
  /// exist, it defaults to one cycle.
146
1.34M
  unsigned getStageLatency(unsigned ItinClassIndx) const {
147
1.34M
    // If the target doesn't provide itinerary information, use a simple
148
1.34M
    // non-zero default value for all instructions.
149
1.34M
    if (isEmpty())
150
26.7k
      return 1;
151
1.32M
152
1.32M
    // Calculate the maximum completion time for any stage.
153
1.32M
    unsigned Latency = 0, StartCycle = 0;
154
1.32M
    for (const InstrStage *IS = beginStage(ItinClassIndx),
155
2.94M
           *E = endStage(ItinClassIndx); IS != E; 
++IS1.62M
) {
156
1.62M
      Latency = std::max(Latency, StartCycle + IS->getCycles());
157
1.62M
      StartCycle += IS->getNextCycles();
158
1.62M
    }
159
1.32M
    return Latency;
160
1.32M
  }
161
162
  /// Return the cycle for the given class and operand.  Return -1 if no
163
  /// cycle is specified for the operand.
164
1.74M
  int getOperandCycle(unsigned ItinClassIndx, unsigned OperandIdx) const {
165
1.74M
    if (isEmpty())
166
1
      return -1;
167
1.74M
168
1.74M
    unsigned FirstIdx = Itineraries[ItinClassIndx].FirstOperandCycle;
169
1.74M
    unsigned LastIdx = Itineraries[ItinClassIndx].LastOperandCycle;
170
1.74M
    if ((FirstIdx + OperandIdx) >= LastIdx)
171
408k
      return -1;
172
1.33M
173
1.33M
    return (int)OperandCycles[FirstIdx + OperandIdx];
174
1.33M
  }
175
176
  /// Return true if there is a pipeline forwarding between instructions
177
  /// of itinerary classes DefClass and UseClasses so that value produced by an
178
  /// instruction of itinerary class DefClass, operand index DefIdx can be
179
  /// bypassed when it's read by an instruction of itinerary class UseClass,
180
  /// operand index UseIdx.
181
  bool hasPipelineForwarding(unsigned DefClass, unsigned DefIdx,
182
302k
                             unsigned UseClass, unsigned UseIdx) const {
183
302k
    unsigned FirstDefIdx = Itineraries[DefClass].FirstOperandCycle;
184
302k
    unsigned LastDefIdx = Itineraries[DefClass].LastOperandCycle;
185
302k
    if ((FirstDefIdx + DefIdx) >= LastDefIdx)
186
24
      return false;
187
302k
    if (Forwardings[FirstDefIdx + DefIdx] == 0)
188
243k
      return false;
189
59.2k
190
59.2k
    unsigned FirstUseIdx = Itineraries[UseClass].FirstOperandCycle;
191
59.2k
    unsigned LastUseIdx = Itineraries[UseClass].LastOperandCycle;
192
59.2k
    if ((FirstUseIdx + UseIdx) >= LastUseIdx)
193
0
      return false;
194
59.2k
195
59.2k
    return Forwardings[FirstDefIdx + DefIdx] ==
196
59.2k
      Forwardings[FirstUseIdx + UseIdx];
197
59.2k
  }
198
199
  /// Compute and return the use operand latency of a given itinerary
200
  /// class and operand index if the value is produced by an instruction of the
201
  /// specified itinerary class and def operand index.
202
  int getOperandLatency(unsigned DefClass, unsigned DefIdx,
203
648k
                        unsigned UseClass, unsigned UseIdx) const {
204
648k
    if (isEmpty())
205
0
      return -1;
206
648k
207
648k
    int DefCycle = getOperandCycle(DefClass, DefIdx);
208
648k
    if (DefCycle == -1)
209
201k
      return -1;
210
446k
211
446k
    int UseCycle = getOperandCycle(UseClass, UseIdx);
212
446k
    if (UseCycle == -1)
213
43.2k
      return -1;
214
403k
215
403k
    UseCycle = DefCycle - UseCycle + 1;
216
403k
    if (UseCycle > 0 &&
217
403k
        
hasPipelineForwarding(DefClass, DefIdx, UseClass, UseIdx)301k
)
218
58.9k
      // FIXME: This assumes one cycle benefit for every pipeline forwarding.
219
58.9k
      --UseCycle;
220
403k
    return UseCycle;
221
403k
  }
222
223
  /// Return the number of micro-ops that the given class decodes to.
224
  /// Return -1 for classes that require dynamic lookup via TargetInstrInfo.
225
1.10M
  int getNumMicroOps(unsigned ItinClassIndx) const {
226
1.10M
    if (isEmpty())
227
0
      return 1;
228
1.10M
    return Itineraries[ItinClassIndx].NumMicroOps;
229
1.10M
  }
230
};
231
232
} // end namespace llvm
233
234
#endif // LLVM_MC_MCINSTRITINERARIES_H