CFLAndersAliasAnalysis.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //==- CFLAndersAliasAnalysis.h - Unification-based Alias Analysis -*- 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 LLVM's inclusion-based alias analysis
  10. /// implemented with CFL graph reachability.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ANALYSIS_CFLANDERSALIASANALYSIS_H
  14. #define LLVM_ANALYSIS_CFLANDERSALIASANALYSIS_H
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/Optional.h"
  17. #include "llvm/Analysis/AliasAnalysis.h"
  18. #include "llvm/Analysis/CFLAliasAnalysisUtils.h"
  19. #include "llvm/IR/PassManager.h"
  20. #include "llvm/Pass.h"
  21. #include <forward_list>
  22. #include <memory>
  23. namespace llvm {
  24. class Function;
  25. class MemoryLocation;
  26. class TargetLibraryInfo;
  27. namespace cflaa {
  28. struct AliasSummary;
  29. } // end namespace cflaa
  30. class CFLAndersAAResult : public AAResultBase<CFLAndersAAResult> {
  31. friend AAResultBase<CFLAndersAAResult>;
  32. class FunctionInfo;
  33. public:
  34. explicit CFLAndersAAResult(
  35. std::function<const TargetLibraryInfo &(Function &F)> GetTLI);
  36. CFLAndersAAResult(CFLAndersAAResult &&RHS);
  37. ~CFLAndersAAResult();
  38. /// Handle invalidation events from the new pass manager.
  39. /// By definition, this result is stateless and so remains valid.
  40. bool invalidate(Function &, const PreservedAnalyses &,
  41. FunctionAnalysisManager::Invalidator &) {
  42. return false;
  43. }
  44. /// Evict the given function from cache
  45. void evict(const Function *Fn);
  46. /// Get the alias summary for the given function
  47. /// Return nullptr if the summary is not found or not available
  48. const cflaa::AliasSummary *getAliasSummary(const Function &);
  49. AliasResult query(const MemoryLocation &, const MemoryLocation &);
  50. AliasResult alias(const MemoryLocation &, const MemoryLocation &,
  51. AAQueryInfo &);
  52. private:
  53. /// Ensures that the given function is available in the cache.
  54. /// Returns the appropriate entry from the cache.
  55. const Optional<FunctionInfo> &ensureCached(const Function &);
  56. /// Inserts the given Function into the cache.
  57. void scan(const Function &);
  58. /// Build summary for a given function
  59. FunctionInfo buildInfoFrom(const Function &);
  60. std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
  61. /// Cached mapping of Functions to their StratifiedSets.
  62. /// If a function's sets are currently being built, it is marked
  63. /// in the cache as an Optional without a value. This way, if we
  64. /// have any kind of recursion, it is discernable from a function
  65. /// that simply has empty sets.
  66. DenseMap<const Function *, Optional<FunctionInfo>> Cache;
  67. std::forward_list<cflaa::FunctionHandle<CFLAndersAAResult>> Handles;
  68. };
  69. /// Analysis pass providing a never-invalidated alias analysis result.
  70. ///
  71. /// FIXME: We really should refactor CFL to use the analysis more heavily, and
  72. /// in particular to leverage invalidation to trigger re-computation.
  73. class CFLAndersAA : public AnalysisInfoMixin<CFLAndersAA> {
  74. friend AnalysisInfoMixin<CFLAndersAA>;
  75. static AnalysisKey Key;
  76. public:
  77. using Result = CFLAndersAAResult;
  78. CFLAndersAAResult run(Function &F, FunctionAnalysisManager &AM);
  79. };
  80. /// Legacy wrapper pass to provide the CFLAndersAAResult object.
  81. class CFLAndersAAWrapperPass : public ImmutablePass {
  82. std::unique_ptr<CFLAndersAAResult> Result;
  83. public:
  84. static char ID;
  85. CFLAndersAAWrapperPass();
  86. CFLAndersAAResult &getResult() { return *Result; }
  87. const CFLAndersAAResult &getResult() const { return *Result; }
  88. void initializePass() override;
  89. void getAnalysisUsage(AnalysisUsage &AU) const override;
  90. };
  91. // createCFLAndersAAWrapperPass - This pass implements a set-based approach to
  92. // alias analysis.
  93. ImmutablePass *createCFLAndersAAWrapperPass();
  94. } // end namespace llvm
  95. #endif // LLVM_ANALYSIS_CFLANDERSALIASANALYSIS_H