blob: 7cfc4c0f3543bdda87e50ecae96e6142f1e0afb9 [file] [log] [blame]
Vikram S. Adve851d44c2001-09-18 12:49:39 +00001// -*-C++-*-
2//***************************************************************************
3// File:
4// SchedPriorities.h
5//
6// Purpose:
7// Encapsulate heuristics for instruction scheduling.
8//
9// Strategy:
10// Priority ordering rules:
11// (1) Max delay, which is the order of the heap S.candsAsHeap.
12// (2) Instruction that frees up a register.
13// (3) Instruction that has the maximum number of dependent instructions.
14// Note that rules 2 and 3 are only used if issue conflicts prevent
15// choosing a higher priority instruction by rule 1.
16//
17// History:
18// 7/30/01 - Vikram Adve - Created
19//**************************************************************************/
Vikram S. Adve37866b32001-08-28 23:06:49 +000020
21#ifndef LLVM_CODEGEN_SCHEDPRIORITIES_H
22#define LLVM_CODEGEN_SCHEDPRIORITIES_H
23
Chris Lattner46cbff62001-09-14 16:56:32 +000024#include "SchedGraph.h"
Chris Lattnerdcd2fb52001-09-07 21:18:16 +000025#include "llvm/CodeGen/InstrScheduling.h"
Vikram S. Adve851d44c2001-09-18 12:49:39 +000026#include "llvm/Target/MachineSchedInfo.h"
Chris Lattner77f66c12002-02-04 02:44:20 +000027#include "Support/CommandLine.h"
Chris Lattner3ff43872001-09-28 22:56:31 +000028#include <list>
Chris Lattnerf35f2fb2002-02-04 16:35:45 +000029#include <hash_set>
Vikram S. Adve37866b32001-08-28 23:06:49 +000030class Method;
31class MachineInstr;
32class SchedulingManager;
Chris Lattner9adb7ad2002-02-04 20:02:16 +000033class MethodLiveVarInfo;
Vikram S. Adve37866b32001-08-28 23:06:49 +000034
Chris Lattnerb99bd2b2002-02-04 05:55:42 +000035//---------------------------------------------------------------------------
Chris Lattner77f66c12002-02-04 02:44:20 +000036// Debug option levels for instruction scheduling
Chris Lattnerb99bd2b2002-02-04 05:55:42 +000037
Chris Lattner77f66c12002-02-04 02:44:20 +000038enum SchedDebugLevel_t {
39 Sched_NoDebugInfo,
40 Sched_PrintMachineCode,
41 Sched_PrintSchedTrace,
42 Sched_PrintSchedGraphs,
43};
44
45extern cl::Enum<SchedDebugLevel_t> SchedDebugLevel;
Vikram S. Adve37866b32001-08-28 23:06:49 +000046
Chris Lattnerb99bd2b2002-02-04 05:55:42 +000047//---------------------------------------------------------------------------
48// Function: instrIsFeasible
49//
50// Purpose:
51// Used by the priority analysis to filter out instructions
52// that are not feasible to issue in the current cycle.
53// Should only be used during schedule construction..
54//---------------------------------------------------------------------------
55
56bool instrIsFeasible(const SchedulingManager &S, MachineOpCode opCode);
57
58
59
Vikram S. Adve37866b32001-08-28 23:06:49 +000060struct NodeDelayPair {
61 const SchedGraphNode* node;
62 cycles_t delay;
63 NodeDelayPair(const SchedGraphNode* n, cycles_t d) : node(n), delay(d) {}
Chris Lattner697954c2002-01-20 22:54:45 +000064 inline bool operator<(const NodeDelayPair& np) { return delay < np.delay; }
Vikram S. Adve37866b32001-08-28 23:06:49 +000065};
66
67inline bool
68NDPLessThan(const NodeDelayPair* np1, const NodeDelayPair* np2)
69{
Chris Lattner697954c2002-01-20 22:54:45 +000070 return np1->delay < np2->delay;
Vikram S. Adve37866b32001-08-28 23:06:49 +000071}
72
Chris Lattner697954c2002-01-20 22:54:45 +000073class NodeHeap: public std::list<NodeDelayPair*>, public NonCopyable {
Vikram S. Adve37866b32001-08-28 23:06:49 +000074public:
Chris Lattner697954c2002-01-20 22:54:45 +000075 typedef std::list<NodeDelayPair*>::iterator iterator;
76 typedef std::list<NodeDelayPair*>::const_iterator const_iterator;
Vikram S. Adve37866b32001-08-28 23:06:49 +000077
78public:
Chris Lattnerf35f2fb2002-02-04 16:35:45 +000079 NodeHeap() : _size(0) {}
Vikram S. Adve37866b32001-08-28 23:06:49 +000080
Chris Lattnerf35f2fb2002-02-04 16:35:45 +000081 inline unsigned size() const { return _size; }
Vikram S. Adve37866b32001-08-28 23:06:49 +000082
83 const SchedGraphNode* getNode (const_iterator i) const { return (*i)->node; }
84 cycles_t getDelay(const_iterator i) const { return (*i)->delay;}
85
86 inline void makeHeap() {
87 // make_heap(begin(), end(), NDPLessThan);
88 }
89
90 inline iterator findNode(const SchedGraphNode* node) {
91 for (iterator I=begin(); I != end(); ++I)
92 if (getNode(I) == node)
93 return I;
94 return end();
95 }
96
97 inline void removeNode (const SchedGraphNode* node) {
98 iterator ndpPtr = findNode(node);
99 if (ndpPtr != end())
100 {
101 delete *ndpPtr;
102 erase(ndpPtr);
103 --_size;
104 }
105 };
106
107 void insert(const SchedGraphNode* node, cycles_t delay) {
108 NodeDelayPair* ndp = new NodeDelayPair(node, delay);
109 if (_size == 0 || front()->delay < delay)
110 push_front(ndp);
111 else
112 {
113 iterator I=begin();
114 for ( ; I != end() && getDelay(I) >= delay; ++I)
115 ;
Chris Lattner697954c2002-01-20 22:54:45 +0000116 std::list<NodeDelayPair*>::insert(I, ndp);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000117 }
118 _size++;
119 }
120private:
121 unsigned int _size;
122};
123
124
125class SchedPriorities: public NonCopyable {
126public:
Chris Lattner9adb7ad2002-02-04 20:02:16 +0000127 SchedPriorities(const Method *M, const SchedGraph *G, MethodLiveVarInfo &LVI);
128
Vikram S. Adve37866b32001-08-28 23:06:49 +0000129
130 // This must be called before scheduling begins.
131 void initialize ();
132
133 cycles_t getTime () const { return curTime; }
134 cycles_t getEarliestReadyTime () const { return earliestReadyTime; }
135 unsigned getNumReady () const { return candsAsHeap.size(); }
136 bool nodeIsReady (const SchedGraphNode* node) const {
137 return (candsAsSet.find(node) != candsAsSet.end());
138 }
139
140 void issuedReadyNodeAt (cycles_t curTime,
141 const SchedGraphNode* node);
142
143 void insertReady (const SchedGraphNode* node);
144
145 void updateTime (cycles_t /*unused*/);
146
147 const SchedGraphNode* getNextHighest (const SchedulingManager& S,
148 cycles_t curTime);
149 // choose next highest priority instr
150
151private:
152 typedef NodeHeap::iterator candIndex;
153
154private:
155 cycles_t curTime;
156 const SchedGraph* graph;
Chris Lattner9adb7ad2002-02-04 20:02:16 +0000157 MethodLiveVarInfo &methodLiveVarInfo;
Chris Lattner697954c2002-01-20 22:54:45 +0000158 std::hash_map<const MachineInstr*, bool> lastUseMap;
159 std::vector<cycles_t> nodeDelayVec;
160 std::vector<cycles_t> earliestForNode;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000161 cycles_t earliestReadyTime;
162 NodeHeap candsAsHeap; // candidate nodes, ready to go
Chris Lattner697954c2002-01-20 22:54:45 +0000163 std::hash_set<const SchedGraphNode*> candsAsSet;//same entries as candsAsHeap,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000164 // but as set for fast lookup
Chris Lattner697954c2002-01-20 22:54:45 +0000165 std::vector<candIndex> mcands; // holds pointers into cands
Vikram S. Adve37866b32001-08-28 23:06:49 +0000166 candIndex nextToTry; // next cand after the last
167 // one tried in this cycle
168
Chris Lattner697954c2002-01-20 22:54:45 +0000169 int chooseByRule1 (std::vector<candIndex>& mcands);
170 int chooseByRule2 (std::vector<candIndex>& mcands);
171 int chooseByRule3 (std::vector<candIndex>& mcands);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000172
Chris Lattner697954c2002-01-20 22:54:45 +0000173 void findSetWithMaxDelay (std::vector<candIndex>& mcands,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000174 const SchedulingManager& S);
175
176 void computeDelays (const SchedGraph* graph);
177
178 void initializeReadyHeap (const SchedGraph* graph);
179
180 bool instructionHasLastUse (MethodLiveVarInfo& methodLiveVarInfo,
181 const SchedGraphNode* graphNode);
182
183 // NOTE: The next two return references to the actual vector entries.
184 // Use with care.
185 cycles_t& getNodeDelayRef (const SchedGraphNode* node) {
186 assert(node->getNodeId() < nodeDelayVec.size());
187 return nodeDelayVec[node->getNodeId()];
188 }
189 cycles_t& getEarliestForNodeRef (const SchedGraphNode* node) {
190 assert(node->getNodeId() < earliestForNode.size());
191 return earliestForNode[node->getNodeId()];
192 }
193};
194
195
Chris Lattnerdcd2fb52001-09-07 21:18:16 +0000196inline void SchedPriorities::updateTime(cycles_t c) {
Vikram S. Adve37866b32001-08-28 23:06:49 +0000197 curTime = c;
198 nextToTry = candsAsHeap.begin();
199 mcands.clear();
200}
201
Chris Lattner697954c2002-01-20 22:54:45 +0000202inline std::ostream &operator<<(std::ostream &os, const NodeDelayPair* nd) {
Chris Lattnerdcd2fb52001-09-07 21:18:16 +0000203 return os << "Delay for node " << nd->node->getNodeId()
Chris Lattner697954c2002-01-20 22:54:45 +0000204 << " = " << (long)nd->delay << "\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +0000205}
206
Vikram S. Adve37866b32001-08-28 23:06:49 +0000207#endif