68b8ee39d5297a9e0ab8d0af2054d993efe4d569
[oota-llvm.git] / lib / Target / SparcV9 / ModuloScheduling / ModuloSchedGraph.cpp
1 //===- ModuloSchedGraph.cpp - Modulo Scheduling Graph and Set -*- C++ -*---===//
2 // 
3 // Description here
4 //===----------------------------------------------------------------------===//
5
6 #include "ModuloSchedGraph.h"
7 #include "llvm/Type.h"
8
9 ModuloSchedGraphNode::ModuloSchedGraphNode(unsigned id, int index, 
10                                            const Instruction *inst, 
11                                            const TargetMachine &targ) 
12   : SchedGraphNodeCommon(id, index), Inst(inst), Target(targ) {
13 }
14
15 void ModuloSchedGraphNode::print(std::ostream &os) const {
16   os << "Modulo Scheduling Node\n";
17 }
18
19 ModuloSchedGraph::ModuloSchedGraph(const BasicBlock *bb, const TargetMachine &targ) 
20   : SchedGraphCommon(), BB(bb), Target(targ) {
21
22   assert(BB != NULL && "Basic Block is null");
23
24   //Builds nodes from each instruction in the basic block
25   buildNodesForBB();
26
27 }
28
29 void ModuloSchedGraph::buildNodesForBB() {
30   int count = 0;
31   for (BasicBlock::const_iterator i = BB->begin(), e = BB->end(); i != e; ++i) {
32     addNode(i,new ModuloSchedGraphNode(size(), count, i, Target));
33     count++;
34   }
35
36             //Get machine instruction(s) for the llvm instruction
37             //MachineCodeForInstruction &MC = MachineCodeForInstruction::get(Node->first);
38             
39
40 }
41
42 void ModuloSchedGraph::addNode(const Instruction *I,
43                                ModuloSchedGraphNode *node) {
44   assert(node!= NULL && "New ModuloSchedGraphNode is null");
45   GraphMap[I] = node;
46 }
47
48 void ModuloSchedGraph::addDepEdges() {
49   
50   //Get Machine target information for calculating delay
51   const TargetInstrInfo &MTI = Target.getInstrInfo();
52   
53   //Loop over instruction in BB and gather dependencies
54   for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
55     
56     //Ignore instructions of the void type
57     if(I->getType() != Type::VoidTy) {
58       
59       //Iterate over def-use chain and add true dependencies
60       for (Value::use_const_iterator U = I->use_begin(), e = I->use_end(); U != e; 
61            ++U) {
62         if (Instruction *Inst = dyn_cast<Instruction>(*U)) {
63           //Check if a node already exists for this instruction
64           ModuloSchedGraph::iterator Sink = find(Inst);
65           
66           //If the instruction is in our graph, add appropriate edges
67           if(Sink->second != NULL) {
68             //assert if self loop
69             assert(&*I == Sink->first && "Use edge to itself!");
70             
71             //Create edge and set delay equal to node latency
72             //FIXME: Is it safe to do this?
73             ModuloSchedGraph::iterator Src = find(I);
74             SchedGraphEdge *trueDep = new SchedGraphEdge(&*Src->second ,&*Sink->second,
75                                                          &*I, SchedGraphEdge::TrueDep,
76                                                          Src->second->getLatency());
77             //Determine the iteration difference
78             //FIXME: Will this ever happen?
79           }
80         }
81       }
82     }
83     
84   }
85   
86   
87 }
88
89 void ModuloSchedGraph::ASAP() {
90
91
92 }
93
94 void ModuloSchedGraph::ALAP() {
95
96
97 }
98
99 void ModuloSchedGraph::MOB() {
100
101 }
102
103 void ModuloSchedGraph::ComputeDepth() {
104
105 }
106
107 void  ModuloSchedGraph::ComputeHeight() {
108
109 }
110
111 void ModuloSchedGraphSet::addGraph(ModuloSchedGraph *graph) {
112   assert(graph!=NULL && "Graph for BasicBlock is null");
113   Graphs.push_back(graph);
114 }
115
116
117 ModuloSchedGraphSet::ModuloSchedGraphSet(const Function *F, 
118                                          const TargetMachine &targ) 
119   : function(F) {
120
121   //Create graph for each BB in this function
122   for (Function::const_iterator BI = F->begin(); BI != F->end(); ++BI)
123     addGraph(new ModuloSchedGraph(BI, targ));
124 }
125
126 ModuloSchedGraphSet::~ModuloSchedGraphSet(){
127   
128   //delete all the graphs
129 }
130