blob: fa8f7b0004115cbbfe9dc3f2b5828741bae78c25 [file] [log] [blame]
Vikram S. Adve96b21c12002-12-08 13:26:29 +00001//===- DependenceGraph.cpp - Dependence graph for a function ----*- C++ -*-===//
2//
3// This file implments an explicit representation for the dependence graph
4// of a function, with one node per instruction and one edge per dependence.
5// Dependences include both data and control dependences.
6//
7// Each dep. graph node (class DepGraphNode) keeps lists of incoming and
8// outgoing dependence edges.
9//
10// Each dep. graph edge (class Dependence) keeps a pointer to one end-point
11// of the dependence. This saves space and is important because dep. graphs
12// can grow quickly. It works just fine because the standard idiom is to
13// start with a known node and enumerate the dependences to or from that node.
14//===----------------------------------------------------------------------===//
15
16
17#include "llvm/Analysis/DependenceGraph.h"
18#include "llvm/Function.h"
19#include "llvm/BasicBlock.h"
20#include "llvm/Instruction.h"
21
22
23//----------------------------------------------------------------------------
24// class Dependence:
25//
26// A representation of a simple (non-loop-related) dependence
27//----------------------------------------------------------------------------
28
29void Dependence::print(std::ostream &O) const
30{
31 assert(depType != NoDependence && "This dependence should never be created!");
32 switch (depType) {
33 case TrueDependence: O << "TRUE dependence"; break;
34 case AntiDependence: O << "ANTI dependence"; break;
35 case OutputDependence: O << "OUTPUT dependence"; break;
36 case ControlDependence: O << "CONTROL dependence"; break;
37 default: assert(0 && "Invalid dependence type"); break;
38 }
39}
40
41
42//----------------------------------------------------------------------------
43// class DepGraphNode
44//----------------------------------------------------------------------------
45
46void DepGraphNode::print(std::ostream &O) const
47{
48 const_iterator DI = outDepBegin(), DE = outDepEnd();
49
50 O << "\nDeps. from instr:" << getInstr();
51
52 for ( ; DI != DE; ++DI)
53 {
54 O << "\t";
55 DI->print(O);
56 O << " to instruction:";
57 O << DI->getSink()->getInstr();
58 }
59}
60
61//----------------------------------------------------------------------------
62// class DependenceGraph
63//----------------------------------------------------------------------------
64
65DependenceGraph::~DependenceGraph()
66{
67 // Free all DepGraphNode objects created for this graph
68 for (map_iterator I = depNodeMap.begin(), E = depNodeMap.end(); I != E; ++I)
69 delete I->second;
70}
71
72void DependenceGraph::print(const Function& func, std::ostream &O) const
73{
74 O << "DEPENDENCE GRAPH FOR FUNCTION " << func.getName() << ":\n";
75 for (Function::const_iterator BB=func.begin(), FE=func.end(); BB != FE; ++BB)
76 for (BasicBlock::const_iterator II=BB->begin(), IE=BB->end(); II !=IE; ++II)
77 if (const DepGraphNode* dgNode = this->getNode(*II))
78 dgNode->print(O);
79}