blob: f5c29b27e328d74e1ee7e4dbf8a8871df87b7204 [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 Lattner3ff43872001-09-28 22:56:31 +000027#include <list>
Chris Lattnerdfb8b952002-02-24 23:01:50 +000028#include <ext/hash_set>
Anand Shukla155d2c92002-02-26 18:57:15 +000029#include <iostream>
Chris Lattnere7506a32002-03-23 22:51:58 +000030class Function;
Vikram S. Adve37866b32001-08-28 23:06:49 +000031class MachineInstr;
32class SchedulingManager;
Chris Lattner483e14e2002-04-27 07:27:19 +000033class FunctionLiveVarInfo;
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,
Vikram S. Adve7c7e46a2002-03-24 03:45:35 +000040 Sched_Disable,
Chris Lattner77f66c12002-02-04 02:44:20 +000041 Sched_PrintMachineCode,
42 Sched_PrintSchedTrace,
43 Sched_PrintSchedGraphs,
44};
45
Chris Lattner70e60cb2002-05-22 17:08:27 +000046extern SchedDebugLevel_t SchedDebugLevel;
Vikram S. Adve37866b32001-08-28 23:06:49 +000047
Chris Lattnerb99bd2b2002-02-04 05:55:42 +000048//---------------------------------------------------------------------------
49// Function: instrIsFeasible
50//
51// Purpose:
52// Used by the priority analysis to filter out instructions
53// that are not feasible to issue in the current cycle.
54// Should only be used during schedule construction..
55//---------------------------------------------------------------------------
56
57bool instrIsFeasible(const SchedulingManager &S, MachineOpCode opCode);
58
59
60
Vikram S. Adve37866b32001-08-28 23:06:49 +000061struct NodeDelayPair {
62 const SchedGraphNode* node;
63 cycles_t delay;
64 NodeDelayPair(const SchedGraphNode* n, cycles_t d) : node(n), delay(d) {}
Chris Lattner697954c2002-01-20 22:54:45 +000065 inline bool operator<(const NodeDelayPair& np) { return delay < np.delay; }
Vikram S. Adve37866b32001-08-28 23:06:49 +000066};
67
68inline bool
69NDPLessThan(const NodeDelayPair* np1, const NodeDelayPair* np2)
70{
Chris Lattner697954c2002-01-20 22:54:45 +000071 return np1->delay < np2->delay;
Vikram S. Adve37866b32001-08-28 23:06:49 +000072}
73
Chris Lattner697954c2002-01-20 22:54:45 +000074class NodeHeap: public std::list<NodeDelayPair*>, public NonCopyable {
Vikram S. Adve37866b32001-08-28 23:06:49 +000075public:
Chris Lattner697954c2002-01-20 22:54:45 +000076 typedef std::list<NodeDelayPair*>::iterator iterator;
77 typedef std::list<NodeDelayPair*>::const_iterator const_iterator;
Vikram S. Adve37866b32001-08-28 23:06:49 +000078
79public:
Chris Lattnerf35f2fb2002-02-04 16:35:45 +000080 NodeHeap() : _size(0) {}
Vikram S. Adve37866b32001-08-28 23:06:49 +000081
Chris Lattnerf35f2fb2002-02-04 16:35:45 +000082 inline unsigned size() const { return _size; }
Vikram S. Adve37866b32001-08-28 23:06:49 +000083
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:
Chris Lattnere7506a32002-03-23 22:51:58 +0000128 SchedPriorities(const Function *F, const SchedGraph *G,
Chris Lattner483e14e2002-04-27 07:27:19 +0000129 FunctionLiveVarInfo &LVI);
Chris Lattner9adb7ad2002-02-04 20:02:16 +0000130
Vikram S. Adve37866b32001-08-28 23:06:49 +0000131
132 // This must be called before scheduling begins.
133 void initialize ();
134
135 cycles_t getTime () const { return curTime; }
136 cycles_t getEarliestReadyTime () const { return earliestReadyTime; }
137 unsigned getNumReady () const { return candsAsHeap.size(); }
138 bool nodeIsReady (const SchedGraphNode* node) const {
139 return (candsAsSet.find(node) != candsAsSet.end());
140 }
141
142 void issuedReadyNodeAt (cycles_t curTime,
143 const SchedGraphNode* node);
144
145 void insertReady (const SchedGraphNode* node);
146
147 void updateTime (cycles_t /*unused*/);
148
149 const SchedGraphNode* getNextHighest (const SchedulingManager& S,
150 cycles_t curTime);
151 // choose next highest priority instr
152
153private:
154 typedef NodeHeap::iterator candIndex;
155
156private:
157 cycles_t curTime;
158 const SchedGraph* graph;
Chris Lattner483e14e2002-04-27 07:27:19 +0000159 FunctionLiveVarInfo &methodLiveVarInfo;
Chris Lattner697954c2002-01-20 22:54:45 +0000160 std::hash_map<const MachineInstr*, bool> lastUseMap;
161 std::vector<cycles_t> nodeDelayVec;
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000162 std::vector<cycles_t> nodeEarliestUseVec;
163 std::vector<cycles_t> earliestReadyTimeForNode;
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
Chris Lattner483e14e2002-04-27 07:27:19 +0000183 bool instructionHasLastUse (FunctionLiveVarInfo& LVI,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000184 const SchedGraphNode* graphNode);
185
186 // NOTE: The next two return references to the actual vector entries.
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000187 // Use the following two if you don't need to modify the value.
Vikram S. Adve37866b32001-08-28 23:06:49 +0000188 cycles_t& getNodeDelayRef (const SchedGraphNode* node) {
189 assert(node->getNodeId() < nodeDelayVec.size());
190 return nodeDelayVec[node->getNodeId()];
191 }
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000192 cycles_t& getEarliestReadyTimeForNodeRef (const SchedGraphNode* node) {
193 assert(node->getNodeId() < earliestReadyTimeForNode.size());
194 return earliestReadyTimeForNode[node->getNodeId()];
195 }
196
197 cycles_t getNodeDelay (const SchedGraphNode* node) const {
198 return ((SchedPriorities*) this)->getNodeDelayRef(node);
199 }
200 cycles_t getEarliestReadyTimeForNode(const SchedGraphNode* node) const {
201 return ((SchedPriorities*) this)->getEarliestReadyTimeForNodeRef(node);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000202 }
203};
204
205
Chris Lattnerdcd2fb52001-09-07 21:18:16 +0000206inline void SchedPriorities::updateTime(cycles_t c) {
Vikram S. Adve37866b32001-08-28 23:06:49 +0000207 curTime = c;
208 nextToTry = candsAsHeap.begin();
209 mcands.clear();
210}
211
Chris Lattner697954c2002-01-20 22:54:45 +0000212inline std::ostream &operator<<(std::ostream &os, const NodeDelayPair* nd) {
Chris Lattnerdcd2fb52001-09-07 21:18:16 +0000213 return os << "Delay for node " << nd->node->getNodeId()
Chris Lattner697954c2002-01-20 22:54:45 +0000214 << " = " << (long)nd->delay << "\n";
Vikram S. Adve37866b32001-08-28 23:06:49 +0000215}
216
Vikram S. Adve37866b32001-08-28 23:06:49 +0000217#endif