1 //===-- SchedPriorities.h - Encapsulate scheduling heuristics -------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
18 //===----------------------------------------------------------------------===//
20 #include "SchedPriorities.h"
21 #include "llvm/CodeGen/FunctionLiveVarInfo.h"
22 #include "llvm/CodeGen/MachineBasicBlock.h"
23 #include "llvm/Support/CFG.h"
24 #include "Support/PostOrderIterator.h"
27 std::ostream &operator<<(std::ostream &os, const NodeDelayPair* nd) {
28 return os << "Delay for node " << nd->node->getNodeId()
29 << " = " << (long)nd->delay << "\n";
33 SchedPriorities::SchedPriorities(const Function *, const SchedGraph *G,
34 FunctionLiveVarInfo &LVI)
35 : curTime(0), graph(G), methodLiveVarInfo(LVI),
36 nodeDelayVec(G->getNumNodes(), INVALID_LATENCY), // make errors obvious
37 earliestReadyTimeForNode(G->getNumNodes(), 0),
39 nextToTry(candsAsHeap.begin())
46 SchedPriorities::initialize()
48 initializeReadyHeap(graph);
53 SchedPriorities::computeDelays(const SchedGraph* graph)
55 po_iterator<const SchedGraph*> poIter = po_begin(graph), poEnd =po_end(graph);
56 for ( ; poIter != poEnd; ++poIter)
58 const SchedGraphNode* node = *poIter;
60 if (node->beginOutEdges() == node->endOutEdges())
61 nodeDelay = node->getLatency();
64 // Iterate over the out-edges of the node to compute delay
66 for (SchedGraphNode::const_iterator E=node->beginOutEdges();
67 E != node->endOutEdges(); ++E)
69 cycles_t sinkDelay = getNodeDelay((SchedGraphNode*)(*E)->getSink());
70 nodeDelay = std::max(nodeDelay, sinkDelay + (*E)->getMinDelay());
73 getNodeDelayRef(node) = nodeDelay;
79 SchedPriorities::initializeReadyHeap(const SchedGraph* graph)
81 const SchedGraphNode* graphRoot = (const SchedGraphNode*)graph->getRoot();
82 assert(graphRoot->getMachineInstr() == NULL && "Expect dummy root");
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);
89 #undef TEST_HEAP_CONVERSION
90 #ifdef TEST_HEAP_CONVERSION
91 cerr << "Before heap conversion:\n";
92 copy(candsAsHeap.begin(), candsAsHeap.end(),
93 ostream_iterator<NodeDelayPair*>(cerr,"\n"));
96 candsAsHeap.makeHeap();
98 nextToTry = candsAsHeap.begin();
100 #ifdef TEST_HEAP_CONVERSION
101 cerr << "After heap conversion:\n";
102 copy(candsAsHeap.begin(), candsAsHeap.end(),
103 ostream_iterator<NodeDelayPair*>(cerr,"\n"));
108 SchedPriorities::insertReady(const SchedGraphNode* node)
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,
114 getEarliestReadyTimeForNode(node));
116 if (SchedDebugLevel >= Sched_PrintSchedTrace)
118 cerr << " Node " << node->getNodeId() << " will be ready in Cycle "
119 << getEarliestReadyTimeForNode(node) << "; "
120 << " Delay = " <<(long)getNodeDelay(node) << "; Instruction: \n";
121 cerr << " " << *node->getMachineInstr() << "\n";
126 SchedPriorities::issuedReadyNodeAt(cycles_t curTime,
127 const SchedGraphNode* node)
129 candsAsHeap.removeNode(node);
130 candsAsSet.erase(node);
131 mcands.clear(); // ensure reset choices is called before any more choices
133 if (earliestReadyTime == getEarliestReadyTimeForNode(node))
134 {// 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))
139 earliestReadyTime = std::min(earliestReadyTime,
140 getEarliestReadyTimeForNode(candsAsHeap.getNode(I)));
143 // Now update ready times for successors
144 for (SchedGraphNode::const_iterator E=node->beginOutEdges();
145 E != node->endOutEdges(); ++E)
147 cycles_t& etime = getEarliestReadyTimeForNodeRef((SchedGraphNode*)(*E)->getSink());
148 etime = std::max(etime, curTime + (*E)->getMinDelay());
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 //----------------------------------------------------------------------
163 SchedPriorities::chooseByRule1(std::vector<candIndex>& mcands)
165 return (mcands.size() == 1)? 0 // only one choice exists so take it
166 : -1; // -1 indicates multiple choices
170 SchedPriorities::chooseByRule2(std::vector<candIndex>& mcands)
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])))
181 SchedPriorities::chooseByRule3(std::vector<candIndex>& mcands)
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++)
188 int numUses = candsAsHeap.getNode(mcands[i])->getNumOutEdges();
189 if (numUses > maxUses)
192 indexWithMaxUses = i;
195 return indexWithMaxUses;
198 const SchedGraphNode*
199 SchedPriorities::getNextHighest(const SchedulingManager& S,
203 const SchedGraphNode* nextChoice = NULL;
205 if (mcands.size() == 0)
206 findSetWithMaxDelay(mcands, S);
208 while (nextIdx < 0 && mcands.size() > 0)
210 nextIdx = chooseByRule1(mcands); // rule 1
213 nextIdx = chooseByRule2(mcands); // rule 2
216 nextIdx = chooseByRule3(mcands); // rule 3
219 nextIdx = 0; // default to first choice by delays
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
225 nextChoice = candsAsHeap.getNode(mcands[nextIdx]);
226 if (getEarliestReadyTimeForNode(nextChoice) > curTime
227 || ! instrIsFeasible(S, nextChoice->getMachineInstr()->getOpCode()))
229 mcands.erase(mcands.begin() + nextIdx);
231 if (mcands.size() == 0)
232 findSetWithMaxDelay(mcands, S);
238 mcands.erase(mcands.begin() + nextIdx);
247 SchedPriorities::findSetWithMaxDelay(std::vector<candIndex>& mcands,
248 const SchedulingManager& S)
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);
261 if (SchedDebugLevel >= Sched_PrintSchedTrace)
263 cerr << " Cycle " << (long)getTime() << ": "
264 << "Next highest delay = " << (long)maxDelay << " : "
265 << mcands.size() << " Nodes with this delay: ";
266 for (unsigned i=0; i < mcands.size(); i++)
267 cerr << candsAsHeap.getNode(mcands[i])->getNodeId() << ", ";
275 SchedPriorities::instructionHasLastUse(FunctionLiveVarInfo &LVI,
276 const SchedGraphNode* graphNode) {
277 const MachineInstr *MI = graphNode->getMachineInstr();
279 hash_map<const MachineInstr*, bool>::const_iterator
280 ui = lastUseMap.find(MI);
281 if (ui != lastUseMap.end())
284 // else check if instruction is a last use and save it in the hash_map
285 bool hasLastUse = false;
286 const BasicBlock* bb = graphNode->getMachineBasicBlock().getBasicBlock();
287 const ValueSet &LVs = LVI.getLiveVarSetBeforeMInst(MI, bb);
289 for (MachineInstr::const_val_op_iterator OI = MI->begin(), OE = MI->end();
291 if (!LVs.count(*OI)) {
296 return lastUseMap[MI] = hasLastUse;