Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 1 | //===-- 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 Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 17 | #include "MSSchedule.h" |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/Pass.h" |
Tanya Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame] | 20 | #include "DependenceAnalyzer.h" |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetData.h" |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 22 | #include <set> |
| 23 | |
| 24 | namespace 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 ⌖ |
| 43 | |
Tanya Lattner | 80f0855 | 2004-11-02 21:04:56 +0000 | [diff] [blame] | 44 | //Map to hold Value* defs |
| 45 | std::map<const Value*, MachineInstr*> defMap; |
| 46 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 47 | //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 Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame] | 50 | //Map to hold machine to llvm instrs for each valid BB |
| 51 | std::map<const MachineBasicBlock*, std::map<MachineInstr*, Instruction*> > machineTollvm; |
| 52 | |
Tanya Lattner | 80f0855 | 2004-11-02 21:04:56 +0000 | [diff] [blame] | 53 | //LLVM Instruction we know we can add TmpInstructions to its MCFI |
| 54 | Instruction *defaultInst; |
| 55 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 56 | //Map that holds node to node attribute information |
| 57 | std::map<MSchedGraphNode*, MSNodeAttributes> nodeToAttributesMap; |
Tanya Lattner | 420025b | 2004-10-10 22:44:35 +0000 | [diff] [blame] | 58 | |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 59 | //Map to hold all reccurrences |
| 60 | std::set<std::pair<int, std::vector<MSchedGraphNode*> > > recurrenceList; |
Tanya Lattner | 420025b | 2004-10-10 22:44:35 +0000 | [diff] [blame] | 61 | |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 62 | //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 Lattner | 260652a | 2004-10-30 00:39:07 +0000 | [diff] [blame] | 66 | std::vector<std::set<MSchedGraphNode*> > partialOrder; |
Tanya Lattner | 420025b | 2004-10-10 22:44:35 +0000 | [diff] [blame] | 67 | |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 68 | //Vector containing the final node order |
| 69 | std::vector<MSchedGraphNode*> FinalNodeOrder; |
Tanya Lattner | 420025b | 2004-10-10 22:44:35 +0000 | [diff] [blame] | 70 | |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 71 | //Schedule table, key is the cycle number and the vector is resource, node pairs |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 72 | MSSchedule schedule; |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 73 | |
| 74 | //Current initiation interval |
| 75 | int II; |
| 76 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 77 | //Internal functions |
Tanya Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 78 | bool CreateDefMap(MachineBasicBlock *BI); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 79 | bool MachineBBisValid(const MachineBasicBlock *BI); |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 80 | bool assocIndVar(Instruction *I, std::set<Instruction*> &indVar, |
| 81 | std::vector<Instruction*> &stack, BasicBlock *BB); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 82 | int calculateResMII(const MachineBasicBlock *BI); |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 83 | int calculateRecMII(MSchedGraph *graph, int MII); |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 84 | void calculateNodeAttributes(MSchedGraph *graph, int MII); |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 85 | |
| 86 | bool ignoreEdge(MSchedGraphNode *srcNode, MSchedGraphNode *destNode); |
| 87 | |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 88 | 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 Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 93 | |
| 94 | int findMaxASAP(); |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 95 | 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 Lattner | db40cf1 | 2005-02-10 17:02:58 +0000 | [diff] [blame] | 100 | 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 Lattner | 01b4abd | 2005-02-23 02:01:42 +0000 | [diff] [blame] | 110 | void searchPath(MSchedGraphNode *node, |
| 111 | std::vector<MSchedGraphNode*> &path, |
| 112 | std::set<MSchedGraphNode*> &nodesToAdd); |
| 113 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 114 | void pathToRecc(MSchedGraphNode *node, |
| 115 | std::vector<MSchedGraphNode*> &path, |
| 116 | std::set<MSchedGraphNode*> &poSet, std::set<MSchedGraphNode*> &lastNodes); |
| 117 | |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 118 | void computePartialOrder(); |
Tanya Lattner | 01b4abd | 2005-02-23 02:01:42 +0000 | [diff] [blame] | 119 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 120 | bool computeSchedule(const MachineBasicBlock *BB); |
Tanya Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 121 | bool scheduleNode(MSchedGraphNode *node, |
| 122 | int start, int end); |
| 123 | |
Tanya Lattner | 260652a | 2004-10-30 00:39:07 +0000 | [diff] [blame] | 124 | void predIntersect(std::set<MSchedGraphNode*> &CurrentSet, std::set<MSchedGraphNode*> &IntersectResult); |
| 125 | void succIntersect(std::set<MSchedGraphNode*> &CurrentSet, std::set<MSchedGraphNode*> &IntersectResult); |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 126 | |
Tanya Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 127 | void reconstructLoop(MachineBasicBlock*); |
Tanya Lattner | 4cffb58 | 2004-05-26 06:27:18 +0000 | [diff] [blame] | 128 | |
| 129 | //void saveValue(const MachineInstr*, const std::set<Value*>&, std::vector<Value*>*); |
| 130 | |
Tanya Lattner | 58fe2f0 | 2004-11-29 04:39:47 +0000 | [diff] [blame] | 131 | 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 Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 133 | 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 Lattner | 73e3e2e | 2004-05-08 16:12:10 +0000 | [diff] [blame] | 134 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 135 | 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 Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 136 | |
| 137 | |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 138 | 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 Lattner | 0a88d2d | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 139 | |
| 140 | void removePHIs(const MachineBasicBlock *origBB, std::vector<MachineBasicBlock *> &prologues, std::vector<MachineBasicBlock *> &epilogues, MachineBasicBlock *kernelBB, std::map<Value*, MachineBasicBlock*> &newValLocation); |
Tanya Lattner | 2089083 | 2004-05-28 20:14:12 +0000 | [diff] [blame] | 141 | |
Tanya Lattner | 260652a | 2004-10-30 00:39:07 +0000 | [diff] [blame] | 142 | void connectedComponentSet(MSchedGraphNode *node, std::set<MSchedGraphNode*> &ccSet, std::set<MSchedGraphNode*> &lastNodes); |
| 143 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 144 | public: |
| 145 | ModuloSchedulingPass(TargetMachine &targ) : target(targ) {} |
| 146 | virtual bool runOnFunction(Function &F); |
Tanya Lattner | e1df212 | 2004-11-22 20:41:24 +0000 | [diff] [blame] | 147 | virtual const char* getPassName() const { return "ModuloScheduling"; } |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 148 | |
| 149 | // getAnalysisUsage |
| 150 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Tanya Lattner | 5e9f352 | 2005-03-29 20:35:10 +0000 | [diff] [blame] | 151 | AU.addRequired<DependenceAnalyzer>(); |
Tanya Lattner | 9532ab9 | 2005-03-23 01:47:20 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Tanya Lattner | 9b3cbdb | 2004-03-01 02:50:57 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | } |
| 157 | |
| 158 | |
| 159 | #endif |