1 //===- ModuloSchedGraph.cpp - Modulo Scheduling Graph and Set -*- C++ -*---===//
4 //===----------------------------------------------------------------------===//
6 #include "ModuloSchedGraph.h"
9 ModuloSchedGraphNode::ModuloSchedGraphNode(unsigned id, int index,
10 const Instruction *inst,
11 const TargetMachine &targ)
12 : SchedGraphNodeCommon(id, index), Inst(inst), Target(targ) {
15 void ModuloSchedGraphNode::print(std::ostream &os) const {
16 os << "Modulo Scheduling Node\n";
19 ModuloSchedGraph::ModuloSchedGraph(const BasicBlock *bb, const TargetMachine &targ)
20 : SchedGraphCommon(), BB(bb), Target(targ) {
22 assert(BB != NULL && "Basic Block is null");
24 //Builds nodes from each instruction in the basic block
29 void ModuloSchedGraph::buildNodesForBB() {
31 for (BasicBlock::const_iterator i = BB->begin(), e = BB->end(); i != e; ++i) {
32 addNode(i,new ModuloSchedGraphNode(size(), count, i, Target));
36 //Get machine instruction(s) for the llvm instruction
37 //MachineCodeForInstruction &MC = MachineCodeForInstruction::get(Node->first);
42 void ModuloSchedGraph::addNode(const Instruction *I,
43 ModuloSchedGraphNode *node) {
44 assert(node!= NULL && "New ModuloSchedGraphNode is null");
48 void ModuloSchedGraph::addDepEdges() {
50 //Get Machine target information for calculating delay
51 const TargetInstrInfo &MTI = Target.getInstrInfo();
53 //Loop over instruction in BB and gather dependencies
54 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
56 //Ignore instructions of the void type
57 if(I->getType() != Type::VoidTy) {
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;
62 if (Instruction *Inst = dyn_cast<Instruction>(*U)) {
63 //Check if a node already exists for this instruction
64 ModuloSchedGraph::iterator Sink = find(Inst);
66 //If the instruction is in our graph, add appropriate edges
67 if(Sink->second != NULL) {
69 assert(&*I == Sink->first && "Use edge to itself!");
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?
89 void ModuloSchedGraph::ASAP() {
94 void ModuloSchedGraph::ALAP() {
99 void ModuloSchedGraph::MOB() {
103 void ModuloSchedGraph::ComputeDepth() {
107 void ModuloSchedGraph::ComputeHeight() {
111 void ModuloSchedGraphSet::addGraph(ModuloSchedGraph *graph) {
112 assert(graph!=NULL && "Graph for BasicBlock is null");
113 Graphs.push_back(graph);
117 ModuloSchedGraphSet::ModuloSchedGraphSet(const Function *F,
118 const TargetMachine &targ)
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));
126 ModuloSchedGraphSet::~ModuloSchedGraphSet(){
128 //delete all the graphs