Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //==- CFLAndersAliasAnalysis.h - Unification-based Alias Analysis -*- C++-*-==// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
| 14 | #ifndef LLVM_ANALYSIS_CFLANDERSALIASANALYSIS_H |
| 15 | #define LLVM_ANALYSIS_CFLANDERSALIASANALYSIS_H |
| 16 | |
| 17 | #include "llvm/ADT/DenseMap.h" |
| 18 | #include "llvm/ADT/Optional.h" |
| 19 | #include "llvm/Analysis/AliasAnalysis.h" |
| 20 | #include "llvm/Analysis/CFLAliasAnalysisUtils.h" |
| 21 | #include "llvm/IR/PassManager.h" |
| 22 | #include "llvm/Pass.h" |
| 23 | #include <forward_list> |
| 24 | #include <memory> |
| 25 | |
| 26 | namespace llvm { |
| 27 | |
| 28 | class Function; |
| 29 | class MemoryLocation; |
| 30 | class TargetLibraryInfo; |
| 31 | |
| 32 | namespace cflaa { |
| 33 | |
| 34 | struct AliasSummary; |
| 35 | |
| 36 | } // end namespace cflaa |
| 37 | |
| 38 | class CFLAndersAAResult : public AAResultBase<CFLAndersAAResult> { |
| 39 | friend AAResultBase<CFLAndersAAResult>; |
| 40 | |
| 41 | class FunctionInfo; |
| 42 | |
| 43 | public: |
| 44 | explicit CFLAndersAAResult(const TargetLibraryInfo &TLI); |
| 45 | CFLAndersAAResult(CFLAndersAAResult &&RHS); |
| 46 | ~CFLAndersAAResult(); |
| 47 | |
| 48 | /// Handle invalidation events from the new pass manager. |
| 49 | /// By definition, this result is stateless and so remains valid. |
| 50 | bool invalidate(Function &, const PreservedAnalyses &, |
| 51 | FunctionAnalysisManager::Invalidator &) { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | /// Evict the given function from cache |
| 56 | void evict(const Function *Fn); |
| 57 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 58 | /// Get the alias summary for the given function |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 59 | /// Return nullptr if the summary is not found or not available |
| 60 | const cflaa::AliasSummary *getAliasSummary(const Function &); |
| 61 | |
| 62 | AliasResult query(const MemoryLocation &, const MemoryLocation &); |
| 63 | AliasResult alias(const MemoryLocation &, const MemoryLocation &); |
| 64 | |
| 65 | private: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 66 | /// Ensures that the given function is available in the cache. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 67 | /// Returns the appropriate entry from the cache. |
| 68 | const Optional<FunctionInfo> &ensureCached(const Function &); |
| 69 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 70 | /// Inserts the given Function into the cache. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 71 | void scan(const Function &); |
| 72 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 73 | /// Build summary for a given function |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 74 | FunctionInfo buildInfoFrom(const Function &); |
| 75 | |
| 76 | const TargetLibraryInfo &TLI; |
| 77 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 78 | /// Cached mapping of Functions to their StratifiedSets. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 79 | /// If a function's sets are currently being built, it is marked |
| 80 | /// in the cache as an Optional without a value. This way, if we |
| 81 | /// have any kind of recursion, it is discernable from a function |
| 82 | /// that simply has empty sets. |
| 83 | DenseMap<const Function *, Optional<FunctionInfo>> Cache; |
| 84 | |
| 85 | std::forward_list<cflaa::FunctionHandle<CFLAndersAAResult>> Handles; |
| 86 | }; |
| 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. |
| 92 | class CFLAndersAA : public AnalysisInfoMixin<CFLAndersAA> { |
| 93 | friend AnalysisInfoMixin<CFLAndersAA>; |
| 94 | |
| 95 | static AnalysisKey Key; |
| 96 | |
| 97 | public: |
| 98 | using Result = CFLAndersAAResult; |
| 99 | |
| 100 | CFLAndersAAResult run(Function &F, FunctionAnalysisManager &AM); |
| 101 | }; |
| 102 | |
| 103 | /// Legacy wrapper pass to provide the CFLAndersAAResult object. |
| 104 | class CFLAndersAAWrapperPass : public ImmutablePass { |
| 105 | std::unique_ptr<CFLAndersAAResult> Result; |
| 106 | |
| 107 | public: |
| 108 | static char ID; |
| 109 | |
| 110 | CFLAndersAAWrapperPass(); |
| 111 | |
| 112 | CFLAndersAAResult &getResult() { return *Result; } |
| 113 | const CFLAndersAAResult &getResult() const { return *Result; } |
| 114 | |
| 115 | void initializePass() override; |
| 116 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 117 | }; |
| 118 | |
| 119 | // createCFLAndersAAWrapperPass - This pass implements a set-based approach to |
| 120 | // alias analysis. |
| 121 | ImmutablePass *createCFLAndersAAWrapperPass(); |
| 122 | |
| 123 | } // end namespace llvm |
| 124 | |
| 125 | #endif // LLVM_ANALYSIS_CFLANDERSALIASANALYSIS_H |