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 
16 #include "llvm/Analysis/AliasAnalysis.h"
17 #include "llvm/Analysis/CallGraph.h"
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> {
32   friend AAResultBase<GlobalsAAResult>;
33 
34   class FunctionInfo;
35 
36   const DataLayout &DL;
37   std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
38 
39   /// The globals that do not have their addresses taken.
40   SmallPtrSet<const GlobalValue *, 8> NonAddressTakenGlobals;
41 
42   /// Are there functions with local linkage that may modify globals.
43   bool UnknownFunctionsWithLocalLinkage = false;
44 
45   /// IndirectGlobals - The memory pointed to by this global is known to be
46   /// 'owned' by the global.
47   SmallPtrSet<const GlobalValue *, 8> IndirectGlobals;
48 
49   /// AllocsForIndirectGlobals - If an instruction allocates memory for an
50   /// indirect global, this map indicates which one.
51   DenseMap<const Value *, const GlobalValue *> AllocsForIndirectGlobals;
52 
53   /// For each function, keep track of what globals are modified or read.
54   DenseMap<const Function *, FunctionInfo> FunctionInfos;
55 
56   /// A map of functions to SCC. The SCCs are described by a simple integer
57   /// ID that is only useful for comparing for equality (are two functions
58   /// in the same SCC or not?)
59   DenseMap<const Function *, unsigned> FunctionToSCCMap;
60 
61   /// Handle to clear this analysis on deletion of values.
62   struct DeletionCallbackHandle final : CallbackVH {
63     GlobalsAAResult *GAR;
64     std::list<DeletionCallbackHandle>::iterator I;
65 
66     DeletionCallbackHandle(GlobalsAAResult &GAR, Value *V)
67         : CallbackVH(V), GAR(&GAR) {}
68 
69     void deleted() override;
70   };
71 
72   /// List of callbacks for globals being tracked by this analysis. Note that
73   /// these objects are quite large, but we only anticipate having one per
74   /// global tracked by this analysis. There are numerous optimizations we
75   /// could perform to the memory utilization here if this becomes a problem.
76   std::list<DeletionCallbackHandle> Handles;
77 
78   explicit GlobalsAAResult(
79       const DataLayout &DL,
80       std::function<const TargetLibraryInfo &(Function &F)> GetTLI);
81 
82 public:
83   GlobalsAAResult(GlobalsAAResult &&Arg);
84   ~GlobalsAAResult();
85 
86   static GlobalsAAResult
87   analyzeModule(Module &M,
88                 std::function<const TargetLibraryInfo &(Function &F)> GetTLI,
89                 CallGraph &CG);
90 
91   //------------------------------------------------
92   // Implement the AliasAnalysis API
93   //
94   AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
95                     AAQueryInfo &AAQI);
96 
97   using AAResultBase::getModRefInfo;
98   ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc,
99                            AAQueryInfo &AAQI);
100 
101   /// getModRefBehavior - Return the behavior of the specified function if
102   /// called from the specified call site.  The call site may be null in which
103   /// case the most generic behavior of this function should be returned.
104   FunctionModRefBehavior getModRefBehavior(const Function *F);
105 
106   /// getModRefBehavior - Return the behavior of the specified function if
107   /// called from the specified call site.  The call site may be null in which
108   /// case the most generic behavior of this function should be returned.
109   FunctionModRefBehavior getModRefBehavior(const CallBase *Call);
110 
111 private:
112   FunctionInfo *getFunctionInfo(const Function *F);
113 
114   void AnalyzeGlobals(Module &M);
115   void AnalyzeCallGraph(CallGraph &CG, Module &M);
116   bool AnalyzeUsesOfPointer(Value *V,
117                             SmallPtrSetImpl<Function *> *Readers = nullptr,
118                             SmallPtrSetImpl<Function *> *Writers = nullptr,
119                             GlobalValue *OkayStoreDest = nullptr);
120   bool AnalyzeIndirectGlobalMemory(GlobalVariable *GV);
121   void CollectSCCMembership(CallGraph &CG);
122 
123   bool isNonEscapingGlobalNoAlias(const GlobalValue *GV, const Value *V);
124   ModRefInfo getModRefInfoForArgument(const CallBase *Call,
125                                       const GlobalValue *GV, AAQueryInfo &AAQI);
126 };
127 
128 /// Analysis pass providing a never-invalidated alias analysis result.
129 class GlobalsAA : public AnalysisInfoMixin<GlobalsAA> {
130   friend AnalysisInfoMixin<GlobalsAA>;
131   static AnalysisKey Key;
132 
133 public:
134   typedef GlobalsAAResult Result;
135 
136   GlobalsAAResult run(Module &M, ModuleAnalysisManager &AM);
137 };
138 
139 /// Legacy wrapper pass to provide the GlobalsAAResult object.
140 class GlobalsAAWrapperPass : public ModulePass {
141   std::unique_ptr<GlobalsAAResult> Result;
142 
143 public:
144   static char ID;
145 
146   GlobalsAAWrapperPass();
147 
148   GlobalsAAResult &getResult() { return *Result; }
149   const GlobalsAAResult &getResult() const { return *Result; }
150 
151   bool runOnModule(Module &M) override;
152   bool doFinalization(Module &M) override;
153   void getAnalysisUsage(AnalysisUsage &AU) const override;
154 };
155 
156 //===--------------------------------------------------------------------===//
157 //
158 // createGlobalsAAWrapperPass - This pass provides alias and mod/ref info for
159 // global values that do not have their addresses taken.
160 //
161 ModulePass *createGlobalsAAWrapperPass();
162 }
163 
164 #endif
165