blob: 40d86f0fabfc4d951475cc0023d39904c8f3c975 [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 Lattner5e9f3522005-03-29 20:35:10 +000020#include "DependenceAnalyzer.h"
Tanya Lattner9532ab92005-03-23 01:47:20 +000021#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 Lattner5e9f3522005-03-29 20:35:10 +000050 //Map to hold machine to llvm instrs for each valid BB
51 std::map<const MachineBasicBlock*, std::map<MachineInstr*, Instruction*> > machineTollvm;
52
Tanya Lattner80f08552004-11-02 21:04:56 +000053 //LLVM Instruction we know we can add TmpInstructions to its MCFI
54 Instruction *defaultInst;
55
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000056 //Map that holds node to node attribute information
57 std::map<MSchedGraphNode*, MSNodeAttributes> nodeToAttributesMap;
Tanya Lattner420025b2004-10-10 22:44:35 +000058
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000059 //Map to hold all reccurrences
60 std::set<std::pair<int, std::vector<MSchedGraphNode*> > > recurrenceList;
Tanya Lattner420025b2004-10-10 22:44:35 +000061
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000062 //Set of edges to ignore, stored as src node and index into vector of successors
63 std::set<std::pair<MSchedGraphNode*, unsigned> > edgesToIgnore;
64
65 //Vector containing the partial order
Tanya Lattner260652a2004-10-30 00:39:07 +000066 std::vector<std::set<MSchedGraphNode*> > partialOrder;
Tanya Lattner420025b2004-10-10 22:44:35 +000067
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000068 //Vector containing the final node order
69 std::vector<MSchedGraphNode*> FinalNodeOrder;
Tanya Lattner420025b2004-10-10 22:44:35 +000070
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000071 //Schedule table, key is the cycle number and the vector is resource, node pairs
Tanya Lattner4cffb582004-05-26 06:27:18 +000072 MSSchedule schedule;
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000073
74 //Current initiation interval
75 int II;
76
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000077 //Internal functions
Tanya Lattnerdb40cf12005-02-10 17:02:58 +000078 bool CreateDefMap(MachineBasicBlock *BI);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000079 bool MachineBBisValid(const MachineBasicBlock *BI);
Tanya Lattner9532ab92005-03-23 01:47:20 +000080 bool assocIndVar(Instruction *I, std::set<Instruction*> &indVar,
81 std::vector<Instruction*> &stack, BasicBlock *BB);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000082 int calculateResMII(const MachineBasicBlock *BI);
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000083 int calculateRecMII(MSchedGraph *graph, int MII);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000084 void calculateNodeAttributes(MSchedGraph *graph, int MII);
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000085
86 bool ignoreEdge(MSchedGraphNode *srcNode, MSchedGraphNode *destNode);
87
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000088 int calculateASAP(MSchedGraphNode *node, int MII,MSchedGraphNode *destNode);
89 int calculateALAP(MSchedGraphNode *node, int MII, int maxASAP, MSchedGraphNode *srcNode);
90
91 int calculateHeight(MSchedGraphNode *node,MSchedGraphNode *srcNode);
92 int calculateDepth(MSchedGraphNode *node, MSchedGraphNode *destNode);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000093
94 int findMaxASAP();
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000095 void orderNodes();
96 void findAllReccurrences(MSchedGraphNode *node,
97 std::vector<MSchedGraphNode*> &visitedNodes, int II);
98 void addReccurrence(std::vector<MSchedGraphNode*> &recurrence, int II, MSchedGraphNode*, MSchedGraphNode*);
99
Tanya Lattnerdb40cf12005-02-10 17:02:58 +0000100 void findAllCircuits(MSchedGraph *MSG, int II);
101 bool circuit(MSchedGraphNode *v, std::vector<MSchedGraphNode*> &stack,
102 std::set<MSchedGraphNode*> &blocked,
103 std::vector<MSchedGraphNode*> &SCC, MSchedGraphNode *s,
104 std::map<MSchedGraphNode*, std::set<MSchedGraphNode*> > &B, int II,
105 std::map<MSchedGraphNode*, MSchedGraphNode*> &newNodes);
106
107 void unblock(MSchedGraphNode *u, std::set<MSchedGraphNode*> &blocked,
108 std::map<MSchedGraphNode*, std::set<MSchedGraphNode*> > &B);
109
Tanya Lattner01b4abd2005-02-23 02:01:42 +0000110 void searchPath(MSchedGraphNode *node,
111 std::vector<MSchedGraphNode*> &path,
112 std::set<MSchedGraphNode*> &nodesToAdd);
113
Tanya Lattner9532ab92005-03-23 01:47:20 +0000114 void pathToRecc(MSchedGraphNode *node,
115 std::vector<MSchedGraphNode*> &path,
116 std::set<MSchedGraphNode*> &poSet, std::set<MSchedGraphNode*> &lastNodes);
117
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000118 void computePartialOrder();
Tanya Lattner01b4abd2005-02-23 02:01:42 +0000119
Tanya Lattner9532ab92005-03-23 01:47:20 +0000120 bool computeSchedule(const MachineBasicBlock *BB);
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000121 bool scheduleNode(MSchedGraphNode *node,
122 int start, int end);
123
Tanya Lattner260652a2004-10-30 00:39:07 +0000124 void predIntersect(std::set<MSchedGraphNode*> &CurrentSet, std::set<MSchedGraphNode*> &IntersectResult);
125 void succIntersect(std::set<MSchedGraphNode*> &CurrentSet, std::set<MSchedGraphNode*> &IntersectResult);
Tanya Lattner4cffb582004-05-26 06:27:18 +0000126
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000127 void reconstructLoop(MachineBasicBlock*);
Tanya Lattner4cffb582004-05-26 06:27:18 +0000128
129 //void saveValue(const MachineInstr*, const std::set<Value*>&, std::vector<Value*>*);
130
Tanya Lattner58fe2f02004-11-29 04:39:47 +0000131 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*);
132
Tanya Lattner9532ab92005-03-23 01:47:20 +0000133 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 +0000134
Tanya Lattner9532ab92005-03-23 01:47:20 +0000135 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 +0000136
137
Tanya Lattner9532ab92005-03-23 01:47:20 +0000138 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 +0000139
140 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 +0000141
Tanya Lattner260652a2004-10-30 00:39:07 +0000142 void connectedComponentSet(MSchedGraphNode *node, std::set<MSchedGraphNode*> &ccSet, std::set<MSchedGraphNode*> &lastNodes);
143
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000144 public:
145 ModuloSchedulingPass(TargetMachine &targ) : target(targ) {}
146 virtual bool runOnFunction(Function &F);
Tanya Lattnere1df2122004-11-22 20:41:24 +0000147 virtual const char* getPassName() const { return "ModuloScheduling"; }
Tanya Lattner9532ab92005-03-23 01:47:20 +0000148
149 // getAnalysisUsage
150 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Tanya Lattner5e9f3522005-03-29 20:35:10 +0000151 AU.addRequired<DependenceAnalyzer>();
Tanya Lattner9532ab92005-03-23 01:47:20 +0000152 }
153
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000154 };
155
156}
157
158
159#endif