CFLSteensAliasAnalysis.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //==- CFLSteensAliasAnalysis.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 unification-based alias analysis
  10. /// implemented with CFL graph reachability.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ANALYSIS_CFLSTEENSALIASANALYSIS_H
  14. #define LLVM_ANALYSIS_CFLSTEENSALIASANALYSIS_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/Analysis/MemoryLocation.h"
  20. #include "llvm/IR/PassManager.h"
  21. #include "llvm/Pass.h"
  22. #include "llvm/Support/Casting.h"
  23. #include <forward_list>
  24. #include <memory>
  25. namespace llvm {
  26. class Function;
  27. class TargetLibraryInfo;
  28. namespace cflaa {
  29. struct AliasSummary;
  30. } // end namespace cflaa
  31. class CFLSteensAAResult : public AAResultBase<CFLSteensAAResult> {
  32. friend AAResultBase<CFLSteensAAResult>;
  33. class FunctionInfo;
  34. public:
  35. explicit CFLSteensAAResult(
  36. std::function<const TargetLibraryInfo &(Function &)> GetTLI);
  37. CFLSteensAAResult(CFLSteensAAResult &&Arg);
  38. ~CFLSteensAAResult();
  39. /// Handle invalidation events from the new pass manager.
  40. ///
  41. /// By definition, this result is stateless and so remains valid.
  42. bool invalidate(Function &, const PreservedAnalyses &,
  43. FunctionAnalysisManager::Invalidator &) {
  44. return false;
  45. }
  46. /// Inserts the given Function into the cache.
  47. void scan(Function *Fn);
  48. void evict(Function *Fn);
  49. /// Ensures that the given function is available in the cache.
  50. /// Returns the appropriate entry from the cache.
  51. const Optional<FunctionInfo> &ensureCached(Function *Fn);
  52. /// Get the alias summary for the given function
  53. /// Return nullptr if the summary is not found or not available
  54. const cflaa::AliasSummary *getAliasSummary(Function &Fn);
  55. AliasResult query(const MemoryLocation &LocA, const MemoryLocation &LocB);
  56. AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
  57. AAQueryInfo &AAQI) {
  58. if (LocA.Ptr == LocB.Ptr)
  59. return AliasResult::MustAlias;
  60. // Comparisons between global variables and other constants should be
  61. // handled by BasicAA.
  62. // CFLSteensAA may report NoAlias when comparing a GlobalValue and
  63. // ConstantExpr, but every query needs to have at least one Value tied to a
  64. // Function, and neither GlobalValues nor ConstantExprs are.
  65. if (isa<Constant>(LocA.Ptr) && isa<Constant>(LocB.Ptr))
  66. return AAResultBase::alias(LocA, LocB, AAQI);
  67. AliasResult QueryResult = query(LocA, LocB);
  68. if (QueryResult == AliasResult::MayAlias)
  69. return AAResultBase::alias(LocA, LocB, AAQI);
  70. return QueryResult;
  71. }
  72. private:
  73. std::function<const TargetLibraryInfo &(Function &)> GetTLI;
  74. /// Cached mapping of Functions to their StratifiedSets.
  75. /// If a function's sets are currently being built, it is marked
  76. /// in the cache as an Optional without a value. This way, if we
  77. /// have any kind of recursion, it is discernable from a function
  78. /// that simply has empty sets.
  79. DenseMap<Function *, Optional<FunctionInfo>> Cache;
  80. std::forward_list<cflaa::FunctionHandle<CFLSteensAAResult>> Handles;
  81. FunctionInfo buildSetsFrom(Function *F);
  82. };
  83. /// Analysis pass providing a never-invalidated alias analysis result.
  84. ///
  85. /// FIXME: We really should refactor CFL to use the analysis more heavily, and
  86. /// in particular to leverage invalidation to trigger re-computation of sets.
  87. class CFLSteensAA : public AnalysisInfoMixin<CFLSteensAA> {
  88. friend AnalysisInfoMixin<CFLSteensAA>;
  89. static AnalysisKey Key;
  90. public:
  91. using Result = CFLSteensAAResult;
  92. CFLSteensAAResult run(Function &F, FunctionAnalysisManager &AM);
  93. };
  94. /// Legacy wrapper pass to provide the CFLSteensAAResult object.
  95. class CFLSteensAAWrapperPass : public ImmutablePass {
  96. std::unique_ptr<CFLSteensAAResult> Result;
  97. public:
  98. static char ID;
  99. CFLSteensAAWrapperPass();
  100. CFLSteensAAResult &getResult() { return *Result; }
  101. const CFLSteensAAResult &getResult() const { return *Result; }
  102. void initializePass() override;
  103. void getAnalysisUsage(AnalysisUsage &AU) const override;
  104. };
  105. // createCFLSteensAAWrapperPass - This pass implements a set-based approach to
  106. // alias analysis.
  107. ImmutablePass *createCFLSteensAAWrapperPass();
  108. } // end namespace llvm
  109. #endif // LLVM_ANALYSIS_CFLSTEENSALIASANALYSIS_H