blob: ef9f9e459972bb2b01c3f2ce341926fd18a872fc [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 Lattner9adb7ad2002-02-04 20:02:16 +000022#include "llvm/Analysis/LiveVar/MethodLiveVarInfo.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 +000025#include <iostream>
26using std::cerr;
Vikram S. Adve37866b32001-08-28 23:06:49 +000027
Chris Lattner9adb7ad2002-02-04 20:02:16 +000028SchedPriorities::SchedPriorities(const Method *method, const SchedGraph *G,
29 MethodLiveVarInfo &LVI)
30 : curTime(0), graph(G), methodLiveVarInfo(LVI),
31 nodeDelayVec(G->getNumNodes(), INVALID_LATENCY), // make errors obvious
32 earliestForNode(G->getNumNodes(), 0),
Vikram S. Adve37866b32001-08-28 23:06:49 +000033 earliestReadyTime(0),
Chris Lattner9adb7ad2002-02-04 20:02:16 +000034 nextToTry(candsAsHeap.begin()) {
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())
55 nodeDelay = node->getLatency();
56 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 {
63 cycles_t sinkDelay = getNodeDelayRef((*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
92#ifdef TEST_HEAP_CONVERSION
Chris Lattner697954c2002-01-20 22:54:45 +000093 cerr << "After heap conversion:\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +000094 copy(candsAsHeap.begin(), candsAsHeap.end(),
Chris Lattner697954c2002-01-20 22:54:45 +000095 ostream_iterator<NodeDelayPair*>(cerr,"\n"));
Vikram S. Adve37866b32001-08-28 23:06:49 +000096#endif
97}
98
Chris Lattner697954c2002-01-20 22:54:45 +000099void
100SchedPriorities::insertReady(const SchedGraphNode* node)
101{
102 candsAsHeap.insert(node, nodeDelayVec[node->getNodeId()]);
103 candsAsSet.insert(node);
104 mcands.clear(); // ensure reset choices is called before any more choices
105 earliestReadyTime = std::min(earliestReadyTime,
106 earliestForNode[node->getNodeId()]);
107
108 if (SchedDebugLevel >= Sched_PrintSchedTrace)
109 {
110 cerr << " Cycle " << (long)getTime() << ": "
111 << " Node " << node->getNodeId() << " is ready; "
112 << " Delay = " << (long)getNodeDelayRef(node) << "; Instruction: \n";
113 cerr << " " << *node->getMachineInstr() << "\n";
114 }
115}
Vikram S. Adve37866b32001-08-28 23:06:49 +0000116
117void
118SchedPriorities::issuedReadyNodeAt(cycles_t curTime,
119 const SchedGraphNode* node)
120{
121 candsAsHeap.removeNode(node);
122 candsAsSet.erase(node);
123 mcands.clear(); // ensure reset choices is called before any more choices
124
125 if (earliestReadyTime == getEarliestForNodeRef(node))
126 {// earliestReadyTime may have been due to this node, so recompute it
127 earliestReadyTime = HUGE_LATENCY;
128 for (NodeHeap::const_iterator I=candsAsHeap.begin();
129 I != candsAsHeap.end(); ++I)
130 if (candsAsHeap.getNode(I))
Chris Lattner697954c2002-01-20 22:54:45 +0000131 earliestReadyTime = std::min(earliestReadyTime,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000132 getEarliestForNodeRef(candsAsHeap.getNode(I)));
133 }
134
135 // Now update ready times for successors
136 for (SchedGraphNode::const_iterator E=node->beginOutEdges();
137 E != node->endOutEdges(); ++E)
138 {
139 cycles_t& etime = getEarliestForNodeRef((*E)->getSink());
Chris Lattner697954c2002-01-20 22:54:45 +0000140 etime = std::max(etime, curTime + (*E)->getMinDelay());
Vikram S. Adve37866b32001-08-28 23:06:49 +0000141 }
142}
143
144
145//----------------------------------------------------------------------
146// Priority ordering rules:
147// (1) Max delay, which is the order of the heap S.candsAsHeap.
148// (2) Instruction that frees up a register.
149// (3) Instruction that has the maximum number of dependent instructions.
150// Note that rules 2 and 3 are only used if issue conflicts prevent
151// choosing a higher priority instruction by rule 1.
152//----------------------------------------------------------------------
153
154inline int
Chris Lattner697954c2002-01-20 22:54:45 +0000155SchedPriorities::chooseByRule1(std::vector<candIndex>& mcands)
Vikram S. Adve37866b32001-08-28 23:06:49 +0000156{
157 return (mcands.size() == 1)? 0 // only one choice exists so take it
158 : -1; // -1 indicates multiple choices
159}
160
161inline int
Chris Lattner697954c2002-01-20 22:54:45 +0000162SchedPriorities::chooseByRule2(std::vector<candIndex>& mcands)
Vikram S. Adve37866b32001-08-28 23:06:49 +0000163{
164 assert(mcands.size() >= 1 && "Should have at least one candidate here.");
165 for (unsigned i=0, N = mcands.size(); i < N; i++)
166 if (instructionHasLastUse(methodLiveVarInfo,
167 candsAsHeap.getNode(mcands[i])))
168 return i;
169 return -1;
170}
171
172inline int
Chris Lattner697954c2002-01-20 22:54:45 +0000173SchedPriorities::chooseByRule3(std::vector<candIndex>& mcands)
Vikram S. Adve37866b32001-08-28 23:06:49 +0000174{
175 assert(mcands.size() >= 1 && "Should have at least one candidate here.");
176 int maxUses = candsAsHeap.getNode(mcands[0])->getNumOutEdges();
177 int indexWithMaxUses = 0;
178 for (unsigned i=1, N = mcands.size(); i < N; i++)
179 {
180 int numUses = candsAsHeap.getNode(mcands[i])->getNumOutEdges();
181 if (numUses > maxUses)
182 {
183 maxUses = numUses;
184 indexWithMaxUses = i;
185 }
186 }
187 return indexWithMaxUses;
188}
189
190const SchedGraphNode*
191SchedPriorities::getNextHighest(const SchedulingManager& S,
192 cycles_t curTime)
193{
194 int nextIdx = -1;
195 const SchedGraphNode* nextChoice = NULL;
196
197 if (mcands.size() == 0)
198 findSetWithMaxDelay(mcands, S);
199
200 while (nextIdx < 0 && mcands.size() > 0)
201 {
202 nextIdx = chooseByRule1(mcands); // rule 1
203
204 if (nextIdx == -1)
205 nextIdx = chooseByRule2(mcands); // rule 2
206
207 if (nextIdx == -1)
208 nextIdx = chooseByRule3(mcands); // rule 3
209
210 if (nextIdx == -1)
211 nextIdx = 0; // default to first choice by delays
212
213 // We have found the next best candidate. Check if it ready in
214 // the current cycle, and if it is feasible.
215 // If not, remove it from mcands and continue. Refill mcands if
216 // it becomes empty.
217 nextChoice = candsAsHeap.getNode(mcands[nextIdx]);
218 if (getEarliestForNodeRef(nextChoice) > curTime
Chris Lattner15dedbc2001-09-07 21:22:28 +0000219 || ! instrIsFeasible(S, nextChoice->getMachineInstr()->getOpCode()))
Vikram S. Adve37866b32001-08-28 23:06:49 +0000220 {
221 mcands.erase(mcands.begin() + nextIdx);
222 nextIdx = -1;
223 if (mcands.size() == 0)
224 findSetWithMaxDelay(mcands, S);
225 }
226 }
227
228 if (nextIdx >= 0)
229 {
230 mcands.erase(mcands.begin() + nextIdx);
231 return nextChoice;
232 }
233 else
234 return NULL;
235}
236
237
238void
Chris Lattner697954c2002-01-20 22:54:45 +0000239SchedPriorities::findSetWithMaxDelay(std::vector<candIndex>& mcands,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000240 const SchedulingManager& S)
241{
242 if (mcands.size() == 0 && nextToTry != candsAsHeap.end())
243 { // out of choices at current maximum delay;
244 // put nodes with next highest delay in mcands
245 candIndex next = nextToTry;
246 cycles_t maxDelay = candsAsHeap.getDelay(next);
247 for (; next != candsAsHeap.end()
248 && candsAsHeap.getDelay(next) == maxDelay; ++next)
249 mcands.push_back(next);
250
251 nextToTry = next;
252
253 if (SchedDebugLevel >= Sched_PrintSchedTrace)
254 {
Chris Lattner697954c2002-01-20 22:54:45 +0000255 cerr << " Cycle " << (long)getTime() << ": "
256 << "Next highest delay = " << (long)maxDelay << " : "
Vikram S. Adve37866b32001-08-28 23:06:49 +0000257 << mcands.size() << " Nodes with this delay: ";
258 for (unsigned i=0; i < mcands.size(); i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000259 cerr << candsAsHeap.getNode(mcands[i])->getNodeId() << ", ";
260 cerr << "\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +0000261 }
262 }
263}
264
265
266bool
267SchedPriorities::instructionHasLastUse(MethodLiveVarInfo& methodLiveVarInfo,
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000268 const SchedGraphNode* graphNode) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000269 const MachineInstr *MI = graphNode->getMachineInstr();
Vikram S. Adve37866b32001-08-28 23:06:49 +0000270
Chris Lattner697954c2002-01-20 22:54:45 +0000271 std::hash_map<const MachineInstr*, bool>::const_iterator
Chris Lattner2f898d22002-02-05 06:02:59 +0000272 ui = lastUseMap.find(MI);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000273 if (ui != lastUseMap.end())
Chris Lattner697954c2002-01-20 22:54:45 +0000274 return ui->second;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000275
276 // else check if instruction is a last use and save it in the hash_map
277 bool hasLastUse = false;
Vikram S. Adveaf00d482001-11-12 14:18:01 +0000278 const BasicBlock* bb = graphNode->getBB();
Chris Lattner2f898d22002-02-05 06:02:59 +0000279 const ValueSet &LVs = methodLiveVarInfo.getLiveVarSetBeforeMInst(MI, bb);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000280
Chris Lattner2f898d22002-02-05 06:02:59 +0000281 for (MachineInstr::const_val_op_iterator OI = MI->begin(), OE = MI->end();
282 OI != OE; ++OI)
283 if (!LVs.count(*OI)) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000284 hasLastUse = true;
285 break;
286 }
Chris Lattner748697d2002-02-05 04:20:12 +0000287
Chris Lattner2f898d22002-02-05 06:02:59 +0000288 return lastUseMap[MI] = hasLastUse;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000289}
290