GlobalsModRef.h 6.0 KB

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