blob: a8b3e23397611ec0c5466e0d6234b9bdc7992065 [file] [log] [blame]
Vikram S. Adve851d44c2001-09-18 12:49:39 +00001// -*-C++-*-
2//***************************************************************************
3// File:
4// SchedPriorities.h
5//
6// Purpose:
7// Encapsulate heuristics for instruction scheduling.
8//
9// Strategy:
10// Priority ordering rules:
11// (1) Max delay, which is the order of the heap S.candsAsHeap.
12// (2) Instruction that frees up a register.
13// (3) Instruction that has the maximum number of dependent instructions.
14// Note that rules 2 and 3 are only used if issue conflicts prevent
15// choosing a higher priority instruction by rule 1.
16//
17// History:
18// 7/30/01 - Vikram Adve - Created
19//**************************************************************************/
Vikram S. Adve37866b32001-08-28 23:06:49 +000020
21#ifndef LLVM_CODEGEN_SCHEDPRIORITIES_H
22#define LLVM_CODEGEN_SCHEDPRIORITIES_H
23
Chris Lattner46cbff62001-09-14 16:56:32 +000024#include "SchedGraph.h"
Chris Lattnerdcd2fb52001-09-07 21:18:16 +000025#include "llvm/CodeGen/InstrScheduling.h"
Vikram S. Adve37866b32001-08-28 23:06:49 +000026#include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
Vikram S. Adve851d44c2001-09-18 12:49:39 +000027#include "llvm/Target/MachineSchedInfo.h"
Chris Lattner3ff43872001-09-28 22:56:31 +000028#include <list>
Chris Lattner697954c2002-01-20 22:54:45 +000029#include <ostream>
Vikram S. Adve37866b32001-08-28 23:06:49 +000030
31class Method;
32class MachineInstr;
33class SchedulingManager;
34
Vikram S. Adve37866b32001-08-28 23:06:49 +000035
36struct NodeDelayPair {
37 const SchedGraphNode* node;
38 cycles_t delay;
39 NodeDelayPair(const SchedGraphNode* n, cycles_t d) : node(n), delay(d) {}
Chris Lattner697954c2002-01-20 22:54:45 +000040 inline bool operator<(const NodeDelayPair& np) { return delay < np.delay; }
Vikram S. Adve37866b32001-08-28 23:06:49 +000041};
42
43inline bool
44NDPLessThan(const NodeDelayPair* np1, const NodeDelayPair* np2)
45{
Chris Lattner697954c2002-01-20 22:54:45 +000046 return np1->delay < np2->delay;
Vikram S. Adve37866b32001-08-28 23:06:49 +000047}
48
Chris Lattner697954c2002-01-20 22:54:45 +000049class NodeHeap: public std::list<NodeDelayPair*>, public NonCopyable {
Vikram S. Adve37866b32001-08-28 23:06:49 +000050public:
Chris Lattner697954c2002-01-20 22:54:45 +000051 typedef std::list<NodeDelayPair*>::iterator iterator;
52 typedef std::list<NodeDelayPair*>::const_iterator const_iterator;
Vikram S. Adve37866b32001-08-28 23:06:49 +000053
54public:
Chris Lattner697954c2002-01-20 22:54:45 +000055 /*ctor*/ NodeHeap () : std::list<NodeDelayPair*>(), _size(0) {}
Vikram S. Adve37866b32001-08-28 23:06:49 +000056 /*dtor*/ ~NodeHeap () {}
57
58 inline unsigned int size () const { return _size; }
59
60 const SchedGraphNode* getNode (const_iterator i) const { return (*i)->node; }
61 cycles_t getDelay(const_iterator i) const { return (*i)->delay;}
62
63 inline void makeHeap() {
64 // make_heap(begin(), end(), NDPLessThan);
65 }
66
67 inline iterator findNode(const SchedGraphNode* node) {
68 for (iterator I=begin(); I != end(); ++I)
69 if (getNode(I) == node)
70 return I;
71 return end();
72 }
73
74 inline void removeNode (const SchedGraphNode* node) {
75 iterator ndpPtr = findNode(node);
76 if (ndpPtr != end())
77 {
78 delete *ndpPtr;
79 erase(ndpPtr);
80 --_size;
81 }
82 };
83
84 void insert(const SchedGraphNode* node, cycles_t delay) {
85 NodeDelayPair* ndp = new NodeDelayPair(node, delay);
86 if (_size == 0 || front()->delay < delay)
87 push_front(ndp);
88 else
89 {
90 iterator I=begin();
91 for ( ; I != end() && getDelay(I) >= delay; ++I)
92 ;
Chris Lattner697954c2002-01-20 22:54:45 +000093 std::list<NodeDelayPair*>::insert(I, ndp);
Vikram S. Adve37866b32001-08-28 23:06:49 +000094 }
95 _size++;
96 }
97private:
98 unsigned int _size;
99};
100
101
102class SchedPriorities: public NonCopyable {
103public:
104 /*ctor*/ SchedPriorities (const Method* method,
105 const SchedGraph* _graph);
106
107 // This must be called before scheduling begins.
108 void initialize ();
109
110 cycles_t getTime () const { return curTime; }
111 cycles_t getEarliestReadyTime () const { return earliestReadyTime; }
112 unsigned getNumReady () const { return candsAsHeap.size(); }
113 bool nodeIsReady (const SchedGraphNode* node) const {
114 return (candsAsSet.find(node) != candsAsSet.end());
115 }
116
117 void issuedReadyNodeAt (cycles_t curTime,
118 const SchedGraphNode* node);
119
120 void insertReady (const SchedGraphNode* node);
121
122 void updateTime (cycles_t /*unused*/);
123
124 const SchedGraphNode* getNextHighest (const SchedulingManager& S,
125 cycles_t curTime);
126 // choose next highest priority instr
127
128private:
129 typedef NodeHeap::iterator candIndex;
130
131private:
132 cycles_t curTime;
133 const SchedGraph* graph;
134 MethodLiveVarInfo methodLiveVarInfo;
Chris Lattner697954c2002-01-20 22:54:45 +0000135 std::hash_map<const MachineInstr*, bool> lastUseMap;
136 std::vector<cycles_t> nodeDelayVec;
137 std::vector<cycles_t> earliestForNode;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000138 cycles_t earliestReadyTime;
139 NodeHeap candsAsHeap; // candidate nodes, ready to go
Chris Lattner697954c2002-01-20 22:54:45 +0000140 std::hash_set<const SchedGraphNode*> candsAsSet;//same entries as candsAsHeap,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000141 // but as set for fast lookup
Chris Lattner697954c2002-01-20 22:54:45 +0000142 std::vector<candIndex> mcands; // holds pointers into cands
Vikram S. Adve37866b32001-08-28 23:06:49 +0000143 candIndex nextToTry; // next cand after the last
144 // one tried in this cycle
145
Chris Lattner697954c2002-01-20 22:54:45 +0000146 int chooseByRule1 (std::vector<candIndex>& mcands);
147 int chooseByRule2 (std::vector<candIndex>& mcands);
148 int chooseByRule3 (std::vector<candIndex>& mcands);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000149
Chris Lattner697954c2002-01-20 22:54:45 +0000150 void findSetWithMaxDelay (std::vector<candIndex>& mcands,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000151 const SchedulingManager& S);
152
153 void computeDelays (const SchedGraph* graph);
154
155 void initializeReadyHeap (const SchedGraph* graph);
156
157 bool instructionHasLastUse (MethodLiveVarInfo& methodLiveVarInfo,
158 const SchedGraphNode* graphNode);
159
160 // NOTE: The next two return references to the actual vector entries.
161 // Use with care.
162 cycles_t& getNodeDelayRef (const SchedGraphNode* node) {
163 assert(node->getNodeId() < nodeDelayVec.size());
164 return nodeDelayVec[node->getNodeId()];
165 }
166 cycles_t& getEarliestForNodeRef (const SchedGraphNode* node) {
167 assert(node->getNodeId() < earliestForNode.size());
168 return earliestForNode[node->getNodeId()];
169 }
170};
171
172
Chris Lattnerdcd2fb52001-09-07 21:18:16 +0000173inline void SchedPriorities::updateTime(cycles_t c) {
Vikram S. Adve37866b32001-08-28 23:06:49 +0000174 curTime = c;
175 nextToTry = candsAsHeap.begin();
176 mcands.clear();
177}
178
Chris Lattner697954c2002-01-20 22:54:45 +0000179inline std::ostream &operator<<(std::ostream &os, const NodeDelayPair* nd) {
Chris Lattnerdcd2fb52001-09-07 21:18:16 +0000180 return os << "Delay for node " << nd->node->getNodeId()
Chris Lattner697954c2002-01-20 22:54:45 +0000181 << " = " << (long)nd->delay << "\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +0000182}
183
Vikram S. Adve37866b32001-08-28 23:06:49 +0000184#endif