Skip to content

Commit f3a9339

Browse files
committed
[IRGen] Add hashable conformance to [Stack]Addr.
Provide template instantiations of llvm::DenseMapInfo at swift::irgen::Address and swift::irgen::StackAddress.
1 parent 9f6407a commit f3a9339

File tree

1 file changed

+68
-3
lines changed

1 file changed

+68
-3
lines changed

lib/IRGen/Address.h

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
#define SWIFT_IRGEN_ADDRESS_H
1919

2020
#include "IRGen.h"
21-
#include "llvm/IR/Value.h"
21+
#include "llvm/ADT/ilist.h"
2222
#include "llvm/IR/Argument.h"
23+
#include "llvm/IR/DerivedTypes.h"
2324
#include "llvm/IR/Function.h"
2425
#include "llvm/IR/Instruction.h"
25-
#include "llvm/ADT/ilist.h"
26-
#include "llvm/IR/DerivedTypes.h"
26+
#include "llvm/IR/Type.h"
27+
#include "llvm/IR/Value.h"
2728

2829
namespace swift {
2930
namespace irgen {
@@ -39,6 +40,9 @@ class Address {
3940

4041
Address(llvm::Value *addr, llvm::Type *elementType, Alignment align)
4142
: Addr(addr), ElementType(elementType), Align(align) {
43+
if (addr == llvm::DenseMapInfo<llvm::Value *>::getEmptyKey() ||
44+
llvm::DenseMapInfo<llvm::Value *>::getTombstoneKey())
45+
return;
4246
assert(llvm::cast<llvm::PointerType>(addr->getType())
4347
->isOpaqueOrPointeeTypeMatches(elementType) &&
4448
"Incorrect pointer element type");
@@ -63,6 +67,12 @@ class Address {
6367
}
6468

6569
llvm::Type *getElementType() const { return ElementType; }
70+
71+
bool operator==(Address RHS) const {
72+
return Addr == RHS.Addr && ElementType == RHS.ElementType &&
73+
Align == RHS.Align;
74+
}
75+
bool operator!=(Address RHS) const { return !(*this == RHS); }
6676
};
6777

6878
/// An address in memory together with the (possibly null) heap
@@ -139,9 +149,64 @@ class StackAddress {
139149
llvm::Value *getExtraInfo() const { return ExtraInfo; }
140150

141151
bool isValid() const { return Addr.isValid(); }
152+
153+
bool operator==(StackAddress RHS) const {
154+
return Addr == RHS.Addr && ExtraInfo == RHS.ExtraInfo;
155+
}
156+
bool operator!=(StackAddress RHS) const { return !(*this == RHS); }
142157
};
143158

144159
} // end namespace irgen
145160
} // end namespace swift
146161

162+
namespace llvm {
163+
template <>
164+
struct DenseMapInfo<swift::irgen::Address> {
165+
static swift::irgen::Address getEmptyKey() {
166+
return swift::irgen::Address(DenseMapInfo<llvm::Value *>::getEmptyKey(),
167+
DenseMapInfo<llvm::Type *>::getEmptyKey(),
168+
swift::irgen::Alignment(8));
169+
}
170+
static swift::irgen::Address getTombstoneKey() {
171+
return swift::irgen::Address(DenseMapInfo<llvm::Value *>::getTombstoneKey(),
172+
DenseMapInfo<llvm::Type *>::getTombstoneKey(),
173+
swift::irgen::Alignment(8));
174+
}
175+
static unsigned getHashValue(swift::irgen::Address address) {
176+
return detail::combineHashValue(
177+
DenseMapInfo<llvm::Value *>::getHashValue(address.getAddress()),
178+
detail::combineHashValue(
179+
DenseMapInfo<llvm::Type *>::getHashValue(address.getElementType()),
180+
DenseMapInfo<swift::irgen::Alignment::int_type>::getHashValue(
181+
address.getAlignment().getValue())));
182+
}
183+
static bool isEqual(swift::irgen::Address LHS, swift::irgen::Address RHS) {
184+
return LHS == RHS;
185+
}
186+
};
187+
template <>
188+
struct DenseMapInfo<swift::irgen::StackAddress> {
189+
static swift::irgen::StackAddress getEmptyKey() {
190+
return swift::irgen::StackAddress(
191+
DenseMapInfo<swift::irgen::Address>::getEmptyKey(),
192+
DenseMapInfo<llvm::Value *>::getEmptyKey());
193+
}
194+
static swift::irgen::StackAddress getTombstoneKey() {
195+
return swift::irgen::StackAddress(
196+
DenseMapInfo<swift::irgen::Address>::getTombstoneKey(),
197+
DenseMapInfo<llvm::Value *>::getTombstoneKey());
198+
}
199+
static unsigned getHashValue(swift::irgen::StackAddress address) {
200+
return detail::combineHashValue(
201+
DenseMapInfo<swift::irgen::Address>::getHashValue(address.getAddress()),
202+
DenseMapInfo<swift::irgen::Alignment::int_type>::getHashValue(
203+
address.getAlignment().getValue()));
204+
}
205+
static bool isEqual(swift::irgen::StackAddress LHS,
206+
swift::irgen::StackAddress RHS) {
207+
return LHS == RHS;
208+
}
209+
};
210+
} // end namespace llvm
211+
147212
#endif

0 commit comments

Comments
 (0)