|
| 1 | +/****************************************************************************** |
| 2 | + * Copyright (c) 2022 Philipp Schubert. |
| 3 | + * All rights reserved. This program and the accompanying materials are made |
| 4 | + * available under the terms of LICENSE.txt. |
| 5 | + * |
| 6 | + * Contributors: |
| 7 | + * Philipp Schubert and others |
| 8 | + *****************************************************************************/ |
| 9 | + |
| 10 | +#ifndef PHASAR_PHASARLLVM_DATAFLOWSOLVER_IFDSIDE_DEFAULTEDGEFUNCTIONSINGLETONCACHE_H |
| 11 | +#define PHASAR_PHASARLLVM_DATAFLOWSOLVER_IFDSIDE_DEFAULTEDGEFUNCTIONSINGLETONCACHE_H |
| 12 | + |
| 13 | +#include "phasar/DataFlow/IfdsIde/EdgeFunction.h" |
| 14 | +#include "phasar/DataFlow/IfdsIde/EdgeFunctionSingletonCache.h" |
| 15 | + |
| 16 | +namespace psr { |
| 17 | + |
| 18 | +/// Default implementation of EdgeFunctionSingletonCache. |
| 19 | +/// |
| 20 | +/// For an edge function EdgeFunctionTy to be cached, it must be hashable, i.e. |
| 21 | +/// it must implement the friend- or nonmember function llvm::hash_code |
| 22 | +/// hash_value(const EdgeFunctionTy&). |
| 23 | +/// |
| 24 | +/// This cache is *not* thread-safe. |
| 25 | +template <typename EdgeFunctionTy, typename = void> |
| 26 | +class DefaultEdgeFunctionSingletonCache |
| 27 | + : public EdgeFunctionSingletonCache<EdgeFunctionTy> { |
| 28 | +public: |
| 29 | + DefaultEdgeFunctionSingletonCache() noexcept = default; |
| 30 | + |
| 31 | + DefaultEdgeFunctionSingletonCache(const DefaultEdgeFunctionSingletonCache &) = |
| 32 | + delete; |
| 33 | + DefaultEdgeFunctionSingletonCache & |
| 34 | + operator=(const DefaultEdgeFunctionSingletonCache &) = delete; |
| 35 | + |
| 36 | + DefaultEdgeFunctionSingletonCache( |
| 37 | + DefaultEdgeFunctionSingletonCache &&) noexcept = default; |
| 38 | + DefaultEdgeFunctionSingletonCache & |
| 39 | + operator=(DefaultEdgeFunctionSingletonCache &&) noexcept = delete; |
| 40 | + ~DefaultEdgeFunctionSingletonCache() override = default; |
| 41 | + |
| 42 | + [[nodiscard]] const void * |
| 43 | + lookup(ByConstRef<EdgeFunctionTy> EF) const noexcept override { |
| 44 | + return Cache.lookup(&EF); |
| 45 | + } |
| 46 | + |
| 47 | + void insert(const EdgeFunctionTy *EF, const void *Mem) override { |
| 48 | + assert(EF != nullptr); |
| 49 | + auto [It, Inserted] = Cache.try_emplace(EF, Mem); |
| 50 | + assert(Inserted); |
| 51 | + } |
| 52 | + |
| 53 | + void erase(ByConstRef<EdgeFunctionTy> EF) noexcept override { |
| 54 | + Cache.erase(&EF); |
| 55 | + } |
| 56 | + |
| 57 | + template <typename... ArgTys> |
| 58 | + [[nodiscard]] EdgeFunction<typename EdgeFunctionTy::l_t> |
| 59 | + createEdgeFunction(ArgTys &&...Args) { |
| 60 | + return CachedEdgeFunction<EdgeFunctionTy>{ |
| 61 | + EdgeFunctionTy{std::forward<ArgTys>(Args)...}, this}; |
| 62 | + } |
| 63 | + |
| 64 | +private: |
| 65 | + struct DSI : public llvm::DenseMapInfo<const EdgeFunctionTy *> { |
| 66 | + static bool isEqual(const EdgeFunctionTy *LHS, |
| 67 | + const EdgeFunctionTy *RHS) noexcept { |
| 68 | + if (LHS == RHS) { |
| 69 | + return true; |
| 70 | + } |
| 71 | + auto Empty = llvm::DenseMapInfo<const EdgeFunctionTy *>::getEmptyKey(); |
| 72 | + auto Tombstone = |
| 73 | + llvm::DenseMapInfo<const EdgeFunctionTy *>::getTombstoneKey(); |
| 74 | + if (LHS == Empty || LHS == Tombstone || RHS == Empty || |
| 75 | + RHS == Tombstone) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + return *LHS == *RHS; |
| 80 | + } |
| 81 | + |
| 82 | + static auto getHashValue(const EdgeFunctionTy *EF) noexcept { |
| 83 | + assert(EF != llvm::DenseMapInfo<const EdgeFunctionTy *>::getEmptyKey()); |
| 84 | + assert(EF != |
| 85 | + llvm::DenseMapInfo<const EdgeFunctionTy *>::getTombstoneKey()); |
| 86 | + |
| 87 | + return hash_value(*EF); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + llvm::DenseMap<const EdgeFunctionTy *, const void *, DSI> Cache; |
| 92 | +}; |
| 93 | + |
| 94 | +template <typename EdgeFunctionTy> |
| 95 | +class DefaultEdgeFunctionSingletonCache< |
| 96 | + EdgeFunctionTy, |
| 97 | + std::enable_if_t<EdgeFunctionBase::IsSOOCandidate<EdgeFunctionTy>>> { |
| 98 | +public: |
| 99 | + [[nodiscard]] const void * |
| 100 | + lookup(ByConstRef<EdgeFunctionTy> /*EF*/) const noexcept override { |
| 101 | + return nullptr; |
| 102 | + } |
| 103 | + void insert(const EdgeFunctionTy * /*EF*/, const void * /*Mem*/) override { |
| 104 | + assert(false && "We should never go here"); |
| 105 | + } |
| 106 | + void erase(ByConstRef<EdgeFunctionTy> /*EF*/) noexcept override { |
| 107 | + assert(false && "We should never go here"); |
| 108 | + } |
| 109 | + [[nodiscard]] EdgeFunction<typename EdgeFunctionTy::l_t> |
| 110 | + createEdgeFunction(EdgeFunctionTy EF) { |
| 111 | + return EF; |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | +} // namespace psr |
| 116 | + |
| 117 | +#endif // PHASAR_PHASARLLVM_DATAFLOWSOLVER_IFDSIDE_DEFAULTEDGEFUNCTIONSINGLETONCACHE_H |
0 commit comments