CFLSteensAliasAnalysis.h 4.7 KB

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