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