GlobalsModRef.h 6.0 KB

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