blob: c76beadcb590c7268782b5ec032e611be0ed7ebd [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 Lattnerdfb8b952002-02-24 23:01:50 +000029#include <ext/hash_set>
Anand Shukla155d2c92002-02-26 18:57:15 +000030#include <iostream>
Chris Lattnere7506a32002-03-23 22:51:58 +000031class Function;
Vikram S. Adve37866b32001-08-28 23:06:49 +000032class MachineInstr;
33class SchedulingManager;
Chris Lattner9adb7ad2002-02-04 20:02:16 +000034class MethodLiveVarInfo;
Vikram S. Adve37866b32001-08-28 23:06:49 +000035
Chris Lattnerb99bd2b2002-02-04 05:55:42 +000036//---------------------------------------------------------------------------
Chris Lattner77f66c12002-02-04 02:44:20 +000037// Debug option levels for instruction scheduling
Chris Lattnerb99bd2b2002-02-04 05:55:42 +000038
Chris Lattner77f66c12002-02-04 02:44:20 +000039enum SchedDebugLevel_t {
40 Sched_NoDebugInfo,
Vikram S. Adve7c7e46a2002-03-24 03:45:35 +000041 Sched_Disable,
Chris Lattner77f66c12002-02-04 02:44:20 +000042 Sched_PrintMachineCode,
43 Sched_PrintSchedTrace,
44 Sched_PrintSchedGraphs,
45};
46
47extern cl::Enum<SchedDebugLevel_t> SchedDebugLevel;
Vikram S. Adve37866b32001-08-28 23:06:49 +000048
Chris Lattnerb99bd2b2002-02-04 05:55:42 +000049//---------------------------------------------------------------------------
50// Function: instrIsFeasible
51//
52// Purpose:
53// Used by the priority analysis to filter out instructions
54// that are not feasible to issue in the current cycle.
55// Should only be used during schedule construction..
56//---------------------------------------------------------------------------
57
58bool instrIsFeasible(const SchedulingManager &S, MachineOpCode opCode);
59
60
61
Vikram S. Adve37866b32001-08-28 23:06:49 +000062struct NodeDelayPair {
63 const SchedGraphNode* node;
64 cycles_t delay;
65 NodeDelayPair(const SchedGraphNode* n, cycles_t d) : node(n), delay(d) {}
Chris Lattner697954c2002-01-20 22:54:45 +000066 inline bool operator<(const NodeDelayPair& np) { return delay < np.delay; }
Vikram S. Adve37866b32001-08-28 23:06:49 +000067};
68
69inline bool
70NDPLessThan(const NodeDelayPair* np1, const NodeDelayPair* np2)
71{
Chris Lattner697954c2002-01-20 22:54:45 +000072 return np1->delay < np2->delay;
Vikram S. Adve37866b32001-08-28 23:06:49 +000073}
74
Chris Lattner697954c2002-01-20 22:54:45 +000075class NodeHeap: public std::list<NodeDelayPair*>, public NonCopyable {
Vikram S. Adve37866b32001-08-28 23:06:49 +000076public:
Chris Lattner697954c2002-01-20 22:54:45 +000077 typedef std::list<NodeDelayPair*>::iterator iterator;
78 typedef std::list<NodeDelayPair*>::const_iterator const_iterator;
Vikram S. Adve37866b32001-08-28 23:06:49 +000079
80public:
Chris Lattnerf35f2fb2002-02-04 16:35:45 +000081 NodeHeap() : _size(0) {}
Vikram S. Adve37866b32001-08-28 23:06:49 +000082
Chris Lattnerf35f2fb2002-02-04 16:35:45 +000083 inline unsigned size() const { return _size; }
Vikram S. Adve37866b32001-08-28 23:06:49 +000084
85 const SchedGraphNode* getNode (const_iterator i) const { return (*i)->node; }
86 cycles_t getDelay(const_iterator i) const { return (*i)->delay;}
87
88 inline void makeHeap() {
89 // make_heap(begin(), end(), NDPLessThan);
90 }
91
92 inline iterator findNode(const SchedGraphNode* node) {
93 for (iterator I=begin(); I != end(); ++I)
94 if (getNode(I) == node)
95 return I;
96 return end();
97 }
98
99 inline void removeNode (const SchedGraphNode* node) {
100 iterator ndpPtr = findNode(node);
101 if (ndpPtr != end())
102 {
103 delete *ndpPtr;
104 erase(ndpPtr);
105 --_size;
106 }
107 };
108
109 void insert(const SchedGraphNode* node, cycles_t delay) {
110 NodeDelayPair* ndp = new NodeDelayPair(node, delay);
111 if (_size == 0 || front()->delay < delay)
112 push_front(ndp);
113 else
114 {
115 iterator I=begin();
116 for ( ; I != end() && getDelay(I) >= delay; ++I)
117 ;
Chris Lattner697954c2002-01-20 22:54:45 +0000118 std::list<NodeDelayPair*>::insert(I, ndp);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000119 }
120 _size++;
121 }
122private:
123 unsigned int _size;
124};
125
126
127class SchedPriorities: public NonCopyable {
128public:
Chris Lattnere7506a32002-03-23 22:51:58 +0000129 SchedPriorities(const Function *F, const SchedGraph *G,
130 MethodLiveVarInfo &LVI);
Chris Lattner9adb7ad2002-02-04 20:02:16 +0000131
Vikram S. Adve37866b32001-08-28 23:06:49 +0000132
133 // This must be called before scheduling begins.
134 void initialize ();
135
136 cycles_t getTime () const { return curTime; }
137 cycles_t getEarliestReadyTime () const { return earliestReadyTime; }
138 unsigned getNumReady () const { return candsAsHeap.size(); }
139 bool nodeIsReady (const SchedGraphNode* node) const {
140 return (candsAsSet.find(node) != candsAsSet.end());
141 }
142
143 void issuedReadyNodeAt (cycles_t curTime,
144 const SchedGraphNode* node);
145
146 void insertReady (const SchedGraphNode* node);
147
148 void updateTime (cycles_t /*unused*/);
149
150 const SchedGraphNode* getNextHighest (const SchedulingManager& S,
151 cycles_t curTime);
152 // choose next highest priority instr
153
154private:
155 typedef NodeHeap::iterator candIndex;
156
157private:
158 cycles_t curTime;
159 const SchedGraph* graph;
Chris Lattner9adb7ad2002-02-04 20:02:16 +0000160 MethodLiveVarInfo &methodLiveVarInfo;
Chris Lattner697954c2002-01-20 22:54:45 +0000161 std::hash_map<const MachineInstr*, bool> lastUseMap;
162 std::vector<cycles_t> nodeDelayVec;
163 std::vector<cycles_t> earliestForNode;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000164 cycles_t earliestReadyTime;
165 NodeHeap candsAsHeap; // candidate nodes, ready to go
Chris Lattner697954c2002-01-20 22:54:45 +0000166 std::hash_set<const SchedGraphNode*> candsAsSet;//same entries as candsAsHeap,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000167 // but as set for fast lookup
Chris Lattner697954c2002-01-20 22:54:45 +0000168 std::vector<candIndex> mcands; // holds pointers into cands
Vikram S. Adve37866b32001-08-28 23:06:49 +0000169 candIndex nextToTry; // next cand after the last
170 // one tried in this cycle
171
Chris Lattner697954c2002-01-20 22:54:45 +0000172 int chooseByRule1 (std::vector<candIndex>& mcands);
173 int chooseByRule2 (std::vector<candIndex>& mcands);
174 int chooseByRule3 (std::vector<candIndex>& mcands);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000175
Chris Lattner697954c2002-01-20 22:54:45 +0000176 void findSetWithMaxDelay (std::vector<candIndex>& mcands,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000177 const SchedulingManager& S);
178
179 void computeDelays (const SchedGraph* graph);
180
181 void initializeReadyHeap (const SchedGraph* graph);
182
183 bool instructionHasLastUse (MethodLiveVarInfo& methodLiveVarInfo,
184 const SchedGraphNode* graphNode);
185
186 // NOTE: The next two return references to the actual vector entries.
187 // Use with care.
188 cycles_t& getNodeDelayRef (const SchedGraphNode* node) {
189 assert(node->getNodeId() < nodeDelayVec.size());
190 return nodeDelayVec[node->getNodeId()];
191 }
192 cycles_t& getEarliestForNodeRef (const SchedGraphNode* node) {
193 assert(node->getNodeId() < earliestForNode.size());
194 return earliestForNode[node->getNodeId()];
195 }
196};
197
198
Chris Lattnerdcd2fb52001-09-07 21:18:16 +0000199inline void SchedPriorities::updateTime(cycles_t c) {
Vikram S. Adve37866b32001-08-28 23:06:49 +0000200 curTime = c;
201 nextToTry = candsAsHeap.begin();
202 mcands.clear();
203}
204
Chris Lattner697954c2002-01-20 22:54:45 +0000205inline std::ostream &operator<<(std::ostream &os, const NodeDelayPair* nd) {
Chris Lattnerdcd2fb52001-09-07 21:18:16 +0000206 return os << "Delay for node " << nd->node->getNodeId()
Chris Lattner697954c2002-01-20 22:54:45 +0000207 << " = " << (long)nd->delay << "\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +0000208}
209
Vikram S. Adve37866b32001-08-28 23:06:49 +0000210#endif