blob: 5ed898631406a9c2d05dc05f777dbc4d76ec633c [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
Tanya Lattnerced82222004-11-16 21:31:37 +000070 void CreateDefMap(MachineBasicBlock *BI);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000071 bool MachineBBisValid(const MachineBasicBlock *BI);
72 int calculateResMII(const MachineBasicBlock *BI);
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000073 int calculateRecMII(MSchedGraph *graph, int MII);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000074 void calculateNodeAttributes(MSchedGraph *graph, int MII);
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000075
76 bool ignoreEdge(MSchedGraphNode *srcNode, MSchedGraphNode *destNode);
77
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000078 int calculateASAP(MSchedGraphNode *node, int MII,MSchedGraphNode *destNode);
79 int calculateALAP(MSchedGraphNode *node, int MII, int maxASAP, MSchedGraphNode *srcNode);
80
81 int calculateHeight(MSchedGraphNode *node,MSchedGraphNode *srcNode);
82 int calculateDepth(MSchedGraphNode *node, MSchedGraphNode *destNode);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000083
84 int findMaxASAP();
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000085 void orderNodes();
86 void findAllReccurrences(MSchedGraphNode *node,
87 std::vector<MSchedGraphNode*> &visitedNodes, int II);
88 void addReccurrence(std::vector<MSchedGraphNode*> &recurrence, int II, MSchedGraphNode*, MSchedGraphNode*);
89
90 void computePartialOrder();
91 void computeSchedule();
92 bool scheduleNode(MSchedGraphNode *node,
93 int start, int end);
94
Tanya Lattner260652a2004-10-30 00:39:07 +000095 void predIntersect(std::set<MSchedGraphNode*> &CurrentSet, std::set<MSchedGraphNode*> &IntersectResult);
96 void succIntersect(std::set<MSchedGraphNode*> &CurrentSet, std::set<MSchedGraphNode*> &IntersectResult);
Tanya Lattner4cffb582004-05-26 06:27:18 +000097
Tanya Lattner0a88d2d2004-07-30 23:36:10 +000098 void reconstructLoop(MachineBasicBlock*);
Tanya Lattner4cffb582004-05-26 06:27:18 +000099
100 //void saveValue(const MachineInstr*, const std::set<Value*>&, std::vector<Value*>*);
101
Tanya Lattner420025b2004-10-10 22:44:35 +0000102 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 +0000103
Tanya Lattner420025b2004-10-10 22:44:35 +0000104 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 +0000105
106
Tanya Lattner420025b2004-10-10 22:44:35 +0000107 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 +0000108
109 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 +0000110
Tanya Lattner260652a2004-10-30 00:39:07 +0000111 void connectedComponentSet(MSchedGraphNode *node, std::set<MSchedGraphNode*> &ccSet, std::set<MSchedGraphNode*> &lastNodes);
112
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000113 public:
114 ModuloSchedulingPass(TargetMachine &targ) : target(targ) {}
115 virtual bool runOnFunction(Function &F);
Tanya Lattnere1df2122004-11-22 20:41:24 +0000116 virtual const char* getPassName() const { return "ModuloScheduling"; }
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000117 };
118
119}
120
121
122#endif