Chris Lattner | 23ebd75 | 2003-08-31 19:23:41 +0000 | [diff] [blame] | 1 | //===- PrintSCC.cpp - Enumerate SCCs in some key graphs -------------------===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame^] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 20 | // |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 21 | // and similarly: |
| 22 | // analyze -callscc [-stats] [-debug] to print SCCs in the CallGraph |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 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> |
Chris Lattner | 68d033c | 2004-09-20 04:44:31 +0000 | [diff] [blame] | 34 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 35 | |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 36 | namespace { |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 37 | struct CFGSCC : public FunctionPass { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 38 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 39 | CFGSCC() : FunctionPass((intptr_t)&ID) {} |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 40 | bool runOnFunction(Function& func); |
| 41 | |
Reid Spencer | ce9653c | 2004-12-07 04:03:45 +0000 | [diff] [blame] | 42 | void print(std::ostream &O, const Module* = 0) const { } |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 43 | |
| 44 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 45 | AU.setPreservesAll(); |
Chris Lattner | 23ebd75 | 2003-08-31 19:23:41 +0000 | [diff] [blame] | 46 | } |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 47 | }; |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 68d033c | 2004-09-20 04:44:31 +0000 | [diff] [blame] | 49 | struct CallGraphSCC : public ModulePass { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 50 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 51 | CallGraphSCC() : ModulePass((intptr_t)&ID) {} |
| 52 | |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 53 | // run - Print out SCCs in the call graph for the specified module. |
Chris Lattner | 68d033c | 2004-09-20 04:44:31 +0000 | [diff] [blame] | 54 | bool runOnModule(Module &M); |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 55 | |
Reid Spencer | ce9653c | 2004-12-07 04:03:45 +0000 | [diff] [blame] | 56 | void print(std::ostream &O, const Module* = 0) const { } |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 58 | // getAnalysisUsage - This pass requires the CallGraph. |
| 59 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 60 | AU.setPreservesAll(); |
| 61 | AU.addRequired<CallGraph>(); |
Chris Lattner | 23ebd75 | 2003-08-31 19:23:41 +0000 | [diff] [blame] | 62 | } |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 63 | }; |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 64 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 65 | char CFGSCC::ID = 0; |
Chris Lattner | 5d8925c | 2006-08-27 22:30:17 +0000 | [diff] [blame] | 66 | RegisterPass<CFGSCC> |
Chris Lattner | 23ebd75 | 2003-08-31 19:23:41 +0000 | [diff] [blame] | 67 | Y("cfgscc", "Print SCCs of each function CFG"); |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 68 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 69 | char CallGraphSCC::ID = 0; |
Chris Lattner | 5d8925c | 2006-08-27 22:30:17 +0000 | [diff] [blame] | 70 | RegisterPass<CallGraphSCC> |
Chris Lattner | 23ebd75 | 2003-08-31 19:23:41 +0000 | [diff] [blame] | 71 | Z("callscc", "Print SCCs of the Call Graph"); |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 72 | } |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 73 | |
| 74 | bool CFGSCC::runOnFunction(Function &F) { |
| 75 | unsigned sccNum = 0; |
| 76 | std::cout << "SCCs for Function " << F.getName() << " in PostOrder:"; |
Chris Lattner | 55b2eb3 | 2003-08-31 20:01:57 +0000 | [diff] [blame] | 77 | for (scc_iterator<Function*> SCCI = scc_begin(&F), |
| 78 | E = scc_end(&F); SCCI != E; ++SCCI) { |
Chris Lattner | 729d73d | 2003-08-31 19:55:06 +0000 | [diff] [blame] | 79 | std::vector<BasicBlock*> &nextSCC = *SCCI; |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 80 | std::cout << "\nSCC #" << ++sccNum << " : "; |
Chris Lattner | 9f2a06e | 2003-08-31 19:51:38 +0000 | [diff] [blame] | 81 | for (std::vector<BasicBlock*>::const_iterator I = nextSCC.begin(), |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 82 | E = nextSCC.end(); I != E; ++I) |
| 83 | std::cout << (*I)->getName() << ", "; |
Chris Lattner | 9f2a06e | 2003-08-31 19:51:38 +0000 | [diff] [blame] | 84 | if (nextSCC.size() == 1 && SCCI.hasLoop()) |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 85 | std::cout << " (Has self-loop)."; |
| 86 | } |
| 87 | std::cout << "\n"; |
| 88 | |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | // run - Print out SCCs in the call graph for the specified module. |
Chris Lattner | 68d033c | 2004-09-20 04:44:31 +0000 | [diff] [blame] | 94 | bool CallGraphSCC::runOnModule(Module &M) { |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 95 | CallGraphNode* rootNode = getAnalysis<CallGraph>().getRoot(); |
| 96 | unsigned sccNum = 0; |
| 97 | std::cout << "SCCs for the program in PostOrder:"; |
Chris Lattner | 55b2eb3 | 2003-08-31 20:01:57 +0000 | [diff] [blame] | 98 | for (scc_iterator<CallGraphNode*> SCCI = scc_begin(rootNode), |
| 99 | E = scc_end(rootNode); SCCI != E; ++SCCI) { |
Chris Lattner | 729d73d | 2003-08-31 19:55:06 +0000 | [diff] [blame] | 100 | const std::vector<CallGraphNode*> &nextSCC = *SCCI; |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 101 | std::cout << "\nSCC #" << ++sccNum << " : "; |
Chris Lattner | 9f2a06e | 2003-08-31 19:51:38 +0000 | [diff] [blame] | 102 | for (std::vector<CallGraphNode*>::const_iterator I = nextSCC.begin(), |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 103 | E = nextSCC.end(); I != E; ++I) |
| 104 | std::cout << ((*I)->getFunction() ? (*I)->getFunction()->getName() |
| 105 | : std::string("Indirect CallGraph node")) << ", "; |
Chris Lattner | 9f2a06e | 2003-08-31 19:51:38 +0000 | [diff] [blame] | 106 | if (nextSCC.size() == 1 && SCCI.hasLoop()) |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 107 | std::cout << " (Has self-loop)."; |
| 108 | } |
| 109 | std::cout << "\n"; |
| 110 | |
| 111 | return true; |
| 112 | } |