blob: 7cbd0d12596bbccdb10a57095c275163cbf42f6a [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 Lattner77f66c12002-02-04 02:44:20 +000028#include "Support/CommandLine.h"
Chris Lattner3ff43872001-09-28 22:56:31 +000029#include <list>
Vikram S. Adve37866b32001-08-28 23:06:49 +000030
31class Method;
32class MachineInstr;
33class SchedulingManager;
34
Chris Lattner77f66c12002-02-04 02:44:20 +000035// Debug option levels for instruction scheduling
36enum SchedDebugLevel_t {
37 Sched_NoDebugInfo,
38 Sched_PrintMachineCode,
39 Sched_PrintSchedTrace,
40 Sched_PrintSchedGraphs,
41};
42
43extern cl::Enum<SchedDebugLevel_t> SchedDebugLevel;
Vikram S. Adve37866b32001-08-28 23:06:49 +000044
45struct NodeDelayPair {
46 const SchedGraphNode* node;
47 cycles_t delay;
48 NodeDelayPair(const SchedGraphNode* n, cycles_t d) : node(n), delay(d) {}
Chris Lattner697954c2002-01-20 22:54:45 +000049 inline bool operator<(const NodeDelayPair& np) { return delay < np.delay; }
Vikram S. Adve37866b32001-08-28 23:06:49 +000050};
51
52inline bool
53NDPLessThan(const NodeDelayPair* np1, const NodeDelayPair* np2)
54{
Chris Lattner697954c2002-01-20 22:54:45 +000055 return np1->delay < np2->delay;
Vikram S. Adve37866b32001-08-28 23:06:49 +000056}
57
Chris Lattner697954c2002-01-20 22:54:45 +000058class NodeHeap: public std::list<NodeDelayPair*>, public NonCopyable {
Vikram S. Adve37866b32001-08-28 23:06:49 +000059public:
Chris Lattner697954c2002-01-20 22:54:45 +000060 typedef std::list<NodeDelayPair*>::iterator iterator;
61 typedef std::list<NodeDelayPair*>::const_iterator const_iterator;
Vikram S. Adve37866b32001-08-28 23:06:49 +000062
63public:
Chris Lattner697954c2002-01-20 22:54:45 +000064 /*ctor*/ NodeHeap () : std::list<NodeDelayPair*>(), _size(0) {}
Vikram S. Adve37866b32001-08-28 23:06:49 +000065 /*dtor*/ ~NodeHeap () {}
66
67 inline unsigned int size () const { return _size; }
68
69 const SchedGraphNode* getNode (const_iterator i) const { return (*i)->node; }
70 cycles_t getDelay(const_iterator i) const { return (*i)->delay;}
71
72 inline void makeHeap() {
73 // make_heap(begin(), end(), NDPLessThan);
74 }
75
76 inline iterator findNode(const SchedGraphNode* node) {
77 for (iterator I=begin(); I != end(); ++I)
78 if (getNode(I) == node)
79 return I;
80 return end();
81 }
82
83 inline void removeNode (const SchedGraphNode* node) {
84 iterator ndpPtr = findNode(node);
85 if (ndpPtr != end())
86 {
87 delete *ndpPtr;
88 erase(ndpPtr);
89 --_size;
90 }
91 };
92
93 void insert(const SchedGraphNode* node, cycles_t delay) {
94 NodeDelayPair* ndp = new NodeDelayPair(node, delay);
95 if (_size == 0 || front()->delay < delay)
96 push_front(ndp);
97 else
98 {
99 iterator I=begin();
100 for ( ; I != end() && getDelay(I) >= delay; ++I)
101 ;
Chris Lattner697954c2002-01-20 22:54:45 +0000102 std::list<NodeDelayPair*>::insert(I, ndp);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000103 }
104 _size++;
105 }
106private:
107 unsigned int _size;
108};
109
110
111class SchedPriorities: public NonCopyable {
112public:
113 /*ctor*/ SchedPriorities (const Method* method,
114 const SchedGraph* _graph);
115
116 // This must be called before scheduling begins.
117 void initialize ();
118
119 cycles_t getTime () const { return curTime; }
120 cycles_t getEarliestReadyTime () const { return earliestReadyTime; }
121 unsigned getNumReady () const { return candsAsHeap.size(); }
122 bool nodeIsReady (const SchedGraphNode* node) const {
123 return (candsAsSet.find(node) != candsAsSet.end());
124 }
125
126 void issuedReadyNodeAt (cycles_t curTime,
127 const SchedGraphNode* node);
128
129 void insertReady (const SchedGraphNode* node);
130
131 void updateTime (cycles_t /*unused*/);
132
133 const SchedGraphNode* getNextHighest (const SchedulingManager& S,
134 cycles_t curTime);
135 // choose next highest priority instr
136
137private:
138 typedef NodeHeap::iterator candIndex;
139
140private:
141 cycles_t curTime;
142 const SchedGraph* graph;
143 MethodLiveVarInfo methodLiveVarInfo;
Chris Lattner697954c2002-01-20 22:54:45 +0000144 std::hash_map<const MachineInstr*, bool> lastUseMap;
145 std::vector<cycles_t> nodeDelayVec;
146 std::vector<cycles_t> earliestForNode;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000147 cycles_t earliestReadyTime;
148 NodeHeap candsAsHeap; // candidate nodes, ready to go
Chris Lattner697954c2002-01-20 22:54:45 +0000149 std::hash_set<const SchedGraphNode*> candsAsSet;//same entries as candsAsHeap,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000150 // but as set for fast lookup
Chris Lattner697954c2002-01-20 22:54:45 +0000151 std::vector<candIndex> mcands; // holds pointers into cands
Vikram S. Adve37866b32001-08-28 23:06:49 +0000152 candIndex nextToTry; // next cand after the last
153 // one tried in this cycle
154
Chris Lattner697954c2002-01-20 22:54:45 +0000155 int chooseByRule1 (std::vector<candIndex>& mcands);
156 int chooseByRule2 (std::vector<candIndex>& mcands);
157 int chooseByRule3 (std::vector<candIndex>& mcands);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000158
Chris Lattner697954c2002-01-20 22:54:45 +0000159 void findSetWithMaxDelay (std::vector<candIndex>& mcands,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000160 const SchedulingManager& S);
161
162 void computeDelays (const SchedGraph* graph);
163
164 void initializeReadyHeap (const SchedGraph* graph);
165
166 bool instructionHasLastUse (MethodLiveVarInfo& methodLiveVarInfo,
167 const SchedGraphNode* graphNode);
168
169 // NOTE: The next two return references to the actual vector entries.
170 // Use with care.
171 cycles_t& getNodeDelayRef (const SchedGraphNode* node) {
172 assert(node->getNodeId() < nodeDelayVec.size());
173 return nodeDelayVec[node->getNodeId()];
174 }
175 cycles_t& getEarliestForNodeRef (const SchedGraphNode* node) {
176 assert(node->getNodeId() < earliestForNode.size());
177 return earliestForNode[node->getNodeId()];
178 }
179};
180
181
Chris Lattnerdcd2fb52001-09-07 21:18:16 +0000182inline void SchedPriorities::updateTime(cycles_t c) {
Vikram S. Adve37866b32001-08-28 23:06:49 +0000183 curTime = c;
184 nextToTry = candsAsHeap.begin();
185 mcands.clear();
186}
187
Chris Lattner697954c2002-01-20 22:54:45 +0000188inline std::ostream &operator<<(std::ostream &os, const NodeDelayPair* nd) {
Chris Lattnerdcd2fb52001-09-07 21:18:16 +0000189 return os << "Delay for node " << nd->node->getNodeId()
Chris Lattner697954c2002-01-20 22:54:45 +0000190 << " = " << (long)nd->delay << "\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +0000191}
192
Vikram S. Adve37866b32001-08-28 23:06:49 +0000193#endif