1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
/* -*-C++-*-
****************************************************************************
* File:
* SchedGraph.h
*
* Purpose:
* Scheduling graph based on SSA graph plus extra dependence edges
* capturing dependences due to machine resources (machine registers,
* CC registers, and any others).
*
* Strategy:
* This graph tries to leverage the SSA graph as much as possible,
* but captures the extra dependences through a common interface.
*
* History:
* 7/20/01 - Vikram Adve - Created
***************************************************************************/
#ifndef LLVM_CODEGEN_SCHEDGRAPH_H
#define LLVM_CODEGEN_SCHEDGRAPH_H
#include "llvm/Support/NonCopyable.h"
#include "llvm/Support/HashExtras.h"
#include "llvm/Support/GraphTraits.h"
#include "llvm/Target/MachineInstrInfo.h"
#include "llvm/CodeGen/MachineInstr.h"
#include <hash_map>
class Value;
class Instruction;
class TerminatorInst;
class BasicBlock;
class Method;
class TargetMachine;
class SchedGraphEdge;
class SchedGraphNode;
class SchedGraph;
class RegToRefVecMap;
class ValueToDefVecMap;
class RefVec;
class MachineInstr;
class MachineCodeForBasicBlock;
/******************** Exported Data Types and Constants ********************/
typedef int ResourceId;
const ResourceId InvalidRID = -1;
const ResourceId MachineCCRegsRID = -2; // use +ve numbers for actual regs
const ResourceId MachineIntRegsRID = -3; // use +ve numbers for actual regs
const ResourceId MachineFPRegsRID = -4; // use +ve numbers for actual regs
//*********************** Public Class Declarations ************************/
class SchedGraphEdge: public NonCopyable {
public:
enum SchedGraphEdgeDepType {
CtrlDep, MemoryDep, ValueDep, MachineRegister, MachineResource
};
enum DataDepOrderType {
TrueDep = 0x1, AntiDep=0x2, OutputDep=0x4, NonDataDep=0x8
};
protected:
SchedGraphNode* src;
SchedGraphNode* sink;
SchedGraphEdgeDepType depType;
unsigned int depOrderType;
int minDelay; // cached latency (assumes fixed target arch)
union {
const Value* val;
int machineRegNum;
ResourceId resourceId;
};
public:
// For all constructors, if minDelay is unspecified, minDelay is
// set to _src->getLatency().
// constructor for CtrlDep or MemoryDep edges, selected by 3rd argument
/*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
SchedGraphNode* _sink,
SchedGraphEdgeDepType _depType,
unsigned int _depOrderType,
int _minDelay = -1);
// constructor for explicit value dependence (may be true/anti/output)
/*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
SchedGraphNode* _sink,
const Value* _val,
unsigned int _depOrderType,
int _minDelay = -1);
// constructor for machine register dependence
/*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
SchedGraphNode* _sink,
unsigned int _regNum,
unsigned int _depOrderType,
int _minDelay = -1);
// constructor for any other machine resource dependences.
// DataDepOrderType is always NonDataDep. It it not an argument to
// avoid overloading ambiguity with previous constructor.
/*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
SchedGraphNode* _sink,
ResourceId _resourceId,
int _minDelay = -1);
/*dtor*/ ~SchedGraphEdge();
SchedGraphNode* getSrc () const { return src; }
SchedGraphNode* getSink () const { return sink; }
int getMinDelay () const { return minDelay; }
SchedGraphEdgeDepType getDepType () const { return depType; }
const Value* getValue () const {
assert(depType == ValueDep); return val;
}
int getMachineReg () const {
assert(depType == MachineRegister); return machineRegNum;
}
int getResourceId () const {
assert(depType == MachineResource); return resourceId;
}
public:
//
// Debugging support
//
friend ostream& operator<<(ostream& os, const SchedGraphEdge& edge);
void dump (int indent=0) const;
private:
// disable default ctor
/*ctor*/ SchedGraphEdge(); // DO NOT IMPLEMENT
};
class SchedGraphNode: public NonCopyable {
private:
unsigned int nodeId;
const BasicBlock* bb;
const MachineInstr* minstr;
vector<SchedGraphEdge*> inEdges;
vector<SchedGraphEdge*> outEdges;
int origIndexInBB; // original position of machine instr in BB
int latency;
public:
typedef vector<SchedGraphEdge*>:: iterator iterator;
typedef vector<SchedGraphEdge*>::const_iterator const_iterator;
typedef vector<SchedGraphEdge*>:: reverse_iterator reverse_iterator;
typedef vector<SchedGraphEdge*>::const_reverse_iterator const_reverse_iterator;
public:
//
// Accessor methods
//
unsigned int getNodeId () const { return nodeId; }
const MachineInstr* getMachineInstr () const { return minstr; }
const MachineOpCode getOpCode () const { return minstr->getOpCode();}
int getLatency () const { return latency; }
unsigned int getNumInEdges () const { return inEdges.size(); }
unsigned int getNumOutEdges () const { return outEdges.size(); }
bool isDummyNode () const { return (minstr == NULL); }
const BasicBlock* getBB () const { return bb; }
int getOrigIndexInBB() const { return origIndexInBB; }
//
// Iterators
//
iterator beginInEdges () { return inEdges.begin(); }
iterator endInEdges () { return inEdges.end(); }
iterator beginOutEdges () { return outEdges.begin(); }
iterator endOutEdges () { return outEdges.end(); }
const_iterator beginInEdges () const { return inEdges.begin(); }
const_iterator endInEdges () const { return inEdges.end(); }
const_iterator beginOutEdges () const { return outEdges.begin(); }
const_iterator endOutEdges () const { return outEdges.end(); }
public:
//
// Debugging support
//
friend ostream& operator<<(ostream& os, const SchedGraphNode& node);
void dump (int indent=0) const;
private:
friend class SchedGraph; // give access for ctor and dtor
friend class SchedGraphEdge; // give access for adding edges
void addInEdge (SchedGraphEdge* edge);
void addOutEdge (SchedGraphEdge* edge);
void removeInEdge (const SchedGraphEdge* edge);
void removeOutEdge (const SchedGraphEdge* edge);
// disable default constructor and provide a ctor for single-block graphs
/*ctor*/ SchedGraphNode(); // DO NOT IMPLEMENT
/*ctor*/ SchedGraphNode (unsigned int _nodeId,
const BasicBlock* _bb,
const MachineInstr* _minstr,
int indexInBB,
const TargetMachine& _target);
/*dtor*/ ~SchedGraphNode ();
};
class SchedGraph :
public NonCopyable,
private hash_map<const MachineInstr*, SchedGraphNode*>
{
private:
vector<const BasicBlock*> bbVec; // basic blocks included in the graph
SchedGraphNode* graphRoot; // the root and leaf are not inserted
SchedGraphNode* graphLeaf; // in the hash_map (see getNumNodes())
public:
typedef hash_map<const MachineInstr*, SchedGraphNode*>::iterator iterator;
typedef hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator const_iterator;
public:
//
// Accessor methods
//
const vector<const BasicBlock*>& getBasicBlocks() const { return bbVec; }
const unsigned int getNumNodes() const { return size()+2; }
SchedGraphNode* getRoot() const { return graphRoot; }
SchedGraphNode* getLeaf() const { return graphLeaf; }
SchedGraphNode* getGraphNodeForInstr(const MachineInstr* minstr) const {
const_iterator onePair = this->find(minstr);
return (onePair != this->end())? (*onePair).second : NULL;
}
//
// Delete nodes or edges from the graph.
//
void eraseNode (SchedGraphNode* node);
void eraseIncomingEdges (SchedGraphNode* node,
bool addDummyEdges = true);
void eraseOutgoingEdges (SchedGraphNode* node,
bool addDummyEdges = true);
void eraseIncidentEdges (SchedGraphNode* node,
bool addDummyEdges = true);
//
// Unordered iterators.
// Return values is pair<const MachineIntr*,SchedGraphNode*>.
//
iterator begin() {
return hash_map<const MachineInstr*, SchedGraphNode*>::begin();
}
iterator end() {
return hash_map<const MachineInstr*, SchedGraphNode*>::end();
}
const_iterator begin() const {
return hash_map<const MachineInstr*, SchedGraphNode*>::begin();
}
const_iterator end() const {
return hash_map<const MachineInstr*, SchedGraphNode*>::end();
}
//
// Ordered iterators.
// Return values is pair<const MachineIntr*,SchedGraphNode*>.
//
// void postord_init();
// postorder_iterator postord_begin();
// postorder_iterator postord_end();
// const_postorder_iterator postord_begin() const;
// const_postorder_iterator postord_end() const;
//
// Debugging support
//
void dump () const;
private:
friend class SchedGraphSet; // give access to ctor
// disable default constructor and provide a ctor for single-block graphs
/*ctor*/ SchedGraph (); // DO NOT IMPLEMENT
/*ctor*/ SchedGraph (const BasicBlock* bb,
const TargetMachine& target);
/*dtor*/ ~SchedGraph ();
inline void noteGraphNodeForInstr (const MachineInstr* minstr,
SchedGraphNode* node)
{
assert((*this)[minstr] == NULL);
(*this)[minstr] = node;
}
//
// Graph builder
//
void buildGraph (const TargetMachine& target);
void buildNodesforBB (const TargetMachine& target,
const BasicBlock* bb,
vector<SchedGraphNode*>& memNodeVec,
RegToRefVecMap& regToRefVecMap,
ValueToDefVecMap& valueToDefVecMap);
void findDefUseInfoAtInstr (const TargetMachine& target,
SchedGraphNode* node,
vector<SchedGraphNode*>& memNodeVec,
RegToRefVecMap& regToRefVecMap,
ValueToDefVecMap& valueToDefVecMap);
void addEdgesForInstruction(const MachineInstr& minstr,
const ValueToDefVecMap& valueToDefVecMap,
const TargetMachine& target);
void addCDEdges (const TerminatorInst* term,
const TargetMachine& target);
void addMemEdges (const vector<SchedGraphNode*>& memNodeVec,
const TargetMachine& target);
void addCallCCEdges (const vector<SchedGraphNode*>& memNodeVec,
MachineCodeForBasicBlock& bbMvec,
const TargetMachine& target);
void addMachineRegEdges (RegToRefVecMap& regToRefVecMap,
const TargetMachine& target);
void addEdgesForValue (SchedGraphNode* refNode,
const RefVec& defVec,
const Value* defValue,
bool refNodeIsDef,
const TargetMachine& target);
void addDummyEdges ();
};
class SchedGraphSet :
public NonCopyable,
private hash_map<const BasicBlock*, SchedGraph*>
{
private:
const Method* method;
public:
typedef hash_map<const BasicBlock*, SchedGraph*>::iterator iterator;
typedef hash_map<const BasicBlock*, SchedGraph*>::const_iterator const_iterator;
public:
/*ctor*/ SchedGraphSet (const Method* _method,
const TargetMachine& target);
/*dtor*/ ~SchedGraphSet ();
//
// Accessors
//
SchedGraph* getGraphForBasicBlock (const BasicBlock* bb) const {
const_iterator onePair = this->find(bb);
return (onePair != this->end())? (*onePair).second : NULL;
}
//
// Iterators
//
iterator begin() {
return hash_map<const BasicBlock*, SchedGraph*>::begin();
}
iterator end() {
return hash_map<const BasicBlock*, SchedGraph*>::end();
}
const_iterator begin() const {
return hash_map<const BasicBlock*, SchedGraph*>::begin();
}
const_iterator end() const {
return hash_map<const BasicBlock*, SchedGraph*>::end();
}
//
// Debugging support
//
void dump () const;
private:
inline void noteGraphForBlock(const BasicBlock* bb, SchedGraph* graph) {
assert((*this)[bb] == NULL);
(*this)[bb] = graph;
}
//
// Graph builder
//
void buildGraphsForMethod (const Method *method,
const TargetMachine& target);
};
//********************** Sched Graph Iterators *****************************/
// Ok to make it a template because it shd get instantiated at most twice:
// for <SchedGraphNode, SchedGraphNode::iterator> and
// for <const SchedGraphNode, SchedGraphNode::const_iterator>.
//
template <class _NodeType, class _EdgeType, class _EdgeIter>
class PredIterator: public std::bidirectional_iterator<_NodeType, ptrdiff_t> {
protected:
_EdgeIter oi;
public:
typedef PredIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
inline PredIterator(_EdgeIter startEdge) : oi(startEdge) {}
inline bool operator==(const _Self& x) const { return oi == x.oi; }
inline bool operator!=(const _Self& x) const { return !operator==(x); }
// operator*() differs for pred or succ iterator
inline _NodeType* operator*() const { return (*oi)->getSrc(); }
inline _NodeType* operator->() const { return operator*(); }
inline _EdgeType* getEdge() const { return *(oi); }
inline _Self &operator++() { ++oi; return *this; } // Preincrement
inline _Self operator++(int) { // Postincrement
_Self tmp(*this); ++*this; return tmp;
}
inline _Self &operator--() { --oi; return *this; } // Predecrement
inline _Self operator--(int) { // Postdecrement
_Self tmp = *this; --*this; return tmp;
}
};
template <class _NodeType, class _EdgeType, class _EdgeIter>
class SuccIterator: public std::bidirectional_iterator<_NodeType, ptrdiff_t> {
protected:
_EdgeIter oi;
public:
typedef SuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
inline SuccIterator(_EdgeIter startEdge) : oi(startEdge) {}
inline bool operator==(const _Self& x) const { return oi == x.oi; }
inline bool operator!=(const _Self& x) const { return !operator==(x); }
inline _NodeType* operator*() const { return (*oi)->getSink(); }
inline _NodeType* operator->() const { return operator*(); }
inline _EdgeType* getEdge() const { return *(oi); }
inline _Self &operator++() { ++oi; return *this; } // Preincrement
inline _Self operator++(int) { // Postincrement
_Self tmp(*this); ++*this; return tmp;
}
inline _Self &operator--() { --oi; return *this; } // Predecrement
inline _Self operator--(int) { // Postdecrement
_Self tmp = *this; --*this; return tmp;
}
};
//
// sg_pred_iterator
// sg_pred_const_iterator
//
typedef PredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
sg_pred_iterator;
typedef PredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
sg_pred_const_iterator;
inline sg_pred_iterator pred_begin( SchedGraphNode *N) {
return sg_pred_iterator(N->beginInEdges());
}
inline sg_pred_iterator pred_end( SchedGraphNode *N) {
return sg_pred_iterator(N->endInEdges());
}
inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) {
return sg_pred_const_iterator(N->beginInEdges());
}
inline sg_pred_const_iterator pred_end( const SchedGraphNode *N) {
return sg_pred_const_iterator(N->endInEdges());
}
//
// sg_succ_iterator
// sg_succ_const_iterator
//
typedef SuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
sg_succ_iterator;
typedef SuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
sg_succ_const_iterator;
inline sg_succ_iterator succ_begin( SchedGraphNode *N) {
return sg_succ_iterator(N->beginOutEdges());
}
inline sg_succ_iterator succ_end( SchedGraphNode *N) {
return sg_succ_iterator(N->endOutEdges());
}
inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) {
return sg_succ_const_iterator(N->beginOutEdges());
}
inline sg_succ_const_iterator succ_end( const SchedGraphNode *N) {
return sg_succ_const_iterator(N->endOutEdges());
}
// Provide specializations of GraphTraits to be able to use graph iterators on
// the scheduling graph!
//
template <> struct GraphTraits<SchedGraph*> {
typedef SchedGraphNode NodeType;
typedef sg_succ_iterator ChildIteratorType;
static inline NodeType *getEntryNode(SchedGraph *SG) { return SG->getRoot(); }
static inline ChildIteratorType child_begin(NodeType *N) {
return succ_begin(N);
}
static inline ChildIteratorType child_end(NodeType *N) {
return succ_end(N);
}
};
template <> struct GraphTraits<const SchedGraph*> {
typedef const SchedGraphNode NodeType;
typedef sg_succ_const_iterator ChildIteratorType;
static inline NodeType *getEntryNode(const SchedGraph *SG) {
return SG->getRoot();
}
static inline ChildIteratorType child_begin(NodeType *N) {
return succ_begin(N);
}
static inline ChildIteratorType child_end(NodeType *N) {
return succ_end(N);
}
};
//************************ External Functions *****************************/
ostream& operator<<(ostream& os, const SchedGraphEdge& edge);
ostream& operator<<(ostream& os, const SchedGraphNode& node);
/***************************************************************************/
#endif
|