LLVM  8.0.1
GlobalsModRef.h
Go to the documentation of this file.
1 //===- GlobalsModRef.h - Simple Mod/Ref AA for Globals ----------*- C++ -*-===//
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 /// \file
10 /// This is the interface for a simple mod/ref and alias analysis over globals.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_ANALYSIS_GLOBALSMODREF_H
15 #define LLVM_ANALYSIS_GLOBALSMODREF_H
16 
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/IR/ValueHandle.h"
23 #include "llvm/Pass.h"
24 #include <list>
25 
26 namespace llvm {
27 
28 /// An alias analysis result set for globals.
29 ///
30 /// This focuses on handling aliasing properties of globals and interprocedural
31 /// function call mod/ref information.
32 class GlobalsAAResult : public AAResultBase<GlobalsAAResult> {
34 
35  class FunctionInfo;
36 
37  const DataLayout &DL;
38  const TargetLibraryInfo &TLI;
39 
40  /// The globals that do not have their addresses taken.
41  SmallPtrSet<const GlobalValue *, 8> NonAddressTakenGlobals;
42 
43  /// IndirectGlobals - The memory pointed to by this global is known to be
44  /// 'owned' by the global.
46 
47  /// AllocsForIndirectGlobals - If an instruction allocates memory for an
48  /// indirect global, this map indicates which one.
49  DenseMap<const Value *, const GlobalValue *> AllocsForIndirectGlobals;
50 
51  /// For each function, keep track of what globals are modified or read.
53 
54  /// A map of functions to SCC. The SCCs are described by a simple integer
55  /// ID that is only useful for comparing for equality (are two functions
56  /// in the same SCC or not?)
57  DenseMap<const Function *, unsigned> FunctionToSCCMap;
58 
59  /// Handle to clear this analysis on deletion of values.
60  struct DeletionCallbackHandle final : CallbackVH {
61  GlobalsAAResult *GAR;
62  std::list<DeletionCallbackHandle>::iterator I;
63 
64  DeletionCallbackHandle(GlobalsAAResult &GAR, Value *V)
65  : CallbackVH(V), GAR(&GAR) {}
66 
67  void deleted() override;
68  };
69 
70  /// List of callbacks for globals being tracked by this analysis. Note that
71  /// these objects are quite large, but we only anticipate having one per
72  /// global tracked by this analysis. There are numerous optimizations we
73  /// could perform to the memory utilization here if this becomes a problem.
74  std::list<DeletionCallbackHandle> Handles;
75 
76  explicit GlobalsAAResult(const DataLayout &DL, const TargetLibraryInfo &TLI);
77 
78 public:
81 
83  CallGraph &CG);
84 
85  //------------------------------------------------
86  // Implement the AliasAnalysis API
87  //
88  AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB);
89 
91  ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc);
92 
93  /// getModRefBehavior - Return the behavior of the specified function if
94  /// called from the specified call site. The call site may be null in which
95  /// case the most generic behavior of this function should be returned.
97 
98  /// getModRefBehavior - Return the behavior of the specified function if
99  /// called from the specified call site. The call site may be null in which
100  /// case the most generic behavior of this function should be returned.
102 
103 private:
104  FunctionInfo *getFunctionInfo(const Function *F);
105 
106  void AnalyzeGlobals(Module &M);
107  void AnalyzeCallGraph(CallGraph &CG, Module &M);
108  bool AnalyzeUsesOfPointer(Value *V,
109  SmallPtrSetImpl<Function *> *Readers = nullptr,
110  SmallPtrSetImpl<Function *> *Writers = nullptr,
111  GlobalValue *OkayStoreDest = nullptr);
112  bool AnalyzeIndirectGlobalMemory(GlobalVariable *GV);
113  void CollectSCCMembership(CallGraph &CG);
114 
115  bool isNonEscapingGlobalNoAlias(const GlobalValue *GV, const Value *V);
116  ModRefInfo getModRefInfoForArgument(const CallBase *Call,
117  const GlobalValue *GV);
118 };
119 
120 /// Analysis pass providing a never-invalidated alias analysis result.
121 class GlobalsAA : public AnalysisInfoMixin<GlobalsAA> {
123  static AnalysisKey Key;
124 
125 public:
127 
129 };
130 
131 /// Legacy wrapper pass to provide the GlobalsAAResult object.
133  std::unique_ptr<GlobalsAAResult> Result;
134 
135 public:
136  static char ID;
137 
139 
140  GlobalsAAResult &getResult() { return *Result; }
141  const GlobalsAAResult &getResult() const { return *Result; }
142 
143  bool runOnModule(Module &M) override;
144  bool doFinalization(Module &M) override;
145  void getAnalysisUsage(AnalysisUsage &AU) const override;
146 };
147 
148 //===--------------------------------------------------------------------===//
149 //
150 // createGlobalsAAWrapperPass - This pass provides alias and mod/ref info for
151 // global values that do not have their addresses taken.
152 //
154 }
155 
156 #endif
Legacy wrapper pass to provide the GlobalsAAResult object.
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
const GlobalsAAResult & getResult() const
This class represents lattice values for constants.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1014
F(f)
The mod/ref information collected for a particular function.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:344
A CRTP-driven "mixin" base class to help implement the function alias analysis results concept...
static GlobalsAAResult analyzeModule(Module &M, const TargetLibraryInfo &TLI, CallGraph &CG)
Key
PAL metadata keys.
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
FunctionModRefBehavior
Summary of how a function affects memory in the program.
AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:78
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
alias - If one of the pointers is to a global that we are tracking, and the other is some random poin...
An alias analysis result set for globals.
Definition: GlobalsModRef.h:32
This file contains the declarations for the subclasses of Constant, which represent the different fla...
FunctionModRefBehavior getModRefBehavior(const Function *F)
getModRefBehavior - Return the behavior of the specified function if called from the specified call s...
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:383
Represent the analysis usage information of a pass.
Analysis pass providing a never-invalidated alias analysis result.
GlobalsAAResult Result
Representation for a specific memory location.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:418
GlobalsAAResult & getResult()
Module.h This file contains the declarations for the Module class.
Provides information about what library functions are available for the current target.
amdgpu Simplify well known AMD library false Value Value * Arg
The basic data container for the call graph of a Module of IR.
Definition: CallGraph.h:74
ModulePass * createGlobalsAAWrapperPass()
#define I(x, y, z)
Definition: MD5.cpp:58
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:225
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc)
LLVM Value Representation.
Definition: Value.h:73
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Value handle with callbacks on RAUW and destruction.
Definition: ValueHandle.h:389
A container for analyses that lazily runs them and caches their results.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:71
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc)