blob: 74704678066088137f9fc3b9e72e0e9c2774d313 [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//
John Criswell856ba762003-10-21 15:17:13 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Vikram S. Adve851d44c2001-09-18 12:49:39 +000010// Strategy:
11// Priority ordering rules:
12// (1) Max delay, which is the order of the heap S.candsAsHeap.
13// (2) Instruction that frees up a register.
14// (3) Instruction that has the maximum number of dependent instructions.
15// Note that rules 2 and 3 are only used if issue conflicts prevent
16// choosing a higher priority instruction by rule 1.
Chris Lattner179cdfb2002-08-09 20:08:03 +000017//
18//===----------------------------------------------------------------------===//
Vikram S. Adve37866b32001-08-28 23:06:49 +000019
20#ifndef LLVM_CODEGEN_SCHEDPRIORITIES_H
21#define LLVM_CODEGEN_SCHEDPRIORITIES_H
22
Chris Lattner46cbff62001-09-14 16:56:32 +000023#include "SchedGraph.h"
Chris Lattnerdcd2fb52001-09-07 21:18:16 +000024#include "llvm/CodeGen/InstrScheduling.h"
Chris Lattnerd0f166a2002-12-29 03:13:05 +000025#include "llvm/Target/TargetSchedInfo.h"
Chris Lattner4a63b722002-10-28 02:11:53 +000026#include "Support/hash_set"
Chris Lattner3ff43872001-09-28 22:56:31 +000027#include <list>
Chris Lattner179cdfb2002-08-09 20:08:03 +000028
Brian Gaeked0fde302003-11-11 22:41:34 +000029namespace llvm {
30
Chris Lattnere7506a32002-03-23 22:51:58 +000031class Function;
Vikram S. Adve37866b32001-08-28 23:06:49 +000032class MachineInstr;
33class SchedulingManager;
Chris Lattner483e14e2002-04-27 07:27:19 +000034class FunctionLiveVarInfo;
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
Chris Lattner70e60cb2002-05-22 17:08:27 +000047extern 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 Lattner9efc4d62003-06-02 22:45:07 +000075class NodeHeap : public std::list<NodeDelayPair*> {
76 NodeHeap(const NodeHeap&); // DO NOT IMPLEMENT
77 void operator=(const NodeHeap&); // DO NOT IMPLEMENT
Vikram S. Adve37866b32001-08-28 23:06:49 +000078public:
Chris Lattner697954c2002-01-20 22:54:45 +000079 typedef std::list<NodeDelayPair*>::iterator iterator;
80 typedef std::list<NodeDelayPair*>::const_iterator const_iterator;
Vikram S. Adve37866b32001-08-28 23:06:49 +000081
82public:
Chris Lattnerf35f2fb2002-02-04 16:35:45 +000083 NodeHeap() : _size(0) {}
Vikram S. Adve37866b32001-08-28 23:06:49 +000084
Chris Lattnerf35f2fb2002-02-04 16:35:45 +000085 inline unsigned size() const { return _size; }
Vikram S. Adve37866b32001-08-28 23:06:49 +000086
87 const SchedGraphNode* getNode (const_iterator i) const { return (*i)->node; }
88 cycles_t getDelay(const_iterator i) const { return (*i)->delay;}
89
90 inline void makeHeap() {
91 // make_heap(begin(), end(), NDPLessThan);
92 }
93
94 inline iterator findNode(const SchedGraphNode* node) {
95 for (iterator I=begin(); I != end(); ++I)
96 if (getNode(I) == node)
97 return I;
98 return end();
99 }
100
101 inline void removeNode (const SchedGraphNode* node) {
102 iterator ndpPtr = findNode(node);
103 if (ndpPtr != end())
104 {
105 delete *ndpPtr;
106 erase(ndpPtr);
107 --_size;
108 }
109 };
110
111 void insert(const SchedGraphNode* node, cycles_t delay) {
112 NodeDelayPair* ndp = new NodeDelayPair(node, delay);
113 if (_size == 0 || front()->delay < delay)
114 push_front(ndp);
115 else
116 {
117 iterator I=begin();
118 for ( ; I != end() && getDelay(I) >= delay; ++I)
119 ;
Chris Lattner697954c2002-01-20 22:54:45 +0000120 std::list<NodeDelayPair*>::insert(I, ndp);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000121 }
122 _size++;
123 }
124private:
125 unsigned int _size;
126};
127
128
Chris Lattner9efc4d62003-06-02 22:45:07 +0000129class SchedPriorities {
130 SchedPriorities(const SchedPriorities&); // DO NOT IMPLEMENT
131 void operator=(const SchedPriorities &); // DO NOT IMPLEMENT
Vikram S. Adve37866b32001-08-28 23:06:49 +0000132public:
Chris Lattnere7506a32002-03-23 22:51:58 +0000133 SchedPriorities(const Function *F, const SchedGraph *G,
Chris Lattner483e14e2002-04-27 07:27:19 +0000134 FunctionLiveVarInfo &LVI);
Chris Lattner9adb7ad2002-02-04 20:02:16 +0000135
Vikram S. Adve37866b32001-08-28 23:06:49 +0000136
137 // This must be called before scheduling begins.
138 void initialize ();
139
140 cycles_t getTime () const { return curTime; }
141 cycles_t getEarliestReadyTime () const { return earliestReadyTime; }
142 unsigned getNumReady () const { return candsAsHeap.size(); }
143 bool nodeIsReady (const SchedGraphNode* node) const {
144 return (candsAsSet.find(node) != candsAsSet.end());
145 }
146
147 void issuedReadyNodeAt (cycles_t curTime,
148 const SchedGraphNode* node);
149
150 void insertReady (const SchedGraphNode* node);
151
152 void updateTime (cycles_t /*unused*/);
153
154 const SchedGraphNode* getNextHighest (const SchedulingManager& S,
155 cycles_t curTime);
156 // choose next highest priority instr
157
158private:
159 typedef NodeHeap::iterator candIndex;
160
161private:
162 cycles_t curTime;
163 const SchedGraph* graph;
Chris Lattner483e14e2002-04-27 07:27:19 +0000164 FunctionLiveVarInfo &methodLiveVarInfo;
Chris Lattnercb6289a2002-07-24 21:21:33 +0000165 hash_map<const MachineInstr*, bool> lastUseMap;
Chris Lattner697954c2002-01-20 22:54:45 +0000166 std::vector<cycles_t> nodeDelayVec;
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000167 std::vector<cycles_t> nodeEarliestUseVec;
168 std::vector<cycles_t> earliestReadyTimeForNode;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000169 cycles_t earliestReadyTime;
170 NodeHeap candsAsHeap; // candidate nodes, ready to go
Chris Lattnercb6289a2002-07-24 21:21:33 +0000171 hash_set<const SchedGraphNode*> candsAsSet; //same entries as candsAsHeap,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000172 // but as set for fast lookup
Chris Lattner697954c2002-01-20 22:54:45 +0000173 std::vector<candIndex> mcands; // holds pointers into cands
Vikram S. Adve37866b32001-08-28 23:06:49 +0000174 candIndex nextToTry; // next cand after the last
175 // one tried in this cycle
176
Chris Lattner697954c2002-01-20 22:54:45 +0000177 int chooseByRule1 (std::vector<candIndex>& mcands);
178 int chooseByRule2 (std::vector<candIndex>& mcands);
179 int chooseByRule3 (std::vector<candIndex>& mcands);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000180
Chris Lattner697954c2002-01-20 22:54:45 +0000181 void findSetWithMaxDelay (std::vector<candIndex>& mcands,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000182 const SchedulingManager& S);
183
184 void computeDelays (const SchedGraph* graph);
185
186 void initializeReadyHeap (const SchedGraph* graph);
187
Chris Lattner483e14e2002-04-27 07:27:19 +0000188 bool instructionHasLastUse (FunctionLiveVarInfo& LVI,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000189 const SchedGraphNode* graphNode);
190
191 // NOTE: The next two return references to the actual vector entries.
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000192 // Use the following two if you don't need to modify the value.
Vikram S. Adve37866b32001-08-28 23:06:49 +0000193 cycles_t& getNodeDelayRef (const SchedGraphNode* node) {
194 assert(node->getNodeId() < nodeDelayVec.size());
195 return nodeDelayVec[node->getNodeId()];
196 }
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000197 cycles_t& getEarliestReadyTimeForNodeRef (const SchedGraphNode* node) {
198 assert(node->getNodeId() < earliestReadyTimeForNode.size());
199 return earliestReadyTimeForNode[node->getNodeId()];
200 }
201
202 cycles_t getNodeDelay (const SchedGraphNode* node) const {
203 return ((SchedPriorities*) this)->getNodeDelayRef(node);
204 }
205 cycles_t getEarliestReadyTimeForNode(const SchedGraphNode* node) const {
206 return ((SchedPriorities*) this)->getEarliestReadyTimeForNodeRef(node);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000207 }
208};
209
210
Chris Lattnerdcd2fb52001-09-07 21:18:16 +0000211inline void SchedPriorities::updateTime(cycles_t c) {
Vikram S. Adve37866b32001-08-28 23:06:49 +0000212 curTime = c;
213 nextToTry = candsAsHeap.begin();
214 mcands.clear();
215}
216
Chris Lattner5da2e6a2002-11-02 22:07:51 +0000217std::ostream &operator<<(std::ostream &os, const NodeDelayPair* nd);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000218
Brian Gaeked0fde302003-11-11 22:41:34 +0000219} // End llvm namespace
220
Vikram S. Adve37866b32001-08-28 23:06:49 +0000221#endif