CIRCT 23.0.0git
Loading...
Searching...
No Matches
FieldRef.h
Go to the documentation of this file.
1//===- FieldRef.h - Field References ---------------------------*- 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// This header file defines FieldRefs and helpers for them.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef CIRCT_SUPPORT_FIELDREF_H
14#define CIRCT_SUPPORT_FIELDREF_H
15
16#include "circt/Support/LLVM.h"
17#include "mlir/IR/Value.h"
18#include "llvm/ADT/DenseMapInfo.h"
19
20namespace circt {
21
22/// This class represents a reference to a specific field or element of an
23/// aggregate value. Typically, the user will assign a unique field ID to each
24/// field in an aggregate type by visiting them in a depth-first pre-order.
25///
26/// This can be used as the key in a hashtable to store field specific
27/// information.
28class FieldRef {
29public:
30 /// Get a null FieldRef.
32
34
35 /// Get a FieldRef location for the specified value.
36 FieldRef(Value value, unsigned id) : value(value), id(id) {}
37
38 /// Get the Value which created this location.
39 Value getValue() const { return value; }
40
41 /// Get the operation which defines this field. If the field is a block
42 /// argument it will return the operation which owns the block.
43 Operation *getDefiningOp() const;
44
45 /// Get the operation which defines this field and cast it to the OpTy.
46 /// Returns null if the defining operation is of a different type.
47 template <typename OpTy>
48 OpTy getDefiningOp() const {
49 return llvm::dyn_cast<OpTy>(getDefiningOp());
50 }
51
52 template <typename... Any>
53 bool isa() const {
54 auto *op = getDefiningOp();
55 assert(op && "isa<> used on a null type.");
56 return ::llvm::isa<Any...>(op);
57 }
58
59 /// Get the field ID of this FieldRef, which is a unique identifier mapped to
60 /// a specific field in a bundle.
61 unsigned getFieldID() const { return id; }
62
63 /// Get a reference to a subfield.
64 FieldRef getSubField(unsigned subFieldID) const {
65 return FieldRef(value, id + subFieldID);
66 }
67
68 /// Get the location associated with the value of this field ref.
69 Location getLoc() const { return getValue().getLoc(); }
70
71 bool operator==(const FieldRef &other) const {
72 return value == other.value && id == other.id;
73 }
74
75 bool operator<(const FieldRef &other) const {
76 if (value.getImpl() < other.value.getImpl())
77 return true;
78 if (value.getImpl() > other.value.getImpl())
79 return false;
80 return id < other.id;
81 }
82
83 operator bool() const { return bool(value); }
84
85private:
86 /// A pointer to the value which created this.
87 Value value;
88
89 /// A unique field ID.
90 unsigned id = 0;
91};
92
93/// Get a hash code for a FieldRef.
94inline ::llvm::hash_code hash_value(const FieldRef &fieldRef) {
95 return llvm::hash_combine(fieldRef.getValue(), fieldRef.getFieldID());
96}
97
98} // namespace circt
99
100namespace llvm {
101/// Allow using FieldRef with DenseMaps. This hash is based on the Value
102/// identity and field ID.
103template <>
104struct DenseMapInfo<circt::FieldRef> {
111 static unsigned getHashValue(const circt::FieldRef &val) {
112 return circt::hash_value(val);
113 }
114 static bool isEqual(const circt::FieldRef &lhs, const circt::FieldRef &rhs) {
115 return lhs == rhs;
116 }
117};
118} // namespace llvm
119
120#endif // CIRCT_SUPPORT_FIELDREF_H
assert(baseType &&"element must be base type")
This class represents a reference to a specific field or element of an aggregate value.
Definition FieldRef.h:28
FieldRef(Value value)
Definition FieldRef.h:33
unsigned id
A unique field ID.
Definition FieldRef.h:90
bool operator<(const FieldRef &other) const
Definition FieldRef.h:75
FieldRef()
Get a null FieldRef.
Definition FieldRef.h:31
FieldRef getSubField(unsigned subFieldID) const
Get a reference to a subfield.
Definition FieldRef.h:64
unsigned getFieldID() const
Get the field ID of this FieldRef, which is a unique identifier mapped to a specific field in a bundl...
Definition FieldRef.h:61
bool isa() const
Definition FieldRef.h:53
Value getValue() const
Get the Value which created this location.
Definition FieldRef.h:39
Location getLoc() const
Get the location associated with the value of this field ref.
Definition FieldRef.h:69
Operation * getDefiningOp() const
Get the operation which defines this field.
Definition FieldRef.cpp:19
OpTy getDefiningOp() const
Get the operation which defines this field and cast it to the OpTy.
Definition FieldRef.h:48
FieldRef(Value value, unsigned id)
Get a FieldRef location for the specified value.
Definition FieldRef.h:36
bool operator==(const FieldRef &other) const
Definition FieldRef.h:71
Value value
A pointer to the value which created this.
Definition FieldRef.h:87
static llvm::hash_code hash_value(const ModulePort &port)
Definition HWTypes.h:38
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
static circt::FieldRef getTombstoneKey()
Definition FieldRef.h:108
static circt::FieldRef getEmptyKey()
Definition FieldRef.h:105
static unsigned getHashValue(const circt::FieldRef &val)
Definition FieldRef.h:111
static bool isEqual(const circt::FieldRef &lhs, const circt::FieldRef &rhs)
Definition FieldRef.h:114