blob: 720bef8579862cd9adce1a6db3a22fdef112b078 [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"
Chandler Carruthda7c1912015-07-22 09:27:58 +000032#include <list>
Chris Lattner26dff502004-06-28 06:33:13 +000033#include <set>
34using namespace llvm;
35
Chandler Carruthf1221bd2014-04-22 02:48:03 +000036#define DEBUG_TYPE "globalsmodref-aa"
37
Chris Lattner57ef9422006-12-19 22:30:33 +000038STATISTIC(NumNonAddrTakenGlobalVars,
39 "Number of global vars without address taken");
40STATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken");
41STATISTIC(NumNoMemFunctions, "Number of functions that do not access memory");
42STATISTIC(NumReadMemFunctions, "Number of functions that only read memory");
43STATISTIC(NumIndirectGlobalVars, "Number of indirect global objects");
44
Chandler Carruthf55803f2015-07-17 06:58:24 +000045// An option to enable unsafe alias results from the GlobalsModRef analysis.
46// When enabled, GlobalsModRef will provide no-alias results which in extremely
47// rare cases may not be conservatively correct. In particular, in the face of
48// transforms which cause assymetry between how effective GetUnderlyingObject
49// is for two pointers, it may produce incorrect results.
50//
51// These unsafe results have been returned by GMR for many years without
52// causing significant issues in the wild and so we provide a mechanism to
53// re-enable them for users of LLVM that have a particular performance
54// sensitivity and no known issues. The option also makes it easy to evaluate
55// the performance impact of these results.
56static cl::opt<bool> EnableUnsafeGlobalsModRefAliasResults(
57 "enable-unsafe-globalsmodref-alias-results", cl::init(false), cl::Hidden);
58
Chris Lattner26dff502004-06-28 06:33:13 +000059namespace {
Chandler Carruth466d7ad2015-07-14 08:42:39 +000060/// FunctionRecord - One instance of this structure is stored for every
61/// function in the program. Later, the entries for these functions are
62/// removed if the function is found to call an external function (in which
63/// case we know nothing about it.
64struct FunctionRecord {
65 /// GlobalInfo - Maintain mod/ref info for all of the globals without
66 /// addresses taken that are read or written (transitively) by this
67 /// function.
68 std::map<const GlobalValue *, unsigned> GlobalInfo;
Chris Lattner3a353e82004-07-27 06:40:37 +000069
Chandler Carruth466d7ad2015-07-14 08:42:39 +000070 /// MayReadAnyGlobal - May read global variables, but it is not known which.
71 bool MayReadAnyGlobal;
Duncan Sands06dbb122008-09-12 07:29:58 +000072
Chandler Carruth466d7ad2015-07-14 08:42:39 +000073 unsigned getInfoForGlobal(const GlobalValue *GV) const {
74 unsigned Effect = MayReadAnyGlobal ? AliasAnalysis::Ref : 0;
75 std::map<const GlobalValue *, unsigned>::const_iterator I =
Dan Gohman5442c712010-08-03 21:48:53 +000076 GlobalInfo.find(GV);
Chandler Carruth466d7ad2015-07-14 08:42:39 +000077 if (I != GlobalInfo.end())
78 Effect |= I->second;
79 return Effect;
80 }
81
82 /// FunctionEffect - Capture whether or not this function reads or writes to
83 /// ANY memory. If not, we can do a lot of aggressive analysis on it.
84 unsigned FunctionEffect;
85
86 FunctionRecord() : MayReadAnyGlobal(false), FunctionEffect(0) {}
87};
88
89/// GlobalsModRef - The actual analysis pass.
90class GlobalsModRef : public ModulePass, public AliasAnalysis {
Chandler Carruthda7c1912015-07-22 09:27:58 +000091 /// The globals that do not have their addresses taken.
Chandler Carruth466d7ad2015-07-14 08:42:39 +000092 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
Chandler Carruthda7c1912015-07-22 09:27:58 +0000106 /// Handle to clear this analysis on deletion of values.
Chandler Carruth8f1b63e2015-07-22 11:10:41 +0000107 struct DeletionCallbackHandle final : CallbackVH {
108 GlobalsModRef &GMR;
109 std::list<DeletionCallbackHandle>::iterator I;
110
111 DeletionCallbackHandle(GlobalsModRef &GMR, Value *V)
112 : CallbackVH(V), GMR(GMR) {}
113
114 void deleted() override {
115 Value *V = getValPtr();
116 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
117 if (GMR.NonAddressTakenGlobals.erase(GV)) {
118 // This global might be an indirect global. If so, remove it and
119 // remove
120 // any AllocRelatedValues for it.
121 if (GMR.IndirectGlobals.erase(GV)) {
122 // Remove any entries in AllocsForIndirectGlobals for this global.
123 for (std::map<const Value *, const GlobalValue *>::iterator
124 I = GMR.AllocsForIndirectGlobals.begin(),
125 E = GMR.AllocsForIndirectGlobals.end();
126 I != E;) {
127 if (I->second == GV) {
128 GMR.AllocsForIndirectGlobals.erase(I++);
129 } else {
130 ++I;
131 }
132 }
133 }
134 }
135 }
136
137 // If this is an allocation related to an indirect global, remove it.
138 GMR.AllocsForIndirectGlobals.erase(V);
139
140 // And clear out the handle.
141 setValPtr(nullptr);
142 GMR.Handles.erase(I);
143 // This object is now destroyed!
144 }
145 };
Chandler Carruthda7c1912015-07-22 09:27:58 +0000146
147 /// List of callbacks for globals being tracked by this analysis. Note that
148 /// these objects are quite large, but we only anticipate having one per
149 /// global tracked by this analysis. There are numerous optimizations we
150 /// could perform to the memory utilization here if this becomes a problem.
151 std::list<DeletionCallbackHandle> Handles;
152
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000153public:
154 static char ID;
155 GlobalsModRef() : ModulePass(ID) {
156 initializeGlobalsModRefPass(*PassRegistry::getPassRegistry());
157 }
158
159 bool runOnModule(Module &M) override {
160 InitializeAliasAnalysis(this, &M.getDataLayout());
161
162 // Find non-addr taken globals.
163 AnalyzeGlobals(M);
164
165 // Propagate on CG.
166 AnalyzeCallGraph(getAnalysis<CallGraphWrapperPass>().getCallGraph(), M);
167 return false;
168 }
169
170 void getAnalysisUsage(AnalysisUsage &AU) const override {
171 AliasAnalysis::getAnalysisUsage(AU);
172 AU.addRequired<CallGraphWrapperPass>();
173 AU.setPreservesAll(); // Does not transform code
174 }
175
176 //------------------------------------------------
177 // Implement the AliasAnalysis API
178 //
179 AliasResult alias(const MemoryLocation &LocA,
180 const MemoryLocation &LocB) override;
181 ModRefResult getModRefInfo(ImmutableCallSite CS,
182 const MemoryLocation &Loc) override;
183 ModRefResult getModRefInfo(ImmutableCallSite CS1,
184 ImmutableCallSite CS2) override {
185 return AliasAnalysis::getModRefInfo(CS1, CS2);
186 }
187
188 /// getModRefBehavior - Return the behavior of the specified function if
189 /// called from the specified call site. The call site may be null in which
190 /// case the most generic behavior of this function should be returned.
191 ModRefBehavior getModRefBehavior(const Function *F) override {
192 ModRefBehavior Min = UnknownModRefBehavior;
193
194 if (FunctionRecord *FR = getFunctionInfo(F)) {
195 if (FR->FunctionEffect == 0)
196 Min = DoesNotAccessMemory;
197 else if ((FR->FunctionEffect & Mod) == 0)
198 Min = OnlyReadsMemory;
Chris Lattner3a353e82004-07-27 06:40:37 +0000199 }
Misha Brukman01808ca2005-04-21 21:13:18 +0000200
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000201 return ModRefBehavior(AliasAnalysis::getModRefBehavior(F) & Min);
202 }
Chris Lattnerb6964622004-07-27 07:46:26 +0000203
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000204 /// getModRefBehavior - Return the behavior of the specified function if
205 /// called from the specified call site. The call site may be null in which
206 /// case the most generic behavior of this function should be returned.
207 ModRefBehavior getModRefBehavior(ImmutableCallSite CS) override {
208 ModRefBehavior Min = UnknownModRefBehavior;
Chris Lattner3a353e82004-07-27 06:40:37 +0000209
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000210 if (const Function *F = CS.getCalledFunction())
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000211 if (FunctionRecord *FR = getFunctionInfo(F)) {
Chris Lattner3a353e82004-07-27 06:40:37 +0000212 if (FR->FunctionEffect == 0)
Dan Gohman2694e142010-11-10 01:02:18 +0000213 Min = DoesNotAccessMemory;
Misha Brukman77451162005-04-22 04:01:18 +0000214 else if ((FR->FunctionEffect & Mod) == 0)
Dan Gohman2694e142010-11-10 01:02:18 +0000215 Min = OnlyReadsMemory;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000216 }
Dan Gohman2694e142010-11-10 01:02:18 +0000217
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000218 return ModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min);
219 }
Dan Gohman2694e142010-11-10 01:02:18 +0000220
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000221 /// getAdjustedAnalysisPointer - This method is used when a pass implements
222 /// an analysis interface through multiple inheritance. If needed, it
223 /// should override this to adjust the this pointer as needed for the
224 /// specified pass info.
225 void *getAdjustedAnalysisPointer(AnalysisID PI) override {
226 if (PI == &AliasAnalysis::ID)
227 return (AliasAnalysis *)this;
228 return this;
229 }
Chris Lattner3a353e82004-07-27 06:40:37 +0000230
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000231private:
232 /// getFunctionInfo - Return the function info for the function, or null if
233 /// we don't have anything useful to say about it.
234 FunctionRecord *getFunctionInfo(const Function *F) {
235 std::map<const Function *, FunctionRecord>::iterator I =
Dan Gohman5442c712010-08-03 21:48:53 +0000236 FunctionInfo.find(F);
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000237 if (I != FunctionInfo.end())
238 return &I->second;
239 return nullptr;
240 }
Chris Lattner3a353e82004-07-27 06:40:37 +0000241
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000242 void AnalyzeGlobals(Module &M);
243 void AnalyzeCallGraph(CallGraph &CG, Module &M);
244 bool AnalyzeUsesOfPointer(Value *V, std::vector<Function *> &Readers,
245 std::vector<Function *> &Writers,
246 GlobalValue *OkayStoreDest = nullptr);
247 bool AnalyzeIndirectGlobalMemory(GlobalValue *GV);
248};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000249}
Chris Lattner26dff502004-06-28 06:33:13 +0000250
Dan Gohmand78c4002008-05-13 00:00:25 +0000251char GlobalsModRef::ID = 0;
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000252INITIALIZE_AG_PASS_BEGIN(GlobalsModRef, AliasAnalysis, "globalsmodref-aa",
253 "Simple mod/ref analysis for globals", false, true,
254 false)
Chandler Carruth6378cf52013-11-26 04:19:30 +0000255INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000256INITIALIZE_AG_PASS_END(GlobalsModRef, AliasAnalysis, "globalsmodref-aa",
257 "Simple mod/ref analysis for globals", false, true,
258 false)
Dan Gohmand78c4002008-05-13 00:00:25 +0000259
Chris Lattner26dff502004-06-28 06:33:13 +0000260Pass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); }
261
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000262/// AnalyzeGlobals - Scan through the users of all of the internal
Duncan Sands42c644e2008-09-03 12:55:42 +0000263/// GlobalValue's in the program. If none of them have their "address taken"
Chris Lattner26dff502004-06-28 06:33:13 +0000264/// (really, their address passed to something nontrivial), record this fact,
265/// and record the functions that they are used directly in.
266void GlobalsModRef::AnalyzeGlobals(Module &M) {
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000267 std::vector<Function *> Readers, Writers;
Chandler Carrutha033bbb2015-07-15 08:09:23 +0000268 for (Function &F : M)
269 if (F.hasLocalLinkage()) {
270 if (!AnalyzeUsesOfPointer(&F, Readers, Writers)) {
Chris Lattner3a353e82004-07-27 06:40:37 +0000271 // Remember that we are tracking this global.
Chandler Carrutha033bbb2015-07-15 08:09:23 +0000272 NonAddressTakenGlobals.insert(&F);
Chandler Carruthda7c1912015-07-22 09:27:58 +0000273 Handles.emplace_front(*this, &F);
274 Handles.front().I = Handles.begin();
Chris Lattner26dff502004-06-28 06:33:13 +0000275 ++NumNonAddrTakenFunctions;
276 }
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000277 Readers.clear();
278 Writers.clear();
Chris Lattner26dff502004-06-28 06:33:13 +0000279 }
280
Chandler Carrutha033bbb2015-07-15 08:09:23 +0000281 for (GlobalVariable &GV : M.globals())
282 if (GV.hasLocalLinkage()) {
283 if (!AnalyzeUsesOfPointer(&GV, Readers, Writers)) {
Chris Lattner26dff502004-06-28 06:33:13 +0000284 // Remember that we are tracking this global, and the mod/ref fns
Chandler Carrutha033bbb2015-07-15 08:09:23 +0000285 NonAddressTakenGlobals.insert(&GV);
Chandler Carruthda7c1912015-07-22 09:27:58 +0000286 Handles.emplace_front(*this, &GV);
287 Handles.front().I = Handles.begin();
Duncan Sands42c644e2008-09-03 12:55:42 +0000288
Chandler Carrutha033bbb2015-07-15 08:09:23 +0000289 for (Function *Reader : Readers)
290 FunctionInfo[Reader].GlobalInfo[&GV] |= Ref;
Chris Lattner3a353e82004-07-27 06:40:37 +0000291
Chandler Carrutha033bbb2015-07-15 08:09:23 +0000292 if (!GV.isConstant()) // No need to keep track of writers to constants
293 for (Function *Writer : Writers)
294 FunctionInfo[Writer].GlobalInfo[&GV] |= Mod;
Chris Lattner26dff502004-06-28 06:33:13 +0000295 ++NumNonAddrTakenGlobalVars;
Duncan Sands42c644e2008-09-03 12:55:42 +0000296
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000297 // If this global holds a pointer type, see if it is an indirect global.
Chandler Carrutha033bbb2015-07-15 08:09:23 +0000298 if (GV.getType()->getElementType()->isPointerTy() &&
299 AnalyzeIndirectGlobalMemory(&GV))
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000300 ++NumIndirectGlobalVars;
Chris Lattner26dff502004-06-28 06:33:13 +0000301 }
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000302 Readers.clear();
303 Writers.clear();
Chris Lattner26dff502004-06-28 06:33:13 +0000304 }
305}
306
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000307/// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer.
308/// If this is used by anything complex (i.e., the address escapes), return
309/// true. Also, while we are at it, keep track of those functions that read and
310/// write to the value.
311///
312/// If OkayStoreDest is non-null, stores into this global are allowed.
313bool GlobalsModRef::AnalyzeUsesOfPointer(Value *V,
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000314 std::vector<Function *> &Readers,
315 std::vector<Function *> &Writers,
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000316 GlobalValue *OkayStoreDest) {
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000317 if (!V->getType()->isPointerTy())
318 return true;
Chris Lattner26dff502004-06-28 06:33:13 +0000319
Chandler Carruthcdf47882014-03-09 03:16:01 +0000320 for (Use &U : V->uses()) {
321 User *I = U.getUser();
322 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Chris Lattner26dff502004-06-28 06:33:13 +0000323 Readers.push_back(LI->getParent()->getParent());
Chandler Carruthcdf47882014-03-09 03:16:01 +0000324 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000325 if (V == SI->getOperand(1)) {
326 Writers.push_back(SI->getParent()->getParent());
327 } else if (SI->getOperand(1) != OkayStoreDest) {
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000328 return true; // Storing the pointer
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000329 }
Chandler Carruthcdf47882014-03-09 03:16:01 +0000330 } else if (Operator::getOpcode(I) == Instruction::GetElementPtr) {
331 if (AnalyzeUsesOfPointer(I, Readers, Writers))
Victor Hernandez537d8d92009-09-18 21:34:51 +0000332 return true;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000333 } else if (Operator::getOpcode(I) == Instruction::BitCast) {
334 if (AnalyzeUsesOfPointer(I, Readers, Writers, OkayStoreDest))
Benjamin Kramerb8266d22014-02-10 14:17:30 +0000335 return true;
Benjamin Kramer3a09ef62015-04-10 14:50:08 +0000336 } else if (auto CS = CallSite(I)) {
Chris Lattner26dff502004-06-28 06:33:13 +0000337 // Make sure that this is just the function being called, not that it is
338 // passing into the function.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000339 if (!CS.isCallee(&U)) {
Benjamin Kramerb8266d22014-02-10 14:17:30 +0000340 // Detect calls to free.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000341 if (isFreeCall(I, TLI))
Benjamin Kramerb8266d22014-02-10 14:17:30 +0000342 Writers.push_back(CS->getParent()->getParent());
343 else
344 return true; // Argument of an unknown call.
Misha Brukman01808ca2005-04-21 21:13:18 +0000345 }
Chandler Carruthcdf47882014-03-09 03:16:01 +0000346 } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000347 if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000348 return true; // Allow comparison against null.
Chris Lattner26dff502004-06-28 06:33:13 +0000349 } else {
350 return true;
351 }
Gabor Greif070b9a22010-07-09 15:53:42 +0000352 }
353
Chris Lattner26dff502004-06-28 06:33:13 +0000354 return false;
355}
356
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000357/// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable
358/// which holds a pointer type. See if the global always points to non-aliased
359/// heap memory: that is, all initializers of the globals are allocations, and
360/// those allocations have no use other than initialization of the global.
361/// Further, all loads out of GV must directly use the memory, not store the
362/// pointer somewhere. If this is true, we consider the memory pointed to by
363/// GV to be owned by GV and can disambiguate other pointers from it.
364bool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) {
365 // Keep track of values related to the allocation of the memory, f.e. the
366 // value produced by the malloc call and any casts.
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000367 std::vector<Value *> AllocRelatedValues;
Duncan Sands42c644e2008-09-03 12:55:42 +0000368
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000369 // Walk the user list of the global. If we find anything other than a direct
370 // load or store, bail out.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000371 for (User *U : GV->users()) {
Gabor Greifaa389f52010-07-09 16:22:36 +0000372 if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000373 // The pointer loaded from the global can only be used in simple ways:
374 // we allow addressing of it and loading storing to it. We do *not* allow
375 // storing the loaded pointer somewhere else or passing to a function.
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000376 std::vector<Function *> ReadersWriters;
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000377 if (AnalyzeUsesOfPointer(LI, ReadersWriters, ReadersWriters))
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000378 return false; // Loaded pointer escapes.
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000379 // TODO: Could try some IP mod/ref of the loaded pointer.
Gabor Greifaa389f52010-07-09 16:22:36 +0000380 } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000381 // Storing the global itself.
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000382 if (SI->getOperand(0) == GV)
383 return false;
Duncan Sands42c644e2008-09-03 12:55:42 +0000384
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000385 // If storing the null pointer, ignore it.
386 if (isa<ConstantPointerNull>(SI->getOperand(0)))
387 continue;
Duncan Sands42c644e2008-09-03 12:55:42 +0000388
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000389 // Check the value being stored.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000390 Value *Ptr = GetUnderlyingObject(SI->getOperand(0),
391 GV->getParent()->getDataLayout());
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000392
Benjamin Kramer8bcc9712012-08-29 15:32:21 +0000393 if (!isAllocLikeFn(Ptr, TLI))
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000394 return false; // Too hard to analyze.
Duncan Sands42c644e2008-09-03 12:55:42 +0000395
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000396 // Analyze all uses of the allocation. If any of them are used in a
397 // non-simple way (e.g. stored to another global) bail out.
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000398 std::vector<Function *> ReadersWriters;
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000399 if (AnalyzeUsesOfPointer(Ptr, ReadersWriters, ReadersWriters, GV))
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000400 return false; // Loaded pointer escapes.
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000401
402 // Remember that this allocation is related to the indirect global.
403 AllocRelatedValues.push_back(Ptr);
404 } else {
405 // Something complex, bail out.
406 return false;
407 }
408 }
Duncan Sands42c644e2008-09-03 12:55:42 +0000409
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000410 // Okay, this is an indirect global. Remember all of the allocations for
411 // this global in AllocsForIndirectGlobals.
412 while (!AllocRelatedValues.empty()) {
413 AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV;
Chandler Carruthda7c1912015-07-22 09:27:58 +0000414 Handles.emplace_front(*this, AllocRelatedValues.back());
415 Handles.front().I = Handles.begin();
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000416 AllocRelatedValues.pop_back();
417 }
418 IndirectGlobals.insert(GV);
Chandler Carruthda7c1912015-07-22 09:27:58 +0000419 Handles.emplace_front(*this, GV);
420 Handles.front().I = Handles.begin();
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000421 return true;
422}
423
Chris Lattner26dff502004-06-28 06:33:13 +0000424/// AnalyzeCallGraph - At this point, we know the functions where globals are
425/// immediately stored to and read from. Propagate this information up the call
Chris Lattner3a353e82004-07-27 06:40:37 +0000426/// graph to all callers and compute the mod/ref info for all memory for each
Misha Brukman01808ca2005-04-21 21:13:18 +0000427/// function.
Chris Lattner26dff502004-06-28 06:33:13 +0000428void GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) {
Chris Lattner26dff502004-06-28 06:33:13 +0000429 // We do a bottom-up SCC traversal of the call graph. In other words, we
430 // visit all callees before callers (leaf-first).
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000431 for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000432 const std::vector<CallGraphNode *> &SCC = *I;
Duncan Sands21a57992008-09-04 19:16:20 +0000433 assert(!SCC.empty() && "SCC with no functions?");
Duncan Sands42c644e2008-09-03 12:55:42 +0000434
Duncan Sands21a57992008-09-04 19:16:20 +0000435 if (!SCC[0]->getFunction()) {
436 // Calls externally - can't say anything useful. Remove any existing
437 // function records (may have been created when scanning globals).
Chandler Carrutha033bbb2015-07-15 08:09:23 +0000438 for (auto *Node : SCC)
439 FunctionInfo.erase(Node->getFunction());
Duncan Sands42c644e2008-09-03 12:55:42 +0000440 continue;
Duncan Sands21a57992008-09-04 19:16:20 +0000441 }
442
443 FunctionRecord &FR = FunctionInfo[SCC[0]->getFunction()];
Chris Lattner26dff502004-06-28 06:33:13 +0000444
Duncan Sands42c644e2008-09-03 12:55:42 +0000445 bool KnowNothing = false;
446 unsigned FunctionEffect = 0;
Chris Lattner3a353e82004-07-27 06:40:37 +0000447
Duncan Sands42c644e2008-09-03 12:55:42 +0000448 // Collect the mod/ref properties due to called functions. We only compute
449 // one mod-ref set.
450 for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) {
451 Function *F = SCC[i]->getFunction();
452 if (!F) {
453 KnowNothing = true;
Chris Lattner3a353e82004-07-27 06:40:37 +0000454 break;
Chris Lattner26dff502004-06-28 06:33:13 +0000455 }
Chris Lattner3a353e82004-07-27 06:40:37 +0000456
Duncan Sands42c644e2008-09-03 12:55:42 +0000457 if (F->isDeclaration()) {
458 // Try to get mod/ref behaviour from function attributes.
Duncan Sands0eca0572008-09-03 15:31:24 +0000459 if (F->doesNotAccessMemory()) {
460 // Can't do better than that!
461 } else if (F->onlyReadsMemory()) {
Duncan Sands42c644e2008-09-03 12:55:42 +0000462 FunctionEffect |= Ref;
Duncan Sands06dbb122008-09-12 07:29:58 +0000463 if (!F->isIntrinsic())
Duncan Sandse30b36f2008-09-11 15:43:12 +0000464 // This function might call back into the module and read a global -
Duncan Sands06dbb122008-09-12 07:29:58 +0000465 // consider every global as possibly being read by this function.
466 FR.MayReadAnyGlobal = true;
Duncan Sands0eca0572008-09-03 15:31:24 +0000467 } else {
Duncan Sandsd4133ac2008-09-11 19:35:55 +0000468 FunctionEffect |= ModRef;
469 // Can't say anything useful unless it's an intrinsic - they don't
470 // read or write global variables of the kind considered here.
471 KnowNothing = !F->isIntrinsic();
Duncan Sands42c644e2008-09-03 12:55:42 +0000472 }
473 continue;
474 }
Misha Brukman01808ca2005-04-21 21:13:18 +0000475
Duncan Sands42c644e2008-09-03 12:55:42 +0000476 for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end();
Duncan Sands21a57992008-09-04 19:16:20 +0000477 CI != E && !KnowNothing; ++CI)
Duncan Sands42c644e2008-09-03 12:55:42 +0000478 if (Function *Callee = CI->second->getFunction()) {
479 if (FunctionRecord *CalleeFR = getFunctionInfo(Callee)) {
480 // Propagate function effect up.
481 FunctionEffect |= CalleeFR->FunctionEffect;
482
483 // Incorporate callee's effects on globals into our info.
Rafael Espindola0c4eea72014-05-08 17:57:50 +0000484 for (const auto &G : CalleeFR->GlobalInfo)
485 FR.GlobalInfo[G.first] |= G.second;
Duncan Sands06dbb122008-09-12 07:29:58 +0000486 FR.MayReadAnyGlobal |= CalleeFR->MayReadAnyGlobal;
Duncan Sands42c644e2008-09-03 12:55:42 +0000487 } else {
488 // Can't say anything about it. However, if it is inside our SCC,
489 // then nothing needs to be done.
490 CallGraphNode *CalleeNode = CG[Callee];
491 if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end())
492 KnowNothing = true;
493 }
494 } else {
495 KnowNothing = true;
496 }
497 }
498
499 // If we can't say anything useful about this SCC, remove all SCC functions
500 // from the FunctionInfo map.
501 if (KnowNothing) {
Chandler Carrutha033bbb2015-07-15 08:09:23 +0000502 for (auto *Node : SCC)
503 FunctionInfo.erase(Node->getFunction());
Duncan Sandse74d7502008-09-03 16:10:55 +0000504 continue;
Duncan Sands42c644e2008-09-03 12:55:42 +0000505 }
506
507 // Scan the function bodies for explicit loads or stores.
Chandler Carruth6af95d02015-07-15 08:53:29 +0000508 for (auto *Node : SCC) {
509 if (FunctionEffect == ModRef)
510 break; // The mod/ref lattice saturates here.
511 for (Instruction &I : inst_range(Node->getFunction())) {
512 if (FunctionEffect == ModRef)
513 break; // The mod/ref lattice saturates here.
514
515 // We handle calls specially because the graph-relevant aspects are
516 // handled above.
517 if (auto CS = CallSite(&I)) {
518 if (isAllocationFn(&I, TLI) || isFreeCall(&I, TLI)) {
519 // FIXME: It is completely unclear why this is necessary and not
520 // handled by the above graph code.
521 FunctionEffect |= ModRef;
522 } else if (Function *Callee = CS.getCalledFunction()) {
523 // The callgraph doesn't include intrinsic calls.
524 if (Callee->isIntrinsic()) {
525 ModRefBehavior Behaviour =
526 AliasAnalysis::getModRefBehavior(Callee);
527 FunctionEffect |= (Behaviour & ModRef);
528 }
529 }
530 continue;
Duncan Sands9ddb3142008-09-13 12:45:50 +0000531 }
Duncan Sands42c644e2008-09-03 12:55:42 +0000532
Chandler Carruth6af95d02015-07-15 08:53:29 +0000533 // All non-call instructions we use the primary predicates for whether
534 // thay read or write memory.
535 if (I.mayReadFromMemory())
536 FunctionEffect |= Ref;
537 if (I.mayWriteToMemory())
538 FunctionEffect |= Mod;
539 }
540 }
541
Duncan Sands42c644e2008-09-03 12:55:42 +0000542 if ((FunctionEffect & Mod) == 0)
543 ++NumReadMemFunctions;
544 if (FunctionEffect == 0)
545 ++NumNoMemFunctions;
Duncan Sands21a57992008-09-04 19:16:20 +0000546 FR.FunctionEffect = FunctionEffect;
Duncan Sands42c644e2008-09-03 12:55:42 +0000547
548 // Finally, now that we know the full effect on this SCC, clone the
549 // information to each function in the SCC.
550 for (unsigned i = 1, e = SCC.size(); i != e; ++i)
Duncan Sands21a57992008-09-04 19:16:20 +0000551 FunctionInfo[SCC[i]->getFunction()] = FR;
Chris Lattner3a353e82004-07-27 06:40:37 +0000552 }
Chris Lattner26dff502004-06-28 06:33:13 +0000553}
554
Chris Lattner26dff502004-06-28 06:33:13 +0000555/// alias - If one of the pointers is to a global that we are tracking, and the
556/// other is some random pointer, we know there cannot be an alias, because the
557/// address of the global isn't taken.
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000558AliasResult GlobalsModRef::alias(const MemoryLocation &LocA,
559 const MemoryLocation &LocB) {
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000560 // Get the base object these pointers point to.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000561 const Value *UV1 = GetUnderlyingObject(LocA.Ptr, *DL);
562 const Value *UV2 = GetUnderlyingObject(LocB.Ptr, *DL);
Duncan Sands42c644e2008-09-03 12:55:42 +0000563
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000564 // If either of the underlying values is a global, they may be non-addr-taken
565 // globals, which we can answer queries about.
Dan Gohman5442c712010-08-03 21:48:53 +0000566 const GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1);
567 const GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2);
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000568 if (GV1 || GV2) {
569 // If the global's address is taken, pretend we don't know it's a pointer to
570 // the global.
Chandler Carruth466d7ad2015-07-14 08:42:39 +0000571 if (GV1 && !NonAddressTakenGlobals.count(GV1))
572 GV1 = nullptr;
573 if (GV2 && !NonAddressTakenGlobals.count(GV2))
574 GV2 = nullptr;
Chris Lattner26dff502004-06-28 06:33:13 +0000575
Dan Gohman4a618822010-02-10 16:03:48 +0000576 // If the two pointers are derived from two different non-addr-taken
Chandler Carruthf55803f2015-07-17 06:58:24 +0000577 // globals we know these can't alias.
578 if (GV1 && GV2 && GV1 != GV2)
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000579 return NoAlias;
Chris Lattner26dff502004-06-28 06:33:13 +0000580
Chandler Carruthf55803f2015-07-17 06:58:24 +0000581 // If one is and the other isn't, it isn't strictly safe but we can fake
582 // this result if necessary for performance. This does not appear to be
583 // a common problem in practice.
584 if (EnableUnsafeGlobalsModRefAliasResults)
585 if ((GV1 || GV2) && GV1 != GV2)
586 return NoAlias;
587
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000588 // Otherwise if they are both derived from the same addr-taken global, we
589 // can't know the two accesses don't overlap.
590 }
Duncan Sands42c644e2008-09-03 12:55:42 +0000591
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000592 // These pointers may be based on the memory owned by an indirect global. If
593 // so, we may be able to handle this. First check to see if the base pointer
594 // is a direct load from an indirect global.
Craig Topper353eda42014-04-24 06:44:33 +0000595 GV1 = GV2 = nullptr;
Dan Gohman5442c712010-08-03 21:48:53 +0000596 if (const LoadInst *LI = dyn_cast<LoadInst>(UV1))
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000597 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
598 if (IndirectGlobals.count(GV))
599 GV1 = GV;
Dan Gohman5442c712010-08-03 21:48:53 +0000600 if (const LoadInst *LI = dyn_cast<LoadInst>(UV2))
601 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000602 if (IndirectGlobals.count(GV))
603 GV2 = GV;
Duncan Sands42c644e2008-09-03 12:55:42 +0000604
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000605 // These pointers may also be from an allocation for the indirect global. If
606 // so, also handle them.
607 if (AllocsForIndirectGlobals.count(UV1))
608 GV1 = AllocsForIndirectGlobals[UV1];
609 if (AllocsForIndirectGlobals.count(UV2))
610 GV2 = AllocsForIndirectGlobals[UV2];
Duncan Sands42c644e2008-09-03 12:55:42 +0000611
Chris Lattnerbfdd19b2006-10-01 22:36:45 +0000612 // Now that we know whether the two pointers are related to indirect globals,
Chandler Carruthf55803f2015-07-17 06:58:24 +0000613 // use this to disambiguate the pointers. If the pointers are based on
614 // different indirect globals they cannot alias.
615 if (GV1 && GV2 && GV1 != GV2)
Chris Lattner26dff502004-06-28 06:33:13 +0000616 return NoAlias;
Duncan Sands42c644e2008-09-03 12:55:42 +0000617
Chandler Carruthf55803f2015-07-17 06:58:24 +0000618 // If one is based on an indirect global and the other isn't, it isn't
619 // strictly safe but we can fake this result if necessary for performance.
620 // This does not appear to be a common problem in practice.
621 if (EnableUnsafeGlobalsModRefAliasResults)
622 if ((GV1 || GV2) && GV1 != GV2)
623 return NoAlias;
624
Dan Gohman41f14cf2010-09-14 21:25:10 +0000625 return AliasAnalysis::alias(LocA, LocB);
Chris Lattner26dff502004-06-28 06:33:13 +0000626}
627
628AliasAnalysis::ModRefResult
Chandler Carruthac80dc72015-06-17 07:18:54 +0000629GlobalsModRef::getModRefInfo(ImmutableCallSite CS, const MemoryLocation &Loc) {
Chris Lattner26dff502004-06-28 06:33:13 +0000630 unsigned Known = ModRef;
631
632 // If we are asking for mod/ref info of a direct call with a pointer to a
Chris Lattner3a353e82004-07-27 06:40:37 +0000633 // global we are tracking, return information if we have it.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000634 const DataLayout &DL = CS.getCaller()->getParent()->getDataLayout();
Dan Gohman41f14cf2010-09-14 21:25:10 +0000635 if (const GlobalValue *GV =
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000636 dyn_cast<GlobalValue>(GetUnderlyingObject(Loc.Ptr, DL)))
Rafael Espindola6de96a12009-01-15 20:18:42 +0000637 if (GV->hasLocalLinkage())
Dan Gohman5442c712010-08-03 21:48:53 +0000638 if (const Function *F = CS.getCalledFunction())
Chris Lattner3a353e82004-07-27 06:40:37 +0000639 if (NonAddressTakenGlobals.count(GV))
Dan Gohman5442c712010-08-03 21:48:53 +0000640 if (const FunctionRecord *FR = getFunctionInfo(F))
Chris Lattner3a353e82004-07-27 06:40:37 +0000641 Known = FR->getInfoForGlobal(GV);
Chris Lattner26dff502004-06-28 06:33:13 +0000642
643 if (Known == NoModRef)
644 return NoModRef; // No need to query other mod/ref analyses
Dan Gohman41f14cf2010-09-14 21:25:10 +0000645 return ModRefResult(Known & AliasAnalysis::getModRefInfo(CS, Loc));
Chris Lattner26dff502004-06-28 06:33:13 +0000646}