Skip to content

Commit ed7a802

Browse files
committed
ValueEnumerator: make the index type unsigned instead of size_t
This reduces the size of the index from 8 to 4 bytes, which is important in AliasAnalysis where we use pairs of such indices.
1 parent 7f2c262 commit ed7a802

File tree

4 files changed

+27
-24
lines changed

4 files changed

+27
-24
lines changed

include/swift/Basic/ValueEnumerator.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,20 @@
1818

1919
namespace swift {
2020

21+
typedef unsigned ValueIndexTy;
22+
2123
/// / This class maps values to unique indices.
22-
template<class ValueTy, class IndexTy = size_t>
24+
template<class ValueTy>
2325
class ValueEnumerator {
2426
/// A running counter to enumerate values.
25-
IndexTy counter = 0;
27+
ValueIndexTy counter = 0;
2628

2729
/// Maps values to unique integers.
28-
llvm::DenseMap<ValueTy, IndexTy> ValueToIndex;
30+
llvm::DenseMap<ValueTy, ValueIndexTy> ValueToIndex;
2931

3032
public:
3133
/// Return the index of value \p v.
32-
IndexTy getIndex(const ValueTy &v) {
34+
ValueIndexTy getIndex(const ValueTy &v) {
3335
// Return the index of this Key, if we've assigned one already.
3436
auto It = ValueToIndex.find(v);
3537
if (It != ValueToIndex.end()) {
@@ -38,6 +40,7 @@ class ValueEnumerator {
3840

3941
// Generate a new counter for the key.
4042
ValueToIndex[v] = ++counter;
43+
assert(counter != 0 && "counter overflow in ValueEnumerator");
4144
return counter;
4245
}
4346

include/swift/SILOptimizer/Analysis/AliasAnalysis.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,26 @@ namespace {
2727
/// A key used for the AliasAnalysis cache.
2828
///
2929
/// This struct represents the argument list to the method 'alias'. The two
30-
/// SILValue pointers are mapped to size_t indices because we need an
30+
/// SILValue pointers are mapped to integer indices because we need an
3131
/// efficient way to invalidate them (the mechanism is described below). The
3232
/// Type arguments are translated to void* because their underlying storage is
3333
/// opaque pointers that never goes away.
3434
struct AliasKeyTy {
3535
// The SILValue pair:
36-
size_t V1, V2;
36+
swift::ValueIndexTy V1, V2;
3737
// The TBAAType pair:
3838
void *T1, *T2;
3939
};
4040

4141
/// A key used for the MemoryBehavior Analysis cache.
4242
///
43-
/// The two SILValue pointers are mapped to size_t indices because we need an
43+
/// The two SILValue pointers are mapped to integer indices because we need an
4444
/// efficient way to invalidate them (the mechanism is described below). The
4545
/// RetainObserveKind represents the inspection mode for the memory behavior
4646
/// analysis.
4747
struct MemBehaviorKeyTy {
4848
// The SILValue pair:
49-
size_t V1, V2;
49+
swift::ValueIndexTy V1, V2;
5050
};
5151
}
5252

@@ -276,17 +276,17 @@ SILType computeTBAAType(SILValue V);
276276
namespace llvm {
277277
template <> struct DenseMapInfo<AliasKeyTy> {
278278
static inline AliasKeyTy getEmptyKey() {
279-
auto Allone = std::numeric_limits<size_t>::max();
279+
auto Allone = std::numeric_limits<swift::ValueIndexTy>::max();
280280
return {0, Allone, nullptr, nullptr};
281281
}
282282
static inline AliasKeyTy getTombstoneKey() {
283-
auto Allone = std::numeric_limits<size_t>::max();
283+
auto Allone = std::numeric_limits<swift::ValueIndexTy>::max();
284284
return {Allone, 0, nullptr, nullptr};
285285
}
286286
static unsigned getHashValue(const AliasKeyTy Val) {
287287
unsigned H = 0;
288-
H ^= DenseMapInfo<size_t>::getHashValue(Val.V1);
289-
H ^= DenseMapInfo<size_t>::getHashValue(Val.V2);
288+
H ^= DenseMapInfo<swift::ValueIndexTy>::getHashValue(Val.V1);
289+
H ^= DenseMapInfo<swift::ValueIndexTy>::getHashValue(Val.V2);
290290
H ^= DenseMapInfo<void *>::getHashValue(Val.T1);
291291
H ^= DenseMapInfo<void *>::getHashValue(Val.T2);
292292
return H;
@@ -301,17 +301,17 @@ namespace llvm {
301301

302302
template <> struct DenseMapInfo<MemBehaviorKeyTy> {
303303
static inline MemBehaviorKeyTy getEmptyKey() {
304-
auto Allone = std::numeric_limits<size_t>::max();
304+
auto Allone = std::numeric_limits<swift::ValueIndexTy>::max();
305305
return {0, Allone};
306306
}
307307
static inline MemBehaviorKeyTy getTombstoneKey() {
308-
auto Allone = std::numeric_limits<size_t>::max();
308+
auto Allone = std::numeric_limits<swift::ValueIndexTy>::max();
309309
return {Allone, 0};
310310
}
311311
static unsigned getHashValue(const MemBehaviorKeyTy V) {
312312
unsigned H = 0;
313-
H ^= DenseMapInfo<size_t>::getHashValue(V.V1);
314-
H ^= DenseMapInfo<size_t>::getHashValue(V.V2);
313+
H ^= DenseMapInfo<swift::ValueIndexTy>::getHashValue(V.V1);
314+
H ^= DenseMapInfo<swift::ValueIndexTy>::getHashValue(V.V2);
315315
return H;
316316
}
317317
static bool isEqual(const MemBehaviorKeyTy LHS,

lib/SILOptimizer/Analysis/AliasAnalysis.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -818,11 +818,11 @@ SILAnalysis *swift::createAliasAnalysis(SILModule *M) {
818818

819819
AliasKeyTy AliasAnalysis::toAliasKey(SILValue V1, SILValue V2,
820820
SILType Type1, SILType Type2) {
821-
size_t idx1 = ValueToIndex.getIndex(V1);
822-
assert(idx1 != std::numeric_limits<size_t>::max() &&
821+
ValueIndexTy idx1 = ValueToIndex.getIndex(V1);
822+
assert(idx1 != std::numeric_limits<ValueIndexTy>::max() &&
823823
"~0 index reserved for empty/tombstone keys");
824-
size_t idx2 = ValueToIndex.getIndex(V2);
825-
assert(idx2 != std::numeric_limits<size_t>::max() &&
824+
ValueIndexTy idx2 = ValueToIndex.getIndex(V2);
825+
assert(idx2 != std::numeric_limits<ValueIndexTy>::max() &&
826826
"~0 index reserved for empty/tombstone keys");
827827
void *t1 = Type1.getOpaqueValue();
828828
void *t2 = Type2.getOpaqueValue();

lib/SILOptimizer/Analysis/MemoryBehavior.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -546,11 +546,11 @@ AliasAnalysis::computeMemoryBehaviorInner(SILInstruction *Inst, SILValue V) {
546546

547547
MemBehaviorKeyTy AliasAnalysis::toMemoryBehaviorKey(SILInstruction *V1,
548548
SILValue V2) {
549-
size_t idx1 = InstructionToIndex.getIndex(V1);
550-
assert(idx1 != std::numeric_limits<size_t>::max() &&
549+
ValueIndexTy idx1 = InstructionToIndex.getIndex(V1);
550+
assert(idx1 != std::numeric_limits<ValueIndexTy>::max() &&
551551
"~0 index reserved for empty/tombstone keys");
552-
size_t idx2 = ValueToIndex.getIndex(V2);
553-
assert(idx2 != std::numeric_limits<size_t>::max() &&
552+
ValueIndexTy idx2 = ValueToIndex.getIndex(V2);
553+
assert(idx2 != std::numeric_limits<ValueIndexTy>::max() &&
554554
"~0 index reserved for empty/tombstone keys");
555555
return {idx1, idx2};
556556
}

0 commit comments

Comments
 (0)