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: |
Duncan Sands | 3ee8fc9 | 2008-09-23 12:47:39 +0000 | [diff] [blame] | 17 | // analyze -print-cfg-sccs to print the SCCs in each CFG of a module. |
| 18 | // analyze -print-cfg-sccs -stats to print the #SCCs and the maximum SCC size. |
| 19 | // analyze -print-cfg-sccs -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: |
Duncan Sands | 3ee8fc9 | 2008-09-23 12:47:39 +0000 | [diff] [blame] | 22 | // analyze -print-callgraph-sccs [-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" |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SCCIterator.h" |
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 |
Dan Gohman | 865f006 | 2009-02-18 05:09:16 +0000 | [diff] [blame] | 39 | CFGSCC() : FunctionPass(&ID) {} |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 40 | bool runOnFunction(Function& func); |
| 41 | |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 42 | void print(raw_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 |
Dan Gohman | 865f006 | 2009-02-18 05:09:16 +0000 | [diff] [blame] | 51 | CallGraphSCC() : ModulePass(&ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 56 | void print(raw_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> |
Duncan Sands | 3ee8fc9 | 2008-09-23 12:47:39 +0000 | [diff] [blame] | 67 | Y("print-cfg-sccs", "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> |
Duncan Sands | 3ee8fc9 | 2008-09-23 12:47:39 +0000 | [diff] [blame] | 71 | Z("print-callgraph-sccs", "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; |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 76 | outs() << "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; |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 80 | outs() << "\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) |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 83 | outs() << (*I)->getName() << ", "; |
Chris Lattner | 9f2a06e | 2003-08-31 19:51:38 +0000 | [diff] [blame] | 84 | if (nextSCC.size() == 1 && SCCI.hasLoop()) |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 85 | outs() << " (Has self-loop)."; |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 86 | } |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 87 | outs() << "\n"; |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 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; |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 97 | outs() << "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; |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 101 | outs() << "\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) |
Daniel Dunbar | 3d5126f | 2009-07-22 21:33:09 +0000 | [diff] [blame] | 104 | outs() << ((*I)->getFunction() ? (*I)->getFunction()->getNameStr() |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 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()) |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 107 | outs() << " (Has self-loop)."; |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 108 | } |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 109 | outs() << "\n"; |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 110 | |
| 111 | return true; |
| 112 | } |