Implement writer support for Loops, Induction Variables, and CallGraphs
[oota-llvm.git] / lib / Analysis / Writer.cpp
1 //===-- Analysis/Writer.cpp - Printing routines for analyses -----*- C++ -*--=//
2 //
3 // This library file implements analysis result printing support for 
4 // llvm/Analysis/Writer.h
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/Analysis/Writer.h"
9 #include "llvm/Analysis/IntervalPartition.h"
10 #include "llvm/Analysis/Dominators.h"
11 #include "llvm/Analysis/LoopInfo.h"
12 #include "llvm/Analysis/InductionVariable.h"
13 #include <iterator>
14 #include <algorithm>
15
16 //===----------------------------------------------------------------------===//
17 //  Interval Printing Routines
18 //===----------------------------------------------------------------------===//
19
20 void cfg::WriteToOutput(const Interval *I, ostream &o) {
21   o << "-------------------------------------------------------------\n"
22        << "Interval Contents:\n";
23   
24   // Print out all of the basic blocks in the interval...
25   copy(I->Nodes.begin(), I->Nodes.end(), 
26        ostream_iterator<BasicBlock*>(o, "\n"));
27
28   o << "Interval Predecessors:\n";
29   copy(I->Predecessors.begin(), I->Predecessors.end(), 
30        ostream_iterator<BasicBlock*>(o, "\n"));
31   
32   o << "Interval Successors:\n";
33   copy(I->Successors.begin(), I->Successors.end(), 
34        ostream_iterator<BasicBlock*>(o, "\n"));
35 }
36
37 void cfg::WriteToOutput(const IntervalPartition &IP, ostream &o) {
38   copy(IP.begin(), IP.end(), ostream_iterator<const Interval *>(o, "\n"));
39 }
40
41
42
43 //===----------------------------------------------------------------------===//
44 //  Dominator Printing Routines
45 //===----------------------------------------------------------------------===//
46
47 ostream &operator<<(ostream &o, const set<const BasicBlock*> &BBs) {
48   copy(BBs.begin(), BBs.end(), ostream_iterator<const BasicBlock*>(o, "\n"));
49   return o;
50 }
51
52 void cfg::WriteToOutput(const DominatorSet &DS, ostream &o) {
53   for (DominatorSet::const_iterator I = DS.begin(), E = DS.end(); I != E; ++I) {
54     o << "=============================--------------------------------\n"
55       << "\nDominator Set For Basic Block\n" << I->first
56       << "-------------------------------\n" << I->second << endl;
57   }
58 }
59
60
61 void cfg::WriteToOutput(const ImmediateDominators &ID, ostream &o) {
62   for (ImmediateDominators::const_iterator I = ID.begin(), E = ID.end();
63        I != E; ++I) {
64     o << "=============================--------------------------------\n"
65       << "\nImmediate Dominator For Basic Block\n" << I->first
66       << "is: \n" << I->second << endl;
67   }
68 }
69
70
71 static ostream &operator<<(ostream &o, const cfg::DominatorTree::Node *Node) {
72   return o << Node->getNode() << "\n------------------------------------------\n";
73            
74 }
75
76 static void PrintDomTree(const cfg::DominatorTree::Node *N, ostream &o,
77                          unsigned Lev) {
78   o << "Level #" << Lev << ":  " << N;
79   for (cfg::DominatorTree::Node::const_iterator I = N->begin(), E = N->end(); 
80        I != E; ++I) {
81     PrintDomTree(*I, o, Lev+1);
82   }
83 }
84
85 void cfg::WriteToOutput(const DominatorTree &DT, ostream &o) {
86   o << "=============================--------------------------------\n"
87     << "Inorder Dominator Tree:\n";
88   PrintDomTree(DT[DT.getRoot()], o, 1);
89 }
90
91 void cfg::WriteToOutput(const DominanceFrontier &DF, ostream &o) {
92   for (DominanceFrontier::const_iterator I = DF.begin(), E = DF.end();
93        I != E; ++I) {
94     o << "=============================--------------------------------\n"
95       << "\nDominance Frontier For Basic Block\n" << I->first
96       << "is: \n" << I->second << endl;
97   }
98 }
99
100
101 //===----------------------------------------------------------------------===//
102 //  Loop Printing Routines
103 //===----------------------------------------------------------------------===//
104
105 void cfg::WriteToOutput(const Loop *L, ostream &o) {
106   o << string(L->getLoopDepth()*2, ' ') << "Loop Containing: ";
107
108   for (unsigned i = 0; i < L->getBlocks().size(); ++i) {
109     if (i) o << ",";
110     WriteAsOperand(o, (const Value*)L->getBlocks()[i]);
111   }
112   o << endl;
113
114   copy(L->getSubLoops().begin(), L->getSubLoops().end(),
115        ostream_iterator<const Loop*>(o, "\n"));
116 }
117
118 void cfg::WriteToOutput(const LoopInfo &LI, ostream &o) {
119   copy(LI.getTopLevelLoops().begin(), LI.getTopLevelLoops().end(),
120        ostream_iterator<const Loop*>(o, "\n"));
121 }
122
123
124
125 //===----------------------------------------------------------------------===//
126 //  Induction Variable Printing Routines
127 //===----------------------------------------------------------------------===//
128
129 void WriteToOutput(const InductionVariable &IV, ostream &o) {
130   switch (IV.InductionType) {
131   case InductionVariable::Cannonical:   o << "Cannonical ";   break;
132   case InductionVariable::SimpleLinear: o << "SimpleLinear "; break;
133   case InductionVariable::Linear:       o << "Linear ";       break;
134   case InductionVariable::Unknown:      o << "Unrecognized "; break;
135   }
136   o << "Induction Variable";
137   if (IV.Phi) {
138     WriteAsOperand(o, (const Value*)IV.Phi);
139     o << ":\n" << (const Value*)IV.Phi;
140   } else {
141     o << endl;
142   }
143   if (IV.InductionType == InductionVariable::Unknown) return;
144
145   o << "  Start ="; WriteAsOperand(o, IV.Start);
146   o << "  Step =" ; WriteAsOperand(o, IV.Step);
147   o << endl;
148 }