Skip to content

Commit 9c60431

Browse files
[NFC] Add a specialization of DenseMapInfo for SmallVector (llvm#140380)
Equivalent to the three existing uses I found which were all pointers. Implementing the general pattern so SmallVector<int> etc will work as well. Added to the SmallVector.h header as opposed to DenseMapInfo.h following the StringRef.h and SmallBitVector.h prior art. Noticed while writing an unrelated patch which currently wants a map from small vectors to other things and cleaner to generalise than add another specialisation to said patch.
1 parent 60fb921 commit 9c60431

File tree

3 files changed

+24
-51
lines changed

3 files changed

+24
-51
lines changed

llvm/include/llvm/ADT/SmallVector.h

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

17+
#include "llvm/ADT/DenseMapInfo.h"
1718
#include "llvm/Support/Compiler.h"
1819
#include <algorithm>
1920
#include <cassert>
@@ -1319,6 +1320,26 @@ extern template class llvm::SmallVectorBase<uint32_t>;
13191320
extern template class llvm::SmallVectorBase<uint64_t>;
13201321
#endif
13211322

1323+
// Provide DenseMapInfo for SmallVector of a type which has info.
1324+
template <typename T, unsigned N> struct DenseMapInfo<llvm::SmallVector<T, N>> {
1325+
static SmallVector<T, N> getEmptyKey() {
1326+
return {DenseMapInfo<T>::getEmptyKey()};
1327+
}
1328+
1329+
static SmallVector<T, N> getTombstoneKey() {
1330+
return {DenseMapInfo<T>::getTombstoneKey()};
1331+
}
1332+
1333+
static unsigned getHashValue(const SmallVector<T, N> &V) {
1334+
return static_cast<unsigned>(hash_combine_range(V));
1335+
}
1336+
1337+
static bool isEqual(const SmallVector<T, N> &LHS,
1338+
const SmallVector<T, N> &RHS) {
1339+
return LHS == RHS;
1340+
}
1341+
};
1342+
13221343
} // end namespace llvm
13231344

13241345
namespace std {

llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,38 +1275,13 @@ struct LSRFixup {
12751275
void dump() const;
12761276
};
12771277

1278-
/// A DenseMapInfo implementation for holding DenseMaps and DenseSets of sorted
1279-
/// SmallVectors of const SCEV*.
1280-
struct UniquifierDenseMapInfo {
1281-
static SmallVector<const SCEV *, 4> getEmptyKey() {
1282-
SmallVector<const SCEV *, 4> V;
1283-
V.push_back(reinterpret_cast<const SCEV *>(-1));
1284-
return V;
1285-
}
1286-
1287-
static SmallVector<const SCEV *, 4> getTombstoneKey() {
1288-
SmallVector<const SCEV *, 4> V;
1289-
V.push_back(reinterpret_cast<const SCEV *>(-2));
1290-
return V;
1291-
}
1292-
1293-
static unsigned getHashValue(const SmallVector<const SCEV *, 4> &V) {
1294-
return static_cast<unsigned>(hash_combine_range(V));
1295-
}
1296-
1297-
static bool isEqual(const SmallVector<const SCEV *, 4> &LHS,
1298-
const SmallVector<const SCEV *, 4> &RHS) {
1299-
return LHS == RHS;
1300-
}
1301-
};
1302-
13031278
/// This class holds the state that LSR keeps for each use in IVUsers, as well
13041279
/// as uses invented by LSR itself. It includes information about what kinds of
13051280
/// things can be folded into the user, information about the user itself, and
13061281
/// information about how the use may be satisfied. TODO: Represent multiple
13071282
/// users of the same expression in common?
13081283
class LSRUse {
1309-
DenseSet<SmallVector<const SCEV *, 4>, UniquifierDenseMapInfo> Uniquifier;
1284+
DenseSet<SmallVector<const SCEV *, 4>> Uniquifier;
13101285

13111286
public:
13121287
/// An enum for a kind of use, indicating what types of scaled and immediate
@@ -4754,8 +4729,7 @@ void LSRInstance::FilterOutUndesirableDedicatedRegisters() {
47544729

47554730
// Collect the best formula for each unique set of shared registers. This
47564731
// is reset for each use.
4757-
using BestFormulaeTy =
4758-
DenseMap<SmallVector<const SCEV *, 4>, size_t, UniquifierDenseMapInfo>;
4732+
using BestFormulaeTy = DenseMap<SmallVector<const SCEV *, 4>, size_t>;
47594733

47604734
BestFormulaeTy BestFormulae;
47614735

llvm/lib/Transforms/Vectorize/VPlanSLP.h

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -73,30 +73,8 @@ class VPInterleavedAccessInfo {
7373
class VPlanSlp {
7474
enum class OpMode { Failed, Load, Opcode };
7575

76-
/// A DenseMapInfo implementation for using SmallVector<VPValue *, 4> as
77-
/// DenseMap keys.
78-
struct BundleDenseMapInfo {
79-
static SmallVector<VPValue *, 4> getEmptyKey() {
80-
return {reinterpret_cast<VPValue *>(-1)};
81-
}
82-
83-
static SmallVector<VPValue *, 4> getTombstoneKey() {
84-
return {reinterpret_cast<VPValue *>(-2)};
85-
}
86-
87-
static unsigned getHashValue(const SmallVector<VPValue *, 4> &V) {
88-
return static_cast<unsigned>(hash_combine_range(V));
89-
}
90-
91-
static bool isEqual(const SmallVector<VPValue *, 4> &LHS,
92-
const SmallVector<VPValue *, 4> &RHS) {
93-
return LHS == RHS;
94-
}
95-
};
96-
9776
/// Mapping of values in the original VPlan to a combined VPInstruction.
98-
DenseMap<SmallVector<VPValue *, 4>, VPInstruction *, BundleDenseMapInfo>
99-
BundleToCombined;
77+
DenseMap<SmallVector<VPValue *, 4>, VPInstruction *> BundleToCombined;
10078

10179
VPInterleavedAccessInfo &IAI;
10280

0 commit comments

Comments
 (0)