40
40
#ifndef LLVM_CODEGEN_MACHINEPIPELINER_H
41
41
#define LLVM_CODEGEN_MACHINEPIPELINER_H
42
42
43
+ #include " llvm/ADT/STLExtras.h"
43
44
#include " llvm/ADT/SetVector.h"
45
+ #include " llvm/Analysis/AliasAnalysis.h"
44
46
#include " llvm/CodeGen/DFAPacketizer.h"
45
47
#include " llvm/CodeGen/MachineDominators.h"
46
48
#include " llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
@@ -59,6 +61,8 @@ namespace llvm {
59
61
class AAResults ;
60
62
class NodeSet ;
61
63
class SMSchedule ;
64
+ class DDGEdge ;
65
+ class DataDependenceGraph ;
62
66
63
67
extern cl::opt<bool > SwpEnableCopyToPhi;
64
68
extern cl::opt<int > SwpForceIssueWidth;
@@ -114,10 +118,124 @@ class MachinePipeliner : public MachineFunctionPass {
114
118
bool useWindowScheduler (bool Changed);
115
119
};
116
120
121
+ // / Represents a dependnece between two instruction.
122
+ class DDGEdge {
123
+ SUnit *Dst = nullptr ;
124
+ SDep Pred;
125
+ unsigned Distance = 0 ;
126
+
127
+ public:
128
+ // / Creates an edge corresponding to an edge represented by \p PredOrSucc and
129
+ // / \p Dep in the original DAG. This pair has no information about the
130
+ // / direction of the edge, so we need to pass an additional argument \p
131
+ // / IsSucc.
132
+ DDGEdge (SUnit *PredOrSucc, const SDep &Dep, bool IsSucc)
133
+ : Dst(PredOrSucc), Pred(Dep), Distance(0u ) {
134
+ SUnit *Src = Dep.getSUnit ();
135
+
136
+ if (IsSucc) {
137
+ std::swap (Src, Dst);
138
+ Pred.setSUnit (Src);
139
+ }
140
+
141
+ // An anti-dependence to PHI means loop-carried dependence.
142
+ if (Pred.getKind () == SDep::Anti && Src->getInstr ()->isPHI ()) {
143
+ Distance = 1 ;
144
+ std::swap (Src, Dst);
145
+ auto Reg = Pred.getReg ();
146
+ Pred = SDep (Src, SDep::Kind::Data, Reg);
147
+ }
148
+ }
149
+
150
+ // / Returns the SUnit from which the edge comes (source node).
151
+ SUnit *getSrc () const { return Pred.getSUnit (); }
152
+
153
+ // / Returns the SUnit to which the edge points (destination node).
154
+ SUnit *getDst () const { return Dst; }
155
+
156
+ // / Returns the latency value for the edge.
157
+ unsigned getLatency () const { return Pred.getLatency (); }
158
+
159
+ // / Sets the latency for the edge.
160
+ void setLatency (unsigned Latency) { Pred.setLatency (Latency); }
161
+
162
+ // / Returns the distance value for the edge.
163
+ unsigned getDistance () const { return Distance; }
164
+
165
+ // / Sets the distance value for the edge.
166
+ void setDistance (unsigned D) { Distance = D; }
167
+
168
+ // / Returns the register associated with the edge.
169
+ Register getReg () const { return Pred.getReg (); }
170
+
171
+ // / Returns true if the edge represents anti dependence.
172
+ bool isAntiDep () const { return Pred.getKind () == SDep::Kind::Anti; }
173
+
174
+ // / Returns true if the edge represents output dependence.
175
+ bool isOutputDep () const { return Pred.getKind () == SDep::Kind::Output; }
176
+
177
+ // / Returns true if the edge represents a dependence that is not data, anti or
178
+ // / output dependence.
179
+ bool isOrderDep () const { return Pred.getKind () == SDep::Kind::Order; }
180
+
181
+ // / Returns true if the edge represents unknown scheduling barrier.
182
+ bool isBarrier () const { return Pred.isBarrier (); }
183
+
184
+ // / Returns true if the edge represents an artificial dependence.
185
+ bool isArtificial () const { return Pred.isArtificial (); }
186
+
187
+ // / Tests if this is a Data dependence that is associated with a register.
188
+ bool isAssignedRegDep () const { return Pred.isAssignedRegDep (); }
189
+
190
+ // / Returns true for DDG nodes that we ignore when computing the cost
191
+ // / functions. We ignore the back-edge recurrence in order to avoid unbounded
192
+ // / recursion in the calculation of the ASAP, ALAP, etc functions.
193
+ bool ignoreDependence (bool IgnoreAnti) const ;
194
+ };
195
+
196
+ // / Represents dependencies between instructions. This class is a wrapper of
197
+ // / `SUnits` and its dependencies to manipulate back-edges in a natural way.
198
+ // / Currently it only supports back-edges via PHI, which are expressed as
199
+ // / anti-dependencies in the original DAG.
200
+ // / FIXME: Support any other loop-carried dependencies
201
+ class DataDependenceGraph {
202
+ using EdgesType = SmallVector<DDGEdge, 4 >;
203
+
204
+ struct DDGEdges {
205
+ EdgesType Preds;
206
+ EdgesType Succs;
207
+ };
208
+
209
+ void initEdges (SUnit *SU);
210
+
211
+ SUnit *EntrySU;
212
+ SUnit *ExitSU;
213
+
214
+ std::vector<DDGEdges> EdgesVec;
215
+ DDGEdges EntrySUEdges;
216
+ DDGEdges ExitSUEdges;
217
+
218
+ void addEdge (SUnit *SU, const DDGEdge &Edge);
219
+
220
+ DDGEdges &getEdges (const SUnit *SU);
221
+ const DDGEdges &getEdges (const SUnit *SU) const ;
222
+
223
+ public:
224
+ DataDependenceGraph (std::vector<SUnit> &SUnits, SUnit *EntrySU,
225
+ SUnit *ExitSU);
226
+
227
+ const EdgesType &getInEdges (const SUnit *SU) const ;
228
+
229
+ const EdgesType &getOutEdges (const SUnit *SU) const ;
230
+ };
231
+
117
232
// / This class builds the dependence graph for the instructions in a loop,
118
233
// / and attempts to schedule the instructions using the SMS algorithm.
119
234
class SwingSchedulerDAG : public ScheduleDAGInstrs {
120
235
MachinePipeliner &Pass;
236
+
237
+ std::unique_ptr<DataDependenceGraph> DDG;
238
+
121
239
// / The minimum initiation interval between iterations for this schedule.
122
240
unsigned MII = 0 ;
123
241
// / The maximum initiation interval between iterations for this schedule.
@@ -130,7 +248,7 @@ class SwingSchedulerDAG : public ScheduleDAGInstrs {
130
248
unsigned II_setByPragma = 0 ;
131
249
TargetInstrInfo::PipelinerLoopInfo *LoopPipelinerInfo = nullptr ;
132
250
133
- // / A toplogical ordering of the SUnits, which is needed for changing
251
+ // / A topological ordering of the SUnits, which is needed for changing
134
252
// / dependences and iterating over the SUnits.
135
253
ScheduleDAGTopologicalSort Topo;
136
254
@@ -252,27 +370,7 @@ class SwingSchedulerDAG : public ScheduleDAGInstrs {
252
370
return ScheduleInfo[Node->NodeNum ].ZeroLatencyHeight ;
253
371
}
254
372
255
- // / Return true if the dependence is a back-edge in the data dependence graph.
256
- // / Since the DAG doesn't contain cycles, we represent a cycle in the graph
257
- // / using an anti dependence from a Phi to an instruction.
258
- bool isBackedge (SUnit *Source, const SDep &Dep) {
259
- if (Dep.getKind () != SDep::Anti)
260
- return false ;
261
- return Source->getInstr ()->isPHI () || Dep.getSUnit ()->getInstr ()->isPHI ();
262
- }
263
-
264
- bool isLoopCarriedDep (SUnit *Source, const SDep &Dep,
265
- bool isSucc = true ) const ;
266
-
267
- // / The distance function, which indicates that operation V of iteration I
268
- // / depends on operations U of iteration I-distance.
269
- unsigned getDistance (SUnit *U, SUnit *V, const SDep &Dep) {
270
- // Instructions that feed a Phi have a distance of 1. Computing larger
271
- // values for arrays requires data dependence information.
272
- if (V->getInstr ()->isPHI () && Dep.getKind () == SDep::Anti)
273
- return 1 ;
274
- return 0 ;
275
- }
373
+ bool isLoopCarriedDep (const DDGEdge &Edge) const ;
276
374
277
375
void applyInstrChange (MachineInstr *MI, SMSchedule &Schedule);
278
376
@@ -294,6 +392,8 @@ class SwingSchedulerDAG : public ScheduleDAGInstrs {
294
392
295
393
static bool classof (const ScheduleDAGInstrs *DAG) { return true ; }
296
394
395
+ const DataDependenceGraph *getDDG () const { return DDG.get (); }
396
+
297
397
private:
298
398
void addLoopCarriedDependences (AAResults *AA);
299
399
void updatePhiDependences ();
@@ -357,15 +457,16 @@ class NodeSet {
357
457
//
358
458
// Hold a map from each SUnit in the circle to the maximum distance from the
359
459
// source node by only considering the nodes.
460
+ const DataDependenceGraph *DDG = DAG->getDDG ();
360
461
DenseMap<SUnit *, unsigned > SUnitToDistance;
361
462
for (auto *Node : Nodes)
362
463
SUnitToDistance[Node] = 0 ;
363
464
364
465
for (unsigned I = 1 , E = Nodes.size (); I <= E; ++I) {
365
466
SUnit *U = Nodes[I - 1 ];
366
467
SUnit *V = Nodes[I % Nodes.size ()];
367
- for (const SDep &Succ : U-> Succs ) {
368
- SUnit *SuccSUnit = Succ.getSUnit ();
468
+ for (const DDGEdge &Succ : DDG-> getOutEdges (U) ) {
469
+ SUnit *SuccSUnit = Succ.getDst ();
369
470
if (V != SuccSUnit)
370
471
continue ;
371
472
if (SUnitToDistance[U] + Succ.getLatency () > SUnitToDistance[V]) {
@@ -377,13 +478,13 @@ class NodeSet {
377
478
SUnit *FirstNode = Nodes[0 ];
378
479
SUnit *LastNode = Nodes[Nodes.size () - 1 ];
379
480
380
- for (auto &PI : LastNode-> Preds ) {
481
+ for (auto &PI : DDG-> getInEdges (LastNode) ) {
381
482
// If we have an order dep that is potentially loop carried then a
382
483
// back-edge exists between the last node and the first node that isn't
383
484
// modeled in the DAG. Handle it manually by adding 1 to the distance of
384
485
// the last node.
385
- if (PI.getSUnit () != FirstNode || PI.getKind () != SDep::Order ||
386
- !DAG->isLoopCarriedDep (LastNode, PI, false ))
486
+ if (PI.getSrc () != FirstNode || ! PI.isOrderDep () ||
487
+ !DAG->isLoopCarriedDep (PI ))
387
488
continue ;
388
489
SUnitToDistance[FirstNode] =
389
490
std::max (SUnitToDistance[FirstNode], SUnitToDistance[LastNode] + 1 );
@@ -627,11 +728,11 @@ class SMSchedule {
627
728
628
729
// / Return the cycle of the earliest scheduled instruction in the dependence
629
730
// / chain.
630
- int earliestCycleInChain (const SDep &Dep);
731
+ int earliestCycleInChain (const DDGEdge &Dep, const DataDependenceGraph *DDG );
631
732
632
733
// / Return the cycle of the latest scheduled instruction in the dependence
633
734
// / chain.
634
- int latestCycleInChain (const SDep &Dep);
735
+ int latestCycleInChain (const DDGEdge &Dep, const DataDependenceGraph *DDG );
635
736
636
737
void computeStart (SUnit *SU, int *MaxEarlyStart, int *MinLateStart, int II,
637
738
SwingSchedulerDAG *DAG);
@@ -693,8 +794,9 @@ class SMSchedule {
693
794
bool isLoopCarriedDefOfUse (const SwingSchedulerDAG *SSD, MachineInstr *Def,
694
795
MachineOperand &MO) const ;
695
796
696
- bool onlyHasLoopCarriedOutputOrOrderPreds (SUnit *SU,
697
- SwingSchedulerDAG *DAG) const ;
797
+ bool
798
+ onlyHasLoopCarriedOutputOrOrderPreds (SUnit *SU,
799
+ const DataDependenceGraph *DDG) const ;
698
800
void print (raw_ostream &os) const ;
699
801
void dump () const ;
700
802
};
0 commit comments