Chris Lattner | c4caf3a | 2003-08-31 19:23:41 +0000 | [diff] [blame] | 1 | //===- PrintSCC.cpp - Enumerate SCCs in some key graphs -------------------===// |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 6 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | 5beed60 | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 8 | // |
| 9 | // This file provides passes to print out SCCs in a CFG or a CallGraph. |
| 10 | // Normally, you would not use these passes; instead, you would use the |
Chris Lattner | 78a5c1a | 2003-08-31 20:01:57 +0000 | [diff] [blame] | 11 | // scc_iterator directly to enumerate SCCs and process them in some way. These |
| 12 | // passes serve three purposes: |
| 13 | // |
| 14 | // (1) As a reference for how to use the scc_iterator. |
Vikram S. Adve | 5beed60 | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 15 | // (2) To print out the SCCs for a CFG or a CallGraph: |
Duncan Sands | 9c40c28 | 2008-09-23 12:47:39 +0000 | [diff] [blame] | 16 | // analyze -print-cfg-sccs to print the SCCs in each CFG of a module. |
| 17 | // analyze -print-cfg-sccs -stats to print the #SCCs and the maximum SCC size. |
| 18 | // analyze -print-cfg-sccs -debug > /dev/null to watch the algorithm in action. |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 19 | // |
Vikram S. Adve | 5beed60 | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 20 | // and similarly: |
Duncan Sands | 9c40c28 | 2008-09-23 12:47:39 +0000 | [diff] [blame] | 21 | // analyze -print-callgraph-sccs [-stats] [-debug] to print SCCs in the CallGraph |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 22 | // |
Chris Lattner | 78a5c1a | 2003-08-31 20:01:57 +0000 | [diff] [blame] | 23 | // (3) To test the scc_iterator. |
Chris Lattner | c4caf3a | 2003-08-31 19:23:41 +0000 | [diff] [blame] | 24 | // |
Vikram S. Adve | 5beed60 | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SCCIterator.h" |
Vikram S. Adve | 5beed60 | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/CallGraph.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 29 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Module.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 31 | #include "llvm/Pass.h" |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 1354191 | 2004-09-20 04:44:31 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 34 | |
Vikram S. Adve | 5beed60 | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 35 | namespace { |
Chris Lattner | 2f120bb | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 36 | struct CFGSCC : public FunctionPass { |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 37 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 38 | CFGSCC() : FunctionPass(ID) {} |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 39 | bool runOnFunction(Function& func) override; |
Chris Lattner | 2f120bb | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 40 | |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 41 | void print(raw_ostream &O, const Module* = nullptr) const override { } |
Chris Lattner | 2f120bb | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 42 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 43 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chris Lattner | 2f120bb | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 44 | AU.setPreservesAll(); |
Chris Lattner | c4caf3a | 2003-08-31 19:23:41 +0000 | [diff] [blame] | 45 | } |
Chris Lattner | 2f120bb | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 46 | }; |
Vikram S. Adve | 5beed60 | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 1354191 | 2004-09-20 04:44:31 +0000 | [diff] [blame] | 48 | struct CallGraphSCC : public ModulePass { |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 49 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 50 | CallGraphSCC() : ModulePass(ID) {} |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 2f120bb | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 52 | // run - Print out SCCs in the call graph for the specified module. |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 53 | bool runOnModule(Module &M) override; |
Vikram S. Adve | 5beed60 | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 54 | |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 55 | void print(raw_ostream &O, const Module* = nullptr) const override { } |
Vikram S. Adve | 5beed60 | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 2f120bb | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 57 | // getAnalysisUsage - This pass requires the CallGraph. |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 58 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chris Lattner | 2f120bb | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 59 | AU.setPreservesAll(); |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 60 | AU.addRequired<CallGraphWrapperPass>(); |
Chris Lattner | c4caf3a | 2003-08-31 19:23:41 +0000 | [diff] [blame] | 61 | } |
Chris Lattner | 2f120bb | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 62 | }; |
Vikram S. Adve | 5beed60 | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 63 | } |
Chris Lattner | 2f120bb | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 64 | |
Dan Gohman | 061cb1c | 2010-08-20 01:00:03 +0000 | [diff] [blame] | 65 | char CFGSCC::ID = 0; |
| 66 | static RegisterPass<CFGSCC> |
| 67 | Y("print-cfg-sccs", "Print SCCs of each function CFG"); |
| 68 | |
| 69 | char CallGraphSCC::ID = 0; |
| 70 | static RegisterPass<CallGraphSCC> |
| 71 | Z("print-callgraph-sccs", "Print SCCs of the Call Graph"); |
| 72 | |
Chris Lattner | 2f120bb | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 73 | bool CFGSCC::runOnFunction(Function &F) { |
| 74 | unsigned sccNum = 0; |
Dan Gohman | d3ee423 | 2010-08-20 01:03:44 +0000 | [diff] [blame] | 75 | errs() << "SCCs for Function " << F.getName() << " in PostOrder:"; |
Duncan P. N. Exon Smith | 8e661ef | 2014-02-04 19:19:07 +0000 | [diff] [blame] | 76 | for (scc_iterator<Function*> SCCI = scc_begin(&F); !SCCI.isAtEnd(); ++SCCI) { |
Duncan P. N. Exon Smith | d2b2fac | 2014-04-25 18:24:50 +0000 | [diff] [blame] | 77 | const std::vector<BasicBlock *> &nextSCC = *SCCI; |
Dan Gohman | d3ee423 | 2010-08-20 01:03:44 +0000 | [diff] [blame] | 78 | errs() << "\nSCC #" << ++sccNum << " : "; |
Chris Lattner | 4336db8 | 2003-08-31 19:51:38 +0000 | [diff] [blame] | 79 | for (std::vector<BasicBlock*>::const_iterator I = nextSCC.begin(), |
Chris Lattner | 2f120bb | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 80 | E = nextSCC.end(); I != E; ++I) |
Dan Gohman | d3ee423 | 2010-08-20 01:03:44 +0000 | [diff] [blame] | 81 | errs() << (*I)->getName() << ", "; |
Chris Lattner | 4336db8 | 2003-08-31 19:51:38 +0000 | [diff] [blame] | 82 | if (nextSCC.size() == 1 && SCCI.hasLoop()) |
Dan Gohman | d3ee423 | 2010-08-20 01:03:44 +0000 | [diff] [blame] | 83 | errs() << " (Has self-loop)."; |
Chris Lattner | 2f120bb | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 84 | } |
Dan Gohman | d3ee423 | 2010-08-20 01:03:44 +0000 | [diff] [blame] | 85 | errs() << "\n"; |
Chris Lattner | 2f120bb | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | // run - Print out SCCs in the call graph for the specified module. |
Chris Lattner | 1354191 | 2004-09-20 04:44:31 +0000 | [diff] [blame] | 92 | bool CallGraphSCC::runOnModule(Module &M) { |
Chandler Carruth | 16f56b4 | 2013-11-27 01:32:17 +0000 | [diff] [blame] | 93 | CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph(); |
Chris Lattner | 2f120bb | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 94 | unsigned sccNum = 0; |
Dan Gohman | d3ee423 | 2010-08-20 01:03:44 +0000 | [diff] [blame] | 95 | errs() << "SCCs for the program in PostOrder:"; |
Duncan P. N. Exon Smith | 8e661ef | 2014-02-04 19:19:07 +0000 | [diff] [blame] | 96 | for (scc_iterator<CallGraph*> SCCI = scc_begin(&CG); !SCCI.isAtEnd(); |
| 97 | ++SCCI) { |
Chris Lattner | bb6fe25 | 2003-08-31 19:55:06 +0000 | [diff] [blame] | 98 | const std::vector<CallGraphNode*> &nextSCC = *SCCI; |
Dan Gohman | d3ee423 | 2010-08-20 01:03:44 +0000 | [diff] [blame] | 99 | errs() << "\nSCC #" << ++sccNum << " : "; |
Chris Lattner | 4336db8 | 2003-08-31 19:51:38 +0000 | [diff] [blame] | 100 | for (std::vector<CallGraphNode*>::const_iterator I = nextSCC.begin(), |
Chris Lattner | 2f120bb | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 101 | E = nextSCC.end(); I != E; ++I) |
Benjamin Kramer | 1f97a5a | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 102 | errs() << ((*I)->getFunction() ? (*I)->getFunction()->getName() |
| 103 | : "external node") << ", "; |
Chris Lattner | 4336db8 | 2003-08-31 19:51:38 +0000 | [diff] [blame] | 104 | if (nextSCC.size() == 1 && SCCI.hasLoop()) |
Dan Gohman | d3ee423 | 2010-08-20 01:03:44 +0000 | [diff] [blame] | 105 | errs() << " (Has self-loop)."; |
Chris Lattner | 2f120bb | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 106 | } |
Dan Gohman | d3ee423 | 2010-08-20 01:03:44 +0000 | [diff] [blame] | 107 | errs() << "\n"; |
Chris Lattner | 2f120bb | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 108 | |
| 109 | return true; |
| 110 | } |