CFLAndersAliasAnalysis.h 4.1 KB

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