blob: 78b685daddec8eb09983d0023906ee9f2468f80f [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. Adve37866b32001-08-28 23:06:49 +000026#include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
Vikram S. Adve851d44c2001-09-18 12:49:39 +000027#include "llvm/Target/MachineSchedInfo.h"
Chris Lattner77f66c12002-02-04 02:44:20 +000028#include "Support/CommandLine.h"
Chris Lattner3ff43872001-09-28 22:56:31 +000029#include <list>
Vikram S. Adve37866b32001-08-28 23:06:49 +000030
31class Method;
32class MachineInstr;
33class SchedulingManager;
34
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 Lattner697954c2002-01-20 22:54:45 +000079 /*ctor*/ NodeHeap () : std::list<NodeDelayPair*>(), _size(0) {}
Vikram S. Adve37866b32001-08-28 23:06:49 +000080 /*dtor*/ ~NodeHeap () {}
81
82 inline unsigned int size () const { return _size; }
83
84 const SchedGraphNode* getNode (const_iterator i) const { return (*i)->node; }
85 cycles_t getDelay(const_iterator i) const { return (*i)->delay;}
86
87 inline void makeHeap() {
88 // make_heap(begin(), end(), NDPLessThan);
89 }
90
91 inline iterator findNode(const SchedGraphNode* node) {
92 for (iterator I=begin(); I != end(); ++I)
93 if (getNode(I) == node)
94 return I;
95 return end();
96 }
97
98 inline void removeNode (const SchedGraphNode* node) {
99 iterator ndpPtr = findNode(node);
100 if (ndpPtr != end())
101 {
102 delete *ndpPtr;
103 erase(ndpPtr);
104 --_size;
105 }
106 };
107
108 void insert(const SchedGraphNode* node, cycles_t delay) {
109 NodeDelayPair* ndp = new NodeDelayPair(node, delay);
110 if (_size == 0 || front()->delay < delay)
111 push_front(ndp);
112 else
113 {
114 iterator I=begin();
115 for ( ; I != end() && getDelay(I) >= delay; ++I)
116 ;
Chris Lattner697954c2002-01-20 22:54:45 +0000117 std::list<NodeDelayPair*>::insert(I, ndp);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000118 }
119 _size++;
120 }
121private:
122 unsigned int _size;
123};
124
125
126class SchedPriorities: public NonCopyable {
127public:
128 /*ctor*/ SchedPriorities (const Method* method,
129 const SchedGraph* _graph);
130
131 // This must be called before scheduling begins.
132 void initialize ();
133
134 cycles_t getTime () const { return curTime; }
135 cycles_t getEarliestReadyTime () const { return earliestReadyTime; }
136 unsigned getNumReady () const { return candsAsHeap.size(); }
137 bool nodeIsReady (const SchedGraphNode* node) const {
138 return (candsAsSet.find(node) != candsAsSet.end());
139 }
140
141 void issuedReadyNodeAt (cycles_t curTime,
142 const SchedGraphNode* node);
143
144 void insertReady (const SchedGraphNode* node);
145
146 void updateTime (cycles_t /*unused*/);
147
148 const SchedGraphNode* getNextHighest (const SchedulingManager& S,
149 cycles_t curTime);
150 // choose next highest priority instr
151
152private:
153 typedef NodeHeap::iterator candIndex;
154
155private:
156 cycles_t curTime;
157 const SchedGraph* graph;
158 MethodLiveVarInfo methodLiveVarInfo;
Chris Lattner697954c2002-01-20 22:54:45 +0000159 std::hash_map<const MachineInstr*, bool> lastUseMap;
160 std::vector<cycles_t> nodeDelayVec;
161 std::vector<cycles_t> earliestForNode;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000162 cycles_t earliestReadyTime;
163 NodeHeap candsAsHeap; // candidate nodes, ready to go
Chris Lattner697954c2002-01-20 22:54:45 +0000164 std::hash_set<const SchedGraphNode*> candsAsSet;//same entries as candsAsHeap,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000165 // but as set for fast lookup
Chris Lattner697954c2002-01-20 22:54:45 +0000166 std::vector<candIndex> mcands; // holds pointers into cands
Vikram S. Adve37866b32001-08-28 23:06:49 +0000167 candIndex nextToTry; // next cand after the last
168 // one tried in this cycle
169
Chris Lattner697954c2002-01-20 22:54:45 +0000170 int chooseByRule1 (std::vector<candIndex>& mcands);
171 int chooseByRule2 (std::vector<candIndex>& mcands);
172 int chooseByRule3 (std::vector<candIndex>& mcands);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000173
Chris Lattner697954c2002-01-20 22:54:45 +0000174 void findSetWithMaxDelay (std::vector<candIndex>& mcands,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000175 const SchedulingManager& S);
176
177 void computeDelays (const SchedGraph* graph);
178
179 void initializeReadyHeap (const SchedGraph* graph);
180
181 bool instructionHasLastUse (MethodLiveVarInfo& methodLiveVarInfo,
182 const SchedGraphNode* graphNode);
183
184 // NOTE: The next two return references to the actual vector entries.
185 // Use with care.
186 cycles_t& getNodeDelayRef (const SchedGraphNode* node) {
187 assert(node->getNodeId() < nodeDelayVec.size());
188 return nodeDelayVec[node->getNodeId()];
189 }
190 cycles_t& getEarliestForNodeRef (const SchedGraphNode* node) {
191 assert(node->getNodeId() < earliestForNode.size());
192 return earliestForNode[node->getNodeId()];
193 }
194};
195
196
Chris Lattnerdcd2fb52001-09-07 21:18:16 +0000197inline void SchedPriorities::updateTime(cycles_t c) {
Vikram S. Adve37866b32001-08-28 23:06:49 +0000198 curTime = c;
199 nextToTry = candsAsHeap.begin();
200 mcands.clear();
201}
202
Chris Lattner697954c2002-01-20 22:54:45 +0000203inline std::ostream &operator<<(std::ostream &os, const NodeDelayPair* nd) {
Chris Lattnerdcd2fb52001-09-07 21:18:16 +0000204 return os << "Delay for node " << nd->node->getNodeId()
Chris Lattner697954c2002-01-20 22:54:45 +0000205 << " = " << (long)nd->delay << "\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +0000206}
207
Vikram S. Adve37866b32001-08-28 23:06:49 +0000208#endif