1//===- GlobalsModRef.cpp - Simple Mod/Ref Analysis for Globals ------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
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 "AnalysisOptions.h"
18#include "llvm/ADT/SCCIterator.h"
19#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/ADT/Statistic.h"
21#include "llvm/Analysis/CallGraph.h"
22#include "llvm/Analysis/MemoryBuiltins.h"
23#include "llvm/Analysis/TargetLibraryInfo.h"
24#include "llvm/Analysis/ValueTracking.h"
25#include "llvm/IR/Constants.h"
26#include "llvm/IR/InstIterator.h"
27#include "llvm/IR/Instructions.h"
28#include "llvm/IR/Module.h"
29#include "llvm/IR/PassManager.h"
30#include "llvm/InitializePasses.h"
31#include "llvm/Pass.h"
32
33using namespace llvm;
34
35#define DEBUG_TYPE "globalsmodref-aa"
36
37STATISTIC(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
44/// The mod/ref information collected for a particular function.
45///
46/// We collect information about mod/ref behavior of a function here, both in
47/// general and as pertains to specific globals. We only have this detailed
48/// information when we know *something* useful about the behavior. If we
49/// saturate to fully general mod/ref, we remove the info for the function.
50class GlobalsAAResult::FunctionInfo {
51 typedef SmallDenseMap<const GlobalValue *, ModRefInfo, 16> GlobalInfoMapType;
52
53 /// Build a wrapper struct that has 8-byte alignment. All heap allocations
54 /// should provide this much alignment at least, but this makes it clear we
55 /// specifically rely on this amount of alignment.
56 struct alignas(8) AlignedMap {
57 AlignedMap() = default;
58 AlignedMap(const AlignedMap &Arg) = default;
59 GlobalInfoMapType Map;
60 };
61
62 /// Pointer traits for our aligned map.
63 struct AlignedMapPointerTraits {
64 static inline void *getAsVoidPointer(AlignedMap *P) { return P; }
65 static inline AlignedMap *getFromVoidPointer(void *P) {
66 return (AlignedMap *)P;
67 }
68 static constexpr int NumLowBitsAvailable = 3;
69 static_assert(alignof(AlignedMap) >= (1 << NumLowBitsAvailable),
70 "AlignedMap insufficiently aligned to have enough low bits.");
71 };
72
73 /// The bit that flags that this function may read any global. This is
74 /// chosen to mix together with ModRefInfo bits.
75 /// FIXME: This assumes ModRefInfo lattice will remain 4 bits!
76 /// FunctionInfo.getModRefInfo() masks out everything except ModRef so
77 /// this remains correct.
78 enum { MayReadAnyGlobal = 4 };
79
80 /// Checks to document the invariants of the bit packing here.
81 static_assert((MayReadAnyGlobal & static_cast<int>(ModRefInfo::ModRef)) == 0,
82 "ModRef and the MayReadAnyGlobal flag bits overlap.");
83 static_assert(((MayReadAnyGlobal | static_cast<int>(ModRefInfo::ModRef)) >>
84 AlignedMapPointerTraits::NumLowBitsAvailable) == 0,
85 "Insufficient low bits to store our flag and ModRef info.");
86
87public:
88 FunctionInfo() = default;
89 ~FunctionInfo() {
90 delete Info.getPointer();
91 }
92 // Spell out the copy ond move constructors and assignment operators to get
93 // deep copy semantics and correct move semantics in the face of the
94 // pointer-int pair.
95 FunctionInfo(const FunctionInfo &Arg)
96 : Info(nullptr, Arg.Info.getInt()) {
97 if (const auto *ArgPtr = Arg.Info.getPointer())
98 Info.setPointer(new AlignedMap(*ArgPtr));
99 }
100 FunctionInfo(FunctionInfo &&Arg)
101 : Info(Arg.Info.getPointer(), Arg.Info.getInt()) {
102 Arg.Info.setPointerAndInt(PtrVal: nullptr, IntVal: 0);
103 }
104 FunctionInfo &operator=(const FunctionInfo &RHS) {
105 delete Info.getPointer();
106 Info.setPointerAndInt(PtrVal: nullptr, IntVal: RHS.Info.getInt());
107 if (const auto *RHSPtr = RHS.Info.getPointer())
108 Info.setPointer(new AlignedMap(*RHSPtr));
109 return *this;
110 }
111 FunctionInfo &operator=(FunctionInfo &&RHS) {
112 delete Info.getPointer();
113 Info.setPointerAndInt(PtrVal: RHS.Info.getPointer(), IntVal: RHS.Info.getInt());
114 RHS.Info.setPointerAndInt(PtrVal: nullptr, IntVal: 0);
115 return *this;
116 }
117
118 /// This method clears MayReadAnyGlobal bit added by GlobalsAAResult to return
119 /// the corresponding ModRefInfo.
120 ModRefInfo globalClearMayReadAnyGlobal(int I) const {
121 return ModRefInfo(I & static_cast<int>(ModRefInfo::ModRef));
122 }
123
124 /// Returns the \c ModRefInfo info for this function.
125 ModRefInfo getModRefInfo() const {
126 return globalClearMayReadAnyGlobal(I: Info.getInt());
127 }
128
129 /// Adds new \c ModRefInfo for this function to its state.
130 void addModRefInfo(ModRefInfo NewMRI) {
131 Info.setInt(Info.getInt() | static_cast<int>(NewMRI));
132 }
133
134 /// Returns whether this function may read any global variable, and we don't
135 /// know which global.
136 bool mayReadAnyGlobal() const { return Info.getInt() & MayReadAnyGlobal; }
137
138 /// Sets this function as potentially reading from any global.
139 void setMayReadAnyGlobal() { Info.setInt(Info.getInt() | MayReadAnyGlobal); }
140
141 /// Returns the \c ModRefInfo info for this function w.r.t. a particular
142 /// global, which may be more precise than the general information above.
143 ModRefInfo getModRefInfoForGlobal(const GlobalValue &GV) const {
144 ModRefInfo GlobalMRI =
145 mayReadAnyGlobal() ? ModRefInfo::Ref : ModRefInfo::NoModRef;
146 if (AlignedMap *P = Info.getPointer()) {
147 auto I = P->Map.find(Val: &GV);
148 if (I != P->Map.end())
149 GlobalMRI |= I->second;
150 }
151 return GlobalMRI;
152 }
153
154 /// Add mod/ref info from another function into ours, saturating towards
155 /// ModRef.
156 void addFunctionInfo(const FunctionInfo &FI) {
157 addModRefInfo(NewMRI: 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(GV: *G.first, NewMRI: 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 |= 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(Val: &GV);
182 }
183
184private:
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<AlignedMap *, 3, unsigned, AlignedMapPointerTraits> Info;
191};
192
193void GlobalsAAResult::DeletionCallbackHandle::deleted() {
194 Value *V = getValPtr();
195 if (auto *F = dyn_cast<Function>(Val: V))
196 GAR->FunctionInfos.erase(Val: F);
197
198 if (GlobalValue *GV = dyn_cast<GlobalValue>(Val: V)) {
199 if (GAR->NonAddressTakenGlobals.erase(Ptr: GV)) {
200 // This global might be an indirect global. If so, remove it and
201 // remove any AllocRelatedValues for it.
202 if (GAR->IndirectGlobals.erase(Ptr: GV)) {
203 // Remove any entries in AllocsForIndirectGlobals for this global.
204 GAR->AllocsForIndirectGlobals.remove_if(
205 Pred: [GV](const auto &Entry) { return Entry.second == GV; });
206 }
207
208 // Scan the function info we have collected and remove this global
209 // from all of them.
210 for (auto &FIPair : GAR->FunctionInfos)
211 FIPair.second.eraseModRefInfoForGlobal(GV: *GV);
212 }
213 }
214
215 // If this is an allocation related to an indirect global, remove it.
216 GAR->AllocsForIndirectGlobals.erase(Val: V);
217
218 // And clear out the handle.
219 setValPtr(nullptr);
220 GAR->Handles.erase(position: I);
221 // This object is now destroyed!
222}
223
224MemoryEffects GlobalsAAResult::getMemoryEffects(const Function *F) {
225 if (FunctionInfo *FI = getFunctionInfo(F))
226 return MemoryEffects(FI->getModRefInfo());
227
228 return MemoryEffects::unknown();
229}
230
231/// Returns the function info for the function, or null if we don't have
232/// anything useful to say about it.
233GlobalsAAResult::FunctionInfo *
234GlobalsAAResult::getFunctionInfo(const Function *F) {
235 auto I = FunctionInfos.find(Val: F);
236 if (I != FunctionInfos.end())
237 return &I->second;
238 return nullptr;
239}
240
241/// AnalyzeGlobals - Scan through the users of all of the internal
242/// GlobalValue's in the program. If none of them have their "address taken"
243/// (really, their address passed to something nontrivial), record this fact,
244/// and record the functions that they are used directly in.
245void GlobalsAAResult::AnalyzeGlobals(Module &M) {
246 SmallPtrSet<Function *, 32> TrackedFunctions;
247 for (Function &F : M)
248 if (F.hasLocalLinkage()) {
249 if (!AnalyzeUsesOfPointer(V: &F)) {
250 // Remember that we are tracking this global.
251 NonAddressTakenGlobals.insert(Ptr: &F);
252 TrackedFunctions.insert(Ptr: &F);
253 Handles.emplace_front(args&: *this, args: &F);
254 Handles.front().I = Handles.begin();
255 ++NumNonAddrTakenFunctions;
256 } else
257 UnknownFunctionsWithLocalLinkage = true;
258 }
259
260 SmallPtrSet<Function *, 16> Readers, Writers;
261 for (GlobalVariable &GV : M.globals())
262 if (GV.hasLocalLinkage()) {
263 if (!AnalyzeUsesOfPointer(V: &GV, Readers: &Readers,
264 Writers: GV.isConstant() ? nullptr : &Writers)) {
265 // Remember that we are tracking this global, and the mod/ref fns
266 NonAddressTakenGlobals.insert(Ptr: &GV);
267 Handles.emplace_front(args&: *this, args: &GV);
268 Handles.front().I = Handles.begin();
269
270 for (Function *Reader : Readers) {
271 if (TrackedFunctions.insert(Ptr: Reader).second) {
272 Handles.emplace_front(args&: *this, args&: Reader);
273 Handles.front().I = Handles.begin();
274 }
275 FunctionInfos[Reader].addModRefInfoForGlobal(GV, NewMRI: ModRefInfo::Ref);
276 }
277
278 if (!GV.isConstant()) // No need to keep track of writers to constants
279 for (Function *Writer : Writers) {
280 if (TrackedFunctions.insert(Ptr: Writer).second) {
281 Handles.emplace_front(args&: *this, args&: Writer);
282 Handles.front().I = Handles.begin();
283 }
284 FunctionInfos[Writer].addModRefInfoForGlobal(GV, NewMRI: ModRefInfo::Mod);
285 }
286 ++NumNonAddrTakenGlobalVars;
287
288 // If this global holds a pointer type, see if it is an indirect global.
289 if (GV.getValueType()->isPointerTy() &&
290 AnalyzeIndirectGlobalMemory(GV: &GV))
291 ++NumIndirectGlobalVars;
292 }
293 Readers.clear();
294 Writers.clear();
295 }
296}
297
298/// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer.
299/// If this is used by anything complex (i.e., the address escapes), return
300/// true. Also, while we are at it, keep track of those functions that read and
301/// write to the value.
302///
303/// If OkayStoreDest is non-null, stores into this global are allowed.
304bool GlobalsAAResult::AnalyzeUsesOfPointer(Value *V,
305 SmallPtrSetImpl<Function *> *Readers,
306 SmallPtrSetImpl<Function *> *Writers,
307 GlobalValue *OkayStoreDest) {
308 if (!V->getType()->isPointerTy())
309 return true;
310
311 for (Use &U : V->uses()) {
312 User *I = U.getUser();
313 if (LoadInst *LI = dyn_cast<LoadInst>(Val: I)) {
314 if (Readers)
315 Readers->insert(Ptr: LI->getParent()->getParent());
316 } else if (StoreInst *SI = dyn_cast<StoreInst>(Val: I)) {
317 // Check the pointer operand use of the store.
318 if (&U == &SI->getOperandUse(i: 1)) {
319 if (Writers)
320 Writers->insert(Ptr: SI->getParent()->getParent());
321 } else if (SI->getOperand(i_nocapture: 1) != OkayStoreDest) {
322 return true; // Storing the pointer
323 }
324 } else if (Operator::getOpcode(V: I) == Instruction::GetElementPtr) {
325 if (AnalyzeUsesOfPointer(V: I, Readers, Writers))
326 return true;
327 } else if (Operator::getOpcode(V: I) == Instruction::BitCast ||
328 Operator::getOpcode(V: I) == Instruction::AddrSpaceCast) {
329 if (AnalyzeUsesOfPointer(V: I, Readers, Writers, OkayStoreDest))
330 return true;
331 } else if (auto *Call = dyn_cast<CallBase>(Val: I)) {
332 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Val: I)) {
333 if (II->getIntrinsicID() == Intrinsic::threadlocal_address &&
334 V == II->getArgOperand(i: 0)) {
335 if (AnalyzeUsesOfPointer(V: II, Readers, Writers))
336 return true;
337 continue;
338 }
339 }
340 // Make sure that this is just the function being called, not that it is
341 // passing into the function.
342 if (Call->isDataOperand(U: &U)) {
343 // Detect calls to free.
344 if (Call->isArgOperand(U: &U) &&
345 getFreedOperand(CB: Call, TLI: &GetTLI(*Call->getFunction())) == U) {
346 if (Writers)
347 Writers->insert(Ptr: Call->getParent()->getParent());
348 } else {
349 // In general, we return true for unknown calls, but there are
350 // some simple checks that we can do for functions that
351 // will never call back into the module.
352 auto *F = Call->getCalledFunction();
353 // TODO: we should be able to remove isDeclaration() check
354 // and let the function body analysis check for captures,
355 // and collect the mod-ref effects. This information will
356 // be later propagated via the call graph.
357 if (!F || !F->isDeclaration())
358 return true;
359 // Note that the NoCallback check here is a little bit too
360 // conservative. If there are no captures of the global
361 // in the module, then this call may not be a capture even
362 // if it does not have NoCallback.
363 if (!Call->hasFnAttr(Kind: Attribute::NoCallback) ||
364 !Call->isArgOperand(U: &U) ||
365 !Call->doesNotCapture(OpNo: Call->getArgOperandNo(U: &U)))
366 return true;
367
368 // Conservatively, assume the call reads and writes the global.
369 // We could use memory attributes to make it more precise.
370 if (Readers)
371 Readers->insert(Ptr: Call->getParent()->getParent());
372 if (Writers)
373 Writers->insert(Ptr: Call->getParent()->getParent());
374 }
375 }
376 } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(Val: I)) {
377 if (!isa<ConstantPointerNull>(Val: ICI->getOperand(i_nocapture: 1)))
378 return true; // Allow comparison against null.
379 } else if (Constant *C = dyn_cast<Constant>(Val: I)) {
380 // Ignore constants which don't have any live uses.
381 if (isa<GlobalValue>(Val: C) || C->isConstantUsed())
382 return true;
383 } else {
384 return true;
385 }
386 }
387
388 return false;
389}
390
391/// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable
392/// which holds a pointer type. See if the global always points to non-aliased
393/// heap memory: that is, all initializers of the globals store a value known
394/// to be obtained via a noalias return function call which have no other use.
395/// Further, all loads out of GV must directly use the memory, not store the
396/// pointer somewhere. If this is true, we consider the memory pointed to by
397/// GV to be owned by GV and can disambiguate other pointers from it.
398bool GlobalsAAResult::AnalyzeIndirectGlobalMemory(GlobalVariable *GV) {
399 // Keep track of values related to the allocation of the memory, f.e. the
400 // value produced by the noalias call and any casts.
401 std::vector<Value *> AllocRelatedValues;
402
403 // If the initializer is a non-null pointer, bail.
404 if (Constant *C = GV->getInitializer())
405 if (!isa<ConstantPointerNull>(Val: C))
406 return false;
407
408 // Walk the user list of the global. If we find anything other than a direct
409 // load or store, bail out.
410 for (User *U : GV->users()) {
411 if (LoadInst *LI = dyn_cast<LoadInst>(Val: U)) {
412 // The pointer loaded from the global can only be used in simple ways:
413 // we allow addressing of it and loading storing to it. We do *not* allow
414 // storing the loaded pointer somewhere else or passing to a function.
415 if (AnalyzeUsesOfPointer(V: LI))
416 return false; // Loaded pointer escapes.
417 // TODO: Could try some IP mod/ref of the loaded pointer.
418 } else if (StoreInst *SI = dyn_cast<StoreInst>(Val: U)) {
419 // Storing the global itself.
420 if (SI->getOperand(i_nocapture: 0) == GV)
421 return false;
422
423 // If storing the null pointer, ignore it.
424 if (isa<ConstantPointerNull>(Val: SI->getOperand(i_nocapture: 0)))
425 continue;
426
427 // Check the value being stored.
428 Value *Ptr = getUnderlyingObject(V: SI->getOperand(i_nocapture: 0));
429
430 if (!isNoAliasCall(V: Ptr))
431 return false; // Too hard to analyze.
432
433 // Analyze all uses of the allocation. If any of them are used in a
434 // non-simple way (e.g. stored to another global) bail out.
435 if (AnalyzeUsesOfPointer(V: Ptr, /*Readers*/ nullptr, /*Writers*/ nullptr,
436 OkayStoreDest: GV))
437 return false; // Loaded pointer escapes.
438
439 // Remember that this allocation is related to the indirect global.
440 AllocRelatedValues.push_back(x: Ptr);
441 } else {
442 // Something complex, bail out.
443 return false;
444 }
445 }
446
447 // Okay, this is an indirect global. Remember all of the allocations for
448 // this global in AllocsForIndirectGlobals.
449 while (!AllocRelatedValues.empty()) {
450 AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV;
451 Handles.emplace_front(args&: *this, args&: AllocRelatedValues.back());
452 Handles.front().I = Handles.begin();
453 AllocRelatedValues.pop_back();
454 }
455 IndirectGlobals.insert(Ptr: GV);
456 Handles.emplace_front(args&: *this, args&: GV);
457 Handles.front().I = Handles.begin();
458 return true;
459}
460
461void GlobalsAAResult::CollectSCCMembership(CallGraph &CG) {
462 // We do a bottom-up SCC traversal of the call graph. In other words, we
463 // visit all callees before callers (leaf-first).
464 unsigned SCCID = 0;
465 for (scc_iterator<CallGraph *> I = scc_begin(G: &CG); !I.isAtEnd(); ++I) {
466 const std::vector<CallGraphNode *> &SCC = *I;
467 assert(!SCC.empty() && "SCC with no functions?");
468
469 for (auto *CGN : SCC)
470 if (Function *F = CGN->getFunction())
471 FunctionToSCCMap[F] = SCCID;
472 ++SCCID;
473 }
474}
475
476/// AnalyzeCallGraph - At this point, we know the functions where globals are
477/// immediately stored to and read from. Propagate this information up the call
478/// graph to all callers and compute the mod/ref info for all memory for each
479/// function.
480void GlobalsAAResult::AnalyzeCallGraph(CallGraph &CG, Module &M) {
481 // We do a bottom-up SCC traversal of the call graph. In other words, we
482 // visit all callees before callers (leaf-first).
483 for (scc_iterator<CallGraph *> I = scc_begin(G: &CG); !I.isAtEnd(); ++I) {
484 const std::vector<CallGraphNode *> &SCC = *I;
485 assert(!SCC.empty() && "SCC with no functions?");
486
487 if (any_of(Range: SCC, P: [](CallGraphNode *Node) { return !Node->getFunction(); })) {
488 // Calls externally - can't say anything useful. Remove any existing
489 // function records (may have been created when scanning globals).
490 for (auto *Node : SCC)
491 FunctionInfos.erase(Val: Node->getFunction());
492 continue;
493 }
494
495 Function *FirstF = SCC[0]->getFunction();
496 FunctionInfo &FI = FunctionInfos[FirstF];
497 Handles.emplace_front(args&: *this, args&: FirstF);
498 Handles.front().I = Handles.begin();
499 bool KnowNothing = false;
500
501 // Only the attributes of declarations, functions without an exact
502 // definition (which may be replaced at link time), and optnone functions
503 // can be relied on, not their bodies.
504 auto UseAttributesOnly = [](const Function &F) {
505 return F.isDeclaration() || !F.isDefinitionExact() || F.hasOptNone();
506 };
507
508 // Intrinsics, like any other synchronizing function, can make effects
509 // of other threads visible. Without nosync we know nothing really.
510 // Similarly, if `nocallback` is missing the function, or intrinsic,
511 // can call into the module arbitrarily. If both are set the function
512 // has an effect but will not interact with accesses of internal
513 // globals inside the module. We are conservative here for definitions
514 // (optnone or non-exact), might not be necessary.
515 auto MaySyncOrCallIntoModule = [](const Function &F) {
516 return !F.isDeclaration() || !F.hasNoSync() ||
517 !F.hasFnAttribute(Kind: Attribute::NoCallback);
518 };
519
520 // Collect the mod/ref properties due to called functions. We only compute
521 // one mod-ref set.
522 for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) {
523 Function *F = SCC[i]->getFunction();
524 if (!F) {
525 KnowNothing = true;
526 break;
527 }
528
529 if (UseAttributesOnly(*F)) {
530 // Try to get mod/ref behaviour from function attributes.
531 if (F->doesNotAccessMemory()) {
532 // Can't do better than that!
533 } else if (F->onlyReadsMemory()) {
534 FI.addModRefInfo(NewMRI: ModRefInfo::Ref);
535 if (!F->onlyAccessesArgMemory() && MaySyncOrCallIntoModule(*F))
536 // This function might call back into the module and read a global -
537 // consider every global as possibly being read by this function.
538 FI.setMayReadAnyGlobal();
539 } else {
540 FI.addModRefInfo(NewMRI: ModRefInfo::ModRef);
541 if (!F->onlyAccessesArgMemory())
542 FI.setMayReadAnyGlobal();
543 if (MaySyncOrCallIntoModule(*F)) {
544 KnowNothing = true;
545 break;
546 }
547 }
548 continue;
549 }
550
551 for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end();
552 CI != E && !KnowNothing; ++CI)
553 if (Function *Callee = CI->second->getFunction()) {
554 if (FunctionInfo *CalleeFI = getFunctionInfo(F: Callee)) {
555 // Propagate function effect up.
556 FI.addFunctionInfo(FI: *CalleeFI);
557 } else {
558 // Can't say anything about it. However, if it is inside our SCC,
559 // then nothing needs to be done.
560 CallGraphNode *CalleeNode = CG[Callee];
561 if (!is_contained(Range: SCC, Element: CalleeNode))
562 KnowNothing = true;
563 }
564 } else {
565 KnowNothing = true;
566 }
567 }
568
569 // If we can't say anything useful about this SCC, remove all SCC functions
570 // from the FunctionInfos map.
571 if (KnowNothing) {
572 for (auto *Node : SCC)
573 FunctionInfos.erase(Val: Node->getFunction());
574 continue;
575 }
576
577 // Scan the function bodies for explicit loads or stores.
578 for (auto *Node : SCC) {
579 if (isModAndRefSet(MRI: FI.getModRefInfo()))
580 break; // The mod/ref lattice saturates here.
581
582 // Don't prove any properties based on the body of a function whose body
583 // can't be relied on. Function attributes were already used as a best
584 // approximation above.
585 if (UseAttributesOnly(*Node->getFunction()))
586 continue;
587
588 for (Instruction &I : instructions(F: Node->getFunction())) {
589 if (isModAndRefSet(MRI: FI.getModRefInfo()))
590 break; // The mod/ref lattice saturates here.
591
592 // We handle calls specially because the graph-relevant aspects are
593 // handled above.
594 if (isa<CallBase>(Val: &I))
595 continue;
596
597 // All non-call instructions we use the primary predicates for whether
598 // they read or write memory.
599 if (I.mayReadFromMemory())
600 FI.addModRefInfo(NewMRI: ModRefInfo::Ref);
601 if (I.mayWriteToMemory())
602 FI.addModRefInfo(NewMRI: ModRefInfo::Mod);
603 }
604 }
605
606 if (!isModSet(MRI: FI.getModRefInfo()))
607 ++NumReadMemFunctions;
608 if (!isModOrRefSet(MRI: FI.getModRefInfo()))
609 ++NumNoMemFunctions;
610
611 // Finally, now that we know the full effect on this SCC, clone the
612 // information to each function in the SCC.
613 // FI is a reference into FunctionInfos, so copy it now so that it doesn't
614 // get invalidated if DenseMap decides to re-hash.
615 FunctionInfo CachedFI = FI;
616 for (unsigned i = 1, e = SCC.size(); i != e; ++i)
617 FunctionInfos[SCC[i]->getFunction()] = CachedFI;
618 }
619}
620
621// GV is a non-escaping global. V is a pointer address that has been loaded from.
622// If we can prove that V must escape, we can conclude that a load from V cannot
623// alias GV.
624static bool isNonEscapingGlobalNoAliasWithLoad(const GlobalValue *GV,
625 const Value *V,
626 int &Depth,
627 const DataLayout &DL) {
628 SmallPtrSet<const Value *, 8> Visited;
629 SmallVector<const Value *, 8> Inputs;
630 Visited.insert(Ptr: V);
631 Inputs.push_back(Elt: V);
632 do {
633 const Value *Input = Inputs.pop_back_val();
634
635 if (isa<GlobalValue>(Val: Input) || isa<Argument>(Val: Input) || isa<CallInst>(Val: Input) ||
636 isa<InvokeInst>(Val: Input))
637 // Arguments to functions or returns from functions are inherently
638 // escaping, so we can immediately classify those as not aliasing any
639 // non-addr-taken globals.
640 //
641 // (Transitive) loads from a global are also safe - if this aliased
642 // another global, its address would escape, so no alias.
643 continue;
644
645 // Recurse through a limited number of selects, loads and PHIs. This is an
646 // arbitrary depth of 4, lower numbers could be used to fix compile time
647 // issues if needed, but this is generally expected to be only be important
648 // for small depths.
649 if (++Depth > 4)
650 return false;
651
652 if (auto *LI = dyn_cast<LoadInst>(Val: Input)) {
653 Inputs.push_back(Elt: getUnderlyingObject(V: LI->getPointerOperand()));
654 continue;
655 }
656 if (auto *SI = dyn_cast<SelectInst>(Val: Input)) {
657 const Value *LHS = getUnderlyingObject(V: SI->getTrueValue());
658 const Value *RHS = getUnderlyingObject(V: SI->getFalseValue());
659 if (Visited.insert(Ptr: LHS).second)
660 Inputs.push_back(Elt: LHS);
661 if (Visited.insert(Ptr: RHS).second)
662 Inputs.push_back(Elt: RHS);
663 continue;
664 }
665 if (auto *PN = dyn_cast<PHINode>(Val: Input)) {
666 for (const Value *Op : PN->incoming_values()) {
667 Op = getUnderlyingObject(V: Op);
668 if (Visited.insert(Ptr: Op).second)
669 Inputs.push_back(Elt: Op);
670 }
671 continue;
672 }
673
674 return false;
675 } while (!Inputs.empty());
676
677 // All inputs were known to be no-alias.
678 return true;
679}
680
681// There are particular cases where we can conclude no-alias between
682// a non-addr-taken global and some other underlying object. Specifically,
683// a non-addr-taken global is known to not be escaped from any function. It is
684// also incorrect for a transformation to introduce an escape of a global in
685// a way that is observable when it was not there previously. One function
686// being transformed to introduce an escape which could possibly be observed
687// (via loading from a global or the return value for example) within another
688// function is never safe. If the observation is made through non-atomic
689// operations on different threads, it is a data-race and UB. If the
690// observation is well defined, by being observed the transformation would have
691// changed program behavior by introducing the observed escape, making it an
692// invalid transform.
693//
694// This property does require that transformations which *temporarily* escape
695// a global that was not previously escaped, prior to restoring it, cannot rely
696// on the results of GMR::alias. This seems a reasonable restriction, although
697// currently there is no way to enforce it. There is also no realistic
698// optimization pass that would make this mistake. The closest example is
699// a transformation pass which does reg2mem of SSA values but stores them into
700// global variables temporarily before restoring the global variable's value.
701// This could be useful to expose "benign" races for example. However, it seems
702// reasonable to require that a pass which introduces escapes of global
703// variables in this way to either not trust AA results while the escape is
704// active, or to be forced to operate as a module pass that cannot co-exist
705// with an alias analysis such as GMR.
706bool GlobalsAAResult::isNonEscapingGlobalNoAlias(const GlobalValue *GV,
707 const Value *V,
708 const Instruction *CtxI) {
709 // In order to know that the underlying object cannot alias the
710 // non-addr-taken global, we must know that it would have to be an escape.
711 // Thus if the underlying object is a function argument, a load from
712 // a global, or the return of a function, it cannot alias. We can also
713 // recurse through PHI nodes and select nodes provided all of their inputs
714 // resolve to one of these known-escaping roots.
715
716 // A non-addr-taken global cannot alias with any non-pointer value.
717 // Check this early and exit.
718 if (!V->getType()->isPointerTy())
719 return true;
720
721 SmallPtrSet<const Value *, 8> Visited;
722 SmallVector<const Value *, 8> Inputs;
723 Visited.insert(Ptr: V);
724 Inputs.push_back(Elt: V);
725 int Depth = 0;
726 do {
727 const Value *Input = Inputs.pop_back_val();
728
729 if (auto *InputGV = dyn_cast<GlobalValue>(Val: Input)) {
730 // If one input is the very global we're querying against, then we can't
731 // conclude no-alias.
732 if (InputGV == GV)
733 return false;
734
735 // Distinct GlobalVariables never alias, unless overriden or zero-sized.
736 // FIXME: The condition can be refined, but be conservative for now.
737 auto *GVar = dyn_cast<GlobalVariable>(Val: GV);
738 auto *InputGVar = dyn_cast<GlobalVariable>(Val: InputGV);
739 if (GVar && InputGVar &&
740 !GVar->isDeclaration() && !InputGVar->isDeclaration() &&
741 !GVar->isInterposable() && !InputGVar->isInterposable()) {
742 Type *GVType = GVar->getInitializer()->getType();
743 Type *InputGVType = InputGVar->getInitializer()->getType();
744 if (GVType->isSized() && InputGVType->isSized() &&
745 (DL.getTypeAllocSize(Ty: GVType) > 0) &&
746 (DL.getTypeAllocSize(Ty: InputGVType) > 0))
747 continue;
748 }
749
750 // Conservatively return false, even though we could be smarter
751 // (e.g. look through GlobalAliases).
752 return false;
753 }
754
755 if (isa<Argument>(Val: Input) || isa<CallInst>(Val: Input) ||
756 isa<InvokeInst>(Val: Input)) {
757 // Arguments to functions or returns from functions are inherently
758 // escaping, so we can immediately classify those as not aliasing any
759 // non-addr-taken globals.
760 continue;
761 }
762
763 if (CtxI)
764 if (auto *CPN = dyn_cast<ConstantPointerNull>(Val: Input)) {
765 // Null pointer cannot alias with a non-addr-taken global.
766 const Function *F = CtxI->getFunction();
767 if (!NullPointerIsDefined(F, AS: CPN->getPointerType()->getAddressSpace()))
768 continue;
769 }
770
771 // Recurse through a limited number of selects, loads and PHIs. This is an
772 // arbitrary depth of 4, lower numbers could be used to fix compile time
773 // issues if needed, but this is generally expected to be only be important
774 // for small depths.
775 if (++Depth > 4)
776 return false;
777
778 if (auto *LI = dyn_cast<LoadInst>(Val: Input)) {
779 // A pointer loaded from a global would have been captured, and we know
780 // that the global is non-escaping, so no alias.
781 const Value *Ptr = getUnderlyingObject(V: LI->getPointerOperand());
782 if (isNonEscapingGlobalNoAliasWithLoad(GV, V: Ptr, Depth, DL))
783 // The load does not alias with GV.
784 continue;
785 // Otherwise, a load could come from anywhere, so bail.
786 return false;
787 }
788 if (auto *SI = dyn_cast<SelectInst>(Val: Input)) {
789 const Value *LHS = getUnderlyingObject(V: SI->getTrueValue());
790 const Value *RHS = getUnderlyingObject(V: SI->getFalseValue());
791 if (Visited.insert(Ptr: LHS).second)
792 Inputs.push_back(Elt: LHS);
793 if (Visited.insert(Ptr: RHS).second)
794 Inputs.push_back(Elt: RHS);
795 continue;
796 }
797 if (auto *PN = dyn_cast<PHINode>(Val: Input)) {
798 for (const Value *Op : PN->incoming_values()) {
799 Op = getUnderlyingObject(V: Op);
800 if (Visited.insert(Ptr: Op).second)
801 Inputs.push_back(Elt: Op);
802 }
803 continue;
804 }
805
806 // FIXME: It would be good to handle other obvious no-alias cases here, but
807 // it isn't clear how to do so reasonably without building a small version
808 // of BasicAA into this code.
809 return false;
810 } while (!Inputs.empty());
811
812 // If all the inputs to V were definitively no-alias, then V is no-alias.
813 return true;
814}
815
816bool GlobalsAAResult::invalidate(Module &, const PreservedAnalyses &PA,
817 ModuleAnalysisManager::Invalidator &) {
818 // Check whether the analysis has been explicitly invalidated. Otherwise, it's
819 // stateless and remains preserved.
820 auto PAC = PA.getChecker<GlobalsAA>();
821 return !PAC.preservedWhenStateless();
822}
823
824/// alias - If one of the pointers is to a global that we are tracking, and the
825/// other is some random pointer, we know there cannot be an alias, because the
826/// address of the global isn't taken.
827AliasResult GlobalsAAResult::alias(const MemoryLocation &LocA,
828 const MemoryLocation &LocB,
829 AAQueryInfo &AAQI, const Instruction *CtxI) {
830 // Get the base object these pointers point to.
831 const Value *UV1 =
832 getUnderlyingObject(V: LocA.Ptr->stripPointerCastsForAliasAnalysis());
833 const Value *UV2 =
834 getUnderlyingObject(V: LocB.Ptr->stripPointerCastsForAliasAnalysis());
835
836 // If either of the underlying values is a global, they may be non-addr-taken
837 // globals, which we can answer queries about.
838 const GlobalValue *GV1 = dyn_cast<GlobalValue>(Val: UV1);
839 const GlobalValue *GV2 = dyn_cast<GlobalValue>(Val: UV2);
840 if (GV1 || GV2) {
841 // If the global's address is taken, pretend we don't know it's a pointer to
842 // the global.
843 if (GV1 && !NonAddressTakenGlobals.count(Ptr: GV1))
844 GV1 = nullptr;
845 if (GV2 && !NonAddressTakenGlobals.count(Ptr: GV2))
846 GV2 = nullptr;
847
848 // If the two pointers are derived from two different non-addr-taken
849 // globals we know these can't alias.
850 if (GV1 && GV2 && GV1 != GV2)
851 return AliasResult::NoAlias;
852
853 // If one is and the other isn't, it isn't strictly safe but we can fake
854 // this result if necessary for performance. This does not appear to be
855 // a common problem in practice.
856 if ((GV1 || GV2) && GV1 != GV2 &&
857 Opts.enable_unsafe_globalsmodref_alias_results)
858 return AliasResult::NoAlias;
859
860 // Check for a special case where a non-escaping global can be used to
861 // conclude no-alias.
862 if ((GV1 || GV2) && GV1 != GV2) {
863 const GlobalValue *GV = GV1 ? GV1 : GV2;
864 const Value *UV = GV1 ? UV2 : UV1;
865 if (isNonEscapingGlobalNoAlias(GV, V: UV, CtxI))
866 return AliasResult::NoAlias;
867 }
868
869 // Otherwise if they are both derived from the same addr-taken global, we
870 // can't know the two accesses don't overlap.
871 }
872
873 // These pointers may be based on the memory owned by an indirect global. If
874 // so, we may be able to handle this. First check to see if the base pointer
875 // is a direct load from an indirect global.
876 GV1 = GV2 = nullptr;
877 if (const LoadInst *LI = dyn_cast<LoadInst>(Val: UV1))
878 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Val: LI->getOperand(i_nocapture: 0)))
879 if (IndirectGlobals.count(Ptr: GV))
880 GV1 = GV;
881 if (const LoadInst *LI = dyn_cast<LoadInst>(Val: UV2))
882 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val: LI->getOperand(i_nocapture: 0)))
883 if (IndirectGlobals.count(Ptr: GV))
884 GV2 = GV;
885
886 // These pointers may also be from an allocation for the indirect global. If
887 // so, also handle them.
888 if (!GV1)
889 GV1 = AllocsForIndirectGlobals.lookup(Val: UV1);
890 if (!GV2)
891 GV2 = AllocsForIndirectGlobals.lookup(Val: UV2);
892
893 // Now that we know whether the two pointers are related to indirect globals,
894 // use this to disambiguate the pointers. If the pointers are based on
895 // different indirect globals they cannot alias.
896 if (GV1 && GV2 && GV1 != GV2)
897 return AliasResult::NoAlias;
898
899 // If one is based on an indirect global and the other isn't, it isn't
900 // strictly safe but we can fake this result if necessary for performance.
901 // This does not appear to be a common problem in practice.
902 if ((GV1 || GV2) && GV1 != GV2 &&
903 Opts.enable_unsafe_globalsmodref_alias_results)
904 return AliasResult::NoAlias;
905
906 return AliasResult::MayAlias;
907}
908
909ModRefInfo GlobalsAAResult::getModRefInfoForArgument(const CallBase *Call,
910 const GlobalValue *GV,
911 AAQueryInfo &AAQI) {
912 if (Call->doesNotAccessMemory())
913 return ModRefInfo::NoModRef;
914 ModRefInfo ConservativeResult =
915 Call->onlyReadsMemory() ? ModRefInfo::Ref : ModRefInfo::ModRef;
916
917 // Iterate through all the arguments to the called function. If any argument
918 // is based on GV, return the conservative result.
919 for (const auto &A : Call->args()) {
920 SmallVector<const Value*, 4> Objects;
921 getUnderlyingObjects(V: A, Objects);
922
923 // All objects must be identified.
924 if (!all_of(Range&: Objects, P: isIdentifiedObject) &&
925 // Try ::alias to see if all objects are known not to alias GV.
926 !all_of(Range&: Objects, P: [&](const Value *V) {
927 return this->alias(LocA: MemoryLocation::getBeforeOrAfter(Ptr: V),
928 LocB: MemoryLocation::getBeforeOrAfter(Ptr: GV), AAQI,
929 CtxI: Call) == AliasResult::NoAlias;
930 }))
931 return ConservativeResult;
932
933 if (is_contained(Range&: Objects, Element: GV))
934 return ConservativeResult;
935 }
936
937 // We identified all objects in the argument list, and none of them were GV.
938 return ModRefInfo::NoModRef;
939}
940
941ModRefInfo GlobalsAAResult::getModRefInfo(const CallBase *Call,
942 const MemoryLocation &Loc,
943 AAQueryInfo &AAQI) {
944 ModRefInfo Known = ModRefInfo::ModRef;
945
946 // If we are asking for mod/ref info of a direct call with a pointer to a
947 // global we are tracking, return information if we have it.
948 if (const GlobalValue *GV =
949 dyn_cast<GlobalValue>(Val: getUnderlyingObject(V: Loc.Ptr)))
950 // If GV is internal to this IR and there is no function with local linkage
951 // that has had their address taken, keep looking for a tighter ModRefInfo.
952 if (GV->hasLocalLinkage() && !UnknownFunctionsWithLocalLinkage)
953 if (const Function *F = Call->getCalledFunction())
954 if (NonAddressTakenGlobals.count(Ptr: GV))
955 if (const FunctionInfo *FI = getFunctionInfo(F))
956 Known = FI->getModRefInfoForGlobal(GV: *GV) |
957 getModRefInfoForArgument(Call, GV, AAQI);
958
959 return Known;
960}
961
962GlobalsAAResult::GlobalsAAResult(
963 const AnalysisOptions &Opts, const DataLayout &DL,
964 std::function<const TargetLibraryInfo &(Function &F)> GetTLI)
965 : Opts(Opts), DL(DL), GetTLI(std::move(GetTLI)) {}
966
967GlobalsAAResult::GlobalsAAResult(GlobalsAAResult &&Arg)
968 : AAResultBase(std::move(Arg)), Opts(Arg.Opts), DL(Arg.DL),
969 GetTLI(std::move(Arg.GetTLI)),
970 NonAddressTakenGlobals(std::move(Arg.NonAddressTakenGlobals)),
971 IndirectGlobals(std::move(Arg.IndirectGlobals)),
972 AllocsForIndirectGlobals(std::move(Arg.AllocsForIndirectGlobals)),
973 FunctionInfos(std::move(Arg.FunctionInfos)),
974 Handles(std::move(Arg.Handles)) {
975 // Update the parent for each DeletionCallbackHandle.
976 for (auto &H : Handles) {
977 assert(H.GAR == &Arg);
978 H.GAR = this;
979 }
980}
981
982GlobalsAAResult::~GlobalsAAResult() = default;
983
984/*static*/ GlobalsAAResult GlobalsAAResult::analyzeModule(
985 Module &M, std::function<const TargetLibraryInfo &(Function &F)> GetTLI,
986 CallGraph &CG) {
987 GlobalsAAResult Result(getAnalysisOptions(M), M.getDataLayout(), GetTLI);
988
989 // Discover which functions aren't recursive, to feed into AnalyzeGlobals.
990 Result.CollectSCCMembership(CG);
991
992 // Find non-addr taken globals.
993 Result.AnalyzeGlobals(M);
994
995 // Propagate on CG.
996 Result.AnalyzeCallGraph(CG, M);
997
998 return Result;
999}
1000
1001AnalysisKey GlobalsAA::Key;
1002
1003GlobalsAAResult GlobalsAA::run(Module &M, ModuleAnalysisManager &AM) {
1004 FunctionAnalysisManager &FAM =
1005 AM.getResult<FunctionAnalysisManagerModuleProxy>(IR&: M).getManager();
1006 auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
1007 return FAM.getResult<TargetLibraryAnalysis>(IR&: F);
1008 };
1009 return GlobalsAAResult::analyzeModule(M, GetTLI,
1010 CG&: AM.getResult<CallGraphAnalysis>(IR&: M));
1011}
1012
1013PreservedAnalyses RecomputeGlobalsAAPass::run(Module &M,
1014 ModuleAnalysisManager &AM) {
1015 if (auto *G = AM.getCachedResult<GlobalsAA>(IR&: M)) {
1016 auto &CG = AM.getResult<CallGraphAnalysis>(IR&: M);
1017 G->NonAddressTakenGlobals.clear();
1018 G->UnknownFunctionsWithLocalLinkage = false;
1019 G->IndirectGlobals.clear();
1020 G->AllocsForIndirectGlobals.clear();
1021 G->FunctionInfos.clear();
1022 G->FunctionToSCCMap.clear();
1023 G->Handles.clear();
1024 G->CollectSCCMembership(CG);
1025 G->AnalyzeGlobals(M);
1026 G->AnalyzeCallGraph(CG, M);
1027 }
1028 return PreservedAnalyses::all();
1029}
1030
1031char GlobalsAAWrapperPass::ID = 0;
1032INITIALIZE_PASS_BEGIN(GlobalsAAWrapperPass, "globals-aa",
1033 "Globals Alias Analysis", false, true)
1034INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1035INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
1036INITIALIZE_PASS_END(GlobalsAAWrapperPass, "globals-aa",
1037 "Globals Alias Analysis", false, true)
1038
1039ModulePass *llvm::createGlobalsAAWrapperPass() {
1040 return new GlobalsAAWrapperPass();
1041}
1042
1043GlobalsAAWrapperPass::GlobalsAAWrapperPass() : ModulePass(ID) {}
1044
1045bool GlobalsAAWrapperPass::runOnModule(Module &M) {
1046 auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {
1047 return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
1048 };
1049 Result.reset(p: new GlobalsAAResult(GlobalsAAResult::analyzeModule(
1050 M, GetTLI, CG&: getAnalysis<CallGraphWrapperPass>().getCallGraph())));
1051 return false;
1052}
1053
1054bool GlobalsAAWrapperPass::doFinalization(Module &M) {
1055 Result.reset();
1056 return false;
1057}
1058
1059void GlobalsAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
1060 AU.setPreservesAll();
1061 AU.addRequired<CallGraphWrapperPass>();
1062 AU.addRequired<TargetLibraryInfoWrapperPass>();
1063}
1064