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 unsigned getHashValue(const Variant &Val) {
29 return std::visit(
30 [&Val](auto &&Alternative) {
31 using T = std::decay_t<decltype(Alternative)>;
32 // Include index in hash to make sure same value as different
33 // alternatives don't collide.
34 return DenseMapInfo<std::pair<size_t, T>>::getHashValuePiecewise(
35 Val.index(), Alternative);
36 },
37 Val);
38 }
39
40 static bool isEqual(const Variant &LHS, const Variant &RHS) {
41 if (LHS.index() != RHS.index())
42 return false;
43 if (LHS.valueless_by_exception())
44 return true;
45 // We want to dispatch to DenseMapInfo<T>::isEqual(LHS.get(I), RHS.get(I))
46 // We know the types are the same, but std::visit(V, LHS, RHS) doesn't.
47 // We erase the type held in LHS to void*, and dispatch over RHS.
48 const void *ErasedLHS =
49 std::visit([](const auto &LHS) -> const void * { return &LHS; }, LHS);
50 return std::visit(
51 [&](const auto &RHS) -> bool {
52 using T = std::remove_cv_t<std::remove_reference_t<decltype(RHS)>>;
53 return DenseMapInfo<T>::isEqual(*static_cast<const T *>(ErasedLHS),
54 RHS);
55 },
56 RHS);
57 }
58};
59
60} // end namespace llvm
61
62#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:860
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...