Chris Lattner | 4a81067 | 2003-08-31 01:54:59 +0000 | [diff] [blame] | 1 | //===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===// |
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 | 4ee451d | 2007-12-29 20:36:04 +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 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4a81067 | 2003-08-31 01:54:59 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the CallGraphSCCPass class, which is used for passes |
| 11 | // which are implemented as bottom-up traversals on the call graph. Because |
| 12 | // there may be cycles in the call graph, passes of this type operate on the |
| 13 | // call-graph in SCC order: that is, they process function bottom-up, except for |
| 14 | // recursive functions, which they process all at once. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Chandler Carruth | 3251e81 | 2013-01-07 15:26:48 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/CallGraphSCCPass.h" |
Eugene Zelenko | 046ca04 | 2017-08-31 21:56:16 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SCCIterator.h" |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/CallGraph.h" |
Eugene Zelenko | 046ca04 | 2017-08-31 21:56:16 +0000 | [diff] [blame] | 23 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Function.h" |
Fedor Sergeev | a7c2370 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 25 | #include "llvm/IR/IRPrintingPasses.h" |
Eugene Zelenko | 046ca04 | 2017-08-31 21:56:16 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Intrinsics.h" |
Juergen Ributzka | 9bc1b73 | 2014-05-16 02:33:15 +0000 | [diff] [blame] | 27 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 1b27914 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 28 | #include "llvm/IR/LegacyPassManagers.h" |
Eugene Zelenko | 046ca04 | 2017-08-31 21:56:16 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Module.h" |
Andrew Kaylor | 1e455c5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 30 | #include "llvm/IR/OptBisect.h" |
Fedor Sergeev | 9f6be22 | 2018-08-28 21:06:51 +0000 | [diff] [blame] | 31 | #include "llvm/IR/PassTimingInfo.h" |
Eugene Zelenko | 046ca04 | 2017-08-31 21:56:16 +0000 | [diff] [blame] | 32 | #include "llvm/Pass.h" |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
Chris Lattner | a782e75 | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Timer.h" |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 36 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 046ca04 | 2017-08-31 21:56:16 +0000 | [diff] [blame] | 37 | #include <cassert> |
| 38 | #include <string> |
| 39 | #include <utility> |
| 40 | #include <vector> |
| 41 | |
Chris Lattner | a10df50 | 2004-04-20 21:30:06 +0000 | [diff] [blame] | 42 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 43 | |
Chandler Carruth | 4da2537 | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 44 | #define DEBUG_TYPE "cgscc-passmgr" |
| 45 | |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 46 | static cl::opt<unsigned> |
Chris Lattner | 6da12e6 | 2010-05-01 01:15:56 +0000 | [diff] [blame] | 47 | MaxIterations("max-cg-scc-iterations", cl::ReallyHidden, cl::init(4)); |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 48 | |
| 49 | STATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC"); |
| 50 | |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 51 | //===----------------------------------------------------------------------===// |
| 52 | // CGPassManager |
| 53 | // |
Dale Johannesen | db2659b | 2009-09-10 22:01:32 +0000 | [diff] [blame] | 54 | /// CGPassManager manages FPPassManagers and CallGraphSCCPasses. |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 55 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 56 | namespace { |
| 57 | |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 58 | class CGPassManager : public ModulePass, public PMDataManager { |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 59 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 60 | static char ID; |
Eugene Zelenko | 046ca04 | 2017-08-31 21:56:16 +0000 | [diff] [blame] | 61 | |
| 62 | explicit CGPassManager() : ModulePass(ID), PMDataManager() {} |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 63 | |
Sanjay Patel | ec53c65 | 2015-03-10 03:48:14 +0000 | [diff] [blame] | 64 | /// Execute all of the passes scheduled for execution. Keep track of |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 65 | /// whether any of the passes modifies the module, and if so, return true. |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 66 | bool runOnModule(Module &M) override; |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 67 | |
Owen Anderson | 40b6fdb | 2012-11-15 00:14:15 +0000 | [diff] [blame] | 68 | using ModulePass::doInitialization; |
| 69 | using ModulePass::doFinalization; |
| 70 | |
Bill Wendling | 905c7e9 | 2009-02-11 18:19:24 +0000 | [diff] [blame] | 71 | bool doInitialization(CallGraph &CG); |
| 72 | bool doFinalization(CallGraph &CG); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 73 | |
| 74 | /// Pass Manager itself does not invalidate any analysis info. |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 75 | void getAnalysisUsage(AnalysisUsage &Info) const override { |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 76 | // CGPassManager walks SCC and it needs CallGraph. |
Chandler Carruth | 54fec07 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 77 | Info.addRequired<CallGraphWrapperPass>(); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 78 | Info.setPreservesAll(); |
| 79 | } |
| 80 | |
Mehdi Amini | 67f335d | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 81 | StringRef getPassName() const override { return "CallGraph Pass Manager"; } |
Devang Patel | 505f36a | 2007-02-01 22:09:37 +0000 | [diff] [blame] | 82 | |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 83 | PMDataManager *getAsPMDataManager() override { return this; } |
| 84 | Pass *getAsPass() override { return this; } |
Chris Lattner | 3660eca | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 85 | |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 86 | // Print passes managed by this manager |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 87 | void dumpPassStructure(unsigned Offset) override { |
David Greene | 2e48e53 | 2009-12-23 23:09:39 +0000 | [diff] [blame] | 88 | errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n"; |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 89 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 90 | Pass *P = getContainedPass(Index); |
Dan Gohman | 8a757ae | 2010-08-19 01:29:07 +0000 | [diff] [blame] | 91 | P->dumpPassStructure(Offset + 1); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 92 | dumpLastUses(P, Offset+1); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | Pass *getContainedPass(unsigned N) { |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 97 | assert(N < PassVector.size() && "Pass number out of range!"); |
| 98 | return static_cast<Pass *>(PassVector[N]); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 101 | PassManagerType getPassManagerType() const override { |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 102 | return PMT_CallGraphPassManager; |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 103 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 104 | |
Chris Lattner | f3a1c15 | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 105 | private: |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 106 | bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG, |
| 107 | bool &DevirtualizedCall); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 109 | bool RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC, |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 110 | CallGraph &CG, bool &CallGraphUpToDate, |
| 111 | bool &DevirtualizedCall); |
Mehdi Amini | a27deb3 | 2016-08-08 18:51:05 +0000 | [diff] [blame] | 112 | bool RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG, |
Chris Lattner | 5a6a363 | 2009-09-01 18:32:03 +0000 | [diff] [blame] | 113 | bool IsCheckingMode); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
Chris Lattner | f3a1c15 | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 116 | } // end anonymous namespace. |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 117 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 118 | char CGPassManager::ID = 0; |
Chris Lattner | f3a1c15 | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 120 | bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC, |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 121 | CallGraph &CG, bool &CallGraphUpToDate, |
| 122 | bool &DevirtualizedCall) { |
Chris Lattner | f3a1c15 | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 123 | bool Changed = false; |
Chris Lattner | 5a66cb9 | 2010-01-22 05:46:59 +0000 | [diff] [blame] | 124 | PMDataManager *PM = P->getAsPMDataManager(); |
Jessica Paquette | 849da55 | 2018-05-18 17:26:39 +0000 | [diff] [blame] | 125 | Module &M = CG.getModule(); |
Chris Lattner | 5a66cb9 | 2010-01-22 05:46:59 +0000 | [diff] [blame] | 126 | |
Craig Topper | e703fcb | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 127 | if (!PM) { |
Jessica Paquette | 4f3a219 | 2018-08-31 20:20:57 +0000 | [diff] [blame] | 128 | CallGraphSCCPass *CGSP = (CallGraphSCCPass *)P; |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 129 | if (!CallGraphUpToDate) { |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 130 | DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false); |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 131 | CallGraphUpToDate = true; |
| 132 | } |
Chris Lattner | 44a1837 | 2009-09-01 20:33:43 +0000 | [diff] [blame] | 133 | |
Chris Lattner | a782e75 | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 134 | { |
Jessica Paquette | 1466707 | 2018-08-31 20:20:56 +0000 | [diff] [blame] | 135 | unsigned InstrCount, SCCCount = 0; |
Jessica Paquette | 9e21280 | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 136 | StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount; |
Xin Tong | a747d61 | 2018-07-22 05:27:41 +0000 | [diff] [blame] | 137 | bool EmitICRemark = M.shouldEmitInstrCountChangedRemark(); |
Chris Lattner | a782e75 | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 138 | TimeRegion PassTimer(getPassTimer(CGSP)); |
Xin Tong | a747d61 | 2018-07-22 05:27:41 +0000 | [diff] [blame] | 139 | if (EmitICRemark) |
Jessica Paquette | 9e21280 | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 140 | InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount); |
Chris Lattner | a782e75 | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 141 | Changed = CGSP->runOnSCC(CurSCC); |
Jessica Paquette | 849da55 | 2018-05-18 17:26:39 +0000 | [diff] [blame] | 142 | |
Jessica Paquette | 1466707 | 2018-08-31 20:20:56 +0000 | [diff] [blame] | 143 | if (EmitICRemark) { |
| 144 | // FIXME: Add getInstructionCount to CallGraphSCC. |
Jessica Paquette | 1466707 | 2018-08-31 20:20:56 +0000 | [diff] [blame] | 145 | SCCCount = M.getInstructionCount(); |
| 146 | // Is there a difference in the number of instructions in the module? |
| 147 | if (SCCCount != InstrCount) { |
| 148 | // Yep. Emit a remark and update InstrCount. |
Jessica Paquette | 4f3a219 | 2018-08-31 20:20:57 +0000 | [diff] [blame] | 149 | int64_t Delta = |
| 150 | static_cast<int64_t>(SCCCount) - static_cast<int64_t>(InstrCount); |
Jessica Paquette | 9e21280 | 2018-09-06 21:19:54 +0000 | [diff] [blame] | 151 | emitInstrCountChangedRemark(P, M, Delta, InstrCount, |
| 152 | FunctionToInstrCount); |
Jessica Paquette | 1466707 | 2018-08-31 20:20:56 +0000 | [diff] [blame] | 153 | InstrCount = SCCCount; |
| 154 | } |
| 155 | } |
Chris Lattner | a782e75 | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 156 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 5a6a363 | 2009-09-01 18:32:03 +0000 | [diff] [blame] | 158 | // After the CGSCCPass is done, when assertions are enabled, use |
| 159 | // RefreshCallGraph to verify that the callgraph was correctly updated. |
| 160 | #ifndef NDEBUG |
| 161 | if (Changed) |
| 162 | RefreshCallGraph(CurSCC, CG, true); |
| 163 | #endif |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 164 | |
Chris Lattner | f3a1c15 | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 165 | return Changed; |
| 166 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 5a66cb9 | 2010-01-22 05:46:59 +0000 | [diff] [blame] | 168 | assert(PM->getPassManagerType() == PMT_FunctionPassManager && |
| 169 | "Invalid CGPassManager member"); |
| 170 | FPPassManager *FPP = (FPPassManager*)P; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 171 | |
Chris Lattner | f3a1c15 | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 172 | // Run pass P on all functions in the current SCC. |
Sanjay Patel | 9e894a9 | 2015-03-10 03:26:39 +0000 | [diff] [blame] | 173 | for (CallGraphNode *CGN : CurSCC) { |
| 174 | if (Function *F = CGN->getFunction()) { |
Chris Lattner | f3a1c15 | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 175 | dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName()); |
Juergen Ributzka | 9bc1b73 | 2014-05-16 02:33:15 +0000 | [diff] [blame] | 176 | { |
| 177 | TimeRegion PassTimer(getPassTimer(FPP)); |
| 178 | Changed |= FPP->runOnFunction(*F); |
| 179 | } |
| 180 | F->getContext().yield(); |
Chris Lattner | f3a1c15 | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 2038cf3 | 2009-08-31 17:08:30 +0000 | [diff] [blame] | 184 | // The function pass(es) modified the IR, they may have clobbered the |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 185 | // callgraph. |
| 186 | if (Changed && CallGraphUpToDate) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 187 | LLVM_DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: " << P->getPassName() |
| 188 | << '\n'); |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 189 | CallGraphUpToDate = false; |
| 190 | } |
Chris Lattner | f3a1c15 | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 191 | return Changed; |
| 192 | } |
| 193 | |
Sanjay Patel | ec53c65 | 2015-03-10 03:48:14 +0000 | [diff] [blame] | 194 | /// Scan the functions in the specified CFG and resync the |
Chris Lattner | 5a6a363 | 2009-09-01 18:32:03 +0000 | [diff] [blame] | 195 | /// callgraph with the call sites found in it. This is used after |
| 196 | /// FunctionPasses have potentially munged the callgraph, and can be used after |
| 197 | /// CallGraphSCC passes to verify that they correctly updated the callgraph. |
| 198 | /// |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 199 | /// This function returns true if it devirtualized an existing function call, |
| 200 | /// meaning it turned an indirect call into a direct call. This happens when |
| 201 | /// a function pass like GVN optimizes away stuff feeding the indirect call. |
| 202 | /// This never happens in checking mode. |
Mehdi Amini | a27deb3 | 2016-08-08 18:51:05 +0000 | [diff] [blame] | 203 | bool CGPassManager::RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG, |
| 204 | bool CheckingMode) { |
Chris Lattner | a541b0f | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 205 | DenseMap<Value*, CallGraphNode*> CallSites; |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 206 | |
| 207 | LLVM_DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size() |
| 208 | << " nodes:\n"; |
| 209 | for (CallGraphNode *CGN |
| 210 | : CurSCC) CGN->dump();); |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 211 | |
| 212 | bool MadeChange = false; |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 213 | bool DevirtualizedCall = false; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 214 | |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 215 | // Scan all functions in the SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 216 | unsigned FunctionNo = 0; |
| 217 | for (CallGraphSCC::iterator SCCIdx = CurSCC.begin(), E = CurSCC.end(); |
| 218 | SCCIdx != E; ++SCCIdx, ++FunctionNo) { |
| 219 | CallGraphNode *CGN = *SCCIdx; |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 220 | Function *F = CGN->getFunction(); |
Craig Topper | e703fcb | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 221 | if (!F || F->isDeclaration()) continue; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 222 | |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 223 | // Walk the function body looking for call sites. Sync up the call sites in |
| 224 | // CGN with those actually in the function. |
Chris Lattner | bccb41a | 2010-05-01 06:38:43 +0000 | [diff] [blame] | 225 | |
| 226 | // Keep track of the number of direct and indirect calls that were |
| 227 | // invalidated and removed. |
| 228 | unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 229 | |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 230 | // Get the set of call sites currently in the function. |
Duncan Sands | b3020d7 | 2009-09-02 03:48:41 +0000 | [diff] [blame] | 231 | for (CallGraphNode::iterator I = CGN->begin(), E = CGN->end(); I != E; ) { |
Chris Lattner | a541b0f | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 232 | // If this call site is null, then the function pass deleted the call |
Sanjoy Das | 399b4d0 | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 233 | // entirely and the WeakTrackingVH nulled it out. |
Craig Topper | e703fcb | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 234 | if (!I->first || |
Chris Lattner | a541b0f | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 235 | // If we've already seen this call site, then the FunctionPass RAUW'd |
| 236 | // one call with another, which resulted in two "uses" in the edge |
| 237 | // list of the same call. |
| 238 | CallSites.count(I->first) || |
| 239 | |
Chad Rosier | 2fbccf6 | 2015-04-14 15:52:57 +0000 | [diff] [blame] | 240 | // If the call edge is not from a call or invoke, or it is a |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 241 | // instrinsic call, then the function pass RAUW'd a call with |
Chad Rosier | 2fbccf6 | 2015-04-14 15:52:57 +0000 | [diff] [blame] | 242 | // another value. This can happen when constant folding happens |
| 243 | // of well known functions etc. |
| 244 | !CallSite(I->first) || |
Sanjoy Das | aabacb6 | 2015-06-18 19:28:26 +0000 | [diff] [blame] | 245 | (CallSite(I->first).getCalledFunction() && |
| 246 | CallSite(I->first).getCalledFunction()->isIntrinsic() && |
| 247 | Intrinsic::isLeaf( |
| 248 | CallSite(I->first).getCalledFunction()->getIntrinsicID()))) { |
Chris Lattner | 5a6a363 | 2009-09-01 18:32:03 +0000 | [diff] [blame] | 249 | assert(!CheckingMode && |
| 250 | "CallGraphSCCPass did not update the CallGraph correctly!"); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 251 | |
Chris Lattner | bccb41a | 2010-05-01 06:38:43 +0000 | [diff] [blame] | 252 | // If this was an indirect call site, count it. |
Craig Topper | e703fcb | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 253 | if (!I->second->getFunction()) |
Chris Lattner | bccb41a | 2010-05-01 06:38:43 +0000 | [diff] [blame] | 254 | ++NumIndirectRemoved; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 255 | else |
Chris Lattner | bccb41a | 2010-05-01 06:38:43 +0000 | [diff] [blame] | 256 | ++NumDirectRemoved; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 257 | |
Chris Lattner | d3bd082 | 2009-09-02 04:39:04 +0000 | [diff] [blame] | 258 | // Just remove the edge from the set of callees, keep track of whether |
| 259 | // I points to the last element of the vector. |
| 260 | bool WasLast = I + 1 == E; |
Chris Lattner | a541b0f | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 261 | CGN->removeCallEdge(I); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 262 | |
Chris Lattner | d3bd082 | 2009-09-02 04:39:04 +0000 | [diff] [blame] | 263 | // If I pointed to the last element of the vector, we have to bail out: |
| 264 | // iterator checking rejects comparisons of the resultant pointer with |
| 265 | // end. |
| 266 | if (WasLast) |
| 267 | break; |
Chris Lattner | a541b0f | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 268 | E = CGN->end(); |
Chris Lattner | a541b0f | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 269 | continue; |
| 270 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 271 | |
Chris Lattner | a541b0f | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 272 | assert(!CallSites.count(I->first) && |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 273 | "Call site occurs in node multiple times"); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 274 | |
Sanjay Patel | bc8114f | 2014-11-12 18:25:47 +0000 | [diff] [blame] | 275 | CallSite CS(I->first); |
| 276 | if (CS) { |
| 277 | Function *Callee = CS.getCalledFunction(); |
| 278 | // Ignore intrinsics because they're not really function calls. |
| 279 | if (!Callee || !(Callee->isIntrinsic())) |
| 280 | CallSites.insert(std::make_pair(I->first, I->second)); |
| 281 | } |
Chris Lattner | 17146b8 | 2009-09-01 18:13:40 +0000 | [diff] [blame] | 282 | ++I; |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 283 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 284 | |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 285 | // Loop over all of the instructions in the function, getting the callsites. |
Chris Lattner | bccb41a | 2010-05-01 06:38:43 +0000 | [diff] [blame] | 286 | // Keep track of the number of direct/indirect calls added. |
| 287 | unsigned NumDirectAdded = 0, NumIndirectAdded = 0; |
Benjamin Kramer | 8d0d2b6 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 288 | |
| 289 | for (BasicBlock &BB : *F) |
| 290 | for (Instruction &I : BB) { |
| 291 | CallSite CS(&I); |
Nuno Lopes | 7d53971 | 2012-06-29 17:49:32 +0000 | [diff] [blame] | 292 | if (!CS) continue; |
| 293 | Function *Callee = CS.getCalledFunction(); |
| 294 | if (Callee && Callee->isIntrinsic()) continue; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 295 | |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 296 | // If this call site already existed in the callgraph, just verify it |
| 297 | // matches up to expectations and remove it from CallSites. |
Chris Lattner | a541b0f | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 298 | DenseMap<Value*, CallGraphNode*>::iterator ExistingIt = |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 299 | CallSites.find(CS.getInstruction()); |
| 300 | if (ExistingIt != CallSites.end()) { |
| 301 | CallGraphNode *ExistingNode = ExistingIt->second; |
| 302 | |
| 303 | // Remove from CallSites since we have now seen it. |
| 304 | CallSites.erase(ExistingIt); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 305 | |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 306 | // Verify that the callee is right. |
| 307 | if (ExistingNode->getFunction() == CS.getCalledFunction()) |
| 308 | continue; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 309 | |
Chris Lattner | 5a6a363 | 2009-09-01 18:32:03 +0000 | [diff] [blame] | 310 | // If we are in checking mode, we are not allowed to actually mutate |
| 311 | // the callgraph. If this is a case where we can infer that the |
| 312 | // callgraph is less precise than it could be (e.g. an indirect call |
| 313 | // site could be turned direct), don't reject it in checking mode, and |
| 314 | // don't tweak it to be more precise. |
| 315 | if (CheckingMode && CS.getCalledFunction() && |
Craig Topper | e703fcb | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 316 | ExistingNode->getFunction() == nullptr) |
Chris Lattner | 5a6a363 | 2009-09-01 18:32:03 +0000 | [diff] [blame] | 317 | continue; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 318 | |
Chris Lattner | 5a6a363 | 2009-09-01 18:32:03 +0000 | [diff] [blame] | 319 | assert(!CheckingMode && |
| 320 | "CallGraphSCCPass did not update the CallGraph correctly!"); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 321 | |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 322 | // If not, we either went from a direct call to indirect, indirect to |
| 323 | // direct, or direct to different direct. |
| 324 | CallGraphNode *CalleeNode; |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 325 | if (Function *Callee = CS.getCalledFunction()) { |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 326 | CalleeNode = CG.getOrInsertFunction(Callee); |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 327 | // Keep track of whether we turned an indirect call into a direct |
| 328 | // one. |
Craig Topper | e703fcb | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 329 | if (!ExistingNode->getFunction()) { |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 330 | DevirtualizedCall = true; |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 331 | LLVM_DEBUG(dbgs() << " CGSCCPASSMGR: Devirtualized call to '" |
| 332 | << Callee->getName() << "'\n"); |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 333 | } |
| 334 | } else { |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 335 | CalleeNode = CG.getCallsExternalNode(); |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 336 | } |
Chris Lattner | 44a1837 | 2009-09-01 20:33:43 +0000 | [diff] [blame] | 337 | |
| 338 | // Update the edge target in CGN. |
Chris Lattner | b61789d | 2010-04-22 20:42:33 +0000 | [diff] [blame] | 339 | CGN->replaceCallEdge(CS, CS, CalleeNode); |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 340 | MadeChange = true; |
| 341 | continue; |
| 342 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 343 | |
Chris Lattner | 5a6a363 | 2009-09-01 18:32:03 +0000 | [diff] [blame] | 344 | assert(!CheckingMode && |
| 345 | "CallGraphSCCPass did not update the CallGraph correctly!"); |
| 346 | |
Chris Lattner | bccb41a | 2010-05-01 06:38:43 +0000 | [diff] [blame] | 347 | // If the call site didn't exist in the CGN yet, add it. |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 348 | CallGraphNode *CalleeNode; |
Chris Lattner | bccb41a | 2010-05-01 06:38:43 +0000 | [diff] [blame] | 349 | if (Function *Callee = CS.getCalledFunction()) { |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 350 | CalleeNode = CG.getOrInsertFunction(Callee); |
Chris Lattner | bccb41a | 2010-05-01 06:38:43 +0000 | [diff] [blame] | 351 | ++NumDirectAdded; |
| 352 | } else { |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 353 | CalleeNode = CG.getCallsExternalNode(); |
Chris Lattner | bccb41a | 2010-05-01 06:38:43 +0000 | [diff] [blame] | 354 | ++NumIndirectAdded; |
| 355 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 356 | |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 357 | CGN->addCalledFunction(CS, CalleeNode); |
| 358 | MadeChange = true; |
| 359 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 360 | |
Chris Lattner | bccb41a | 2010-05-01 06:38:43 +0000 | [diff] [blame] | 361 | // We scanned the old callgraph node, removing invalidated call sites and |
| 362 | // then added back newly found call sites. One thing that can happen is |
| 363 | // that an old indirect call site was deleted and replaced with a new direct |
| 364 | // call. In this case, we have devirtualized a call, and CGSCCPM would like |
| 365 | // to iteratively optimize the new code. Unfortunately, we don't really |
| 366 | // have a great way to detect when this happens. As an approximation, we |
| 367 | // just look at whether the number of indirect calls is reduced and the |
| 368 | // number of direct calls is increased. There are tons of ways to fool this |
| 369 | // (e.g. DCE'ing an indirect call and duplicating an unrelated block with a |
| 370 | // direct call) but this is close enough. |
| 371 | if (NumIndirectRemoved > NumIndirectAdded && |
| 372 | NumDirectRemoved < NumDirectAdded) |
| 373 | DevirtualizedCall = true; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 374 | |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 375 | // After scanning this function, if we still have entries in callsites, then |
Sanjoy Das | 399b4d0 | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 376 | // they are dangling pointers. WeakTrackingVH should save us for this, so |
| 377 | // abort if |
Sanjoy Das | 263da12 | 2017-04-26 16:37:05 +0000 | [diff] [blame] | 378 | // this happens. |
Chris Lattner | a541b0f | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 379 | assert(CallSites.empty() && "Dangling pointers found in call sites map"); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 380 | |
Chris Lattner | a541b0f | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 381 | // Periodically do an explicit clear to remove tombstones when processing |
| 382 | // large scc's. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 383 | if ((FunctionNo & 15) == 15) |
Chris Lattner | a541b0f | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 384 | CallSites.clear(); |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 387 | LLVM_DEBUG(if (MadeChange) { |
| 388 | dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n"; |
| 389 | for (CallGraphNode *CGN : CurSCC) |
| 390 | CGN->dump(); |
| 391 | if (DevirtualizedCall) |
| 392 | dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n"; |
| 393 | } else { |
| 394 | dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n"; |
| 395 | }); |
Duncan Sands | 1f6a329 | 2011-08-12 14:54:45 +0000 | [diff] [blame] | 396 | (void)MadeChange; |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 397 | |
| 398 | return DevirtualizedCall; |
| 399 | } |
| 400 | |
Sanjay Patel | ec53c65 | 2015-03-10 03:48:14 +0000 | [diff] [blame] | 401 | /// Execute the body of the entire pass manager on the specified SCC. |
| 402 | /// This keeps track of whether a function pass devirtualizes |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 403 | /// any calls and returns it in DevirtualizedCall. |
| 404 | bool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG, |
| 405 | bool &DevirtualizedCall) { |
| 406 | bool Changed = false; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 407 | |
Sanjay Patel | ec53c65 | 2015-03-10 03:48:14 +0000 | [diff] [blame] | 408 | // Keep track of whether the callgraph is known to be up-to-date or not. |
| 409 | // The CGSSC pass manager runs two types of passes: |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 410 | // CallGraphSCC Passes and other random function passes. Because other |
| 411 | // random function passes are not CallGraph aware, they may clobber the |
| 412 | // call graph by introducing new calls or deleting other ones. This flag |
| 413 | // is set to false when we run a function pass so that we know to clean up |
| 414 | // the callgraph when we need to run a CGSCCPass again. |
| 415 | bool CallGraphUpToDate = true; |
| 416 | |
| 417 | // Run all passes on current SCC. |
| 418 | for (unsigned PassNo = 0, e = getNumContainedPasses(); |
| 419 | PassNo != e; ++PassNo) { |
| 420 | Pass *P = getContainedPass(PassNo); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 421 | |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 422 | // If we're in -debug-pass=Executions mode, construct the SCC node list, |
| 423 | // otherwise avoid constructing this string as it is expensive. |
| 424 | if (isPassDebuggingExecutionsOrMore()) { |
| 425 | std::string Functions; |
| 426 | #ifndef NDEBUG |
| 427 | raw_string_ostream OS(Functions); |
| 428 | for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end(); |
| 429 | I != E; ++I) { |
| 430 | if (I != CurSCC.begin()) OS << ", "; |
| 431 | (*I)->print(OS); |
| 432 | } |
| 433 | OS.flush(); |
| 434 | #endif |
| 435 | dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions); |
| 436 | } |
| 437 | dumpRequiredSet(P); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 438 | |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 439 | initializeAnalysisImpl(P); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 440 | |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 441 | // Actually run this pass on the current SCC. |
| 442 | Changed |= RunPassOnSCC(P, CurSCC, CG, |
| 443 | CallGraphUpToDate, DevirtualizedCall); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 444 | |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 445 | if (Changed) |
| 446 | dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, ""); |
| 447 | dumpPreservedSet(P); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 448 | |
| 449 | verifyPreservedAnalysis(P); |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 450 | removeNotPreservedAnalysis(P); |
| 451 | recordAvailableAnalysis(P); |
| 452 | removeDeadPasses(P, "", ON_CG_MSG); |
| 453 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 454 | |
Chris Lattner | 08e322d | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 455 | // If the callgraph was left out of date (because the last pass run was a |
| 456 | // functionpass), refresh it before we move on to the next SCC. |
| 457 | if (!CallGraphUpToDate) |
| 458 | DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false); |
| 459 | return Changed; |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 460 | } |
Chris Lattner | f3a1c15 | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 461 | |
Sanjay Patel | ec53c65 | 2015-03-10 03:48:14 +0000 | [diff] [blame] | 462 | /// Execute all of the passes scheduled for execution. Keep track of |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 463 | /// whether any of the passes modifies the module, and if so, return true. |
| 464 | bool CGPassManager::runOnModule(Module &M) { |
Chandler Carruth | 54fec07 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 465 | CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph(); |
Bill Wendling | 905c7e9 | 2009-02-11 18:19:24 +0000 | [diff] [blame] | 466 | bool Changed = doInitialization(CG); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 467 | |
Evgeniy Stepanov | b7219f9 | 2018-07-13 16:32:31 +0000 | [diff] [blame] | 468 | // Walk the callgraph in bottom-up SCC order. |
| 469 | scc_iterator<CallGraph*> CGI = scc_begin(&CG); |
Chris Lattner | a3dfc64 | 2010-04-16 22:59:24 +0000 | [diff] [blame] | 470 | |
Evgeniy Stepanov | b7219f9 | 2018-07-13 16:32:31 +0000 | [diff] [blame] | 471 | CallGraphSCC CurSCC(CG, &CGI); |
| 472 | while (!CGI.isAtEnd()) { |
| 473 | // Copy the current SCC and increment past it so that the pass can hack |
| 474 | // on the SCC if it wants to without invalidating our iterator. |
| 475 | const std::vector<CallGraphNode *> &NodeVec = *CGI; |
| 476 | CurSCC.initialize(NodeVec); |
| 477 | ++CGI; |
Yaron Keren | 016970f | 2015-07-02 14:17:12 +0000 | [diff] [blame] | 478 | |
Evgeniy Stepanov | b7219f9 | 2018-07-13 16:32:31 +0000 | [diff] [blame] | 479 | // At the top level, we run all the passes in this pass manager on the |
| 480 | // functions in this SCC. However, we support iterative compilation in the |
| 481 | // case where a function pass devirtualizes a call to a function. For |
| 482 | // example, it is very common for a function pass (often GVN or instcombine) |
| 483 | // to eliminate the addressing that feeds into a call. With that improved |
| 484 | // information, we would like the call to be an inline candidate, infer |
| 485 | // mod-ref information etc. |
| 486 | // |
| 487 | // Because of this, we allow iteration up to a specified iteration count. |
| 488 | // This only happens in the case of a devirtualized call, so we only burn |
| 489 | // compile time in the case that we're making progress. We also have a hard |
| 490 | // iteration count limit in case there is crazy code. |
| 491 | unsigned Iteration = 0; |
| 492 | bool DevirtualizedCall = false; |
| 493 | do { |
| 494 | LLVM_DEBUG(if (Iteration) dbgs() |
| 495 | << " SCCPASSMGR: Re-visiting SCC, iteration #" << Iteration |
| 496 | << '\n'); |
| 497 | DevirtualizedCall = false; |
| 498 | Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall); |
| 499 | } while (Iteration++ < MaxIterations && DevirtualizedCall); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 500 | |
Evgeniy Stepanov | b7219f9 | 2018-07-13 16:32:31 +0000 | [diff] [blame] | 501 | if (DevirtualizedCall) |
| 502 | LLVM_DEBUG(dbgs() << " CGSCCPASSMGR: Stopped iteration after " |
| 503 | << Iteration |
| 504 | << " times, due to -max-cg-scc-iterations\n"); |
Craig Topper | ad0e666 | 2017-05-18 00:51:39 +0000 | [diff] [blame] | 505 | |
Evgeniy Stepanov | b7219f9 | 2018-07-13 16:32:31 +0000 | [diff] [blame] | 506 | MaxSCCIterations.updateMax(Iteration); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 507 | } |
Bill Wendling | 905c7e9 | 2009-02-11 18:19:24 +0000 | [diff] [blame] | 508 | Changed |= doFinalization(CG); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 509 | return Changed; |
| 510 | } |
| 511 | |
| 512 | /// Initialize CG |
Bill Wendling | 905c7e9 | 2009-02-11 18:19:24 +0000 | [diff] [blame] | 513 | bool CGPassManager::doInitialization(CallGraph &CG) { |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 514 | bool Changed = false; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 515 | for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) { |
Chris Lattner | 5a66cb9 | 2010-01-22 05:46:59 +0000 | [diff] [blame] | 516 | if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) { |
| 517 | assert(PM->getPassManagerType() == PMT_FunctionPassManager && |
| 518 | "Invalid CGPassManager member"); |
| 519 | Changed |= ((FPPassManager*)PM)->doInitialization(CG.getModule()); |
Nick Lewycky | 8968a07 | 2009-02-13 07:15:53 +0000 | [diff] [blame] | 520 | } else { |
Chris Lattner | 5a66cb9 | 2010-01-22 05:46:59 +0000 | [diff] [blame] | 521 | Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG); |
Nick Lewycky | 8968a07 | 2009-02-13 07:15:53 +0000 | [diff] [blame] | 522 | } |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 523 | } |
| 524 | return Changed; |
| 525 | } |
| 526 | |
| 527 | /// Finalize CG |
Bill Wendling | 905c7e9 | 2009-02-11 18:19:24 +0000 | [diff] [blame] | 528 | bool CGPassManager::doFinalization(CallGraph &CG) { |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 529 | bool Changed = false; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 530 | for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) { |
Chris Lattner | 5a66cb9 | 2010-01-22 05:46:59 +0000 | [diff] [blame] | 531 | if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) { |
| 532 | assert(PM->getPassManagerType() == PMT_FunctionPassManager && |
| 533 | "Invalid CGPassManager member"); |
| 534 | Changed |= ((FPPassManager*)PM)->doFinalization(CG.getModule()); |
Nick Lewycky | 8968a07 | 2009-02-13 07:15:53 +0000 | [diff] [blame] | 535 | } else { |
Chris Lattner | 5a66cb9 | 2010-01-22 05:46:59 +0000 | [diff] [blame] | 536 | Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG); |
Nick Lewycky | 8968a07 | 2009-02-13 07:15:53 +0000 | [diff] [blame] | 537 | } |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 538 | } |
| 539 | return Changed; |
| 540 | } |
| 541 | |
Chris Lattner | c93760c | 2010-04-16 21:43:55 +0000 | [diff] [blame] | 542 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 543 | // CallGraphSCC Implementation |
| 544 | //===----------------------------------------------------------------------===// |
| 545 | |
Sanjay Patel | ec53c65 | 2015-03-10 03:48:14 +0000 | [diff] [blame] | 546 | /// This informs the SCC and the pass manager that the specified |
Chris Lattner | a3dfc64 | 2010-04-16 22:59:24 +0000 | [diff] [blame] | 547 | /// Old node has been deleted, and New is to be used in its place. |
| 548 | void CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) { |
| 549 | assert(Old != New && "Should not replace node with self"); |
| 550 | for (unsigned i = 0; ; ++i) { |
| 551 | assert(i != Nodes.size() && "Node not in SCC"); |
| 552 | if (Nodes[i] != Old) continue; |
| 553 | Nodes[i] = New; |
| 554 | break; |
| 555 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 556 | |
Chris Lattner | bde0bb5 | 2010-04-16 23:04:30 +0000 | [diff] [blame] | 557 | // Update the active scc_iterator so that it doesn't contain dangling |
| 558 | // pointers to the old CallGraphNode. |
| 559 | scc_iterator<CallGraph*> *CGI = (scc_iterator<CallGraph*>*)Context; |
| 560 | CGI->ReplaceNode(Old, New); |
Chris Lattner | a3dfc64 | 2010-04-16 22:59:24 +0000 | [diff] [blame] | 561 | } |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 562 | |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 563 | //===----------------------------------------------------------------------===// |
Chris Lattner | c93760c | 2010-04-16 21:43:55 +0000 | [diff] [blame] | 564 | // CallGraphSCCPass Implementation |
| 565 | //===----------------------------------------------------------------------===// |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 566 | |
Devang Patel | d9f10c3 | 2007-01-23 21:55:17 +0000 | [diff] [blame] | 567 | /// Assign pass manager to manage this pass. |
Devang Patel | 97fd243 | 2007-01-23 21:52:35 +0000 | [diff] [blame] | 568 | void CallGraphSCCPass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 569 | PassManagerType PreferredType) { |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 570 | // Find CGPassManager |
Duncan Sands | 20d824b | 2007-07-19 09:42:01 +0000 | [diff] [blame] | 571 | while (!PMS.empty() && |
| 572 | PMS.top()->getPassManagerType() > PMT_CallGraphPassManager) |
| 573 | PMS.pop(); |
Devang Patel | 97fd243 | 2007-01-23 21:52:35 +0000 | [diff] [blame] | 574 | |
Chris Lattner | 77c95ed | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 575 | assert(!PMS.empty() && "Unable to handle Call Graph Pass"); |
| 576 | CGPassManager *CGP; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 577 | |
Chris Lattner | 77c95ed | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 578 | if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager) |
| 579 | CGP = (CGPassManager*)PMS.top(); |
| 580 | else { |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 581 | // Create new Call Graph SCC Pass Manager if it does not exist. |
Chris Lattner | 77c95ed | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 582 | assert(!PMS.empty() && "Unable to create Call Graph Pass Manager"); |
Devang Patel | 97fd243 | 2007-01-23 21:52:35 +0000 | [diff] [blame] | 583 | PMDataManager *PMD = PMS.top(); |
| 584 | |
| 585 | // [1] Create new Call Graph Pass Manager |
Andrew Trick | 0e122d1 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 586 | CGP = new CGPassManager(); |
Devang Patel | 97fd243 | 2007-01-23 21:52:35 +0000 | [diff] [blame] | 587 | |
| 588 | // [2] Set up new manager's top level manager |
| 589 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 590 | TPM->addIndirectPassManager(CGP); |
| 591 | |
| 592 | // [3] Assign manager to manage this new manager. This may create |
| 593 | // and push new managers into PMS |
Chris Lattner | 77c95ed | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 594 | Pass *P = CGP; |
Devang Patel | 25e681a | 2007-06-21 22:29:02 +0000 | [diff] [blame] | 595 | TPM->schedulePass(P); |
Devang Patel | 97fd243 | 2007-01-23 21:52:35 +0000 | [diff] [blame] | 596 | |
| 597 | // [4] Push new manager into PMS |
| 598 | PMS.push(CGP); |
| 599 | } |
| 600 | |
| 601 | CGP->add(this); |
| 602 | } |
| 603 | |
Sanjay Patel | ec53c65 | 2015-03-10 03:48:14 +0000 | [diff] [blame] | 604 | /// For this class, we declare that we require and preserve the call graph. |
| 605 | /// If the derived class implements this method, it should |
Chris Lattner | 4a81067 | 2003-08-31 01:54:59 +0000 | [diff] [blame] | 606 | /// always explicitly call the implementation here. |
| 607 | void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const { |
Chandler Carruth | 54fec07 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 608 | AU.addRequired<CallGraphWrapperPass>(); |
| 609 | AU.addPreserved<CallGraphWrapperPass>(); |
Chris Lattner | 4a81067 | 2003-08-31 01:54:59 +0000 | [diff] [blame] | 610 | } |
Chris Lattner | c93760c | 2010-04-16 21:43:55 +0000 | [diff] [blame] | 611 | |
Chris Lattner | c93760c | 2010-04-16 21:43:55 +0000 | [diff] [blame] | 612 | //===----------------------------------------------------------------------===// |
| 613 | // PrintCallGraphPass Implementation |
| 614 | //===----------------------------------------------------------------------===// |
| 615 | |
| 616 | namespace { |
Eugene Zelenko | 046ca04 | 2017-08-31 21:56:16 +0000 | [diff] [blame] | 617 | |
Chris Lattner | c93760c | 2010-04-16 21:43:55 +0000 | [diff] [blame] | 618 | /// PrintCallGraphPass - Print a Module corresponding to a call graph. |
| 619 | /// |
| 620 | class PrintCallGraphPass : public CallGraphSCCPass { |
| 621 | std::string Banner; |
Eugene Zelenko | 046ca04 | 2017-08-31 21:56:16 +0000 | [diff] [blame] | 622 | raw_ostream &OS; // raw_ostream to print on. |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 623 | |
Chris Lattner | c93760c | 2010-04-16 21:43:55 +0000 | [diff] [blame] | 624 | public: |
| 625 | static char ID; |
Eugene Zelenko | 046ca04 | 2017-08-31 21:56:16 +0000 | [diff] [blame] | 626 | |
| 627 | PrintCallGraphPass(const std::string &B, raw_ostream &OS) |
| 628 | : CallGraphSCCPass(ID), Banner(B), OS(OS) {} |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 629 | |
| 630 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chris Lattner | c93760c | 2010-04-16 21:43:55 +0000 | [diff] [blame] | 631 | AU.setPreservesAll(); |
| 632 | } |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 633 | |
| 634 | bool runOnSCC(CallGraphSCC &SCC) override { |
Yaron Keren | eb2b31d | 2017-06-12 02:18:50 +0000 | [diff] [blame] | 635 | bool BannerPrinted = false; |
Fedor Sergeev | 90f4912 | 2018-12-03 14:48:15 +0000 | [diff] [blame] | 636 | auto PrintBannerOnce = [&]() { |
Mehdi Amini | 01355e8 | 2017-01-18 21:37:11 +0000 | [diff] [blame] | 637 | if (BannerPrinted) |
| 638 | return; |
Eugene Zelenko | 046ca04 | 2017-08-31 21:56:16 +0000 | [diff] [blame] | 639 | OS << Banner; |
Mehdi Amini | 01355e8 | 2017-01-18 21:37:11 +0000 | [diff] [blame] | 640 | BannerPrinted = true; |
Fedor Sergeev | 90f4912 | 2018-12-03 14:48:15 +0000 | [diff] [blame] | 641 | }; |
| 642 | |
| 643 | bool NeedModule = llvm::forcePrintModuleIR(); |
| 644 | if (isFunctionInPrintList("*") && NeedModule) { |
| 645 | PrintBannerOnce(); |
| 646 | OS << "\n"; |
| 647 | SCC.getCallGraph().getModule().print(OS, nullptr); |
| 648 | return false; |
| 649 | } |
| 650 | bool FoundFunction = false; |
Sanjay Patel | 9e894a9 | 2015-03-10 03:26:39 +0000 | [diff] [blame] | 651 | for (CallGraphNode *CGN : SCC) { |
Yaron Keren | eb2b31d | 2017-06-12 02:18:50 +0000 | [diff] [blame] | 652 | if (Function *F = CGN->getFunction()) { |
| 653 | if (!F->isDeclaration() && isFunctionInPrintList(F->getName())) { |
Fedor Sergeev | 90f4912 | 2018-12-03 14:48:15 +0000 | [diff] [blame] | 654 | FoundFunction = true; |
| 655 | if (!NeedModule) { |
| 656 | PrintBannerOnce(); |
| 657 | F->print(OS); |
| 658 | } |
Mehdi Amini | 01355e8 | 2017-01-18 21:37:11 +0000 | [diff] [blame] | 659 | } |
Eugene Zelenko | 046ca04 | 2017-08-31 21:56:16 +0000 | [diff] [blame] | 660 | } else if (isFunctionInPrintList("*")) { |
Mehdi Amini | 01355e8 | 2017-01-18 21:37:11 +0000 | [diff] [blame] | 661 | PrintBannerOnce(); |
Eugene Zelenko | 046ca04 | 2017-08-31 21:56:16 +0000 | [diff] [blame] | 662 | OS << "\nPrinting <null> Function\n"; |
Mehdi Amini | 01355e8 | 2017-01-18 21:37:11 +0000 | [diff] [blame] | 663 | } |
Richard Trieu | f31ecd3 | 2014-06-09 22:53:16 +0000 | [diff] [blame] | 664 | } |
Fedor Sergeev | 90f4912 | 2018-12-03 14:48:15 +0000 | [diff] [blame] | 665 | if (NeedModule && FoundFunction) { |
| 666 | PrintBannerOnce(); |
| 667 | OS << "\n"; |
| 668 | SCC.getCallGraph().getModule().print(OS, nullptr); |
| 669 | } |
Chris Lattner | c93760c | 2010-04-16 21:43:55 +0000 | [diff] [blame] | 670 | return false; |
| 671 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 672 | |
Yaron Keren | ea56368 | 2017-03-10 07:09:20 +0000 | [diff] [blame] | 673 | StringRef getPassName() const override { return "Print CallGraph IR"; } |
Chris Lattner | c93760c | 2010-04-16 21:43:55 +0000 | [diff] [blame] | 674 | }; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 675 | |
Chris Lattner | c93760c | 2010-04-16 21:43:55 +0000 | [diff] [blame] | 676 | } // end anonymous namespace. |
| 677 | |
| 678 | char PrintCallGraphPass::ID = 0; |
| 679 | |
Eugene Zelenko | 046ca04 | 2017-08-31 21:56:16 +0000 | [diff] [blame] | 680 | Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &OS, |
Chris Lattner | c93760c | 2010-04-16 21:43:55 +0000 | [diff] [blame] | 681 | const std::string &Banner) const { |
Eugene Zelenko | 046ca04 | 2017-08-31 21:56:16 +0000 | [diff] [blame] | 682 | return new PrintCallGraphPass(Banner, OS); |
Chris Lattner | c93760c | 2010-04-16 21:43:55 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Andrew Kaylor | 1e455c5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 685 | bool CallGraphSCCPass::skipSCC(CallGraphSCC &SCC) const { |
| 686 | return !SCC.getCallGraph().getModule() |
| 687 | .getContext() |
Fedor Sergeev | 558f76f | 2018-03-27 16:57:20 +0000 | [diff] [blame] | 688 | .getOptPassGate() |
Andrew Kaylor | 1e455c5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 689 | .shouldRunPass(this, SCC); |
| 690 | } |
Mehdi Amini | 32b9ed8 | 2016-06-10 16:19:46 +0000 | [diff] [blame] | 691 | |
| 692 | char DummyCGSCCPass::ID = 0; |
Eugene Zelenko | 046ca04 | 2017-08-31 21:56:16 +0000 | [diff] [blame] | 693 | |
Mehdi Amini | 32b9ed8 | 2016-06-10 16:19:46 +0000 | [diff] [blame] | 694 | INITIALIZE_PASS(DummyCGSCCPass, "DummyCGSCCPass", "DummyCGSCCPass", false, |
| 695 | false) |