LLVM  10.0.0svn
GlobalsModRef.h
Go to the documentation of this file.
1 //===- GlobalsModRef.h - Simple Mod/Ref AA for Globals ----------*- C++ -*-===//
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 /// \file
9 /// This is the interface for a simple mod/ref and alias analysis over globals.
10 ///
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_ANALYSIS_GLOBALSMODREF_H
14 #define LLVM_ANALYSIS_GLOBALSMODREF_H
15 
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/IR/ValueHandle.h"
22 #include "llvm/Pass.h"
23 #include <list>
24 
25 namespace llvm {
26 
27 /// An alias analysis result set for globals.
28 ///
29 /// This focuses on handling aliasing properties of globals and interprocedural
30 /// function call mod/ref information.
31 class GlobalsAAResult : public AAResultBase<GlobalsAAResult> {
33 
34  class FunctionInfo;
35 
36  const DataLayout &DL;
37  const TargetLibraryInfo &TLI;
38 
39  /// The globals that do not have their addresses taken.
40  SmallPtrSet<const GlobalValue *, 8> NonAddressTakenGlobals;
41 
42  /// IndirectGlobals - The memory pointed to by this global is known to be
43  /// 'owned' by the global.
45 
46  /// AllocsForIndirectGlobals - If an instruction allocates memory for an
47  /// indirect global, this map indicates which one.
48  DenseMap<const Value *, const GlobalValue *> AllocsForIndirectGlobals;
49 
50  /// For each function, keep track of what globals are modified or read.
52 
53  /// A map of functions to SCC. The SCCs are described by a simple integer
54  /// ID that is only useful for comparing for equality (are two functions
55  /// in the same SCC or not?)
56  DenseMap<const Function *, unsigned> FunctionToSCCMap;
57 
58  /// Handle to clear this analysis on deletion of values.
59  struct DeletionCallbackHandle final : CallbackVH {
60  GlobalsAAResult *GAR;
61  std::list<DeletionCallbackHandle>::iterator I;
62 
63  DeletionCallbackHandle(GlobalsAAResult &GAR, Value *V)
64  : CallbackVH(V), GAR(&GAR) {}
65 
66  void deleted() override;
67  };
68 
69  /// List of callbacks for globals being tracked by this analysis. Note that
70  /// these objects are quite large, but we only anticipate having one per
71  /// global tracked by this analysis. There are numerous optimizations we
72  /// could perform to the memory utilization here if this becomes a problem.
73  std::list<DeletionCallbackHandle> Handles;
74 
75  explicit GlobalsAAResult(const DataLayout &DL, const TargetLibraryInfo &TLI);
76 
77 public:
80 
82  CallGraph &CG);
83 
84  //------------------------------------------------
85  // Implement the AliasAnalysis API
86  //
87  AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
88  AAQueryInfo &AAQI);
89 
91  ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc,
92  AAQueryInfo &AAQI);
93 
94  /// getModRefBehavior - Return the behavior of the specified function if
95  /// called from the specified call site. The call site may be null in which
96  /// case the most generic behavior of this function should be returned.
98 
99  /// getModRefBehavior - Return the behavior of the specified function if
100  /// called from the specified call site. The call site may be null in which
101  /// case the most generic behavior of this function should be returned.
103 
104 private:
105  FunctionInfo *getFunctionInfo(const Function *F);
106 
107  void AnalyzeGlobals(Module &M);
108  void AnalyzeCallGraph(CallGraph &CG, Module &M);
109  bool AnalyzeUsesOfPointer(Value *V,
110  SmallPtrSetImpl<Function *> *Readers = nullptr,
111  SmallPtrSetImpl<Function *> *Writers = nullptr,
112  GlobalValue *OkayStoreDest = nullptr);
113  bool AnalyzeIndirectGlobalMemory(GlobalVariable *GV);
114  void CollectSCCMembership(CallGraph &CG);
115 
116  bool isNonEscapingGlobalNoAlias(const GlobalValue *GV, const Value *V);
117  ModRefInfo getModRefInfoForArgument(const CallBase *Call,
118  const GlobalValue *GV, AAQueryInfo &AAQI);
119 };
120 
121 /// Analysis pass providing a never-invalidated alias analysis result.
122 class GlobalsAA : public AnalysisInfoMixin<GlobalsAA> {
124  static AnalysisKey Key;
125 
126 public:
128 
130 };
131 
132 /// Legacy wrapper pass to provide the GlobalsAAResult object.
134  std::unique_ptr<GlobalsAAResult> Result;
135 
136 public:
137  static char ID;
138 
140 
141  GlobalsAAResult &getResult() { return *Result; }
142  const GlobalsAAResult &getResult() const { return *Result; }
143 
144  bool runOnModule(Module &M) override;
145  bool doFinalization(Module &M) override;
146  void getAnalysisUsage(AnalysisUsage &AU) const override;
147 };
148 
149 //===--------------------------------------------------------------------===//
150 //
151 // createGlobalsAAWrapperPass - This pass provides alias and mod/ref info for
152 // global values that do not have their addresses taken.
153 //
155 }
156 
157 #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:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
This class stores info we want to provide to or retain within an alias query.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1100
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:343
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)
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
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
An alias analysis result set for globals.
Definition: GlobalsModRef.h:31
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:389
Represent the analysis usage information of a pass.
Analysis pass providing a never-invalidated alias analysis result.
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
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:417
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.
The basic data container for the call graph of a Module of IR.
Definition: CallGraph.h:73
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:224
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:379
A container for analyses that lazily runs them and caches their results.
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:70
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI)
alias - If one of the pointers is to a global that we are tracking, and the other is some random poin...