blob: 53b71fa3a737a3eb5c3676a1cdb7f54cffe97c5c [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"
Brian Gaeke367b91d2004-02-25 22:09:36 +000021#include "../../Target/SparcV9/LiveVar/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
Brian Gaeked0fde302003-11-11 22:41:34 +000026namespace llvm {
27
Chris Lattner5da2e6a2002-11-02 22:07:51 +000028std::ostream &operator<<(std::ostream &os, const NodeDelayPair* nd) {
29 return os << "Delay for node " << nd->node->getNodeId()
30 << " = " << (long)nd->delay << "\n";
31}
32
33
Chris Lattnerb7653df2002-04-08 22:03:57 +000034SchedPriorities::SchedPriorities(const Function *, const SchedGraph *G,
Chris Lattner483e14e2002-04-27 07:27:19 +000035 FunctionLiveVarInfo &LVI)
Chris Lattner9adb7ad2002-02-04 20:02:16 +000036 : curTime(0), graph(G), methodLiveVarInfo(LVI),
37 nodeDelayVec(G->getNumNodes(), INVALID_LATENCY), // make errors obvious
Vikram S. Adve0baf1c02002-07-08 22:59:23 +000038 earliestReadyTimeForNode(G->getNumNodes(), 0),
Vikram S. Adve37866b32001-08-28 23:06:49 +000039 earliestReadyTime(0),
Vikram S. Adve0baf1c02002-07-08 22:59:23 +000040 nextToTry(candsAsHeap.begin())
41{
Vikram S. Adve37866b32001-08-28 23:06:49 +000042 computeDelays(graph);
43}
44
45
46void
Misha Brukman0849f5a2003-10-23 17:43:17 +000047SchedPriorities::initialize() {
Vikram S. Adve37866b32001-08-28 23:06:49 +000048 initializeReadyHeap(graph);
49}
50
51
52void
Misha Brukman0849f5a2003-10-23 17:43:17 +000053SchedPriorities::computeDelays(const SchedGraph* graph) {
Chris Lattner3ff43872001-09-28 22:56:31 +000054 po_iterator<const SchedGraph*> poIter = po_begin(graph), poEnd =po_end(graph);
Misha Brukman0849f5a2003-10-23 17:43:17 +000055 for ( ; poIter != poEnd; ++poIter) {
56 const SchedGraphNode* node = *poIter;
57 cycles_t nodeDelay;
58 if (node->beginOutEdges() == node->endOutEdges())
59 nodeDelay = node->getLatency();
60 else {
61 // Iterate over the out-edges of the node to compute delay
62 nodeDelay = 0;
63 for (SchedGraphNode::const_iterator E=node->beginOutEdges();
64 E != node->endOutEdges(); ++E) {
65 cycles_t sinkDelay = getNodeDelay((SchedGraphNode*)(*E)->getSink());
66 nodeDelay = std::max(nodeDelay, sinkDelay + (*E)->getMinDelay());
67 }
Vikram S. Adve37866b32001-08-28 23:06:49 +000068 }
Misha Brukman0849f5a2003-10-23 17:43:17 +000069 getNodeDelayRef(node) = nodeDelay;
70 }
Vikram S. Adve37866b32001-08-28 23:06:49 +000071}
72
73
74void
Misha Brukman0849f5a2003-10-23 17:43:17 +000075SchedPriorities::initializeReadyHeap(const SchedGraph* graph) {
Tanya Lattnerb6489f32003-08-25 22:42:20 +000076 const SchedGraphNode* graphRoot = (const SchedGraphNode*)graph->getRoot();
Vikram S. Adve37866b32001-08-28 23:06:49 +000077 assert(graphRoot->getMachineInstr() == NULL && "Expect dummy root");
78
79 // Insert immediate successors of dummy root, which are the actual roots
80 sg_succ_const_iterator SEnd = succ_end(graphRoot);
81 for (sg_succ_const_iterator S = succ_begin(graphRoot); S != SEnd; ++S)
82 this->insertReady(*S);
83
84#undef TEST_HEAP_CONVERSION
85#ifdef TEST_HEAP_CONVERSION
Misha Brukman0849f5a2003-10-23 17:43:17 +000086 std::cerr << "Before heap conversion:\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +000087 copy(candsAsHeap.begin(), candsAsHeap.end(),
Misha Brukman0849f5a2003-10-23 17:43:17 +000088 ostream_iterator<NodeDelayPair*>(std::cerr,"\n"));
Vikram S. Adve37866b32001-08-28 23:06:49 +000089#endif
90
91 candsAsHeap.makeHeap();
92
Vikram S. Adve1392d692002-03-24 03:46:15 +000093 nextToTry = candsAsHeap.begin();
94
Vikram S. Adve37866b32001-08-28 23:06:49 +000095#ifdef TEST_HEAP_CONVERSION
Misha Brukman0849f5a2003-10-23 17:43:17 +000096 std::cerr << "After heap conversion:\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +000097 copy(candsAsHeap.begin(), candsAsHeap.end(),
Misha Brukman0849f5a2003-10-23 17:43:17 +000098 ostream_iterator<NodeDelayPair*>(std::cerr,"\n"));
Vikram S. Adve37866b32001-08-28 23:06:49 +000099#endif
100}
101
Chris Lattner697954c2002-01-20 22:54:45 +0000102void
Misha Brukman0849f5a2003-10-23 17:43:17 +0000103SchedPriorities::insertReady(const SchedGraphNode* node) {
Chris Lattner697954c2002-01-20 22:54:45 +0000104 candsAsHeap.insert(node, nodeDelayVec[node->getNodeId()]);
105 candsAsSet.insert(node);
106 mcands.clear(); // ensure reset choices is called before any more choices
107 earliestReadyTime = std::min(earliestReadyTime,
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000108 getEarliestReadyTimeForNode(node));
Chris Lattner697954c2002-01-20 22:54:45 +0000109
Misha Brukman0849f5a2003-10-23 17:43:17 +0000110 if (SchedDebugLevel >= Sched_PrintSchedTrace) {
111 std::cerr << " Node " << node->getNodeId() << " will be ready in Cycle "
112 << getEarliestReadyTimeForNode(node) << "; "
113 << " Delay = " <<(long)getNodeDelay(node) << "; Instruction: \n"
114 << " " << *node->getMachineInstr() << "\n";
115 }
Chris Lattner697954c2002-01-20 22:54:45 +0000116}
Vikram S. Adve37866b32001-08-28 23:06:49 +0000117
118void
119SchedPriorities::issuedReadyNodeAt(cycles_t curTime,
Misha Brukman0849f5a2003-10-23 17:43:17 +0000120 const SchedGraphNode* node) {
Vikram S. Adve37866b32001-08-28 23:06:49 +0000121 candsAsHeap.removeNode(node);
122 candsAsSet.erase(node);
123 mcands.clear(); // ensure reset choices is called before any more choices
124
Misha Brukman0849f5a2003-10-23 17:43:17 +0000125 if (earliestReadyTime == getEarliestReadyTimeForNode(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)) {
131 earliestReadyTime =
132 std::min(earliestReadyTime,
133 getEarliestReadyTimeForNode(candsAsHeap.getNode(I)));
134 }
135 }
Vikram S. Adve37866b32001-08-28 23:06:49 +0000136
137 // Now update ready times for successors
138 for (SchedGraphNode::const_iterator E=node->beginOutEdges();
Misha Brukman0849f5a2003-10-23 17:43:17 +0000139 E != node->endOutEdges(); ++E) {
140 cycles_t& etime =
141 getEarliestReadyTimeForNodeRef((SchedGraphNode*)(*E)->getSink());
142 etime = std::max(etime, curTime + (*E)->getMinDelay());
143 }
Vikram S. Adve37866b32001-08-28 23:06:49 +0000144}
145
146
147//----------------------------------------------------------------------
148// Priority ordering rules:
149// (1) Max delay, which is the order of the heap S.candsAsHeap.
150// (2) Instruction that frees up a register.
151// (3) Instruction that has the maximum number of dependent instructions.
152// Note that rules 2 and 3 are only used if issue conflicts prevent
153// choosing a higher priority instruction by rule 1.
154//----------------------------------------------------------------------
155
156inline int
Misha Brukman0849f5a2003-10-23 17:43:17 +0000157SchedPriorities::chooseByRule1(std::vector<candIndex>& mcands) {
Vikram S. Adve37866b32001-08-28 23:06:49 +0000158 return (mcands.size() == 1)? 0 // only one choice exists so take it
159 : -1; // -1 indicates multiple choices
160}
161
162inline int
Misha Brukman0849f5a2003-10-23 17:43:17 +0000163SchedPriorities::chooseByRule2(std::vector<candIndex>& mcands) {
Vikram S. Adve37866b32001-08-28 23:06:49 +0000164 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
Misha Brukman0849f5a2003-10-23 17:43:17 +0000173SchedPriorities::chooseByRule3(std::vector<candIndex>& mcands) {
Vikram S. Adve37866b32001-08-28 23:06:49 +0000174 assert(mcands.size() >= 1 && "Should have at least one candidate here.");
175 int maxUses = candsAsHeap.getNode(mcands[0])->getNumOutEdges();
176 int indexWithMaxUses = 0;
Misha Brukman0849f5a2003-10-23 17:43:17 +0000177 for (unsigned i=1, N = mcands.size(); i < N; i++) {
178 int numUses = candsAsHeap.getNode(mcands[i])->getNumOutEdges();
179 if (numUses > maxUses) {
180 maxUses = numUses;
181 indexWithMaxUses = i;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000182 }
Misha Brukman0849f5a2003-10-23 17:43:17 +0000183 }
Vikram S. Adve37866b32001-08-28 23:06:49 +0000184 return indexWithMaxUses;
185}
186
187const SchedGraphNode*
188SchedPriorities::getNextHighest(const SchedulingManager& S,
Misha Brukman0849f5a2003-10-23 17:43:17 +0000189 cycles_t curTime) {
Vikram S. Adve37866b32001-08-28 23:06:49 +0000190 int nextIdx = -1;
191 const SchedGraphNode* nextChoice = NULL;
192
193 if (mcands.size() == 0)
194 findSetWithMaxDelay(mcands, S);
195
Misha Brukman0849f5a2003-10-23 17:43:17 +0000196 while (nextIdx < 0 && mcands.size() > 0) {
197 nextIdx = chooseByRule1(mcands); // rule 1
Vikram S. Adve37866b32001-08-28 23:06:49 +0000198
Misha Brukman0849f5a2003-10-23 17:43:17 +0000199 if (nextIdx == -1)
200 nextIdx = chooseByRule2(mcands); // rule 2
Vikram S. Adve37866b32001-08-28 23:06:49 +0000201
Misha Brukman0849f5a2003-10-23 17:43:17 +0000202 if (nextIdx == -1)
203 nextIdx = chooseByRule3(mcands); // rule 3
Vikram S. Adve37866b32001-08-28 23:06:49 +0000204
Misha Brukman0849f5a2003-10-23 17:43:17 +0000205 if (nextIdx == -1)
206 nextIdx = 0; // default to first choice by delays
Vikram S. Adve37866b32001-08-28 23:06:49 +0000207
Misha Brukman0849f5a2003-10-23 17:43:17 +0000208 // 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]);
213 if (getEarliestReadyTimeForNode(nextChoice) > curTime
Brian Gaeke918cdd42004-02-12 01:34:05 +0000214 || ! instrIsFeasible(S, nextChoice->getMachineInstr()->getOpcode()))
Vikram S. Adve37866b32001-08-28 23:06:49 +0000215 {
216 mcands.erase(mcands.begin() + nextIdx);
Misha Brukman0849f5a2003-10-23 17:43:17 +0000217 nextIdx = -1;
218 if (mcands.size() == 0)
219 findSetWithMaxDelay(mcands, S);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000220 }
Misha Brukman0849f5a2003-10-23 17:43:17 +0000221 }
222
223 if (nextIdx >= 0) {
224 mcands.erase(mcands.begin() + nextIdx);
225 return nextChoice;
226 } else
Vikram S. Adve37866b32001-08-28 23:06:49 +0000227 return NULL;
228}
229
230
231void
Chris Lattner697954c2002-01-20 22:54:45 +0000232SchedPriorities::findSetWithMaxDelay(std::vector<candIndex>& mcands,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000233 const SchedulingManager& S)
234{
235 if (mcands.size() == 0 && nextToTry != candsAsHeap.end())
236 { // out of choices at current maximum delay;
237 // put nodes with next highest delay in mcands
238 candIndex next = nextToTry;
239 cycles_t maxDelay = candsAsHeap.getDelay(next);
240 for (; next != candsAsHeap.end()
241 && candsAsHeap.getDelay(next) == maxDelay; ++next)
242 mcands.push_back(next);
243
244 nextToTry = next;
245
Misha Brukman0849f5a2003-10-23 17:43:17 +0000246 if (SchedDebugLevel >= Sched_PrintSchedTrace) {
247 std::cerr << " Cycle " << (long)getTime() << ": "
248 << "Next highest delay = " << (long)maxDelay << " : "
249 << mcands.size() << " Nodes with this delay: ";
250 for (unsigned i=0; i < mcands.size(); i++)
251 std::cerr << candsAsHeap.getNode(mcands[i])->getNodeId() << ", ";
252 std::cerr << "\n";
253 }
Vikram S. Adve37866b32001-08-28 23:06:49 +0000254 }
255}
256
257
258bool
Chris Lattner483e14e2002-04-27 07:27:19 +0000259SchedPriorities::instructionHasLastUse(FunctionLiveVarInfo &LVI,
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000260 const SchedGraphNode* graphNode) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000261 const MachineInstr *MI = graphNode->getMachineInstr();
Vikram S. Adve37866b32001-08-28 23:06:49 +0000262
Chris Lattner09ff1122002-07-24 21:21:32 +0000263 hash_map<const MachineInstr*, bool>::const_iterator
Chris Lattner2f898d22002-02-05 06:02:59 +0000264 ui = lastUseMap.find(MI);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000265 if (ui != lastUseMap.end())
Chris Lattner697954c2002-01-20 22:54:45 +0000266 return ui->second;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000267
268 // else check if instruction is a last use and save it in the hash_map
269 bool hasLastUse = false;
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000270 const BasicBlock* bb = graphNode->getMachineBasicBlock().getBasicBlock();
Chris Lattner483e14e2002-04-27 07:27:19 +0000271 const ValueSet &LVs = LVI.getLiveVarSetBeforeMInst(MI, bb);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000272
Chris Lattner2f898d22002-02-05 06:02:59 +0000273 for (MachineInstr::const_val_op_iterator OI = MI->begin(), OE = MI->end();
274 OI != OE; ++OI)
275 if (!LVs.count(*OI)) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000276 hasLastUse = true;
277 break;
278 }
Chris Lattner748697d2002-02-05 04:20:12 +0000279
Chris Lattner2f898d22002-02-05 06:02:59 +0000280 return lastUseMap[MI] = hasLastUse;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000281}
282
Brian Gaeked0fde302003-11-11 22:41:34 +0000283} // End llvm namespace