|
| 1 | +/****************************************************************************** |
| 2 | + * Copyright (c) 2020 Fabian Schiebel. |
| 3 | + * All rights reserved. This program and the accompanying materials are made |
| 4 | + * available under the terms of LICENSE.txt. |
| 5 | + * |
| 6 | + * Contributors: |
| 7 | + * Fabian Schiebel and others |
| 8 | + *****************************************************************************/ |
| 9 | + |
| 10 | +#ifndef PHASAR_PHASARLLVM_POINTER_FILTEREDLLVMALIASSET_H |
| 11 | +#define PHASAR_PHASARLLVM_POINTER_FILTEREDLLVMALIASSET_H |
| 12 | + |
| 13 | +#include "phasar/Pointer/AliasAnalysisType.h" |
| 14 | +#include "phasar/Pointer/AliasInfoTraits.h" |
| 15 | +#include "phasar/Pointer/AliasResult.h" |
| 16 | +#include "phasar/Pointer/AliasSetOwner.h" |
| 17 | +#include "phasar/Utils/AnalysisProperties.h" |
| 18 | +#include "phasar/Utils/MaybeUniquePtr.h" |
| 19 | + |
| 20 | +#include "llvm/ADT/DenseMap.h" |
| 21 | +#include "llvm/ADT/DenseMapInfo.h" |
| 22 | +#include "llvm/ADT/Hashing.h" |
| 23 | +#include "llvm/ADT/PointerIntPair.h" |
| 24 | +#include "llvm/IR/Function.h" |
| 25 | +#include "llvm/Support/ErrorHandling.h" |
| 26 | + |
| 27 | +#include "nlohmann/json_fwd.hpp" |
| 28 | + |
| 29 | +#include <type_traits> |
| 30 | +#include <utility> |
| 31 | + |
| 32 | +namespace llvm { |
| 33 | +class Value; |
| 34 | +class Instruction; |
| 35 | +} // namespace llvm |
| 36 | + |
| 37 | +namespace psr { |
| 38 | + |
| 39 | +class LLVMAliasSet; |
| 40 | +class FilteredLLVMAliasSet; |
| 41 | + |
| 42 | +template <> |
| 43 | +struct AliasInfoTraits<FilteredLLVMAliasSet> |
| 44 | + : DefaultAATraits<const llvm::Value *, const llvm::Instruction *> {}; |
| 45 | + |
| 46 | +class FilteredLLVMAliasSet { |
| 47 | +public: |
| 48 | + using alias_traits_t = AliasInfoTraits<FilteredLLVMAliasSet>; |
| 49 | + using n_t = alias_traits_t::n_t; |
| 50 | + using v_t = alias_traits_t::v_t; |
| 51 | + using AliasSetTy = alias_traits_t::AliasSetTy; |
| 52 | + using AliasSetPtrTy = alias_traits_t::AliasSetPtrTy; |
| 53 | + using AllocationSiteSetPtrTy = alias_traits_t::AllocationSiteSetPtrTy; |
| 54 | + |
| 55 | + FilteredLLVMAliasSet(LLVMAliasSet *AS) noexcept; |
| 56 | + |
| 57 | + FilteredLLVMAliasSet(const FilteredLLVMAliasSet &) = delete; |
| 58 | + FilteredLLVMAliasSet &operator=(const FilteredLLVMAliasSet &) = delete; |
| 59 | + FilteredLLVMAliasSet &operator=(FilteredLLVMAliasSet &&) noexcept = delete; |
| 60 | + |
| 61 | + FilteredLLVMAliasSet(FilteredLLVMAliasSet &&) noexcept = default; |
| 62 | + |
| 63 | + ~FilteredLLVMAliasSet(); |
| 64 | + |
| 65 | + template <typename... ArgsT, |
| 66 | + typename = std::enable_if_t< |
| 67 | + std::is_constructible_v<LLVMAliasSet, ArgsT...>>> |
| 68 | + explicit FilteredLLVMAliasSet(ArgsT &&...Args) |
| 69 | + : FilteredLLVMAliasSet(std::forward<ArgsT>(Args)...) {} |
| 70 | + |
| 71 | + // --- API Functions: |
| 72 | + |
| 73 | + [[nodiscard]] inline bool isInterProcedural() const noexcept { |
| 74 | + return false; |
| 75 | + }; |
| 76 | + |
| 77 | + [[nodiscard]] AliasAnalysisType getAliasAnalysisType() const noexcept; |
| 78 | + |
| 79 | + [[nodiscard]] AliasResult alias(const llvm::Value *V1, const llvm::Value *V2, |
| 80 | + const llvm::Instruction *I); |
| 81 | + [[nodiscard]] AliasResult alias(const llvm::Value *V1, const llvm::Value *V2, |
| 82 | + const llvm::Function *Fun); |
| 83 | + |
| 84 | + [[nodiscard]] AliasSetPtrTy getAliasSet(const llvm::Value *V, |
| 85 | + const llvm::Instruction *I); |
| 86 | + [[nodiscard]] AliasSetPtrTy getAliasSet(const llvm::Value *V, |
| 87 | + const llvm::Function *Fun); |
| 88 | + |
| 89 | + [[nodiscard]] AllocationSiteSetPtrTy |
| 90 | + getReachableAllocationSites(const llvm::Value *V, bool IntraProcOnly = false, |
| 91 | + const llvm::Instruction *I = nullptr); |
| 92 | + |
| 93 | + // Checks if PotentialValue is in the reachable allocation sites of V. |
| 94 | + [[nodiscard]] bool isInReachableAllocationSites( |
| 95 | + const llvm::Value *V, const llvm::Value *PotentialValue, |
| 96 | + bool IntraProcOnly = false, const llvm::Instruction *I = nullptr); |
| 97 | + |
| 98 | + void mergeWith(const FilteredLLVMAliasSet & /*OtherPTI*/) { |
| 99 | + llvm::report_fatal_error("Not Supported"); |
| 100 | + } |
| 101 | + |
| 102 | + void introduceAlias(const llvm::Value * /*V1*/, const llvm::Value * /*V2*/, |
| 103 | + const llvm::Instruction * /*I*/ = nullptr, |
| 104 | + AliasResult /*Kind*/ = AliasResult::MustAlias) { |
| 105 | + llvm::report_fatal_error("Not Supported"); |
| 106 | + } |
| 107 | + |
| 108 | + void print(llvm::raw_ostream &OS = llvm::outs()) const; |
| 109 | + |
| 110 | + [[nodiscard]] nlohmann::json getAsJson() const; |
| 111 | + |
| 112 | + void printAsJson(llvm::raw_ostream &OS = llvm::outs()) const; |
| 113 | + |
| 114 | + [[nodiscard]] AnalysisProperties getAnalysisProperties() const noexcept { |
| 115 | + return AnalysisProperties::None; |
| 116 | + } |
| 117 | + |
| 118 | +private: |
| 119 | + struct ReachableAllocationSitesKey { |
| 120 | + llvm::PointerIntPair<const llvm::Function *, 1, bool> FunAndIntraProcOnly; |
| 121 | + v_t Value{}; |
| 122 | + }; |
| 123 | + |
| 124 | + struct ReachableAllocationSitesKeyDMI { |
| 125 | + inline static ReachableAllocationSitesKey getEmptyKey() noexcept { |
| 126 | + return {{}, llvm::DenseMapInfo<v_t>::getEmptyKey()}; |
| 127 | + } |
| 128 | + inline static ReachableAllocationSitesKey getTombstoneKey() noexcept { |
| 129 | + return {{}, llvm::DenseMapInfo<v_t>::getTombstoneKey()}; |
| 130 | + } |
| 131 | + inline static auto getHashValue(ReachableAllocationSitesKey Key) noexcept { |
| 132 | + return llvm::hash_combine(Key.FunAndIntraProcOnly.getOpaqueValue(), |
| 133 | + Key.Value); |
| 134 | + } |
| 135 | + inline static bool isEqual(ReachableAllocationSitesKey Key1, |
| 136 | + ReachableAllocationSitesKey Key2) noexcept { |
| 137 | + return Key1.FunAndIntraProcOnly == Key2.FunAndIntraProcOnly && |
| 138 | + Key1.Value == Key2.Value; |
| 139 | + } |
| 140 | + }; |
| 141 | + |
| 142 | + FilteredLLVMAliasSet(MaybeUniquePtr<LLVMAliasSet, true> AS) noexcept; |
| 143 | + |
| 144 | + MaybeUniquePtr<LLVMAliasSet, /*RequireAlignment=*/true> AS; |
| 145 | + AliasSetOwner<AliasSetTy> Owner; |
| 146 | + llvm::DenseMap<std::pair<const llvm::Function *, v_t>, AliasSetPtrTy> |
| 147 | + AliasSetMap; |
| 148 | + llvm::DenseMap<ReachableAllocationSitesKey, std::unique_ptr<AliasSetTy>, |
| 149 | + ReachableAllocationSitesKeyDMI> |
| 150 | + ReachableAllocationSitesMap; |
| 151 | +}; |
| 152 | +} // namespace psr |
| 153 | + |
| 154 | +#endif // PHASAR_PHASARLLVM_POINTER_FILTEREDLLVMALIASSET_H |
0 commit comments