blob: d1376b702d7e6132f90dff74a8f8231d48139602 [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
42 //Map that holds node to node attribute information
43 std::map<MSchedGraphNode*, MSNodeAttributes> nodeToAttributesMap;
Tanya Lattner420025b2004-10-10 22:44:35 +000044
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000045 //Map to hold all reccurrences
46 std::set<std::pair<int, std::vector<MSchedGraphNode*> > > recurrenceList;
Tanya Lattner420025b2004-10-10 22:44:35 +000047
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000048 //Set of edges to ignore, stored as src node and index into vector of successors
49 std::set<std::pair<MSchedGraphNode*, unsigned> > edgesToIgnore;
50
51 //Vector containing the partial order
52 std::vector<std::vector<MSchedGraphNode*> > partialOrder;
Tanya Lattner420025b2004-10-10 22:44:35 +000053
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000054 //Vector containing the final node order
55 std::vector<MSchedGraphNode*> FinalNodeOrder;
Tanya Lattner420025b2004-10-10 22:44:35 +000056
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000057 //Schedule table, key is the cycle number and the vector is resource, node pairs
Tanya Lattner4cffb582004-05-26 06:27:18 +000058 MSSchedule schedule;
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000059
60 //Current initiation interval
61 int II;
62
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000063 //Internal functions
64 bool MachineBBisValid(const MachineBasicBlock *BI);
65 int calculateResMII(const MachineBasicBlock *BI);
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000066 int calculateRecMII(MSchedGraph *graph, int MII);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000067 void calculateNodeAttributes(MSchedGraph *graph, int MII);
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000068
69 bool ignoreEdge(MSchedGraphNode *srcNode, MSchedGraphNode *destNode);
70
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000071 int calculateASAP(MSchedGraphNode *node, int MII,MSchedGraphNode *destNode);
72 int calculateALAP(MSchedGraphNode *node, int MII, int maxASAP, MSchedGraphNode *srcNode);
73
74 int calculateHeight(MSchedGraphNode *node,MSchedGraphNode *srcNode);
75 int calculateDepth(MSchedGraphNode *node, MSchedGraphNode *destNode);
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +000076
77 int findMaxASAP();
Tanya Lattner73e3e2e2004-05-08 16:12:10 +000078 void orderNodes();
79 void findAllReccurrences(MSchedGraphNode *node,
80 std::vector<MSchedGraphNode*> &visitedNodes, int II);
81 void addReccurrence(std::vector<MSchedGraphNode*> &recurrence, int II, MSchedGraphNode*, MSchedGraphNode*);
82
83 void computePartialOrder();
84 void computeSchedule();
85 bool scheduleNode(MSchedGraphNode *node,
86 int start, int end);
87
88 void predIntersect(std::vector<MSchedGraphNode*> &CurrentSet, std::vector<MSchedGraphNode*> &IntersectResult);
89 void succIntersect(std::vector<MSchedGraphNode*> &CurrentSet, std::vector<MSchedGraphNode*> &IntersectResult);
Tanya Lattner4cffb582004-05-26 06:27:18 +000090
Tanya Lattner0a88d2d2004-07-30 23:36:10 +000091 void reconstructLoop(MachineBasicBlock*);
Tanya Lattner4cffb582004-05-26 06:27:18 +000092
93 //void saveValue(const MachineInstr*, const std::set<Value*>&, std::vector<Value*>*);
94
Tanya Lattner420025b2004-10-10 22:44:35 +000095 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 +000096
Tanya Lattner420025b2004-10-10 22:44:35 +000097 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 +000098
99
Tanya Lattner420025b2004-10-10 22:44:35 +0000100 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 +0000101
102 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 +0000103
Tanya Lattner9b3cbdb2004-03-01 02:50:57 +0000104 public:
105 ModuloSchedulingPass(TargetMachine &targ) : target(targ) {}
106 virtual bool runOnFunction(Function &F);
107 };
108
109}
110
111
112#endif