blob: 00cca149da233f14af3549cde7bc8983084f6241 [file] [log] [blame]
Misha Brukman8baa01c2003-04-09 21:51:34 +00001// ModuloScheduling.h -------------------------------------------*- C++ -*-===//
Guochun Shif1c154f2003-03-27 17:57:44 +00002//
Misha Brukman8baa01c2003-04-09 21:51:34 +00003// This header defines the the classes ModuloScheduling and
4// ModuloSchedulingSet's structure
Guochun Shif1c154f2003-03-27 17:57:44 +00005//
6//===----------------------------------------------------------------------===//
7
8
9#ifndef LLVM_CODEGEN_MODULOSCHEDULING_H
10#define LLVM_CODEGEN_MODULOSCHEDULING_H
11
12#include "ModuloSchedGraph.h"
13#include <iostream>
Guochun Shi6fbe5fb2003-04-06 23:56:19 +000014#include <vector>
15
Guochun Shi0e936872003-06-10 20:04:30 +000016//#define DEBUG_PRINT(x) x
17
18#define DEBUG_PRINT(x)
Guochun Shi33280522003-06-08 20:40:47 +000019
Guochun Shi139f0c22003-05-30 00:17:09 +000020// for debug information selecton
21enum ModuloSchedDebugLevel_t {
22 ModuloSchedDebugLevel_NoDebugInfo,
23 ModuloSchedDebugLevel_PrintSchedule,
24 ModuloSchedDebugLevel_PrintScheduleProcess,
25};
26
Misha Brukman8baa01c2003-04-09 21:51:34 +000027class ModuloScheduling: NonCopyable {
28private:
Guochun Shif1c154f2003-03-27 17:57:44 +000029
Guochun Shif1c154f2003-03-27 17:57:44 +000030 typedef std::vector<ModuloSchedGraphNode*> NodeVec;
Misha Brukman8baa01c2003-04-09 21:51:34 +000031 typedef std::vector<std::vector<unsigned> > Resources;
Guochun Shif1c154f2003-03-27 17:57:44 +000032
Misha Brukman8baa01c2003-04-09 21:51:34 +000033 // 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
Guochun Shif1c154f2003-03-27 17:57:44 +000042 unsigned II;
43
Misha Brukman8baa01c2003-04-09 21:51:34 +000044 // The vector containing the nodes which have been scheduled
Guochun Shif1c154f2003-03-27 17:57:44 +000045 NodeVec nodeScheduled;
Misha Brukman8baa01c2003-04-09 21:51:34 +000046
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
Guochun Shif1c154f2003-03-27 17:57:44 +000053 ///the schedule( with many schedule stage)
54 std::vector<std::vector<ModuloSchedGraphNode*> > schedule;
Misha Brukman8baa01c2003-04-09 21:51:34 +000055
Guochun Shif1c154f2003-03-27 17:57:44 +000056 ///the kernel(core) schedule(length = II)
57 std::vector<std::vector<ModuloSchedGraphNode*> > coreSchedule;
58
Misha Brukman8baa01c2003-04-09 21:51:34 +000059 typedef BasicBlock::InstListType InstListType;
60 typedef std::vector<std::vector<ModuloSchedGraphNode*> > vvNodeType;
Guochun Shif1c154f2003-03-27 17:57:44 +000061
Guochun Shi139f0c22003-05-30 00:17:09 +000062
63
64
65
Guochun Shif1c154f2003-03-27 17:57:44 +000066public:
Guochun Shif1c154f2003-03-27 17:57:44 +000067
Misha Brukman8baa01c2003-04-09 21:51:34 +000068 ModuloScheduling(ModuloSchedGraph & _graph):
69 graph(_graph), target(graph.getTarget()), oNodes(graph.getONodes())
70 {
71 II = graph.getMII();
Guochun Shi099b0642003-06-02 17:48:56 +000072 bb = graph.getBasicBlock();
Misha Brukman8baa01c2003-04-09 21:51:34 +000073 instrScheduling();
74 };
Guochun Shif1c154f2003-03-27 17:57:44 +000075
Misha Brukman8baa01c2003-04-09 21:51:34 +000076 ~ModuloScheduling() {};
Guochun Shif1c154f2003-03-27 17:57:44 +000077
Misha Brukman2c821cc2003-04-10 19:19:23 +000078
Misha Brukman2c821cc2003-04-10 19:19:23 +000079
Guochun Shi139f0c22003-05-30 00:17:09 +000080 static bool
81 printSchedule() {
82
83 //return ModuloScheduling::DebugLevel >= DebugLevel_PrintSchedule;
Guochun Shi8f1d4ab2003-06-08 23:16:07 +000084 return true;
Guochun Shi139f0c22003-05-30 00:17:09 +000085
Guochun Shi8f1d4ab2003-06-08 23:16:07 +000086
Guochun Shi139f0c22003-05-30 00:17:09 +000087 }
88 static bool
89 printScheduleProcess() {
90
91 //return DebugLevel >= DebugLevel_PrintScheduleProcess;
Guochun Shi8f1d4ab2003-06-08 23:16:07 +000092 return true;
Guochun Shi139f0c22003-05-30 00:17:09 +000093
94
Misha Brukman2c821cc2003-04-10 19:19:23 +000095 }
96
97 // The method to compute schedule and instert epilogue and prologue
Guochun Shif1c154f2003-03-27 17:57:44 +000098 void instrScheduling();
99
Misha Brukman2c821cc2003-04-10 19:19:23 +0000100 // Debug functions:
101 // Dump the schedule and core schedule
102 void dumpScheduling();
Guochun Shi0e936872003-06-10 20:04:30 +0000103 void dumpFinalSchedule();
Misha Brukman8baa01c2003-04-09 21:51:34 +0000104
Misha Brukman2c821cc2003-04-10 19:19:23 +0000105 // Dump the input vector of nodes
106 // sch: the input vector of nodes
107 void dumpSchedule(std::vector<std::vector<ModuloSchedGraphNode*> > sch);
Guochun Shif1c154f2003-03-27 17:57:44 +0000108
Misha Brukman2c821cc2003-04-10 19:19:23 +0000109 // Dump the resource usage table
Guochun Shif1c154f2003-03-27 17:57:44 +0000110 void dumpResourceUsageTable();
111
Misha Brukman8baa01c2003-04-09 21:51:34 +0000112 //*******************internal functions*******************************
Guochun Shif1c154f2003-03-27 17:57:44 +0000113private:
114 //clear memory from the last round and initialize if necessary
Misha Brukman8baa01c2003-04-09 21:51:34 +0000115 void clearInitMem(const TargetSchedInfo&);
Guochun Shif1c154f2003-03-27 17:57:44 +0000116
117 //compute schedule and coreSchedule with the current II
118 bool computeSchedule();
119
Misha Brukman8baa01c2003-04-09 21:51:34 +0000120 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);
Guochun Shif1c154f2003-03-27 17:57:44 +0000127
Misha Brukman8baa01c2003-04-09 21:51:34 +0000128 // update the resource table at the startCycle
129 // vec: the resouce usage
130 // startCycle: the start cycle the resouce usage is
Misha Brukman2c821cc2003-04-10 19:19:23 +0000131 void updateResourceTable(std::vector<std::vector<unsigned> > vec,
Misha Brukman8baa01c2003-04-09 21:51:34 +0000132 int startCycle);
Guochun Shif1c154f2003-03-27 17:57:44 +0000133
Misha Brukman8baa01c2003-04-09 21:51:34 +0000134 // 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
Misha Brukman2c821cc2003-04-10 19:19:23 +0000137 void undoUpdateResourceTable(std::vector<std::vector<unsigned> > vec,
Misha Brukman8baa01c2003-04-09 21:51:34 +0000138 int startCycle);
Guochun Shif1c154f2003-03-27 17:57:44 +0000139
Misha Brukman8baa01c2003-04-09 21:51:34 +0000140 // 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
Guochun Shif1c154f2003-03-27 17:57:44 +0000143 bool resourceTableNegative();
144
Misha Brukman8baa01c2003-04-09 21:51:34 +0000145 // 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);
Guochun Shif1c154f2003-03-27 17:57:44 +0000153
154 //each instruction has a memory of the latest clone instruction
155 //the clone instruction can be get using getClone()
Misha Brukman8baa01c2003-04-09 21:51:34 +0000156 //this function clears the memory, i.e. getClone() after calling this function
157 //returns null
Guochun Shif1c154f2003-03-27 17:57:44 +0000158 void clearCloneMemory();
159
Misha Brukman8baa01c2003-04-09 21:51:34 +0000160 //this fuction make a clone of this input Instruction and update the clone
161 //memory
Guochun Shif1c154f2003-03-27 17:57:44 +0000162 //inst: the instrution to be cloned
Misha Brukman8baa01c2003-04-09 21:51:34 +0000163 Instruction *cloneInstSetMemory(Instruction *inst);
Guochun Shif1c154f2003-03-27 17:57:44 +0000164
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
Misha Brukman8baa01c2003-04-09 21:51:34 +0000167 void updateUseWithClone(Instruction * ist);
Guochun Shif1c154f2003-03-27 17:57:44 +0000168
169};
170
171
Misha Brukman8baa01c2003-04-09 21:51:34 +0000172class ModuloSchedulingSet:
173NonCopyable {
174private:
175
Guochun Shif1c154f2003-03-27 17:57:44 +0000176 //the graphSet to feed in
Misha Brukman8baa01c2003-04-09 21:51:34 +0000177 ModuloSchedGraphSet & graphSet;
178
179public:
Guochun Shif1c154f2003-03-27 17:57:44 +0000180
181 //constructor
182 //Scheduling graph one by one
Misha Brukman8baa01c2003-04-09 21:51:34 +0000183 ModuloSchedulingSet(ModuloSchedGraphSet _graphSet): graphSet(_graphSet) {
184 for (unsigned i = 0; i < graphSet.size(); i++) {
185 ModuloSchedGraph & graph = *(graphSet[i]);
Guochun Shi8f1d4ab2003-06-08 23:16:07 +0000186 if (graph.isLoop(graph.getBasicBlock()))
Misha Brukman8baa01c2003-04-09 21:51:34 +0000187 ModuloScheduling ModuloScheduling(graph);
Guochun Shif1c154f2003-03-27 17:57:44 +0000188 }
189 };
Misha Brukman8baa01c2003-04-09 21:51:34 +0000190
191 ~ModuloSchedulingSet() {};
Guochun Shif1c154f2003-03-27 17:57:44 +0000192};
193
Guochun Shif1c154f2003-03-27 17:57:44 +0000194#endif