llvm.org GIT mirror llvm / 896f064
[PM/AA] Remove the last relics of the separate IPA library from LLVM, folding the code into the main Analysis library. There already wasn't much of a distinction between Analysis and IPA. A number of the passes in Analysis are actually IPA passes, and there doesn't seem to be any advantage to separating them. Moreover, it makes it hard to have interactions between analyses that are both local and interprocedural. In trying to make the Alias Analysis infrastructure work with the new pass manager, it becomes particularly awkward to navigate this split. I've tried to find all the places where we referenced this, but I may have missed some. I have also adjusted the C API to continue to be equivalently functional after this change. Differential Revision: http://reviews.llvm.org/D12075 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@245318 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 4 years ago
34 changed file(s) with 3301 addition(s) and 3387 deletion(s). Raw diff Collapse all Expand all
464464 library.LLVMInitializeAnalysis.argtypes = [PassRegistry]
465465 library.LLVMInitializeAnalysis.restype = None
466466
467 library.LLVMInitializeIPA.argtypes = [PassRegistry]
468 library.LLVMInitializeIPA.restype = None
469
470467 library.LLVMInitializeCodeGen.argtypes = [PassRegistry]
471468 library.LLVMInitializeCodeGen.restype = None
472469
620617 lib.LLVMInitializeIPO(p)
621618 lib.LLVMInitializeInstrumentation(p)
622619 lib.LLVMInitializeAnalysis(p)
623 lib.LLVMInitializeIPA(p)
624620 lib.LLVMInitializeCodeGen(p)
625621 lib.LLVMInitializeTarget(p)
626622
5151
5252 /// initializeAnalysis - Initialize all passes linked into the Analysis library.
5353 void initializeAnalysis(PassRegistry&);
54
55 /// initializeIPA - Initialize all passes linked into the IPA library.
56 void initializeIPA(PassRegistry&);
5754
5855 /// initializeCodeGen - Initialize all passes linked into the CodeGen library.
5956 void initializeCodeGen(PassRegistry&);
2727 initializeBasicAliasAnalysisPass(Registry);
2828 initializeBlockFrequencyInfoWrapperPassPass(Registry);
2929 initializeBranchProbabilityInfoWrapperPassPass(Registry);
30 initializeCallGraphWrapperPassPass(Registry);
31 initializeCallGraphPrinterPass(Registry);
32 initializeCallGraphViewerPass(Registry);
3033 initializeCostModelAnalysisPass(Registry);
3134 initializeCFGViewerPass(Registry);
3235 initializeCFGPrinterPass(Registry);
4649 initializePostDomPrinterPass(Registry);
4750 initializePostDomOnlyViewerPass(Registry);
4851 initializePostDomOnlyPrinterPass(Registry);
52 initializeGlobalsModRefPass(Registry);
4953 initializeIVUsersPass(Registry);
5054 initializeInstCountPass(Registry);
5155 initializeIntervalPartitionPass(Registry);
7074 }
7175
7276 void LLVMInitializeAnalysis(LLVMPassRegistryRef R) {
77 initializeAnalysis(*unwrap(R));
78 }
79
80 void LLVMInitializeIPA(LLVMPassRegistryRef R) {
7381 initializeAnalysis(*unwrap(R));
7482 }
7583
1212 CFGPrinter.cpp
1313 CFLAliasAnalysis.cpp
1414 CGSCCPassManager.cpp
15 CallGraph.cpp
16 CallGraphSCCPass.cpp
17 CallPrinter.cpp
1518 CaptureTracking.cpp
1619 CostModel.cpp
1720 CodeMetrics.cpp
2225 DivergenceAnalysis.cpp
2326 DomPrinter.cpp
2427 DominanceFrontier.cpp
28 GlobalsModRef.cpp
2529 IVUsers.cpp
30 InlineCost.cpp
2631 InstCount.cpp
2732 InstructionSimplify.cpp
2833 Interval.cpp
6873 )
6974
7075 add_dependencies(LLVMAnalysis intrinsics_gen)
71
72 add_subdirectory(IPA)
0 //===- CallGraph.cpp - Build a Module's call graph ------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Analysis/CallGraph.h"
10 #include "llvm/IR/CallSite.h"
11 #include "llvm/IR/Instructions.h"
12 #include "llvm/IR/IntrinsicInst.h"
13 #include "llvm/IR/Module.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/raw_ostream.h"
16 using namespace llvm;
17
18 //===----------------------------------------------------------------------===//
19 // Implementations of the CallGraph class methods.
20 //
21
22 CallGraph::CallGraph(Module &M)
23 : M(M), Root(nullptr), ExternalCallingNode(getOrInsertFunction(nullptr)),
24 CallsExternalNode(llvm::make_unique(nullptr)) {
25 // Add every function to the call graph.
26 for (Function &F : M)
27 addToCallGraph(&F);
28
29 // If we didn't find a main function, use the external call graph node
30 if (!Root)
31 Root = ExternalCallingNode;
32 }
33
34 CallGraph::CallGraph(CallGraph &&Arg)
35 : M(Arg.M), FunctionMap(std::move(Arg.FunctionMap)), Root(Arg.Root),
36 ExternalCallingNode(Arg.ExternalCallingNode),
37 CallsExternalNode(std::move(Arg.CallsExternalNode)) {
38 Arg.FunctionMap.clear();
39 Arg.Root = nullptr;
40 Arg.ExternalCallingNode = nullptr;
41 }
42
43 CallGraph::~CallGraph() {
44 // CallsExternalNode is not in the function map, delete it explicitly.
45 if (CallsExternalNode)
46 CallsExternalNode->allReferencesDropped();
47
48 // Reset all node's use counts to zero before deleting them to prevent an
49 // assertion from firing.
50 #ifndef NDEBUG
51 for (auto &I : FunctionMap)
52 I.second->allReferencesDropped();
53 #endif
54 }
55
56 void CallGraph::addToCallGraph(Function *F) {
57 CallGraphNode *Node = getOrInsertFunction(F);
58
59 // If this function has external linkage, anything could call it.
60 if (!F->hasLocalLinkage()) {
61 ExternalCallingNode->addCalledFunction(CallSite(), Node);
62
63 // Found the entry point?
64 if (F->getName() == "main") {
65 if (Root) // Found multiple external mains? Don't pick one.
66 Root = ExternalCallingNode;
67 else
68 Root = Node; // Found a main, keep track of it!
69 }
70 }
71
72 // If this function has its address taken, anything could call it.
73 if (F->hasAddressTaken())
74 ExternalCallingNode->addCalledFunction(CallSite(), Node);
75
76 // If this function is not defined in this translation unit, it could call
77 // anything.
78 if (F->isDeclaration() && !F->isIntrinsic())
79 Node->addCalledFunction(CallSite(), CallsExternalNode.get());
80
81 // Look for calls by this function.
82 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
83 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE;
84 ++II) {
85 CallSite CS(cast(II));
86 if (CS) {
87 const Function *Callee = CS.getCalledFunction();
88 if (!Callee || !Intrinsic::isLeaf(Callee->getIntrinsicID()))
89 // Indirect calls of intrinsics are not allowed so no need to check.
90 // We can be more precise here by using TargetArg returned by
91 // Intrinsic::isLeaf.
92 Node->addCalledFunction(CS, CallsExternalNode.get());
93 else if (!Callee->isIntrinsic())
94 Node->addCalledFunction(CS, getOrInsertFunction(Callee));
95 }
96 }
97 }
98
99 void CallGraph::print(raw_ostream &OS) const {
100 OS << "CallGraph Root is: ";
101 if (Function *F = Root->getFunction())
102 OS << F->getName() << "\n";
103 else {
104 OS << "<>\n";
105 }
106
107 // Print in a deterministic order by sorting CallGraphNodes by name. We do
108 // this here to avoid slowing down the non-printing fast path.
109
110 SmallVector Nodes;
111 Nodes.reserve(FunctionMap.size());
112
113 for (auto I = begin(), E = end(); I != E; ++I)
114 Nodes.push_back(I->second.get());
115
116 std::sort(Nodes.begin(), Nodes.end(),
117 [](CallGraphNode *LHS, CallGraphNode *RHS) {
118 if (Function *LF = LHS->getFunction())
119 if (Function *RF = RHS->getFunction())
120 return LF->getName() < RF->getName();
121
122 return RHS->getFunction() != nullptr;
123 });
124
125 for (CallGraphNode *CN : Nodes)
126 CN->print(OS);
127 }
128
129 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
130 void CallGraph::dump() const { print(dbgs()); }
131 #endif
132
133 // removeFunctionFromModule - Unlink the function from this module, returning
134 // it. Because this removes the function from the module, the call graph node
135 // is destroyed. This is only valid if the function does not call any other
136 // functions (ie, there are no edges in it's CGN). The easiest way to do this
137 // is to dropAllReferences before calling this.
138 //
139 Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
140 assert(CGN->empty() && "Cannot remove function from call "
141 "graph if it references other functions!");
142 Function *F = CGN->getFunction(); // Get the function for the call graph node
143 FunctionMap.erase(F); // Remove the call graph node from the map
144
145 M.getFunctionList().remove(F);
146 return F;
147 }
148
149 /// spliceFunction - Replace the function represented by this node by another.
150 /// This does not rescan the body of the function, so it is suitable when
151 /// splicing the body of the old function to the new while also updating all
152 /// callers from old to new.
153 ///
154 void CallGraph::spliceFunction(const Function *From, const Function *To) {
155 assert(FunctionMap.count(From) && "No CallGraphNode for function!");
156 assert(!FunctionMap.count(To) &&
157 "Pointing CallGraphNode at a function that already exists");
158 FunctionMapTy::iterator I = FunctionMap.find(From);
159 I->second->F = const_cast(To);
160 FunctionMap[To] = std::move(I->second);
161 FunctionMap.erase(I);
162 }
163
164 // getOrInsertFunction - This method is identical to calling operator[], but
165 // it will insert a new CallGraphNode for the specified function if one does
166 // not already exist.
167 CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
168 auto &CGN = FunctionMap[F];
169 if (CGN)
170 return CGN.get();
171
172 assert((!F || F->getParent() == &M) && "Function not in current module!");
173 CGN = llvm::make_unique(const_cast(F));
174 return CGN.get();
175 }
176
177 //===----------------------------------------------------------------------===//
178 // Implementations of the CallGraphNode class methods.
179 //
180
181 void CallGraphNode::print(raw_ostream &OS) const {
182 if (Function *F = getFunction())
183 OS << "Call graph node for function: '" << F->getName() << "'";
184 else
185 OS << "Call graph node <>";
186
187 OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n';
188
189 for (const_iterator I = begin(), E = end(); I != E; ++I) {
190 OS << " CS<" << I->first << "> calls ";
191 if (Function *FI = I->second->getFunction())
192 OS << "function '" << FI->getName() <<"'\n";
193 else
194 OS << "external node\n";
195 }
196 OS << '\n';
197 }
198
199 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
200 void CallGraphNode::dump() const { print(dbgs()); }
201 #endif
202
203 /// removeCallEdgeFor - This method removes the edge in the node for the
204 /// specified call site. Note that this method takes linear time, so it
205 /// should be used sparingly.
206 void CallGraphNode::removeCallEdgeFor(CallSite CS) {
207 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
208 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
209 if (I->first == CS.getInstruction()) {
210 I->second->DropRef();
211 *I = CalledFunctions.back();
212 CalledFunctions.pop_back();
213 return;
214 }
215 }
216 }
217
218 // removeAnyCallEdgeTo - This method removes any call edges from this node to
219 // the specified callee function. This takes more time to execute than
220 // removeCallEdgeTo, so it should not be used unless necessary.
221 void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
222 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
223 if (CalledFunctions[i].second == Callee) {
224 Callee->DropRef();
225 CalledFunctions[i] = CalledFunctions.back();
226 CalledFunctions.pop_back();
227 --i; --e;
228 }
229 }
230
231 /// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
232 /// from this node to the specified callee function.
233 void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) {
234 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
235 assert(I != CalledFunctions.end() && "Cannot find callee to remove!");
236 CallRecord &CR = *I;
237 if (CR.second == Callee && CR.first == nullptr) {
238 Callee->DropRef();
239 *I = CalledFunctions.back();
240 CalledFunctions.pop_back();
241 return;
242 }
243 }
244 }
245
246 /// replaceCallEdge - This method replaces the edge in the node for the
247 /// specified call site with a new one. Note that this method takes linear
248 /// time, so it should be used sparingly.
249 void CallGraphNode::replaceCallEdge(CallSite CS,
250 CallSite NewCS, CallGraphNode *NewNode){
251 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
252 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
253 if (I->first == CS.getInstruction()) {
254 I->second->DropRef();
255 I->first = NewCS.getInstruction();
256 I->second = NewNode;
257 NewNode->AddRef();
258 return;
259 }
260 }
261 }
262
263 //===----------------------------------------------------------------------===//
264 // Out-of-line definitions of CallGraphAnalysis class members.
265 //
266
267 char CallGraphAnalysis::PassID;
268
269 //===----------------------------------------------------------------------===//
270 // Implementations of the CallGraphWrapperPass class methods.
271 //
272
273 CallGraphWrapperPass::CallGraphWrapperPass() : ModulePass(ID) {
274 initializeCallGraphWrapperPassPass(*PassRegistry::getPassRegistry());
275 }
276
277 CallGraphWrapperPass::~CallGraphWrapperPass() {}
278
279 void CallGraphWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
280 AU.setPreservesAll();
281 }
282
283 bool CallGraphWrapperPass::runOnModule(Module &M) {
284 // All the real work is done in the constructor for the CallGraph.
285 G.reset(new CallGraph(M));
286 return false;
287 }
288
289 INITIALIZE_PASS(CallGraphWrapperPass, "basiccg", "CallGraph Construction",
290 false, true)
291
292 char CallGraphWrapperPass::ID = 0;
293
294 void CallGraphWrapperPass::releaseMemory() { G.reset(); }
295
296 void CallGraphWrapperPass::print(raw_ostream &OS, const Module *) const {
297 if (!G) {
298 OS << "No call graph has been built!\n";
299 return;
300 }
301
302 // Just delegate.
303 G->print(OS);
304 }
305
306 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
307 void CallGraphWrapperPass::dump() const { print(dbgs(), nullptr); }
308 #endif
0 //===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the CallGraphSCCPass class, which is used for passes
10 // which are implemented as bottom-up traversals on the call graph. Because
11 // there may be cycles in the call graph, passes of this type operate on the
12 // call-graph in SCC order: that is, they process function bottom-up, except for
13 // recursive functions, which they process all at once.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Analysis/CallGraphSCCPass.h"
18 #include "llvm/ADT/SCCIterator.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Analysis/CallGraph.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/IntrinsicInst.h"
23 #include "llvm/IR/LLVMContext.h"
24 #include "llvm/IR/LegacyPassManagers.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/Timer.h"
28 #include "llvm/Support/raw_ostream.h"
29 using namespace llvm;
30
31 #define DEBUG_TYPE "cgscc-passmgr"
32
33 static cl::opt
34 MaxIterations("max-cg-scc-iterations", cl::ReallyHidden, cl::init(4));
35
36 STATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC");
37
38 //===----------------------------------------------------------------------===//
39 // CGPassManager
40 //
41 /// CGPassManager manages FPPassManagers and CallGraphSCCPasses.
42
43 namespace {
44
45 class CGPassManager : public ModulePass, public PMDataManager {
46 public:
47 static char ID;
48 explicit CGPassManager()
49 : ModulePass(ID), PMDataManager() { }
50
51 /// Execute all of the passes scheduled for execution. Keep track of
52 /// whether any of the passes modifies the module, and if so, return true.
53 bool runOnModule(Module &M) override;
54
55 using ModulePass::doInitialization;
56 using ModulePass::doFinalization;
57
58 bool doInitialization(CallGraph &CG);
59 bool doFinalization(CallGraph &CG);
60
61 /// Pass Manager itself does not invalidate any analysis info.
62 void getAnalysisUsage(AnalysisUsage &Info) const override {
63 // CGPassManager walks SCC and it needs CallGraph.
64 Info.addRequired();
65 Info.setPreservesAll();
66 }
67
68 const char *getPassName() const override {
69 return "CallGraph Pass Manager";
70 }
71
72 PMDataManager *getAsPMDataManager() override { return this; }
73 Pass *getAsPass() override { return this; }
74
75 // Print passes managed by this manager
76 void dumpPassStructure(unsigned Offset) override {
77 errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n";
78 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
79 Pass *P = getContainedPass(Index);
80 P->dumpPassStructure(Offset + 1);
81 dumpLastUses(P, Offset+1);
82 }
83 }
84
85 Pass *getContainedPass(unsigned N) {
86 assert(N < PassVector.size() && "Pass number out of range!");
87 return static_cast(PassVector[N]);
88 }
89
90 PassManagerType getPassManagerType() const override {
91 return PMT_CallGraphPassManager;
92 }
93
94 private:
95 bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
96 bool &DevirtualizedCall);
97
98 bool RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
99 CallGraph &CG, bool &CallGraphUpToDate,
100 bool &DevirtualizedCall);
101 bool RefreshCallGraph(CallGraphSCC &CurSCC, CallGraph &CG,
102 bool IsCheckingMode);
103 };
104
105 } // end anonymous namespace.
106
107 char CGPassManager::ID = 0;
108
109
110 bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
111 CallGraph &CG, bool &CallGraphUpToDate,
112 bool &DevirtualizedCall) {
113 bool Changed = false;
114 PMDataManager *PM = P->getAsPMDataManager();
115
116 if (!PM) {
117 CallGraphSCCPass *CGSP = (CallGraphSCCPass*)P;
118 if (!CallGraphUpToDate) {
119 DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
120 CallGraphUpToDate = true;
121 }
122
123 {
124 TimeRegion PassTimer(getPassTimer(CGSP));
125 Changed = CGSP->runOnSCC(CurSCC);
126 }
127
128 // After the CGSCCPass is done, when assertions are enabled, use
129 // RefreshCallGraph to verify that the callgraph was correctly updated.
130 #ifndef NDEBUG
131 if (Changed)
132 RefreshCallGraph(CurSCC, CG, true);
133 #endif
134
135 return Changed;
136 }
137
138
139 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
140 "Invalid CGPassManager member");
141 FPPassManager *FPP = (FPPassManager*)P;
142
143 // Run pass P on all functions in the current SCC.
144 for (CallGraphNode *CGN : CurSCC) {
145 if (Function *F = CGN->getFunction()) {
146 dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
147 {
148 TimeRegion PassTimer(getPassTimer(FPP));
149 Changed |= FPP->runOnFunction(*F);
150 }
151 F->getContext().yield();
152 }
153 }
154
155 // The function pass(es) modified the IR, they may have clobbered the
156 // callgraph.
157 if (Changed && CallGraphUpToDate) {
158 DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: "
159 << P->getPassName() << '\n');
160 CallGraphUpToDate = false;
161 }
162 return Changed;
163 }
164
165
166 /// Scan the functions in the specified CFG and resync the
167 /// callgraph with the call sites found in it. This is used after
168 /// FunctionPasses have potentially munged the callgraph, and can be used after
169 /// CallGraphSCC passes to verify that they correctly updated the callgraph.
170 ///
171 /// This function returns true if it devirtualized an existing function call,
172 /// meaning it turned an indirect call into a direct call. This happens when
173 /// a function pass like GVN optimizes away stuff feeding the indirect call.
174 /// This never happens in checking mode.
175 ///
176 bool CGPassManager::RefreshCallGraph(CallGraphSCC &CurSCC,
177 CallGraph &CG, bool CheckingMode) {
178 DenseMap CallSites;
179
180 DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size()
181 << " nodes:\n";
182 for (CallGraphNode *CGN : CurSCC)
183 CGN->dump();
184 );
185
186 bool MadeChange = false;
187 bool DevirtualizedCall = false;
188
189 // Scan all functions in the SCC.
190 unsigned FunctionNo = 0;
191 for (CallGraphSCC::iterator SCCIdx = CurSCC.begin(), E = CurSCC.end();
192 SCCIdx != E; ++SCCIdx, ++FunctionNo) {
193 CallGraphNode *CGN = *SCCIdx;
194 Function *F = CGN->getFunction();
195 if (!F || F->isDeclaration()) continue;
196
197 // Walk the function body looking for call sites. Sync up the call sites in
198 // CGN with those actually in the function.
199
200 // Keep track of the number of direct and indirect calls that were
201 // invalidated and removed.
202 unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0;
203
204 // Get the set of call sites currently in the function.
205 for (CallGraphNode::iterator I = CGN->begin(), E = CGN->end(); I != E; ) {
206 // If this call site is null, then the function pass deleted the call
207 // entirely and the WeakVH nulled it out.
208 if (!I->first ||
209 // If we've already seen this call site, then the FunctionPass RAUW'd
210 // one call with another, which resulted in two "uses" in the edge
211 // list of the same call.
212 CallSites.count(I->first) ||
213
214 // If the call edge is not from a call or invoke, or it is a
215 // instrinsic call, then the function pass RAUW'd a call with
216 // another value. This can happen when constant folding happens
217 // of well known functions etc.
218 !CallSite(I->first) ||
219 (CallSite(I->first).getCalledFunction() &&
220 CallSite(I->first).getCalledFunction()->isIntrinsic() &&
221 Intrinsic::isLeaf(
222 CallSite(I->first).getCalledFunction()->getIntrinsicID()))) {
223 assert(!CheckingMode &&
224 "CallGraphSCCPass did not update the CallGraph correctly!");
225
226 // If this was an indirect call site, count it.
227 if (!I->second->getFunction())
228 ++NumIndirectRemoved;
229 else
230 ++NumDirectRemoved;
231
232 // Just remove the edge from the set of callees, keep track of whether
233 // I points to the last element of the vector.
234 bool WasLast = I + 1 == E;
235 CGN->removeCallEdge(I);
236
237 // If I pointed to the last element of the vector, we have to bail out:
238 // iterator checking rejects comparisons of the resultant pointer with
239 // end.
240 if (WasLast)
241 break;
242 E = CGN->end();
243 continue;
244 }
245
246 assert(!CallSites.count(I->first) &&
247 "Call site occurs in node multiple times");
248
249 CallSite CS(I->first);
250 if (CS) {
251 Function *Callee = CS.getCalledFunction();
252 // Ignore intrinsics because they're not really function calls.
253 if (!Callee || !(Callee->isIntrinsic()))
254 CallSites.insert(std::make_pair(I->first, I->second));
255 }
256 ++I;
257 }
258
259 // Loop over all of the instructions in the function, getting the callsites.
260 // Keep track of the number of direct/indirect calls added.
261 unsigned NumDirectAdded = 0, NumIndirectAdded = 0;
262
263 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
264 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
265 CallSite CS(cast(I));
266 if (!CS) continue;
267 Function *Callee = CS.getCalledFunction();
268 if (Callee && Callee->isIntrinsic()) continue;
269
270 // If this call site already existed in the callgraph, just verify it
271 // matches up to expectations and remove it from CallSites.
272 DenseMap::iterator ExistingIt =
273 CallSites.find(CS.getInstruction());
274 if (ExistingIt != CallSites.end()) {
275 CallGraphNode *ExistingNode = ExistingIt->second;
276
277 // Remove from CallSites since we have now seen it.
278 CallSites.erase(ExistingIt);
279
280 // Verify that the callee is right.
281 if (ExistingNode->getFunction() == CS.getCalledFunction())
282 continue;
283
284 // If we are in checking mode, we are not allowed to actually mutate
285 // the callgraph. If this is a case where we can infer that the
286 // callgraph is less precise than it could be (e.g. an indirect call
287 // site could be turned direct), don't reject it in checking mode, and
288 // don't tweak it to be more precise.
289 if (CheckingMode && CS.getCalledFunction() &&
290 ExistingNode->getFunction() == nullptr)
291 continue;
292
293 assert(!CheckingMode &&
294 "CallGraphSCCPass did not update the CallGraph correctly!");
295
296 // If not, we either went from a direct call to indirect, indirect to
297 // direct, or direct to different direct.
298 CallGraphNode *CalleeNode;
299 if (Function *Callee = CS.getCalledFunction()) {
300 CalleeNode = CG.getOrInsertFunction(Callee);
301 // Keep track of whether we turned an indirect call into a direct
302 // one.
303 if (!ExistingNode->getFunction()) {
304 DevirtualizedCall = true;
305 DEBUG(dbgs() << " CGSCCPASSMGR: Devirtualized call to '"
306 << Callee->getName() << "'\n");
307 }
308 } else {
309 CalleeNode = CG.getCallsExternalNode();
310 }
311
312 // Update the edge target in CGN.
313 CGN->replaceCallEdge(CS, CS, CalleeNode);
314 MadeChange = true;
315 continue;
316 }
317
318 assert(!CheckingMode &&
319 "CallGraphSCCPass did not update the CallGraph correctly!");
320
321 // If the call site didn't exist in the CGN yet, add it.
322 CallGraphNode *CalleeNode;
323 if (Function *Callee = CS.getCalledFunction()) {
324 CalleeNode = CG.getOrInsertFunction(Callee);
325 ++NumDirectAdded;
326 } else {
327 CalleeNode = CG.getCallsExternalNode();
328 ++NumIndirectAdded;
329 }
330
331 CGN->addCalledFunction(CS, CalleeNode);
332 MadeChange = true;
333 }
334
335 // We scanned the old callgraph node, removing invalidated call sites and
336 // then added back newly found call sites. One thing that can happen is
337 // that an old indirect call site was deleted and replaced with a new direct
338 // call. In this case, we have devirtualized a call, and CGSCCPM would like
339 // to iteratively optimize the new code. Unfortunately, we don't really
340 // have a great way to detect when this happens. As an approximation, we
341 // just look at whether the number of indirect calls is reduced and the
342 // number of direct calls is increased. There are tons of ways to fool this
343 // (e.g. DCE'ing an indirect call and duplicating an unrelated block with a
344 // direct call) but this is close enough.
345 if (NumIndirectRemoved > NumIndirectAdded &&
346 NumDirectRemoved < NumDirectAdded)
347 DevirtualizedCall = true;
348
349 // After scanning this function, if we still have entries in callsites, then
350 // they are dangling pointers. WeakVH should save us for this, so abort if
351 // this happens.
352 assert(CallSites.empty() && "Dangling pointers found in call sites map");
353
354 // Periodically do an explicit clear to remove tombstones when processing
355 // large scc's.
356 if ((FunctionNo & 15) == 15)
357 CallSites.clear();
358 }
359
360 DEBUG(if (MadeChange) {
361 dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n";
362 for (CallGraphNode *CGN : CurSCC)
363 CGN->dump();
364 if (DevirtualizedCall)
365 dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n";
366
367 } else {
368 dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n";
369 }
370 );
371 (void)MadeChange;
372
373 return DevirtualizedCall;
374 }
375
376 /// Execute the body of the entire pass manager on the specified SCC.
377 /// This keeps track of whether a function pass devirtualizes
378 /// any calls and returns it in DevirtualizedCall.
379 bool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
380 bool &DevirtualizedCall) {
381 bool Changed = false;
382
383 // Keep track of whether the callgraph is known to be up-to-date or not.
384 // The CGSSC pass manager runs two types of passes:
385 // CallGraphSCC Passes and other random function passes. Because other
386 // random function passes are not CallGraph aware, they may clobber the
387 // call graph by introducing new calls or deleting other ones. This flag
388 // is set to false when we run a function pass so that we know to clean up
389 // the callgraph when we need to run a CGSCCPass again.
390 bool CallGraphUpToDate = true;
391
392 // Run all passes on current SCC.
393 for (unsigned PassNo = 0, e = getNumContainedPasses();
394 PassNo != e; ++PassNo) {
395 Pass *P = getContainedPass(PassNo);
396
397 // If we're in -debug-pass=Executions mode, construct the SCC node list,
398 // otherwise avoid constructing this string as it is expensive.
399 if (isPassDebuggingExecutionsOrMore()) {
400 std::string Functions;
401 #ifndef NDEBUG
402 raw_string_ostream OS(Functions);
403 for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
404 I != E; ++I) {
405 if (I != CurSCC.begin()) OS << ", ";
406 (*I)->print(OS);
407 }
408 OS.flush();
409 #endif
410 dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions);
411 }
412 dumpRequiredSet(P);
413
414 initializeAnalysisImpl(P);
415
416 // Actually run this pass on the current SCC.
417 Changed |= RunPassOnSCC(P, CurSCC, CG,
418 CallGraphUpToDate, DevirtualizedCall);
419
420 if (Changed)
421 dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
422 dumpPreservedSet(P);
423
424 verifyPreservedAnalysis(P);
425 removeNotPreservedAnalysis(P);
426 recordAvailableAnalysis(P);
427 removeDeadPasses(P, "", ON_CG_MSG);
428 }
429
430 // If the callgraph was left out of date (because the last pass run was a
431 // functionpass), refresh it before we move on to the next SCC.
432 if (!CallGraphUpToDate)
433 DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
434 return Changed;
435 }
436
437 /// Execute all of the passes scheduled for execution. Keep track of
438 /// whether any of the passes modifies the module, and if so, return true.
439 bool CGPassManager::runOnModule(Module &M) {
440 CallGraph &CG = getAnalysis().getCallGraph();
441 bool Changed = doInitialization(CG);
442
443 // Walk the callgraph in bottom-up SCC order.
444 scc_iterator CGI = scc_begin(&CG);
445
446 CallGraphSCC CurSCC(&CGI);
447 while (!CGI.isAtEnd()) {
448 // Copy the current SCC and increment past it so that the pass can hack
449 // on the SCC if it wants to without invalidating our iterator.
450 const std::vector &NodeVec = *CGI;
451 CurSCC.initialize(NodeVec.data(), NodeVec.data() + NodeVec.size());
452 ++CGI;
453
454 // At the top level, we run all the passes in this pass manager on the
455 // functions in this SCC. However, we support iterative compilation in the
456 // case where a function pass devirtualizes a call to a function. For
457 // example, it is very common for a function pass (often GVN or instcombine)
458 // to eliminate the addressing that feeds into a call. With that improved
459 // information, we would like the call to be an inline candidate, infer
460 // mod-ref information etc.
461 //
462 // Because of this, we allow iteration up to a specified iteration count.
463 // This only happens in the case of a devirtualized call, so we only burn
464 // compile time in the case that we're making progress. We also have a hard
465 // iteration count limit in case there is crazy code.
466 unsigned Iteration = 0;
467 bool DevirtualizedCall = false;
468 do {
469 DEBUG(if (Iteration)
470 dbgs() << " SCCPASSMGR: Re-visiting SCC, iteration #"
471 << Iteration << '\n');
472 DevirtualizedCall = false;
473 Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall);
474 } while (Iteration++ < MaxIterations && DevirtualizedCall);
475
476 if (DevirtualizedCall)
477 DEBUG(dbgs() << " CGSCCPASSMGR: Stopped iteration after " << Iteration
478 << " times, due to -max-cg-scc-iterations\n");
479
480 if (Iteration > MaxSCCIterations)
481 MaxSCCIterations = Iteration;
482
483 }
484 Changed |= doFinalization(CG);
485 return Changed;
486 }
487
488
489 /// Initialize CG
490 bool CGPassManager::doInitialization(CallGraph &CG) {
491 bool Changed = false;
492 for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
493 if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
494 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
495 "Invalid CGPassManager member");
496 Changed |= ((FPPassManager*)PM)->doInitialization(CG.getModule());
497 } else {
498 Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG);
499 }
500 }
501 return Changed;
502 }
503
504 /// Finalize CG
505 bool CGPassManager::doFinalization(CallGraph &CG) {
506 bool Changed = false;
507 for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
508 if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
509 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
510 "Invalid CGPassManager member");
511 Changed |= ((FPPassManager*)PM)->doFinalization(CG.getModule());
512 } else {
513 Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG);
514 }
515 }
516 return Changed;
517 }
518
519 //===----------------------------------------------------------------------===//
520 // CallGraphSCC Implementation
521 //===----------------------------------------------------------------------===//
522
523 /// This informs the SCC and the pass manager that the specified
524 /// Old node has been deleted, and New is to be used in its place.
525 void CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) {
526 assert(Old != New && "Should not replace node with self");
527 for (unsigned i = 0; ; ++i) {
528 assert(i != Nodes.size() && "Node not in SCC");
529 if (Nodes[i] != Old) continue;
530 Nodes[i] = New;
531 break;
532 }
533
534 // Update the active scc_iterator so that it doesn't contain dangling
535 // pointers to the old CallGraphNode.
536 scc_iterator *CGI = (scc_iterator*)Context;
537 CGI->ReplaceNode(Old, New);
538 }
539
540
541 //===----------------------------------------------------------------------===//
542 // CallGraphSCCPass Implementation
543 //===----------------------------------------------------------------------===//
544
545 /// Assign pass manager to manage this pass.
546 void CallGraphSCCPass::assignPassManager(PMStack &PMS,
547 PassManagerType PreferredType) {
548 // Find CGPassManager
549 while (!PMS.empty() &&
550 PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
551 PMS.pop();
552
553 assert(!PMS.empty() && "Unable to handle Call Graph Pass");
554 CGPassManager *CGP;
555
556 if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager)
557 CGP = (CGPassManager*)PMS.top();
558 else {
559 // Create new Call Graph SCC Pass Manager if it does not exist.
560 assert(!PMS.empty() && "Unable to create Call Graph Pass Manager");
561 PMDataManager *PMD = PMS.top();
562
563 // [1] Create new Call Graph Pass Manager
564 CGP = new CGPassManager();
565
566 // [2] Set up new manager's top level manager
567 PMTopLevelManager *TPM = PMD->getTopLevelManager();
568 TPM->addIndirectPassManager(CGP);
569
570 // [3] Assign manager to manage this new manager. This may create
571 // and push new managers into PMS
572 Pass *P = CGP;
573 TPM->schedulePass(P);
574
575 // [4] Push new manager into PMS
576 PMS.push(CGP);
577 }
578
579 CGP->add(this);
580 }
581
582 /// For this class, we declare that we require and preserve the call graph.
583 /// If the derived class implements this method, it should
584 /// always explicitly call the implementation here.
585 void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
586 AU.addRequired();
587 AU.addPreserved();
588 }
589
590
591 //===----------------------------------------------------------------------===//
592 // PrintCallGraphPass Implementation
593 //===----------------------------------------------------------------------===//
594
595 namespace {
596 /// PrintCallGraphPass - Print a Module corresponding to a call graph.
597 ///
598 class PrintCallGraphPass : public CallGraphSCCPass {
599 std::string Banner;
600 raw_ostream &Out; // raw_ostream to print on.
601
602 public:
603 static char ID;
604 PrintCallGraphPass(const std::string &B, raw_ostream &o)
605 : CallGraphSCCPass(ID), Banner(B), Out(o) {}
606
607 void getAnalysisUsage(AnalysisUsage &AU) const override {
608 AU.setPreservesAll();
609 }
610
611 bool runOnSCC(CallGraphSCC &SCC) override {
612 Out << Banner;
613 for (CallGraphNode *CGN : SCC) {
614 if (CGN->getFunction())
615 CGN->getFunction()->print(Out);
616 else
617 Out << "\nPrinting Function\n";
618 }
619 return false;
620 }
621 };
622
623 } // end anonymous namespace.
624
625 char PrintCallGraphPass::ID = 0;
626
627 Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &O,
628 const std::string &Banner) const {
629 return new PrintCallGraphPass(Banner, O);
630 }
631
0 //===- CallPrinter.cpp - DOT printer for call graph -----------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines '-dot-callgraph', which emit a callgraph..dot
10 // containing the call graph of a module.
11 //
12 // There is also a pass available to directly call dotty ('-view-callgraph').
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Analysis/CallGraph.h"
17 #include "llvm/Analysis/CallPrinter.h"
18 #include "llvm/Analysis/DOTGraphTraitsPass.h"
19
20 using namespace llvm;
21
22 namespace llvm {
23
24 template <> struct DOTGraphTraits : public DefaultDOTGraphTraits {
25 DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
26
27 static std::string getGraphName(CallGraph *Graph) { return "Call graph"; }
28
29 std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
30 if (Function *Func = Node->getFunction())
31 return Func->getName();
32
33 return "external node";
34 }
35 };
36
37 struct AnalysisCallGraphWrapperPassTraits {
38 static CallGraph *getGraph(CallGraphWrapperPass *P) {
39 return &P->getCallGraph();
40 }
41 };
42
43 } // end llvm namespace
44
45 namespace {
46
47 struct CallGraphViewer
48 : public DOTGraphTraitsModuleViewer
49 AnalysisCallGraphWrapperPassTraits> {
50 static char ID;
51
52 CallGraphViewer()
53 : DOTGraphTraitsModuleViewer
54 AnalysisCallGraphWrapperPassTraits>(
55 "callgraph", ID) {
56 initializeCallGraphViewerPass(*PassRegistry::getPassRegistry());
57 }
58 };
59
60 struct CallGraphPrinter : public DOTGraphTraitsModulePrinter<
61 CallGraphWrapperPass, true, CallGraph *,
62 AnalysisCallGraphWrapperPassTraits> {
63 static char ID;
64
65 CallGraphPrinter()
66 : DOTGraphTraitsModulePrinter
67 AnalysisCallGraphWrapperPassTraits>(
68 "callgraph", ID) {
69 initializeCallGraphPrinterPass(*PassRegistry::getPassRegistry());
70 }
71 };
72
73 } // end anonymous namespace
74
75 char CallGraphViewer::ID = 0;
76 INITIALIZE_PASS(CallGraphViewer, "view-callgraph", "View call graph", false,
77 false)
78
79 char CallGraphPrinter::ID = 0;
80 INITIALIZE_PASS(CallGraphPrinter, "dot-callgraph",
81 "Print call graph to 'dot' file", false, false)
82
83 // Create methods available outside of this file, to use them
84 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
85 // the link time optimization.
86
87 ModulePass *llvm::createCallGraphViewerPass() { return new CallGraphViewer(); }
88
89 ModulePass *llvm::createCallGraphPrinterPass() {
90 return new CallGraphPrinter();
91 }
0 //===- GlobalsModRef.cpp - Simple Mod/Ref Analysis for Globals ------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This simple pass provides alias and mod/ref information for global values
10 // that do not have their address taken, and keeps track of whether functions
11 // read or write memory (are "pure"). For this simple (but very common) case,
12 // we can provide pretty accurate and useful information.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Analysis/GlobalsModRef.h"
17 #include "llvm/ADT/SCCIterator.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Analysis/MemoryBuiltins.h"
21 #include "llvm/Analysis/ValueTracking.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/InstIterator.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Support/CommandLine.h"
29 using namespace llvm;
30
31 #define DEBUG_TYPE "globalsmodref-aa"
32
33 STATISTIC(NumNonAddrTakenGlobalVars,
34 "Number of global vars without address taken");
35 STATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken");
36 STATISTIC(NumNoMemFunctions, "Number of functions that do not access memory");
37 STATISTIC(NumReadMemFunctions, "Number of functions that only read memory");
38 STATISTIC(NumIndirectGlobalVars, "Number of indirect global objects");
39
40 // An option to enable unsafe alias results from the GlobalsModRef analysis.
41 // When enabled, GlobalsModRef will provide no-alias results which in extremely
42 // rare cases may not be conservatively correct. In particular, in the face of
43 // transforms which cause assymetry between how effective GetUnderlyingObject
44 // is for two pointers, it may produce incorrect results.
45 //
46 // These unsafe results have been returned by GMR for many years without
47 // causing significant issues in the wild and so we provide a mechanism to
48 // re-enable them for users of LLVM that have a particular performance
49 // sensitivity and no known issues. The option also makes it easy to evaluate
50 // the performance impact of these results.
51 static cl::opt EnableUnsafeGlobalsModRefAliasResults(
52 "enable-unsafe-globalsmodref-alias-results", cl::init(false), cl::Hidden);
53
54 /// The mod/ref information collected for a particular function.
55 ///
56 /// We collect information about mod/ref behavior of a function here, both in
57 /// general and as pertains to specific globals. We only have this detailed
58 /// information when we know *something* useful about the behavior. If we
59 /// saturate to fully general mod/ref, we remove the info for the function.
60 class GlobalsModRef::FunctionInfo {
61 typedef SmallDenseMap GlobalInfoMapType;
62
63 /// Build a wrapper struct that has 8-byte alignment. All heap allocations
64 /// should provide this much alignment at least, but this makes it clear we
65 /// specifically rely on this amount of alignment.
66 struct LLVM_ALIGNAS(8) AlignedMap {
67 AlignedMap() {}
68 AlignedMap(const AlignedMap &Arg) : Map(Arg.Map) {}
69 GlobalInfoMapType Map;
70 };
71
72 /// Pointer traits for our aligned map.
73 struct AlignedMapPointerTraits {
74 static inline void *getAsVoidPointer(AlignedMap *P) { return P; }
75 static inline AlignedMap *getFromVoidPointer(void *P) {
76 return (AlignedMap *)P;
77 }
78 enum { NumLowBitsAvailable = 3 };
79 static_assert(AlignOf::Alignment >= (1 << NumLowBitsAvailable),
80 "AlignedMap insufficiently aligned to have enough low bits.");
81 };
82
83 /// The bit that flags that this function may read any global. This is
84 /// chosen to mix together with ModRefInfo bits.
85 enum { MayReadAnyGlobal = 4 };
86
87 /// Checks to document the invariants of the bit packing here.
88 static_assert((MayReadAnyGlobal & MRI_ModRef) == 0,
89 "ModRef and the MayReadAnyGlobal flag bits overlap.");
90 static_assert(((MayReadAnyGlobal | MRI_ModRef) >>
91 AlignedMapPointerTraits::NumLowBitsAvailable) == 0,
92 "Insufficient low bits to store our flag and ModRef info.");
93
94 public:
95 FunctionInfo() : Info() {}
96 ~FunctionInfo() {
97 delete Info.getPointer();
98 }
99 // Spell out the copy ond move constructors and assignment operators to get
100 // deep copy semantics and correct move semantics in the face of the
101 // pointer-int pair.
102 FunctionInfo(const FunctionInfo &Arg)
103 : Info(nullptr, Arg.Info.getInt()) {
104 if (const auto *ArgPtr = Arg.Info.getPointer())
105 Info.setPointer(new AlignedMap(*ArgPtr));
106 }
107 FunctionInfo(FunctionInfo &&Arg)
108 : Info(Arg.Info.getPointer(), Arg.Info.getInt()) {
109 Arg.Info.setPointerAndInt(nullptr, 0);
110 }
111 FunctionInfo &operator=(const FunctionInfo &RHS) {
112 delete Info.getPointer();
113 Info.setPointerAndInt(nullptr, RHS.Info.getInt());
114 if (const auto *RHSPtr = RHS.Info.getPointer())
115 Info.setPointer(new AlignedMap(*RHSPtr));
116 return *this;
117 }
118 FunctionInfo &operator=(FunctionInfo &&RHS) {
119 delete Info.getPointer();
120 Info.setPointerAndInt(RHS.Info.getPointer(), RHS.Info.getInt());
121 RHS.Info.setPointerAndInt(nullptr, 0);
122 return *this;
123 }
124
125 /// Returns the \c ModRefInfo info for this function.
126 ModRefInfo getModRefInfo() const {
127 return ModRefInfo(Info.getInt() & MRI_ModRef);
128 }
129
130 /// Adds new \c ModRefInfo for this function to its state.
131 void addModRefInfo(ModRefInfo NewMRI) {
132 Info.setInt(Info.getInt() | NewMRI);
133 }
134
135 /// Returns whether this function may read any global variable, and we don't
136 /// know which global.
137 bool mayReadAnyGlobal() const { return Info.getInt() & MayReadAnyGlobal; }
138
139 /// Sets this function as potentially reading from any global.
140 void setMayReadAnyGlobal() { Info.setInt(Info.getInt() | MayReadAnyGlobal); }
141
142 /// Returns the \c ModRefInfo info for this function w.r.t. a particular
143 /// global, which may be more precise than the general information above.
144 ModRefInfo getModRefInfoForGlobal(const GlobalValue &GV) const {
145 ModRefInfo GlobalMRI = mayReadAnyGlobal() ? MRI_Ref : MRI_NoModRef;
146 if (AlignedMap *P = Info.getPointer()) {
147 auto I = P->Map.find(&GV);
148 if (I != P->Map.end())
149 GlobalMRI = ModRefInfo(GlobalMRI | I->second);
150 }
151 return GlobalMRI;
152 }
153
154 /// Add mod/ref info from another function into ours, saturating towards
155 /// MRI_ModRef.
156 void addFunctionInfo(const FunctionInfo &FI) {
157 addModRefInfo(FI.getModRefInfo());
158
159 if (FI.mayReadAnyGlobal())
160 setMayReadAnyGlobal();
161
162 if (AlignedMap *P = FI.Info.getPointer())
163 for (const auto &G : P->Map)
164 addModRefInfoForGlobal(*G.first, G.second);
165 }
166
167 void addModRefInfoForGlobal(const GlobalValue &GV, ModRefInfo NewMRI) {
168 AlignedMap *P = Info.getPointer();
169 if (!P) {
170 P = new AlignedMap();
171 Info.setPointer(P);
172 }
173 auto &GlobalMRI = P->Map[&GV];
174 GlobalMRI = ModRefInfo(GlobalMRI | NewMRI);
175 }
176
177 /// Clear a global's ModRef info. Should be used when a global is being
178 /// deleted.
179 void eraseModRefInfoForGlobal(const GlobalValue &GV) {
180 if (AlignedMap *P = Info.getPointer())
181 P->Map.erase(&GV);
182 }
183
184 private:
185 /// All of the information is encoded into a single pointer, with a three bit
186 /// integer in the low three bits. The high bit provides a flag for when this
187 /// function may read any global. The low two bits are the ModRefInfo. And
188 /// the pointer, when non-null, points to a map from GlobalValue to
189 /// ModRefInfo specific to that GlobalValue.
190 PointerIntPair Info;
191 };
192
193 void GlobalsModRef::DeletionCallbackHandle::deleted() {
194 Value *V = getValPtr();
195 if (auto *F = dyn_cast(V))
196 GMR.FunctionInfos.erase(F);
197
198 if (GlobalValue *GV = dyn_cast(V)) {
199 if (GMR.NonAddressTakenGlobals.erase(GV)) {
200 // This global might be an indirect global. If so, remove it and
201 // remove any AllocRelatedValues for it.
202 if (GMR.IndirectGlobals.erase(GV)) {
203 // Remove any entries in AllocsForIndirectGlobals for this global.
204 for (auto I = GMR.AllocsForIndirectGlobals.begin(),
205 E = GMR.AllocsForIndirectGlobals.end();
206 I != E; ++I)
207 if (I->second == GV)
208 GMR.AllocsForIndirectGlobals.erase(I);
209 }
210
211 // Scan the function info we have collected and remove this global
212 // from all of them.
213 for (auto &FIPair : GMR.FunctionInfos)
214 FIPair.second.eraseModRefInfoForGlobal(*GV);
215 }
216 }
217
218 // If this is an allocation related to an indirect global, remove it.
219 GMR.AllocsForIndirectGlobals.erase(V);
220
221 // And clear out the handle.
222 setValPtr(nullptr);
223 GMR.Handles.erase(I);
224 // This object is now destroyed!
225 }
226
227 char GlobalsModRef::ID = 0;
228 INITIALIZE_AG_PASS_BEGIN(GlobalsModRef, AliasAnalysis, "globalsmodref-aa",
229 "Simple mod/ref analysis for globals", false, true,
230 false)
231 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
232 INITIALIZE_AG_PASS_END(GlobalsModRef, AliasAnalysis, "globalsmodref-aa",
233 "Simple mod/ref analysis for globals", false, true,
234 false)
235
236 Pass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); }
237
238 GlobalsModRef::GlobalsModRef() : ModulePass(ID) {
239 initializeGlobalsModRefPass(*PassRegistry::getPassRegistry());
240 }
241
242 FunctionModRefBehavior GlobalsModRef::getModRefBehavior(const Function *F) {
243 FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
244
245 if (FunctionInfo *FI = getFunctionInfo(F)) {
246 if (FI->getModRefInfo() == MRI_NoModRef)
247 Min = FMRB_DoesNotAccessMemory;
248 else if ((FI->getModRefInfo() & MRI_Mod) == 0)
249 Min = FMRB_OnlyReadsMemory;
250 }
251
252 return FunctionModRefBehavior(AliasAnalysis::getModRefBehavior(F) & Min);
253 }
254
255 FunctionModRefBehavior GlobalsModRef::getModRefBehavior(ImmutableCallSite CS) {
256 FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
257
258 if (const Function *F = CS.getCalledFunction())
259 if (FunctionInfo *FI = getFunctionInfo(F)) {
260 if (FI->getModRefInfo() == MRI_NoModRef)
261 Min = FMRB_DoesNotAccessMemory;
262 else if ((FI->getModRefInfo() & MRI_Mod) == 0)
263 Min = FMRB_OnlyReadsMemory;
264 }
265
266 return FunctionModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min);
267 }
268
269 /// Returns the function info for the function, or null if we don't have
270 /// anything useful to say about it.
271 GlobalsModRef::FunctionInfo *GlobalsModRef::getFunctionInfo(const Function *F) {
272 auto I = FunctionInfos.find(F);
273 if (I != FunctionInfos.end())
274 return &I->second;
275 return nullptr;
276 }
277
278 /// AnalyzeGlobals - Scan through the users of all of the internal
279 /// GlobalValue's in the program. If none of them have their "address taken"
280 /// (really, their address passed to something nontrivial), record this fact,
281 /// and record the functions that they are used directly in.
282 void GlobalsModRef::AnalyzeGlobals(Module &M) {
283 SmallPtrSet TrackedFunctions;
284 for (Function &F : M)
285 if (F.hasLocalLinkage())
286 if (!AnalyzeUsesOfPointer(&F)) {
287 // Remember that we are tracking this global.
288 NonAddressTakenGlobals.insert(&F);
289 TrackedFunctions.insert(&F);
290 Handles.emplace_front(*this, &F);
291 Handles.front().I = Handles.begin();
292 ++NumNonAddrTakenFunctions;
293 }
294
295 SmallPtrSet Readers, Writers;
296 for (GlobalVariable &GV : M.globals())
297 if (GV.hasLocalLinkage()) {
298 if (!AnalyzeUsesOfPointer(&GV, &Readers,
299 GV.isConstant() ? nullptr : &Writers)) {
300 // Remember that we are tracking this global, and the mod/ref fns
301 NonAddressTakenGlobals.insert(&GV);
302 Handles.emplace_front(*this, &GV);
303 Handles.front().I = Handles.begin();
304
305 for (Function *Reader : Readers) {
306 if (TrackedFunctions.insert(Reader).second) {
307 Handles.emplace_front(*this, Reader);
308 Handles.front().I = Handles.begin();
309 }
310 FunctionInfos[Reader].addModRefInfoForGlobal(GV, MRI_Ref);
311 }
312
313 if (!GV.isConstant()) // No need to keep track of writers to constants
314 for (Function *Writer : Writers) {
315 if (TrackedFunctions.insert(Writer).second) {
316 Handles.emplace_front(*this, Writer);
317 Handles.front().I = Handles.begin();
318 }
319 FunctionInfos[Writer].addModRefInfoForGlobal(GV, MRI_Mod);
320 }
321 ++NumNonAddrTakenGlobalVars;
322
323 // If this global holds a pointer type, see if it is an indirect global.
324 if (GV.getType()->getElementType()->isPointerTy() &&
325 AnalyzeIndirectGlobalMemory(&GV))
326 ++NumIndirectGlobalVars;
327 }
328 Readers.clear();
329 Writers.clear();
330 }
331 }
332
333 /// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer.
334 /// If this is used by anything complex (i.e., the address escapes), return
335 /// true. Also, while we are at it, keep track of those functions that read and
336 /// write to the value.
337 ///
338 /// If OkayStoreDest is non-null, stores into this global are allowed.
339 bool GlobalsModRef::AnalyzeUsesOfPointer(Value *V,
340 SmallPtrSetImpl *Readers,
341 SmallPtrSetImpl *Writers,
342 GlobalValue *OkayStoreDest) {
343 if (!V->getType()->isPointerTy())
344 return true;
345
346 for (Use &U : V->uses()) {
347 User *I = U.getUser();
348 if (LoadInst *LI = dyn_cast(I)) {
349 if (Readers)
350 Readers->insert(LI->getParent()->getParent());
351 } else if (StoreInst *SI = dyn_cast(I)) {
352 if (V == SI->getOperand(1)) {
353 if (Writers)
354 Writers->insert(SI->getParent()->getParent());
355 } else if (SI->getOperand(1) != OkayStoreDest) {
356 return true; // Storing the pointer
357 }
358 } else if (Operator::getOpcode(I) == Instruction::GetElementPtr) {
359 if (AnalyzeUsesOfPointer(I, Readers, Writers))
360 return true;
361 } else if (Operator::getOpcode(I) == Instruction::BitCast) {
362 if (AnalyzeUsesOfPointer(I, Readers, Writers, OkayStoreDest))
363 return true;
364 } else if (auto CS = CallSite(I)) {
365 // Make sure that this is just the function being called, not that it is
366 // passing into the function.
367 if (!CS.isCallee(&U)) {
368 // Detect calls to free.
369 if (isFreeCall(I, TLI)) {
370 if (Writers)
371 Writers->insert(CS->getParent()->getParent());
372 } else {
373 return true; // Argument of an unknown call.
374 }
375 }
376 } else if (ICmpInst *ICI = dyn_cast(I)) {
377 if (!isa(ICI->getOperand(1)))
378 return true; // Allow comparison against null.
379 } else {
380 return true;
381 }
382 }
383
384 return false;
385 }
386
387 /// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable
388 /// which holds a pointer type. See if the global always points to non-aliased
389 /// heap memory: that is, all initializers of the globals are allocations, and
390 /// those allocations have no use other than initialization of the global.
391 /// Further, all loads out of GV must directly use the memory, not store the
392 /// pointer somewhere. If this is true, we consider the memory pointed to by
393 /// GV to be owned by GV and can disambiguate other pointers from it.
394 bool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) {
395 // Keep track of values related to the allocation of the memory, f.e. the
396 // value produced by the malloc call and any casts.
397 std::vector AllocRelatedValues;
398
399 // Walk the user list of the global. If we find anything other than a direct
400 // load or store, bail out.
401 for (User *U : GV->users()) {
402 if (LoadInst *LI = dyn_cast(U)) {
403 // The pointer loaded from the global can only be used in simple ways:
404 // we allow addressing of it and loading storing to it. We do *not* allow
405 // storing the loaded pointer somewhere else or passing to a function.
406 if (AnalyzeUsesOfPointer(LI))
407 return false; // Loaded pointer escapes.
408 // TODO: Could try some IP mod/ref of the loaded pointer.
409 } else if (StoreInst *SI = dyn_cast(U)) {
410 // Storing the global itself.
411 if (SI->getOperand(0) == GV)
412 return false;
413
414 // If storing the null pointer, ignore it.
415 if (isa(SI->getOperand(0)))
416 continue;
417
418 // Check the value being stored.
419 Value *Ptr = GetUnderlyingObject(SI->getOperand(0),
420 GV->getParent()->getDataLayout());
421
422 if (!isAllocLikeFn(Ptr, TLI))
423 return false; // Too hard to analyze.
424
425 // Analyze all uses of the allocation. If any of them are used in a
426 // non-simple way (e.g. stored to another global) bail out.
427 if (AnalyzeUsesOfPointer(Ptr, /*Readers*/ nullptr, /*Writers*/ nullptr,
428 GV))
429 return false; // Loaded pointer escapes.
430
431 // Remember that this allocation is related to the indirect global.
432 AllocRelatedValues.push_back(Ptr);
433 } else {
434 // Something complex, bail out.
435 return false;
436 }
437 }
438
439 // Okay, this is an indirect global. Remember all of the allocations for
440 // this global in AllocsForIndirectGlobals.
441 while (!AllocRelatedValues.empty()) {
442 AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV;
443 Handles.emplace_front(*this, AllocRelatedValues.back());
444 Handles.front().I = Handles.begin();
445 AllocRelatedValues.pop_back();
446 }
447 IndirectGlobals.insert(GV);
448 Handles.emplace_front(*this, GV);
449 Handles.front().I = Handles.begin();
450 return true;
451 }
452
453 /// AnalyzeCallGraph - At this point, we know the functions where globals are
454 /// immediately stored to and read from. Propagate this information up the call
455 /// graph to all callers and compute the mod/ref info for all memory for each
456 /// function.
457 void GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) {
458 // We do a bottom-up SCC traversal of the call graph. In other words, we
459 // visit all callees before callers (leaf-first).
460 for (scc_iterator I = scc_begin(&CG); !I.isAtEnd(); ++I) {
461 const std::vector &SCC = *I;
462 assert(!SCC.empty() && "SCC with no functions?");
463
464 if (!SCC[0]->getFunction()) {
465 // Calls externally - can't say anything useful. Remove any existing
466 // function records (may have been created when scanning globals).
467 for (auto *Node : SCC)
468 FunctionInfos.erase(Node->getFunction());
469 continue;
470 }
471
472 FunctionInfo &FI = FunctionInfos[SCC[0]->getFunction()];
473 bool KnowNothing = false;
474
475 // Collect the mod/ref properties due to called functions. We only compute
476 // one mod-ref set.
477 for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) {
478 Function *F = SCC[i]->getFunction();
479 if (!F) {
480 KnowNothing = true;
481 break;
482 }
483
484 if (F->isDeclaration()) {
485 // Try to get mod/ref behaviour from function attributes.
486 if (F->doesNotAccessMemory()) {
487 // Can't do better than that!
488 } else if (F->onlyReadsMemory()) {
489 FI.addModRefInfo(MRI_Ref);
490 if (!F->isIntrinsic())
491 // This function might call back into the module and read a global -
492 // consider every global as possibly being read by this function.
493 FI.setMayReadAnyGlobal();
494 } else {
495 FI.addModRefInfo(MRI_ModRef);
496 // Can't say anything useful unless it's an intrinsic - they don't
497 // read or write global variables of the kind considered here.
498 KnowNothing = !F->isIntrinsic();
499 }
500 continue;
501 }
502
503 for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end();
504 CI != E && !KnowNothing; ++CI)
505 if (Function *Callee = CI->second->getFunction()) {
506 if (FunctionInfo *CalleeFI = getFunctionInfo(Callee)) {
507 // Propagate function effect up.
508 FI.addFunctionInfo(*CalleeFI);
509 } else {
510 // Can't say anything about it. However, if it is inside our SCC,
511 // then nothing needs to be done.
512 CallGraphNode *CalleeNode = CG[Callee];
513 if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end())
514 KnowNothing = true;
515 }
516 } else {
517 KnowNothing = true;
518 }
519 }
520
521 // If we can't say anything useful about this SCC, remove all SCC functions
522 // from the FunctionInfos map.
523 if (KnowNothing) {
524 for (auto *Node : SCC)
525 FunctionInfos.erase(Node->getFunction());
526 continue;
527 }
528
529 // Scan the function bodies for explicit loads or stores.
530 for (auto *Node : SCC) {
531 if (FI.getModRefInfo() == MRI_ModRef)
532 break; // The mod/ref lattice saturates here.
533 for (Instruction &I : instructions(Node->getFunction())) {
534 if (FI.getModRefInfo() == MRI_ModRef)
535 break; // The mod/ref lattice saturates here.
536
537 // We handle calls specially because the graph-relevant aspects are
538 // handled above.
539 if (auto CS = CallSite(&I)) {
540 if (isAllocationFn(&I, TLI) || isFreeCall(&I, TLI)) {
541 // FIXME: It is completely unclear why this is necessary and not
542 // handled by the above graph code.
543 FI.addModRefInfo(MRI_ModRef);
544 } else if (Function *Callee = CS.getCalledFunction()) {
545 // The callgraph doesn't include intrinsic calls.
546 if (Callee->isIntrinsic()) {
547 FunctionModRefBehavior Behaviour =
548 AliasAnalysis::getModRefBehavior(Callee);
549 FI.addModRefInfo(ModRefInfo(Behaviour & MRI_ModRef));
550 }
551 }
552 continue;
553 }
554
555 // All non-call instructions we use the primary predicates for whether
556 // thay read or write memory.
557 if (I.mayReadFromMemory())
558 FI.addModRefInfo(MRI_Ref);
559 if (I.mayWriteToMemory())
560 FI.addModRefInfo(MRI_Mod);
561 }
562 }
563
564 if ((FI.getModRefInfo() & MRI_Mod) == 0)
565 ++NumReadMemFunctions;
566 if (FI.getModRefInfo() == MRI_NoModRef)
567 ++NumNoMemFunctions;
568
569 // Finally, now that we know the full effect on this SCC, clone the
570 // information to each function in the SCC.
571 for (unsigned i = 1, e = SCC.size(); i != e; ++i)
572 FunctionInfos[SCC[i]->getFunction()] = FI;
573 }
574 }
575
576 // There are particular cases where we can conclude no-alias between
577 // a non-addr-taken global and some other underlying object. Specifically,
578 // a non-addr-taken global is known to not be escaped from any function. It is
579 // also incorrect for a transformation to introduce an escape of a global in
580 // a way that is observable when it was not there previously. One function
581 // being transformed to introduce an escape which could possibly be observed
582 // (via loading from a global or the return value for example) within another
583 // function is never safe. If the observation is made through non-atomic
584 // operations on different threads, it is a data-race and UB. If the
585 // observation is well defined, by being observed the transformation would have
586 // changed program behavior by introducing the observed escape, making it an
587 // invalid transform.
588 //
589 // This property does require that transformations which *temporarily* escape
590 // a global that was not previously escaped, prior to restoring it, cannot rely
591 // on the results of GMR::alias. This seems a reasonable restriction, although
592 // currently there is no way to enforce it. There is also no realistic
593 // optimization pass that would make this mistake. The closest example is
594 // a transformation pass which does reg2mem of SSA values but stores them into
595 // global variables temporarily before restoring the global variable's value.
596 // This could be useful to expose "benign" races for example. However, it seems
597 // reasonable to require that a pass which introduces escapes of global
598 // variables in this way to either not trust AA results while the escape is
599 // active, or to be forced to operate as a module pass that cannot co-exist
600 // with an alias analysis such as GMR.
601 bool GlobalsModRef::isNonEscapingGlobalNoAlias(const GlobalValue *GV,
602 const Value *V) {
603 // In order to know that the underlying object cannot alias the
604 // non-addr-taken global, we must know that it would have to be an escape.
605 // Thus if the underlying object is a function argument, a load from
606 // a global, or the return of a function, it cannot alias. We can also
607 // recurse through PHI nodes and select nodes provided all of their inputs
608 // resolve to one of these known-escaping roots.
609 SmallPtrSet Visited;
610 SmallVector Inputs;
611 Visited.insert(V);
612 Inputs.push_back(V);
613 int Depth = 0;
614 do {
615 const Value *Input = Inputs.pop_back_val();
616
617 if (auto *InputGV = dyn_cast(Input)) {
618 // If one input is the very global we're querying against, then we can't
619 // conclude no-alias.
620 if (InputGV == GV)
621 return false;
622
623 // Distinct GlobalVariables never alias, unless overriden or zero-sized.
624 // FIXME: The condition can be refined, but be conservative for now.
625 auto *GVar = dyn_cast(GV);
626 auto *InputGVar = dyn_cast(InputGV);
627 if (GVar && InputGVar &&
628 !GVar->isDeclaration() && !InputGVar->isDeclaration() &&
629 !GVar->mayBeOverridden() && !InputGVar->mayBeOverridden()) {
630 Type *GVType = GVar->getInitializer()->getType();
631 Type *InputGVType = InputGVar->getInitializer()->getType();
632 if (GVType->isSized() && InputGVType->isSized() &&
633 (DL->getTypeAllocSize(GVType) > 0) &&
634 (DL->getTypeAllocSize(InputGVType) > 0))
635 continue;
636 }
637
638 // Conservatively return false, even though we could be smarter
639 // (e.g. look through GlobalAliases).
640 return false;
641 }
642
643 if (isa(Input) || isa(Input) ||
644 isa(Input)) {
645 // Arguments to functions or returns from functions are inherently
646 // escaping, so we can immediately classify those as not aliasing any
647 // non-addr-taken globals.
648 continue;
649 }
650 if (auto *LI = dyn_cast(Input)) {
651 // A pointer loaded from a global would have been captured, and we know
652 // that the global is non-escaping, so no alias.
653 if (isa(GetUnderlyingObject(LI->getPointerOperand(), *DL)))
654 continue;
655
656 // Otherwise, a load could come from anywhere, so bail.
657 return false;
658 }
659
660 // Recurse through a limited number of selects and PHIs. This is an
661 // arbitrary depth of 4, lower numbers could be used to fix compile time
662 // issues if needed, but this is generally expected to be only be important
663 // for small depths.
664 if (++Depth > 4)
665 return false;
666 if (auto *SI = dyn_cast(Input)) {
667 const Value *LHS = GetUnderlyingObject(SI->getTrueValue(), *DL);
668 const Value *RHS = GetUnderlyingObject(SI->getFalseValue(), *DL);
669 if (Visited.insert(LHS).second)
670 Inputs.push_back(LHS);
671 if (Visited.insert(RHS).second)
672 Inputs.push_back(RHS);
673 continue;
674 }
675 if (auto *PN = dyn_cast(Input)) {
676 for (const Value *Op : PN->incoming_values()) {
677 Op = GetUnderlyingObject(Op, *DL);
678 if (Visited.insert(Op).second)
679 Inputs.push_back(Op);
680 }
681 continue;
682 }
683
684 // FIXME: It would be good to handle other obvious no-alias cases here, but
685 // it isn't clear how to do so reasonbly without building a small version
686 // of BasicAA into this code. We could recurse into AliasAnalysis::alias
687 // here but that seems likely to go poorly as we're inside the
688 // implementation of such a query. Until then, just conservatievly retun
689 // false.
690 return false;
691 } while (!Inputs.empty());
692
693 // If all the inputs to V were definitively no-alias, then V is no-alias.
694 return true;
695 }
696
697 /// alias - If one of the pointers is to a global that we are tracking, and the
698 /// other is some random pointer, we know there cannot be an alias, because the
699 /// address of the global isn't taken.
700 AliasResult GlobalsModRef::alias(const MemoryLocation &LocA,
701 const MemoryLocation &LocB) {
702 // Get the base object these pointers point to.
703 const Value *UV1 = GetUnderlyingObject(LocA.Ptr, *DL);
704 const Value *UV2 = GetUnderlyingObject(LocB.Ptr, *DL);
705
706 // If either of the underlying values is a global, they may be non-addr-taken
707 // globals, which we can answer queries about.
708 const GlobalValue *GV1 = dyn_cast(UV1);
709 const GlobalValue *GV2 = dyn_cast(UV2);
710 if (GV1 || GV2) {
711 // If the global's address is taken, pretend we don't know it's a pointer to
712 // the global.
713 if (GV1 && !NonAddressTakenGlobals.count(GV1))
714 GV1 = nullptr;
715 if (GV2 && !NonAddressTakenGlobals.count(GV2))
716 GV2 = nullptr;
717
718 // If the two pointers are derived from two different non-addr-taken
719 // globals we know these can't alias.
720 if (GV1 && GV2 && GV1 != GV2)
721 return NoAlias;
722
723 // If one is and the other isn't, it isn't strictly safe but we can fake
724 // this result if necessary for performance. This does not appear to be
725 // a common problem in practice.
726 if (EnableUnsafeGlobalsModRefAliasResults)
727 if ((GV1 || GV2) && GV1 != GV2)
728 return NoAlias;
729
730 // Check for a special case where a non-escaping global can be used to
731 // conclude no-alias.
732 if ((GV1 || GV2) && GV1 != GV2) {
733 const GlobalValue *GV = GV1 ? GV1 : GV2;
734 const Value *UV = GV1 ? UV2 : UV1;
735 if (isNonEscapingGlobalNoAlias(GV, UV))
736 return NoAlias;
737 }
738
739 // Otherwise if they are both derived from the same addr-taken global, we
740 // can't know the two accesses don't overlap.
741 }
742
743 // These pointers may be based on the memory owned by an indirect global. If
744 // so, we may be able to handle this. First check to see if the base pointer
745 // is a direct load from an indirect global.
746 GV1 = GV2 = nullptr;
747 if (const LoadInst *LI = dyn_cast(UV1))
748 if (GlobalVariable *GV = dyn_cast(LI->getOperand(0)))
749 if (IndirectGlobals.count(GV))
750 GV1 = GV;
751 if (const LoadInst *LI = dyn_cast(UV2))
752 if (const GlobalVariable *GV = dyn_cast(LI->getOperand(0)))
753 if (IndirectGlobals.count(GV))
754 GV2 = GV;
755
756 // These pointers may also be from an allocation for the indirect global. If
757 // so, also handle them.
758 if (!GV1)
759 GV1 = AllocsForIndirectGlobals.lookup(UV1);
760 if (!GV2)
761 GV2 = AllocsForIndirectGlobals.lookup(UV2);
762
763 // Now that we know whether the two pointers are related to indirect globals,
764 // use this to disambiguate the pointers. If the pointers are based on
765 // different indirect globals they cannot alias.
766 if (GV1 && GV2 && GV1 != GV2)
767 return NoAlias;
768
769 // If one is based on an indirect global and the other isn't, it isn't
770 // strictly safe but we can fake this result if necessary for performance.
771 // This does not appear to be a common problem in practice.
772 if (EnableUnsafeGlobalsModRefAliasResults)
773 if ((GV1 || GV2) && GV1 != GV2)
774 return NoAlias;
775
776 return AliasAnalysis::alias(LocA, LocB);
777 }
778
779 ModRefInfo GlobalsModRef::getModRefInfo(ImmutableCallSite CS,
780 const MemoryLocation &Loc) {
781 unsigned Known = MRI_ModRef;
782
783 // If we are asking for mod/ref info of a direct call with a pointer to a
784 // global we are tracking, return information if we have it.
785 const DataLayout &DL = CS.getCaller()->getParent()->getDataLayout();
786 if (const GlobalValue *GV =
787 dyn_cast(GetUnderlyingObject(Loc.Ptr, DL)))
788 if (GV->hasLocalLinkage())
789 if (const Function *F = CS.getCalledFunction())
790 if (NonAddressTakenGlobals.count(GV))
791 if (const FunctionInfo *FI = getFunctionInfo(F))
792 Known = FI->getModRefInfoForGlobal(*GV);
793
794 if (Known == MRI_NoModRef)
795 return MRI_NoModRef; // No need to query other mod/ref analyses
796 return ModRefInfo(Known & AliasAnalysis::getModRefInfo(CS, Loc));
797 }
+0
-10
lib/Analysis/IPA/CMakeLists.txt less more
None add_llvm_library(LLVMipa
1 CallGraph.cpp
2 CallGraphSCCPass.cpp
3 CallPrinter.cpp
4 GlobalsModRef.cpp
5 IPA.cpp
6 InlineCost.cpp
7 )
8
9 add_dependencies(LLVMipa intrinsics_gen)
+0
-309
lib/Analysis/IPA/CallGraph.cpp less more
None //===- CallGraph.cpp - Build a Module's call graph ------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Analysis/CallGraph.h"
10 #include "llvm/IR/CallSite.h"
11 #include "llvm/IR/Instructions.h"
12 #include "llvm/IR/IntrinsicInst.h"
13 #include "llvm/IR/Module.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/raw_ostream.h"
16 using namespace llvm;
17
18 //===----------------------------------------------------------------------===//
19 // Implementations of the CallGraph class methods.
20 //
21
22 CallGraph::CallGraph(Module &M)
23 : M(M), Root(nullptr), ExternalCallingNode(getOrInsertFunction(nullptr)),
24 CallsExternalNode(llvm::make_unique(nullptr)) {
25 // Add every function to the call graph.
26 for (Function &F : M)
27 addToCallGraph(&F);
28
29 // If we didn't find a main function, use the external call graph node
30 if (!Root)
31 Root = ExternalCallingNode;
32 }
33
34 CallGraph::CallGraph(CallGraph &&Arg)
35 : M(Arg.M), FunctionMap(std::move(Arg.FunctionMap)), Root(Arg.Root),
36 ExternalCallingNode(Arg.ExternalCallingNode),
37 CallsExternalNode(std::move(Arg.CallsExternalNode)) {
38 Arg.FunctionMap.clear();
39 Arg.Root = nullptr;
40 Arg.ExternalCallingNode = nullptr;
41 }
42
43 CallGraph::~CallGraph() {
44 // CallsExternalNode is not in the function map, delete it explicitly.
45 if (CallsExternalNode)
46 CallsExternalNode->allReferencesDropped();
47
48 // Reset all node's use counts to zero before deleting them to prevent an
49 // assertion from firing.
50 #ifndef NDEBUG
51 for (auto &I : FunctionMap)
52 I.second->allReferencesDropped();
53 #endif
54 }
55
56 void CallGraph::addToCallGraph(Function *F) {
57 CallGraphNode *Node = getOrInsertFunction(F);
58
59 // If this function has external linkage, anything could call it.
60 if (!F->hasLocalLinkage()) {
61 ExternalCallingNode->addCalledFunction(CallSite(), Node);
62
63 // Found the entry point?
64 if (F->getName() == "main") {
65 if (Root) // Found multiple external mains? Don't pick one.
66 Root = ExternalCallingNode;
67 else
68 Root = Node; // Found a main, keep track of it!
69 }
70 }
71
72 // If this function has its address taken, anything could call it.
73 if (F->hasAddressTaken())
74 ExternalCallingNode->addCalledFunction(CallSite(), Node);
75
76 // If this function is not defined in this translation unit, it could call
77 // anything.
78 if (F->isDeclaration() && !F->isIntrinsic())
79 Node->addCalledFunction(CallSite(), CallsExternalNode.get());
80
81 // Look for calls by this function.
82 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
83 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE;
84 ++II) {
85 CallSite CS(cast(II));
86 if (CS) {
87 const Function *Callee = CS.getCalledFunction();
88 if (!Callee || !Intrinsic::isLeaf(Callee->getIntrinsicID()))
89 // Indirect calls of intrinsics are not allowed so no need to check.
90 // We can be more precise here by using TargetArg returned by
91 // Intrinsic::isLeaf.
92 Node->addCalledFunction(CS, CallsExternalNode.get());
93 else if (!Callee->isIntrinsic())
94 Node->addCalledFunction(CS, getOrInsertFunction(Callee));
95 }
96 }
97 }
98
99 void CallGraph::print(raw_ostream &OS) const {
100 OS << "CallGraph Root is: ";
101 if (Function *F = Root->getFunction())
102 OS << F->getName() << "\n";
103 else {
104 OS << "<>\n";
105 }
106
107 // Print in a deterministic order by sorting CallGraphNodes by name. We do
108 // this here to avoid slowing down the non-printing fast path.
109
110 SmallVector Nodes;
111 Nodes.reserve(FunctionMap.size());
112
113 for (auto I = begin(), E = end(); I != E; ++I)
114 Nodes.push_back(I->second.get());
115
116 std::sort(Nodes.begin(), Nodes.end(),
117 [](CallGraphNode *LHS, CallGraphNode *RHS) {
118 if (Function *LF = LHS->getFunction())
119 if (Function *RF = RHS->getFunction())
120 return LF->getName() < RF->getName();
121
122 return RHS->getFunction() != nullptr;
123 });
124
125 for (CallGraphNode *CN : Nodes)
126 CN->print(OS);
127 }
128
129 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
130 void CallGraph::dump() const { print(dbgs()); }
131 #endif
132
133 // removeFunctionFromModule - Unlink the function from this module, returning
134 // it. Because this removes the function from the module, the call graph node
135 // is destroyed. This is only valid if the function does not call any other
136 // functions (ie, there are no edges in it's CGN). The easiest way to do this
137 // is to dropAllReferences before calling this.
138 //
139 Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
140 assert(CGN->empty() && "Cannot remove function from call "
141 "graph if it references other functions!");
142 Function *F = CGN->getFunction(); // Get the function for the call graph node
143 FunctionMap.erase(F); // Remove the call graph node from the map
144
145 M.getFunctionList().remove(F);
146 return F;
147 }
148
149 /// spliceFunction - Replace the function represented by this node by another.
150 /// This does not rescan the body of the function, so it is suitable when
151 /// splicing the body of the old function to the new while also updating all
152 /// callers from old to new.
153 ///
154 void CallGraph::spliceFunction(const Function *From, const Function *To) {
155 assert(FunctionMap.count(From) && "No CallGraphNode for function!");
156 assert(!FunctionMap.count(To) &&
157 "Pointing CallGraphNode at a function that already exists");
158 FunctionMapTy::iterator I = FunctionMap.find(From);
159 I->second->F = const_cast(To);
160 FunctionMap[To] = std::move(I->second);
161 FunctionMap.erase(I);
162 }
163
164 // getOrInsertFunction - This method is identical to calling operator[], but
165 // it will insert a new CallGraphNode for the specified function if one does
166 // not already exist.
167 CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
168 auto &CGN = FunctionMap[F];
169 if (CGN)
170 return CGN.get();
171
172 assert((!F || F->getParent() == &M) && "Function not in current module!");
173 CGN = llvm::make_unique(const_cast(F));
174 return CGN.get();
175 }
176
177 //===----------------------------------------------------------------------===//
178 // Implementations of the CallGraphNode class methods.
179 //
180
181 void CallGraphNode::print(raw_ostream &OS) const {
182 if (Function *F = getFunction())
183 OS << "Call graph node for function: '" << F->getName() << "'";
184 else
185 OS << "Call graph node <>";
186
187 OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n';
188
189 for (const_iterator I = begin(), E = end(); I != E; ++I) {
190 OS << " CS<" << I->first << "> calls ";
191 if (Function *FI = I->second->getFunction())
192 OS << "function '" << FI->getName() <<"'\n";
193 else
194 OS << "external node\n";
195 }
196 OS << '\n';
197 }
198
199 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
200 void CallGraphNode::dump() const { print(dbgs()); }
201 #endif
202
203 /// removeCallEdgeFor - This method removes the edge in the node for the
204 /// specified call site. Note that this method takes linear time, so it
205 /// should be used sparingly.
206 void CallGraphNode::removeCallEdgeFor(CallSite CS) {
207 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
208 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
209 if (I->first == CS.getInstruction()) {
210 I->second->DropRef();
211 *I = CalledFunctions.back();
212 CalledFunctions.pop_back();
213 return;
214 }
215 }
216 }
217
218 // removeAnyCallEdgeTo - This method removes any call edges from this node to
219 // the specified callee function. This takes more time to execute than
220 // removeCallEdgeTo, so it should not be used unless necessary.
221 void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
222 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
223 if (CalledFunctions[i].second == Callee) {
224 Callee->DropRef();
225 CalledFunctions[i] = CalledFunctions.back();
226 CalledFunctions.pop_back();
227 --i; --e;
228 }
229 }
230
231 /// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
232 /// from this node to the specified callee function.
233 void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) {
234 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
235 assert(I != CalledFunctions.end() && "Cannot find callee to remove!");
236 CallRecord &CR = *I;
237 if (CR.second == Callee && CR.first == nullptr) {
238 Callee->DropRef();
239 *I = CalledFunctions.back();
240 CalledFunctions.pop_back();
241 return;
242 }
243 }
244 }
245
246 /// replaceCallEdge - This method replaces the edge in the node for the
247 /// specified call site with a new one. Note that this method takes linear
248 /// time, so it should be used sparingly.
249 void CallGraphNode::replaceCallEdge(CallSite CS,
250 CallSite NewCS, CallGraphNode *NewNode){
251 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
252 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
253 if (I->first == CS.getInstruction()) {
254 I->second->DropRef();
255 I->first = NewCS.getInstruction();
256 I->second = NewNode;
257 NewNode->AddRef();
258 return;
259 }
260 }
261 }
262
263 //===----------------------------------------------------------------------===//
264 // Out-of-line definitions of CallGraphAnalysis class members.
265 //
266
267 char CallGraphAnalysis::PassID;
268
269 //===----------------------------------------------------------------------===//
270 // Implementations of the CallGraphWrapperPass class methods.
271 //
272
273 CallGraphWrapperPass::CallGraphWrapperPass() : ModulePass(ID) {
274 initializeCallGraphWrapperPassPass(*PassRegistry::getPassRegistry());
275 }
276
277 CallGraphWrapperPass::~CallGraphWrapperPass() {}
278
279 void CallGraphWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
280 AU.setPreservesAll();
281 }
282
283 bool CallGraphWrapperPass::runOnModule(Module &M) {
284 // All the real work is done in the constructor for the CallGraph.
285 G.reset(new CallGraph(M));
286 return false;
287 }
288
289 INITIALIZE_PASS(CallGraphWrapperPass, "basiccg", "CallGraph Construction",
290 false, true)
291
292 char CallGraphWrapperPass::ID = 0;
293
294 void CallGraphWrapperPass::releaseMemory() { G.reset(); }
295
296 void CallGraphWrapperPass::print(raw_ostream &OS, const Module *) const {
297 if (!G) {
298 OS << "No call graph has been built!\n";
299 return;
300 }
301
302 // Just delegate.
303 G->print(OS);
304 }
305
306 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
307 void CallGraphWrapperPass::dump() const { print(dbgs(), nullptr); }
308 #endif
+0
-632
lib/Analysis/IPA/CallGraphSCCPass.cpp less more
None //===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the CallGraphSCCPass class, which is used for passes
10 // which are implemented as bottom-up traversals on the call graph. Because
11 // there may be cycles in the call graph, passes of this type operate on the
12 // call-graph in SCC order: that is, they process function bottom-up, except for
13 // recursive functions, which they process all at once.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Analysis/CallGraphSCCPass.h"
18 #include "llvm/ADT/SCCIterator.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Analysis/CallGraph.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/IntrinsicInst.h"
23 #include "llvm/IR/LLVMContext.h"
24 #include "llvm/IR/LegacyPassManagers.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/Timer.h"
28 #include "llvm/Support/raw_ostream.h"
29 using namespace llvm;
30
31 #define DEBUG_TYPE "cgscc-passmgr"
32
33 static cl::opt
34 MaxIterations("max-cg-scc-iterations", cl::ReallyHidden, cl::init(4));
35
36 STATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC");
37
38 //===----------------------------------------------------------------------===//
39 // CGPassManager
40 //
41 /// CGPassManager manages FPPassManagers and CallGraphSCCPasses.
42
43 namespace {
44
45 class CGPassManager : public ModulePass, public PMDataManager {
46 public:
47 static char ID;
48 explicit CGPassManager()
49 : ModulePass(ID), PMDataManager() { }
50
51 /// Execute all of the passes scheduled for execution. Keep track of
52 /// whether any of the passes modifies the module, and if so, return true.
53 bool runOnModule(Module &M) override;
54
55 using ModulePass::doInitialization;
56 using ModulePass::doFinalization;
57
58 bool doInitialization(CallGraph &CG);
59 bool doFinalization(CallGraph &CG);
60
61 /// Pass Manager itself does not invalidate any analysis info.
62 void getAnalysisUsage(AnalysisUsage &Info) const override {
63 // CGPassManager walks SCC and it needs CallGraph.
64 Info.addRequired();
65 Info.setPreservesAll();
66 }
67
68 const char *getPassName() const override {
69 return "CallGraph Pass Manager";
70 }
71
72 PMDataManager *getAsPMDataManager() override { return this; }
73 Pass *getAsPass() override { return this; }
74
75 // Print passes managed by this manager
76 void dumpPassStructure(unsigned Offset) override {
77 errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n";
78 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
79 Pass *P = getContainedPass(Index);
80 P->dumpPassStructure(Offset + 1);
81 dumpLastUses(P, Offset+1);
82 }
83 }
84
85 Pass *getContainedPass(unsigned N) {
86 assert(N < PassVector.size() && "Pass number out of range!");
87 return static_cast(PassVector[N]);
88 }
89
90 PassManagerType getPassManagerType() const override {
91 return PMT_CallGraphPassManager;
92 }
93
94 private:
95 bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
96 bool &DevirtualizedCall);
97
98 bool RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
99 CallGraph &CG, bool &CallGraphUpToDate,
100 bool &DevirtualizedCall);
101 bool RefreshCallGraph(CallGraphSCC &CurSCC, CallGraph &CG,
102 bool IsCheckingMode);
103 };
104
105 } // end anonymous namespace.
106
107 char CGPassManager::ID = 0;
108
109
110 bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
111 CallGraph &CG, bool &CallGraphUpToDate,
112 bool &DevirtualizedCall) {
113 bool Changed = false;
114 PMDataManager *PM = P->getAsPMDataManager();
115
116 if (!PM) {
117 CallGraphSCCPass *CGSP = (CallGraphSCCPass*)P;
118 if (!CallGraphUpToDate) {
119 DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
120 CallGraphUpToDate = true;
121 }
122
123 {
124 TimeRegion PassTimer(getPassTimer(CGSP));
125 Changed = CGSP->runOnSCC(CurSCC);
126 }
127
128 // After the CGSCCPass is done, when assertions are enabled, use
129 // RefreshCallGraph to verify that the callgraph was correctly updated.
130 #ifndef NDEBUG
131 if (Changed)
132 RefreshCallGraph(CurSCC, CG, true);
133 #endif
134
135 return Changed;
136 }
137
138
139 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
140 "Invalid CGPassManager member");
141 FPPassManager *FPP = (FPPassManager*)P;
142
143 // Run pass P on all functions in the current SCC.
144 for (CallGraphNode *CGN : CurSCC) {
145 if (Function *F = CGN->getFunction()) {
146 dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
147 {
148 TimeRegion PassTimer(getPassTimer(FPP));
149 Changed |= FPP->runOnFunction(*F);
150 }
151 F->getContext().yield();
152 }
153 }
154
155 // The function pass(es) modified the IR, they may have clobbered the
156 // callgraph.
157 if (Changed && CallGraphUpToDate) {
158 DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: "
159 << P->getPassName() << '\n');
160 CallGraphUpToDate = false;
161 }
162 return Changed;
163 }
164
165
166 /// Scan the functions in the specified CFG and resync the
167 /// callgraph with the call sites found in it. This is used after
168 /// FunctionPasses have potentially munged the callgraph, and can be used after
169 /// CallGraphSCC passes to verify that they correctly updated the callgraph.
170 ///
171 /// This function returns true if it devirtualized an existing function call,
172 /// meaning it turned an indirect call into a direct call. This happens when
173 /// a function pass like GVN optimizes away stuff feeding the indirect call.
174 /// This never happens in checking mode.
175 ///
176 bool CGPassManager::RefreshCallGraph(CallGraphSCC &CurSCC,
177 CallGraph &CG, bool CheckingMode) {
178 DenseMap CallSites;
179
180 DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size()
181 << " nodes:\n";
182 for (CallGraphNode *CGN : CurSCC)
183 CGN->dump();
184 );
185
186 bool MadeChange = false;
187 bool DevirtualizedCall = false;
188
189 // Scan all functions in the SCC.
190 unsigned FunctionNo = 0;
191 for (CallGraphSCC::iterator SCCIdx = CurSCC.begin(), E = CurSCC.end();
192 SCCIdx != E; ++SCCIdx, ++FunctionNo) {
193 CallGraphNode *CGN = *SCCIdx;
194 Function *F = CGN->getFunction();
195 if (!F || F->isDeclaration()) continue;
196
197 // Walk the function body looking for call sites. Sync up the call sites in
198 // CGN with those actually in the function.
199
200 // Keep track of the number of direct and indirect calls that were
201 // invalidated and removed.
202 unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0;
203
204 // Get the set of call sites currently in the function.
205 for (CallGraphNode::iterator I = CGN->begin(), E = CGN->end(); I != E; ) {
206 // If this call site is null, then the function pass deleted the call
207 // entirely and the WeakVH nulled it out.
208 if (!I->first ||
209 // If we've already seen this call site, then the FunctionPass RAUW'd
210 // one call with another, which resulted in two "uses" in the edge
211 // list of the same call.
212 CallSites.count(I->first) ||
213
214 // If the call edge is not from a call or invoke, or it is a
215 // instrinsic call, then the function pass RAUW'd a call with
216 // another value. This can happen when constant folding happens
217 // of well known functions etc.
218 !CallSite(I->first) ||
219 (CallSite(I->first).getCalledFunction() &&
220 CallSite(I->first).getCalledFunction()->isIntrinsic() &&
221 Intrinsic::isLeaf(
222 CallSite(I->first).getCalledFunction()->getIntrinsicID()))) {
223 assert(!CheckingMode &&
224 "CallGraphSCCPass did not update the CallGraph correctly!");
225
226 // If this was an indirect call site, count it.
227 if (!I->second->getFunction())
228 ++NumIndirectRemoved;
229 else
230 ++NumDirectRemoved;
231
232 // Just remove the edge from the set of callees, keep track of whether
233 // I points to the last element of the vector.
234 bool WasLast = I + 1 == E;
235 CGN->removeCallEdge(I);
236
237 // If I pointed to the last element of the vector, we have to bail out:
238 // iterator checking rejects comparisons of the resultant pointer with
239 // end.
240 if (WasLast)
241 break;
242 E = CGN->end();
243 continue;
244 }
245
246 assert(!CallSites.count(I->first) &&
247 "Call site occurs in node multiple times");
248
249 CallSite CS(I->first);
250 if (CS) {
251 Function *Callee = CS.getCalledFunction();
252 // Ignore intrinsics because they're not really function calls.
253 if (!Callee || !(Callee->isIntrinsic()))
254 CallSites.insert(std::make_pair(I->first, I->second));
255 }
256 ++I;
257 }
258
259 // Loop over all of the instructions in the function, getting the callsites.
260 // Keep track of the number of direct/indirect calls added.
261 unsigned NumDirectAdded = 0, NumIndirectAdded = 0;
262
263 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
264 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
265 CallSite CS(cast(I));
266 if (!CS) continue;
267 Function *Callee = CS.getCalledFunction();
268 if (Callee && Callee->isIntrinsic()) continue;
269
270 // If this call site already existed in the callgraph, just verify it
271 // matches up to expectations and remove it from CallSites.
272 DenseMap::iterator ExistingIt =
273 CallSites.find(CS.getInstruction());
274 if (ExistingIt != CallSites.end()) {
275 CallGraphNode *ExistingNode = ExistingIt->second;
276
277 // Remove from CallSites since we have now seen it.
278 CallSites.erase(ExistingIt);
279
280 // Verify that the callee is right.
281 if (ExistingNode->getFunction() == CS.getCalledFunction())
282 continue;
283
284 // If we are in checking mode, we are not allowed to actually mutate
285 // the callgraph. If this is a case where we can infer that the
286 // callgraph is less precise than it could be (e.g. an indirect call
287 // site could be turned direct), don't reject it in checking mode, and
288 // don't tweak it to be more precise.
289 if (CheckingMode && CS.getCalledFunction() &&
290 ExistingNode->getFunction() == nullptr)
291 continue;
292
293 assert(!CheckingMode &&
294 "CallGraphSCCPass did not update the CallGraph correctly!");
295
296 // If not, we either went from a direct call to indirect, indirect to
297 // direct, or direct to different direct.
298 CallGraphNode *CalleeNode;
299 if (Function *Callee = CS.getCalledFunction()) {
300 CalleeNode = CG.getOrInsertFunction(Callee);
301 // Keep track of whether we turned an indirect call into a direct
302 // one.
303 if (!ExistingNode->getFunction()) {
304 DevirtualizedCall = true;
305 DEBUG(dbgs() << " CGSCCPASSMGR: Devirtualized call to '"
306 << Callee->getName() << "'\n");
307 }
308 } else {
309 CalleeNode = CG.getCallsExternalNode();
310 }
311
312 // Update the edge target in CGN.
313 CGN->replaceCallEdge(CS, CS, CalleeNode);
314 MadeChange = true;
315 continue;
316 }
317
318 assert(!CheckingMode &&
319 "CallGraphSCCPass did not update the CallGraph correctly!");
320
321 // If the call site didn't exist in the CGN yet, add it.
322 CallGraphNode *CalleeNode;
323 if (Function *Callee = CS.getCalledFunction()) {
324 CalleeNode = CG.getOrInsertFunction(Callee);
325 ++NumDirectAdded;
326 } else {
327 CalleeNode = CG.getCallsExternalNode();
328 ++NumIndirectAdded;
329 }
330
331 CGN->addCalledFunction(CS, CalleeNode);
332 MadeChange = true;
333 }
334
335 // We scanned the old callgraph node, removing invalidated call sites and
336 // then added back newly found call sites. One thing that can happen is
337 // that an old indirect call site was deleted and replaced with a new direct
338 // call. In this case, we have devirtualized a call, and CGSCCPM would like
339 // to iteratively optimize the new code. Unfortunately, we don't really
340 // have a great way to detect when this happens. As an approximation, we
341 // just look at whether the number of indirect calls is reduced and the
342 // number of direct calls is increased. There are tons of ways to fool this
343 // (e.g. DCE'ing an indirect call and duplicating an unrelated block with a
344 // direct call) but this is close enough.
345 if (NumIndirectRemoved > NumIndirectAdded &&
346 NumDirectRemoved < NumDirectAdded)
347 DevirtualizedCall = true;
348
349 // After scanning this function, if we still have entries in callsites, then
350 // they are dangling pointers. WeakVH should save us for this, so abort if
351 // this happens.
352 assert(CallSites.empty() && "Dangling pointers found in call sites map");
353
354 // Periodically do an explicit clear to remove tombstones when processing
355 // large scc's.
356 if ((FunctionNo & 15) == 15)
357 CallSites.clear();
358 }
359
360 DEBUG(if (MadeChange) {
361 dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n";
362 for (CallGraphNode *CGN : CurSCC)
363 CGN->dump();
364 if (DevirtualizedCall)
365 dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n";
366
367 } else {
368 dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n";
369 }
370 );
371 (void)MadeChange;
372
373 return DevirtualizedCall;
374 }
375
376 /// Execute the body of the entire pass manager on the specified SCC.
377 /// This keeps track of whether a function pass devirtualizes
378 /// any calls and returns it in DevirtualizedCall.
379 bool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
380 bool &DevirtualizedCall) {
381 bool Changed = false;
382
383 // Keep track of whether the callgraph is known to be up-to-date or not.
384 // The CGSSC pass manager runs two types of passes:
385 // CallGraphSCC Passes and other random function passes. Because other
386 // random function passes are not CallGraph aware, they may clobber the
387 // call graph by introducing new calls or deleting other ones. This flag
388 // is set to false when we run a function pass so that we know to clean up
389 // the callgraph when we need to run a CGSCCPass again.
390 bool CallGraphUpToDate = true;
391
392 // Run all passes on current SCC.
393 for (unsigned PassNo = 0, e = getNumContainedPasses();
394 PassNo != e; ++PassNo) {
395 Pass *P = getContainedPass(PassNo);
396
397 // If we're in -debug-pass=Executions mode, construct the SCC node list,
398 // otherwise avoid constructing this string as it is expensive.
399 if (isPassDebuggingExecutionsOrMore()) {
400 std::string Functions;
401 #ifndef NDEBUG
402 raw_string_ostream OS(Functions);
403 for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
404 I != E; ++I) {
405 if (I != CurSCC.begin()) OS << ", ";
406 (*I)->print(OS);
407 }
408 OS.flush();
409 #endif
410 dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions);
411 }
412 dumpRequiredSet(P);
413
414 initializeAnalysisImpl(P);
415
416 // Actually run this pass on the current SCC.
417 Changed |= RunPassOnSCC(P, CurSCC, CG,
418 CallGraphUpToDate, DevirtualizedCall);
419
420 if (Changed)
421 dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
422 dumpPreservedSet(P);
423
424 verifyPreservedAnalysis(P);
425 removeNotPreservedAnalysis(P);
426 recordAvailableAnalysis(P);
427 removeDeadPasses(P, "", ON_CG_MSG);
428 }
429
430 // If the callgraph was left out of date (because the last pass run was a
431 // functionpass), refresh it before we move on to the next SCC.
432 if (!CallGraphUpToDate)
433 DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
434 return Changed;
435 }
436
437 /// Execute all of the passes scheduled for execution. Keep track of
438 /// whether any of the passes modifies the module, and if so, return true.
439 bool CGPassManager::runOnModule(Module &M) {
440 CallGraph &CG = getAnalysis().getCallGraph();
441 bool Changed = doInitialization(CG);
442
443 // Walk the callgraph in bottom-up SCC order.
444 scc_iterator CGI = scc_begin(&CG);
445
446 CallGraphSCC CurSCC(&CGI);
447 while (!CGI.isAtEnd()) {
448 // Copy the current SCC and increment past it so that the pass can hack
449 // on the SCC if it wants to without invalidating our iterator.
450 const std::vector &NodeVec = *CGI;
451 CurSCC.initialize(NodeVec.data(), NodeVec.data() + NodeVec.size());
452 ++CGI;
453
454 // At the top level, we run all the passes in this pass manager on the
455 // functions in this SCC. However, we support iterative compilation in the
456 // case where a function pass devirtualizes a call to a function. For
457 // example, it is very common for a function pass (often GVN or instcombine)
458 // to eliminate the addressing that feeds into a call. With that improved
459 // information, we would like the call to be an inline candidate, infer
460 // mod-ref information etc.
461 //
462 // Because of this, we allow iteration up to a specified iteration count.
463 // This only happens in the case of a devirtualized call, so we only burn
464 // compile time in the case that we're making progress. We also have a hard
465 // iteration count limit in case there is crazy code.
466 unsigned Iteration = 0;
467 bool DevirtualizedCall = false;
468 do {
469 DEBUG(if (Iteration)
470 dbgs() << " SCCPASSMGR: Re-visiting SCC, iteration #"
471 << Iteration << '\n');
472 DevirtualizedCall = false;
473 Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall);
474 } while (Iteration++ < MaxIterations && DevirtualizedCall);
475
476 if (DevirtualizedCall)
477 DEBUG(dbgs() << " CGSCCPASSMGR: Stopped iteration after " << Iteration
478 << " times, due to -max-cg-scc-iterations\n");
479
480 if (Iteration > MaxSCCIterations)
481 MaxSCCIterations = Iteration;
482
483 }
484 Changed |= doFinalization(CG);
485 return Changed;
486 }
487
488
489 /// Initialize CG
490 bool CGPassManager::doInitialization(CallGraph &CG) {
491 bool Changed = false;
492 for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
493 if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
494 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
495 "Invalid CGPassManager member");
496 Changed |= ((FPPassManager*)PM)->doInitialization(CG.getModule());
497 } else {
498 Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG);
499 }
500 }
501 return Changed;
502 }
503
504 /// Finalize CG
505 bool CGPassManager::doFinalization(CallGraph &CG) {
506 bool Changed = false;
507 for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
508 if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
509 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
510 "Invalid CGPassManager member");
511 Changed |= ((FPPassManager*)PM)->doFinalization(CG.getModule());
512 } else {
513 Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG);
514 }
515 }
516 return Changed;
517 }
518
519 //===----------------------------------------------------------------------===//
520 // CallGraphSCC Implementation
521 //===----------------------------------------------------------------------===//
522
523 /// This informs the SCC and the pass manager that the specified
524 /// Old node has been deleted, and New is to be used in its place.
525 void CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) {
526 assert(Old != New && "Should not replace node with self");
527 for (unsigned i = 0; ; ++i) {
528 assert(i != Nodes.size() && "Node not in SCC");
529 if (Nodes[i] != Old) continue;
530 Nodes[i] = New;
531 break;
532 }
533
534 // Update the active scc_iterator so that it doesn't contain dangling
535 // pointers to the old CallGraphNode.
536 scc_iterator *CGI = (scc_iterator*)Context;
537 CGI->ReplaceNode(Old, New);
538 }
539
540
541 //===----------------------------------------------------------------------===//
542 // CallGraphSCCPass Implementation
543 //===----------------------------------------------------------------------===//
544
545 /// Assign pass manager to manage this pass.
546 void CallGraphSCCPass::assignPassManager(PMStack &PMS,
547 PassManagerType PreferredType) {
548 // Find CGPassManager
549 while (!PMS.empty() &&
550 PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
551 PMS.pop();
552
553 assert(!PMS.empty() && "Unable to handle Call Graph Pass");
554 CGPassManager *CGP;
555
556 if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager)
557 CGP = (CGPassManager*)PMS.top();
558 else {
559 // Create new Call Graph SCC Pass Manager if it does not exist.
560 assert(!PMS.empty() && "Unable to create Call Graph Pass Manager");
561 PMDataManager *PMD = PMS.top();
562
563 // [1] Create new Call Graph Pass Manager
564 CGP = new CGPassManager();
565
566 // [2] Set up new manager's top level manager
567 PMTopLevelManager *TPM = PMD->getTopLevelManager();
568 TPM->addIndirectPassManager(CGP);
569
570 // [3] Assign manager to manage this new manager. This may create
571 // and push new managers into PMS
572 Pass *P = CGP;
573 TPM->schedulePass(P);
574
575 // [4] Push new manager into PMS
576 PMS.push(CGP);
577 }
578
579 CGP->add(this);
580 }
581
582 /// For this class, we declare that we require and preserve the call graph.
583 /// If the derived class implements this method, it should
584 /// always explicitly call the implementation here.
585 void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
586 AU.addRequired();
587 AU.addPreserved();
588 }
589
590
591 //===----------------------------------------------------------------------===//
592 // PrintCallGraphPass Implementation
593 //===----------------------------------------------------------------------===//
594
595 namespace {
596 /// PrintCallGraphPass - Print a Module corresponding to a call graph.
597 ///
598 class PrintCallGraphPass : public CallGraphSCCPass {
599 std::string Banner;
600 raw_ostream &Out; // raw_ostream to print on.
601
602 public:
603 static char ID;
604 PrintCallGraphPass(const std::string &B, raw_ostream &o)
605 : CallGraphSCCPass(ID), Banner(B), Out(o) {}
606
607 void getAnalysisUsage(AnalysisUsage &AU) const override {
608 AU.setPreservesAll();
609 }
610
611 bool runOnSCC(CallGraphSCC &SCC) override {
612 Out << Banner;
613 for (CallGraphNode *CGN : SCC) {
614 if (CGN->getFunction())
615 CGN->getFunction()->print(Out);
616 else
617 Out << "\nPrinting Function\n";
618 }
619 return false;
620 }
621 };
622
623 } // end anonymous namespace.
624
625 char PrintCallGraphPass::ID = 0;
626
627 Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &O,
628 const std::string &Banner) const {
629 return new PrintCallGraphPass(Banner, O);
630 }
631
+0
-92
lib/Analysis/IPA/CallPrinter.cpp less more
None //===- CallPrinter.cpp - DOT printer for call graph -----------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines '-dot-callgraph', which emit a callgraph..dot
10 // containing the call graph of a module.
11 //
12 // There is also a pass available to directly call dotty ('-view-callgraph').
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Analysis/CallGraph.h"
17 #include "llvm/Analysis/CallPrinter.h"
18 #include "llvm/Analysis/DOTGraphTraitsPass.h"
19
20 using namespace llvm;
21
22 namespace llvm {
23
24 template <> struct DOTGraphTraits : public DefaultDOTGraphTraits {
25 DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
26
27 static std::string getGraphName(CallGraph *Graph) { return "Call graph"; }
28
29 std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
30 if (Function *Func = Node->getFunction())
31 return Func->getName();
32
33 return "external node";
34 }
35 };
36
37 struct AnalysisCallGraphWrapperPassTraits {
38 static CallGraph *getGraph(CallGraphWrapperPass *P) {
39 return &P->getCallGraph();
40 }
41 };
42
43 } // end llvm namespace
44
45 namespace {
46
47 struct CallGraphViewer
48 : public DOTGraphTraitsModuleViewer
49 AnalysisCallGraphWrapperPassTraits> {
50 static char ID;
51
52 CallGraphViewer()
53 : DOTGraphTraitsModuleViewer
54 AnalysisCallGraphWrapperPassTraits>(
55 "callgraph", ID) {
56 initializeCallGraphViewerPass(*PassRegistry::getPassRegistry());
57 }
58 };
59
60 struct CallGraphPrinter : public DOTGraphTraitsModulePrinter<
61 CallGraphWrapperPass, true, CallGraph *,
62 AnalysisCallGraphWrapperPassTraits> {
63 static char ID;
64
65 CallGraphPrinter()
66 : DOTGraphTraitsModulePrinter
67 AnalysisCallGraphWrapperPassTraits>(
68 "callgraph", ID) {
69 initializeCallGraphPrinterPass(*PassRegistry::getPassRegistry());
70 }
71 };
72
73 } // end anonymous namespace
74
75 char CallGraphViewer::ID = 0;
76 INITIALIZE_PASS(CallGraphViewer, "view-callgraph", "View call graph", false,
77 false)
78
79 char CallGraphPrinter::ID = 0;
80 INITIALIZE_PASS(CallGraphPrinter, "dot-callgraph",
81 "Print call graph to 'dot' file", false, false)
82
83 // Create methods available outside of this file, to use them
84 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
85 // the link time optimization.
86
87 ModulePass *llvm::createCallGraphViewerPass() { return new CallGraphViewer(); }
88
89 ModulePass *llvm::createCallGraphPrinterPass() {
90 return new CallGraphPrinter();
91 }
+0
-798
lib/Analysis/IPA/GlobalsModRef.cpp less more
None //===- GlobalsModRef.cpp - Simple Mod/Ref Analysis for Globals ------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This simple pass provides alias and mod/ref information for global values
10 // that do not have their address taken, and keeps track of whether functions
11 // read or write memory (are "pure"). For this simple (but very common) case,
12 // we can provide pretty accurate and useful information.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Analysis/GlobalsModRef.h"
17 #include "llvm/ADT/SCCIterator.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Analysis/MemoryBuiltins.h"
21 #include "llvm/Analysis/ValueTracking.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/InstIterator.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Support/CommandLine.h"
29 using namespace llvm;
30
31 #define DEBUG_TYPE "globalsmodref-aa"
32
33 STATISTIC(NumNonAddrTakenGlobalVars,
34 "Number of global vars without address taken");
35 STATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken");
36 STATISTIC(NumNoMemFunctions, "Number of functions that do not access memory");
37 STATISTIC(NumReadMemFunctions, "Number of functions that only read memory");
38 STATISTIC(NumIndirectGlobalVars, "Number of indirect global objects");
39
40 // An option to enable unsafe alias results from the GlobalsModRef analysis.
41 // When enabled, GlobalsModRef will provide no-alias results which in extremely
42 // rare cases may not be conservatively correct. In particular, in the face of
43 // transforms which cause assymetry between how effective GetUnderlyingObject
44 // is for two pointers, it may produce incorrect results.
45 //
46 // These unsafe results have been returned by GMR for many years without
47 // causing significant issues in the wild and so we provide a mechanism to
48 // re-enable them for users of LLVM that have a particular performance
49 // sensitivity and no known issues. The option also makes it easy to evaluate
50 // the performance impact of these results.
51 static cl::opt EnableUnsafeGlobalsModRefAliasResults(
52 "enable-unsafe-globalsmodref-alias-results", cl::init(false), cl::Hidden);
53
54 /// The mod/ref information collected for a particular function.
55 ///
56 /// We collect information about mod/ref behavior of a function here, both in
57 /// general and as pertains to specific globals. We only have this detailed
58 /// information when we know *something* useful about the behavior. If we
59 /// saturate to fully general mod/ref, we remove the info for the function.
60 class GlobalsModRef::FunctionInfo {
61 typedef SmallDenseMap GlobalInfoMapType;
62
63 /// Build a wrapper struct that has 8-byte alignment. All heap allocations
64 /// should provide this much alignment at least, but this makes it clear we
65 /// specifically rely on this amount of alignment.
66 struct LLVM_ALIGNAS(8) AlignedMap {
67 AlignedMap() {}
68 AlignedMap(const AlignedMap &Arg) : Map(Arg.Map) {}
69 GlobalInfoMapType Map;
70 };
71
72 /// Pointer traits for our aligned map.
73 struct AlignedMapPointerTraits {
74 static inline void *getAsVoidPointer(AlignedMap *P) { return P; }
75 static inline AlignedMap *getFromVoidPointer(void *P) {
76 return (AlignedMap *)P;
77 }
78 enum { NumLowBitsAvailable = 3 };
79 static_assert(AlignOf::Alignment >= (1 << NumLowBitsAvailable),
80 "AlignedMap insufficiently aligned to have enough low bits.");
81 };
82
83 /// The bit that flags that this function may read any global. This is
84 /// chosen to mix together with ModRefInfo bits.
85 enum { MayReadAnyGlobal = 4 };
86
87 /// Checks to document the invariants of the bit packing here.
88 static_assert((MayReadAnyGlobal & MRI_ModRef) == 0,
89 "ModRef and the MayReadAnyGlobal flag bits overlap.");
90 static_assert(((MayReadAnyGlobal | MRI_ModRef) >>
91 AlignedMapPointerTraits::NumLowBitsAvailable) == 0,
92 "Insufficient low bits to store our flag and ModRef info.");
93
94 public:
95 FunctionInfo() : Info() {}
96 ~FunctionInfo() {
97 delete Info.getPointer();
98 }
99 // Spell out the copy ond move constructors and assignment operators to get
100 // deep copy semantics and correct move semantics in the face of the
101 // pointer-int pair.
102 FunctionInfo(const FunctionInfo &Arg)
103 : Info(nullptr, Arg.Info.getInt()) {
104 if (const auto *ArgPtr = Arg.Info.getPointer())
105 Info.setPointer(new AlignedMap(*ArgPtr));
106 }
107 FunctionInfo(FunctionInfo &&Arg)
108 : Info(Arg.Info.getPointer(), Arg.Info.getInt()) {
109 Arg.Info.setPointerAndInt(nullptr, 0);
110 }
111 FunctionInfo &operator=(const FunctionInfo &RHS) {
112 delete Info.getPointer();
113 Info.setPointerAndInt(nullptr, RHS.Info.getInt());
114 if (const auto *RHSPtr = RHS.Info.getPointer())
115 Info.setPointer(new AlignedMap(*RHSPtr));
116 return *this;
117 }
118 FunctionInfo &operator=(FunctionInfo &&RHS) {
119 delete Info.getPointer();
120 Info.setPointerAndInt(RHS.Info.getPointer(), RHS.Info.getInt());
121 RHS.Info.setPointerAndInt(nullptr, 0);
122 return *this;
123 }
124
125 /// Returns the \c ModRefInfo info for this function.
126 ModRefInfo getModRefInfo() const {
127 return ModRefInfo(Info.getInt() & MRI_ModRef);
128 }
129
130 /// Adds new \c ModRefInfo for this function to its state.
131 void addModRefInfo(ModRefInfo NewMRI) {
132 Info.setInt(Info.getInt() | NewMRI);
133 }
134
135 /// Returns whether this function may read any global variable, and we don't
136 /// know which global.
137 bool mayReadAnyGlobal() const { return Info.getInt() & MayReadAnyGlobal; }
138
139 /// Sets this function as potentially reading from any global.
140 void setMayReadAnyGlobal() { Info.setInt(Info.getInt() | MayReadAnyGlobal); }
141
142 /// Returns the \c ModRefInfo info for this function w.r.t. a particular
143 /// global, which may be more precise than the general information above.
144 ModRefInfo getModRefInfoForGlobal(const GlobalValue &GV) const {
145 ModRefInfo GlobalMRI = mayReadAnyGlobal() ? MRI_Ref : MRI_NoModRef;
146 if (AlignedMap *P = Info.getPointer()) {
147 auto I = P->Map.find(&GV);
148 if (I != P->Map.end())
149 GlobalMRI = ModRefInfo(GlobalMRI | I->second);
150 }
151 return GlobalMRI;
152 }
153
154 /// Add mod/ref info from another function into ours, saturating towards
155 /// MRI_ModRef.
156 void addFunctionInfo(const FunctionInfo &FI) {
157 addModRefInfo(FI.getModRefInfo());
158
159 if (FI.mayReadAnyGlobal())
160 setMayReadAnyGlobal();
161
162 if (AlignedMap *P = FI.Info.getPointer())
163 for (const auto &G : P->Map)
164 addModRefInfoForGlobal(*G.first, G.second);
165 }
166
167 void addModRefInfoForGlobal(const GlobalValue &GV, ModRefInfo NewMRI) {
168 AlignedMap *P = Info.getPointer();
169 if (!P) {
170 P = new AlignedMap();
171 Info.setPointer(P);
172 }
173 auto &GlobalMRI = P->Map[&GV];
174 GlobalMRI = ModRefInfo(GlobalMRI | NewMRI);
175 }
176
177 /// Clear a global's ModRef info. Should be used when a global is being
178 /// deleted.
179 void eraseModRefInfoForGlobal(const GlobalValue &GV) {
180 if (AlignedMap *P = Info.getPointer())
181 P->Map.erase(&GV);
182 }
183
184 private:
185 /// All of the information is encoded into a single pointer, with a three bit
186 /// integer in the low three bits. The high bit provides a flag for when this
187 /// function may read any global. The low two bits are the ModRefInfo. And
188 /// the pointer, when non-null, points to a map from GlobalValue to
189 /// ModRefInfo specific to that GlobalValue.
190 PointerIntPair Info;
191 };
192
193 void GlobalsModRef::DeletionCallbackHandle::deleted() {
194 Value *V = getValPtr();
195 if (auto *F = dyn_cast(V))
196 GMR.FunctionInfos.erase(F);
197
198 if (GlobalValue *GV = dyn_cast(V)) {
199 if (GMR.NonAddressTakenGlobals.erase(GV)) {
200 // This global might be an indirect global. If so, remove it and
201 // remove any AllocRelatedValues for it.
202 if (GMR.IndirectGlobals.erase(GV)) {
203 // Remove any entries in AllocsForIndirectGlobals for this global.
204 for (auto I = GMR.AllocsForIndirectGlobals.begin(),
205 E = GMR.AllocsForIndirectGlobals.end();
206 I != E; ++I)
207 if (I->second == GV)
208 GMR.AllocsForIndirectGlobals.erase(I);
209 }
210
211 // Scan the function info we have collected and remove this global
212 // from all of them.
213 for (auto &FIPair : GMR.FunctionInfos)
214 FIPair.second.eraseModRefInfoForGlobal(*GV);
215 }
216 }
217
218 // If this is an allocation related to an indirect global, remove it.
219 GMR.AllocsForIndirectGlobals.erase(V);
220
221 // And clear out the handle.
222 setValPtr(nullptr);
223 GMR.Handles.erase(I);
224 // This object is now destroyed!
225 }
226
227 char GlobalsModRef::ID = 0;
228 INITIALIZE_AG_PASS_BEGIN(GlobalsModRef, AliasAnalysis, "globalsmodref-aa",
229 "Simple mod/ref analysis for globals", false, true,
230 false)
231 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
232 INITIALIZE_AG_PASS_END(GlobalsModRef, AliasAnalysis, "globalsmodref-aa",
233 "Simple mod/ref analysis for globals", false, true,
234 false)
235
236 Pass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); }
237
238 GlobalsModRef::GlobalsModRef() : ModulePass(ID) {
239 initializeGlobalsModRefPass(*PassRegistry::getPassRegistry());
240 }
241
242 FunctionModRefBehavior GlobalsModRef::getModRefBehavior(const Function *F) {
243 FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
244
245 if (FunctionInfo *FI = getFunctionInfo(F)) {
246 if (FI->getModRefInfo() == MRI_NoModRef)
247 Min = FMRB_DoesNotAccessMemory;
248 else if ((FI->getModRefInfo() & MRI_Mod) == 0)
249 Min = FMRB_OnlyReadsMemory;
250 }
251
252 return FunctionModRefBehavior(AliasAnalysis::getModRefBehavior(F) & Min);
253 }
254
255 FunctionModRefBehavior GlobalsModRef::getModRefBehavior(ImmutableCallSite CS) {
256 FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
257
258 if (const Function *F = CS.getCalledFunction())
259 if (FunctionInfo *FI = getFunctionInfo(F)) {
260 if (FI->getModRefInfo() == MRI_NoModRef)
261 Min = FMRB_DoesNotAccessMemory;
262 else if ((FI->getModRefInfo() & MRI_Mod) == 0)
263 Min = FMRB_OnlyReadsMemory;
264 }
265
266 return FunctionModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min);
267 }
268
269 /// Returns the function info for the function, or null if we don't have
270 /// anything useful to say about it.
271 GlobalsModRef::FunctionInfo *GlobalsModRef::getFunctionInfo(const Function *F) {
272 auto I = FunctionInfos.find(F);
273 if (I != FunctionInfos.end())
274 return &I->second;
275 return nullptr;
276 }
277
278 /// AnalyzeGlobals - Scan through the users of all of the internal
279 /// GlobalValue's in the program. If none of them have their "address taken"
280 /// (really, their address passed to something nontrivial), record this fact,
281 /// and record the functions that they are used directly in.
282 void GlobalsModRef::AnalyzeGlobals(Module &M) {
283 SmallPtrSet TrackedFunctions;
284 for (Function &F : M)
285 if (F.hasLocalLinkage())
286 if (!AnalyzeUsesOfPointer(&F)) {
287 // Remember that we are tracking this global.
288 NonAddressTakenGlobals.insert(&F);
289 TrackedFunctions.insert(&F);
290 Handles.emplace_front(*this, &F);
291 Handles.front().I = Handles.begin();
292 ++NumNonAddrTakenFunctions;
293 }
294
295 SmallPtrSet Readers, Writers;
296 for (GlobalVariable &GV : M.globals())
297 if (GV.hasLocalLinkage()) {
298 if (!AnalyzeUsesOfPointer(&GV, &Readers,
299 GV.isConstant() ? nullptr : &Writers)) {
300 // Remember that we are tracking this global, and the mod/ref fns
301 NonAddressTakenGlobals.insert(&GV);
302 Handles.emplace_front(*this, &GV);
303 Handles.front().I = Handles.begin();
304
305 for (Function *Reader : Readers) {
306 if (TrackedFunctions.insert(Reader).second) {
307 Handles.emplace_front(*this, Reader);
308 Handles.front().I = Handles.begin();
309 }
310 FunctionInfos[Reader].addModRefInfoForGlobal(GV, MRI_Ref);
311 }
312
313 if (!GV.isConstant()) // No need to keep track of writers to constants
314 for (Function *Writer : Writers) {
315 if (TrackedFunctions.insert(Writer).second) {
316 Handles.emplace_front(*this, Writer);
317 Handles.front().I = Handles.begin();
318 }
319 FunctionInfos[Writer].addModRefInfoForGlobal(GV, MRI_Mod);
320 }
321 ++NumNonAddrTakenGlobalVars;
322
323 // If this global holds a pointer type, see if it is an indirect global.
324 if (GV.getType()->getElementType()->isPointerTy() &&
325 AnalyzeIndirectGlobalMemory(&GV))
326 ++NumIndirectGlobalVars;
327 }
328 Readers.clear();
329 Writers.clear();
330 }
331 }
332
333 /// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer.
334 /// If this is used by anything complex (i.e., the address escapes), return
335 /// true. Also, while we are at it, keep track of those functions that read and
336 /// write to the value.
337 ///
338 /// If OkayStoreDest is non-null, stores into this global are allowed.
339 bool GlobalsModRef::AnalyzeUsesOfPointer(Value *V,
340 SmallPtrSetImpl *Readers,
341 SmallPtrSetImpl *Writers,
342 GlobalValue *OkayStoreDest) {
343 if (!V->getType()->isPointerTy())
344 return true;
345
346 for (Use &U : V->uses()) {
347 User *I = U.getUser();
348 if (LoadInst *LI = dyn_cast(I)) {
349 if (Readers)
350 Readers->insert(LI->getParent()->getParent());
351 } else if (StoreInst *SI = dyn_cast(I)) {
352 if (V == SI->getOperand(1)) {
353 if (Writers)
354 Writers->insert(SI->getParent()->getParent());
355 } else if (SI->getOperand(1) != OkayStoreDest) {
356 return true; // Storing the pointer
357 }
358 } else if (Operator::getOpcode(I) == Instruction::GetElementPtr) {
359 if (AnalyzeUsesOfPointer(I, Readers, Writers))
360 return true;
361 } else if (Operator::getOpcode(I) == Instruction::BitCast) {
362 if (AnalyzeUsesOfPointer(I, Readers, Writers, OkayStoreDest))
363 return true;
364 } else if (auto CS = CallSite(I)) {
365 // Make sure that this is just the function being called, not that it is
366 // passing into the function.
367 if (!CS.isCallee(&U)) {
368 // Detect calls to free.
369 if (isFreeCall(I, TLI)) {
370 if (Writers)
371 Writers->insert(CS->getParent()->getParent());
372 } else {
373 return true; // Argument of an unknown call.
374 }
375 }
376 } else if (ICmpInst *ICI = dyn_cast(I)) {
377 if (!isa(ICI->getOperand(1)))
378 return true; // Allow comparison against null.
379 } else {
380 return true;
381 }
382 }
383
384 return false;
385 }
386
387 /// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable
388 /// which holds a pointer type. See if the global always points to non-aliased
389 /// heap memory: that is, all initializers of the globals are allocations, and
390 /// those allocations have no use other than initialization of the global.
391 /// Further, all loads out of GV must directly use the memory, not store the
392 /// pointer somewhere. If this is true, we consider the memory pointed to by
393 /// GV to be owned by GV and can disambiguate other pointers from it.
394 bool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) {
395 // Keep track of values related to the allocation of the memory, f.e. the
396 // value produced by the malloc call and any casts.
397 std::vector AllocRelatedValues;
398
399 // Walk the user list of the global. If we find anything other than a direct
400 // load or store, bail out.
401 for (User *U : GV->users()) {
402 if (LoadInst *LI = dyn_cast(U)) {
403 // The pointer loaded from the global can only be used in simple ways:
404 // we allow addressing of it and loading storing to it. We do *not* allow
405 // storing the loaded pointer somewhere else or passing to a function.
406 if (AnalyzeUsesOfPointer(LI))
407 return false; // Loaded pointer escapes.
408 // TODO: Could try some IP mod/ref of the loaded pointer.
409 } else if (StoreInst *SI = dyn_cast(U)) {
410 // Storing the global itself.
411 if (SI->getOperand(0) == GV)
412 return false;
413
414 // If storing the null pointer, ignore it.
415 if (isa(SI->getOperand(0)))
416 continue;
417
418 // Check the value being stored.
419 Value *Ptr = GetUnderlyingObject(SI->getOperand(0),
420 GV->getParent()->getDataLayout());
421
422 if (!isAllocLikeFn(Ptr, TLI))
423 return false; // Too hard to analyze.
424
425 // Analyze all uses of the allocation. If any of them are used in a
426 // non-simple way (e.g. stored to another global) bail out.
427 if (AnalyzeUsesOfPointer(Ptr, /*Readers*/ nullptr, /*Writers*/ nullptr,
428 GV))
429 return false; // Loaded pointer escapes.
430
431 // Remember that this allocation is related to the indirect global.
432 AllocRelatedValues.push_back(Ptr);
433 } else {
434 // Something complex, bail out.
435 return false;
436 }
437 }
438
439 // Okay, this is an indirect global. Remember all of the allocations for
440 // this global in AllocsForIndirectGlobals.
441 while (!AllocRelatedValues.empty()) {
442 AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV;
443 Handles.emplace_front(*this, AllocRelatedValues.back());
444 Handles.front().I = Handles.begin();
445 AllocRelatedValues.pop_back();
446 }
447 IndirectGlobals.insert(GV);
448 Handles.emplace_front(*this, GV);
449 Handles.front().I = Handles.begin();
450 return true;
451 }
452
453 /// AnalyzeCallGraph - At this point, we know the functions where globals are
454 /// immediately stored to and read from. Propagate this information up the call
455 /// graph to all callers and compute the mod/ref info for all memory for each
456 /// function.
457 void GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) {
458 // We do a bottom-up SCC traversal of the call graph. In other words, we
459 // visit all callees before callers (leaf-first).
460 for (scc_iterator I = scc_begin(&CG); !I.isAtEnd(); ++I) {
461 const std::vector &SCC = *I;
462 assert(!SCC.empty() && "SCC with no functions?");
463
464 if (!SCC[0]->getFunction()) {
465 // Calls externally - can't say anything useful. Remove any existing
466 // function records (may have been created when scanning globals).
467 for (auto *Node : SCC)
468 FunctionInfos.erase(Node->getFunction());
469 continue;
470 }
471
472 FunctionInfo &FI = FunctionInfos[SCC[0]->getFunction()];
473 bool KnowNothing = false;
474
475 // Collect the mod/ref properties due to called functions. We only compute
476 // one mod-ref set.
477 for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) {
478 Function *F = SCC[i]->getFunction();
479 if (!F) {
480 KnowNothing = true;
481 break;
482 }
483
484 if (F->isDeclaration()) {
485 // Try to get mod/ref behaviour from function attributes.
486 if (F->doesNotAccessMemory()) {
487 // Can't do better than that!
488 } else if (F->onlyReadsMemory()) {
489 FI.addModRefInfo(MRI_Ref);
490 if (!F->isIntrinsic())
491 // This function might call back into the module and read a global -
492 // consider every global as possibly being read by this function.
493 FI.setMayReadAnyGlobal();
494 } else {
495 FI.addModRefInfo(MRI_ModRef);
496 // Can't say anything useful unless it's an intrinsic - they don't
497 // read or write global variables of the kind considered here.
498 KnowNothing = !F->isIntrinsic();
499 }
500 continue;
501 }
502
503 for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end();
504 CI != E && !KnowNothing; ++CI)
505 if (Function *Callee = CI->second->getFunction()) {
506 if (FunctionInfo *CalleeFI = getFunctionInfo(Callee)) {
507 // Propagate function effect up.
508 FI.addFunctionInfo(*CalleeFI);
509 } else {
510 // Can't say anything about it. However, if it is inside our SCC,
511 // then nothing needs to be done.
512 CallGraphNode *CalleeNode = CG[Callee];
513 if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end())
514 KnowNothing = true;
515 }
516 } else {
517 KnowNothing = true;
518 }
519 }
520
521 // If we can't say anything useful about this SCC, remove all SCC functions
522 // from the FunctionInfos map.
523 if (KnowNothing) {
524 for (auto *Node : SCC)
525 FunctionInfos.erase(Node->getFunction());
526 continue;
527 }
528
529 // Scan the function bodies for explicit loads or stores.
530 for (auto *Node : SCC) {
531 if (FI.getModRefInfo() == MRI_ModRef)
532 break; // The mod/ref lattice saturates here.
533 for (Instruction &I : instructions(Node->getFunction())) {
534 if (FI.getModRefInfo() == MRI_ModRef)
535 break; // The mod/ref lattice saturates here.
536
537 // We handle calls specially because the graph-relevant aspects are
538 // handled above.
539 if (auto CS = CallSite(&I)) {
540 if (isAllocationFn(&I, TLI) || isFreeCall(&I, TLI)) {
541 // FIXME: It is completely unclear why this is necessary and not
542 // handled by the above graph code.
543 FI.addModRefInfo(MRI_ModRef);
544 } else if (Function *Callee = CS.getCalledFunction()) {
545 // The callgraph doesn't include intrinsic calls.
546 if (Callee->isIntrinsic()) {
547 FunctionModRefBehavior Behaviour =
548 AliasAnalysis::getModRefBehavior(Callee);
549 FI.addModRefInfo(ModRefInfo(Behaviour & MRI_ModRef));
550 }
551 }
552 continue;
553 }
554
555 // All non-call instructions we use the primary predicates for whether
556 // thay read or write memory.
557 if (I.mayReadFromMemory())
558 FI.addModRefInfo(MRI_Ref);
559 if (I.mayWriteToMemory())
560 FI.addModRefInfo(MRI_Mod);
561 }
562 }
563
564 if ((FI.getModRefInfo() & MRI_Mod) == 0)
565 ++NumReadMemFunctions;
566 if (FI.getModRefInfo() == MRI_NoModRef)
567 ++NumNoMemFunctions;
568
569 // Finally, now that we know the full effect on this SCC, clone the
570 // information to each function in the SCC.
571 for (unsigned i = 1, e = SCC.size(); i != e; ++i)
572 FunctionInfos[SCC[i]->getFunction()] = FI;
573 }
574 }
575
576 // There are particular cases where we can conclude no-alias between
577 // a non-addr-taken global and some other underlying object. Specifically,
578 // a non-addr-taken global is known to not be escaped from any function. It is
579 // also incorrect for a transformation to introduce an escape of a global in
580 // a way that is observable when it was not there previously. One function
581 // being transformed to introduce an escape which could possibly be observed
582 // (via loading from a global or the return value for example) within another
583 // function is never safe. If the observation is made through non-atomic
584 // operations on different threads, it is a data-race and UB. If the
585 // observation is well defined, by being observed the transformation would have
586 // changed program behavior by introducing the observed escape, making it an
587 // invalid transform.
588 //
589 // This property does require that transformations which *temporarily* escape
590 // a global that was not previously escaped, prior to restoring it, cannot rely
591 // on the results of GMR::alias. This seems a reasonable restriction, although
592 // currently there is no way to enforce it. There is also no realistic
593 // optimization pass that would make this mistake. The closest example is
594