blob: e6c7a6a9df35148236622718437ef3abaea059d3 [file] [log] [blame]
Chris Lattner26dff502004-06-28 06:33:13 +00001//===- GlobalsModRef.cpp - Simple Mod/Ref Analysis for Globals ------------===//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
Chris Lattner26dff502004-06-28 06:33:13 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman01808ca2005-04-21 21:13:18 +00007//
Chris Lattner26dff502004-06-28 06:33:13 +00008//===----------------------------------------------------------------------===//
9//
10// This simple pass provides alias and mod/ref information for global values
Chris Lattner3a353e82004-07-27 06:40:37 +000011// that do not have their address taken, and keeps track of whether functions
12// read or write memory (are "pure"). For this simple (but very common) case,
13// we can provide pretty accurate and useful information.
Chris Lattner26dff502004-06-28 06:33:13 +000014//
15//===----------------------------------------------------------------------===//
16
Chris Lattner26dff502004-06-28 06:33:13 +000017#include "llvm/Analysis/Passes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/SCCIterator.h"
19#include "llvm/ADT/Statistic.h"
Chris Lattner26dff502004-06-28 06:33:13 +000020#include "llvm/Analysis/AliasAnalysis.h"
21#include "llvm/Analysis/CallGraph.h"
Victor Hernandezf390e042009-10-27 20:05:49 +000022#include "llvm/Analysis/MemoryBuiltins.h"
Dan Gohmana4fcd242010-12-15 20:02:24 +000023#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
25#include "llvm/IR/DerivedTypes.h"
Chandler Carruth83948572014-03-04 10:30:26 +000026#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Instructions.h"
28#include "llvm/IR/IntrinsicInst.h"
29#include "llvm/IR/Module.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/Pass.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000031#include "llvm/Support/CommandLine.h"
Chris Lattner26dff502004-06-28 06:33:13 +000032#include <set>
33using namespace llvm;
34
Chandler Carruthf1221bd2014-04-22 02:48:03 +000035#define DEBUG_TYPE "globalsmodref-aa"
36
Chris Lattner57ef9422006-12-19 22:30:33 +000037STATISTIC(NumNonAddrTakenGlobalVars,
38 "Number of global vars without address taken");
39STATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken");
40STATISTIC(NumNoMemFunctions, "Number of functions that do not access memory");
41STATISTIC(NumReadMemFunctions, "Number of functions that only read memory");
42STATISTIC(NumIndirectGlobalVars, "Number of indirect global objects");
43
Chandler Carruthf55803f2015-07-17 06:58:24 +000044// An option to enable unsafe alias results from the GlobalsModRef analysis.
45// When enabled, GlobalsModRef will provide no-alias results which in extremely
46// rare cases may not be conservatively correct. In particular, in the face of
47// transforms which cause assymetry between how effective GetUnderlyingObject
48// is for two pointers, it may produce incorrect results.
49//
50// These unsafe results have been returned by GMR for many years without
51// causing significant issues in the wild and so we provide a mechanism to
52// re-enable them for users of LLVM that have a particular performance
53// sensitivity and no known issues. The option also makes it easy to evaluate
54// the performance impact of these results.
55static cl::opt<bool> EnableUnsafeGlobalsModRefAliasResults(
56 "enable-unsafe-globalsmodref-alias-results", cl::init(false), cl::Hidden);
57
Chris Lattner26dff502004-06-28 06:33:13 +000058namespace {
Chandler Carruth466d7ad2015-07-14 08:42:39 +000059/// FunctionRecord - One instance of this structure is stored for every
60/// function in the program. Later, the entries for these functions are
61/// removed if the function is found to call an external function (in which
62/// case we know nothing about it.
63struct FunctionRecord {
64 /// GlobalInfo - Maintain mod/ref info for all of the globals without
65 /// addresses taken that are read or written (transitively) by this
66 /// function.
67 std::map<const GlobalValue *, unsigned> GlobalInfo;
Chris Lattner3a353e82004-07-27 06:40:37 +000068
Chandler Carruth466d7ad2015-07-14 08:42:39 +000069 /// MayReadAnyGlobal - May read global variables, but it is not known which.
70 bool MayReadAnyGlobal;
Duncan Sands06dbb122008-09-12 07:29:58 +000071
Chandler Carruth466d7ad2015-07-14 08:42:39 +000072 unsigned getInfoForGlobal(const GlobalValue *GV) const {
73 unsigned Effect = MayReadAnyGlobal ? AliasAnalysis::Ref : 0;
74 std::map<const GlobalValue *, unsigned>::const_iterator I =
Dan Gohman5442c712010-08-03 21:48:53 +000075 GlobalInfo.find(GV);
Chandler Carruth466d7ad2015-07-14 08:42:39 +000076 if (I != GlobalInfo.end())
77 Effect |= I->second;
78 return Effect;
79 }
80
81 /// FunctionEffect - Capture whether or not this function reads or writes to
82 /// ANY memory. If not, we can do a lot of aggressive analysis on it.
83 unsigned FunctionEffect;
84
85 FunctionRecord() : MayReadAnyGlobal(false), FunctionEffect(0) {}
86};
87
88/// GlobalsModRef - The actual analysis pass.
89class GlobalsModRef : public ModulePass, public AliasAnalysis {
90 /// NonAddressTakenGlobals - The globals that do not have their addresses
91 /// taken.
92 std::set<const GlobalValue *> NonAddressTakenGlobals;
93
94 /// IndirectGlobals - The memory pointed to by this global is known to be
95 /// 'owned' by the global.
96 std::set<const GlobalValue *> IndirectGlobals;
97
98 /// AllocsForIndirectGlobals - If an instruction allocates memory for an
99 /// indirect global, this map indicates which one.
100 std::map<const Value *, const GlobalValue *> AllocsForIndirectGlobals;
101
102 /// FunctionInfo - For each function, keep track of what globals are
103 /// modified or read.
104 std::map<const Function *, FunctionRecord> FunctionInfo;
105
106public:
107 static char ID;
108 GlobalsModRef() : ModulePass(ID) {
109 initializeGlobalsModRefPass(*PassRegistry::getPassRegistry());
110 }
111
112 bool runOnModule(Module &M) override {
113 InitializeAliasAnalysis(this, &M.getDataLayout());
114
115 // Find non-addr taken globals.
116 AnalyzeGlobals(M);
117
118 // Propagate on CG.
119 AnalyzeCallGraph(getAnalysis<CallGraphWrapperPass>().getCallGraph(), M);
120 return false;
121 }
122
123 void getAnalysisUsage(AnalysisUsage &AU) const override {
124 AliasAnalysis::getAnalysisUsage(AU);
125 AU.addRequired<CallGraphWrapperPass>();
126 AU.setPreservesAll(); // Does not transform code
127 }
128
129 //------------------------------------------------
130 // Implement the AliasAnalysis API
131 //
132 AliasResult alias(const MemoryLocation &LocA,
133 const MemoryLocation &LocB) override;
134 ModRefResult getModRefInfo(ImmutableCallSite CS,
135 const MemoryLocation &Loc) override;
136 ModRefResult getModRefInfo(ImmutableCallSite CS1,
137 ImmutableCallSite CS2) override {
138 return AliasAnalysis::getModRefInfo(CS1, CS2);
139 }
140
141 /// getModRefBehavior - Return the behavior of the specified function if
142 /// called from the specified call site. The call site may be null in which
143 /// case the most generic behavior of this function should be returned.
144 ModRefBehavior getModRefBehavior(const Function *F) override {
145 ModRefBehavior Min = UnknownModRefBehavior;
146
147 if (FunctionRecord *FR = getFunctionInfo(F)) {
148 if (FR->FunctionEffect == 0)
149 Min = DoesNotAccessMemory;
150 else if ((FR->FunctionEffect & Mod) == 0)
151 Min = OnlyReadsMemory;
Chris Lattner3a353e82004-07-27 06:40:37 +0000152 }
Misha Brukman01808ca2005-04-21 21:13:18 +0000153
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000154 return ModRefBehavior(AliasAnalysis::getModRefBehavior(F) & Min);
155 }
Chris Lattnerb6964622004-07-27 07:46:26 +0000156
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000157 /// getModRefBehavior - Return the behavior of the specified function if
158 /// called from the specified call site. The call site may be null in which
159 /// case the most generic behavior of this function should be returned.
160 ModRefBehavior getModRefBehavior(ImmutableCallSite CS) override {
161 ModRefBehavior Min = UnknownModRefBehavior;
Chris Lattner3a353e82004-07-27 06:40:37 +0000162
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000163 if (const Function *F = CS.getCalledFunction())
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000164 if (FunctionRecord *FR = getFunctionInfo(F)) {
Chris Lattner3a353e82004-07-27 06:40:37 +0000165 if (FR->FunctionEffect == 0)
Dan Gohman2694e142010-11-10 01:02:18 +0000166 Min = DoesNotAccessMemory;
Misha Brukman77451162005-04-22 04:01:18 +0000167 else if ((FR->FunctionEffect & Mod) == 0)
Dan Gohman2694e142010-11-10 01:02:18 +0000168 Min = OnlyReadsMemory;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000169 }
Dan Gohman2694e142010-11-10 01:02:18 +0000170
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000171 return ModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min);
172 }
Dan Gohman2694e142010-11-10 01:02:18 +0000173
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000174 void deleteValue(Value *V) override;
175 void addEscapingUse(Use &U) override;
Dan Gohman2694e142010-11-10 01:02:18 +0000176
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000177 /// getAdjustedAnalysisPointer - This method is used when a pass implements
178 /// an analysis interface through multiple inheritance. If needed, it
179 /// should override this to adjust the this pointer as needed for the
180 /// specified pass info.
181 void *getAdjustedAnalysisPointer(AnalysisID PI) override {
182 if (PI == &AliasAnalysis::ID)
183 return (AliasAnalysis *)this;
184 return this;
185 }
Chris Lattner3a353e82004-07-27 06:40:37 +0000186
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000187private:
188 /// getFunctionInfo - Return the function info for the function, or null if
189 /// we don't have anything useful to say about it.
190 FunctionRecord *getFunctionInfo(const Function *F) {
191 std::map<const Function *, FunctionRecord>::iterator I =
Dan Gohman5442c712010-08-03 21:48:53 +0000192 FunctionInfo.find(F);
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000193 if (I != FunctionInfo.end())
194 return &I->second;
195 return nullptr;
196 }
Chris Lattner3a353e82004-07-27 06:40:37 +0000197
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000198 void AnalyzeGlobals(Module &M);
199 void AnalyzeCallGraph(CallGraph &CG, Module &M);
200 bool AnalyzeUsesOfPointer(Value *V, std::vector<Function *> &Readers,
201 std::vector<Function *> &Writers,
202 GlobalValue *OkayStoreDest = nullptr);
203 bool AnalyzeIndirectGlobalMemory(GlobalValue *GV);
204};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000205}
Chris Lattner26dff502004-06-28 06:33:13 +0000206
Dan Gohmand78c4002008-05-13 00:00:25 +0000207char GlobalsModRef::ID = 0;
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000208INITIALIZE_AG_PASS_BEGIN(GlobalsModRef, AliasAnalysis, "globalsmodref-aa",
209 "Simple mod/ref analysis for globals", false, true,
210 false)
Chandler Carruth6378cf52013-11-26 04:19:30 +0000211INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000212INITIALIZE_AG_PASS_END(GlobalsModRef, AliasAnalysis, "globalsmodref-aa",
213 "Simple mod/ref analysis for globals", false, true,
214 false)
Dan Gohmand78c4002008-05-13 00:00:25 +0000215
Chris Lattner26dff502004-06-28 06:33:13 +0000216Pass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); }
217
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000218/// AnalyzeGlobals - Scan through the users of all of the internal
Duncan Sands42c644e2008-09-03 12:55:42 +0000219/// GlobalValue's in the program. If none of them have their "address taken"
Chris Lattner26dff502004-06-28 06:33:13 +0000220/// (really, their address passed to something nontrivial), record this fact,
221/// and record the functions that they are used directly in.
222void GlobalsModRef::AnalyzeGlobals(Module &M) {
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000223 std::vector<Function *> Readers, Writers;
Chandler Carrutha033bbb2015-07-15 08:09:23 +0000224 for (Function &F : M)
225 if (F.hasLocalLinkage()) {
226 if (!AnalyzeUsesOfPointer(&F, Readers, Writers)) {
Chris Lattner3a353e82004-07-27 06:40:37 +0000227 // Remember that we are tracking this global.
Chandler Carrutha033bbb2015-07-15 08:09:23 +0000228 NonAddressTakenGlobals.insert(&F);
Chris Lattner26dff502004-06-28 06:33:13 +0000229 ++NumNonAddrTakenFunctions;
230 }
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000231 Readers.clear();
232 Writers.clear();
Chris Lattner26dff502004-06-28 06:33:13 +0000233 }
234
Chandler Carrutha033bbb2015-07-15 08:09:23 +0000235 for (GlobalVariable &GV : M.globals())
236 if (GV.hasLocalLinkage()) {
237 if (!AnalyzeUsesOfPointer(&GV, Readers, Writers)) {
Chris Lattner26dff502004-06-28 06:33:13 +0000238 // Remember that we are tracking this global, and the mod/ref fns
Chandler Carrutha033bbb2015-07-15 08:09:23 +0000239 NonAddressTakenGlobals.insert(&GV);
Duncan Sands42c644e2008-09-03 12:55:42 +0000240
Chandler Carrutha033bbb2015-07-15 08:09:23 +0000241 for (Function *Reader : Readers)
242 FunctionInfo[Reader].GlobalInfo[&GV] |= Ref;
Chris Lattner3a353e82004-07-27 06:40:37 +0000243
Chandler Carrutha033bbb2015-07-15 08:09:23 +0000244 if (!GV.isConstant()) // No need to keep track of writers to constants
245 for (Function *Writer : Writers)
246 FunctionInfo[Writer].GlobalInfo[&GV] |= Mod;
Chris Lattner26dff502004-06-28 06:33:13 +0000247 ++NumNonAddrTakenGlobalVars;
Duncan Sands42c644e2008-09-03 12:55:42 +0000248
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000249 // If this global holds a pointer type, see if it is an indirect global.
Chandler Carrutha033bbb2015-07-15 08:09:23 +0000250 if (GV.getType()->getElementType()->isPointerTy() &&
251 AnalyzeIndirectGlobalMemory(&GV))
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000252 ++NumIndirectGlobalVars;
Chris Lattner26dff502004-06-28 06:33:13 +0000253 }
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000254 Readers.clear();
255 Writers.clear();
Chris Lattner26dff502004-06-28 06:33:13 +0000256 }
257}
258
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000259/// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer.
260/// If this is used by anything complex (i.e., the address escapes), return
261/// true. Also, while we are at it, keep track of those functions that read and
262/// write to the value.
263///
264/// If OkayStoreDest is non-null, stores into this global are allowed.
265bool GlobalsModRef::AnalyzeUsesOfPointer(Value *V,
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000266 std::vector<Function *> &Readers,
267 std::vector<Function *> &Writers,
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000268 GlobalValue *OkayStoreDest) {
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000269 if (!V->getType()->isPointerTy())
270 return true;
Chris Lattner26dff502004-06-28 06:33:13 +0000271
Chandler Carruthcdf47882014-03-09 03:16:01 +0000272 for (Use &U : V->uses()) {
273 User *I = U.getUser();
274 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Chris Lattner26dff502004-06-28 06:33:13 +0000275 Readers.push_back(LI->getParent()->getParent());
Chandler Carruthcdf47882014-03-09 03:16:01 +0000276 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000277 if (V == SI->getOperand(1)) {
278 Writers.push_back(SI->getParent()->getParent());
279 } else if (SI->getOperand(1) != OkayStoreDest) {
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000280 return true; // Storing the pointer
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000281 }
Chandler Carruthcdf47882014-03-09 03:16:01 +0000282 } else if (Operator::getOpcode(I) == Instruction::GetElementPtr) {
283 if (AnalyzeUsesOfPointer(I, Readers, Writers))
Victor Hernandez537d8d92009-09-18 21:34:51 +0000284 return true;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000285 } else if (Operator::getOpcode(I) == Instruction::BitCast) {
286 if (AnalyzeUsesOfPointer(I, Readers, Writers, OkayStoreDest))
Benjamin Kramerb8266d22014-02-10 14:17:30 +0000287 return true;
Benjamin Kramer3a09ef62015-04-10 14:50:08 +0000288 } else if (auto CS = CallSite(I)) {
Chris Lattner26dff502004-06-28 06:33:13 +0000289 // Make sure that this is just the function being called, not that it is
290 // passing into the function.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000291 if (!CS.isCallee(&U)) {
Benjamin Kramerb8266d22014-02-10 14:17:30 +0000292 // Detect calls to free.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000293 if (isFreeCall(I, TLI))
Benjamin Kramerb8266d22014-02-10 14:17:30 +0000294 Writers.push_back(CS->getParent()->getParent());
295 else
296 return true; // Argument of an unknown call.
Misha Brukman01808ca2005-04-21 21:13:18 +0000297 }
Chandler Carruthcdf47882014-03-09 03:16:01 +0000298 } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000299 if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000300 return true; // Allow comparison against null.
Chris Lattner26dff502004-06-28 06:33:13 +0000301 } else {
302 return true;
303 }
Gabor Greif070b9a22010-07-09 15:53:42 +0000304 }
305
Chris Lattner26dff502004-06-28 06:33:13 +0000306 return false;
307}
308
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000309/// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable
310/// which holds a pointer type. See if the global always points to non-aliased
311/// heap memory: that is, all initializers of the globals are allocations, and
312/// those allocations have no use other than initialization of the global.
313/// Further, all loads out of GV must directly use the memory, not store the
314/// pointer somewhere. If this is true, we consider the memory pointed to by
315/// GV to be owned by GV and can disambiguate other pointers from it.
316bool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) {
317 // Keep track of values related to the allocation of the memory, f.e. the
318 // value produced by the malloc call and any casts.
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000319 std::vector<Value *> AllocRelatedValues;
Duncan Sands42c644e2008-09-03 12:55:42 +0000320
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000321 // Walk the user list of the global. If we find anything other than a direct
322 // load or store, bail out.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000323 for (User *U : GV->users()) {
Gabor Greifaa389f52010-07-09 16:22:36 +0000324 if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000325 // The pointer loaded from the global can only be used in simple ways:
326 // we allow addressing of it and loading storing to it. We do *not* allow
327 // storing the loaded pointer somewhere else or passing to a function.
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000328 std::vector<Function *> ReadersWriters;
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000329 if (AnalyzeUsesOfPointer(LI, ReadersWriters, ReadersWriters))
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000330 return false; // Loaded pointer escapes.
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000331 // TODO: Could try some IP mod/ref of the loaded pointer.
Gabor Greifaa389f52010-07-09 16:22:36 +0000332 } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000333 // Storing the global itself.
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000334 if (SI->getOperand(0) == GV)
335 return false;
Duncan Sands42c644e2008-09-03 12:55:42 +0000336
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000337 // If storing the null pointer, ignore it.
338 if (isa<ConstantPointerNull>(SI->getOperand(0)))
339 continue;
Duncan Sands42c644e2008-09-03 12:55:42 +0000340
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000341 // Check the value being stored.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000342 Value *Ptr = GetUnderlyingObject(SI->getOperand(0),
343 GV->getParent()->getDataLayout());
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000344
Benjamin Kramer8bcc9712012-08-29 15:32:21 +0000345 if (!isAllocLikeFn(Ptr, TLI))
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000346 return false; // Too hard to analyze.
Duncan Sands42c644e2008-09-03 12:55:42 +0000347
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000348 // Analyze all uses of the allocation. If any of them are used in a
349 // non-simple way (e.g. stored to another global) bail out.
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000350 std::vector<Function *> ReadersWriters;
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000351 if (AnalyzeUsesOfPointer(Ptr, ReadersWriters, ReadersWriters, GV))
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000352 return false; // Loaded pointer escapes.
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000353
354 // Remember that this allocation is related to the indirect global.
355 AllocRelatedValues.push_back(Ptr);
356 } else {
357 // Something complex, bail out.
358 return false;
359 }
360 }
Duncan Sands42c644e2008-09-03 12:55:42 +0000361
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000362 // Okay, this is an indirect global. Remember all of the allocations for
363 // this global in AllocsForIndirectGlobals.
364 while (!AllocRelatedValues.empty()) {
365 AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV;
366 AllocRelatedValues.pop_back();
367 }
368 IndirectGlobals.insert(GV);
369 return true;
370}
371
Chris Lattner26dff502004-06-28 06:33:13 +0000372/// AnalyzeCallGraph - At this point, we know the functions where globals are
373/// immediately stored to and read from. Propagate this information up the call
Chris Lattner3a353e82004-07-27 06:40:37 +0000374/// graph to all callers and compute the mod/ref info for all memory for each
Misha Brukman01808ca2005-04-21 21:13:18 +0000375/// function.
Chris Lattner26dff502004-06-28 06:33:13 +0000376void GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) {
Chris Lattner26dff502004-06-28 06:33:13 +0000377 // We do a bottom-up SCC traversal of the call graph. In other words, we
378 // visit all callees before callers (leaf-first).
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000379 for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000380 const std::vector<CallGraphNode *> &SCC = *I;
Duncan Sands21a57992008-09-04 19:16:20 +0000381 assert(!SCC.empty() && "SCC with no functions?");
Duncan Sands42c644e2008-09-03 12:55:42 +0000382
Duncan Sands21a57992008-09-04 19:16:20 +0000383 if (!SCC[0]->getFunction()) {
384 // Calls externally - can't say anything useful. Remove any existing
385 // function records (may have been created when scanning globals).
Chandler Carrutha033bbb2015-07-15 08:09:23 +0000386 for (auto *Node : SCC)
387 FunctionInfo.erase(Node->getFunction());
Duncan Sands42c644e2008-09-03 12:55:42 +0000388 continue;
Duncan Sands21a57992008-09-04 19:16:20 +0000389 }
390
391 FunctionRecord &FR = FunctionInfo[SCC[0]->getFunction()];
Chris Lattner26dff502004-06-28 06:33:13 +0000392
Duncan Sands42c644e2008-09-03 12:55:42 +0000393 bool KnowNothing = false;
394 unsigned FunctionEffect = 0;
Chris Lattner3a353e82004-07-27 06:40:37 +0000395
Duncan Sands42c644e2008-09-03 12:55:42 +0000396 // Collect the mod/ref properties due to called functions. We only compute
397 // one mod-ref set.
398 for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) {
399 Function *F = SCC[i]->getFunction();
400 if (!F) {
401 KnowNothing = true;
Chris Lattner3a353e82004-07-27 06:40:37 +0000402 break;
Chris Lattner26dff502004-06-28 06:33:13 +0000403 }
Chris Lattner3a353e82004-07-27 06:40:37 +0000404
Duncan Sands42c644e2008-09-03 12:55:42 +0000405 if (F->isDeclaration()) {
406 // Try to get mod/ref behaviour from function attributes.
Duncan Sands0eca0572008-09-03 15:31:24 +0000407 if (F->doesNotAccessMemory()) {
408 // Can't do better than that!
409 } else if (F->onlyReadsMemory()) {
Duncan Sands42c644e2008-09-03 12:55:42 +0000410 FunctionEffect |= Ref;
Duncan Sands06dbb122008-09-12 07:29:58 +0000411 if (!F->isIntrinsic())
Duncan Sandse30b36f2008-09-11 15:43:12 +0000412 // This function might call back into the module and read a global -
Duncan Sands06dbb122008-09-12 07:29:58 +0000413 // consider every global as possibly being read by this function.
414 FR.MayReadAnyGlobal = true;
Duncan Sands0eca0572008-09-03 15:31:24 +0000415 } else {
Duncan Sandsd4133ac2008-09-11 19:35:55 +0000416 FunctionEffect |= ModRef;
417 // Can't say anything useful unless it's an intrinsic - they don't
418 // read or write global variables of the kind considered here.
419 KnowNothing = !F->isIntrinsic();
Duncan Sands42c644e2008-09-03 12:55:42 +0000420 }
421 continue;
422 }
Misha Brukman01808ca2005-04-21 21:13:18 +0000423
Duncan Sands42c644e2008-09-03 12:55:42 +0000424 for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end();
Duncan Sands21a57992008-09-04 19:16:20 +0000425 CI != E && !KnowNothing; ++CI)
Duncan Sands42c644e2008-09-03 12:55:42 +0000426 if (Function *Callee = CI->second->getFunction()) {
427 if (FunctionRecord *CalleeFR = getFunctionInfo(Callee)) {
428 // Propagate function effect up.
429 FunctionEffect |= CalleeFR->FunctionEffect;
430
431 // Incorporate callee's effects on globals into our info.
Rafael Espindola0c4eea72014-05-08 17:57:50 +0000432 for (const auto &G : CalleeFR->GlobalInfo)
433 FR.GlobalInfo[G.first] |= G.second;
Duncan Sands06dbb122008-09-12 07:29:58 +0000434 FR.MayReadAnyGlobal |= CalleeFR->MayReadAnyGlobal;
Duncan Sands42c644e2008-09-03 12:55:42 +0000435 } else {
436 // Can't say anything about it. However, if it is inside our SCC,
437 // then nothing needs to be done.
438 CallGraphNode *CalleeNode = CG[Callee];
439 if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end())
440 KnowNothing = true;
441 }
442 } else {
443 KnowNothing = true;
444 }
445 }
446
447 // If we can't say anything useful about this SCC, remove all SCC functions
448 // from the FunctionInfo map.
449 if (KnowNothing) {
Chandler Carrutha033bbb2015-07-15 08:09:23 +0000450 for (auto *Node : SCC)
451 FunctionInfo.erase(Node->getFunction());
Duncan Sandse74d7502008-09-03 16:10:55 +0000452 continue;
Duncan Sands42c644e2008-09-03 12:55:42 +0000453 }
454
455 // Scan the function bodies for explicit loads or stores.
Chandler Carruth6af95d02015-07-15 08:53:29 +0000456 for (auto *Node : SCC) {
457 if (FunctionEffect == ModRef)
458 break; // The mod/ref lattice saturates here.
459 for (Instruction &I : inst_range(Node->getFunction())) {
460 if (FunctionEffect == ModRef)
461 break; // The mod/ref lattice saturates here.
462
463 // We handle calls specially because the graph-relevant aspects are
464 // handled above.
465 if (auto CS = CallSite(&I)) {
466 if (isAllocationFn(&I, TLI) || isFreeCall(&I, TLI)) {
467 // FIXME: It is completely unclear why this is necessary and not
468 // handled by the above graph code.
469 FunctionEffect |= ModRef;
470 } else if (Function *Callee = CS.getCalledFunction()) {
471 // The callgraph doesn't include intrinsic calls.
472 if (Callee->isIntrinsic()) {
473 ModRefBehavior Behaviour =
474 AliasAnalysis::getModRefBehavior(Callee);
475 FunctionEffect |= (Behaviour & ModRef);
476 }
477 }
478 continue;
Duncan Sands9ddb3142008-09-13 12:45:50 +0000479 }
Duncan Sands42c644e2008-09-03 12:55:42 +0000480
Chandler Carruth6af95d02015-07-15 08:53:29 +0000481 // All non-call instructions we use the primary predicates for whether
482 // thay read or write memory.
483 if (I.mayReadFromMemory())
484 FunctionEffect |= Ref;
485 if (I.mayWriteToMemory())
486 FunctionEffect |= Mod;
487 }
488 }
489
Duncan Sands42c644e2008-09-03 12:55:42 +0000490 if ((FunctionEffect & Mod) == 0)
491 ++NumReadMemFunctions;
492 if (FunctionEffect == 0)
493 ++NumNoMemFunctions;
Duncan Sands21a57992008-09-04 19:16:20 +0000494 FR.FunctionEffect = FunctionEffect;
Duncan Sands42c644e2008-09-03 12:55:42 +0000495
496 // Finally, now that we know the full effect on this SCC, clone the
497 // information to each function in the SCC.
498 for (unsigned i = 1, e = SCC.size(); i != e; ++i)
Duncan Sands21a57992008-09-04 19:16:20 +0000499 FunctionInfo[SCC[i]->getFunction()] = FR;
Chris Lattner3a353e82004-07-27 06:40:37 +0000500 }
Chris Lattner26dff502004-06-28 06:33:13 +0000501}
502
Chris Lattner26dff502004-06-28 06:33:13 +0000503/// alias - If one of the pointers is to a global that we are tracking, and the
504/// other is some random pointer, we know there cannot be an alias, because the
505/// address of the global isn't taken.
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000506AliasResult GlobalsModRef::alias(const MemoryLocation &LocA,
507 const MemoryLocation &LocB) {
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000508 // Get the base object these pointers point to.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000509 const Value *UV1 = GetUnderlyingObject(LocA.Ptr, *DL);
510 const Value *UV2 = GetUnderlyingObject(LocB.Ptr, *DL);
Duncan Sands42c644e2008-09-03 12:55:42 +0000511
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000512 // If either of the underlying values is a global, they may be non-addr-taken
513 // globals, which we can answer queries about.
Dan Gohman5442c712010-08-03 21:48:53 +0000514 const GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1);
515 const GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2);
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000516 if (GV1 || GV2) {
517 // If the global's address is taken, pretend we don't know it's a pointer to
518 // the global.
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000519 if (GV1 && !NonAddressTakenGlobals.count(GV1))
520 GV1 = nullptr;
521 if (GV2 && !NonAddressTakenGlobals.count(GV2))
522 GV2 = nullptr;
Chris Lattner26dff502004-06-28 06:33:13 +0000523
Dan Gohman4a618822010-02-10 16:03:48 +0000524 // If the two pointers are derived from two different non-addr-taken
Chandler Carruthf55803f2015-07-17 06:58:24 +0000525 // globals we know these can't alias.
526 if (GV1 && GV2 && GV1 != GV2)
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000527 return NoAlias;
Chris Lattner26dff502004-06-28 06:33:13 +0000528
Chandler Carruthf55803f2015-07-17 06:58:24 +0000529 // If one is and the other isn't, it isn't strictly safe but we can fake
530 // this result if necessary for performance. This does not appear to be
531 // a common problem in practice.
532 if (EnableUnsafeGlobalsModRefAliasResults)
533 if ((GV1 || GV2) && GV1 != GV2)
534 return NoAlias;
535
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000536 // Otherwise if they are both derived from the same addr-taken global, we
537 // can't know the two accesses don't overlap.
538 }
Duncan Sands42c644e2008-09-03 12:55:42 +0000539
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000540 // These pointers may be based on the memory owned by an indirect global. If
541 // so, we may be able to handle this. First check to see if the base pointer
542 // is a direct load from an indirect global.
Craig Topper353eda42014-04-24 06:44:33 +0000543 GV1 = GV2 = nullptr;
Dan Gohman5442c712010-08-03 21:48:53 +0000544 if (const LoadInst *LI = dyn_cast<LoadInst>(UV1))
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000545 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
546 if (IndirectGlobals.count(GV))
547 GV1 = GV;
Dan Gohman5442c712010-08-03 21:48:53 +0000548 if (const LoadInst *LI = dyn_cast<LoadInst>(UV2))
549 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000550 if (IndirectGlobals.count(GV))
551 GV2 = GV;
Duncan Sands42c644e2008-09-03 12:55:42 +0000552
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000553 // These pointers may also be from an allocation for the indirect global. If
554 // so, also handle them.
555 if (AllocsForIndirectGlobals.count(UV1))
556 GV1 = AllocsForIndirectGlobals[UV1];
557 if (AllocsForIndirectGlobals.count(UV2))
558 GV2 = AllocsForIndirectGlobals[UV2];
Duncan Sands42c644e2008-09-03 12:55:42 +0000559
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000560 // Now that we know whether the two pointers are related to indirect globals,
Chandler Carruthf55803f2015-07-17 06:58:24 +0000561 // use this to disambiguate the pointers. If the pointers are based on
562 // different indirect globals they cannot alias.
563 if (GV1 && GV2 && GV1 != GV2)
Chris Lattner26dff502004-06-28 06:33:13 +0000564 return NoAlias;
Duncan Sands42c644e2008-09-03 12:55:42 +0000565
Chandler Carruthf55803f2015-07-17 06:58:24 +0000566 // If one is based on an indirect global and the other isn't, it isn't
567 // strictly safe but we can fake this result if necessary for performance.
568 // This does not appear to be a common problem in practice.
569 if (EnableUnsafeGlobalsModRefAliasResults)
570 if ((GV1 || GV2) && GV1 != GV2)
571 return NoAlias;
572
Dan Gohman41f14cf2010-09-14 21:25:10 +0000573 return AliasAnalysis::alias(LocA, LocB);
Chris Lattner26dff502004-06-28 06:33:13 +0000574}
575
576AliasAnalysis::ModRefResult
Chandler Carruthac80dc72015-06-17 07:18:54 +0000577GlobalsModRef::getModRefInfo(ImmutableCallSite CS, const MemoryLocation &Loc) {
Chris Lattner26dff502004-06-28 06:33:13 +0000578 unsigned Known = ModRef;
579
580 // If we are asking for mod/ref info of a direct call with a pointer to a
Chris Lattner3a353e82004-07-27 06:40:37 +0000581 // global we are tracking, return information if we have it.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000582 const DataLayout &DL = CS.getCaller()->getParent()->getDataLayout();
Dan Gohman41f14cf2010-09-14 21:25:10 +0000583 if (const GlobalValue *GV =
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000584 dyn_cast<GlobalValue>(GetUnderlyingObject(Loc.Ptr, DL)))
Rafael Espindola6de96a12009-01-15 20:18:42 +0000585 if (GV->hasLocalLinkage())
Dan Gohman5442c712010-08-03 21:48:53 +0000586 if (const Function *F = CS.getCalledFunction())
Chris Lattner3a353e82004-07-27 06:40:37 +0000587 if (NonAddressTakenGlobals.count(GV))
Dan Gohman5442c712010-08-03 21:48:53 +0000588 if (const FunctionRecord *FR = getFunctionInfo(F))
Chris Lattner3a353e82004-07-27 06:40:37 +0000589 Known = FR->getInfoForGlobal(GV);
Chris Lattner26dff502004-06-28 06:33:13 +0000590
591 if (Known == NoModRef)
592 return NoModRef; // No need to query other mod/ref analyses
Dan Gohman41f14cf2010-09-14 21:25:10 +0000593 return ModRefResult(Known & AliasAnalysis::getModRefInfo(CS, Loc));
Chris Lattner26dff502004-06-28 06:33:13 +0000594}
595
Chris Lattner26dff502004-06-28 06:33:13 +0000596//===----------------------------------------------------------------------===//
597// Methods to update the analysis as a result of the client transformation.
598//
599void GlobalsModRef::deleteValue(Value *V) {
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000600 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
601 if (NonAddressTakenGlobals.erase(GV)) {
602 // This global might be an indirect global. If so, remove it and remove
603 // any AllocRelatedValues for it.
604 if (IndirectGlobals.erase(GV)) {
605 // Remove any entries in AllocsForIndirectGlobals for this global.
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000606 for (std::map<const Value *, const GlobalValue *>::iterator
607 I = AllocsForIndirectGlobals.begin(),
608 E = AllocsForIndirectGlobals.end();
609 I != E;) {
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000610 if (I->second == GV) {
611 AllocsForIndirectGlobals.erase(I++);
612 } else {
613 ++I;
614 }
615 }
616 }
617 }
618 }
Duncan Sands42c644e2008-09-03 12:55:42 +0000619
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000620 // Otherwise, if this is an allocation related to an indirect global, remove
621 // it.
622 AllocsForIndirectGlobals.erase(V);
Duncan Sands42c644e2008-09-03 12:55:42 +0000623
Chris Lattner907703c2007-11-30 18:52:58 +0000624 AliasAnalysis::deleteValue(V);
Chris Lattner26dff502004-06-28 06:33:13 +0000625}
626
Owen Andersond62d3722011-01-03 23:51:43 +0000627void GlobalsModRef::addEscapingUse(Use &U) {
628 // For the purposes of this analysis, it is conservatively correct to treat
629 // a newly escaping value equivalently to a deleted one. We could perhaps
630 // be more precise by processing the new use and attempting to update our
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000631 // saved analysis results to accommodate it.
Owen Andersond62d3722011-01-03 23:51:43 +0000632 deleteValue(U);
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000633
Owen Andersond62d3722011-01-03 23:51:43 +0000634 AliasAnalysis::addEscapingUse(U);
635}