Changes For Bug 352
[oota-llvm.git] / tools / opt / PrintSCC.cpp
1 //===- PrintSCC.cpp - Enumerate SCCs in some key graphs -------------------===//
2 // 
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 //===----------------------------------------------------------------------===//
9 //
10 // This file provides passes to print out SCCs in a CFG or a CallGraph.
11 // Normally, you would not use these passes; instead, you would use the
12 // scc_iterator directly to enumerate SCCs and process them in some way.  These
13 // passes serve three purposes:
14 //
15 // (1) As a reference for how to use the scc_iterator.
16 // (2) To print out the SCCs for a CFG or a CallGraph:
17 //       analyze -cfgscc            to print the SCCs in each CFG of a module.
18 //       analyze -cfgscc -stats     to print the #SCCs and the maximum SCC size.
19 //       analyze -cfgscc -debug > /dev/null to watch the algorithm in action.
20 // 
21 //     and similarly:
22 //       analyze -callscc [-stats] [-debug] to print SCCs in the CallGraph
23 // 
24 // (3) To test the scc_iterator.
25 //
26 //===----------------------------------------------------------------------===//
27
28 #include "llvm/Pass.h"
29 #include "llvm/Module.h"
30 #include "llvm/Analysis/CallGraph.h"
31 #include "llvm/Support/CFG.h"
32 #include "llvm/ADT/SCCIterator.h"
33 #include <iostream>
34
35 namespace llvm {
36
37 namespace {
38   struct CFGSCC : public FunctionPass {
39     bool runOnFunction(Function& func);
40
41     void print(std::ostream &O) const { }
42
43     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
44       AU.setPreservesAll();
45     }
46   };
47
48   struct CallGraphSCC : public Pass {
49     // run - Print out SCCs in the call graph for the specified module.
50     bool run(Module &M);
51
52     void print(std::ostream &O) const { }
53
54     // getAnalysisUsage - This pass requires the CallGraph.
55     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
56       AU.setPreservesAll();
57       AU.addRequired<CallGraph>();
58     }
59   };
60
61   RegisterAnalysis<CFGSCC>
62   Y("cfgscc", "Print SCCs of each function CFG");
63
64   RegisterAnalysis<CallGraphSCC>
65   Z("callscc", "Print SCCs of the Call Graph");
66 }
67
68 bool CFGSCC::runOnFunction(Function &F) {
69   unsigned sccNum = 0;
70   std::cout << "SCCs for Function " << F.getName() << " in PostOrder:";
71   for (scc_iterator<Function*> SCCI = scc_begin(&F),
72          E = scc_end(&F); SCCI != E; ++SCCI) {
73     std::vector<BasicBlock*> &nextSCC = *SCCI;
74     std::cout << "\nSCC #" << ++sccNum << " : ";
75     for (std::vector<BasicBlock*>::const_iterator I = nextSCC.begin(),
76            E = nextSCC.end(); I != E; ++I)
77       std::cout << (*I)->getName() << ", ";
78     if (nextSCC.size() == 1 && SCCI.hasLoop())
79       std::cout << " (Has self-loop).";
80   }
81   std::cout << "\n";
82
83   return true;
84 }
85
86
87 // run - Print out SCCs in the call graph for the specified module.
88 bool CallGraphSCC::run(Module &M) {
89   CallGraphNode* rootNode = getAnalysis<CallGraph>().getRoot();
90   unsigned sccNum = 0;
91   std::cout << "SCCs for the program in PostOrder:";
92   for (scc_iterator<CallGraphNode*> SCCI = scc_begin(rootNode),
93          E = scc_end(rootNode); SCCI != E; ++SCCI) {
94     const std::vector<CallGraphNode*> &nextSCC = *SCCI;
95     std::cout << "\nSCC #" << ++sccNum << " : ";
96     for (std::vector<CallGraphNode*>::const_iterator I = nextSCC.begin(),
97            E = nextSCC.end(); I != E; ++I)
98       std::cout << ((*I)->getFunction() ? (*I)->getFunction()->getName()
99                     : std::string("Indirect CallGraph node")) << ", ";
100     if (nextSCC.size() == 1 && SCCI.hasLoop())
101       std::cout << " (Has self-loop).";
102   }
103   std::cout << "\n";
104
105   return true;
106 }
107
108 } // End llvm namespace