1//===-- SPIRVTypeInst.h - SPIR-V Type Instruction ---------------*- 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// SPIRVTypeInst is used to represent a SPIR-V type instruction.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_TARGET_SPIRV_SPIRVTYPEINST_H
14#define LLVM_LIB_TARGET_SPIRV_SPIRVTYPEINST_H
15
16#include "llvm/ADT/DenseMapInfo.h"
17
18namespace llvm {
19class MachineInstr;
20class MachineRegisterInfo;
21
22class SPIRVTypeInst {
23 const MachineInstr *MI;
24
25 // Used by DenseMapInfo to bypass the assertion. The thombstone and empty keys
26 // are not null. They are -1 and -2 aligned to the appropiate pointer size.
27 struct UncheckedConstructor {};
28 SPIRVTypeInst(const MachineInstr *MI, UncheckedConstructor) : MI(MI) {};
29
30public:
31 SPIRVTypeInst(const MachineInstr &MI) : SPIRVTypeInst(&MI) {}
32 SPIRVTypeInst(const MachineInstr *MI = nullptr);
33
34 // No need to verify the register since it's already verified by the copied
35 // object.
36 SPIRVTypeInst(const SPIRVTypeInst &Other) = default;
37 SPIRVTypeInst &operator=(const SPIRVTypeInst &Other) = default;
38
39 const MachineInstr &operator*() const { return *MI; }
40 const MachineInstr *operator->() const { return MI; }
41 operator const MachineInstr *() const { return MI; }
42
43 bool operator==(const SPIRVTypeInst &Other) const { return MI == Other.MI; }
44 bool operator!=(const SPIRVTypeInst &Other) const { return MI != Other.MI; }
45
46 bool operator==(const MachineInstr *Other) const { return MI == Other; }
47 bool operator!=(const MachineInstr *Other) const { return MI != Other; }
48
49 operator bool() const { return MI; }
50
51 friend struct DenseMapInfo<SPIRVTypeInst>;
52};
53
54template <> struct DenseMapInfo<SPIRVTypeInst> {
55 using MIInfo = DenseMapInfo<MachineInstr *>;
56 static SPIRVTypeInst getEmptyKey() {
57 return {MIInfo::getEmptyKey(), SPIRVTypeInst::UncheckedConstructor()};
58 }
59 static SPIRVTypeInst getTombstoneKey() {
60 return {MIInfo::getTombstoneKey(), SPIRVTypeInst::UncheckedConstructor()};
61 }
62 static unsigned getHashValue(SPIRVTypeInst Ty) {
63 return MIInfo::getHashValue(PtrVal: Ty.MI);
64 }
65 static bool isEqual(SPIRVTypeInst Ty1, SPIRVTypeInst Ty2) {
66 return Ty1 == Ty2;
67 }
68};
69
70} // namespace llvm
71#endif
72