|
| 1 | +#ifndef PHASAR_DATAFLOW_IFDSIDE_SOLVER_COMPRESSOR_H |
| 2 | +#define PHASAR_DATAFLOW_IFDSIDE_SOLVER_COMPRESSOR_H |
| 3 | + |
| 4 | +#include "phasar/DB/ProjectIRDBBase.h" |
| 5 | +#include "phasar/Utils/ByRef.h" |
| 6 | +#include "phasar/Utils/TypeTraits.h" |
| 7 | + |
| 8 | +#include "llvm/ADT/DenseMap.h" |
| 9 | +#include "llvm/ADT/DenseMapInfo.h" |
| 10 | +#include "llvm/ADT/SmallVector.h" |
| 11 | + |
| 12 | +#include <cstdint> |
| 13 | +#include <deque> |
| 14 | +#include <functional> |
| 15 | +#include <optional> |
| 16 | +#include <type_traits> |
| 17 | + |
| 18 | +namespace psr { |
| 19 | +template <typename T, typename Enable = void> class Compressor; |
| 20 | + |
| 21 | +template <typename T> |
| 22 | +class Compressor<T, std::enable_if_t<CanEfficientlyPassByValue<T>>> { |
| 23 | +public: |
| 24 | + void reserve(size_t Capacity) { |
| 25 | + assert(Capacity <= UINT32_MAX); |
| 26 | + ToInt.reserve(Capacity); |
| 27 | + FromInt.reserve(Capacity); |
| 28 | + } |
| 29 | + |
| 30 | + uint32_t getOrInsert(T Elem) { |
| 31 | + auto [It, Inserted] = ToInt.try_emplace(Elem, ToInt.size()); |
| 32 | + if (Inserted) { |
| 33 | + FromInt.push_back(Elem); |
| 34 | + } |
| 35 | + return It->second; |
| 36 | + } |
| 37 | + |
| 38 | + std::optional<uint32_t> getOrNull(T Elem) const { |
| 39 | + if (auto It = ToInt.find(Elem); It != ToInt.end()) { |
| 40 | + return It->second; |
| 41 | + } |
| 42 | + return std::nullopt; |
| 43 | + } |
| 44 | + |
| 45 | + T operator[](size_t Idx) const noexcept { |
| 46 | + assert(Idx < FromInt.size()); |
| 47 | + return FromInt[Idx]; |
| 48 | + } |
| 49 | + |
| 50 | + [[nodiscard]] size_t size() const noexcept { return FromInt.size(); } |
| 51 | + [[nodiscard]] size_t capacity() const noexcept { |
| 52 | + return FromInt.capacity() + |
| 53 | + ToInt.getMemorySize() / sizeof(typename decltype(ToInt)::value_type); |
| 54 | + } |
| 55 | + |
| 56 | + auto begin() const noexcept { return FromInt.begin(); } |
| 57 | + auto end() const noexcept { return FromInt.end(); } |
| 58 | + |
| 59 | +private: |
| 60 | + llvm::DenseMap<T, uint32_t> ToInt; |
| 61 | + llvm::SmallVector<T, 0> FromInt; |
| 62 | +}; |
| 63 | + |
| 64 | +template <typename T> |
| 65 | +class Compressor<T, std::enable_if_t<!CanEfficientlyPassByValue<T>>> { |
| 66 | +public: |
| 67 | + void reserve(size_t Capacity) { |
| 68 | + assert(Capacity <= UINT32_MAX); |
| 69 | + ToInt.reserve(Capacity); |
| 70 | + } |
| 71 | + |
| 72 | + uint32_t getOrInsert(const T &Elem) { |
| 73 | + if (auto It = ToInt.find(&Elem); It != ToInt.end()) { |
| 74 | + return It->second; |
| 75 | + } |
| 76 | + auto Ret = FromInt.size(); |
| 77 | + auto *Ins = &FromInt.emplace_back(Elem); |
| 78 | + ToInt[Ins] = Ret; |
| 79 | + return Ret; |
| 80 | + } |
| 81 | + |
| 82 | + uint32_t getOrInsert(T &&Elem) { |
| 83 | + if (auto It = ToInt.find(&Elem); It != ToInt.end()) { |
| 84 | + return It->second; |
| 85 | + } |
| 86 | + auto Ret = FromInt.size(); |
| 87 | + auto *Ins = &FromInt.emplace_back(std::move(Elem)); |
| 88 | + ToInt[Ins] = Ret; |
| 89 | + return Ret; |
| 90 | + } |
| 91 | + |
| 92 | + std::optional<uint32_t> getOrNull(const T &Elem) const { |
| 93 | + if (auto It = ToInt.find(&Elem); It != ToInt.end()) { |
| 94 | + return It->second; |
| 95 | + } |
| 96 | + return std::nullopt; |
| 97 | + } |
| 98 | + |
| 99 | + const T &operator[](size_t Idx) const noexcept { |
| 100 | + assert(Idx < FromInt.size()); |
| 101 | + return FromInt[Idx]; |
| 102 | + } |
| 103 | + |
| 104 | + [[nodiscard]] size_t size() const noexcept { return FromInt.size(); } |
| 105 | + [[nodiscard]] size_t capacity() const noexcept { |
| 106 | + return FromInt.size() + |
| 107 | + ToInt.getMemorySize() / sizeof(typename decltype(ToInt)::value_type); |
| 108 | + } |
| 109 | + |
| 110 | + auto begin() const noexcept { return FromInt.begin(); } |
| 111 | + auto end() const noexcept { return FromInt.end(); } |
| 112 | + |
| 113 | +private: |
| 114 | + struct DSI : llvm::DenseMapInfo<const T *> { |
| 115 | + static auto getHashValue(const T *Elem) noexcept { |
| 116 | + assert(Elem != nullptr); |
| 117 | + if constexpr (has_llvm_dense_map_info<T>) { |
| 118 | + return llvm::DenseMapInfo<T>::getHashValue(*Elem); |
| 119 | + } else { |
| 120 | + return std::hash<T>{}(*Elem); |
| 121 | + } |
| 122 | + } |
| 123 | + static auto isEqual(const T *LHS, const T *RHS) noexcept { |
| 124 | + if (LHS == RHS) { |
| 125 | + return true; |
| 126 | + } |
| 127 | + if (LHS == DSI::getEmptyKey() || LHS == DSI::getTombstoneKey() || |
| 128 | + RHS == DSI::getEmptyKey() || RHS == DSI::getTombstoneKey()) { |
| 129 | + return false; |
| 130 | + } |
| 131 | + if constexpr (has_llvm_dense_map_info<T>) { |
| 132 | + return llvm::DenseMapInfo<T>::isEqual(*LHS, *RHS); |
| 133 | + } else { |
| 134 | + return *LHS == *RHS; |
| 135 | + } |
| 136 | + } |
| 137 | + }; |
| 138 | + |
| 139 | + std::deque<T> FromInt; |
| 140 | + llvm::DenseMap<const T *, uint32_t, DSI> ToInt; |
| 141 | +}; |
| 142 | + |
| 143 | +struct NoneCompressor final { |
| 144 | + constexpr NoneCompressor() noexcept = default; |
| 145 | + |
| 146 | + template <typename T, |
| 147 | + typename = std::enable_if_t<!std::is_same_v<NoneCompressor, T>>> |
| 148 | + constexpr NoneCompressor(const T & /*unused*/) noexcept {} |
| 149 | + |
| 150 | + template <typename T> |
| 151 | + [[nodiscard]] decltype(auto) getOrInsert(T &&Val) const noexcept { |
| 152 | + return std::forward<T>(Val); |
| 153 | + } |
| 154 | + template <typename T> |
| 155 | + [[nodiscard]] decltype(auto) operator[](T &&Val) const noexcept { |
| 156 | + return std::forward<T>(Val); |
| 157 | + } |
| 158 | + void reserve(size_t /*unused*/) const noexcept {} |
| 159 | + |
| 160 | + [[nodiscard]] size_t size() const noexcept { return 0; } |
| 161 | + [[nodiscard]] size_t capacity() const noexcept { return 0; } |
| 162 | +}; |
| 163 | + |
| 164 | +class LLVMProjectIRDB; |
| 165 | + |
| 166 | +/// Once we have fast instruction IDs (as we already have in IntelliSecPhasar), |
| 167 | +/// we might want to create a specialization for T/const llvm::Value * that uses |
| 168 | +/// the IDs from the IRDB |
| 169 | +template <typename T> struct NodeCompressorTraits { |
| 170 | + using type = Compressor<T>; |
| 171 | + |
| 172 | + static type create(const ProjectIRDBBase<LLVMProjectIRDB> |
| 173 | + * /*IRDB*/) noexcept(noexcept(type())) { |
| 174 | + return type(); |
| 175 | + } |
| 176 | +}; |
| 177 | + |
| 178 | +template <typename T, typename = void> struct ValCompressorTraits { |
| 179 | + using type = Compressor<T>; |
| 180 | + using id_type = uint32_t; |
| 181 | +}; |
| 182 | + |
| 183 | +template <typename T> |
| 184 | +struct ValCompressorTraits<T, std::enable_if_t<CanEfficientlyPassByValue<T>>> { |
| 185 | + using type = NoneCompressor; |
| 186 | + using id_type = T; |
| 187 | +}; |
| 188 | + |
| 189 | +} // namespace psr |
| 190 | + |
| 191 | +#endif // PHASAR_DATAFLOW_IFDSIDE_SOLVER_COMPRESSOR_H |
0 commit comments