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 // Returns true if this is an OpTypeInt instruction.
52 // If N is non-zero, also checks that the bit width matches N.
53 bool isTypeIntN(unsigned N = 0) const;
54 // Returns true if this is an OpTypeFloat instruction.
55 bool isAnyTypeFloat() const;
56 // Returns true if this is an OpTypeInt or OpTypeFloat instruction.
57 bool isTypeIntOrFloat() const { return isTypeIntN() || isAnyTypeFloat(); }
58 // Returns true if this is an OpTypePointer instruction.
59 bool isTypePtr() const;
60
61 friend struct DenseMapInfo<SPIRVTypeInst>;
62};
63
64template <> struct DenseMapInfo<SPIRVTypeInst> {
65 using MIInfo = DenseMapInfo<MachineInstr *>;
66 static unsigned getHashValue(SPIRVTypeInst Ty) {
67 return MIInfo::getHashValue(PtrVal: Ty.MI);
68 }
69 static bool isEqual(SPIRVTypeInst Ty1, SPIRVTypeInst Ty2) {
70 return Ty1 == Ty2;
71 }
72};
73
74} // namespace llvm
75#endif // LLVM_LIB_TARGET_SPIRV_SPIRVTYPEINST_H
76