blob: 33aae5c7f52920276ff2bf20a2709630c53bafad [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 Lattnerfb3a0aed2002-10-28 18:50:08 +000015#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner221d6882002-02-12 21:07:25 +000016#include "llvm/Support/CFG.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000017#include "Support/PostOrderIterator.h"
Chris Lattner697954c2002-01-20 22:54:45 +000018using std::cerr;
Vikram S. Adve37866b32001-08-28 23:06:49 +000019
Chris Lattnerb7653df2002-04-08 22:03:57 +000020SchedPriorities::SchedPriorities(const Function *, const SchedGraph *G,
Chris Lattner483e14e2002-04-27 07:27:19 +000021 FunctionLiveVarInfo &LVI)
Chris Lattner9adb7ad2002-02-04 20:02:16 +000022 : curTime(0), graph(G), methodLiveVarInfo(LVI),
23 nodeDelayVec(G->getNumNodes(), INVALID_LATENCY), // make errors obvious
Vikram S. Adve0baf1c02002-07-08 22:59:23 +000024 earliestReadyTimeForNode(G->getNumNodes(), 0),
Vikram S. Adve37866b32001-08-28 23:06:49 +000025 earliestReadyTime(0),
Vikram S. Adve0baf1c02002-07-08 22:59:23 +000026 nextToTry(candsAsHeap.begin())
27{
Vikram S. Adve37866b32001-08-28 23:06:49 +000028 computeDelays(graph);
29}
30
31
32void
33SchedPriorities::initialize()
34{
35 initializeReadyHeap(graph);
36}
37
38
39void
40SchedPriorities::computeDelays(const SchedGraph* graph)
41{
Chris Lattner3ff43872001-09-28 22:56:31 +000042 po_iterator<const SchedGraph*> poIter = po_begin(graph), poEnd =po_end(graph);
Vikram S. Adve37866b32001-08-28 23:06:49 +000043 for ( ; poIter != poEnd; ++poIter)
44 {
45 const SchedGraphNode* node = *poIter;
46 cycles_t nodeDelay;
47 if (node->beginOutEdges() == node->endOutEdges())
Vikram S. Adve0baf1c02002-07-08 22:59:23 +000048 nodeDelay = node->getLatency();
Vikram S. Adve37866b32001-08-28 23:06:49 +000049 else
50 {
51 // Iterate over the out-edges of the node to compute delay
52 nodeDelay = 0;
53 for (SchedGraphNode::const_iterator E=node->beginOutEdges();
54 E != node->endOutEdges(); ++E)
55 {
Vikram S. Adve0baf1c02002-07-08 22:59:23 +000056 cycles_t sinkDelay = getNodeDelay((*E)->getSink());
Chris Lattner697954c2002-01-20 22:54:45 +000057 nodeDelay = std::max(nodeDelay, sinkDelay + (*E)->getMinDelay());
Vikram S. Adve37866b32001-08-28 23:06:49 +000058 }
59 }
60 getNodeDelayRef(node) = nodeDelay;
61 }
62}
63
64
65void
66SchedPriorities::initializeReadyHeap(const SchedGraph* graph)
67{
68 const SchedGraphNode* graphRoot = graph->getRoot();
69 assert(graphRoot->getMachineInstr() == NULL && "Expect dummy root");
70
71 // Insert immediate successors of dummy root, which are the actual roots
72 sg_succ_const_iterator SEnd = succ_end(graphRoot);
73 for (sg_succ_const_iterator S = succ_begin(graphRoot); S != SEnd; ++S)
74 this->insertReady(*S);
75
76#undef TEST_HEAP_CONVERSION
77#ifdef TEST_HEAP_CONVERSION
Chris Lattner697954c2002-01-20 22:54:45 +000078 cerr << "Before heap conversion:\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +000079 copy(candsAsHeap.begin(), candsAsHeap.end(),
Chris Lattner697954c2002-01-20 22:54:45 +000080 ostream_iterator<NodeDelayPair*>(cerr,"\n"));
Vikram S. Adve37866b32001-08-28 23:06:49 +000081#endif
82
83 candsAsHeap.makeHeap();
84
Vikram S. Adve1392d692002-03-24 03:46:15 +000085 nextToTry = candsAsHeap.begin();
86
Vikram S. Adve37866b32001-08-28 23:06:49 +000087#ifdef TEST_HEAP_CONVERSION
Chris Lattner697954c2002-01-20 22:54:45 +000088 cerr << "After heap conversion:\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +000089 copy(candsAsHeap.begin(), candsAsHeap.end(),
Chris Lattner697954c2002-01-20 22:54:45 +000090 ostream_iterator<NodeDelayPair*>(cerr,"\n"));
Vikram S. Adve37866b32001-08-28 23:06:49 +000091#endif
92}
93
Chris Lattner697954c2002-01-20 22:54:45 +000094void
95SchedPriorities::insertReady(const SchedGraphNode* node)
96{
97 candsAsHeap.insert(node, nodeDelayVec[node->getNodeId()]);
98 candsAsSet.insert(node);
99 mcands.clear(); // ensure reset choices is called before any more choices
100 earliestReadyTime = std::min(earliestReadyTime,
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000101 getEarliestReadyTimeForNode(node));
Chris Lattner697954c2002-01-20 22:54:45 +0000102
103 if (SchedDebugLevel >= Sched_PrintSchedTrace)
104 {
Vikram S. Adve1392d692002-03-24 03:46:15 +0000105 cerr << " Node " << node->getNodeId() << " will be ready in Cycle "
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000106 << getEarliestReadyTimeForNode(node) << "; "
107 << " Delay = " <<(long)getNodeDelay(node) << "; Instruction: \n";
Chris Lattner697954c2002-01-20 22:54:45 +0000108 cerr << " " << *node->getMachineInstr() << "\n";
109 }
110}
Vikram S. Adve37866b32001-08-28 23:06:49 +0000111
112void
113SchedPriorities::issuedReadyNodeAt(cycles_t curTime,
114 const SchedGraphNode* node)
115{
116 candsAsHeap.removeNode(node);
117 candsAsSet.erase(node);
118 mcands.clear(); // ensure reset choices is called before any more choices
119
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000120 if (earliestReadyTime == getEarliestReadyTimeForNode(node))
Vikram S. Adve37866b32001-08-28 23:06:49 +0000121 {// earliestReadyTime may have been due to this node, so recompute it
122 earliestReadyTime = HUGE_LATENCY;
123 for (NodeHeap::const_iterator I=candsAsHeap.begin();
124 I != candsAsHeap.end(); ++I)
125 if (candsAsHeap.getNode(I))
Chris Lattner697954c2002-01-20 22:54:45 +0000126 earliestReadyTime = std::min(earliestReadyTime,
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000127 getEarliestReadyTimeForNode(candsAsHeap.getNode(I)));
Vikram S. Adve37866b32001-08-28 23:06:49 +0000128 }
129
130 // Now update ready times for successors
131 for (SchedGraphNode::const_iterator E=node->beginOutEdges();
132 E != node->endOutEdges(); ++E)
133 {
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000134 cycles_t& etime = getEarliestReadyTimeForNodeRef((*E)->getSink());
Chris Lattner697954c2002-01-20 22:54:45 +0000135 etime = std::max(etime, curTime + (*E)->getMinDelay());
Vikram S. Adve37866b32001-08-28 23:06:49 +0000136 }
137}
138
139
140//----------------------------------------------------------------------
141// Priority ordering rules:
142// (1) Max delay, which is the order of the heap S.candsAsHeap.
143// (2) Instruction that frees up a register.
144// (3) Instruction that has the maximum number of dependent instructions.
145// Note that rules 2 and 3 are only used if issue conflicts prevent
146// choosing a higher priority instruction by rule 1.
147//----------------------------------------------------------------------
148
149inline int
Chris Lattner697954c2002-01-20 22:54:45 +0000150SchedPriorities::chooseByRule1(std::vector<candIndex>& mcands)
Vikram S. Adve37866b32001-08-28 23:06:49 +0000151{
152 return (mcands.size() == 1)? 0 // only one choice exists so take it
153 : -1; // -1 indicates multiple choices
154}
155
156inline int
Chris Lattner697954c2002-01-20 22:54:45 +0000157SchedPriorities::chooseByRule2(std::vector<candIndex>& mcands)
Vikram S. Adve37866b32001-08-28 23:06:49 +0000158{
159 assert(mcands.size() >= 1 && "Should have at least one candidate here.");
160 for (unsigned i=0, N = mcands.size(); i < N; i++)
161 if (instructionHasLastUse(methodLiveVarInfo,
162 candsAsHeap.getNode(mcands[i])))
163 return i;
164 return -1;
165}
166
167inline int
Chris Lattner697954c2002-01-20 22:54:45 +0000168SchedPriorities::chooseByRule3(std::vector<candIndex>& mcands)
Vikram S. Adve37866b32001-08-28 23:06:49 +0000169{
170 assert(mcands.size() >= 1 && "Should have at least one candidate here.");
171 int maxUses = candsAsHeap.getNode(mcands[0])->getNumOutEdges();
172 int indexWithMaxUses = 0;
173 for (unsigned i=1, N = mcands.size(); i < N; i++)
174 {
175 int numUses = candsAsHeap.getNode(mcands[i])->getNumOutEdges();
176 if (numUses > maxUses)
177 {
178 maxUses = numUses;
179 indexWithMaxUses = i;
180 }
181 }
182 return indexWithMaxUses;
183}
184
185const SchedGraphNode*
186SchedPriorities::getNextHighest(const SchedulingManager& S,
187 cycles_t curTime)
188{
189 int nextIdx = -1;
190 const SchedGraphNode* nextChoice = NULL;
191
192 if (mcands.size() == 0)
193 findSetWithMaxDelay(mcands, S);
194
195 while (nextIdx < 0 && mcands.size() > 0)
196 {
197 nextIdx = chooseByRule1(mcands); // rule 1
198
199 if (nextIdx == -1)
200 nextIdx = chooseByRule2(mcands); // rule 2
201
202 if (nextIdx == -1)
203 nextIdx = chooseByRule3(mcands); // rule 3
204
205 if (nextIdx == -1)
206 nextIdx = 0; // default to first choice by delays
207
208 // We have found the next best candidate. Check if it ready in
209 // the current cycle, and if it is feasible.
210 // If not, remove it from mcands and continue. Refill mcands if
211 // it becomes empty.
212 nextChoice = candsAsHeap.getNode(mcands[nextIdx]);
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000213 if (getEarliestReadyTimeForNode(nextChoice) > curTime
Chris Lattner15dedbc2001-09-07 21:22:28 +0000214 || ! instrIsFeasible(S, nextChoice->getMachineInstr()->getOpCode()))
Vikram S. Adve37866b32001-08-28 23:06:49 +0000215 {
216 mcands.erase(mcands.begin() + nextIdx);
217 nextIdx = -1;
218 if (mcands.size() == 0)
219 findSetWithMaxDelay(mcands, S);
220 }
221 }
222
223 if (nextIdx >= 0)
224 {
225 mcands.erase(mcands.begin() + nextIdx);
226 return nextChoice;
227 }
228 else
229 return NULL;
230}
231
232
233void
Chris Lattner697954c2002-01-20 22:54:45 +0000234SchedPriorities::findSetWithMaxDelay(std::vector<candIndex>& mcands,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000235 const SchedulingManager& S)
236{
237 if (mcands.size() == 0 && nextToTry != candsAsHeap.end())
238 { // out of choices at current maximum delay;
239 // put nodes with next highest delay in mcands
240 candIndex next = nextToTry;
241 cycles_t maxDelay = candsAsHeap.getDelay(next);
242 for (; next != candsAsHeap.end()
243 && candsAsHeap.getDelay(next) == maxDelay; ++next)
244 mcands.push_back(next);
245
246 nextToTry = next;
247
248 if (SchedDebugLevel >= Sched_PrintSchedTrace)
249 {
Chris Lattner697954c2002-01-20 22:54:45 +0000250 cerr << " Cycle " << (long)getTime() << ": "
251 << "Next highest delay = " << (long)maxDelay << " : "
Vikram S. Adve37866b32001-08-28 23:06:49 +0000252 << mcands.size() << " Nodes with this delay: ";
253 for (unsigned i=0; i < mcands.size(); i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000254 cerr << candsAsHeap.getNode(mcands[i])->getNodeId() << ", ";
255 cerr << "\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +0000256 }
257 }
258}
259
260
261bool
Chris Lattner483e14e2002-04-27 07:27:19 +0000262SchedPriorities::instructionHasLastUse(FunctionLiveVarInfo &LVI,
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000263 const SchedGraphNode* graphNode) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000264 const MachineInstr *MI = graphNode->getMachineInstr();
Vikram S. Adve37866b32001-08-28 23:06:49 +0000265
Chris Lattner09ff1122002-07-24 21:21:32 +0000266 hash_map<const MachineInstr*, bool>::const_iterator
Chris Lattner2f898d22002-02-05 06:02:59 +0000267 ui = lastUseMap.find(MI);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000268 if (ui != lastUseMap.end())
Chris Lattner697954c2002-01-20 22:54:45 +0000269 return ui->second;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000270
271 // else check if instruction is a last use and save it in the hash_map
272 bool hasLastUse = false;
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000273 const BasicBlock* bb = graphNode->getMachineBasicBlock().getBasicBlock();
Chris Lattner483e14e2002-04-27 07:27:19 +0000274 const ValueSet &LVs = LVI.getLiveVarSetBeforeMInst(MI, bb);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000275
Chris Lattner2f898d22002-02-05 06:02:59 +0000276 for (MachineInstr::const_val_op_iterator OI = MI->begin(), OE = MI->end();
277 OI != OE; ++OI)
278 if (!LVs.count(*OI)) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000279 hasLastUse = true;
280 break;
281 }
Chris Lattner748697d2002-02-05 04:20:12 +0000282
Chris Lattner2f898d22002-02-05 06:02:59 +0000283 return lastUseMap[MI] = hasLastUse;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000284}
285