1 //===-- SchedPriorities.h - Encapsulate scheduling heuristics -------------===//
4 // Priority ordering rules:
5 // (1) Max delay, which is the order of the heap S.candsAsHeap.
6 // (2) Instruction that frees up a register.
7 // (3) Instruction that has the maximum number of dependent instructions.
8 // Note that rules 2 and 3 are only used if issue conflicts prevent
9 // choosing a higher priority instruction by rule 1.
11 //===----------------------------------------------------------------------===//
13 #include "SchedPriorities.h"
14 #include "llvm/CodeGen/FunctionLiveVarInfo.h"
15 #include "llvm/CodeGen/MachineBasicBlock.h"
16 #include "llvm/Support/CFG.h"
17 #include "Support/PostOrderIterator.h"
20 std::ostream &operator<<(std::ostream &os, const NodeDelayPair* nd) {
21 return os << "Delay for node " << nd->node->getNodeId()
22 << " = " << (long)nd->delay << "\n";
26 SchedPriorities::SchedPriorities(const Function *, const SchedGraph *G,
27 FunctionLiveVarInfo &LVI)
28 : curTime(0), graph(G), methodLiveVarInfo(LVI),
29 nodeDelayVec(G->getNumNodes(), INVALID_LATENCY), // make errors obvious
30 earliestReadyTimeForNode(G->getNumNodes(), 0),
32 nextToTry(candsAsHeap.begin())
39 SchedPriorities::initialize()
41 initializeReadyHeap(graph);
46 SchedPriorities::computeDelays(const SchedGraph* graph)
48 po_iterator<const SchedGraph*> poIter = po_begin(graph), poEnd =po_end(graph);
49 for ( ; poIter != poEnd; ++poIter)
51 const SchedGraphNode* node = *poIter;
53 if (node->beginOutEdges() == node->endOutEdges())
54 nodeDelay = node->getLatency();
57 // Iterate over the out-edges of the node to compute delay
59 for (SchedGraphNode::const_iterator E=node->beginOutEdges();
60 E != node->endOutEdges(); ++E)
62 cycles_t sinkDelay = getNodeDelay((*E)->getSink());
63 nodeDelay = std::max(nodeDelay, sinkDelay + (*E)->getMinDelay());
66 getNodeDelayRef(node) = nodeDelay;
72 SchedPriorities::initializeReadyHeap(const SchedGraph* graph)
74 const SchedGraphNode* graphRoot = graph->getRoot();
75 assert(graphRoot->getMachineInstr() == NULL && "Expect dummy root");
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);
82 #undef TEST_HEAP_CONVERSION
83 #ifdef TEST_HEAP_CONVERSION
84 cerr << "Before heap conversion:\n";
85 copy(candsAsHeap.begin(), candsAsHeap.end(),
86 ostream_iterator<NodeDelayPair*>(cerr,"\n"));
89 candsAsHeap.makeHeap();
91 nextToTry = candsAsHeap.begin();
93 #ifdef TEST_HEAP_CONVERSION
94 cerr << "After heap conversion:\n";
95 copy(candsAsHeap.begin(), candsAsHeap.end(),
96 ostream_iterator<NodeDelayPair*>(cerr,"\n"));
101 SchedPriorities::insertReady(const SchedGraphNode* node)
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 getEarliestReadyTimeForNode(node));
109 if (SchedDebugLevel >= Sched_PrintSchedTrace)
111 cerr << " Node " << node->getNodeId() << " will be ready in Cycle "
112 << getEarliestReadyTimeForNode(node) << "; "
113 << " Delay = " <<(long)getNodeDelay(node) << "; Instruction: \n";
114 cerr << " " << *node->getMachineInstr() << "\n";
119 SchedPriorities::issuedReadyNodeAt(cycles_t curTime,
120 const SchedGraphNode* node)
122 candsAsHeap.removeNode(node);
123 candsAsSet.erase(node);
124 mcands.clear(); // ensure reset choices is called before any more choices
126 if (earliestReadyTime == getEarliestReadyTimeForNode(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))
132 earliestReadyTime = std::min(earliestReadyTime,
133 getEarliestReadyTimeForNode(candsAsHeap.getNode(I)));
136 // Now update ready times for successors
137 for (SchedGraphNode::const_iterator E=node->beginOutEdges();
138 E != node->endOutEdges(); ++E)
140 cycles_t& etime = getEarliestReadyTimeForNodeRef((*E)->getSink());
141 etime = std::max(etime, curTime + (*E)->getMinDelay());
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 //----------------------------------------------------------------------
156 SchedPriorities::chooseByRule1(std::vector<candIndex>& mcands)
158 return (mcands.size() == 1)? 0 // only one choice exists so take it
159 : -1; // -1 indicates multiple choices
163 SchedPriorities::chooseByRule2(std::vector<candIndex>& mcands)
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])))
174 SchedPriorities::chooseByRule3(std::vector<candIndex>& mcands)
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++)
181 int numUses = candsAsHeap.getNode(mcands[i])->getNumOutEdges();
182 if (numUses > maxUses)
185 indexWithMaxUses = i;
188 return indexWithMaxUses;
191 const SchedGraphNode*
192 SchedPriorities::getNextHighest(const SchedulingManager& S,
196 const SchedGraphNode* nextChoice = NULL;
198 if (mcands.size() == 0)
199 findSetWithMaxDelay(mcands, S);
201 while (nextIdx < 0 && mcands.size() > 0)
203 nextIdx = chooseByRule1(mcands); // rule 1
206 nextIdx = chooseByRule2(mcands); // rule 2
209 nextIdx = chooseByRule3(mcands); // rule 3
212 nextIdx = 0; // default to first choice by delays
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
218 nextChoice = candsAsHeap.getNode(mcands[nextIdx]);
219 if (getEarliestReadyTimeForNode(nextChoice) > curTime
220 || ! instrIsFeasible(S, nextChoice->getMachineInstr()->getOpCode()))
222 mcands.erase(mcands.begin() + nextIdx);
224 if (mcands.size() == 0)
225 findSetWithMaxDelay(mcands, S);
231 mcands.erase(mcands.begin() + nextIdx);
240 SchedPriorities::findSetWithMaxDelay(std::vector<candIndex>& mcands,
241 const SchedulingManager& S)
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);
254 if (SchedDebugLevel >= Sched_PrintSchedTrace)
256 cerr << " Cycle " << (long)getTime() << ": "
257 << "Next highest delay = " << (long)maxDelay << " : "
258 << mcands.size() << " Nodes with this delay: ";
259 for (unsigned i=0; i < mcands.size(); i++)
260 cerr << candsAsHeap.getNode(mcands[i])->getNodeId() << ", ";
268 SchedPriorities::instructionHasLastUse(FunctionLiveVarInfo &LVI,
269 const SchedGraphNode* graphNode) {
270 const MachineInstr *MI = graphNode->getMachineInstr();
272 hash_map<const MachineInstr*, bool>::const_iterator
273 ui = lastUseMap.find(MI);
274 if (ui != lastUseMap.end())
277 // else check if instruction is a last use and save it in the hash_map
278 bool hasLastUse = false;
279 const BasicBlock* bb = graphNode->getMachineBasicBlock().getBasicBlock();
280 const ValueSet &LVs = LVI.getLiveVarSetBeforeMInst(MI, bb);
282 for (MachineInstr::const_val_op_iterator OI = MI->begin(), OE = MI->end();
284 if (!LVs.count(*OI)) {
289 return lastUseMap[MI] = hasLastUse;