blob: 499d8f380de5a1f748bd04aca61c33987edc3ca6 [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 Shi33280522003-06-08 20:40:47 +000016#define DEBUG_PRINT(x) x
17
Guochun Shi139f0c22003-05-30 00:17:09 +000018// for debug information selecton
19enum ModuloSchedDebugLevel_t {
20 ModuloSchedDebugLevel_NoDebugInfo,
21 ModuloSchedDebugLevel_PrintSchedule,
22 ModuloSchedDebugLevel_PrintScheduleProcess,
23};
24
Misha Brukman8baa01c2003-04-09 21:51:34 +000025class ModuloScheduling: NonCopyable {
26private:
Guochun Shif1c154f2003-03-27 17:57:44 +000027
Guochun Shif1c154f2003-03-27 17:57:44 +000028 typedef std::vector<ModuloSchedGraphNode*> NodeVec;
Misha Brukman8baa01c2003-04-09 21:51:34 +000029 typedef std::vector<std::vector<unsigned> > Resources;
Guochun Shif1c154f2003-03-27 17:57:44 +000030
Misha Brukman8baa01c2003-04-09 21:51:34 +000031 // The graph to feed in
32 ModuloSchedGraph &graph;
33 const TargetMachine &target;
34
35 // The BasicBlock to be scheduled
36 BasicBlock *bb;
37
38 // Iteration Interval
39 // FIXME: II may be a better name for its meaning
Guochun Shif1c154f2003-03-27 17:57:44 +000040 unsigned II;
41
Misha Brukman8baa01c2003-04-09 21:51:34 +000042 // The vector containing the nodes which have been scheduled
Guochun Shif1c154f2003-03-27 17:57:44 +000043 NodeVec nodeScheduled;
Misha Brukman8baa01c2003-04-09 21:51:34 +000044
45 // The remaining unscheduled nodes
46 const NodeVec &oNodes;
47
48 // The machine resource table
49 std::vector<std::vector<std::pair<int,int> > > resourceTable;
50
Guochun Shif1c154f2003-03-27 17:57:44 +000051 ///the schedule( with many schedule stage)
52 std::vector<std::vector<ModuloSchedGraphNode*> > schedule;
Misha Brukman8baa01c2003-04-09 21:51:34 +000053
Guochun Shif1c154f2003-03-27 17:57:44 +000054 ///the kernel(core) schedule(length = II)
55 std::vector<std::vector<ModuloSchedGraphNode*> > coreSchedule;
56
Misha Brukman8baa01c2003-04-09 21:51:34 +000057 typedef BasicBlock::InstListType InstListType;
58 typedef std::vector<std::vector<ModuloSchedGraphNode*> > vvNodeType;
Guochun Shif1c154f2003-03-27 17:57:44 +000059
Guochun Shi139f0c22003-05-30 00:17:09 +000060
61
62
63
Guochun Shif1c154f2003-03-27 17:57:44 +000064public:
Guochun Shif1c154f2003-03-27 17:57:44 +000065
Misha Brukman8baa01c2003-04-09 21:51:34 +000066 ModuloScheduling(ModuloSchedGraph & _graph):
67 graph(_graph), target(graph.getTarget()), oNodes(graph.getONodes())
68 {
69 II = graph.getMII();
Guochun Shi099b0642003-06-02 17:48:56 +000070 bb = graph.getBasicBlock();
Misha Brukman8baa01c2003-04-09 21:51:34 +000071 instrScheduling();
72 };
Guochun Shif1c154f2003-03-27 17:57:44 +000073
Misha Brukman8baa01c2003-04-09 21:51:34 +000074 ~ModuloScheduling() {};
Guochun Shif1c154f2003-03-27 17:57:44 +000075
Misha Brukman2c821cc2003-04-10 19:19:23 +000076
Misha Brukman2c821cc2003-04-10 19:19:23 +000077
Guochun Shi139f0c22003-05-30 00:17:09 +000078 static bool
79 printSchedule() {
80
81 //return ModuloScheduling::DebugLevel >= DebugLevel_PrintSchedule;
Guochun Shi8f1d4ab2003-06-08 23:16:07 +000082 return true;
Guochun Shi139f0c22003-05-30 00:17:09 +000083
Guochun Shi8f1d4ab2003-06-08 23:16:07 +000084
Guochun Shi139f0c22003-05-30 00:17:09 +000085 }
86 static bool
87 printScheduleProcess() {
88
89 //return DebugLevel >= DebugLevel_PrintScheduleProcess;
Guochun Shi8f1d4ab2003-06-08 23:16:07 +000090 return true;
Guochun Shi139f0c22003-05-30 00:17:09 +000091
92
Misha Brukman2c821cc2003-04-10 19:19:23 +000093 }
94
95 // The method to compute schedule and instert epilogue and prologue
Guochun Shif1c154f2003-03-27 17:57:44 +000096 void instrScheduling();
97
Misha Brukman2c821cc2003-04-10 19:19:23 +000098 // Debug functions:
99 // Dump the schedule and core schedule
100 void dumpScheduling();
Misha Brukman8baa01c2003-04-09 21:51:34 +0000101
Misha Brukman2c821cc2003-04-10 19:19:23 +0000102 // Dump the input vector of nodes
103 // sch: the input vector of nodes
104 void dumpSchedule(std::vector<std::vector<ModuloSchedGraphNode*> > sch);
Guochun Shif1c154f2003-03-27 17:57:44 +0000105
Misha Brukman2c821cc2003-04-10 19:19:23 +0000106 // Dump the resource usage table
Guochun Shif1c154f2003-03-27 17:57:44 +0000107 void dumpResourceUsageTable();
108
Misha Brukman8baa01c2003-04-09 21:51:34 +0000109 //*******************internal functions*******************************
Guochun Shif1c154f2003-03-27 17:57:44 +0000110private:
111 //clear memory from the last round and initialize if necessary
Misha Brukman8baa01c2003-04-09 21:51:34 +0000112 void clearInitMem(const TargetSchedInfo&);
Guochun Shif1c154f2003-03-27 17:57:44 +0000113
114 //compute schedule and coreSchedule with the current II
115 bool computeSchedule();
116
Misha Brukman8baa01c2003-04-09 21:51:34 +0000117 BasicBlock *getSuccBB(BasicBlock *);
118 BasicBlock *getPredBB(BasicBlock *);
119 void constructPrologue(BasicBlock *prologue);
120 void constructKernel(BasicBlock *prologue,
121 BasicBlock *kernel,
122 BasicBlock *epilogue);
123 void constructEpilogue(BasicBlock *epilogue, BasicBlock *succ_bb);
Guochun Shif1c154f2003-03-27 17:57:44 +0000124
Misha Brukman8baa01c2003-04-09 21:51:34 +0000125 // update the resource table at the startCycle
126 // vec: the resouce usage
127 // startCycle: the start cycle the resouce usage is
Misha Brukman2c821cc2003-04-10 19:19:23 +0000128 void updateResourceTable(std::vector<std::vector<unsigned> > vec,
Misha Brukman8baa01c2003-04-09 21:51:34 +0000129 int startCycle);
Guochun Shif1c154f2003-03-27 17:57:44 +0000130
Misha Brukman8baa01c2003-04-09 21:51:34 +0000131 // un-do the update in the resource table in the startCycle
132 // vec: the resouce usage
133 // startCycle: the start cycle the resouce usage is
Misha Brukman2c821cc2003-04-10 19:19:23 +0000134 void undoUpdateResourceTable(std::vector<std::vector<unsigned> > vec,
Misha Brukman8baa01c2003-04-09 21:51:34 +0000135 int startCycle);
Guochun Shif1c154f2003-03-27 17:57:44 +0000136
Misha Brukman8baa01c2003-04-09 21:51:34 +0000137 // return whether the resourcetable has negative element
138 // this function is called after updateResouceTable() to determine whether a
139 // node can be scheduled at certain cycle
Guochun Shif1c154f2003-03-27 17:57:44 +0000140 bool resourceTableNegative();
141
Misha Brukman8baa01c2003-04-09 21:51:34 +0000142 // try to Schedule the node starting from start to end cycle(inclusive)
143 // if it can be scheduled, put it in the schedule and update nodeScheduled
144 // node: the node to be scheduled
145 // start: start cycle
146 // end : end cycle
147 // nodeScheduled: a vector storing nodes which has been scheduled
148 bool ScheduleNode(ModuloSchedGraphNode * node, unsigned start,
149 unsigned end, NodeVec &nodeScheduled);
Guochun Shif1c154f2003-03-27 17:57:44 +0000150
151 //each instruction has a memory of the latest clone instruction
152 //the clone instruction can be get using getClone()
Misha Brukman8baa01c2003-04-09 21:51:34 +0000153 //this function clears the memory, i.e. getClone() after calling this function
154 //returns null
Guochun Shif1c154f2003-03-27 17:57:44 +0000155 void clearCloneMemory();
156
Misha Brukman8baa01c2003-04-09 21:51:34 +0000157 //this fuction make a clone of this input Instruction and update the clone
158 //memory
Guochun Shif1c154f2003-03-27 17:57:44 +0000159 //inst: the instrution to be cloned
Misha Brukman8baa01c2003-04-09 21:51:34 +0000160 Instruction *cloneInstSetMemory(Instruction *inst);
Guochun Shif1c154f2003-03-27 17:57:44 +0000161
162 //this function update each instrutions which uses ist as its operand
163 //after update, each instruction will use ist's clone as its operand
Misha Brukman8baa01c2003-04-09 21:51:34 +0000164 void updateUseWithClone(Instruction * ist);
Guochun Shif1c154f2003-03-27 17:57:44 +0000165
166};
167
168
Misha Brukman8baa01c2003-04-09 21:51:34 +0000169class ModuloSchedulingSet:
170NonCopyable {
171private:
172
Guochun Shif1c154f2003-03-27 17:57:44 +0000173 //the graphSet to feed in
Misha Brukman8baa01c2003-04-09 21:51:34 +0000174 ModuloSchedGraphSet & graphSet;
175
176public:
Guochun Shif1c154f2003-03-27 17:57:44 +0000177
178 //constructor
179 //Scheduling graph one by one
Misha Brukman8baa01c2003-04-09 21:51:34 +0000180 ModuloSchedulingSet(ModuloSchedGraphSet _graphSet): graphSet(_graphSet) {
181 for (unsigned i = 0; i < graphSet.size(); i++) {
182 ModuloSchedGraph & graph = *(graphSet[i]);
Guochun Shi8f1d4ab2003-06-08 23:16:07 +0000183 if (graph.isLoop(graph.getBasicBlock()))
Misha Brukman8baa01c2003-04-09 21:51:34 +0000184 ModuloScheduling ModuloScheduling(graph);
Guochun Shif1c154f2003-03-27 17:57:44 +0000185 }
186 };
Misha Brukman8baa01c2003-04-09 21:51:34 +0000187
188 ~ModuloSchedulingSet() {};
Guochun Shif1c154f2003-03-27 17:57:44 +0000189};
190
Guochun Shif1c154f2003-03-27 17:57:44 +0000191#endif