Added LLVM project notice to the top of every C++ source file.
[oota-llvm.git] / tools / analyze / 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 "Support/SCCIterator.h"
33
34 namespace {
35   struct CFGSCC : public FunctionPass {
36     bool runOnFunction(Function& func);
37
38     void print(std::ostream &O) const { }
39
40     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
41       AU.setPreservesAll();
42     }
43   };
44
45   struct CallGraphSCC : public Pass {
46     // run - Print out SCCs in the call graph for the specified module.
47     bool run(Module &M);
48
49     void print(std::ostream &O) const { }
50
51     // getAnalysisUsage - This pass requires the CallGraph.
52     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53       AU.setPreservesAll();
54       AU.addRequired<CallGraph>();
55     }
56   };
57
58   RegisterAnalysis<CFGSCC>
59   Y("cfgscc", "Print SCCs of each function CFG");
60
61   RegisterAnalysis<CallGraphSCC>
62   Z("callscc", "Print SCCs of the Call Graph");
63 }
64
65 bool CFGSCC::runOnFunction(Function &F) {
66   unsigned sccNum = 0;
67   std::cout << "SCCs for Function " << F.getName() << " in PostOrder:";
68   for (scc_iterator<Function*> SCCI = scc_begin(&F),
69          E = scc_end(&F); SCCI != E; ++SCCI) {
70     std::vector<BasicBlock*> &nextSCC = *SCCI;
71     std::cout << "\nSCC #" << ++sccNum << " : ";
72     for (std::vector<BasicBlock*>::const_iterator I = nextSCC.begin(),
73            E = nextSCC.end(); I != E; ++I)
74       std::cout << (*I)->getName() << ", ";
75     if (nextSCC.size() == 1 && SCCI.hasLoop())
76       std::cout << " (Has self-loop).";
77   }
78   std::cout << "\n";
79
80   return true;
81 }
82
83
84 // run - Print out SCCs in the call graph for the specified module.
85 bool CallGraphSCC::run(Module &M) {
86   CallGraphNode* rootNode = getAnalysis<CallGraph>().getRoot();
87   unsigned sccNum = 0;
88   std::cout << "SCCs for the program in PostOrder:";
89   for (scc_iterator<CallGraphNode*> SCCI = scc_begin(rootNode),
90          E = scc_end(rootNode); SCCI != E; ++SCCI) {
91     const std::vector<CallGraphNode*> &nextSCC = *SCCI;
92     std::cout << "\nSCC #" << ++sccNum << " : ";
93     for (std::vector<CallGraphNode*>::const_iterator I = nextSCC.begin(),
94            E = nextSCC.end(); I != E; ++I)
95       std::cout << ((*I)->getFunction() ? (*I)->getFunction()->getName()
96                     : std::string("Indirect CallGraph node")) << ", ";
97     if (nextSCC.size() == 1 && SCCI.hasLoop())
98       std::cout << " (Has self-loop).";
99   }
100   std::cout << "\n";
101
102   return true;
103 }