Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- GlobalsModRef.h - Simple Mod/Ref AA for Globals ----------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | 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. |
| 44 | SmallPtrSet<const GlobalValue *, 8> IndirectGlobals; |
| 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. |
| 51 | DenseMap<const Function *, FunctionInfo> FunctionInfos; |
| 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: |
| 78 | GlobalsAAResult(GlobalsAAResult &&Arg); |
| 79 | ~GlobalsAAResult(); |
| 80 | |
| 81 | static GlobalsAAResult analyzeModule(Module &M, const TargetLibraryInfo &TLI, |
| 82 | CallGraph &CG); |
| 83 | |
| 84 | //------------------------------------------------ |
| 85 | // Implement the AliasAnalysis API |
| 86 | // |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 87 | AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, |
| 88 | AAQueryInfo &AAQI); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 89 | |
| 90 | using AAResultBase::getModRefInfo; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 91 | ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, |
| 92 | AAQueryInfo &AAQI); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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. |
| 97 | FunctionModRefBehavior getModRefBehavior(const Function *F); |
| 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. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 102 | FunctionModRefBehavior getModRefBehavior(const CallBase *Call); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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); |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 117 | ModRefInfo getModRefInfoForArgument(const CallBase *Call, |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 118 | const GlobalValue *GV, AAQueryInfo &AAQI); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | /// Analysis pass providing a never-invalidated alias analysis result. |
| 122 | class GlobalsAA : public AnalysisInfoMixin<GlobalsAA> { |
| 123 | friend AnalysisInfoMixin<GlobalsAA>; |
| 124 | static AnalysisKey Key; |
| 125 | |
| 126 | public: |
| 127 | typedef GlobalsAAResult Result; |
| 128 | |
| 129 | GlobalsAAResult run(Module &M, ModuleAnalysisManager &AM); |
| 130 | }; |
| 131 | |
| 132 | /// Legacy wrapper pass to provide the GlobalsAAResult object. |
| 133 | class GlobalsAAWrapperPass : public ModulePass { |
| 134 | std::unique_ptr<GlobalsAAResult> Result; |
| 135 | |
| 136 | public: |
| 137 | static char ID; |
| 138 | |
| 139 | GlobalsAAWrapperPass(); |
| 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 | // |
| 154 | ModulePass *createGlobalsAAWrapperPass(); |
| 155 | } |
| 156 | |
| 157 | #endif |