Chris Lattner | 23ebd75 | 2003-08-31 19:23:41 +0000 | [diff] [blame] | 1 | //===- PrintSCC.cpp - Enumerate SCCs in some key graphs -------------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 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 |
Chris Lattner | 55b2eb3 | 2003-08-31 20:01:57 +0000 | [diff] [blame] | 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. |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 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 | // |
Chris Lattner | 55b2eb3 | 2003-08-31 20:01:57 +0000 | [diff] [blame] | 24 | // (3) To test the scc_iterator. |
Chris Lattner | 23ebd75 | 2003-08-31 19:23:41 +0000 | [diff] [blame] | 25 | // |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | #include "llvm/Pass.h" |
| 29 | #include "llvm/Module.h" |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/CallGraph.h" |
| 31 | #include "llvm/Support/CFG.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame^] | 32 | #include "llvm/ADT/SCCIterator.h" |
Reid Spencer | 954da37 | 2004-07-04 12:19:56 +0000 | [diff] [blame] | 33 | #include <iostream> |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 34 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 35 | namespace llvm { |
| 36 | |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 37 | namespace { |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 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(); |
Chris Lattner | 23ebd75 | 2003-08-31 19:23:41 +0000 | [diff] [blame] | 45 | } |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 46 | }; |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 48 | struct CallGraphSCC : public Pass { |
| 49 | // run - Print out SCCs in the call graph for the specified module. |
| 50 | bool run(Module &M); |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 52 | void print(std::ostream &O) const { } |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 54 | // getAnalysisUsage - This pass requires the CallGraph. |
| 55 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 56 | AU.setPreservesAll(); |
| 57 | AU.addRequired<CallGraph>(); |
Chris Lattner | 23ebd75 | 2003-08-31 19:23:41 +0000 | [diff] [blame] | 58 | } |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 59 | }; |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 23ebd75 | 2003-08-31 19:23:41 +0000 | [diff] [blame] | 61 | RegisterAnalysis<CFGSCC> |
| 62 | Y("cfgscc", "Print SCCs of each function CFG"); |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 23ebd75 | 2003-08-31 19:23:41 +0000 | [diff] [blame] | 64 | RegisterAnalysis<CallGraphSCC> |
| 65 | Z("callscc", "Print SCCs of the Call Graph"); |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 66 | } |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 67 | |
| 68 | bool CFGSCC::runOnFunction(Function &F) { |
| 69 | unsigned sccNum = 0; |
| 70 | std::cout << "SCCs for Function " << F.getName() << " in PostOrder:"; |
Chris Lattner | 55b2eb3 | 2003-08-31 20:01:57 +0000 | [diff] [blame] | 71 | for (scc_iterator<Function*> SCCI = scc_begin(&F), |
| 72 | E = scc_end(&F); SCCI != E; ++SCCI) { |
Chris Lattner | 729d73d | 2003-08-31 19:55:06 +0000 | [diff] [blame] | 73 | std::vector<BasicBlock*> &nextSCC = *SCCI; |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 74 | std::cout << "\nSCC #" << ++sccNum << " : "; |
Chris Lattner | 9f2a06e | 2003-08-31 19:51:38 +0000 | [diff] [blame] | 75 | for (std::vector<BasicBlock*>::const_iterator I = nextSCC.begin(), |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 76 | E = nextSCC.end(); I != E; ++I) |
| 77 | std::cout << (*I)->getName() << ", "; |
Chris Lattner | 9f2a06e | 2003-08-31 19:51:38 +0000 | [diff] [blame] | 78 | if (nextSCC.size() == 1 && SCCI.hasLoop()) |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 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:"; |
Chris Lattner | 55b2eb3 | 2003-08-31 20:01:57 +0000 | [diff] [blame] | 92 | for (scc_iterator<CallGraphNode*> SCCI = scc_begin(rootNode), |
| 93 | E = scc_end(rootNode); SCCI != E; ++SCCI) { |
Chris Lattner | 729d73d | 2003-08-31 19:55:06 +0000 | [diff] [blame] | 94 | const std::vector<CallGraphNode*> &nextSCC = *SCCI; |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 95 | std::cout << "\nSCC #" << ++sccNum << " : "; |
Chris Lattner | 9f2a06e | 2003-08-31 19:51:38 +0000 | [diff] [blame] | 96 | for (std::vector<CallGraphNode*>::const_iterator I = nextSCC.begin(), |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 97 | E = nextSCC.end(); I != E; ++I) |
| 98 | std::cout << ((*I)->getFunction() ? (*I)->getFunction()->getName() |
| 99 | : std::string("Indirect CallGraph node")) << ", "; |
Chris Lattner | 9f2a06e | 2003-08-31 19:51:38 +0000 | [diff] [blame] | 100 | if (nextSCC.size() == 1 && SCCI.hasLoop()) |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 101 | std::cout << " (Has self-loop)."; |
| 102 | } |
| 103 | std::cout << "\n"; |
| 104 | |
| 105 | return true; |
| 106 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 107 | |
| 108 | } // End llvm namespace |