Fix bug where we considered function types equivalent even if they had differing...
[oota-llvm.git] / lib / Target / SparcV9 / ModuloScheduling / ModuloScheduling.h
1 // ModuloScheduling.h -------------------------------------------*- C++ -*-===//
2 //
3 // This header defines the the classes ModuloScheduling and 
4 // ModuloSchedulingSet's structure
5 //
6 //===----------------------------------------------------------------------===//
7
8
9 #ifndef LLVM_CODEGEN_MODULOSCHEDULING_H
10 #define LLVM_CODEGEN_MODULOSCHEDULING_H
11
12 #include "ModuloSchedGraph.h"
13 #include <iostream>
14 #include <vector>
15
16 //#define DEBUG_PRINT(x) x
17
18 #define DEBUG_PRINT(x) 
19
20 // for debug information selecton
21 enum ModuloSchedDebugLevel_t { 
22   ModuloSchedDebugLevel_NoDebugInfo,
23   ModuloSchedDebugLevel_PrintSchedule,
24   ModuloSchedDebugLevel_PrintScheduleProcess,
25 };
26
27 class ModuloScheduling: NonCopyable {
28 private:
29
30   typedef std::vector<ModuloSchedGraphNode*> NodeVec;
31   typedef std::vector<std::vector<unsigned> > Resources;
32
33   // The graph to feed in
34   ModuloSchedGraph &graph;
35   const TargetMachine &target;
36
37   // The BasicBlock to be scheduled
38   BasicBlock *bb;
39
40   // Iteration Interval
41   // FIXME: II may be a better name for its meaning
42   unsigned II;
43
44   // The vector containing the nodes which have been scheduled
45   NodeVec nodeScheduled;
46
47   // The remaining unscheduled nodes 
48   const NodeVec &oNodes;
49
50   // The machine resource table
51   std::vector<std::vector<std::pair<int,int> > > resourceTable;
52
53   ///the schedule( with many schedule stage)
54   std::vector<std::vector<ModuloSchedGraphNode*> > schedule;
55
56   ///the kernel(core) schedule(length = II)
57   std::vector<std::vector<ModuloSchedGraphNode*> > coreSchedule;
58
59   typedef BasicBlock::InstListType InstListType;
60   typedef std::vector<std::vector<ModuloSchedGraphNode*> > vvNodeType;
61
62
63
64
65
66 public:
67
68   ModuloScheduling(ModuloSchedGraph & _graph):
69     graph(_graph), target(graph.getTarget()), oNodes(graph.getONodes())
70   {
71     II = graph.getMII();
72     bb = graph.getBasicBlock();
73     instrScheduling();
74   };
75
76   ~ModuloScheduling() {};
77
78
79
80   static bool 
81   printSchedule() { 
82
83     //return ModuloScheduling::DebugLevel >= DebugLevel_PrintSchedule; 
84     return true;
85
86     
87   }
88   static bool 
89   printScheduleProcess() {
90   
91     //return DebugLevel >= DebugLevel_PrintScheduleProcess;
92     return true;
93
94
95   }
96
97   // The method to compute schedule and instert epilogue and prologue
98   void instrScheduling();
99
100   // Debug functions:
101   // Dump the schedule and core schedule
102   void dumpScheduling();
103   void dumpFinalSchedule();
104
105   // Dump the input vector of nodes
106   // sch: the input vector of nodes
107   void dumpSchedule(std::vector<std::vector<ModuloSchedGraphNode*> > sch);
108
109   // Dump the resource usage table
110   void dumpResourceUsageTable();
111
112   //*******************internal functions*******************************
113 private:
114   //clear memory from the last round and initialize if necessary
115   void clearInitMem(const TargetSchedInfo&);
116
117   //compute schedule and coreSchedule with the current II
118   bool computeSchedule();
119
120   BasicBlock *getSuccBB(BasicBlock *);
121   BasicBlock *getPredBB(BasicBlock *);
122   void constructPrologue(BasicBlock *prologue);
123   void constructKernel(BasicBlock *prologue,
124                        BasicBlock *kernel,
125                        BasicBlock *epilogue);
126   void constructEpilogue(BasicBlock *epilogue, BasicBlock *succ_bb);
127
128   // update the resource table at the startCycle
129   // vec: the resouce usage
130   // startCycle: the start cycle the resouce usage is
131   void updateResourceTable(std::vector<std::vector<unsigned> > vec,
132                            int startCycle);
133
134   // un-do the update in the resource table in the startCycle
135   // vec: the resouce usage
136   // startCycle: the start cycle the resouce usage is
137   void undoUpdateResourceTable(std::vector<std::vector<unsigned> > vec,
138                                int startCycle);
139
140   // return whether the resourcetable has negative element
141   // this function is called after updateResouceTable() to determine whether a
142   // node can be scheduled at certain cycle
143   bool resourceTableNegative();
144
145   // try to Schedule the node starting from start to end cycle(inclusive)
146   // if it can be scheduled, put it in the schedule and update nodeScheduled
147   // node: the node to be scheduled
148   // start: start cycle
149   // end : end cycle
150   // nodeScheduled: a vector storing nodes which has been scheduled
151   bool ScheduleNode(ModuloSchedGraphNode * node, unsigned start,
152                     unsigned end, NodeVec &nodeScheduled);
153
154   //each instruction has a memory of the latest clone instruction
155   //the clone instruction can be get using getClone() 
156   //this function clears the memory, i.e. getClone() after calling this function
157   //returns null
158   void clearCloneMemory();
159
160   //this fuction make a clone of this input Instruction and update the clone
161   //memory
162   //inst: the instrution to be cloned
163   Instruction *cloneInstSetMemory(Instruction *inst);
164
165   //this function update each instrutions which uses ist as its operand
166   //after update, each instruction will use ist's clone as its operand
167   void updateUseWithClone(Instruction * ist);
168
169 };
170
171
172 class ModuloSchedulingSet:
173 NonCopyable {
174 private:
175
176   //the graphSet to feed in
177   ModuloSchedGraphSet & graphSet;
178
179 public:
180
181   //constructor
182   //Scheduling graph one by one
183   ModuloSchedulingSet(ModuloSchedGraphSet _graphSet): graphSet(_graphSet) {
184     for (unsigned i = 0; i < graphSet.size(); i++) {
185       ModuloSchedGraph & graph = *(graphSet[i]);
186       if (graph.isLoop(graph.getBasicBlock()))
187         ModuloScheduling ModuloScheduling(graph);
188     }
189   };
190
191   ~ModuloSchedulingSet() {};
192 };
193
194 #endif