blob: de321f9fcbf6d3cd2efe7b28052bac7392c5cd67 [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
Chris Lattnere7506a32002-03-23 22:51:58 +000029class Function;
Vikram S. Adve37866b32001-08-28 23:06:49 +000030class MachineInstr;
31class SchedulingManager;
Chris Lattner483e14e2002-04-27 07:27:19 +000032class FunctionLiveVarInfo;
Vikram S. Adve37866b32001-08-28 23:06:49 +000033
Chris Lattnerb99bd2b2002-02-04 05:55:42 +000034//---------------------------------------------------------------------------
Chris Lattner77f66c12002-02-04 02:44:20 +000035// Debug option levels for instruction scheduling
Chris Lattnerb99bd2b2002-02-04 05:55:42 +000036
Chris Lattner77f66c12002-02-04 02:44:20 +000037enum SchedDebugLevel_t {
38 Sched_NoDebugInfo,
Vikram S. Adve7c7e46a2002-03-24 03:45:35 +000039 Sched_Disable,
Chris Lattner77f66c12002-02-04 02:44:20 +000040 Sched_PrintMachineCode,
41 Sched_PrintSchedTrace,
42 Sched_PrintSchedGraphs,
43};
44
Chris Lattner70e60cb2002-05-22 17:08:27 +000045extern 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 Lattner9efc4d62003-06-02 22:45:07 +000073class NodeHeap : public std::list<NodeDelayPair*> {
74 NodeHeap(const NodeHeap&); // DO NOT IMPLEMENT
75 void operator=(const NodeHeap&); // DO NOT IMPLEMENT
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
Chris Lattner9efc4d62003-06-02 22:45:07 +0000127class SchedPriorities {
128 SchedPriorities(const SchedPriorities&); // DO NOT IMPLEMENT
129 void operator=(const SchedPriorities &); // DO NOT IMPLEMENT
Vikram S. Adve37866b32001-08-28 23:06:49 +0000130public:
Chris Lattnere7506a32002-03-23 22:51:58 +0000131 SchedPriorities(const Function *F, const SchedGraph *G,
Chris Lattner483e14e2002-04-27 07:27:19 +0000132 FunctionLiveVarInfo &LVI);
Chris Lattner9adb7ad2002-02-04 20:02:16 +0000133
Vikram S. Adve37866b32001-08-28 23:06:49 +0000134
135 // This must be called before scheduling begins.
136 void initialize ();
137
138 cycles_t getTime () const { return curTime; }
139 cycles_t getEarliestReadyTime () const { return earliestReadyTime; }
140 unsigned getNumReady () const { return candsAsHeap.size(); }
141 bool nodeIsReady (const SchedGraphNode* node) const {
142 return (candsAsSet.find(node) != candsAsSet.end());
143 }
144
145 void issuedReadyNodeAt (cycles_t curTime,
146 const SchedGraphNode* node);
147
148 void insertReady (const SchedGraphNode* node);
149
150 void updateTime (cycles_t /*unused*/);
151
152 const SchedGraphNode* getNextHighest (const SchedulingManager& S,
153 cycles_t curTime);
154 // choose next highest priority instr
155
156private:
157 typedef NodeHeap::iterator candIndex;
158
159private:
160 cycles_t curTime;
161 const SchedGraph* graph;
Chris Lattner483e14e2002-04-27 07:27:19 +0000162 FunctionLiveVarInfo &methodLiveVarInfo;
Chris Lattnercb6289a2002-07-24 21:21:33 +0000163 hash_map<const MachineInstr*, bool> lastUseMap;
Chris Lattner697954c2002-01-20 22:54:45 +0000164 std::vector<cycles_t> nodeDelayVec;
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000165 std::vector<cycles_t> nodeEarliestUseVec;
166 std::vector<cycles_t> earliestReadyTimeForNode;
Vikram S. Adve37866b32001-08-28 23:06:49 +0000167 cycles_t earliestReadyTime;
168 NodeHeap candsAsHeap; // candidate nodes, ready to go
Chris Lattnercb6289a2002-07-24 21:21:33 +0000169 hash_set<const SchedGraphNode*> candsAsSet; //same entries as candsAsHeap,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000170 // but as set for fast lookup
Chris Lattner697954c2002-01-20 22:54:45 +0000171 std::vector<candIndex> mcands; // holds pointers into cands
Vikram S. Adve37866b32001-08-28 23:06:49 +0000172 candIndex nextToTry; // next cand after the last
173 // one tried in this cycle
174
Chris Lattner697954c2002-01-20 22:54:45 +0000175 int chooseByRule1 (std::vector<candIndex>& mcands);
176 int chooseByRule2 (std::vector<candIndex>& mcands);
177 int chooseByRule3 (std::vector<candIndex>& mcands);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000178
Chris Lattner697954c2002-01-20 22:54:45 +0000179 void findSetWithMaxDelay (std::vector<candIndex>& mcands,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000180 const SchedulingManager& S);
181
182 void computeDelays (const SchedGraph* graph);
183
184 void initializeReadyHeap (const SchedGraph* graph);
185
Chris Lattner483e14e2002-04-27 07:27:19 +0000186 bool instructionHasLastUse (FunctionLiveVarInfo& LVI,
Vikram S. Adve37866b32001-08-28 23:06:49 +0000187 const SchedGraphNode* graphNode);
188
189 // NOTE: The next two return references to the actual vector entries.
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000190 // Use the following two if you don't need to modify the value.
Vikram S. Adve37866b32001-08-28 23:06:49 +0000191 cycles_t& getNodeDelayRef (const SchedGraphNode* node) {
192 assert(node->getNodeId() < nodeDelayVec.size());
193 return nodeDelayVec[node->getNodeId()];
194 }
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000195 cycles_t& getEarliestReadyTimeForNodeRef (const SchedGraphNode* node) {
196 assert(node->getNodeId() < earliestReadyTimeForNode.size());
197 return earliestReadyTimeForNode[node->getNodeId()];
198 }
199
200 cycles_t getNodeDelay (const SchedGraphNode* node) const {
201 return ((SchedPriorities*) this)->getNodeDelayRef(node);
202 }
203 cycles_t getEarliestReadyTimeForNode(const SchedGraphNode* node) const {
204 return ((SchedPriorities*) this)->getEarliestReadyTimeForNodeRef(node);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000205 }
206};
207
208
Chris Lattnerdcd2fb52001-09-07 21:18:16 +0000209inline void SchedPriorities::updateTime(cycles_t c) {
Vikram S. Adve37866b32001-08-28 23:06:49 +0000210 curTime = c;
211 nextToTry = candsAsHeap.begin();
212 mcands.clear();
213}
214
Chris Lattner5da2e6a2002-11-02 22:07:51 +0000215std::ostream &operator<<(std::ostream &os, const NodeDelayPair* nd);
Vikram S. Adve37866b32001-08-28 23:06:49 +0000216
Vikram S. Adve37866b32001-08-28 23:06:49 +0000217#endif