LLVM  3.7.0
GlobalsModRef.cpp
Go to the documentation of this file.
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/Passes.h"
18 #include "llvm/ADT/SCCIterator.h"
19 #include "llvm/ADT/Statistic.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/DerivedTypes.h"
26 #include "llvm/IR/InstIterator.h"
27 #include "llvm/IR/Instructions.h"
28 #include "llvm/IR/IntrinsicInst.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/Pass.h"
32 #include <set>
33 using namespace llvm;
34 
35 #define DEBUG_TYPE "globalsmodref-aa"
36 
37 STATISTIC(NumNonAddrTakenGlobalVars,
38  "Number of global vars without address taken");
39 STATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken");
40 STATISTIC(NumNoMemFunctions, "Number of functions that do not access memory");
41 STATISTIC(NumReadMemFunctions, "Number of functions that only read memory");
42 STATISTIC(NumIndirectGlobalVars, "Number of indirect global objects");
43 
44 namespace {
45 /// FunctionRecord - One instance of this structure is stored for every
46 /// function in the program. Later, the entries for these functions are
47 /// removed if the function is found to call an external function (in which
48 /// case we know nothing about it.
49 struct FunctionRecord {
50  /// GlobalInfo - Maintain mod/ref info for all of the globals without
51  /// addresses taken that are read or written (transitively) by this
52  /// function.
53  std::map<const GlobalValue *, unsigned> GlobalInfo;
54 
55  /// MayReadAnyGlobal - May read global variables, but it is not known which.
56  bool MayReadAnyGlobal;
57 
58  unsigned getInfoForGlobal(const GlobalValue *GV) const {
59  unsigned Effect = MayReadAnyGlobal ? AliasAnalysis::Ref : 0;
60  std::map<const GlobalValue *, unsigned>::const_iterator I =
61  GlobalInfo.find(GV);
62  if (I != GlobalInfo.end())
63  Effect |= I->second;
64  return Effect;
65  }
66 
67  /// FunctionEffect - Capture whether or not this function reads or writes to
68  /// ANY memory. If not, we can do a lot of aggressive analysis on it.
69  unsigned FunctionEffect;
70 
71  FunctionRecord() : MayReadAnyGlobal(false), FunctionEffect(0) {}
72 };
73 
74 /// GlobalsModRef - The actual analysis pass.
75 class GlobalsModRef : public ModulePass, public AliasAnalysis {
76  /// NonAddressTakenGlobals - The globals that do not have their addresses
77  /// taken.
78  std::set<const GlobalValue *> NonAddressTakenGlobals;
79 
80  /// IndirectGlobals - The memory pointed to by this global is known to be
81  /// 'owned' by the global.
82  std::set<const GlobalValue *> IndirectGlobals;
83 
84  /// AllocsForIndirectGlobals - If an instruction allocates memory for an
85  /// indirect global, this map indicates which one.
86  std::map<const Value *, const GlobalValue *> AllocsForIndirectGlobals;
87 
88  /// FunctionInfo - For each function, keep track of what globals are
89  /// modified or read.
90  std::map<const Function *, FunctionRecord> FunctionInfo;
91 
92 public:
93  static char ID;
94  GlobalsModRef() : ModulePass(ID) {
96  }
97 
98  bool runOnModule(Module &M) override {
99  InitializeAliasAnalysis(this, &M.getDataLayout());
100 
101  // Find non-addr taken globals.
102  AnalyzeGlobals(M);
103 
104  // Propagate on CG.
105  AnalyzeCallGraph(getAnalysis<CallGraphWrapperPass>().getCallGraph(), M);
106  return false;
107  }
108 
109  void getAnalysisUsage(AnalysisUsage &AU) const override {
112  AU.setPreservesAll(); // Does not transform code
113  }
114 
115  //------------------------------------------------
116  // Implement the AliasAnalysis API
117  //
118  AliasResult alias(const MemoryLocation &LocA,
119  const MemoryLocation &LocB) override;
120  ModRefResult getModRefInfo(ImmutableCallSite CS,
121  const MemoryLocation &Loc) override;
122  ModRefResult getModRefInfo(ImmutableCallSite CS1,
123  ImmutableCallSite CS2) override {
124  return AliasAnalysis::getModRefInfo(CS1, CS2);
125  }
126 
127  /// getModRefBehavior - Return the behavior of the specified function if
128  /// called from the specified call site. The call site may be null in which
129  /// case the most generic behavior of this function should be returned.
130  ModRefBehavior getModRefBehavior(const Function *F) override {
131  ModRefBehavior Min = UnknownModRefBehavior;
132 
133  if (FunctionRecord *FR = getFunctionInfo(F)) {
134  if (FR->FunctionEffect == 0)
135  Min = DoesNotAccessMemory;
136  else if ((FR->FunctionEffect & Mod) == 0)
137  Min = OnlyReadsMemory;
138  }
139 
141  }
142 
143  /// getModRefBehavior - Return the behavior of the specified function if
144  /// called from the specified call site. The call site may be null in which
145  /// case the most generic behavior of this function should be returned.
146  ModRefBehavior getModRefBehavior(ImmutableCallSite CS) override {
147  ModRefBehavior Min = UnknownModRefBehavior;
148 
149  if (const Function *F = CS.getCalledFunction())
150  if (FunctionRecord *FR = getFunctionInfo(F)) {
151  if (FR->FunctionEffect == 0)
152  Min = DoesNotAccessMemory;
153  else if ((FR->FunctionEffect & Mod) == 0)
154  Min = OnlyReadsMemory;
155  }
156 
158  }
159 
160  void deleteValue(Value *V) override;
161  void addEscapingUse(Use &U) override;
162 
163  /// getAdjustedAnalysisPointer - This method is used when a pass implements
164  /// an analysis interface through multiple inheritance. If needed, it
165  /// should override this to adjust the this pointer as needed for the
166  /// specified pass info.
167  void *getAdjustedAnalysisPointer(AnalysisID PI) override {
168  if (PI == &AliasAnalysis::ID)
169  return (AliasAnalysis *)this;
170  return this;
171  }
172 
173 private:
174  /// getFunctionInfo - Return the function info for the function, or null if
175  /// we don't have anything useful to say about it.
176  FunctionRecord *getFunctionInfo(const Function *F) {
177  std::map<const Function *, FunctionRecord>::iterator I =
178  FunctionInfo.find(F);
179  if (I != FunctionInfo.end())
180  return &I->second;
181  return nullptr;
182  }
183 
184  void AnalyzeGlobals(Module &M);
185  void AnalyzeCallGraph(CallGraph &CG, Module &M);
186  bool AnalyzeUsesOfPointer(Value *V, std::vector<Function *> &Readers,
187  std::vector<Function *> &Writers,
188  GlobalValue *OkayStoreDest = nullptr);
189  bool AnalyzeIndirectGlobalMemory(GlobalValue *GV);
190 };
191 }
192 
193 char GlobalsModRef::ID = 0;
194 INITIALIZE_AG_PASS_BEGIN(GlobalsModRef, AliasAnalysis, "globalsmodref-aa",
195  "Simple mod/ref analysis for globals", false, true,
196  false)
198 INITIALIZE_AG_PASS_END(GlobalsModRef, AliasAnalysis, "globalsmodref-aa",
199  "Simple mod/ref analysis for globals", false, true,
200  false)
201 
202 Pass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); }
203 
204 /// AnalyzeGlobals - Scan through the users of all of the internal
205 /// GlobalValue's in the program. If none of them have their "address taken"
206 /// (really, their address passed to something nontrivial), record this fact,
207 /// and record the functions that they are used directly in.
208 void GlobalsModRef::AnalyzeGlobals(Module &M) {
209  std::vector<Function *> Readers, Writers;
210  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
211  if (I->hasLocalLinkage()) {
212  if (!AnalyzeUsesOfPointer(I, Readers, Writers)) {
213  // Remember that we are tracking this global.
214  NonAddressTakenGlobals.insert(I);
215  ++NumNonAddrTakenFunctions;
216  }
217  Readers.clear();
218  Writers.clear();
219  }
220 
221  for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E;
222  ++I)
223  if (I->hasLocalLinkage()) {
224  if (!AnalyzeUsesOfPointer(I, Readers, Writers)) {
225  // Remember that we are tracking this global, and the mod/ref fns
226  NonAddressTakenGlobals.insert(I);
227 
228  for (unsigned i = 0, e = Readers.size(); i != e; ++i)
229  FunctionInfo[Readers[i]].GlobalInfo[I] |= Ref;
230 
231  if (!I->isConstant()) // No need to keep track of writers to constants
232  for (unsigned i = 0, e = Writers.size(); i != e; ++i)
233  FunctionInfo[Writers[i]].GlobalInfo[I] |= Mod;
234  ++NumNonAddrTakenGlobalVars;
235 
236  // If this global holds a pointer type, see if it is an indirect global.
237  if (I->getType()->getElementType()->isPointerTy() &&
238  AnalyzeIndirectGlobalMemory(I))
239  ++NumIndirectGlobalVars;
240  }
241  Readers.clear();
242  Writers.clear();
243  }
244 }
245 
246 /// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer.
247 /// If this is used by anything complex (i.e., the address escapes), return
248 /// true. Also, while we are at it, keep track of those functions that read and
249 /// write to the value.
250 ///
251 /// If OkayStoreDest is non-null, stores into this global are allowed.
252 bool GlobalsModRef::AnalyzeUsesOfPointer(Value *V,
253  std::vector<Function *> &Readers,
254  std::vector<Function *> &Writers,
255  GlobalValue *OkayStoreDest) {
256  if (!V->getType()->isPointerTy())
257  return true;
258 
259  for (Use &U : V->uses()) {
260  User *I = U.getUser();
261  if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
262  Readers.push_back(LI->getParent()->getParent());
263  } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
264  if (V == SI->getOperand(1)) {
265  Writers.push_back(SI->getParent()->getParent());
266  } else if (SI->getOperand(1) != OkayStoreDest) {
267  return true; // Storing the pointer
268  }
269  } else if (Operator::getOpcode(I) == Instruction::GetElementPtr) {
270  if (AnalyzeUsesOfPointer(I, Readers, Writers))
271  return true;
272  } else if (Operator::getOpcode(I) == Instruction::BitCast) {
273  if (AnalyzeUsesOfPointer(I, Readers, Writers, OkayStoreDest))
274  return true;
275  } else if (auto CS = CallSite(I)) {
276  // Make sure that this is just the function being called, not that it is
277  // passing into the function.
278  if (!CS.isCallee(&U)) {
279  // Detect calls to free.
280  if (isFreeCall(I, TLI))
281  Writers.push_back(CS->getParent()->getParent());
282  else
283  return true; // Argument of an unknown call.
284  }
285  } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) {
286  if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
287  return true; // Allow comparison against null.
288  } else {
289  return true;
290  }
291  }
292 
293  return false;
294 }
295 
296 /// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable
297 /// which holds a pointer type. See if the global always points to non-aliased
298 /// heap memory: that is, all initializers of the globals are allocations, and
299 /// those allocations have no use other than initialization of the global.
300 /// Further, all loads out of GV must directly use the memory, not store the
301 /// pointer somewhere. If this is true, we consider the memory pointed to by
302 /// GV to be owned by GV and can disambiguate other pointers from it.
303 bool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) {
304  // Keep track of values related to the allocation of the memory, f.e. the
305  // value produced by the malloc call and any casts.
306  std::vector<Value *> AllocRelatedValues;
307 
308  // Walk the user list of the global. If we find anything other than a direct
309  // load or store, bail out.
310  for (User *U : GV->users()) {
311  if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
312  // The pointer loaded from the global can only be used in simple ways:
313  // we allow addressing of it and loading storing to it. We do *not* allow
314  // storing the loaded pointer somewhere else or passing to a function.
315  std::vector<Function *> ReadersWriters;
316  if (AnalyzeUsesOfPointer(LI, ReadersWriters, ReadersWriters))
317  return false; // Loaded pointer escapes.
318  // TODO: Could try some IP mod/ref of the loaded pointer.
319  } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
320  // Storing the global itself.
321  if (SI->getOperand(0) == GV)
322  return false;
323 
324  // If storing the null pointer, ignore it.
325  if (isa<ConstantPointerNull>(SI->getOperand(0)))
326  continue;
327 
328  // Check the value being stored.
329  Value *Ptr = GetUnderlyingObject(SI->getOperand(0),
330  GV->getParent()->getDataLayout());
331 
332  if (!isAllocLikeFn(Ptr, TLI))
333  return false; // Too hard to analyze.
334 
335  // Analyze all uses of the allocation. If any of them are used in a
336  // non-simple way (e.g. stored to another global) bail out.
337  std::vector<Function *> ReadersWriters;
338  if (AnalyzeUsesOfPointer(Ptr, ReadersWriters, ReadersWriters, GV))
339  return false; // Loaded pointer escapes.
340 
341  // Remember that this allocation is related to the indirect global.
342  AllocRelatedValues.push_back(Ptr);
343  } else {
344  // Something complex, bail out.
345  return false;
346  }
347  }
348 
349  // Okay, this is an indirect global. Remember all of the allocations for
350  // this global in AllocsForIndirectGlobals.
351  while (!AllocRelatedValues.empty()) {
352  AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV;
353  AllocRelatedValues.pop_back();
354  }
355  IndirectGlobals.insert(GV);
356  return true;
357 }
358 
359 /// AnalyzeCallGraph - At this point, we know the functions where globals are
360 /// immediately stored to and read from. Propagate this information up the call
361 /// graph to all callers and compute the mod/ref info for all memory for each
362 /// function.
363 void GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) {
364  // We do a bottom-up SCC traversal of the call graph. In other words, we
365  // visit all callees before callers (leaf-first).
366  for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
367  const std::vector<CallGraphNode *> &SCC = *I;
368  assert(!SCC.empty() && "SCC with no functions?");
369 
370  if (!SCC[0]->getFunction()) {
371  // Calls externally - can't say anything useful. Remove any existing
372  // function records (may have been created when scanning globals).
373  for (unsigned i = 0, e = SCC.size(); i != e; ++i)
374  FunctionInfo.erase(SCC[i]->getFunction());
375  continue;
376  }
377 
378  FunctionRecord &FR = FunctionInfo[SCC[0]->getFunction()];
379 
380  bool KnowNothing = false;
381  unsigned FunctionEffect = 0;
382 
383  // Collect the mod/ref properties due to called functions. We only compute
384  // one mod-ref set.
385  for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) {
386  Function *F = SCC[i]->getFunction();
387  if (!F) {
388  KnowNothing = true;
389  break;
390  }
391 
392  if (F->isDeclaration()) {
393  // Try to get mod/ref behaviour from function attributes.
394  if (F->doesNotAccessMemory()) {
395  // Can't do better than that!
396  } else if (F->onlyReadsMemory()) {
397  FunctionEffect |= Ref;
398  if (!F->isIntrinsic())
399  // This function might call back into the module and read a global -
400  // consider every global as possibly being read by this function.
401  FR.MayReadAnyGlobal = true;
402  } else {
403  FunctionEffect |= ModRef;
404  // Can't say anything useful unless it's an intrinsic - they don't
405  // read or write global variables of the kind considered here.
406  KnowNothing = !F->isIntrinsic();
407  }
408  continue;
409  }
410 
411  for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end();
412  CI != E && !KnowNothing; ++CI)
413  if (Function *Callee = CI->second->getFunction()) {
414  if (FunctionRecord *CalleeFR = getFunctionInfo(Callee)) {
415  // Propagate function effect up.
416  FunctionEffect |= CalleeFR->FunctionEffect;
417 
418  // Incorporate callee's effects on globals into our info.
419  for (const auto &G : CalleeFR->GlobalInfo)
420  FR.GlobalInfo[G.first] |= G.second;
421  FR.MayReadAnyGlobal |= CalleeFR->MayReadAnyGlobal;
422  } else {
423  // Can't say anything about it. However, if it is inside our SCC,
424  // then nothing needs to be done.
425  CallGraphNode *CalleeNode = CG[Callee];
426  if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end())
427  KnowNothing = true;
428  }
429  } else {
430  KnowNothing = true;
431  }
432  }
433 
434  // If we can't say anything useful about this SCC, remove all SCC functions
435  // from the FunctionInfo map.
436  if (KnowNothing) {
437  for (unsigned i = 0, e = SCC.size(); i != e; ++i)
438  FunctionInfo.erase(SCC[i]->getFunction());
439  continue;
440  }
441 
442  // Scan the function bodies for explicit loads or stores.
443  for (auto *Node : SCC) {
444  if (FunctionEffect == ModRef)
445  break; // The mod/ref lattice saturates here.
446  for (Instruction &I : inst_range(Node->getFunction())) {
447  if (FunctionEffect == ModRef)
448  break; // The mod/ref lattice saturates here.
449 
450  // We handle calls specially because the graph-relevant aspects are
451  // handled above.
452  if (auto CS = CallSite(&I)) {
453  if (isAllocationFn(&I, TLI) || isFreeCall(&I, TLI)) {
454  // FIXME: It is completely unclear why this is necessary and not
455  // handled by the above graph code.
456  FunctionEffect |= ModRef;
457  } else if (Function *Callee = CS.getCalledFunction()) {
458  // The callgraph doesn't include intrinsic calls.
459  if (Callee->isIntrinsic()) {
460  ModRefBehavior Behaviour =
462  FunctionEffect |= (Behaviour & ModRef);
463  }
464  }
465  continue;
466  }
467 
468  // All non-call instructions we use the primary predicates for whether
469  // thay read or write memory.
470  if (I.mayReadFromMemory())
471  FunctionEffect |= Ref;
472  if (I.mayWriteToMemory())
473  FunctionEffect |= Mod;
474  }
475  }
476 
477  if ((FunctionEffect & Mod) == 0)
478  ++NumReadMemFunctions;
479  if (FunctionEffect == 0)
480  ++NumNoMemFunctions;
481  FR.FunctionEffect = FunctionEffect;
482 
483  // Finally, now that we know the full effect on this SCC, clone the
484  // information to each function in the SCC.
485  for (unsigned i = 1, e = SCC.size(); i != e; ++i)
486  FunctionInfo[SCC[i]->getFunction()] = FR;
487  }
488 }
489 
490 /// alias - If one of the pointers is to a global that we are tracking, and the
491 /// other is some random pointer, we know there cannot be an alias, because the
492 /// address of the global isn't taken.
493 AliasResult GlobalsModRef::alias(const MemoryLocation &LocA,
494  const MemoryLocation &LocB) {
495  // Get the base object these pointers point to.
496  const Value *UV1 = GetUnderlyingObject(LocA.Ptr, *DL);
497  const Value *UV2 = GetUnderlyingObject(LocB.Ptr, *DL);
498 
499  // If either of the underlying values is a global, they may be non-addr-taken
500  // globals, which we can answer queries about.
501  const GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1);
502  const GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2);
503  if (GV1 || GV2) {
504  // If the global's address is taken, pretend we don't know it's a pointer to
505  // the global.
506  if (GV1 && !NonAddressTakenGlobals.count(GV1))
507  GV1 = nullptr;
508  if (GV2 && !NonAddressTakenGlobals.count(GV2))
509  GV2 = nullptr;
510 
511  // If the two pointers are derived from two different non-addr-taken
512  // globals, or if one is and the other isn't, we know these can't alias.
513  if ((GV1 || GV2) && GV1 != GV2)
514  return NoAlias;
515 
516  // Otherwise if they are both derived from the same addr-taken global, we
517  // can't know the two accesses don't overlap.
518  }
519 
520  // These pointers may be based on the memory owned by an indirect global. If
521  // so, we may be able to handle this. First check to see if the base pointer
522  // is a direct load from an indirect global.
523  GV1 = GV2 = nullptr;
524  if (const LoadInst *LI = dyn_cast<LoadInst>(UV1))
525  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
526  if (IndirectGlobals.count(GV))
527  GV1 = GV;
528  if (const LoadInst *LI = dyn_cast<LoadInst>(UV2))
529  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
530  if (IndirectGlobals.count(GV))
531  GV2 = GV;
532 
533  // These pointers may also be from an allocation for the indirect global. If
534  // so, also handle them.
535  if (AllocsForIndirectGlobals.count(UV1))
536  GV1 = AllocsForIndirectGlobals[UV1];
537  if (AllocsForIndirectGlobals.count(UV2))
538  GV2 = AllocsForIndirectGlobals[UV2];
539 
540  // Now that we know whether the two pointers are related to indirect globals,
541  // use this to disambiguate the pointers. If either pointer is based on an
542  // indirect global and if they are not both based on the same indirect global,
543  // they cannot alias.
544  if ((GV1 || GV2) && GV1 != GV2)
545  return NoAlias;
546 
547  return AliasAnalysis::alias(LocA, LocB);
548 }
549 
551 GlobalsModRef::getModRefInfo(ImmutableCallSite CS, const MemoryLocation &Loc) {
552  unsigned Known = ModRef;
553 
554  // If we are asking for mod/ref info of a direct call with a pointer to a
555  // global we are tracking, return information if we have it.
556  const DataLayout &DL = CS.getCaller()->getParent()->getDataLayout();
557  if (const GlobalValue *GV =
558  dyn_cast<GlobalValue>(GetUnderlyingObject(Loc.Ptr, DL)))
559  if (GV->hasLocalLinkage())
560  if (const Function *F = CS.getCalledFunction())
561  if (NonAddressTakenGlobals.count(GV))
562  if (const FunctionRecord *FR = getFunctionInfo(F))
563  Known = FR->getInfoForGlobal(GV);
564 
565  if (Known == NoModRef)
566  return NoModRef; // No need to query other mod/ref analyses
567  return ModRefResult(Known & AliasAnalysis::getModRefInfo(CS, Loc));
568 }
569 
570 //===----------------------------------------------------------------------===//
571 // Methods to update the analysis as a result of the client transformation.
572 //
573 void GlobalsModRef::deleteValue(Value *V) {
574  if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
575  if (NonAddressTakenGlobals.erase(GV)) {
576  // This global might be an indirect global. If so, remove it and remove
577  // any AllocRelatedValues for it.
578  if (IndirectGlobals.erase(GV)) {
579  // Remove any entries in AllocsForIndirectGlobals for this global.
580  for (std::map<const Value *, const GlobalValue *>::iterator
581  I = AllocsForIndirectGlobals.begin(),
582  E = AllocsForIndirectGlobals.end();
583  I != E;) {
584  if (I->second == GV) {
585  AllocsForIndirectGlobals.erase(I++);
586  } else {
587  ++I;
588  }
589  }
590  }
591  }
592  }
593 
594  // Otherwise, if this is an allocation related to an indirect global, remove
595  // it.
596  AllocsForIndirectGlobals.erase(V);
597 
599 }
600 
601 void GlobalsModRef::addEscapingUse(Use &U) {
602  // For the purposes of this analysis, it is conservatively correct to treat
603  // a newly escaping value equivalently to a deleted one. We could perhaps
604  // be more precise by processing the new use and attempting to update our
605  // saved analysis results to accommodate it.
606  deleteValue(U);
607 
609 }
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:82
bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates or reallocates memory (eith...
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:240
iterator_range< use_iterator > uses()
Definition: Value.h:283
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
bool onlyReadsMemory() const
Determine if the function does not access or only reads memory.
Definition: Function.h:287
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
bool isIntrinsic() const
Definition: Function.h:160
BBTy * getParent() const
Get the basic block containing the call site.
Definition: CallSite.h:87
const_iterator begin(StringRef path)
Get begin iterator over path.
Definition: Path.cpp:232
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:111
The two locations do not alias at all.
Definition: AliasAnalysis.h:78
F(f)
FunTy * getCaller() const
getCaller - Return the caller function for this call site
Definition: CallSite.h:170
LoadInst - an instruction for reading from memory.
Definition: Instructions.h:177
A node in the call graph for a module.
Definition: CallGraph.h:166
const CallInst * isFreeCall(const Value *I, const TargetLibraryInfo *TLI)
isFreeCall - Returns non-null if the value is a call to the builtin free()
std::vector< CallRecord >::iterator iterator
Definition: CallGraph.h:182
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:70
globalsmodref aa
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
INITIALIZE_AG_PASS_BEGIN(GlobalsModRef, AliasAnalysis,"globalsmodref-aa","Simple mod/ref analysis for globals", false, true, false) INITIALIZE_AG_PASS_END(GlobalsModRef
#define G(x, y, z)
Definition: MD5.cpp:52
scc_iterator< T > scc_begin(const T &G)
Construct the begin iterator for a deduced graph type T.
Definition: SCCIterator.h:224
global_iterator global_begin()
Definition: Module.h:552
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
Pass * createGlobalsModRefPass()
globalsmodref Simple mod ref analysis for globals
virtual void addEscapingUse(Use &U)
addEscapingUse - This method should be used whenever an escaping use is added to a pointer value...
StoreInst - an instruction for storing to memory.
Definition: Instructions.h:316
void initializeGlobalsModRefPass(PassRegistry &)
bool doesNotAccessMemory() const
Determine if the function does not access memory.
Definition: Function.h:278
The ModulePass which wraps up a CallGraph and the logic to build it.
Definition: CallGraph.h:316
FunTy * getCalledFunction() const
getCalledFunction - Return the function being called if this is a direct call, otherwise return null ...
Definition: CallSite.h:99
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define ModRefBehavior
AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:72
Represent the analysis usage information of a pass.
This instruction compares its operands according to the predicate given to the constructor.
for(unsigned i=0, e=MI->getNumOperands();i!=e;++i)
bool isPointerTy() const
isPointerTy - True if this is an instance of PointerType.
Definition: Type.h:217
virtual void deleteValue(Value *V)
Methods that clients should call when they transform the program to allow alias analyses to update th...
virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS)
getModRefBehavior - Return the behavior when calling the given call site.
Value * GetUnderlyingObject(Value *V, const DataLayout &DL, unsigned MaxLookup=6)
GetUnderlyingObject - This method strips off any GEP address adjustments and pointer casts from the s...
global_iterator global_end()
Definition: Module.h:554
const Value * Ptr
The address of the start of the location.
Representation for a specific memory location.
globalsmodref Simple mod ref analysis for true
virtual AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
Alias Queries...
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
ModRefResult getModRefInfo(const Instruction *I)
getModRefInfo - Return information about whether or not an instruction may read or write memory (with...
bool isCallee(Value::const_user_iterator UI) const
isCallee - Determine whether the passed iterator points to the callee operand's Use.
Definition: CallSite.h:112
globalsmodref Simple mod ref analysis for false
void setPreservesAll()
Set by analyses that do not transform their input at all.
iterator_range< user_iterator > users()
Definition: Value.h:300
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:285
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition: Operator.h:48
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:372
The basic data container for the call graph of a Module of IR.
Definition: CallGraph.h:75
iterator end()
Definition: Module.h:571
iterator_range< inst_iterator > inst_range(Function *F)
Definition: InstIterator.h:129
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:128
ImmutableCallSite - establish a view to a call site for examination.
Definition: CallSite.h:418
#define I(x, y, z)
Definition: MD5.cpp:54
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:236
bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates memory (either malloc...
iterator begin()
Definition: Module.h:569
const void * AnalysisID
Definition: Pass.h:47
bool hasLocalLinkage() const
Definition: GlobalValue.h:280
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:365
LLVM Value Representation.
Definition: Value.h:69
ModRefResult
Simple mod/ref information...
virtual void getAnalysisUsage(AnalysisUsage &AU) const
getAnalysisUsage - All alias analysis implementations should invoke this directly (using AliasAnalysi...
#define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def)
Definition: PassSupport.h:205
Enumerate the SCCs of a directed graph in reverse topological order of the SCC DAG.
Definition: SCCIterator.h:40