blob: a5036cc8d6469eb08588094ebaa5a76306485046 [file] [log] [blame]
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +00001//===-- ModuloScheduling.h - Swing Modulo Scheduling------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_MODULOSCHEDULING_H
14#define LLVM_MODULOSCHEDULING_H
15
16#include "MSchedGraph.h"
Tanya Lattner4cffb582004-05-26 06:27:18 +000017#include "MSSchedule.h"
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000018#include "llvm/Function.h"
19#include "llvm/Pass.h"
20#include <set>
21
22namespace llvm {
23
24
25 //Struct to contain ModuloScheduling Specific Information for each node
26 struct MSNodeAttributes {
27 int ASAP; //Earliest time at which the opreation can be scheduled
28 int ALAP; //Latest time at which the operation can be scheduled.
29 int MOB;
30 int depth;
31 int height;
32 MSNodeAttributes(int asap=-1, int alap=-1, int mob=-1,
33 int d=-1, int h=-1) : ASAP(asap), ALAP(alap),
34 MOB(mob), depth(d),
35 height(h) {}
36 };
37
38
39 class ModuloSchedulingPass : public FunctionPass {
40 const TargetMachine &target;
41
Tanya Lattner80f08552004-11-02 21:04:56 +000042 //Map to hold Value* defs
43 std::map<const Value*, MachineInstr*> defMap;
44
45 //LLVM Instruction we know we can add TmpInstructions to its MCFI
46 Instruction *defaultInst;
47
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000048 //Map that holds node to node attribute information
49 std::map<MSchedGraphNode*, MSNodeAttributes> nodeToAttributesMap;
Tanya Lattner420025b2004-10-10 22:44:35 +000050
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000051 //Map to hold all reccurrences
52 std::set<std::pair<int, std::vector<MSchedGraphNode*> > > recurrenceList;
Tanya Lattner420025b2004-10-10 22:44:35 +000053
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000054 //Set of edges to ignore, stored as src node and index into vector of successors
55 std::set<std::pair<MSchedGraphNode*, unsigned> > edgesToIgnore;
56
57 //Vector containing the partial order
Tanya Lattner260652a2004-10-30 00:39:07 +000058 std::vector<std::set<MSchedGraphNode*> > partialOrder;
Tanya Lattner420025b2004-10-10 22:44:35 +000059
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000060 //Vector containing the final node order
61 std::vector<MSchedGraphNode*> FinalNodeOrder;
Tanya Lattner420025b2004-10-10 22:44:35 +000062
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000063 //Schedule table, key is the cycle number and the vector is resource, node pairs
Tanya Lattner4cffb582004-05-26 06:27:18 +000064 MSSchedule schedule;
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000065
66 //Current initiation interval
67 int II;
68
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000069 //Internal functions
70 bool MachineBBisValid(const MachineBasicBlock *BI);
71 int calculateResMII(const MachineBasicBlock *BI);
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000072 int calculateRecMII(MSchedGraph *graph, int MII);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000073 void calculateNodeAttributes(MSchedGraph *graph, int MII);
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000074
75 bool ignoreEdge(MSchedGraphNode *srcNode, MSchedGraphNode *destNode);
76
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000077 int calculateASAP(MSchedGraphNode *node, int MII,MSchedGraphNode *destNode);
78 int calculateALAP(MSchedGraphNode *node, int MII, int maxASAP, MSchedGraphNode *srcNode);
79
80 int calculateHeight(MSchedGraphNode *node,MSchedGraphNode *srcNode);
81 int calculateDepth(MSchedGraphNode *node, MSchedGraphNode *destNode);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000082
83 int findMaxASAP();
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000084 void orderNodes();
85 void findAllReccurrences(MSchedGraphNode *node,
86 std::vector<MSchedGraphNode*> &visitedNodes, int II);
87 void addReccurrence(std::vector<MSchedGraphNode*> &recurrence, int II, MSchedGraphNode*, MSchedGraphNode*);
88
89 void computePartialOrder();
90 void computeSchedule();
91 bool scheduleNode(MSchedGraphNode *node,
92 int start, int end);
93
Tanya Lattner260652a2004-10-30 00:39:07 +000094 void predIntersect(std::set<MSchedGraphNode*> &CurrentSet, std::set<MSchedGraphNode*> &IntersectResult);
95 void succIntersect(std::set<MSchedGraphNode*> &CurrentSet, std::set<MSchedGraphNode*> &IntersectResult);
Tanya Lattner4cffb582004-05-26 06:27:18 +000096
Tanya Lattner0a88d2d2004-07-30 23:36:10 +000097 void reconstructLoop(MachineBasicBlock*);
Tanya Lattner4cffb582004-05-26 06:27:18 +000098
99 //void saveValue(const MachineInstr*, const std::set<Value*>&, std::vector<Value*>*);
100
Tanya Lattner420025b2004-10-10 22:44:35 +0000101 void writePrologues(std::vector<MachineBasicBlock *> &prologues, MachineBasicBlock *origBB, std::vector<BasicBlock*> &llvm_prologues, std::map<const Value*, std::pair<const MSchedGraphNode*, int> > &valuesToSave, std::map<Value*, std::map<int, Value*> > &newValues, std::map<Value*, MachineBasicBlock*> &newValLocation);
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000102
Tanya Lattner420025b2004-10-10 22:44:35 +0000103 void writeEpilogues(std::vector<MachineBasicBlock *> &epilogues, const MachineBasicBlock *origBB, std::vector<BasicBlock*> &llvm_epilogues, std::map<const Value*, std::pair<const MSchedGraphNode*, int> > &valuesToSave,std::map<Value*, std::map<int, Value*> > &newValues, std::map<Value*, MachineBasicBlock*> &newValLocation, std::map<Value*, std::map<int, Value*> > &kernelPHIs);
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000104
105
Tanya Lattner420025b2004-10-10 22:44:35 +0000106 void writeKernel(BasicBlock *llvmBB, MachineBasicBlock *machineBB, std::map<const Value*, std::pair<const MSchedGraphNode*, int> > &valuesToSave, std::map<Value*, std::map<int, Value*> > &newValues, std::map<Value*, MachineBasicBlock*> &newValLocation, std::map<Value*, std::map<int, Value*> > &kernelPHIs);
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000107
108 void removePHIs(const MachineBasicBlock *origBB, std::vector<MachineBasicBlock *> &prologues, std::vector<MachineBasicBlock *> &epilogues, MachineBasicBlock *kernelBB, std::map<Value*, MachineBasicBlock*> &newValLocation);
Tanya Lattner20890832004-05-28 20:14:12 +0000109
Tanya Lattner260652a2004-10-30 00:39:07 +0000110 void connectedComponentSet(MSchedGraphNode *node, std::set<MSchedGraphNode*> &ccSet, std::set<MSchedGraphNode*> &lastNodes);
111
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000112 public:
113 ModuloSchedulingPass(TargetMachine &targ) : target(targ) {}
114 virtual bool runOnFunction(Function &F);
115 };
116
117}
118
119
120#endif