blob: 57126aaa3348d49c10afd101a5cf5f672bfde609 [file] [log] [blame]
Chris Lattner179cdfb2002-08-09 20:08:03 +00001//===-- SchedPriorities.h - Encapsulate scheduling heuristics -------------===//
Vikram S. Advefe30f1f2001-09-18 12:52:03 +00002//
3// Strategy:
4// Priority ordering rules:
5// (1) Max delay, which is the order of the heap S.candsAsHeap.
6// (2) Instruction that frees up a register.
7// (3) Instruction that has the maximum number of dependent instructions.
8// Note that rules 2 and 3 are only used if issue conflicts prevent
9// choosing a higher priority instruction by rule 1.
Chris Lattner179cdfb2002-08-09 20:08:03 +000010//
11//===----------------------------------------------------------------------===//
Vikram S. Adve37866b32001-08-28 23:06:49 +000012
Chris Lattner46cbff62001-09-14 16:56:32 +000013#include "SchedPriorities.h"
Chris Lattner483e14e2002-04-27 07:27:19 +000014#include "llvm/Analysis/LiveVar/FunctionLiveVarInfo.h"
Chris Lattner221d6882002-02-12 21:07:25 +000015#include "llvm/Support/CFG.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000016#include "Support/PostOrderIterator.h"
Chris Lattner697954c2002-01-20 22:54:45 +000017using std::cerr;
Vikram S. Adve37866b32001-08-28 23:06:49 +000018
Chris Lattnerb7653df2002-04-08 22:03:57 +000019SchedPriorities::SchedPriorities(const Function *, const SchedGraph *G,
Chris Lattner483e14e2002-04-27 07:27:19 +000020 FunctionLiveVarInfo &LVI)
Chris Lattner9adb7ad2002-02-04 20:02:16 +000021 : curTime(0), graph(G), methodLiveVarInfo(LVI),
22 nodeDelayVec(G->getNumNodes(), INVALID_LATENCY), // make errors obvious
Vikram S. Adve0baf1c02002-07-08 22:59:23 +000023 earliestReadyTimeForNode(G->getNumNodes(), 0),
Vikram S. Adve37866b32001-08-28 23:06:49 +000024 earliestReadyTime(0),
Vikram S. Adve0baf1c02002-07-08 22:59:23 +000025 nextToTry(candsAsHeap.begin())
26{
Vikram S. Adve37866b32001-08-28 23:06:49 +000027 computeDelays(graph);
28}
29
30
31void
32SchedPriorities::initialize()
33{
34 initializeReadyHeap(graph);
35}
36
37
38void
39SchedPriorities::computeDelays(const SchedGraph* graph)
40{
Chris Lattner3ff43872001-09-28 22:56:31 +000041 po_iterator<const SchedGraph*> poIter = po_begin(graph), poEnd =po_end(graph);
Vikram S. Adve37866b32001-08-28 23:06:49 +000042 for ( ; poIter != poEnd; ++poIter)
43 {
44 const SchedGraphNode* node = *poIter;
45 cycles_t nodeDelay;
46 if (node->beginOutEdges() == node->endOutEdges())
Vikram S. Adve0baf1c02002-07-08 22:59:23 +000047 nodeDelay = node->getLatency();
Vikram S. Adve37866b32001-08-28 23:06:49 +000048 else
49 {
50 // Iterate over the out-edges of the node to compute delay
51 nodeDelay = 0;
52 for (SchedGraphNode::const_iterator E=node->beginOutEdges();
53 E != node->endOutEdges(); ++E)
54 {
Vikram S. Adve0baf1c02002-07-08 22:59:23 +000055 cycles_t sinkDelay = getNodeDelay((*E)->getSink());
Chris Lattner697954c2002-01-20 22:54:45 +000056 nodeDelay = std::max(nodeDelay, sinkDelay + (*E)->getMinDelay());
Vikram S. Adve37866b32001-08-28 23:06:49 +000057 }
58 }
59 getNodeDelayRef(node) = nodeDelay;
60 }
61}
62
63
64void
65SchedPriorities::initializeReadyHeap(const SchedGraph* graph)
66{
67 const SchedGraphNode* graphRoot = graph->getRoot();
68 assert(graphRoot->getMachineInstr() == NULL && "Expect dummy root");
69
70 // Insert immediate successors of dummy root, which are the actual roots
71 sg_succ_const_iterator SEnd = succ_end(graphRoot);
72 for (sg_succ_const_iterator S = succ_begin(graphRoot); S != SEnd; ++S)
73 this->insertReady(*S);
74
75#undef TEST_HEAP_CONVERSION
76#ifdef TEST_HEAP_CONVERSION
Chris Lattner697954c2002-01-20 22:54:45 +000077 cerr << "Before heap conversion:\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +000078 copy(candsAsHeap.begin(), candsAsHeap.end(),
Chris Lattner697954c2002-01-20 22:54:45 +000079 ostream_iterator<NodeDelayPair*>(cerr,"\n"));
Vikram S. Adve37866b32001-08-28 23:06:49 +000080#endif
81
82 candsAsHeap.makeHeap();
83
Vikram S. Adve1392d692002-03-24 03:46:15 +000084 nextToTry = candsAsHeap.begin();
85
Vikram S. Adve37866b32001-08-28 23:06:49 +000086#ifdef TEST_HEAP_CONVERSION
Chris Lattner697954c2002-01-20 22:54:45 +000087 cerr << "After heap conversion:\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +000088 copy(candsAsHeap.begin(), candsAsHeap.end(),
Chris Lattner697954c2002-01-20 22:54:45 +000089 ostream_iterator<NodeDelayPair*>(cerr,"\n"));
Vikram S. Adve37866b32001-08-28 23:06:49 +000090#endif
91}
92
Chris Lattner697954c2002-01-20 22:54:45 +000093void
94SchedPriorities::insertReady(const SchedGraphNode* node)
95{
96 candsAsHeap.insert(node, nodeDelayVec[node->getNodeId()]);
97 candsAsSet.insert(node);
98 mcands.clear(); // ensure reset choices is called before any more choices
99 earliestReadyTime = std::min(earliestReadyTime,
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000100 getEarliestReadyTimeForNode(node));
Chris Lattner697954c2002-01-20 22:54:45 +0000101
102 if (SchedDebugLevel >= Sched_PrintSchedTrace)
103 {
Vikram S. Adve1392d692002-03-24 03:46:15 +0000104 cerr << " Node " << node->getNodeId() << " will be ready in Cycle "
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000105 << getEarliestReadyTimeForNode(node) << "; "
106 << " Delay = " <<(long)getNodeDelay(node) << "; Instruction: \n";
Chris Lattner697954c2002-01-20 22:54:45 +0000107 cerr << " " << *node->getMachineInstr() << "\n";
108 }
109}
Vikram S. Adve37866b32001-08-28 23:06:49 +0000110
111void
112SchedPriorities::issuedReadyNodeAt(cycles_t curTime,
113 const SchedGraphNode* node)
114{
115 candsAsHeap.removeNode(node);
116 candsAsSet.erase(node);
117 mcands.clear(); // ensure reset choices is called before any more choices
118
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000119 if (earliestReadyTime == getEarliestReadyTimeForNode(node))
Vikram S. Adve37866b32001-08-28 23:06:49 +0000120 {// earliestReadyTime may have been due to this node, so recompute it
121 earliestReadyTime = HUGE_LATENCY;
122 for (NodeHeap::const_iterator I=candsAsHeap.begin();
123 I != candsAsHeap.end(); ++I)
124 if (candsAsHeap.getNode(I))
Chris Lattner697954c2002-01-20 22:54:45 +0000125 earliestReadyTime = std::min(earliestReadyTime,
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000126 getEarliestReadyTimeForNode(candsAsHeap.getNode(I)));
Vikram S. Adve37866b32001-08-28 23:06:49 +0000127 }
128
129 // Now update ready times for successors
130 for (SchedGraphNode::const_iterator E=node->beginOutEdges();
131 E != node->endOutEdges(); ++E)
132 {
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000133 cycles_t& etime = getEarliestReadyTimeForNodeRef((*E)->getSink());
Chris Lattner697954c2002-01-20 22:54:45 +0000134 etime = std::max(etime, curTime + (*E)->getMinDelay());
Vikram S. Adve37866b32001-08-28 23:06:49 +0000135 }
136}
137
138
139//----------------------------------------------------------------------
140// Priority ordering rules:
141// (1) Max delay, which is the order of the heap S.candsAsHeap.
142// (2) Instruction that frees up a register.
143// (3) Instruction that has the maximum number of dependent instructions.
144// Note that rules 2 and 3 are only used if issue conflicts prevent
145// choosing a higher priority instruction by rule 1.
146//----------------------------------------------------------------------
147
148inline int
Chris Lattner697954c2002-01-20 22:54:45 +0000149SchedPriorities::chooseByRule1(std::vector<candIndex>& mcands)
Vikram S. Adve37866b32001-08-28 23:06:49 +0000150{
151 return (mcands.size() == 1)? 0 // only one choice exists so take it
152 : -1; // -1 indicates multiple choices
153}
154
155inline int
Chris Lattner697954c2002-01-20 22:54:45 +0000156SchedPriorities::chooseByRule2(std::vector<candIndex>& mcands)
Vikram S. Adve37866b32001-08-28 23:06:49 +0000157{
158 assert(mcands.size() >= 1 && "Should have at least one candidate here.");
159 for (unsigned i=0, N = mcands.size(); i < N; i++)
160 if (instructionHasLastUse(methodLiveVarInfo,
161 candsAsHeap.getNode(mcands[i])))
162 return i;
163 return -1;
164}
165
166inline int
Chris Lattner697954c2002-01-20 22:54:45 +0000167SchedPriorities::chooseByRule3(std::vector<candIndex>& mcands)
Vikram S. Adve37866b32001-08-28 23:06:49 +0000168{
169 assert(mcands.size() >= 1 && "Should have at least one candidate here.");
170 int maxUses = candsAsHeap.getNode(mcands[0])->getNumOutEdges();
171 int indexWithMaxUses = 0;
172 for (unsigned i=1, N = mcands.size(); i < N; i++)
173 {
174 int numUses = candsAsHeap.getNode(mcands[i])->getNumOutEdges();
175 if (numUses > maxUses)
176 {
177 maxUses = numUses;
178 indexWithMaxUses = i;
179 }
180 }
181 return indexWithMaxUses;
182}
183
184const SchedGraphNode*
185SchedPriorities::getNextHighest(const SchedulingManager& S,
186 cycles_t curTime)
187{
188 int nextIdx = -1;
189 const SchedGraphNode* nextChoice = NULL;
190
191 if (mcands.size() == 0)
192 findSetWithMaxDelay(mcands, S);
193
194 while (nextIdx < 0 && mcands.size() > 0)
195 {
196 nextIdx = chooseByRule1(mcands); // rule 1
197
198 if (nextIdx == -1)
199 nextIdx = chooseByRule2(mcands); // rule 2
200
201 if (nextIdx == -1)
202 nextIdx = chooseByRule3(mcands); // rule 3
203
204 if (nextIdx == -1)
205 nextIdx = 0; // default to first choice by delays
206
207 // We have found the next best candidate. Check if it ready in
208 // the current cycle, and if it is feasible.
209 // If not, remove it from mcands and continue. Refill mcands if
210 // it becomes empty.
211 nextChoice = candsAsHeap.getNode(mcands[nextIdx]);
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000212 if (getEarliestReadyTimeForNode(nextChoice) > curTime
Chris Lattner15dedbc2001-09-07 21:22:28 +0000213 || ! instrIsFeasible(S, nextChoice->getMachineInstr()->getOpCode()))
Vikram S. Adve37866b32001-08-28 23:06:49 +0000214 {
215 mcands.erase(mcands.begin() + nextIdx);
216 nextIdx = -1;
217 if (mcands.size() == 0)
218 findSetWithMaxDelay(mcands, S);
219 }
220 }
221
222 if (nextIdx >= 0)
223 {
224 mcands.erase(mcands.begin() + nextIdx);
225 return nextChoice;
226 }
227 else
228 return NULL;
229}
230
231
232void
Chris Lattner697954c2002-01-20 22:54:45 +0000233SchedPriorities::findSetWithMaxDelay(std::vector<candIndex>& mcands,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000234 const SchedulingManager& S)
235{
236 if (mcands.size() == 0 && nextToTry != candsAsHeap.end())
237 { // out of choices at current maximum delay;
238 // put nodes with next highest delay in mcands
239 candIndex next = nextToTry;
240 cycles_t maxDelay = candsAsHeap.getDelay(next);
241 for (; next != candsAsHeap.end()
242 && candsAsHeap.getDelay(next) == maxDelay; ++next)
243 mcands.push_back(next);
244
245 nextToTry = next;
246
247 if (SchedDebugLevel >= Sched_PrintSchedTrace)
248 {
Chris Lattner697954c2002-01-20 22:54:45 +0000249 cerr << " Cycle " << (long)getTime() << ": "
250 << "Next highest delay = " << (long)maxDelay << " : "
Vikram S. Adve37866b32001-08-28 23:06:49 +0000251 << mcands.size() << " Nodes with this delay: ";
252 for (unsigned i=0; i < mcands.size(); i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000253 cerr << candsAsHeap.getNode(mcands[i])->getNodeId() << ", ";
254 cerr << "\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +0000255 }
256 }
257}
258
259
260bool
Chris Lattner483e14e2002-04-27 07:27:19 +0000261SchedPriorities::instructionHasLastUse(FunctionLiveVarInfo &LVI,
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000262 const SchedGraphNode* graphNode) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000263 const MachineInstr *MI = graphNode->getMachineInstr();
Vikram S. Adve37866b32001-08-28 23:06:49 +0000264
Chris Lattner09ff1122002-07-24 21:21:32 +0000265 hash_map<const MachineInstr*, bool>::const_iterator
Chris Lattner2f898d22002-02-05 06:02:59 +0000266 ui = lastUseMap.find(MI);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000267 if (ui != lastUseMap.end())
Chris Lattner697954c2002-01-20 22:54:45 +0000268 return ui->second;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000269
270 // else check if instruction is a last use and save it in the hash_map
271 bool hasLastUse = false;
Vikram S. Adveaf00d482001-11-12 14:18:01 +0000272 const BasicBlock* bb = graphNode->getBB();
Chris Lattner483e14e2002-04-27 07:27:19 +0000273 const ValueSet &LVs = LVI.getLiveVarSetBeforeMInst(MI, bb);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000274
Chris Lattner2f898d22002-02-05 06:02:59 +0000275 for (MachineInstr::const_val_op_iterator OI = MI->begin(), OE = MI->end();
276 OI != OE; ++OI)
277 if (!LVs.count(*OI)) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000278 hasLastUse = true;
279 break;
280 }
Chris Lattner748697d2002-02-05 04:20:12 +0000281
Chris Lattner2f898d22002-02-05 06:02:59 +0000282 return lastUseMap[MI] = hasLastUse;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000283}
284