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/IR/PassManager.h" 18 #include "llvm/IR/ValueHandle.h" 19 #include "llvm/Pass.h" 20 #include <list> 21 22 namespace llvm { 23 class CallGraph; 24 class Function; 25 26 /// An alias analysis result set for globals. 27 /// 28 /// This focuses on handling aliasing properties of globals and interprocedural 29 /// function call mod/ref information. 30 class GlobalsAAResult : public AAResultBase<GlobalsAAResult> { 31 friend AAResultBase<GlobalsAAResult>; 32 33 class FunctionInfo; 34 35 const DataLayout &DL; 36 std::function<const TargetLibraryInfo &(Function &F)> GetTLI; 37 38 /// The globals that do not have their addresses taken. 39 SmallPtrSet<const GlobalValue *, 8> NonAddressTakenGlobals; 40 41 /// Are there functions with local linkage that may modify globals. 42 bool UnknownFunctionsWithLocalLinkage = false; 43 44 /// IndirectGlobals - The memory pointed to by this global is known to be 45 /// 'owned' by the global. 46 SmallPtrSet<const GlobalValue *, 8> IndirectGlobals; 47 48 /// AllocsForIndirectGlobals - If an instruction allocates memory for an 49 /// indirect global, this map indicates which one. 50 DenseMap<const Value *, const GlobalValue *> AllocsForIndirectGlobals; 51 52 /// For each function, keep track of what globals are modified or read. 53 DenseMap<const Function *, FunctionInfo> FunctionInfos; 54 55 /// A map of functions to SCC. The SCCs are described by a simple integer 56 /// ID that is only useful for comparing for equality (are two functions 57 /// in the same SCC or not?) 58 DenseMap<const Function *, unsigned> FunctionToSCCMap; 59 60 /// Handle to clear this analysis on deletion of values. 61 struct DeletionCallbackHandle final : CallbackVH { 62 GlobalsAAResult *GAR; 63 std::list<DeletionCallbackHandle>::iterator I; 64 65 DeletionCallbackHandle(GlobalsAAResult &GAR, Value *V) 66 : CallbackVH(V), GAR(&GAR) {} 67 68 void deleted() override; 69 }; 70 71 /// List of callbacks for globals being tracked by this analysis. Note that 72 /// these objects are quite large, but we only anticipate having one per 73 /// global tracked by this analysis. There are numerous optimizations we 74 /// could perform to the memory utilization here if this becomes a problem. 75 std::list<DeletionCallbackHandle> Handles; 76 77 explicit GlobalsAAResult( 78 const DataLayout &DL, 79 std::function<const TargetLibraryInfo &(Function &F)> GetTLI); 80 81 friend struct RecomputeGlobalsAAPass; 82 83 public: 84 GlobalsAAResult(GlobalsAAResult &&Arg); 85 ~GlobalsAAResult(); 86 87 bool invalidate(Module &M, const PreservedAnalyses &PA, 88 ModuleAnalysisManager::Invalidator &); 89 90 static GlobalsAAResult 91 analyzeModule(Module &M, 92 std::function<const TargetLibraryInfo &(Function &F)> GetTLI, 93 CallGraph &CG); 94 95 //------------------------------------------------ 96 // Implement the AliasAnalysis API 97 // 98 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, 99 AAQueryInfo &AAQI); 100 101 using AAResultBase::getModRefInfo; 102 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, 103 AAQueryInfo &AAQI); 104 105 using AAResultBase::getModRefBehavior; 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 Function *F); 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 struct RecomputeGlobalsAAPass : PassInfoMixin<RecomputeGlobalsAAPass> { 140 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); 141 }; 142 143 /// Legacy wrapper pass to provide the GlobalsAAResult object. 144 class GlobalsAAWrapperPass : public ModulePass { 145 std::unique_ptr<GlobalsAAResult> Result; 146 147 public: 148 static char ID; 149 150 GlobalsAAWrapperPass(); 151 152 GlobalsAAResult &getResult() { return *Result; } 153 const GlobalsAAResult &getResult() const { return *Result; } 154 155 bool runOnModule(Module &M) override; 156 bool doFinalization(Module &M) override; 157 void getAnalysisUsage(AnalysisUsage &AU) const override; 158 }; 159 160 //===--------------------------------------------------------------------===// 161 // 162 // createGlobalsAAWrapperPass - This pass provides alias and mod/ref info for 163 // global values that do not have their addresses taken. 164 // 165 ModulePass *createGlobalsAAWrapperPass(); 166 } 167 168 #endif 169