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