|
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, Alexander Meinhold and others |
8 | | - *****************************************************************************/ |
9 | | - |
10 | | -#ifndef PHASAR_PHASARLLVM_IFDSIDE_PROBLEMS_IDEGENERALIZEDLCA_EDGEVALUE_H_ |
11 | | -#define PHASAR_PHASARLLVM_IFDSIDE_PROBLEMS_IDEGENERALIZEDLCA_EDGEVALUE_H_ |
12 | | - |
13 | | -#include <iostream> |
14 | | -#include <memory> |
15 | | -#include <unordered_set> |
16 | | -#include <variant> |
17 | | - |
18 | | -#include "llvm/ADT/APSInt.h" |
19 | | -#include "llvm/ADT/Twine.h" |
20 | | -#include "llvm/IR/Constant.h" |
21 | | -#include "llvm/IR/Instructions.h" |
22 | | - |
23 | | -namespace psr { |
24 | | - |
25 | | -enum class Ordering { Less, Greater, Equal, Incomparable }; |
26 | | - |
27 | | -class EdgeValue { |
28 | | -public: |
29 | | - enum Type { Top, Integer, String, FloatingPoint }; |
30 | | - |
31 | | -private: |
32 | | - std::variant<llvm::APInt, llvm::APFloat, std::string, std::nullptr_t> |
33 | | - ValVariant = nullptr; |
34 | | - Type VariantType; |
35 | | - |
36 | | -public: |
37 | | - EdgeValue(const llvm::Value *Val); |
38 | | - EdgeValue(const EdgeValue &EV); |
39 | | - EdgeValue(llvm::APInt &&VI); |
40 | | - EdgeValue(const llvm::APInt &VI); |
41 | | - EdgeValue(llvm::APFloat &&VF); |
42 | | - EdgeValue(long long VI); |
43 | | - EdgeValue(int VI); |
44 | | - EdgeValue(double Double); |
45 | | - EdgeValue(float Float); |
46 | | - EdgeValue(std::string &&VS); |
47 | | - EdgeValue(std::nullptr_t); |
48 | | - ~EdgeValue(); |
49 | | - const static EdgeValue TopValue; |
50 | | - [[nodiscard]] bool tryGetInt(uint64_t &Res) const; |
51 | | - [[nodiscard]] bool tryGetFP(double &Res) const; |
52 | | - [[nodiscard]] bool tryGetString(std::string &Res) const; |
53 | | - [[nodiscard]] bool isTop() const; |
54 | | - [[nodiscard]] bool isNumeric() const; |
55 | | - [[nodiscard]] bool isString() const; |
56 | | - [[nodiscard]] Type getKind() const; |
57 | | - // std::unique_ptr<ObjectLLVM> asObjLLVM(llvm::LLVMContext &ctx) const; |
58 | | - [[nodiscard]] bool sqSubsetEq(const EdgeValue &Other) const; |
59 | | - [[nodiscard]] EdgeValue performBinOp(llvm::BinaryOperator::BinaryOps Op, |
60 | | - const EdgeValue &Other) const; |
61 | | - [[nodiscard]] EdgeValue typecast(Type Dest, unsigned Bits) const; |
62 | | - EdgeValue &operator=(const EdgeValue &EV); |
63 | | - |
64 | | - operator bool(); |
65 | | - friend bool operator==(const EdgeValue &Lhs, const EdgeValue &Rhs); |
66 | | - |
67 | | - // binary operators |
68 | | - friend EdgeValue operator+(const EdgeValue &Lhs, const EdgeValue &Rhs); |
69 | | - friend EdgeValue operator-(const EdgeValue &Lhs, const EdgeValue &Rhs); |
70 | | - friend EdgeValue operator*(const EdgeValue &Lhs, const EdgeValue &Rhs); |
71 | | - friend EdgeValue operator/(const EdgeValue &Lhs, const EdgeValue &Rhs); |
72 | | - friend EdgeValue operator%(const EdgeValue &Lhs, const EdgeValue &Rhs); |
73 | | - friend EdgeValue operator&(const EdgeValue &Lhs, const EdgeValue &Rhs); |
74 | | - friend EdgeValue operator|(const EdgeValue &Lhs, const EdgeValue &Rhs); |
75 | | - friend EdgeValue operator^(const EdgeValue &Lhs, const EdgeValue &Rhs); |
76 | | - friend EdgeValue operator<<(const EdgeValue &Lhs, const EdgeValue &Rhs); |
77 | | - friend EdgeValue operator>>(const EdgeValue &Lhs, const EdgeValue &Rhs); |
78 | | - static int compare(const EdgeValue &Lhs, const EdgeValue &Rhs); |
79 | | - |
80 | | - // unary operators |
81 | | - EdgeValue operator-() const; |
82 | | - EdgeValue operator~() const; |
83 | | - friend std::ostream &operator<<(std::ostream &Os, const EdgeValue &EV); |
84 | | - static std::string typeToString(Type Ty); |
85 | | -}; |
86 | | -class EdgeValueSet; |
87 | | -typedef EdgeValueSet ev_t; |
88 | | - |
89 | | -ev_t performBinOp(llvm::BinaryOperator::BinaryOps Op, const ev_t &Lhs, |
90 | | - const ev_t &Rhs, size_t MaxSize); |
91 | | -ev_t performTypecast(const ev_t &Ev, EdgeValue::Type Dest, unsigned Bits); |
92 | | -Ordering compare(const ev_t &Lhs, const ev_t &Rhs); |
93 | | -ev_t join(const ev_t &Lhs, const ev_t &Rhs, size_t MaxSize); |
94 | | -/// \brief implements square subset equal |
95 | | -bool operator<(const ev_t &Lhs, const ev_t &Rhs); |
96 | | -bool isTopValue(const ev_t &Val); |
97 | | -std::ostream &operator<<(std::ostream &Os, const ev_t &Val); |
98 | | - |
99 | | -} // namespace psr |
100 | | - |
101 | | -namespace std { |
102 | | - |
103 | | -template <> struct hash<psr::EdgeValue> { |
104 | | - hash() = default; |
105 | | - size_t operator()(const psr::EdgeValue &Val) const { |
106 | | - auto Hash = hash<int>()(Val.getKind()); |
107 | | - uint64_t AsInt; |
108 | | - double AsFloat; |
109 | | - string AsString; |
110 | | - if (Val.tryGetInt(AsInt)) { |
111 | | - return hash<uint64_t>()(AsInt) * 31 + Hash; |
112 | | - } |
113 | | - if (Val.tryGetFP(AsFloat)) { |
114 | | - return hash<double>()(round(AsFloat)) * 31 + Hash; |
115 | | - } |
116 | | - if (Val.tryGetString(AsString)) { |
117 | | - return hash<string>()(AsString) * 31 + Hash; |
118 | | - } |
119 | | - return Hash; |
120 | | - } |
121 | | -}; |
122 | | - |
123 | | -} // namespace std |
124 | | - |
125 | | -#endif |
| 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, Alexander Meinhold and others |
| 8 | + *****************************************************************************/ |
| 9 | + |
| 10 | +#ifndef PHASAR_PHASARLLVM_DATAFLOWSOLVER_IFDSIDE_PROBLEMS_IDEGENERALIZEDLCA_EDGEVALUE_H |
| 11 | +#define PHASAR_PHASARLLVM_DATAFLOWSOLVER_IFDSIDE_PROBLEMS_IDEGENERALIZEDLCA_EDGEVALUE_H |
| 12 | + |
| 13 | +#include <iostream> |
| 14 | +#include <memory> |
| 15 | +#include <unordered_set> |
| 16 | +#include <variant> |
| 17 | + |
| 18 | +#include "llvm/ADT/APSInt.h" |
| 19 | +#include "llvm/ADT/Twine.h" |
| 20 | +#include "llvm/IR/Constant.h" |
| 21 | +#include "llvm/IR/Instructions.h" |
| 22 | + |
| 23 | +namespace psr { |
| 24 | + |
| 25 | +enum class Ordering { Less, Greater, Equal, Incomparable }; |
| 26 | + |
| 27 | +class EdgeValue { |
| 28 | +public: |
| 29 | + enum Type { Top, Integer, String, FloatingPoint }; |
| 30 | + |
| 31 | +private: |
| 32 | + std::variant<llvm::APInt, llvm::APFloat, std::string, std::nullptr_t> |
| 33 | + ValVariant = nullptr; |
| 34 | + Type VariantType; |
| 35 | + |
| 36 | +public: |
| 37 | + EdgeValue(const llvm::Value *Val); |
| 38 | + EdgeValue(const EdgeValue &EV); |
| 39 | + EdgeValue(llvm::APInt &&VI); |
| 40 | + EdgeValue(const llvm::APInt &VI); |
| 41 | + EdgeValue(llvm::APFloat &&VF); |
| 42 | + EdgeValue(long long VI); |
| 43 | + EdgeValue(int VI); |
| 44 | + EdgeValue(double Double); |
| 45 | + EdgeValue(float Float); |
| 46 | + EdgeValue(std::string &&VS); |
| 47 | + EdgeValue(std::nullptr_t); |
| 48 | + ~EdgeValue(); |
| 49 | + const static EdgeValue TopValue; |
| 50 | + [[nodiscard]] bool tryGetInt(uint64_t &Res) const; |
| 51 | + [[nodiscard]] bool tryGetFP(double &Res) const; |
| 52 | + [[nodiscard]] bool tryGetString(std::string &Res) const; |
| 53 | + [[nodiscard]] bool isTop() const; |
| 54 | + [[nodiscard]] bool isNumeric() const; |
| 55 | + [[nodiscard]] bool isString() const; |
| 56 | + [[nodiscard]] Type getKind() const; |
| 57 | + // std::unique_ptr<ObjectLLVM> asObjLLVM(llvm::LLVMContext &ctx) const; |
| 58 | + [[nodiscard]] bool sqSubsetEq(const EdgeValue &Other) const; |
| 59 | + [[nodiscard]] EdgeValue performBinOp(llvm::BinaryOperator::BinaryOps Op, |
| 60 | + const EdgeValue &Other) const; |
| 61 | + [[nodiscard]] EdgeValue typecast(Type Dest, unsigned Bits) const; |
| 62 | + EdgeValue &operator=(const EdgeValue &EV); |
| 63 | + |
| 64 | + operator bool(); |
| 65 | + friend bool operator==(const EdgeValue &Lhs, const EdgeValue &Rhs); |
| 66 | + |
| 67 | + // binary operators |
| 68 | + friend EdgeValue operator+(const EdgeValue &Lhs, const EdgeValue &Rhs); |
| 69 | + friend EdgeValue operator-(const EdgeValue &Lhs, const EdgeValue &Rhs); |
| 70 | + friend EdgeValue operator*(const EdgeValue &Lhs, const EdgeValue &Rhs); |
| 71 | + friend EdgeValue operator/(const EdgeValue &Lhs, const EdgeValue &Rhs); |
| 72 | + friend EdgeValue operator%(const EdgeValue &Lhs, const EdgeValue &Rhs); |
| 73 | + friend EdgeValue operator&(const EdgeValue &Lhs, const EdgeValue &Rhs); |
| 74 | + friend EdgeValue operator|(const EdgeValue &Lhs, const EdgeValue &Rhs); |
| 75 | + friend EdgeValue operator^(const EdgeValue &Lhs, const EdgeValue &Rhs); |
| 76 | + friend EdgeValue operator<<(const EdgeValue &Lhs, const EdgeValue &Rhs); |
| 77 | + friend EdgeValue operator>>(const EdgeValue &Lhs, const EdgeValue &Rhs); |
| 78 | + static int compare(const EdgeValue &Lhs, const EdgeValue &Rhs); |
| 79 | + |
| 80 | + // unary operators |
| 81 | + EdgeValue operator-() const; |
| 82 | + EdgeValue operator~() const; |
| 83 | + friend std::ostream &operator<<(std::ostream &Os, const EdgeValue &EV); |
| 84 | + static std::string typeToString(Type Ty); |
| 85 | +}; |
| 86 | +class EdgeValueSet; |
| 87 | +using ev_t = EdgeValueSet; |
| 88 | + |
| 89 | +ev_t performBinOp(llvm::BinaryOperator::BinaryOps Op, const ev_t &Lhs, |
| 90 | + const ev_t &Rhs, size_t MaxSize); |
| 91 | +ev_t performTypecast(const ev_t &Ev, EdgeValue::Type Dest, unsigned Bits); |
| 92 | +Ordering compare(const ev_t &Lhs, const ev_t &Rhs); |
| 93 | +ev_t join(const ev_t &Lhs, const ev_t &Rhs, size_t MaxSize); |
| 94 | +/// \brief implements square subset equal |
| 95 | +bool operator<(const ev_t &Lhs, const ev_t &Rhs); |
| 96 | +bool isTopValue(const ev_t &Val); |
| 97 | +std::ostream &operator<<(std::ostream &Os, const ev_t &Val); |
| 98 | + |
| 99 | +} // namespace psr |
| 100 | + |
| 101 | +namespace std { |
| 102 | + |
| 103 | +template <> struct hash<psr::EdgeValue> { |
| 104 | + hash() = default; |
| 105 | + size_t operator()(const psr::EdgeValue &Val) const { |
| 106 | + auto Hash = hash<int>()(Val.getKind()); |
| 107 | + uint64_t AsInt; |
| 108 | + double AsFloat; |
| 109 | + string AsString; |
| 110 | + if (Val.tryGetInt(AsInt)) { |
| 111 | + return hash<uint64_t>()(AsInt) * 31 + Hash; |
| 112 | + } |
| 113 | + if (Val.tryGetFP(AsFloat)) { |
| 114 | + return hash<double>()(round(AsFloat)) * 31 + Hash; |
| 115 | + } |
| 116 | + if (Val.tryGetString(AsString)) { |
| 117 | + return hash<string>()(AsString) * 31 + Hash; |
| 118 | + } |
| 119 | + return Hash; |
| 120 | + } |
| 121 | +}; |
| 122 | + |
| 123 | +} // namespace std |
| 124 | + |
| 125 | +#endif |
0 commit comments