|
| 1 | +#pragma once |
| 2 | + |
| 3 | +/****************************************************************************** |
| 4 | + * Copyright (c) 2026 Fabian Schiebel. |
| 5 | + * All rights reserved. This program and the accompanying materials are made |
| 6 | + * available under the terms of LICENSE.txt. |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Fabian Schiebel and others |
| 10 | + *****************************************************************************/ |
| 11 | + |
| 12 | +#include "phasar/PhasarLLVM/ControlFlow/LLVMBasedCallGraph.h" |
| 13 | +#include "phasar/PhasarLLVM/Pointer/LLVMPointerAssignmentGraph.h" |
| 14 | +#include "phasar/PhasarLLVM/Pointer/LLVMUnionFindAA.h" |
| 15 | +#include "phasar/Pointer/RawAliasSet.h" |
| 16 | +#include "phasar/Pointer/UnionFindAA.h" |
| 17 | +#include "phasar/Utils/MaybeUniquePtr.h" |
| 18 | +#include "phasar/Utils/NonNullPtr.h" |
| 19 | +#include "phasar/Utils/Soundness.h" |
| 20 | +#include "phasar/Utils/TypedVector.h" |
| 21 | +#include "phasar/Utils/ValueCompressor.h" |
| 22 | + |
| 23 | +#include "llvm/ADT/ArrayRef.h" |
| 24 | + |
| 25 | +namespace llvm { |
| 26 | +class Function; |
| 27 | +} // namespace llvm |
| 28 | + |
| 29 | +namespace psr { |
| 30 | + |
| 31 | +class LLVMProjectIRDB; |
| 32 | + |
| 33 | +/// Alias-analysis result for the Andersen-style OTF points-to analysis. |
| 34 | +/// |
| 35 | +/// Two values may-alias iff their points-to sets share at least one abstract |
| 36 | +/// object. Satisfies \c UnionFindAAResult so it can be wrapped by |
| 37 | +/// \c LLVMUnionFindAliasIterator. |
| 38 | +struct AndersenOTFResult { |
| 39 | + TypedVector<ValueId, RawAliasSet<ValueId>> AliasSets; |
| 40 | + LLVMBasedCallGraph CG; |
| 41 | + |
| 42 | + [[nodiscard]] static constexpr bool isCached() noexcept { return true; } |
| 43 | + [[nodiscard]] constexpr size_t size() const noexcept { |
| 44 | + return AliasSets.size(); |
| 45 | + } |
| 46 | + |
| 47 | + [[nodiscard]] RawAliasSet<ValueId> |
| 48 | + getRawAliasSet(ValueId Var) const noexcept { |
| 49 | + if (!AliasSets.inbounds(Var)) { |
| 50 | + return {}; |
| 51 | + } |
| 52 | + return AliasSets[Var]; |
| 53 | + } |
| 54 | + |
| 55 | + [[nodiscard]] bool mayAlias(ValueId Var1, ValueId Var2) const noexcept { |
| 56 | + if (Var1 == Var2) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + if (!AliasSets.inbounds(Var1)) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + return AliasSets[Var1].contains(Var2); |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +static_assert(UnionFindAAResult<AndersenOTFResult>); |
| 67 | + |
| 68 | +/// Andersen-style inclusion-based points-to analysis that co-refines the call |
| 69 | +/// graph and points-to sets in a single fixpoint. |
| 70 | +/// |
| 71 | +/// Unlike the staged pipeline (resolver → PA), this solver owns its own |
| 72 | +/// function-worklist loop: direct calls add callees immediately; indirect |
| 73 | +/// calls are resolved as \c pts(fp) grows. |
| 74 | +/// |
| 75 | +/// Phase 1: context- and field-insensitive. |
| 76 | +class AndersenOTFSolver { |
| 77 | +public: |
| 78 | + explicit AndersenOTFSolver(const LLVMProjectIRDB &IRDB, |
| 79 | + llvm::ArrayRef<const llvm::Function *> Entries, |
| 80 | + ValueCompressor<PAGVariable> &VC, |
| 81 | + Soundness S = Soundness::Soundy) noexcept; |
| 82 | + |
| 83 | + /// Run the full OTF fixpoint and return the alias-analysis result. |
| 84 | + [[nodiscard]] AndersenOTFResult solve(); |
| 85 | + |
| 86 | +private: |
| 87 | + struct SolverData; |
| 88 | + |
| 89 | + NonNullPtr<const LLVMProjectIRDB> IRDB; |
| 90 | + llvm::ArrayRef<const llvm::Function *> Entries; |
| 91 | + NonNullPtr<ValueCompressor<PAGVariable>> VC; |
| 92 | + Soundness S; |
| 93 | +}; |
| 94 | + |
| 95 | +// ---- Factory functions ------------------------------------------------ |
| 96 | + |
| 97 | +/// Runs the Andersen OTF fixpoint and returns the raw alias-analysis result |
| 98 | +/// (no LLVM-value wrapping). If \p VC is null, a fresh one is allocated. |
| 99 | +[[nodiscard]] AndersenOTFResult |
| 100 | +computeAndersenOTFRaw(const LLVMProjectIRDB &IRDB, |
| 101 | + llvm::ArrayRef<const llvm::Function *> EntryPoints, |
| 102 | + MaybeUniquePtr<ValueCompressor<PAGVariable>> VC = nullptr, |
| 103 | + Soundness S = Soundness::Soundy); |
| 104 | + |
| 105 | +/// Runs the Andersen OTF fixpoint and returns an \c LLVMUnionFindAliasIterator |
| 106 | +/// that implements \c IsLLVMAliasIterator. |
| 107 | +[[nodiscard]] LLVMUnionFindAliasIterator<AndersenOTFResult> |
| 108 | +computeAndersenOTF(const LLVMProjectIRDB &IRDB, |
| 109 | + llvm::ArrayRef<const llvm::Function *> EntryPoints, |
| 110 | + MaybeUniquePtr<ValueCompressor<PAGVariable>> VC = nullptr, |
| 111 | + Soundness S = Soundness::Soundy); |
| 112 | + |
| 113 | +} // namespace psr |
0 commit comments