Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:13

0001 //===- GlobalsModRef.h - Simple Mod/Ref AA for Globals ----------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 /// \file
0009 /// This is the interface for a simple mod/ref and alias analysis over globals.
0010 ///
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_ANALYSIS_GLOBALSMODREF_H
0014 #define LLVM_ANALYSIS_GLOBALSMODREF_H
0015 
0016 #include "llvm/Analysis/AliasAnalysis.h"
0017 #include "llvm/IR/PassManager.h"
0018 #include "llvm/IR/ValueHandle.h"
0019 #include "llvm/Pass.h"
0020 #include <list>
0021 
0022 namespace llvm {
0023 class CallGraph;
0024 class Function;
0025 
0026 /// An alias analysis result set for globals.
0027 ///
0028 /// This focuses on handling aliasing properties of globals and interprocedural
0029 /// function call mod/ref information.
0030 class GlobalsAAResult : public AAResultBase {
0031   class FunctionInfo;
0032 
0033   const DataLayout &DL;
0034   std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
0035 
0036   /// The globals that do not have their addresses taken.
0037   SmallPtrSet<const GlobalValue *, 8> NonAddressTakenGlobals;
0038 
0039   /// Are there functions with local linkage that may modify globals.
0040   bool UnknownFunctionsWithLocalLinkage = false;
0041 
0042   /// IndirectGlobals - The memory pointed to by this global is known to be
0043   /// 'owned' by the global.
0044   SmallPtrSet<const GlobalValue *, 8> IndirectGlobals;
0045 
0046   /// AllocsForIndirectGlobals - If an instruction allocates memory for an
0047   /// indirect global, this map indicates which one.
0048   DenseMap<const Value *, const GlobalValue *> AllocsForIndirectGlobals;
0049 
0050   /// For each function, keep track of what globals are modified or read.
0051   DenseMap<const Function *, FunctionInfo> FunctionInfos;
0052 
0053   /// A map of functions to SCC. The SCCs are described by a simple integer
0054   /// ID that is only useful for comparing for equality (are two functions
0055   /// in the same SCC or not?)
0056   DenseMap<const Function *, unsigned> FunctionToSCCMap;
0057 
0058   /// Handle to clear this analysis on deletion of values.
0059   struct DeletionCallbackHandle final : CallbackVH {
0060     GlobalsAAResult *GAR;
0061     std::list<DeletionCallbackHandle>::iterator I;
0062 
0063     DeletionCallbackHandle(GlobalsAAResult &GAR, Value *V)
0064         : CallbackVH(V), GAR(&GAR) {}
0065 
0066     void deleted() override;
0067   };
0068 
0069   /// List of callbacks for globals being tracked by this analysis. Note that
0070   /// these objects are quite large, but we only anticipate having one per
0071   /// global tracked by this analysis. There are numerous optimizations we
0072   /// could perform to the memory utilization here if this becomes a problem.
0073   std::list<DeletionCallbackHandle> Handles;
0074 
0075   explicit GlobalsAAResult(
0076       const DataLayout &DL,
0077       std::function<const TargetLibraryInfo &(Function &F)> GetTLI);
0078 
0079   friend struct RecomputeGlobalsAAPass;
0080 
0081 public:
0082   GlobalsAAResult(GlobalsAAResult &&Arg);
0083   ~GlobalsAAResult();
0084 
0085   bool invalidate(Module &M, const PreservedAnalyses &PA,
0086                   ModuleAnalysisManager::Invalidator &);
0087 
0088   static GlobalsAAResult
0089   analyzeModule(Module &M,
0090                 std::function<const TargetLibraryInfo &(Function &F)> GetTLI,
0091                 CallGraph &CG);
0092 
0093   //------------------------------------------------
0094   // Implement the AliasAnalysis API
0095   //
0096   AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
0097                     AAQueryInfo &AAQI, const Instruction *CtxI);
0098 
0099   using AAResultBase::getModRefInfo;
0100   ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc,
0101                            AAQueryInfo &AAQI);
0102 
0103   using AAResultBase::getMemoryEffects;
0104   /// getMemoryEffects - Return the behavior of the specified function if
0105   /// called from the specified call site.  The call site may be null in which
0106   /// case the most generic behavior of this function should be returned.
0107   MemoryEffects getMemoryEffects(const Function *F);
0108 
0109 private:
0110   FunctionInfo *getFunctionInfo(const Function *F);
0111 
0112   void AnalyzeGlobals(Module &M);
0113   void AnalyzeCallGraph(CallGraph &CG, Module &M);
0114   bool AnalyzeUsesOfPointer(Value *V,
0115                             SmallPtrSetImpl<Function *> *Readers = nullptr,
0116                             SmallPtrSetImpl<Function *> *Writers = nullptr,
0117                             GlobalValue *OkayStoreDest = nullptr);
0118   bool AnalyzeIndirectGlobalMemory(GlobalVariable *GV);
0119   void CollectSCCMembership(CallGraph &CG);
0120 
0121   bool isNonEscapingGlobalNoAlias(const GlobalValue *GV, const Value *V);
0122   ModRefInfo getModRefInfoForArgument(const CallBase *Call,
0123                                       const GlobalValue *GV, AAQueryInfo &AAQI);
0124 };
0125 
0126 /// Analysis pass providing a never-invalidated alias analysis result.
0127 class GlobalsAA : public AnalysisInfoMixin<GlobalsAA> {
0128   friend AnalysisInfoMixin<GlobalsAA>;
0129   static AnalysisKey Key;
0130 
0131 public:
0132   typedef GlobalsAAResult Result;
0133 
0134   GlobalsAAResult run(Module &M, ModuleAnalysisManager &AM);
0135 };
0136 
0137 struct RecomputeGlobalsAAPass : PassInfoMixin<RecomputeGlobalsAAPass> {
0138   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
0139 };
0140 
0141 /// Legacy wrapper pass to provide the GlobalsAAResult object.
0142 class GlobalsAAWrapperPass : public ModulePass {
0143   std::unique_ptr<GlobalsAAResult> Result;
0144 
0145 public:
0146   static char ID;
0147 
0148   GlobalsAAWrapperPass();
0149 
0150   GlobalsAAResult &getResult() { return *Result; }
0151   const GlobalsAAResult &getResult() const { return *Result; }
0152 
0153   bool runOnModule(Module &M) override;
0154   bool doFinalization(Module &M) override;
0155   void getAnalysisUsage(AnalysisUsage &AU) const override;
0156 };
0157 
0158 //===--------------------------------------------------------------------===//
0159 //
0160 // createGlobalsAAWrapperPass - This pass provides alias and mod/ref info for
0161 // global values that do not have their addresses taken.
0162 //
0163 ModulePass *createGlobalsAAWrapperPass();
0164 }
0165 
0166 #endif