blob: 1644d5ecbc58a96dcf55af9a39768c8492a59459 [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"
Vikram S. Adve37866b32001-08-28 23:06:49 +000025
Chris Lattner5da2e6a2002-11-02 22:07:51 +000026std::ostream &operator<<(std::ostream &os, const NodeDelayPair* nd) {
27 return os << "Delay for node " << nd->node->getNodeId()
28 << " = " << (long)nd->delay << "\n";
29}
30
31
Chris Lattnerb7653df2002-04-08 22:03:57 +000032SchedPriorities::SchedPriorities(const Function *, const SchedGraph *G,
Chris Lattner483e14e2002-04-27 07:27:19 +000033 FunctionLiveVarInfo &LVI)
Chris Lattner9adb7ad2002-02-04 20:02:16 +000034 : curTime(0), graph(G), methodLiveVarInfo(LVI),
35 nodeDelayVec(G->getNumNodes(), INVALID_LATENCY), // make errors obvious
Vikram S. Adve0baf1c02002-07-08 22:59:23 +000036 earliestReadyTimeForNode(G->getNumNodes(), 0),
Vikram S. Adve37866b32001-08-28 23:06:49 +000037 earliestReadyTime(0),
Vikram S. Adve0baf1c02002-07-08 22:59:23 +000038 nextToTry(candsAsHeap.begin())
39{
Vikram S. Adve37866b32001-08-28 23:06:49 +000040 computeDelays(graph);
41}
42
43
44void
Misha Brukman0849f5a2003-10-23 17:43:17 +000045SchedPriorities::initialize() {
Vikram S. Adve37866b32001-08-28 23:06:49 +000046 initializeReadyHeap(graph);
47}
48
49
50void
Misha Brukman0849f5a2003-10-23 17:43:17 +000051SchedPriorities::computeDelays(const SchedGraph* graph) {
Chris Lattner3ff43872001-09-28 22:56:31 +000052 po_iterator<const SchedGraph*> poIter = po_begin(graph), poEnd =po_end(graph);
Misha Brukman0849f5a2003-10-23 17:43:17 +000053 for ( ; poIter != poEnd; ++poIter) {
54 const SchedGraphNode* node = *poIter;
55 cycles_t nodeDelay;
56 if (node->beginOutEdges() == node->endOutEdges())
57 nodeDelay = node->getLatency();
58 else {
59 // Iterate over the out-edges of the node to compute delay
60 nodeDelay = 0;
61 for (SchedGraphNode::const_iterator E=node->beginOutEdges();
62 E != node->endOutEdges(); ++E) {
63 cycles_t sinkDelay = getNodeDelay((SchedGraphNode*)(*E)->getSink());
64 nodeDelay = std::max(nodeDelay, sinkDelay + (*E)->getMinDelay());
65 }
Vikram S. Adve37866b32001-08-28 23:06:49 +000066 }
Misha Brukman0849f5a2003-10-23 17:43:17 +000067 getNodeDelayRef(node) = nodeDelay;
68 }
Vikram S. Adve37866b32001-08-28 23:06:49 +000069}
70
71
72void
Misha Brukman0849f5a2003-10-23 17:43:17 +000073SchedPriorities::initializeReadyHeap(const SchedGraph* graph) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +000074 const SchedGraphNode* graphRoot = (const SchedGraphNode*)graph->getRoot();
Vikram S. Adve37866b32001-08-28 23:06:49 +000075 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
Misha Brukman0849f5a2003-10-23 17:43:17 +000084 std::cerr << "Before heap conversion:\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +000085 copy(candsAsHeap.begin(), candsAsHeap.end(),
Misha Brukman0849f5a2003-10-23 17:43:17 +000086 ostream_iterator<NodeDelayPair*>(std::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
Misha Brukman0849f5a2003-10-23 17:43:17 +000094 std::cerr << "After heap conversion:\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +000095 copy(candsAsHeap.begin(), candsAsHeap.end(),
Misha Brukman0849f5a2003-10-23 17:43:17 +000096 ostream_iterator<NodeDelayPair*>(std::cerr,"\n"));
Vikram S. Adve37866b32001-08-28 23:06:49 +000097#endif
98}
99
Chris Lattner697954c2002-01-20 22:54:45 +0000100void
Misha Brukman0849f5a2003-10-23 17:43:17 +0000101SchedPriorities::insertReady(const SchedGraphNode* node) {
Chris Lattner697954c2002-01-20 22:54:45 +0000102 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,
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000106 getEarliestReadyTimeForNode(node));
Chris Lattner697954c2002-01-20 22:54:45 +0000107
Misha Brukman0849f5a2003-10-23 17:43:17 +0000108 if (SchedDebugLevel >= Sched_PrintSchedTrace) {
109 std::cerr << " Node " << node->getNodeId() << " will be ready in Cycle "
110 << getEarliestReadyTimeForNode(node) << "; "
111 << " Delay = " <<(long)getNodeDelay(node) << "; Instruction: \n"
112 << " " << *node->getMachineInstr() << "\n";
113 }
Chris Lattner697954c2002-01-20 22:54:45 +0000114}
Vikram S. Adve37866b32001-08-28 23:06:49 +0000115
116void
117SchedPriorities::issuedReadyNodeAt(cycles_t curTime,
Misha Brukman0849f5a2003-10-23 17:43:17 +0000118 const SchedGraphNode* node) {
Vikram S. Adve37866b32001-08-28 23:06:49 +0000119 candsAsHeap.removeNode(node);
120 candsAsSet.erase(node);
121 mcands.clear(); // ensure reset choices is called before any more choices
122
Misha Brukman0849f5a2003-10-23 17:43:17 +0000123 if (earliestReadyTime == getEarliestReadyTimeForNode(node)) {
124 // earliestReadyTime may have been due to this node, so recompute it
125 earliestReadyTime = HUGE_LATENCY;
126 for (NodeHeap::const_iterator I=candsAsHeap.begin();
127 I != candsAsHeap.end(); ++I)
128 if (candsAsHeap.getNode(I)) {
129 earliestReadyTime =
130 std::min(earliestReadyTime,
131 getEarliestReadyTimeForNode(candsAsHeap.getNode(I)));
132 }
133 }
Vikram S. Adve37866b32001-08-28 23:06:49 +0000134
135 // Now update ready times for successors
136 for (SchedGraphNode::const_iterator E=node->beginOutEdges();
Misha Brukman0849f5a2003-10-23 17:43:17 +0000137 E != node->endOutEdges(); ++E) {
138 cycles_t& etime =
139 getEarliestReadyTimeForNodeRef((SchedGraphNode*)(*E)->getSink());
140 etime = std::max(etime, curTime + (*E)->getMinDelay());
141 }
Vikram S. Adve37866b32001-08-28 23:06:49 +0000142}
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
Misha Brukman0849f5a2003-10-23 17:43:17 +0000155SchedPriorities::chooseByRule1(std::vector<candIndex>& mcands) {
Vikram S. Adve37866b32001-08-28 23:06:49 +0000156 return (mcands.size() == 1)? 0 // only one choice exists so take it
157 : -1; // -1 indicates multiple choices
158}
159
160inline int
Misha Brukman0849f5a2003-10-23 17:43:17 +0000161SchedPriorities::chooseByRule2(std::vector<candIndex>& mcands) {
Vikram S. Adve37866b32001-08-28 23:06:49 +0000162 assert(mcands.size() >= 1 && "Should have at least one candidate here.");
163 for (unsigned i=0, N = mcands.size(); i < N; i++)
164 if (instructionHasLastUse(methodLiveVarInfo,
165 candsAsHeap.getNode(mcands[i])))
166 return i;
167 return -1;
168}
169
170inline int
Misha Brukman0849f5a2003-10-23 17:43:17 +0000171SchedPriorities::chooseByRule3(std::vector<candIndex>& mcands) {
Vikram S. Adve37866b32001-08-28 23:06:49 +0000172 assert(mcands.size() >= 1 && "Should have at least one candidate here.");
173 int maxUses = candsAsHeap.getNode(mcands[0])->getNumOutEdges();
174 int indexWithMaxUses = 0;
Misha Brukman0849f5a2003-10-23 17:43:17 +0000175 for (unsigned i=1, N = mcands.size(); i < N; i++) {
176 int numUses = candsAsHeap.getNode(mcands[i])->getNumOutEdges();
177 if (numUses > maxUses) {
178 maxUses = numUses;
179 indexWithMaxUses = i;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000180 }
Misha Brukman0849f5a2003-10-23 17:43:17 +0000181 }
Vikram S. Adve37866b32001-08-28 23:06:49 +0000182 return indexWithMaxUses;
183}
184
185const SchedGraphNode*
186SchedPriorities::getNextHighest(const SchedulingManager& S,
Misha Brukman0849f5a2003-10-23 17:43:17 +0000187 cycles_t curTime) {
Vikram S. Adve37866b32001-08-28 23:06:49 +0000188 int nextIdx = -1;
189 const SchedGraphNode* nextChoice = NULL;
190
191 if (mcands.size() == 0)
192 findSetWithMaxDelay(mcands, S);
193
Misha Brukman0849f5a2003-10-23 17:43:17 +0000194 while (nextIdx < 0 && mcands.size() > 0) {
195 nextIdx = chooseByRule1(mcands); // rule 1
Vikram S. Adve37866b32001-08-28 23:06:49 +0000196
Misha Brukman0849f5a2003-10-23 17:43:17 +0000197 if (nextIdx == -1)
198 nextIdx = chooseByRule2(mcands); // rule 2
Vikram S. Adve37866b32001-08-28 23:06:49 +0000199
Misha Brukman0849f5a2003-10-23 17:43:17 +0000200 if (nextIdx == -1)
201 nextIdx = chooseByRule3(mcands); // rule 3
Vikram S. Adve37866b32001-08-28 23:06:49 +0000202
Misha Brukman0849f5a2003-10-23 17:43:17 +0000203 if (nextIdx == -1)
204 nextIdx = 0; // default to first choice by delays
Vikram S. Adve37866b32001-08-28 23:06:49 +0000205
Misha Brukman0849f5a2003-10-23 17:43:17 +0000206 // We have found the next best candidate. Check if it ready in
207 // the current cycle, and if it is feasible.
208 // If not, remove it from mcands and continue. Refill mcands if
209 // it becomes empty.
210 nextChoice = candsAsHeap.getNode(mcands[nextIdx]);
211 if (getEarliestReadyTimeForNode(nextChoice) > curTime
212 || ! instrIsFeasible(S, nextChoice->getMachineInstr()->getOpCode()))
Vikram S. Adve37866b32001-08-28 23:06:49 +0000213 {
214 mcands.erase(mcands.begin() + nextIdx);
Misha Brukman0849f5a2003-10-23 17:43:17 +0000215 nextIdx = -1;
216 if (mcands.size() == 0)
217 findSetWithMaxDelay(mcands, S);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000218 }
Misha Brukman0849f5a2003-10-23 17:43:17 +0000219 }
220
221 if (nextIdx >= 0) {
222 mcands.erase(mcands.begin() + nextIdx);
223 return nextChoice;
224 } else
Vikram S. Adve37866b32001-08-28 23:06:49 +0000225 return NULL;
226}
227
228
229void
Chris Lattner697954c2002-01-20 22:54:45 +0000230SchedPriorities::findSetWithMaxDelay(std::vector<candIndex>& mcands,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000231 const SchedulingManager& S)
232{
233 if (mcands.size() == 0 && nextToTry != candsAsHeap.end())
234 { // out of choices at current maximum delay;
235 // put nodes with next highest delay in mcands
236 candIndex next = nextToTry;
237 cycles_t maxDelay = candsAsHeap.getDelay(next);
238 for (; next != candsAsHeap.end()
239 && candsAsHeap.getDelay(next) == maxDelay; ++next)
240 mcands.push_back(next);
241
242 nextToTry = next;
243
Misha Brukman0849f5a2003-10-23 17:43:17 +0000244 if (SchedDebugLevel >= Sched_PrintSchedTrace) {
245 std::cerr << " Cycle " << (long)getTime() << ": "
246 << "Next highest delay = " << (long)maxDelay << " : "
247 << mcands.size() << " Nodes with this delay: ";
248 for (unsigned i=0; i < mcands.size(); i++)
249 std::cerr << candsAsHeap.getNode(mcands[i])->getNodeId() << ", ";
250 std::cerr << "\n";
251 }
Vikram S. Adve37866b32001-08-28 23:06:49 +0000252 }
253}
254
255
256bool
Chris Lattner483e14e2002-04-27 07:27:19 +0000257SchedPriorities::instructionHasLastUse(FunctionLiveVarInfo &LVI,
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000258 const SchedGraphNode* graphNode) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000259 const MachineInstr *MI = graphNode->getMachineInstr();
Vikram S. Adve37866b32001-08-28 23:06:49 +0000260
Chris Lattner09ff1122002-07-24 21:21:32 +0000261 hash_map<const MachineInstr*, bool>::const_iterator
Chris Lattner2f898d22002-02-05 06:02:59 +0000262 ui = lastUseMap.find(MI);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000263 if (ui != lastUseMap.end())
Chris Lattner697954c2002-01-20 22:54:45 +0000264 return ui->second;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000265
266 // else check if instruction is a last use and save it in the hash_map
267 bool hasLastUse = false;
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000268 const BasicBlock* bb = graphNode->getMachineBasicBlock().getBasicBlock();
Chris Lattner483e14e2002-04-27 07:27:19 +0000269 const ValueSet &LVs = LVI.getLiveVarSetBeforeMInst(MI, bb);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000270
Chris Lattner2f898d22002-02-05 06:02:59 +0000271 for (MachineInstr::const_val_op_iterator OI = MI->begin(), OE = MI->end();
272 OI != OE; ++OI)
273 if (!LVs.count(*OI)) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000274 hasLastUse = true;
275 break;
276 }
Chris Lattner748697d2002-02-05 04:20:12 +0000277
Chris Lattner2f898d22002-02-05 06:02:59 +0000278 return lastUseMap[MI] = hasLastUse;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000279}
280