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