blob: 8a1c9a534842bfa52d71bc00ad914d87d6bad7a6 [file] [log] [blame]
Vikram S. Advefe30f1f2001-09-18 12:52:03 +00001// $Id$ -*-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
Chris Lattner46cbff62001-09-14 16:56:32 +000021#include "SchedPriorities.h"
Chris Lattner483e14e2002-04-27 07:27:19 +000022#include "llvm/Analysis/LiveVar/FunctionLiveVarInfo.h"
Chris Lattner221d6882002-02-12 21:07:25 +000023#include "llvm/Support/CFG.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000024#include "Support/PostOrderIterator.h"
Chris Lattner697954c2002-01-20 22:54:45 +000025using std::cerr;
Vikram S. Adve37866b32001-08-28 23:06:49 +000026
Chris Lattnerb7653df2002-04-08 22:03:57 +000027SchedPriorities::SchedPriorities(const Function *, const SchedGraph *G,
Chris Lattner483e14e2002-04-27 07:27:19 +000028 FunctionLiveVarInfo &LVI)
Chris Lattner9adb7ad2002-02-04 20:02:16 +000029 : curTime(0), graph(G), methodLiveVarInfo(LVI),
30 nodeDelayVec(G->getNumNodes(), INVALID_LATENCY), // make errors obvious
Vikram S. Adve0baf1c02002-07-08 22:59:23 +000031 earliestReadyTimeForNode(G->getNumNodes(), 0),
Vikram S. Adve37866b32001-08-28 23:06:49 +000032 earliestReadyTime(0),
Vikram S. Adve0baf1c02002-07-08 22:59:23 +000033 nextToTry(candsAsHeap.begin())
34{
Vikram S. Adve37866b32001-08-28 23:06:49 +000035 computeDelays(graph);
36}
37
38
39void
40SchedPriorities::initialize()
41{
42 initializeReadyHeap(graph);
43}
44
45
46void
47SchedPriorities::computeDelays(const SchedGraph* graph)
48{
Chris Lattner3ff43872001-09-28 22:56:31 +000049 po_iterator<const SchedGraph*> poIter = po_begin(graph), poEnd =po_end(graph);
Vikram S. Adve37866b32001-08-28 23:06:49 +000050 for ( ; poIter != poEnd; ++poIter)
51 {
52 const SchedGraphNode* node = *poIter;
53 cycles_t nodeDelay;
54 if (node->beginOutEdges() == node->endOutEdges())
Vikram S. Adve0baf1c02002-07-08 22:59:23 +000055 nodeDelay = node->getLatency();
Vikram S. Adve37866b32001-08-28 23:06:49 +000056 else
57 {
58 // Iterate over the out-edges of the node to compute delay
59 nodeDelay = 0;
60 for (SchedGraphNode::const_iterator E=node->beginOutEdges();
61 E != node->endOutEdges(); ++E)
62 {
Vikram S. Adve0baf1c02002-07-08 22:59:23 +000063 cycles_t sinkDelay = getNodeDelay((*E)->getSink());
Chris Lattner697954c2002-01-20 22:54:45 +000064 nodeDelay = std::max(nodeDelay, sinkDelay + (*E)->getMinDelay());
Vikram S. Adve37866b32001-08-28 23:06:49 +000065 }
66 }
67 getNodeDelayRef(node) = nodeDelay;
68 }
69}
70
71
72void
73SchedPriorities::initializeReadyHeap(const SchedGraph* graph)
74{
75 const SchedGraphNode* graphRoot = graph->getRoot();
76 assert(graphRoot->getMachineInstr() == NULL && "Expect dummy root");
77
78 // Insert immediate successors of dummy root, which are the actual roots
79 sg_succ_const_iterator SEnd = succ_end(graphRoot);
80 for (sg_succ_const_iterator S = succ_begin(graphRoot); S != SEnd; ++S)
81 this->insertReady(*S);
82
83#undef TEST_HEAP_CONVERSION
84#ifdef TEST_HEAP_CONVERSION
Chris Lattner697954c2002-01-20 22:54:45 +000085 cerr << "Before heap conversion:\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +000086 copy(candsAsHeap.begin(), candsAsHeap.end(),
Chris Lattner697954c2002-01-20 22:54:45 +000087 ostream_iterator<NodeDelayPair*>(cerr,"\n"));
Vikram S. Adve37866b32001-08-28 23:06:49 +000088#endif
89
90 candsAsHeap.makeHeap();
91
Vikram S. Adve1392d692002-03-24 03:46:15 +000092 nextToTry = candsAsHeap.begin();
93
Vikram S. Adve37866b32001-08-28 23:06:49 +000094#ifdef TEST_HEAP_CONVERSION
Chris Lattner697954c2002-01-20 22:54:45 +000095 cerr << "After heap conversion:\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +000096 copy(candsAsHeap.begin(), candsAsHeap.end(),
Chris Lattner697954c2002-01-20 22:54:45 +000097 ostream_iterator<NodeDelayPair*>(cerr,"\n"));
Vikram S. Adve37866b32001-08-28 23:06:49 +000098#endif
99}
100
Chris Lattner697954c2002-01-20 22:54:45 +0000101void
102SchedPriorities::insertReady(const SchedGraphNode* node)
103{
104 candsAsHeap.insert(node, nodeDelayVec[node->getNodeId()]);
105 candsAsSet.insert(node);
106 mcands.clear(); // ensure reset choices is called before any more choices
107 earliestReadyTime = std::min(earliestReadyTime,
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000108 getEarliestReadyTimeForNode(node));
Chris Lattner697954c2002-01-20 22:54:45 +0000109
110 if (SchedDebugLevel >= Sched_PrintSchedTrace)
111 {
Vikram S. Adve1392d692002-03-24 03:46:15 +0000112 cerr << " Node " << node->getNodeId() << " will be ready in Cycle "
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000113 << getEarliestReadyTimeForNode(node) << "; "
114 << " Delay = " <<(long)getNodeDelay(node) << "; Instruction: \n";
Chris Lattner697954c2002-01-20 22:54:45 +0000115 cerr << " " << *node->getMachineInstr() << "\n";
116 }
117}
Vikram S. Adve37866b32001-08-28 23:06:49 +0000118
119void
120SchedPriorities::issuedReadyNodeAt(cycles_t curTime,
121 const SchedGraphNode* node)
122{
123 candsAsHeap.removeNode(node);
124 candsAsSet.erase(node);
125 mcands.clear(); // ensure reset choices is called before any more choices
126
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000127 if (earliestReadyTime == getEarliestReadyTimeForNode(node))
Vikram S. Adve37866b32001-08-28 23:06:49 +0000128 {// earliestReadyTime may have been due to this node, so recompute it
129 earliestReadyTime = HUGE_LATENCY;
130 for (NodeHeap::const_iterator I=candsAsHeap.begin();
131 I != candsAsHeap.end(); ++I)
132 if (candsAsHeap.getNode(I))
Chris Lattner697954c2002-01-20 22:54:45 +0000133 earliestReadyTime = std::min(earliestReadyTime,
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000134 getEarliestReadyTimeForNode(candsAsHeap.getNode(I)));
Vikram S. Adve37866b32001-08-28 23:06:49 +0000135 }
136
137 // Now update ready times for successors
138 for (SchedGraphNode::const_iterator E=node->beginOutEdges();
139 E != node->endOutEdges(); ++E)
140 {
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000141 cycles_t& etime = getEarliestReadyTimeForNodeRef((*E)->getSink());
Chris Lattner697954c2002-01-20 22:54:45 +0000142 etime = std::max(etime, curTime + (*E)->getMinDelay());
Vikram S. Adve37866b32001-08-28 23:06:49 +0000143 }
144}
145
146
147//----------------------------------------------------------------------
148// Priority ordering rules:
149// (1) Max delay, which is the order of the heap S.candsAsHeap.
150// (2) Instruction that frees up a register.
151// (3) Instruction that has the maximum number of dependent instructions.
152// Note that rules 2 and 3 are only used if issue conflicts prevent
153// choosing a higher priority instruction by rule 1.
154//----------------------------------------------------------------------
155
156inline int
Chris Lattner697954c2002-01-20 22:54:45 +0000157SchedPriorities::chooseByRule1(std::vector<candIndex>& mcands)
Vikram S. Adve37866b32001-08-28 23:06:49 +0000158{
159 return (mcands.size() == 1)? 0 // only one choice exists so take it
160 : -1; // -1 indicates multiple choices
161}
162
163inline int
Chris Lattner697954c2002-01-20 22:54:45 +0000164SchedPriorities::chooseByRule2(std::vector<candIndex>& mcands)
Vikram S. Adve37866b32001-08-28 23:06:49 +0000165{
166 assert(mcands.size() >= 1 && "Should have at least one candidate here.");
167 for (unsigned i=0, N = mcands.size(); i < N; i++)
168 if (instructionHasLastUse(methodLiveVarInfo,
169 candsAsHeap.getNode(mcands[i])))
170 return i;
171 return -1;
172}
173
174inline int
Chris Lattner697954c2002-01-20 22:54:45 +0000175SchedPriorities::chooseByRule3(std::vector<candIndex>& mcands)
Vikram S. Adve37866b32001-08-28 23:06:49 +0000176{
177 assert(mcands.size() >= 1 && "Should have at least one candidate here.");
178 int maxUses = candsAsHeap.getNode(mcands[0])->getNumOutEdges();
179 int indexWithMaxUses = 0;
180 for (unsigned i=1, N = mcands.size(); i < N; i++)
181 {
182 int numUses = candsAsHeap.getNode(mcands[i])->getNumOutEdges();
183 if (numUses > maxUses)
184 {
185 maxUses = numUses;
186 indexWithMaxUses = i;
187 }
188 }
189 return indexWithMaxUses;
190}
191
192const SchedGraphNode*
193SchedPriorities::getNextHighest(const SchedulingManager& S,
194 cycles_t curTime)
195{
196 int nextIdx = -1;
197 const SchedGraphNode* nextChoice = NULL;
198
199 if (mcands.size() == 0)
200 findSetWithMaxDelay(mcands, S);
201
202 while (nextIdx < 0 && mcands.size() > 0)
203 {
204 nextIdx = chooseByRule1(mcands); // rule 1
205
206 if (nextIdx == -1)
207 nextIdx = chooseByRule2(mcands); // rule 2
208
209 if (nextIdx == -1)
210 nextIdx = chooseByRule3(mcands); // rule 3
211
212 if (nextIdx == -1)
213 nextIdx = 0; // default to first choice by delays
214
215 // We have found the next best candidate. Check if it ready in
216 // the current cycle, and if it is feasible.
217 // If not, remove it from mcands and continue. Refill mcands if
218 // it becomes empty.
219 nextChoice = candsAsHeap.getNode(mcands[nextIdx]);
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000220 if (getEarliestReadyTimeForNode(nextChoice) > curTime
Chris Lattner15dedbc2001-09-07 21:22:28 +0000221 || ! instrIsFeasible(S, nextChoice->getMachineInstr()->getOpCode()))
Vikram S. Adve37866b32001-08-28 23:06:49 +0000222 {
223 mcands.erase(mcands.begin() + nextIdx);
224 nextIdx = -1;
225 if (mcands.size() == 0)
226 findSetWithMaxDelay(mcands, S);
227 }
228 }
229
230 if (nextIdx >= 0)
231 {
232 mcands.erase(mcands.begin() + nextIdx);
233 return nextChoice;
234 }
235 else
236 return NULL;
237}
238
239
240void
Chris Lattner697954c2002-01-20 22:54:45 +0000241SchedPriorities::findSetWithMaxDelay(std::vector<candIndex>& mcands,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000242 const SchedulingManager& S)
243{
244 if (mcands.size() == 0 && nextToTry != candsAsHeap.end())
245 { // out of choices at current maximum delay;
246 // put nodes with next highest delay in mcands
247 candIndex next = nextToTry;
248 cycles_t maxDelay = candsAsHeap.getDelay(next);
249 for (; next != candsAsHeap.end()
250 && candsAsHeap.getDelay(next) == maxDelay; ++next)
251 mcands.push_back(next);
252
253 nextToTry = next;
254
255 if (SchedDebugLevel >= Sched_PrintSchedTrace)
256 {
Chris Lattner697954c2002-01-20 22:54:45 +0000257 cerr << " Cycle " << (long)getTime() << ": "
258 << "Next highest delay = " << (long)maxDelay << " : "
Vikram S. Adve37866b32001-08-28 23:06:49 +0000259 << mcands.size() << " Nodes with this delay: ";
260 for (unsigned i=0; i < mcands.size(); i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000261 cerr << candsAsHeap.getNode(mcands[i])->getNodeId() << ", ";
262 cerr << "\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +0000263 }
264 }
265}
266
267
268bool
Chris Lattner483e14e2002-04-27 07:27:19 +0000269SchedPriorities::instructionHasLastUse(FunctionLiveVarInfo &LVI,
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000270 const SchedGraphNode* graphNode) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000271 const MachineInstr *MI = graphNode->getMachineInstr();
Vikram S. Adve37866b32001-08-28 23:06:49 +0000272
Chris Lattner697954c2002-01-20 22:54:45 +0000273 std::hash_map<const MachineInstr*, bool>::const_iterator
Chris Lattner2f898d22002-02-05 06:02:59 +0000274 ui = lastUseMap.find(MI);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000275 if (ui != lastUseMap.end())
Chris Lattner697954c2002-01-20 22:54:45 +0000276 return ui->second;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000277
278 // else check if instruction is a last use and save it in the hash_map
279 bool hasLastUse = false;
Vikram S. Adveaf00d482001-11-12 14:18:01 +0000280 const BasicBlock* bb = graphNode->getBB();
Chris Lattner483e14e2002-04-27 07:27:19 +0000281 const ValueSet &LVs = LVI.getLiveVarSetBeforeMInst(MI, bb);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000282
Chris Lattner2f898d22002-02-05 06:02:59 +0000283 for (MachineInstr::const_val_op_iterator OI = MI->begin(), OE = MI->end();
284 OI != OE; ++OI)
285 if (!LVs.count(*OI)) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000286 hasLastUse = true;
287 break;
288 }
Chris Lattner748697d2002-02-05 04:20:12 +0000289
Chris Lattner2f898d22002-02-05 06:02:59 +0000290 return lastUseMap[MI] = hasLastUse;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000291}
292