blob: a35600c0f0e16383731bb638e26b5a757075747f [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//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Vikram S. Advefe30f1f2001-09-18 12:52:03 +000010// Strategy:
11// Priority ordering rules:
12// (1) Max delay, which is the order of the heap S.candsAsHeap.
13// (2) Instruction that frees up a register.
14// (3) Instruction that has the maximum number of dependent instructions.
15// Note that rules 2 and 3 are only used if issue conflicts prevent
16// choosing a higher priority instruction by rule 1.
Chris Lattner179cdfb2002-08-09 20:08:03 +000017//
18//===----------------------------------------------------------------------===//
Vikram S. Adve37866b32001-08-28 23:06:49 +000019
Chris Lattner46cbff62001-09-14 16:56:32 +000020#include "SchedPriorities.h"
Chris Lattner92ba2aa2003-01-14 23:05:08 +000021#include "llvm/CodeGen/FunctionLiveVarInfo.h"
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +000022#include "llvm/CodeGen/MachineBasicBlock.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 Lattner5da2e6a2002-11-02 22:07:51 +000027std::ostream &operator<<(std::ostream &os, const NodeDelayPair* nd) {
28 return os << "Delay for node " << nd->node->getNodeId()
29 << " = " << (long)nd->delay << "\n";
30}
31
32
Chris Lattnerb7653df2002-04-08 22:03:57 +000033SchedPriorities::SchedPriorities(const Function *, const SchedGraph *G,
Chris Lattner483e14e2002-04-27 07:27:19 +000034 FunctionLiveVarInfo &LVI)
Chris Lattner9adb7ad2002-02-04 20:02:16 +000035 : curTime(0), graph(G), methodLiveVarInfo(LVI),
36 nodeDelayVec(G->getNumNodes(), INVALID_LATENCY), // make errors obvious
Vikram S. Adve0baf1c02002-07-08 22:59:23 +000037 earliestReadyTimeForNode(G->getNumNodes(), 0),
Vikram S. Adve37866b32001-08-28 23:06:49 +000038 earliestReadyTime(0),
Vikram S. Adve0baf1c02002-07-08 22:59:23 +000039 nextToTry(candsAsHeap.begin())
40{
Vikram S. Adve37866b32001-08-28 23:06:49 +000041 computeDelays(graph);
42}
43
44
45void
46SchedPriorities::initialize()
47{
48 initializeReadyHeap(graph);
49}
50
51
52void
53SchedPriorities::computeDelays(const SchedGraph* graph)
54{
Chris Lattner3ff43872001-09-28 22:56:31 +000055 po_iterator<const SchedGraph*> poIter = po_begin(graph), poEnd =po_end(graph);
Vikram S. Adve37866b32001-08-28 23:06:49 +000056 for ( ; poIter != poEnd; ++poIter)
57 {
58 const SchedGraphNode* node = *poIter;
59 cycles_t nodeDelay;
60 if (node->beginOutEdges() == node->endOutEdges())
Vikram S. Adve0baf1c02002-07-08 22:59:23 +000061 nodeDelay = node->getLatency();
Vikram S. Adve37866b32001-08-28 23:06:49 +000062 else
63 {
64 // Iterate over the out-edges of the node to compute delay
65 nodeDelay = 0;
66 for (SchedGraphNode::const_iterator E=node->beginOutEdges();
67 E != node->endOutEdges(); ++E)
68 {
Tanya Lattnerb6489f32003-08-25 22:42:20 +000069 cycles_t sinkDelay = getNodeDelay((SchedGraphNode*)(*E)->getSink());
Chris Lattner697954c2002-01-20 22:54:45 +000070 nodeDelay = std::max(nodeDelay, sinkDelay + (*E)->getMinDelay());
Vikram S. Adve37866b32001-08-28 23:06:49 +000071 }
72 }
73 getNodeDelayRef(node) = nodeDelay;
74 }
75}
76
77
78void
79SchedPriorities::initializeReadyHeap(const SchedGraph* graph)
80{
Tanya Lattnerb6489f32003-08-25 22:42:20 +000081 const SchedGraphNode* graphRoot = (const SchedGraphNode*)graph->getRoot();
Vikram S. Adve37866b32001-08-28 23:06:49 +000082 assert(graphRoot->getMachineInstr() == NULL && "Expect dummy root");
83
84 // Insert immediate successors of dummy root, which are the actual roots
85 sg_succ_const_iterator SEnd = succ_end(graphRoot);
86 for (sg_succ_const_iterator S = succ_begin(graphRoot); S != SEnd; ++S)
87 this->insertReady(*S);
88
89#undef TEST_HEAP_CONVERSION
90#ifdef TEST_HEAP_CONVERSION
Chris Lattner697954c2002-01-20 22:54:45 +000091 cerr << "Before heap conversion:\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +000092 copy(candsAsHeap.begin(), candsAsHeap.end(),
Chris Lattner697954c2002-01-20 22:54:45 +000093 ostream_iterator<NodeDelayPair*>(cerr,"\n"));
Vikram S. Adve37866b32001-08-28 23:06:49 +000094#endif
95
96 candsAsHeap.makeHeap();
97
Vikram S. Adve1392d692002-03-24 03:46:15 +000098 nextToTry = candsAsHeap.begin();
99
Vikram S. Adve37866b32001-08-28 23:06:49 +0000100#ifdef TEST_HEAP_CONVERSION
Chris Lattner697954c2002-01-20 22:54:45 +0000101 cerr << "After heap conversion:\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +0000102 copy(candsAsHeap.begin(), candsAsHeap.end(),
Chris Lattner697954c2002-01-20 22:54:45 +0000103 ostream_iterator<NodeDelayPair*>(cerr,"\n"));
Vikram S. Adve37866b32001-08-28 23:06:49 +0000104#endif
105}
106
Chris Lattner697954c2002-01-20 22:54:45 +0000107void
108SchedPriorities::insertReady(const SchedGraphNode* node)
109{
110 candsAsHeap.insert(node, nodeDelayVec[node->getNodeId()]);
111 candsAsSet.insert(node);
112 mcands.clear(); // ensure reset choices is called before any more choices
113 earliestReadyTime = std::min(earliestReadyTime,
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000114 getEarliestReadyTimeForNode(node));
Chris Lattner697954c2002-01-20 22:54:45 +0000115
116 if (SchedDebugLevel >= Sched_PrintSchedTrace)
117 {
Vikram S. Adve1392d692002-03-24 03:46:15 +0000118 cerr << " Node " << node->getNodeId() << " will be ready in Cycle "
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000119 << getEarliestReadyTimeForNode(node) << "; "
120 << " Delay = " <<(long)getNodeDelay(node) << "; Instruction: \n";
Chris Lattner697954c2002-01-20 22:54:45 +0000121 cerr << " " << *node->getMachineInstr() << "\n";
122 }
123}
Vikram S. Adve37866b32001-08-28 23:06:49 +0000124
125void
126SchedPriorities::issuedReadyNodeAt(cycles_t curTime,
127 const SchedGraphNode* node)
128{
129 candsAsHeap.removeNode(node);
130 candsAsSet.erase(node);
131 mcands.clear(); // ensure reset choices is called before any more choices
132
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000133 if (earliestReadyTime == getEarliestReadyTimeForNode(node))
Vikram S. Adve37866b32001-08-28 23:06:49 +0000134 {// earliestReadyTime may have been due to this node, so recompute it
135 earliestReadyTime = HUGE_LATENCY;
136 for (NodeHeap::const_iterator I=candsAsHeap.begin();
137 I != candsAsHeap.end(); ++I)
138 if (candsAsHeap.getNode(I))
Chris Lattner697954c2002-01-20 22:54:45 +0000139 earliestReadyTime = std::min(earliestReadyTime,
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000140 getEarliestReadyTimeForNode(candsAsHeap.getNode(I)));
Vikram S. Adve37866b32001-08-28 23:06:49 +0000141 }
142
143 // Now update ready times for successors
144 for (SchedGraphNode::const_iterator E=node->beginOutEdges();
145 E != node->endOutEdges(); ++E)
146 {
Tanya Lattnerb6489f32003-08-25 22:42:20 +0000147 cycles_t& etime = getEarliestReadyTimeForNodeRef((SchedGraphNode*)(*E)->getSink());
Chris Lattner697954c2002-01-20 22:54:45 +0000148 etime = std::max(etime, curTime + (*E)->getMinDelay());
Vikram S. Adve37866b32001-08-28 23:06:49 +0000149 }
150}
151
152
153//----------------------------------------------------------------------
154// Priority ordering rules:
155// (1) Max delay, which is the order of the heap S.candsAsHeap.
156// (2) Instruction that frees up a register.
157// (3) Instruction that has the maximum number of dependent instructions.
158// Note that rules 2 and 3 are only used if issue conflicts prevent
159// choosing a higher priority instruction by rule 1.
160//----------------------------------------------------------------------
161
162inline int
Chris Lattner697954c2002-01-20 22:54:45 +0000163SchedPriorities::chooseByRule1(std::vector<candIndex>& mcands)
Vikram S. Adve37866b32001-08-28 23:06:49 +0000164{
165 return (mcands.size() == 1)? 0 // only one choice exists so take it
166 : -1; // -1 indicates multiple choices
167}
168
169inline int
Chris Lattner697954c2002-01-20 22:54:45 +0000170SchedPriorities::chooseByRule2(std::vector<candIndex>& mcands)
Vikram S. Adve37866b32001-08-28 23:06:49 +0000171{
172 assert(mcands.size() >= 1 && "Should have at least one candidate here.");
173 for (unsigned i=0, N = mcands.size(); i < N; i++)
174 if (instructionHasLastUse(methodLiveVarInfo,
175 candsAsHeap.getNode(mcands[i])))
176 return i;
177 return -1;
178}
179
180inline int
Chris Lattner697954c2002-01-20 22:54:45 +0000181SchedPriorities::chooseByRule3(std::vector<candIndex>& mcands)
Vikram S. Adve37866b32001-08-28 23:06:49 +0000182{
183 assert(mcands.size() >= 1 && "Should have at least one candidate here.");
184 int maxUses = candsAsHeap.getNode(mcands[0])->getNumOutEdges();
185 int indexWithMaxUses = 0;
186 for (unsigned i=1, N = mcands.size(); i < N; i++)
187 {
188 int numUses = candsAsHeap.getNode(mcands[i])->getNumOutEdges();
189 if (numUses > maxUses)
190 {
191 maxUses = numUses;
192 indexWithMaxUses = i;
193 }
194 }
195 return indexWithMaxUses;
196}
197
198const SchedGraphNode*
199SchedPriorities::getNextHighest(const SchedulingManager& S,
200 cycles_t curTime)
201{
202 int nextIdx = -1;
203 const SchedGraphNode* nextChoice = NULL;
204
205 if (mcands.size() == 0)
206 findSetWithMaxDelay(mcands, S);
207
208 while (nextIdx < 0 && mcands.size() > 0)
209 {
210 nextIdx = chooseByRule1(mcands); // rule 1
211
212 if (nextIdx == -1)
213 nextIdx = chooseByRule2(mcands); // rule 2
214
215 if (nextIdx == -1)
216 nextIdx = chooseByRule3(mcands); // rule 3
217
218 if (nextIdx == -1)
219 nextIdx = 0; // default to first choice by delays
220
221 // We have found the next best candidate. Check if it ready in
222 // the current cycle, and if it is feasible.
223 // If not, remove it from mcands and continue. Refill mcands if
224 // it becomes empty.
225 nextChoice = candsAsHeap.getNode(mcands[nextIdx]);
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000226 if (getEarliestReadyTimeForNode(nextChoice) > curTime
Chris Lattner15dedbc2001-09-07 21:22:28 +0000227 || ! instrIsFeasible(S, nextChoice->getMachineInstr()->getOpCode()))
Vikram S. Adve37866b32001-08-28 23:06:49 +0000228 {
229 mcands.erase(mcands.begin() + nextIdx);
230 nextIdx = -1;
231 if (mcands.size() == 0)
232 findSetWithMaxDelay(mcands, S);
233 }
234 }
235
236 if (nextIdx >= 0)
237 {
238 mcands.erase(mcands.begin() + nextIdx);
239 return nextChoice;
240 }
241 else
242 return NULL;
243}
244
245
246void
Chris Lattner697954c2002-01-20 22:54:45 +0000247SchedPriorities::findSetWithMaxDelay(std::vector<candIndex>& mcands,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000248 const SchedulingManager& S)
249{
250 if (mcands.size() == 0 && nextToTry != candsAsHeap.end())
251 { // out of choices at current maximum delay;
252 // put nodes with next highest delay in mcands
253 candIndex next = nextToTry;
254 cycles_t maxDelay = candsAsHeap.getDelay(next);
255 for (; next != candsAsHeap.end()
256 && candsAsHeap.getDelay(next) == maxDelay; ++next)
257 mcands.push_back(next);
258
259 nextToTry = next;
260
261 if (SchedDebugLevel >= Sched_PrintSchedTrace)
262 {
Chris Lattner697954c2002-01-20 22:54:45 +0000263 cerr << " Cycle " << (long)getTime() << ": "
264 << "Next highest delay = " << (long)maxDelay << " : "
Vikram S. Adve37866b32001-08-28 23:06:49 +0000265 << mcands.size() << " Nodes with this delay: ";
266 for (unsigned i=0; i < mcands.size(); i++)
Chris Lattner697954c2002-01-20 22:54:45 +0000267 cerr << candsAsHeap.getNode(mcands[i])->getNodeId() << ", ";
268 cerr << "\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +0000269 }
270 }
271}
272
273
274bool
Chris Lattner483e14e2002-04-27 07:27:19 +0000275SchedPriorities::instructionHasLastUse(FunctionLiveVarInfo &LVI,
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000276 const SchedGraphNode* graphNode) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000277 const MachineInstr *MI = graphNode->getMachineInstr();
Vikram S. Adve37866b32001-08-28 23:06:49 +0000278
Chris Lattner09ff1122002-07-24 21:21:32 +0000279 hash_map<const MachineInstr*, bool>::const_iterator
Chris Lattner2f898d22002-02-05 06:02:59 +0000280 ui = lastUseMap.find(MI);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000281 if (ui != lastUseMap.end())
Chris Lattner697954c2002-01-20 22:54:45 +0000282 return ui->second;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000283
284 // else check if instruction is a last use and save it in the hash_map
285 bool hasLastUse = false;
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000286 const BasicBlock* bb = graphNode->getMachineBasicBlock().getBasicBlock();
Chris Lattner483e14e2002-04-27 07:27:19 +0000287 const ValueSet &LVs = LVI.getLiveVarSetBeforeMInst(MI, bb);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000288
Chris Lattner2f898d22002-02-05 06:02:59 +0000289 for (MachineInstr::const_val_op_iterator OI = MI->begin(), OE = MI->end();
290 OI != OE; ++OI)
291 if (!LVs.count(*OI)) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000292 hasLastUse = true;
293 break;
294 }
Chris Lattner748697d2002-02-05 04:20:12 +0000295
Chris Lattner2f898d22002-02-05 06:02:59 +0000296 return lastUseMap[MI] = hasLastUse;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000297}
298