blob: 5e1e53ebf340546544e5a91ac0fcea66db4fc167 [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 Shi139f0c22003-05-30 00:17:09 +000016// for debug information selecton
17enum ModuloSchedDebugLevel_t {
18 ModuloSchedDebugLevel_NoDebugInfo,
19 ModuloSchedDebugLevel_PrintSchedule,
20 ModuloSchedDebugLevel_PrintScheduleProcess,
21};
22
Misha Brukman8baa01c2003-04-09 21:51:34 +000023class ModuloScheduling: NonCopyable {
24private:
Guochun Shif1c154f2003-03-27 17:57:44 +000025
Guochun Shif1c154f2003-03-27 17:57:44 +000026 typedef std::vector<ModuloSchedGraphNode*> NodeVec;
Misha Brukman8baa01c2003-04-09 21:51:34 +000027 typedef std::vector<std::vector<unsigned> > Resources;
Guochun Shif1c154f2003-03-27 17:57:44 +000028
Misha Brukman8baa01c2003-04-09 21:51:34 +000029 // The graph to feed in
30 ModuloSchedGraph &graph;
31 const TargetMachine &target;
32
33 // The BasicBlock to be scheduled
34 BasicBlock *bb;
35
36 // Iteration Interval
37 // FIXME: II may be a better name for its meaning
Guochun Shif1c154f2003-03-27 17:57:44 +000038 unsigned II;
39
Misha Brukman8baa01c2003-04-09 21:51:34 +000040 // The vector containing the nodes which have been scheduled
Guochun Shif1c154f2003-03-27 17:57:44 +000041 NodeVec nodeScheduled;
Misha Brukman8baa01c2003-04-09 21:51:34 +000042
43 // The remaining unscheduled nodes
44 const NodeVec &oNodes;
45
46 // The machine resource table
47 std::vector<std::vector<std::pair<int,int> > > resourceTable;
48
Guochun Shif1c154f2003-03-27 17:57:44 +000049 ///the schedule( with many schedule stage)
50 std::vector<std::vector<ModuloSchedGraphNode*> > schedule;
Misha Brukman8baa01c2003-04-09 21:51:34 +000051
Guochun Shif1c154f2003-03-27 17:57:44 +000052 ///the kernel(core) schedule(length = II)
53 std::vector<std::vector<ModuloSchedGraphNode*> > coreSchedule;
54
Misha Brukman8baa01c2003-04-09 21:51:34 +000055 typedef BasicBlock::InstListType InstListType;
56 typedef std::vector<std::vector<ModuloSchedGraphNode*> > vvNodeType;
Guochun Shif1c154f2003-03-27 17:57:44 +000057
Guochun Shi139f0c22003-05-30 00:17:09 +000058
59
60
61
Guochun Shif1c154f2003-03-27 17:57:44 +000062public:
Guochun Shif1c154f2003-03-27 17:57:44 +000063
Misha Brukman8baa01c2003-04-09 21:51:34 +000064 ModuloScheduling(ModuloSchedGraph & _graph):
65 graph(_graph), target(graph.getTarget()), oNodes(graph.getONodes())
66 {
67 II = graph.getMII();
Guochun Shi099b0642003-06-02 17:48:56 +000068 bb = graph.getBasicBlock();
Misha Brukman8baa01c2003-04-09 21:51:34 +000069 instrScheduling();
70 };
Guochun Shif1c154f2003-03-27 17:57:44 +000071
Misha Brukman8baa01c2003-04-09 21:51:34 +000072 ~ModuloScheduling() {};
Guochun Shif1c154f2003-03-27 17:57:44 +000073
Misha Brukman2c821cc2003-04-10 19:19:23 +000074
Misha Brukman2c821cc2003-04-10 19:19:23 +000075
Guochun Shi139f0c22003-05-30 00:17:09 +000076 static bool
77 printSchedule() {
78
79 //return ModuloScheduling::DebugLevel >= DebugLevel_PrintSchedule;
80 return false;
81
82
83 }
84 static bool
85 printScheduleProcess() {
86
87 //return DebugLevel >= DebugLevel_PrintScheduleProcess;
88 return false;
89
90
Misha Brukman2c821cc2003-04-10 19:19:23 +000091 }
92
93 // The method to compute schedule and instert epilogue and prologue
Guochun Shif1c154f2003-03-27 17:57:44 +000094 void instrScheduling();
95
Misha Brukman2c821cc2003-04-10 19:19:23 +000096 // Debug functions:
97 // Dump the schedule and core schedule
98 void dumpScheduling();
Misha Brukman8baa01c2003-04-09 21:51:34 +000099
Misha Brukman2c821cc2003-04-10 19:19:23 +0000100 // Dump the input vector of nodes
101 // sch: the input vector of nodes
102 void dumpSchedule(std::vector<std::vector<ModuloSchedGraphNode*> > sch);
Guochun Shif1c154f2003-03-27 17:57:44 +0000103
Misha Brukman2c821cc2003-04-10 19:19:23 +0000104 // Dump the resource usage table
Guochun Shif1c154f2003-03-27 17:57:44 +0000105 void dumpResourceUsageTable();
106
Misha Brukman8baa01c2003-04-09 21:51:34 +0000107 //*******************internal functions*******************************
Guochun Shif1c154f2003-03-27 17:57:44 +0000108private:
109 //clear memory from the last round and initialize if necessary
Misha Brukman8baa01c2003-04-09 21:51:34 +0000110 void clearInitMem(const TargetSchedInfo&);
Guochun Shif1c154f2003-03-27 17:57:44 +0000111
112 //compute schedule and coreSchedule with the current II
113 bool computeSchedule();
114
Misha Brukman8baa01c2003-04-09 21:51:34 +0000115 BasicBlock *getSuccBB(BasicBlock *);
116 BasicBlock *getPredBB(BasicBlock *);
117 void constructPrologue(BasicBlock *prologue);
118 void constructKernel(BasicBlock *prologue,
119 BasicBlock *kernel,
120 BasicBlock *epilogue);
121 void constructEpilogue(BasicBlock *epilogue, BasicBlock *succ_bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000122
Misha Brukman8baa01c2003-04-09 21:51:34 +0000123 // update the resource table at the startCycle
124 // vec: the resouce usage
125 // startCycle: the start cycle the resouce usage is
Misha Brukman2c821cc2003-04-10 19:19:23 +0000126 void updateResourceTable(std::vector<std::vector<unsigned> > vec,
Misha Brukman8baa01c2003-04-09 21:51:34 +0000127 int startCycle);
Guochun Shif1c154f2003-03-27 17:57:44 +0000128
Misha Brukman8baa01c2003-04-09 21:51:34 +0000129 // un-do the update in the resource table in the startCycle
130 // vec: the resouce usage
131 // startCycle: the start cycle the resouce usage is
Misha Brukman2c821cc2003-04-10 19:19:23 +0000132 void undoUpdateResourceTable(std::vector<std::vector<unsigned> > vec,
Misha Brukman8baa01c2003-04-09 21:51:34 +0000133 int startCycle);
Guochun Shif1c154f2003-03-27 17:57:44 +0000134
Misha Brukman8baa01c2003-04-09 21:51:34 +0000135 // return whether the resourcetable has negative element
136 // this function is called after updateResouceTable() to determine whether a
137 // node can be scheduled at certain cycle
Guochun Shif1c154f2003-03-27 17:57:44 +0000138 bool resourceTableNegative();
139
Misha Brukman8baa01c2003-04-09 21:51:34 +0000140 // try to Schedule the node starting from start to end cycle(inclusive)
141 // if it can be scheduled, put it in the schedule and update nodeScheduled
142 // node: the node to be scheduled
143 // start: start cycle
144 // end : end cycle
145 // nodeScheduled: a vector storing nodes which has been scheduled
146 bool ScheduleNode(ModuloSchedGraphNode * node, unsigned start,
147 unsigned end, NodeVec &nodeScheduled);
Guochun Shif1c154f2003-03-27 17:57:44 +0000148
149 //each instruction has a memory of the latest clone instruction
150 //the clone instruction can be get using getClone()
Misha Brukman8baa01c2003-04-09 21:51:34 +0000151 //this function clears the memory, i.e. getClone() after calling this function
152 //returns null
Guochun Shif1c154f2003-03-27 17:57:44 +0000153 void clearCloneMemory();
154
Misha Brukman8baa01c2003-04-09 21:51:34 +0000155 //this fuction make a clone of this input Instruction and update the clone
156 //memory
Guochun Shif1c154f2003-03-27 17:57:44 +0000157 //inst: the instrution to be cloned
Misha Brukman8baa01c2003-04-09 21:51:34 +0000158 Instruction *cloneInstSetMemory(Instruction *inst);
Guochun Shif1c154f2003-03-27 17:57:44 +0000159
160 //this function update each instrutions which uses ist as its operand
161 //after update, each instruction will use ist's clone as its operand
Misha Brukman8baa01c2003-04-09 21:51:34 +0000162 void updateUseWithClone(Instruction * ist);
Guochun Shif1c154f2003-03-27 17:57:44 +0000163
164};
165
166
Misha Brukman8baa01c2003-04-09 21:51:34 +0000167class ModuloSchedulingSet:
168NonCopyable {
169private:
170
Guochun Shif1c154f2003-03-27 17:57:44 +0000171 //the graphSet to feed in
Misha Brukman8baa01c2003-04-09 21:51:34 +0000172 ModuloSchedGraphSet & graphSet;
173
174public:
Guochun Shif1c154f2003-03-27 17:57:44 +0000175
176 //constructor
177 //Scheduling graph one by one
Misha Brukman8baa01c2003-04-09 21:51:34 +0000178 ModuloSchedulingSet(ModuloSchedGraphSet _graphSet): graphSet(_graphSet) {
179 for (unsigned i = 0; i < graphSet.size(); i++) {
180 ModuloSchedGraph & graph = *(graphSet[i]);
181 if (graph.isLoop())
182 ModuloScheduling ModuloScheduling(graph);
Guochun Shif1c154f2003-03-27 17:57:44 +0000183 }
184 };
Misha Brukman8baa01c2003-04-09 21:51:34 +0000185
186 ~ModuloSchedulingSet() {};
Guochun Shif1c154f2003-03-27 17:57:44 +0000187};
188
Guochun Shif1c154f2003-03-27 17:57:44 +0000189#endif