LLVM 23.0.0git
DenseMapInfoVariant.h
Go to the documentation of this file.
1//===- DenseMapInfoVariant.h - Type traits for DenseMap<variant> *- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file defines DenseMapInfo traits for DenseMap<std::variant<Ts...>>.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ADT_DENSEMAPINFOVARIANT_H
15#define LLVM_ADT_DENSEMAPINFOVARIANT_H
16
18#include <utility>
19#include <variant>
20
21namespace llvm {
22
23// Provide DenseMapInfo for variants whose all alternatives have DenseMapInfo.
24template <typename... Ts> struct DenseMapInfo<std::variant<Ts...>> {
25 using Variant = std::variant<Ts...>;
26 using FirstT = std::variant_alternative_t<0, Variant>;
27
28 static inline Variant getEmptyKey() {
29 return Variant(std::in_place_index<0>, DenseMapInfo<FirstT>::getEmptyKey());
30 }
31
32 static unsigned getHashValue(const Variant &Val) {
33 return std::visit(
34 [&Val](auto &&Alternative) {
35 using T = std::decay_t<decltype(Alternative)>;
36 // Include index in hash to make sure same value as different
37 // alternatives don't collide.
38 return DenseMapInfo<std::pair<size_t, T>>::getHashValuePiecewise(
39 Val.index(), Alternative);
40 },
41 Val);
42 }
43
44 static bool isEqual(const Variant &LHS, const Variant &RHS) {
45 if (LHS.index() != RHS.index())
46 return false;
47 if (LHS.valueless_by_exception())
48 return true;
49 // We want to dispatch to DenseMapInfo<T>::isEqual(LHS.get(I), RHS.get(I))
50 // We know the types are the same, but std::visit(V, LHS, RHS) doesn't.
51 // We erase the type held in LHS to void*, and dispatch over RHS.
52 const void *ErasedLHS =
53 std::visit([](const auto &LHS) -> const void * { return &LHS; }, LHS);
54 return std::visit(
55 [&](const auto &RHS) -> bool {
56 using T = std::remove_cv_t<std::remove_reference_t<decltype(RHS)>>;
57 return DenseMapInfo<T>::isEqual(*static_cast<const T *>(ErasedLHS),
58 RHS);
59 },
60 RHS);
61 }
62};
63
64} // end namespace llvm
65
66#endif // LLVM_ADT_DENSEMAPINFOVARIANT_H
This file defines DenseMapInfo traits for DenseMap.
#define T
Value * RHS
Value * LHS
This is an optimization pass for GlobalISel generic memory operations.
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:861
static unsigned getHashValue(const Variant &Val)
static bool isEqual(const Variant &LHS, const Variant &RHS)
std::variant_alternative_t< 0, Variant > FirstT
An information struct used to provide DenseMap with the various necessary components for a given valu...