Skip to content

Commit edb53d4

Browse files
committed
[CachedBitsValue] Switch from std::optional to PointerIntPair
1 parent a736647 commit edb53d4

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

llvm/include/llvm/Analysis/CachedBitsValue.h

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef LLVM_ANALYSIS_CACHEDBITSVALUE_H
1515
#define LLVM_ANALYSIS_CACHEDBITSVALUE_H
1616

17+
#include "llvm/ADT/PointerIntPair.h"
1718
#include "llvm/IR/Value.h"
1819
#include "llvm/Support/KnownBits.h"
1920
#include <type_traits>
@@ -45,75 +46,81 @@ template <bool ConstPointer = true> class ImplCachedBitsValue {
4546
constexpr static bool ValuePointerConvertible =
4647
std::is_convertible_v<T, ValuePointerType>;
4748

48-
ValuePointerType Pointer;
49-
mutable std::optional<KnownBits> Known;
49+
// Store the presence of the KnownBits information in one of the bits of
50+
// Pointer.
51+
// true -> present
52+
// false -> absent
53+
mutable PointerIntPair<ValuePointerType, 1, bool> Pointer;
54+
mutable KnownBits Known;
5055

5156
void calculateKnownBits(unsigned Depth, const SimplifyQuery &Q) const {
52-
Known = computeKnownBits(Pointer, Depth, Q);
57+
Known = computeKnownBits(Pointer.getPointer(), Depth, Q);
58+
Pointer.setInt(true);
5359
}
5460

5561
void calculateKnownBits(const APInt &DemandedElts, unsigned Depth,
5662
const SimplifyQuery &Q) const {
57-
Known = computeKnownBits(Pointer, DemandedElts, Depth, Q);
63+
Known = computeKnownBits(Pointer.getPointer(), DemandedElts, Depth, Q);
64+
Pointer.setInt(false);
5865
}
5966

6067
public:
6168
ImplCachedBitsValue() = default;
6269
ImplCachedBitsValue(ValuePointerType Pointer)
63-
: Pointer(Pointer), Known(std::nullopt) {}
70+
: Pointer(Pointer, false) {}
6471
ImplCachedBitsValue(ValuePointerType Pointer, const KnownBits &Known)
65-
: Pointer(Pointer), Known(Known) {}
72+
: Pointer(Pointer, true), Known(Known) {}
6673

6774
template <typename T, std::enable_if_t<ValuePointerConvertible<T>, int> = 0>
6875
ImplCachedBitsValue(const T &Value)
69-
: Pointer(static_cast<ValuePointerType>(Value)), Known(std::nullopt) {}
76+
: Pointer(static_cast<ValuePointerType>(Value), false) {}
7077

7178
template <typename T, std::enable_if_t<ValuePointerConvertible<T>, int> = 0>
7279
ImplCachedBitsValue(const T &Value, const KnownBits &Known)
73-
: Pointer(static_cast<ValuePointerType>(Value)), Known(Known) {}
80+
: Pointer(static_cast<ValuePointerType>(Value), true), Known(Known) {}
7481

75-
[[nodiscard]] ValuePointerType getValue() { return Pointer; }
76-
[[nodiscard]] ValuePointerType getValue() const { return Pointer; }
82+
[[nodiscard]] ValuePointerType getValue() { return Pointer.getPointer(); }
83+
[[nodiscard]] ValuePointerType getValue() const { return Pointer.getPointer(); }
7784

7885
[[nodiscard]] const KnownBits &getKnownBits(unsigned Depth,
7986
const SimplifyQuery &Q) const {
8087
if (!hasKnownBits())
8188
calculateKnownBits(Depth, Q);
82-
return Known.value();
89+
return Known;
8390
}
8491

8592
[[nodiscard]] KnownBits &getKnownBits(unsigned Depth,
8693
const SimplifyQuery &Q) {
8794
if (!hasKnownBits())
8895
calculateKnownBits(Depth, Q);
89-
return Known.value();
96+
return Known;
9097
}
9198

9299
[[nodiscard]] const KnownBits &getKnownBits(const APInt &DemandedElts,
93100
unsigned Depth,
94101
const SimplifyQuery &Q) const {
95102
if (!hasKnownBits())
96103
calculateKnownBits(DemandedElts, Depth, Q);
97-
return Known.value();
104+
return Known;
98105
}
99106

100107
[[nodiscard]] KnownBits &getKnownBits(const APInt &DemandedElts,
101108
unsigned Depth,
102109
const SimplifyQuery &Q) {
103110
if (!hasKnownBits())
104111
calculateKnownBits(DemandedElts, Depth, Q);
105-
return Known.value();
112+
return Known;
106113
}
107114

108-
[[nodiscard]] bool hasKnownBits() const { return Known.has_value(); }
115+
[[nodiscard]] bool hasKnownBits() const { return Pointer.getInt(); }
109116

110-
operator ValuePointerType() { return Pointer; }
111-
ValuePointerType operator->() { return Pointer; }
112-
ValueReferenceType operator*() { return *Pointer; }
117+
operator ValuePointerType() { return Pointer.getPointer(); }
118+
ValuePointerType operator->() { return Pointer.getPointer(); }
119+
ValueReferenceType operator*() { return *Pointer.getPointer(); }
113120

114-
operator ValuePointerType() const { return Pointer; }
115-
ValuePointerType operator->() const { return Pointer; }
116-
ValueReferenceType operator*() const { return *Pointer; }
121+
operator ValuePointerType() const { return Pointer.getPointer(); }
122+
ValuePointerType operator->() const { return Pointer.getPointer(); }
123+
ValueReferenceType operator*() const { return *Pointer.getPointer(); }
117124
};
118125
} // namespace detail
119126

@@ -151,7 +158,7 @@ class CachedBitsNonConstValue : public detail::ImplCachedBitsValue<false> {
151158

152159
[[nodiscard]] CachedBitsConstValue toConst() const {
153160
if (hasKnownBits())
154-
return CachedBitsConstValue(getValue(), Known.value());
161+
return CachedBitsConstValue(getValue(), Known);
155162
else
156163
return CachedBitsConstValue(getValue());
157164
}

0 commit comments

Comments
 (0)