blob: aba49bd54c16e3f85565b4864d9aae433f2c3e04 [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
31 earliestForNode(G->getNumNodes(), 0),
Vikram S. Adve37866b32001-08-28 23:06:49 +000032 earliestReadyTime(0),
Chris Lattner9adb7ad2002-02-04 20:02:16 +000033 nextToTry(candsAsHeap.begin()) {
Vikram S. Adve37866b32001-08-28 23:06:49 +000034 computeDelays(graph);
35}
36
37
38void
39SchedPriorities::initialize()
40{
41 initializeReadyHeap(graph);
42}
43
44
45void
46SchedPriorities::computeDelays(const SchedGraph* graph)
47{
Chris Lattner3ff43872001-09-28 22:56:31 +000048 po_iterator<const SchedGraph*> poIter = po_begin(graph), poEnd =po_end(graph);
Vikram S. Adve37866b32001-08-28 23:06:49 +000049 for ( ; poIter != poEnd; ++poIter)
50 {
51 const SchedGraphNode* node = *poIter;
52 cycles_t nodeDelay;
53 if (node->beginOutEdges() == node->endOutEdges())
54 nodeDelay = node->getLatency();
55 else
56 {
57 // Iterate over the out-edges of the node to compute delay
58 nodeDelay = 0;
59 for (SchedGraphNode::const_iterator E=node->beginOutEdges();
60 E != node->endOutEdges(); ++E)
61 {
62 cycles_t sinkDelay = getNodeDelayRef((*E)->getSink());
Chris Lattner697954c2002-01-20 22:54:45 +000063 nodeDelay = std::max(nodeDelay, sinkDelay + (*E)->getMinDelay());
Vikram S. Adve37866b32001-08-28 23:06:49 +000064 }
65 }
66 getNodeDelayRef(node) = nodeDelay;
67 }
68}
69
70
71void
72SchedPriorities::initializeReadyHeap(const SchedGraph* graph)
73{
74 const SchedGraphNode* graphRoot = graph->getRoot();
75 assert(graphRoot->getMachineInstr() == NULL && "Expect dummy root");
76
77 // Insert immediate successors of dummy root, which are the actual roots
78 sg_succ_const_iterator SEnd = succ_end(graphRoot);
79 for (sg_succ_const_iterator S = succ_begin(graphRoot); S != SEnd; ++S)
80 this->insertReady(*S);
81
82#undef TEST_HEAP_CONVERSION
83#ifdef TEST_HEAP_CONVERSION
Chris Lattner697954c2002-01-20 22:54:45 +000084 cerr << "Before heap conversion:\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +000085 copy(candsAsHeap.begin(), candsAsHeap.end(),
Chris Lattner697954c2002-01-20 22:54:45 +000086 ostream_iterator<NodeDelayPair*>(cerr,"\n"));
Vikram S. Adve37866b32001-08-28 23:06:49 +000087#endif
88
89 candsAsHeap.makeHeap();
90
Vikram S. Adve1392d692002-03-24 03:46:15 +000091 nextToTry = candsAsHeap.begin();
92
Vikram S. Adve37866b32001-08-28 23:06:49 +000093#ifdef TEST_HEAP_CONVERSION
Chris Lattner697954c2002-01-20 22:54:45 +000094 cerr << "After heap conversion:\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +000095 copy(candsAsHeap.begin(), candsAsHeap.end(),
Chris Lattner697954c2002-01-20 22:54:45 +000096 ostream_iterator<NodeDelayPair*>(cerr,"\n"));
Vikram S. Adve37866b32001-08-28 23:06:49 +000097#endif
98}
99
Chris Lattner697954c2002-01-20 22:54:45 +0000100void
101SchedPriorities::insertReady(const SchedGraphNode* node)
102{
103 candsAsHeap.insert(node, nodeDelayVec[node->getNodeId()]);
104 candsAsSet.insert(node);
105 mcands.clear(); // ensure reset choices is called before any more choices
106 earliestReadyTime = std::min(earliestReadyTime,
107 earliestForNode[node->getNodeId()]);
108
109 if (SchedDebugLevel >= Sched_PrintSchedTrace)
110 {
Vikram S. Adve1392d692002-03-24 03:46:15 +0000111 cerr << " Node " << node->getNodeId() << " will be ready in Cycle "
112 << earliestForNode[node->getNodeId()] << "; "
113 << " Delay = " <<(long)getNodeDelayRef(node) << "; Instruction: \n";
Chris Lattner697954c2002-01-20 22:54:45 +0000114 cerr << " " << *node->getMachineInstr() << "\n";
115 }
116}
Vikram S. Adve37866b32001-08-28 23:06:49 +0000117
118void
119SchedPriorities::issuedReadyNodeAt(cycles_t curTime,
120 const SchedGraphNode* node)
121{
122 candsAsHeap.removeNode(node);
123 candsAsSet.erase(node);
124 mcands.clear(); // ensure reset choices is called before any more choices
125
126 if (earliestReadyTime == getEarliestForNodeRef(node))
127 {// earliestReadyTime may have been due to this node, so recompute it
128 earliestReadyTime = HUGE_LATENCY;
129 for (NodeHeap::const_iterator I=candsAsHeap.begin();
130 I != candsAsHeap.end(); ++I)
131 if (candsAsHeap.getNode(I))
Chris Lattner697954c2002-01-20 22:54:45 +0000132 earliestReadyTime = std::min(earliestReadyTime,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000133 getEarliestForNodeRef(candsAsHeap.getNode(I)));
134 }
135
136 // Now update ready times for successors
137 for (SchedGraphNode::const_iterator E=node->beginOutEdges();
138 E != node->endOutEdges(); ++E)
139 {
140 cycles_t& etime = getEarliestForNodeRef((*E)->getSink());
Chris Lattner697954c2002-01-20 22:54:45 +0000141 etime = std::max(etime, curTime + (*E)->getMinDelay());
Vikram S. Adve37866b32001-08-28 23:06:49 +0000142 }
143}
144
145
146//----------------------------------------------------------------------
147// Priority ordering rules:
148// (1) Max delay, which is the order of the heap S.candsAsHeap.
149// (2) Instruction that frees up a register.
150// (3) Instruction that has the maximum number of dependent instructions.
151// Note that rules 2 and 3 are only used if issue conflicts prevent
152// choosing a higher priority instruction by rule 1.
153//----------------------------------------------------------------------
154
155inline int
Chris Lattner697954c2002-01-20 22:54:45 +0000156SchedPriorities::chooseByRule1(std::vector<candIndex>& mcands)
Vikram S. Adve37866b32001-08-28 23:06:49 +0000157{
158 return (mcands.size() == 1)? 0 // only one choice exists so take it
159 : -1; // -1 indicates multiple choices
160}
161
162inline int
Chris Lattner697954c2002-01-20 22:54:45 +0000163SchedPriorities::chooseByRule2(std::vector<candIndex>& mcands)
Vikram S. Adve37866b32001-08-28 23:06:49 +0000164{
165 assert(mcands.size() >= 1 && "Should have at least one candidate here.");
166 for (unsigned i=0, N = mcands.size(); i < N; i++)
167 if (instructionHasLastUse(methodLiveVarInfo,
168 candsAsHeap.getNode(mcands[i])))
169 return i;
170 return -1;
171}
172
173inline int
Chris Lattner697954c2002-01-20 22:54:45 +0000174SchedPriorities::chooseByRule3(std::vector<candIndex>& mcands)
Vikram S. Adve37866b32001-08-28 23:06:49 +0000175{
176 assert(mcands.size() >= 1 && "Should have at least one candidate here.");
177 int maxUses = candsAsHeap.getNode(mcands[0])->getNumOutEdges();
178 int indexWithMaxUses = 0;
179 for (unsigned i=1, N = mcands.size(); i < N; i++)
180 {
181 int numUses = candsAsHeap.getNode(mcands[i])->getNumOutEdges();
182 if (numUses > maxUses)
183 {
184 maxUses = numUses;
185 indexWithMaxUses = i;
186 }
187 }
188 return indexWithMaxUses;
189}
190
191const SchedGraphNode*
192SchedPriorities::getNextHighest(const SchedulingManager& S,
193 cycles_t curTime)
194{
195 int nextIdx = -1;
196 const SchedGraphNode* nextChoice = NULL;
197
198 if (mcands.size() == 0)
199 findSetWithMaxDelay(mcands, S);
200
201 while (nextIdx < 0 && mcands.size() > 0)
202 {
203 nextIdx = chooseByRule1(mcands); // rule 1
204
205 if (nextIdx == -1)
206 nextIdx = chooseByRule2(mcands); // rule 2
207
208 if (nextIdx == -1)
209 nextIdx = chooseByRule3(mcands); // rule 3
210
211 if (nextIdx == -1)
212 nextIdx = 0; // default to first choice by delays
213
214 // We have found the next best candidate. Check if it ready in
215 // the current cycle, and if it is feasible.
216 // If not, remove it from mcands and continue. Refill mcands if
217 // it becomes empty.
218 nextChoice = candsAsHeap.getNode(mcands[nextIdx]);
219 if (getEarliestForNodeRef(nextChoice) > curTime
Chris Lattner15dedbc2001-09-07 21:22:28 +0000220 || ! instrIsFeasible(S, nextChoice->getMachineInstr()->getOpCode()))
Vikram S. Adve37866b32001-08-28 23:06:49 +0000221 {
222 mcands.erase(mcands.begin() + nextIdx);
223 nextIdx = -1;
224 if (mcands.size() == 0)
225 findSetWithMaxDelay(mcands, S);
226 }
227 }
228
229 if (nextIdx >= 0)
230 {
231 mcands.erase(mcands.begin() + nextIdx);
232 return nextChoice;
233 }
234 else
235 return NULL;
236}
237
238
239void
Chris Lattner697954c2002-01-20 22:54:45 +0000240SchedPriorities::findSetWithMaxDelay(std::vector<candIndex>& mcands,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000241 const SchedulingManager& S)
242{
243 if (mcands.size() == 0 && nextToTry != candsAsHeap.end())
244 { // out of choices at current maximum delay;
245 // put nodes with next highest delay in mcands
246 candIndex next = nextToTry;
247 cycles_t maxDelay = candsAsHeap.getDelay(next);
248 for (; next != candsAsHeap.end()
249 && candsAsHeap.getDelay(next) == maxDelay; ++next)
250 mcands.push_back(next);
251
252 nextToTry = next;
253
254 if (SchedDebugLevel >= Sched_PrintSchedTrace)
255 {
Chris Lattner697954c2002-01-20 22:54:45 +0000256 cerr << " Cycle " << (long)getTime() << ": "
257 << "Next highest delay = " << (long)maxDelay << " : "
Vikram S. Adve37866b32001-08-28 23:06:49 +0000258 << mcands.size() << " Nodes with this delay: ";
259 for (unsigned i=0; i < mcands.size(); i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000260 cerr << candsAsHeap.getNode(mcands[i])->getNodeId() << ", ";
261 cerr << "\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +0000262 }
263 }
264}
265
266
267bool
Chris Lattner483e14e2002-04-27 07:27:19 +0000268SchedPriorities::instructionHasLastUse(FunctionLiveVarInfo &LVI,
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000269 const SchedGraphNode* graphNode) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000270 const MachineInstr *MI = graphNode->getMachineInstr();
Vikram S. Adve37866b32001-08-28 23:06:49 +0000271
Chris Lattner697954c2002-01-20 22:54:45 +0000272 std::hash_map<const MachineInstr*, bool>::const_iterator
Chris Lattner2f898d22002-02-05 06:02:59 +0000273 ui = lastUseMap.find(MI);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000274 if (ui != lastUseMap.end())
Chris Lattner697954c2002-01-20 22:54:45 +0000275 return ui->second;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000276
277 // else check if instruction is a last use and save it in the hash_map
278 bool hasLastUse = false;
Vikram S. Adveaf00d482001-11-12 14:18:01 +0000279 const BasicBlock* bb = graphNode->getBB();
Chris Lattner483e14e2002-04-27 07:27:19 +0000280 const ValueSet &LVs = LVI.getLiveVarSetBeforeMInst(MI, bb);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000281
Chris Lattner2f898d22002-02-05 06:02:59 +0000282 for (MachineInstr::const_val_op_iterator OI = MI->begin(), OE = MI->end();
283 OI != OE; ++OI)
284 if (!LVs.count(*OI)) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000285 hasLastUse = true;
286 break;
287 }
Chris Lattner748697d2002-02-05 04:20:12 +0000288
Chris Lattner2f898d22002-02-05 06:02:59 +0000289 return lastUseMap[MI] = hasLastUse;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000290}
291