blob: b36c5b2c20a554ce49094ceef6111818eb3e1a38 [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"
Tanya Lattner9532ab92005-03-23 01:47:20 +000020#include "llvm/Analysis/AliasAnalysis.h"
21#include "llvm/Target/TargetData.h"
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000022#include <set>
23
24namespace llvm {
25
26
27 //Struct to contain ModuloScheduling Specific Information for each node
28 struct MSNodeAttributes {
29 int ASAP; //Earliest time at which the opreation can be scheduled
30 int ALAP; //Latest time at which the operation can be scheduled.
31 int MOB;
32 int depth;
33 int height;
34 MSNodeAttributes(int asap=-1, int alap=-1, int mob=-1,
35 int d=-1, int h=-1) : ASAP(asap), ALAP(alap),
36 MOB(mob), depth(d),
37 height(h) {}
38 };
39
40
41 class ModuloSchedulingPass : public FunctionPass {
42 const TargetMachine &target;
43
Tanya Lattner80f08552004-11-02 21:04:56 +000044 //Map to hold Value* defs
45 std::map<const Value*, MachineInstr*> defMap;
46
Tanya Lattner9532ab92005-03-23 01:47:20 +000047 //Map to hold list of instructions associate to the induction var for each BB
48 std::map<const MachineBasicBlock*, std::map<const MachineInstr*, unsigned> > indVarInstrs;
49
Tanya Lattner80f08552004-11-02 21:04:56 +000050 //LLVM Instruction we know we can add TmpInstructions to its MCFI
51 Instruction *defaultInst;
52
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000053 //Map that holds node to node attribute information
54 std::map<MSchedGraphNode*, MSNodeAttributes> nodeToAttributesMap;
Tanya Lattner420025b2004-10-10 22:44:35 +000055
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000056 //Map to hold all reccurrences
57 std::set<std::pair<int, std::vector<MSchedGraphNode*> > > recurrenceList;
Tanya Lattner420025b2004-10-10 22:44:35 +000058
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000059 //Set of edges to ignore, stored as src node and index into vector of successors
60 std::set<std::pair<MSchedGraphNode*, unsigned> > edgesToIgnore;
61
62 //Vector containing the partial order
Tanya Lattner260652a2004-10-30 00:39:07 +000063 std::vector<std::set<MSchedGraphNode*> > partialOrder;
Tanya Lattner420025b2004-10-10 22:44:35 +000064
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000065 //Vector containing the final node order
66 std::vector<MSchedGraphNode*> FinalNodeOrder;
Tanya Lattner420025b2004-10-10 22:44:35 +000067
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000068 //Schedule table, key is the cycle number and the vector is resource, node pairs
Tanya Lattner4cffb582004-05-26 06:27:18 +000069 MSSchedule schedule;
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000070
71 //Current initiation interval
72 int II;
73
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000074 //Internal functions
Tanya Lattnerdb40cf12005-02-10 17:02:58 +000075 bool CreateDefMap(MachineBasicBlock *BI);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000076 bool MachineBBisValid(const MachineBasicBlock *BI);
Tanya Lattner9532ab92005-03-23 01:47:20 +000077 bool assocIndVar(Instruction *I, std::set<Instruction*> &indVar,
78 std::vector<Instruction*> &stack, BasicBlock *BB);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000079 int calculateResMII(const MachineBasicBlock *BI);
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000080 int calculateRecMII(MSchedGraph *graph, int MII);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000081 void calculateNodeAttributes(MSchedGraph *graph, int MII);
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000082
83 bool ignoreEdge(MSchedGraphNode *srcNode, MSchedGraphNode *destNode);
84
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000085 int calculateASAP(MSchedGraphNode *node, int MII,MSchedGraphNode *destNode);
86 int calculateALAP(MSchedGraphNode *node, int MII, int maxASAP, MSchedGraphNode *srcNode);
87
88 int calculateHeight(MSchedGraphNode *node,MSchedGraphNode *srcNode);
89 int calculateDepth(MSchedGraphNode *node, MSchedGraphNode *destNode);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000090
91 int findMaxASAP();
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000092 void orderNodes();
93 void findAllReccurrences(MSchedGraphNode *node,
94 std::vector<MSchedGraphNode*> &visitedNodes, int II);
95 void addReccurrence(std::vector<MSchedGraphNode*> &recurrence, int II, MSchedGraphNode*, MSchedGraphNode*);
96
Tanya Lattnerdb40cf12005-02-10 17:02:58 +000097 void findAllCircuits(MSchedGraph *MSG, int II);
98 bool circuit(MSchedGraphNode *v, std::vector<MSchedGraphNode*> &stack,
99 std::set<MSchedGraphNode*> &blocked,
100 std::vector<MSchedGraphNode*> &SCC, MSchedGraphNode *s,
101 std::map<MSchedGraphNode*, std::set<MSchedGraphNode*> > &B, int II,
102 std::map<MSchedGraphNode*, MSchedGraphNode*> &newNodes);
103
104 void unblock(MSchedGraphNode *u, std::set<MSchedGraphNode*> &blocked,
105 std::map<MSchedGraphNode*, std::set<MSchedGraphNode*> > &B);
106
Tanya Lattner01b4abd2005-02-23 02:01:42 +0000107 void searchPath(MSchedGraphNode *node,
108 std::vector<MSchedGraphNode*> &path,
109 std::set<MSchedGraphNode*> &nodesToAdd);
110
Tanya Lattner9532ab92005-03-23 01:47:20 +0000111 void pathToRecc(MSchedGraphNode *node,
112 std::vector<MSchedGraphNode*> &path,
113 std::set<MSchedGraphNode*> &poSet, std::set<MSchedGraphNode*> &lastNodes);
114
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000115 void computePartialOrder();
Tanya Lattner01b4abd2005-02-23 02:01:42 +0000116
Tanya Lattner9532ab92005-03-23 01:47:20 +0000117 bool computeSchedule(const MachineBasicBlock *BB);
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000118 bool scheduleNode(MSchedGraphNode *node,
119 int start, int end);
120
Tanya Lattner260652a2004-10-30 00:39:07 +0000121 void predIntersect(std::set<MSchedGraphNode*> &CurrentSet, std::set<MSchedGraphNode*> &IntersectResult);
122 void succIntersect(std::set<MSchedGraphNode*> &CurrentSet, std::set<MSchedGraphNode*> &IntersectResult);
Tanya Lattner4cffb582004-05-26 06:27:18 +0000123
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000124 void reconstructLoop(MachineBasicBlock*);
Tanya Lattner4cffb582004-05-26 06:27:18 +0000125
126 //void saveValue(const MachineInstr*, const std::set<Value*>&, std::vector<Value*>*);
127
Tanya Lattner58fe2f02004-11-29 04:39:47 +0000128 void fixBranches(std::vector<MachineBasicBlock *> &prologues, std::vector<BasicBlock*> &llvm_prologues, MachineBasicBlock *machineBB, BasicBlock *llvmBB, std::vector<MachineBasicBlock *> &epilogues, std::vector<BasicBlock*> &llvm_epilogues, MachineBasicBlock*);
129
Tanya Lattner9532ab92005-03-23 01:47:20 +0000130 void writePrologues(std::vector<MachineBasicBlock *> &prologues, MachineBasicBlock *origBB, std::vector<BasicBlock*> &llvm_prologues, std::map<const Value*, std::pair<const MachineInstr*, int> > &valuesToSave, std::map<Value*, std::map<int, Value*> > &newValues, std::map<Value*, MachineBasicBlock*> &newValLocation);
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000131
Tanya Lattner9532ab92005-03-23 01:47:20 +0000132 void writeEpilogues(std::vector<MachineBasicBlock *> &epilogues, const MachineBasicBlock *origBB, std::vector<BasicBlock*> &llvm_epilogues, std::map<const Value*, std::pair<const MachineInstr*, 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 +0000133
134
Tanya Lattner9532ab92005-03-23 01:47:20 +0000135 void writeKernel(BasicBlock *llvmBB, MachineBasicBlock *machineBB, std::map<const Value*, std::pair<const MachineInstr*, 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 +0000136
137 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 +0000138
Tanya Lattner260652a2004-10-30 00:39:07 +0000139 void connectedComponentSet(MSchedGraphNode *node, std::set<MSchedGraphNode*> &ccSet, std::set<MSchedGraphNode*> &lastNodes);
140
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000141 public:
142 ModuloSchedulingPass(TargetMachine &targ) : target(targ) {}
143 virtual bool runOnFunction(Function &F);
Tanya Lattnere1df2122004-11-22 20:41:24 +0000144 virtual const char* getPassName() const { return "ModuloScheduling"; }
Tanya Lattner9532ab92005-03-23 01:47:20 +0000145
146 // getAnalysisUsage
147 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
148 AU.addRequired<AliasAnalysis>();
149 AU.addRequired<TargetData>();
150 }
151
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000152 };
153
154}
155
156
157#endif