blob: 87403cfe3138b6b2150a69212607269b1d1e0feb [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
Misha Brukman8baa01c2003-04-09 21:51:34 +000016class ModuloScheduling: NonCopyable {
17private:
Guochun Shif1c154f2003-03-27 17:57:44 +000018
Guochun Shif1c154f2003-03-27 17:57:44 +000019 typedef std::vector<ModuloSchedGraphNode*> NodeVec;
Misha Brukman8baa01c2003-04-09 21:51:34 +000020 typedef std::vector<std::vector<unsigned> > Resources;
Guochun Shif1c154f2003-03-27 17:57:44 +000021
Misha Brukman8baa01c2003-04-09 21:51:34 +000022 // The graph to feed in
23 ModuloSchedGraph &graph;
24 const TargetMachine &target;
25
26 // The BasicBlock to be scheduled
27 BasicBlock *bb;
28
29 // Iteration Interval
30 // FIXME: II may be a better name for its meaning
Guochun Shif1c154f2003-03-27 17:57:44 +000031 unsigned II;
32
Misha Brukman8baa01c2003-04-09 21:51:34 +000033 // The vector containing the nodes which have been scheduled
Guochun Shif1c154f2003-03-27 17:57:44 +000034 NodeVec nodeScheduled;
Misha Brukman8baa01c2003-04-09 21:51:34 +000035
36 // The remaining unscheduled nodes
37 const NodeVec &oNodes;
38
39 // The machine resource table
40 std::vector<std::vector<std::pair<int,int> > > resourceTable;
41
Guochun Shif1c154f2003-03-27 17:57:44 +000042 ///the schedule( with many schedule stage)
43 std::vector<std::vector<ModuloSchedGraphNode*> > schedule;
Misha Brukman8baa01c2003-04-09 21:51:34 +000044
Guochun Shif1c154f2003-03-27 17:57:44 +000045 ///the kernel(core) schedule(length = II)
46 std::vector<std::vector<ModuloSchedGraphNode*> > coreSchedule;
47
Misha Brukman8baa01c2003-04-09 21:51:34 +000048 typedef BasicBlock::InstListType InstListType;
49 typedef std::vector<std::vector<ModuloSchedGraphNode*> > vvNodeType;
Guochun Shif1c154f2003-03-27 17:57:44 +000050
51public:
Guochun Shif1c154f2003-03-27 17:57:44 +000052
Misha Brukman8baa01c2003-04-09 21:51:34 +000053 ModuloScheduling(ModuloSchedGraph & _graph):
54 graph(_graph), target(graph.getTarget()), oNodes(graph.getONodes())
55 {
56 II = graph.getMII();
57 bb = (BasicBlock *) graph.getBasicBlocks()[0];
58 instrScheduling();
59 };
Guochun Shif1c154f2003-03-27 17:57:44 +000060
Misha Brukman8baa01c2003-04-09 21:51:34 +000061 ~ModuloScheduling() {};
Guochun Shif1c154f2003-03-27 17:57:44 +000062
Misha Brukman2c821cc2003-04-10 19:19:23 +000063 // for debug information selecton
64 enum DebugLevel_t {
65 DebugLevel_NoDebugInfo,
66 DebugLevel_PrintSchedule,
67 DebugLevel_PrintScheduleProcess,
68 };
69
70 static DebugLevel_t DebugLevel;
71
72 static bool printSchedule() { return DebugLevel >= DebugLevel_PrintSchedule; }
73 static bool printScheduleProcess() {
74 return DebugLevel >= DebugLevel_PrintScheduleProcess;
75 }
76
77 // The method to compute schedule and instert epilogue and prologue
Guochun Shif1c154f2003-03-27 17:57:44 +000078 void instrScheduling();
79
Misha Brukman2c821cc2003-04-10 19:19:23 +000080 // Debug functions:
81 // Dump the schedule and core schedule
82 void dumpScheduling();
Misha Brukman8baa01c2003-04-09 21:51:34 +000083
Misha Brukman2c821cc2003-04-10 19:19:23 +000084 // Dump the input vector of nodes
85 // sch: the input vector of nodes
86 void dumpSchedule(std::vector<std::vector<ModuloSchedGraphNode*> > sch);
Guochun Shif1c154f2003-03-27 17:57:44 +000087
Misha Brukman2c821cc2003-04-10 19:19:23 +000088 // Dump the resource usage table
Guochun Shif1c154f2003-03-27 17:57:44 +000089 void dumpResourceUsageTable();
90
Misha Brukman8baa01c2003-04-09 21:51:34 +000091 //*******************internal functions*******************************
Guochun Shif1c154f2003-03-27 17:57:44 +000092private:
93 //clear memory from the last round and initialize if necessary
Misha Brukman8baa01c2003-04-09 21:51:34 +000094 void clearInitMem(const TargetSchedInfo&);
Guochun Shif1c154f2003-03-27 17:57:44 +000095
96 //compute schedule and coreSchedule with the current II
97 bool computeSchedule();
98
Misha Brukman8baa01c2003-04-09 21:51:34 +000099 BasicBlock *getSuccBB(BasicBlock *);
100 BasicBlock *getPredBB(BasicBlock *);
101 void constructPrologue(BasicBlock *prologue);
102 void constructKernel(BasicBlock *prologue,
103 BasicBlock *kernel,
104 BasicBlock *epilogue);
105 void constructEpilogue(BasicBlock *epilogue, BasicBlock *succ_bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000106
Misha Brukman8baa01c2003-04-09 21:51:34 +0000107 // update the resource table at the startCycle
108 // vec: the resouce usage
109 // startCycle: the start cycle the resouce usage is
Misha Brukman2c821cc2003-04-10 19:19:23 +0000110 void updateResourceTable(std::vector<std::vector<unsigned> > vec,
Misha Brukman8baa01c2003-04-09 21:51:34 +0000111 int startCycle);
Guochun Shif1c154f2003-03-27 17:57:44 +0000112
Misha Brukman8baa01c2003-04-09 21:51:34 +0000113 // un-do the update in the resource table in the startCycle
114 // vec: the resouce usage
115 // startCycle: the start cycle the resouce usage is
Misha Brukman2c821cc2003-04-10 19:19:23 +0000116 void undoUpdateResourceTable(std::vector<std::vector<unsigned> > vec,
Misha Brukman8baa01c2003-04-09 21:51:34 +0000117 int startCycle);
Guochun Shif1c154f2003-03-27 17:57:44 +0000118
Misha Brukman8baa01c2003-04-09 21:51:34 +0000119 // return whether the resourcetable has negative element
120 // this function is called after updateResouceTable() to determine whether a
121 // node can be scheduled at certain cycle
Guochun Shif1c154f2003-03-27 17:57:44 +0000122 bool resourceTableNegative();
123
Misha Brukman8baa01c2003-04-09 21:51:34 +0000124 // try to Schedule the node starting from start to end cycle(inclusive)
125 // if it can be scheduled, put it in the schedule and update nodeScheduled
126 // node: the node to be scheduled
127 // start: start cycle
128 // end : end cycle
129 // nodeScheduled: a vector storing nodes which has been scheduled
130 bool ScheduleNode(ModuloSchedGraphNode * node, unsigned start,
131 unsigned end, NodeVec &nodeScheduled);
Guochun Shif1c154f2003-03-27 17:57:44 +0000132
133 //each instruction has a memory of the latest clone instruction
134 //the clone instruction can be get using getClone()
Misha Brukman8baa01c2003-04-09 21:51:34 +0000135 //this function clears the memory, i.e. getClone() after calling this function
136 //returns null
Guochun Shif1c154f2003-03-27 17:57:44 +0000137 void clearCloneMemory();
138
Misha Brukman8baa01c2003-04-09 21:51:34 +0000139 //this fuction make a clone of this input Instruction and update the clone
140 //memory
Guochun Shif1c154f2003-03-27 17:57:44 +0000141 //inst: the instrution to be cloned
Misha Brukman8baa01c2003-04-09 21:51:34 +0000142 Instruction *cloneInstSetMemory(Instruction *inst);
Guochun Shif1c154f2003-03-27 17:57:44 +0000143
144 //this function update each instrutions which uses ist as its operand
145 //after update, each instruction will use ist's clone as its operand
Misha Brukman8baa01c2003-04-09 21:51:34 +0000146 void updateUseWithClone(Instruction * ist);
Guochun Shif1c154f2003-03-27 17:57:44 +0000147
148};
149
150
Misha Brukman8baa01c2003-04-09 21:51:34 +0000151class ModuloSchedulingSet:
152NonCopyable {
153private:
154
Guochun Shif1c154f2003-03-27 17:57:44 +0000155 //the graphSet to feed in
Misha Brukman8baa01c2003-04-09 21:51:34 +0000156 ModuloSchedGraphSet & graphSet;
157
158public:
Guochun Shif1c154f2003-03-27 17:57:44 +0000159
160 //constructor
161 //Scheduling graph one by one
Misha Brukman8baa01c2003-04-09 21:51:34 +0000162 ModuloSchedulingSet(ModuloSchedGraphSet _graphSet): graphSet(_graphSet) {
163 for (unsigned i = 0; i < graphSet.size(); i++) {
164 ModuloSchedGraph & graph = *(graphSet[i]);
165 if (graph.isLoop())
166 ModuloScheduling ModuloScheduling(graph);
Guochun Shif1c154f2003-03-27 17:57:44 +0000167 }
168 };
Misha Brukman8baa01c2003-04-09 21:51:34 +0000169
170 ~ModuloSchedulingSet() {};
Guochun Shif1c154f2003-03-27 17:57:44 +0000171};
172
Guochun Shif1c154f2003-03-27 17:57:44 +0000173#endif