blob: 1366884b580f093b117528b68d4f14e0ffbc2481 [file] [log] [blame]
Chris Lattner179cdfb2002-08-09 20:08:03 +00001//===-- SchedPriorities.h - Encapsulate scheduling heuristics --*- C++ -*--===//
Vikram S. Adve851d44c2001-09-18 12:49:39 +00002//
3// Strategy:
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.
Chris Lattner179cdfb2002-08-09 20:08:03 +000010//
11//===----------------------------------------------------------------------===//
Vikram S. Adve37866b32001-08-28 23:06:49 +000012
13#ifndef LLVM_CODEGEN_SCHEDPRIORITIES_H
14#define LLVM_CODEGEN_SCHEDPRIORITIES_H
15
Chris Lattner46cbff62001-09-14 16:56:32 +000016#include "SchedGraph.h"
Chris Lattnerdcd2fb52001-09-07 21:18:16 +000017#include "llvm/CodeGen/InstrScheduling.h"
Vikram S. Adve851d44c2001-09-18 12:49:39 +000018#include "llvm/Target/MachineSchedInfo.h"
Chris Lattner4a63b722002-10-28 02:11:53 +000019#include "Support/hash_set"
Chris Lattner3ff43872001-09-28 22:56:31 +000020#include <list>
Chris Lattner179cdfb2002-08-09 20:08:03 +000021
Chris Lattnere7506a32002-03-23 22:51:58 +000022class Function;
Vikram S. Adve37866b32001-08-28 23:06:49 +000023class MachineInstr;
24class SchedulingManager;
Chris Lattner483e14e2002-04-27 07:27:19 +000025class FunctionLiveVarInfo;
Vikram S. Adve37866b32001-08-28 23:06:49 +000026
Chris Lattnerb99bd2b2002-02-04 05:55:42 +000027//---------------------------------------------------------------------------
Chris Lattner77f66c12002-02-04 02:44:20 +000028// Debug option levels for instruction scheduling
Chris Lattnerb99bd2b2002-02-04 05:55:42 +000029
Chris Lattner77f66c12002-02-04 02:44:20 +000030enum SchedDebugLevel_t {
31 Sched_NoDebugInfo,
Vikram S. Adve7c7e46a2002-03-24 03:45:35 +000032 Sched_Disable,
Chris Lattner77f66c12002-02-04 02:44:20 +000033 Sched_PrintMachineCode,
34 Sched_PrintSchedTrace,
35 Sched_PrintSchedGraphs,
36};
37
Chris Lattner70e60cb2002-05-22 17:08:27 +000038extern SchedDebugLevel_t SchedDebugLevel;
Vikram S. Adve37866b32001-08-28 23:06:49 +000039
Chris Lattnerb99bd2b2002-02-04 05:55:42 +000040//---------------------------------------------------------------------------
41// Function: instrIsFeasible
42//
43// Purpose:
44// Used by the priority analysis to filter out instructions
45// that are not feasible to issue in the current cycle.
46// Should only be used during schedule construction..
47//---------------------------------------------------------------------------
48
49bool instrIsFeasible(const SchedulingManager &S, MachineOpCode opCode);
50
51
52
Vikram S. Adve37866b32001-08-28 23:06:49 +000053struct NodeDelayPair {
54 const SchedGraphNode* node;
55 cycles_t delay;
56 NodeDelayPair(const SchedGraphNode* n, cycles_t d) : node(n), delay(d) {}
Chris Lattner697954c2002-01-20 22:54:45 +000057 inline bool operator<(const NodeDelayPair& np) { return delay < np.delay; }
Vikram S. Adve37866b32001-08-28 23:06:49 +000058};
59
60inline bool
61NDPLessThan(const NodeDelayPair* np1, const NodeDelayPair* np2)
62{
Chris Lattner697954c2002-01-20 22:54:45 +000063 return np1->delay < np2->delay;
Vikram S. Adve37866b32001-08-28 23:06:49 +000064}
65
Chris Lattner697954c2002-01-20 22:54:45 +000066class NodeHeap: public std::list<NodeDelayPair*>, public NonCopyable {
Vikram S. Adve37866b32001-08-28 23:06:49 +000067public:
Chris Lattner697954c2002-01-20 22:54:45 +000068 typedef std::list<NodeDelayPair*>::iterator iterator;
69 typedef std::list<NodeDelayPair*>::const_iterator const_iterator;
Vikram S. Adve37866b32001-08-28 23:06:49 +000070
71public:
Chris Lattnerf35f2fb2002-02-04 16:35:45 +000072 NodeHeap() : _size(0) {}
Vikram S. Adve37866b32001-08-28 23:06:49 +000073
Chris Lattnerf35f2fb2002-02-04 16:35:45 +000074 inline unsigned size() const { return _size; }
Vikram S. Adve37866b32001-08-28 23:06:49 +000075
76 const SchedGraphNode* getNode (const_iterator i) const { return (*i)->node; }
77 cycles_t getDelay(const_iterator i) const { return (*i)->delay;}
78
79 inline void makeHeap() {
80 // make_heap(begin(), end(), NDPLessThan);
81 }
82
83 inline iterator findNode(const SchedGraphNode* node) {
84 for (iterator I=begin(); I != end(); ++I)
85 if (getNode(I) == node)
86 return I;
87 return end();
88 }
89
90 inline void removeNode (const SchedGraphNode* node) {
91 iterator ndpPtr = findNode(node);
92 if (ndpPtr != end())
93 {
94 delete *ndpPtr;
95 erase(ndpPtr);
96 --_size;
97 }
98 };
99
100 void insert(const SchedGraphNode* node, cycles_t delay) {
101 NodeDelayPair* ndp = new NodeDelayPair(node, delay);
102 if (_size == 0 || front()->delay < delay)
103 push_front(ndp);
104 else
105 {
106 iterator I=begin();
107 for ( ; I != end() && getDelay(I) >= delay; ++I)
108 ;
Chris Lattner697954c2002-01-20 22:54:45 +0000109 std::list<NodeDelayPair*>::insert(I, ndp);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000110 }
111 _size++;
112 }
113private:
114 unsigned int _size;
115};
116
117
118class SchedPriorities: public NonCopyable {
119public:
Chris Lattnere7506a32002-03-23 22:51:58 +0000120 SchedPriorities(const Function *F, const SchedGraph *G,
Chris Lattner483e14e2002-04-27 07:27:19 +0000121 FunctionLiveVarInfo &LVI);
Chris Lattner9adb7ad2002-02-04 20:02:16 +0000122
Vikram S. Adve37866b32001-08-28 23:06:49 +0000123
124 // This must be called before scheduling begins.
125 void initialize ();
126
127 cycles_t getTime () const { return curTime; }
128 cycles_t getEarliestReadyTime () const { return earliestReadyTime; }
129 unsigned getNumReady () const { return candsAsHeap.size(); }
130 bool nodeIsReady (const SchedGraphNode* node) const {
131 return (candsAsSet.find(node) != candsAsSet.end());
132 }
133
134 void issuedReadyNodeAt (cycles_t curTime,
135 const SchedGraphNode* node);
136
137 void insertReady (const SchedGraphNode* node);
138
139 void updateTime (cycles_t /*unused*/);
140
141 const SchedGraphNode* getNextHighest (const SchedulingManager& S,
142 cycles_t curTime);
143 // choose next highest priority instr
144
145private:
146 typedef NodeHeap::iterator candIndex;
147
148private:
149 cycles_t curTime;
150 const SchedGraph* graph;
Chris Lattner483e14e2002-04-27 07:27:19 +0000151 FunctionLiveVarInfo &methodLiveVarInfo;
Chris Lattnercb6289a2002-07-24 21:21:33 +0000152 hash_map<const MachineInstr*, bool> lastUseMap;
Chris Lattner697954c2002-01-20 22:54:45 +0000153 std::vector<cycles_t> nodeDelayVec;
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000154 std::vector<cycles_t> nodeEarliestUseVec;
155 std::vector<cycles_t> earliestReadyTimeForNode;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000156 cycles_t earliestReadyTime;
157 NodeHeap candsAsHeap; // candidate nodes, ready to go
Chris Lattnercb6289a2002-07-24 21:21:33 +0000158 hash_set<const SchedGraphNode*> candsAsSet; //same entries as candsAsHeap,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000159 // but as set for fast lookup
Chris Lattner697954c2002-01-20 22:54:45 +0000160 std::vector<candIndex> mcands; // holds pointers into cands
Vikram S. Adve37866b32001-08-28 23:06:49 +0000161 candIndex nextToTry; // next cand after the last
162 // one tried in this cycle
163
Chris Lattner697954c2002-01-20 22:54:45 +0000164 int chooseByRule1 (std::vector<candIndex>& mcands);
165 int chooseByRule2 (std::vector<candIndex>& mcands);
166 int chooseByRule3 (std::vector<candIndex>& mcands);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000167
Chris Lattner697954c2002-01-20 22:54:45 +0000168 void findSetWithMaxDelay (std::vector<candIndex>& mcands,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000169 const SchedulingManager& S);
170
171 void computeDelays (const SchedGraph* graph);
172
173 void initializeReadyHeap (const SchedGraph* graph);
174
Chris Lattner483e14e2002-04-27 07:27:19 +0000175 bool instructionHasLastUse (FunctionLiveVarInfo& LVI,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000176 const SchedGraphNode* graphNode);
177
178 // NOTE: The next two return references to the actual vector entries.
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000179 // Use the following two if you don't need to modify the value.
Vikram S. Adve37866b32001-08-28 23:06:49 +0000180 cycles_t& getNodeDelayRef (const SchedGraphNode* node) {
181 assert(node->getNodeId() < nodeDelayVec.size());
182 return nodeDelayVec[node->getNodeId()];
183 }
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000184 cycles_t& getEarliestReadyTimeForNodeRef (const SchedGraphNode* node) {
185 assert(node->getNodeId() < earliestReadyTimeForNode.size());
186 return earliestReadyTimeForNode[node->getNodeId()];
187 }
188
189 cycles_t getNodeDelay (const SchedGraphNode* node) const {
190 return ((SchedPriorities*) this)->getNodeDelayRef(node);
191 }
192 cycles_t getEarliestReadyTimeForNode(const SchedGraphNode* node) const {
193 return ((SchedPriorities*) this)->getEarliestReadyTimeForNodeRef(node);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000194 }
195};
196
197
Chris Lattnerdcd2fb52001-09-07 21:18:16 +0000198inline void SchedPriorities::updateTime(cycles_t c) {
Vikram S. Adve37866b32001-08-28 23:06:49 +0000199 curTime = c;
200 nextToTry = candsAsHeap.begin();
201 mcands.clear();
202}
203
Chris Lattner697954c2002-01-20 22:54:45 +0000204inline std::ostream &operator<<(std::ostream &os, const NodeDelayPair* nd) {
Chris Lattnerdcd2fb52001-09-07 21:18:16 +0000205 return os << "Delay for node " << nd->node->getNodeId()
Chris Lattner697954c2002-01-20 22:54:45 +0000206 << " = " << (long)nd->delay << "\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +0000207}
208
Vikram S. Adve37866b32001-08-28 23:06:49 +0000209#endif