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