GlobalsModRef.h 6.1 KB

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