5482a13d1e780c2854c56687db0ab4234ec46e69
[oota-llvm.git] / lib / Analysis / GlobalsModRef.cpp
1 //===- GlobalsModRef.cpp - Simple Mod/Ref Analysis for Globals ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This simple pass provides alias and mod/ref information for global values
11 // 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.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Analysis/GlobalsModRef.h"
18 #include "llvm/ADT/SCCIterator.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/Analysis/MemoryBuiltins.h"
22 #include "llvm/Analysis/ValueTracking.h"
23 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/InstIterator.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/Pass.h"
29 #include "llvm/Support/CommandLine.h"
30 using namespace llvm;
31
32 #define DEBUG_TYPE "globalsmodref-aa"
33
34 STATISTIC(NumNonAddrTakenGlobalVars,
35           "Number of global vars without address taken");
36 STATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken");
37 STATISTIC(NumNoMemFunctions, "Number of functions that do not access memory");
38 STATISTIC(NumReadMemFunctions, "Number of functions that only read memory");
39 STATISTIC(NumIndirectGlobalVars, "Number of indirect global objects");
40
41 // An option to enable unsafe alias results from the GlobalsModRef analysis.
42 // When enabled, GlobalsModRef will provide no-alias results which in extremely
43 // rare cases may not be conservatively correct. In particular, in the face of
44 // transforms which cause assymetry between how effective GetUnderlyingObject
45 // is for two pointers, it may produce incorrect results.
46 //
47 // These unsafe results have been returned by GMR for many years without
48 // causing significant issues in the wild and so we provide a mechanism to
49 // re-enable them for users of LLVM that have a particular performance
50 // sensitivity and no known issues. The option also makes it easy to evaluate
51 // the performance impact of these results.
52 static cl::opt<bool> EnableUnsafeGlobalsModRefAliasResults(
53     "enable-unsafe-globalsmodref-alias-results", cl::init(false), cl::Hidden);
54
55 /// The mod/ref information collected for a particular function.
56 ///
57 /// We collect information about mod/ref behavior of a function here, both in
58 /// general and as pertains to specific globals. We only have this detailed
59 /// information when we know *something* useful about the behavior. If we
60 /// saturate to fully general mod/ref, we remove the info for the function.
61 class GlobalsModRef::FunctionInfo {
62   typedef SmallDenseMap<const GlobalValue *, ModRefInfo, 16> GlobalInfoMapType;
63
64   /// Build a wrapper struct that has 8-byte alignment. All heap allocations
65   /// should provide this much alignment at least, but this makes it clear we
66   /// specifically rely on this amount of alignment.
67   struct LLVM_ALIGNAS(8) AlignedMap {
68     AlignedMap() {}
69     AlignedMap(const AlignedMap &Arg) : Map(Arg.Map) {}
70     GlobalInfoMapType Map;
71   };
72
73   /// Pointer traits for our aligned map.
74   struct AlignedMapPointerTraits {
75     static inline void *getAsVoidPointer(AlignedMap *P) { return P; }
76     static inline AlignedMap *getFromVoidPointer(void *P) {
77       return (AlignedMap *)P;
78     }
79     enum { NumLowBitsAvailable = 3 };
80     static_assert(AlignOf<AlignedMap>::Alignment >= (1 << NumLowBitsAvailable),
81                   "AlignedMap insufficiently aligned to have enough low bits.");
82   };
83
84   /// The bit that flags that this function may read any global. This is
85   /// chosen to mix together with ModRefInfo bits.
86   enum { MayReadAnyGlobal = 4 };
87
88   /// Checks to document the invariants of the bit packing here.
89   static_assert((MayReadAnyGlobal & MRI_ModRef) == 0,
90                 "ModRef and the MayReadAnyGlobal flag bits overlap.");
91   static_assert(((MayReadAnyGlobal | MRI_ModRef) >>
92                  AlignedMapPointerTraits::NumLowBitsAvailable) == 0,
93                 "Insufficient low bits to store our flag and ModRef info.");
94
95 public:
96   FunctionInfo() : Info() {}
97   ~FunctionInfo() {
98     delete Info.getPointer();
99   }
100   // Spell out the copy ond move constructors and assignment operators to get
101   // deep copy semantics and correct move semantics in the face of the
102   // pointer-int pair.
103   FunctionInfo(const FunctionInfo &Arg)
104       : Info(nullptr, Arg.Info.getInt()) {
105     if (const auto *ArgPtr = Arg.Info.getPointer())
106       Info.setPointer(new AlignedMap(*ArgPtr));
107   }
108   FunctionInfo(FunctionInfo &&Arg)
109       : Info(Arg.Info.getPointer(), Arg.Info.getInt()) {
110     Arg.Info.setPointerAndInt(nullptr, 0);
111   }
112   FunctionInfo &operator=(const FunctionInfo &RHS) {
113     delete Info.getPointer();
114     Info.setPointerAndInt(nullptr, RHS.Info.getInt());
115     if (const auto *RHSPtr = RHS.Info.getPointer())
116       Info.setPointer(new AlignedMap(*RHSPtr));
117     return *this;
118   }
119   FunctionInfo &operator=(FunctionInfo &&RHS) {
120     delete Info.getPointer();
121     Info.setPointerAndInt(RHS.Info.getPointer(), RHS.Info.getInt());
122     RHS.Info.setPointerAndInt(nullptr, 0);
123     return *this;
124   }
125
126   /// Returns the \c ModRefInfo info for this function.
127   ModRefInfo getModRefInfo() const {
128     return ModRefInfo(Info.getInt() & MRI_ModRef);
129   }
130
131   /// Adds new \c ModRefInfo for this function to its state.
132   void addModRefInfo(ModRefInfo NewMRI) {
133     Info.setInt(Info.getInt() | NewMRI);
134   }
135
136   /// Returns whether this function may read any global variable, and we don't
137   /// know which global.
138   bool mayReadAnyGlobal() const { return Info.getInt() & MayReadAnyGlobal; }
139
140   /// Sets this function as potentially reading from any global.
141   void setMayReadAnyGlobal() { Info.setInt(Info.getInt() | MayReadAnyGlobal); }
142
143   /// Returns the \c ModRefInfo info for this function w.r.t. a particular
144   /// global, which may be more precise than the general information above.
145   ModRefInfo getModRefInfoForGlobal(const GlobalValue &GV) const {
146     ModRefInfo GlobalMRI = mayReadAnyGlobal() ? MRI_Ref : MRI_NoModRef;
147     if (AlignedMap *P = Info.getPointer()) {
148       auto I = P->Map.find(&GV);
149       if (I != P->Map.end())
150         GlobalMRI = ModRefInfo(GlobalMRI | I->second);
151     }
152     return GlobalMRI;
153   }
154
155   /// Add mod/ref info from another function into ours, saturating towards
156   /// MRI_ModRef.
157   void addFunctionInfo(const FunctionInfo &FI) {
158     addModRefInfo(FI.getModRefInfo());
159
160     if (FI.mayReadAnyGlobal())
161       setMayReadAnyGlobal();
162
163     if (AlignedMap *P = FI.Info.getPointer())
164       for (const auto &G : P->Map)
165         addModRefInfoForGlobal(*G.first, G.second);
166   }
167
168   void addModRefInfoForGlobal(const GlobalValue &GV, ModRefInfo NewMRI) {
169     AlignedMap *P = Info.getPointer();
170     if (!P) {
171       P = new AlignedMap();
172       Info.setPointer(P);
173     }
174     auto &GlobalMRI = P->Map[&GV];
175     GlobalMRI = ModRefInfo(GlobalMRI | NewMRI);
176   }
177
178   /// Clear a global's ModRef info. Should be used when a global is being
179   /// deleted.
180   void eraseModRefInfoForGlobal(const GlobalValue &GV) {
181     if (AlignedMap *P = Info.getPointer())
182       P->Map.erase(&GV);
183   }
184
185 private:
186   /// All of the information is encoded into a single pointer, with a three bit
187   /// integer in the low three bits. The high bit provides a flag for when this
188   /// function may read any global. The low two bits are the ModRefInfo. And
189   /// the pointer, when non-null, points to a map from GlobalValue to
190   /// ModRefInfo specific to that GlobalValue.
191   PointerIntPair<AlignedMap *, 3, unsigned, AlignedMapPointerTraits> Info;
192 };
193
194 void GlobalsModRef::DeletionCallbackHandle::deleted() {
195   Value *V = getValPtr();
196   if (auto *F = dyn_cast<Function>(V))
197     GMR.FunctionInfos.erase(F);
198
199   if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
200     if (GMR.NonAddressTakenGlobals.erase(GV)) {
201       // This global might be an indirect global.  If so, remove it and
202       // remove any AllocRelatedValues for it.
203       if (GMR.IndirectGlobals.erase(GV)) {
204         // Remove any entries in AllocsForIndirectGlobals for this global.
205         for (auto I = GMR.AllocsForIndirectGlobals.begin(),
206                   E = GMR.AllocsForIndirectGlobals.end();
207              I != E; ++I)
208           if (I->second == GV)
209             GMR.AllocsForIndirectGlobals.erase(I);
210       }
211
212       // Scan the function info we have collected and remove this global
213       // from all of them.
214       for (auto &FIPair : GMR.FunctionInfos)
215         FIPair.second.eraseModRefInfoForGlobal(*GV);
216     }
217   }
218
219   // If this is an allocation related to an indirect global, remove it.
220   GMR.AllocsForIndirectGlobals.erase(V);
221
222   // And clear out the handle.
223   setValPtr(nullptr);
224   GMR.Handles.erase(I);
225   // This object is now destroyed!
226 }
227
228 char GlobalsModRef::ID = 0;
229 INITIALIZE_AG_PASS_BEGIN(GlobalsModRef, AliasAnalysis, "globalsmodref-aa",
230                          "Simple mod/ref analysis for globals", false, true,
231                          false)
232 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
233 INITIALIZE_AG_PASS_END(GlobalsModRef, AliasAnalysis, "globalsmodref-aa",
234                        "Simple mod/ref analysis for globals", false, true,
235                        false)
236
237 Pass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); }
238
239 GlobalsModRef::GlobalsModRef() : ModulePass(ID) {
240   initializeGlobalsModRefPass(*PassRegistry::getPassRegistry());
241 }
242
243 FunctionModRefBehavior GlobalsModRef::getModRefBehavior(const Function *F) {
244   FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
245
246   if (FunctionInfo *FI = getFunctionInfo(F)) {
247     if (FI->getModRefInfo() == MRI_NoModRef)
248       Min = FMRB_DoesNotAccessMemory;
249     else if ((FI->getModRefInfo() & MRI_Mod) == 0)
250       Min = FMRB_OnlyReadsMemory;
251   }
252
253   return FunctionModRefBehavior(AliasAnalysis::getModRefBehavior(F) & Min);
254 }
255
256 FunctionModRefBehavior GlobalsModRef::getModRefBehavior(ImmutableCallSite CS) {
257   FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
258
259   if (const Function *F = CS.getCalledFunction())
260     if (FunctionInfo *FI = getFunctionInfo(F)) {
261       if (FI->getModRefInfo() == MRI_NoModRef)
262         Min = FMRB_DoesNotAccessMemory;
263       else if ((FI->getModRefInfo() & MRI_Mod) == 0)
264         Min = FMRB_OnlyReadsMemory;
265     }
266
267   return FunctionModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min);
268 }
269
270 /// Returns the function info for the function, or null if we don't have
271 /// anything useful to say about it.
272 GlobalsModRef::FunctionInfo *GlobalsModRef::getFunctionInfo(const Function *F) {
273   auto I = FunctionInfos.find(F);
274   if (I != FunctionInfos.end())
275     return &I->second;
276   return nullptr;
277 }
278
279 /// AnalyzeGlobals - Scan through the users of all of the internal
280 /// GlobalValue's in the program.  If none of them have their "address taken"
281 /// (really, their address passed to something nontrivial), record this fact,
282 /// and record the functions that they are used directly in.
283 void GlobalsModRef::AnalyzeGlobals(Module &M) {
284   SmallPtrSet<Function *, 64> TrackedFunctions;
285   for (Function &F : M)
286     if (F.hasLocalLinkage())
287       if (!AnalyzeUsesOfPointer(&F)) {
288         // Remember that we are tracking this global.
289         NonAddressTakenGlobals.insert(&F);
290         TrackedFunctions.insert(&F);
291         Handles.emplace_front(*this, &F);
292         Handles.front().I = Handles.begin();
293         ++NumNonAddrTakenFunctions;
294       }
295
296   SmallPtrSet<Function *, 64> Readers, Writers;
297   for (GlobalVariable &GV : M.globals())
298     if (GV.hasLocalLinkage()) {
299       if (!AnalyzeUsesOfPointer(&GV, &Readers,
300                                 GV.isConstant() ? nullptr : &Writers)) {
301         // Remember that we are tracking this global, and the mod/ref fns
302         NonAddressTakenGlobals.insert(&GV);
303         Handles.emplace_front(*this, &GV);
304         Handles.front().I = Handles.begin();
305
306         for (Function *Reader : Readers) {
307           if (TrackedFunctions.insert(Reader).second) {
308             Handles.emplace_front(*this, Reader);
309             Handles.front().I = Handles.begin();
310           }
311           FunctionInfos[Reader].addModRefInfoForGlobal(GV, MRI_Ref);
312         }
313
314         if (!GV.isConstant()) // No need to keep track of writers to constants
315           for (Function *Writer : Writers) {
316             if (TrackedFunctions.insert(Writer).second) {
317               Handles.emplace_front(*this, Writer);
318               Handles.front().I = Handles.begin();
319             }
320             FunctionInfos[Writer].addModRefInfoForGlobal(GV, MRI_Mod);
321           }
322         ++NumNonAddrTakenGlobalVars;
323
324         // If this global holds a pointer type, see if it is an indirect global.
325         if (GV.getType()->getElementType()->isPointerTy() &&
326             AnalyzeIndirectGlobalMemory(&GV))
327           ++NumIndirectGlobalVars;
328       }
329       Readers.clear();
330       Writers.clear();
331     }
332 }
333
334 /// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer.
335 /// If this is used by anything complex (i.e., the address escapes), return
336 /// true.  Also, while we are at it, keep track of those functions that read and
337 /// write to the value.
338 ///
339 /// If OkayStoreDest is non-null, stores into this global are allowed.
340 bool GlobalsModRef::AnalyzeUsesOfPointer(Value *V,
341                                          SmallPtrSetImpl<Function *> *Readers,
342                                          SmallPtrSetImpl<Function *> *Writers,
343                                          GlobalValue *OkayStoreDest) {
344   if (!V->getType()->isPointerTy())
345     return true;
346
347   for (Use &U : V->uses()) {
348     User *I = U.getUser();
349     if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
350       if (Readers)
351         Readers->insert(LI->getParent()->getParent());
352     } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
353       if (V == SI->getOperand(1)) {
354         if (Writers)
355           Writers->insert(SI->getParent()->getParent());
356       } else if (SI->getOperand(1) != OkayStoreDest) {
357         return true; // Storing the pointer
358       }
359     } else if (Operator::getOpcode(I) == Instruction::GetElementPtr) {
360       if (AnalyzeUsesOfPointer(I, Readers, Writers))
361         return true;
362     } else if (Operator::getOpcode(I) == Instruction::BitCast) {
363       if (AnalyzeUsesOfPointer(I, Readers, Writers, OkayStoreDest))
364         return true;
365     } else if (auto CS = CallSite(I)) {
366       // Make sure that this is just the function being called, not that it is
367       // passing into the function.
368       if (!CS.isCallee(&U)) {
369         // Detect calls to free.
370         if (isFreeCall(I, TLI)) {
371           if (Writers)
372             Writers->insert(CS->getParent()->getParent());
373         } else {
374           return true; // Argument of an unknown call.
375         }
376       }
377     } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) {
378       if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
379         return true; // Allow comparison against null.
380     } else {
381       return true;
382     }
383   }
384
385   return false;
386 }
387
388 /// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable
389 /// which holds a pointer type.  See if the global always points to non-aliased
390 /// heap memory: that is, all initializers of the globals are allocations, and
391 /// those allocations have no use other than initialization of the global.
392 /// Further, all loads out of GV must directly use the memory, not store the
393 /// pointer somewhere.  If this is true, we consider the memory pointed to by
394 /// GV to be owned by GV and can disambiguate other pointers from it.
395 bool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) {
396   // Keep track of values related to the allocation of the memory, f.e. the
397   // value produced by the malloc call and any casts.
398   std::vector<Value *> AllocRelatedValues;
399
400   // Walk the user list of the global.  If we find anything other than a direct
401   // load or store, bail out.
402   for (User *U : GV->users()) {
403     if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
404       // The pointer loaded from the global can only be used in simple ways:
405       // we allow addressing of it and loading storing to it.  We do *not* allow
406       // storing the loaded pointer somewhere else or passing to a function.
407       if (AnalyzeUsesOfPointer(LI))
408         return false; // Loaded pointer escapes.
409       // TODO: Could try some IP mod/ref of the loaded pointer.
410     } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
411       // Storing the global itself.
412       if (SI->getOperand(0) == GV)
413         return false;
414
415       // If storing the null pointer, ignore it.
416       if (isa<ConstantPointerNull>(SI->getOperand(0)))
417         continue;
418
419       // Check the value being stored.
420       Value *Ptr = GetUnderlyingObject(SI->getOperand(0),
421                                        GV->getParent()->getDataLayout());
422
423       if (!isAllocLikeFn(Ptr, TLI))
424         return false; // Too hard to analyze.
425
426       // Analyze all uses of the allocation.  If any of them are used in a
427       // non-simple way (e.g. stored to another global) bail out.
428       if (AnalyzeUsesOfPointer(Ptr, /*Readers*/ nullptr, /*Writers*/ nullptr,
429                                GV))
430         return false; // Loaded pointer escapes.
431
432       // Remember that this allocation is related to the indirect global.
433       AllocRelatedValues.push_back(Ptr);
434     } else {
435       // Something complex, bail out.
436       return false;
437     }
438   }
439
440   // Okay, this is an indirect global.  Remember all of the allocations for
441   // this global in AllocsForIndirectGlobals.
442   while (!AllocRelatedValues.empty()) {
443     AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV;
444     Handles.emplace_front(*this, AllocRelatedValues.back());
445     Handles.front().I = Handles.begin();
446     AllocRelatedValues.pop_back();
447   }
448   IndirectGlobals.insert(GV);
449   Handles.emplace_front(*this, GV);
450   Handles.front().I = Handles.begin();
451   return true;
452 }
453
454 /// AnalyzeCallGraph - At this point, we know the functions where globals are
455 /// immediately stored to and read from.  Propagate this information up the call
456 /// graph to all callers and compute the mod/ref info for all memory for each
457 /// function.
458 void GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) {
459   // We do a bottom-up SCC traversal of the call graph.  In other words, we
460   // visit all callees before callers (leaf-first).
461   for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
462     const std::vector<CallGraphNode *> &SCC = *I;
463     assert(!SCC.empty() && "SCC with no functions?");
464
465     if (!SCC[0]->getFunction()) {
466       // Calls externally - can't say anything useful.  Remove any existing
467       // function records (may have been created when scanning globals).
468       for (auto *Node : SCC)
469         FunctionInfos.erase(Node->getFunction());
470       continue;
471     }
472
473     FunctionInfo &FI = FunctionInfos[SCC[0]->getFunction()];
474     bool KnowNothing = false;
475
476     // Collect the mod/ref properties due to called functions.  We only compute
477     // one mod-ref set.
478     for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) {
479       Function *F = SCC[i]->getFunction();
480       if (!F) {
481         KnowNothing = true;
482         break;
483       }
484
485       if (F->isDeclaration()) {
486         // Try to get mod/ref behaviour from function attributes.
487         if (F->doesNotAccessMemory()) {
488           // Can't do better than that!
489         } else if (F->onlyReadsMemory()) {
490           FI.addModRefInfo(MRI_Ref);
491           if (!F->isIntrinsic())
492             // This function might call back into the module and read a global -
493             // consider every global as possibly being read by this function.
494             FI.setMayReadAnyGlobal();
495         } else {
496           FI.addModRefInfo(MRI_ModRef);
497           // Can't say anything useful unless it's an intrinsic - they don't
498           // read or write global variables of the kind considered here.
499           KnowNothing = !F->isIntrinsic();
500         }
501         continue;
502       }
503
504       for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end();
505            CI != E && !KnowNothing; ++CI)
506         if (Function *Callee = CI->second->getFunction()) {
507           if (FunctionInfo *CalleeFI = getFunctionInfo(Callee)) {
508             // Propagate function effect up.
509             FI.addFunctionInfo(*CalleeFI);
510           } else {
511             // Can't say anything about it.  However, if it is inside our SCC,
512             // then nothing needs to be done.
513             CallGraphNode *CalleeNode = CG[Callee];
514             if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end())
515               KnowNothing = true;
516           }
517         } else {
518           KnowNothing = true;
519         }
520     }
521
522     // If we can't say anything useful about this SCC, remove all SCC functions
523     // from the FunctionInfos map.
524     if (KnowNothing) {
525       for (auto *Node : SCC)
526         FunctionInfos.erase(Node->getFunction());
527       continue;
528     }
529
530     // Scan the function bodies for explicit loads or stores.
531     for (auto *Node : SCC) {
532       if (FI.getModRefInfo() == MRI_ModRef)
533         break; // The mod/ref lattice saturates here.
534       for (Instruction &I : instructions(Node->getFunction())) {
535         if (FI.getModRefInfo() == MRI_ModRef)
536           break; // The mod/ref lattice saturates here.
537
538         // We handle calls specially because the graph-relevant aspects are
539         // handled above.
540         if (auto CS = CallSite(&I)) {
541           if (isAllocationFn(&I, TLI) || isFreeCall(&I, TLI)) {
542             // FIXME: It is completely unclear why this is necessary and not
543             // handled by the above graph code.
544             FI.addModRefInfo(MRI_ModRef);
545           } else if (Function *Callee = CS.getCalledFunction()) {
546             // The callgraph doesn't include intrinsic calls.
547             if (Callee->isIntrinsic()) {
548               FunctionModRefBehavior Behaviour =
549                   AliasAnalysis::getModRefBehavior(Callee);
550               FI.addModRefInfo(ModRefInfo(Behaviour & MRI_ModRef));
551             }
552           }
553           continue;
554         }
555
556         // All non-call instructions we use the primary predicates for whether
557         // thay read or write memory.
558         if (I.mayReadFromMemory())
559           FI.addModRefInfo(MRI_Ref);
560         if (I.mayWriteToMemory())
561           FI.addModRefInfo(MRI_Mod);
562       }
563     }
564
565     if ((FI.getModRefInfo() & MRI_Mod) == 0)
566       ++NumReadMemFunctions;
567     if (FI.getModRefInfo() == MRI_NoModRef)
568       ++NumNoMemFunctions;
569
570     // Finally, now that we know the full effect on this SCC, clone the
571     // information to each function in the SCC.
572     for (unsigned i = 1, e = SCC.size(); i != e; ++i)
573       FunctionInfos[SCC[i]->getFunction()] = FI;
574   }
575 }
576
577 // There are particular cases where we can conclude no-alias between
578 // a non-addr-taken global and some other underlying object. Specifically,
579 // a non-addr-taken global is known to not be escaped from any function. It is
580 // also incorrect for a transformation to introduce an escape of a global in
581 // a way that is observable when it was not there previously. One function
582 // being transformed to introduce an escape which could possibly be observed
583 // (via loading from a global or the return value for example) within another
584 // function is never safe. If the observation is made through non-atomic
585 // operations on different threads, it is a data-race and UB. If the
586 // observation is well defined, by being observed the transformation would have
587 // changed program behavior by introducing the observed escape, making it an
588 // invalid transform.
589 //
590 // This property does require that transformations which *temporarily* escape
591 // a global that was not previously escaped, prior to restoring it, cannot rely
592 // on the results of GMR::alias. This seems a reasonable restriction, although
593 // currently there is no way to enforce it. There is also no realistic
594 // optimization pass that would make this mistake. The closest example is
595 // a transformation pass which does reg2mem of SSA values but stores them into
596 // global variables temporarily before restoring the global variable's value.
597 // This could be useful to expose "benign" races for example. However, it seems
598 // reasonable to require that a pass which introduces escapes of global
599 // variables in this way to either not trust AA results while the escape is
600 // active, or to be forced to operate as a module pass that cannot co-exist
601 // with an alias analysis such as GMR.
602 bool GlobalsModRef::isNonEscapingGlobalNoAlias(const GlobalValue *GV,
603                                                const Value *V) {
604   // In order to know that the underlying object cannot alias the
605   // non-addr-taken global, we must know that it would have to be an escape.
606   // Thus if the underlying object is a function argument, a load from
607   // a global, or the return of a function, it cannot alias. We can also
608   // recurse through PHI nodes and select nodes provided all of their inputs
609   // resolve to one of these known-escaping roots.
610   SmallPtrSet<const Value *, 8> Visited;
611   SmallVector<const Value *, 8> Inputs;
612   Visited.insert(V);
613   Inputs.push_back(V);
614   int Depth = 0;
615   do {
616     const Value *Input = Inputs.pop_back_val();
617
618     if (auto *InputGV = dyn_cast<GlobalValue>(Input)) {
619       // If one input is the very global we're querying against, then we can't
620       // conclude no-alias.
621       if (InputGV == GV)
622         return false;
623
624       // Distinct GlobalVariables never alias, unless overriden or zero-sized.
625       // FIXME: The condition can be refined, but be conservative for now.
626       auto *GVar = dyn_cast<GlobalVariable>(GV);
627       auto *InputGVar = dyn_cast<GlobalVariable>(InputGV);
628       if (GVar && InputGVar &&
629           !GVar->isDeclaration() && !InputGVar->isDeclaration() &&
630           !GVar->mayBeOverridden() && !InputGVar->mayBeOverridden()) {
631         Type *GVType = GVar->getInitializer()->getType();
632         Type *InputGVType = InputGVar->getInitializer()->getType();
633         if (GVType->isSized() && InputGVType->isSized() &&
634             (DL->getTypeAllocSize(GVType) > 0) &&
635             (DL->getTypeAllocSize(InputGVType) > 0))
636           continue;
637       }
638
639       // Conservatively return false, even though we could be smarter
640       // (e.g. look through GlobalAliases).
641       return false;
642     }
643
644     if (isa<Argument>(Input) || isa<CallInst>(Input) ||
645         isa<InvokeInst>(Input)) {
646       // Arguments to functions or returns from functions are inherently
647       // escaping, so we can immediately classify those as not aliasing any
648       // non-addr-taken globals.
649       continue;
650     }
651     if (auto *LI = dyn_cast<LoadInst>(Input)) {
652       // A pointer loaded from a global would have been captured, and we know
653       // that the global is non-escaping, so no alias.
654       if (isa<GlobalValue>(GetUnderlyingObject(LI->getPointerOperand(), *DL)))
655         continue;
656
657       // Otherwise, a load could come from anywhere, so bail.
658       return false;
659     }
660
661     // Recurse through a limited number of selects and PHIs. This is an
662     // arbitrary depth of 4, lower numbers could be used to fix compile time
663     // issues if needed, but this is generally expected to be only be important
664     // for small depths.
665     if (++Depth > 4)
666       return false;
667     if (auto *SI = dyn_cast<SelectInst>(Input)) {
668       const Value *LHS = GetUnderlyingObject(SI->getTrueValue(), *DL);
669       const Value *RHS = GetUnderlyingObject(SI->getFalseValue(), *DL);
670       if (Visited.insert(LHS).second)
671         Inputs.push_back(LHS);
672       if (Visited.insert(RHS).second)
673         Inputs.push_back(RHS);
674       continue;
675     }
676     if (auto *PN = dyn_cast<PHINode>(Input)) {
677       for (const Value *Op : PN->incoming_values()) {
678         Op = GetUnderlyingObject(Op, *DL);
679         if (Visited.insert(Op).second)
680           Inputs.push_back(Op);
681       }
682       continue;
683     }
684
685     // FIXME: It would be good to handle other obvious no-alias cases here, but
686     // it isn't clear how to do so reasonbly without building a small version
687     // of BasicAA into this code. We could recurse into AliasAnalysis::alias
688     // here but that seems likely to go poorly as we're inside the
689     // implementation of such a query. Until then, just conservatievly retun
690     // false.
691     return false;
692   } while (!Inputs.empty());
693
694   // If all the inputs to V were definitively no-alias, then V is no-alias.
695   return true;
696 }
697
698 /// alias - If one of the pointers is to a global that we are tracking, and the
699 /// other is some random pointer, we know there cannot be an alias, because the
700 /// address of the global isn't taken.
701 AliasResult GlobalsModRef::alias(const MemoryLocation &LocA,
702                                  const MemoryLocation &LocB) {
703   // Get the base object these pointers point to.
704   const Value *UV1 = GetUnderlyingObject(LocA.Ptr, *DL);
705   const Value *UV2 = GetUnderlyingObject(LocB.Ptr, *DL);
706
707   // If either of the underlying values is a global, they may be non-addr-taken
708   // globals, which we can answer queries about.
709   const GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1);
710   const GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2);
711   if (GV1 || GV2) {
712     // If the global's address is taken, pretend we don't know it's a pointer to
713     // the global.
714     if (GV1 && !NonAddressTakenGlobals.count(GV1))
715       GV1 = nullptr;
716     if (GV2 && !NonAddressTakenGlobals.count(GV2))
717       GV2 = nullptr;
718
719     // If the two pointers are derived from two different non-addr-taken
720     // globals we know these can't alias.
721     if (GV1 && GV2 && GV1 != GV2)
722       return NoAlias;
723
724     // If one is and the other isn't, it isn't strictly safe but we can fake
725     // this result if necessary for performance. This does not appear to be
726     // a common problem in practice.
727     if (EnableUnsafeGlobalsModRefAliasResults)
728       if ((GV1 || GV2) && GV1 != GV2)
729         return NoAlias;
730
731     // Check for a special case where a non-escaping global can be used to
732     // conclude no-alias.
733     if ((GV1 || GV2) && GV1 != GV2) {
734       const GlobalValue *GV = GV1 ? GV1 : GV2;
735       const Value *UV = GV1 ? UV2 : UV1;
736       if (isNonEscapingGlobalNoAlias(GV, UV))
737         return NoAlias;
738     }
739
740     // Otherwise if they are both derived from the same addr-taken global, we
741     // can't know the two accesses don't overlap.
742   }
743
744   // These pointers may be based on the memory owned by an indirect global.  If
745   // so, we may be able to handle this.  First check to see if the base pointer
746   // is a direct load from an indirect global.
747   GV1 = GV2 = nullptr;
748   if (const LoadInst *LI = dyn_cast<LoadInst>(UV1))
749     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
750       if (IndirectGlobals.count(GV))
751         GV1 = GV;
752   if (const LoadInst *LI = dyn_cast<LoadInst>(UV2))
753     if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
754       if (IndirectGlobals.count(GV))
755         GV2 = GV;
756
757   // These pointers may also be from an allocation for the indirect global.  If
758   // so, also handle them.
759   if (!GV1)
760     GV1 = AllocsForIndirectGlobals.lookup(UV1);
761   if (!GV2)
762     GV2 = AllocsForIndirectGlobals.lookup(UV2);
763
764   // Now that we know whether the two pointers are related to indirect globals,
765   // use this to disambiguate the pointers. If the pointers are based on
766   // different indirect globals they cannot alias.
767   if (GV1 && GV2 && GV1 != GV2)
768     return NoAlias;
769
770   // If one is based on an indirect global and the other isn't, it isn't
771   // strictly safe but we can fake this result if necessary for performance.
772   // This does not appear to be a common problem in practice.
773   if (EnableUnsafeGlobalsModRefAliasResults)
774     if ((GV1 || GV2) && GV1 != GV2)
775       return NoAlias;
776
777   return AliasAnalysis::alias(LocA, LocB);
778 }
779
780 ModRefInfo GlobalsModRef::getModRefInfo(ImmutableCallSite CS,
781                                         const MemoryLocation &Loc) {
782   unsigned Known = MRI_ModRef;
783
784   // If we are asking for mod/ref info of a direct call with a pointer to a
785   // global we are tracking, return information if we have it.
786   const DataLayout &DL = CS.getCaller()->getParent()->getDataLayout();
787   if (const GlobalValue *GV =
788           dyn_cast<GlobalValue>(GetUnderlyingObject(Loc.Ptr, DL)))
789     if (GV->hasLocalLinkage())
790       if (const Function *F = CS.getCalledFunction())
791         if (NonAddressTakenGlobals.count(GV))
792           if (const FunctionInfo *FI = getFunctionInfo(F))
793             Known = FI->getModRefInfoForGlobal(*GV);
794
795   if (Known == MRI_NoModRef)
796     return MRI_NoModRef; // No need to query other mod/ref analyses
797   return ModRefInfo(Known & AliasAnalysis::getModRefInfo(CS, Loc));
798 }