Jlm
attribute.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Nico Reißmann <nico.reissmann@gmail.com>
3  * See COPYING for terms of redistribution.
4  */
5 
6 #ifndef JLM_LLVM_IR_ATTRIBUTE_HPP
7 #define JLM_LLVM_IR_ATTRIBUTE_HPP
8 
9 #include <jlm/rvsdg/type.hpp>
10 #include <jlm/util/common.hpp>
11 #include <jlm/util/HashSet.hpp>
12 
13 #include <cstdint>
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 namespace jlm::llvm
19 {
20 
23 class Attribute
24 {
25 public:
26  enum class kind
27  {
28  None,
29 
31  AllocAlign,
34  Builtin,
35  Cold,
36  Convergent,
41  Hot,
42  ImmArg,
43  InReg,
44  InlineHint,
45  JumpTable,
46  Memory,
47  MinSize,
49  Naked,
50  Nest,
51  NoAlias,
52  NoBuiltin,
53  NoCallback,
54  NoCapture,
55  NoCfCheck,
57  NoFree,
59  NoInline,
60  NoMerge,
61  NoProfile,
62  NoRecurse,
63  NoRedZone,
64  NoReturn,
67  NoSync,
68  NoUndef,
69  NoUnwind,
71  NonNull,
78  ReadNone,
79  ReadOnly,
80  Returned,
82  SExt,
83  SafeStack,
96  StrictFP,
97  SwiftAsync,
98  SwiftError,
99  SwiftSelf,
100  WillReturn,
101  Writable,
102  WriteOnly,
103  ZExt,
104  LastEnumAttr,
105 
107  ByRef,
108  ByVal,
109  ElementType,
110  InAlloca,
111  Preallocated,
112  StructRet,
113  LastTypeAttr,
114 
115  FirstIntAttr,
116  Alignment,
117  AllocKind,
118  AllocSize,
121  NoFPClass,
123  UWTable,
124  VScaleRange,
125  LastIntAttr,
126 
127  EndAttrKinds
128  };
129 
130  virtual ~Attribute() noexcept;
131 
132  virtual bool
133  operator==(const Attribute &) const = 0;
134 
135  virtual bool
136  operator!=(const Attribute & other) const
137  {
138  return !operator==(other);
139  }
140 };
141 
144 class StringAttribute final : public Attribute
145 {
146 public:
147  ~StringAttribute() noexcept override;
148 
149  StringAttribute(const std::string & kind, const std::string & value)
150  : kind_(kind),
151  value_(value)
152  {}
153 
154  [[nodiscard]] const std::string &
155  kind() const noexcept
156  {
157  return kind_;
158  }
159 
160  [[nodiscard]] const std::string &
161  value() const noexcept
162  {
163  return value_;
164  }
165 
166  bool
167  operator==(const Attribute &) const override;
168 
169 private:
170  std::string kind_;
171  std::string value_;
172 };
173 
176 class EnumAttribute : public Attribute
177 {
178 public:
179  ~EnumAttribute() noexcept override;
180 
181  explicit EnumAttribute(const Attribute::kind & kind)
182  : kind_(kind)
183  {}
184 
185  [[nodiscard]] const Attribute::kind &
186  kind() const noexcept
187  {
188  return kind_;
189  }
190 
191  bool
192  operator==(const Attribute &) const override;
193 
194 private:
196 };
197 
200 class IntAttribute final : public EnumAttribute
201 {
202 public:
203  ~IntAttribute() noexcept override;
204 
206  : EnumAttribute(kind),
207  value_(value)
208  {}
209 
210  [[nodiscard]] uint64_t
211  value() const noexcept
212  {
213  return value_;
214  }
215 
216  bool
217  operator==(const Attribute &) const override;
218 
219 private:
220  uint64_t value_;
221 };
222 
225 class TypeAttribute final : public EnumAttribute
226 {
227 public:
228  ~TypeAttribute() noexcept override;
229 
230  TypeAttribute(Attribute::kind kind, std::shared_ptr<const jlm::rvsdg::Type> type)
231  : EnumAttribute(kind),
232  type_(std::move(type))
233  {}
234 
235  [[nodiscard]] const jlm::rvsdg::Type &
236  type() const noexcept
237  {
238  return *type_;
239  }
240 
241  bool
242  operator==(const Attribute &) const override;
243 
244 private:
245  std::shared_ptr<const jlm::rvsdg::Type> type_;
246 };
247 
248 }
249 
250 namespace jlm::util
251 {
252 
253 template<>
255 {
256  std::size_t
257  operator()(const jlm::llvm::EnumAttribute & attribute) const noexcept
258  {
259  return std::hash<jlm::llvm::Attribute::kind>()(attribute.kind());
260  }
261 };
262 
263 template<>
265 {
266  std::size_t
267  operator()(const jlm::llvm::IntAttribute & attribute) const noexcept
268  {
269  auto kindHash = std::hash<jlm::llvm::Attribute::kind>()(attribute.kind());
270  auto valueHash = std::hash<uint64_t>()(attribute.value());
271  return util::CombineHashes(kindHash, valueHash);
272  }
273 };
274 
275 template<>
277 {
278  std::size_t
279  operator()(const jlm::llvm::StringAttribute & attribute) const noexcept
280  {
281  auto kindHash = std::hash<std::string>()(attribute.kind());
282  auto valueHash = std::hash<std::string>()(attribute.value());
283  return util::CombineHashes(kindHash, valueHash);
284  }
285 };
286 
287 template<>
289 {
290  std::size_t
291  operator()(const jlm::llvm::TypeAttribute & attribute) const noexcept
292  {
293  auto kindHash = std::hash<jlm::llvm::Attribute::kind>()(attribute.kind());
294  auto typeHash = attribute.type().ComputeHash();
295  return util::CombineHashes(kindHash, typeHash);
296  }
297 };
298 
299 }
300 
301 namespace jlm::llvm
302 {
303 
306 class AttributeSet final
307 {
312 
317 
318 public:
319  [[nodiscard]] EnumAttributeRange
320  EnumAttributes() const;
321 
322  [[nodiscard]] IntAttributeRange
323  IntAttributes() const;
324 
325  [[nodiscard]] TypeAttributeRange
326  TypeAttributes() const;
327 
328  [[nodiscard]] StringAttributeRange
329  StringAttributes() const;
330 
331  void
333  {
334  EnumAttributes_.insert(attribute);
335  }
336 
337  void
338  InsertIntAttribute(const IntAttribute & attribute)
339  {
340  IntAttributes_.insert(attribute);
341  }
342 
343  void
345  {
346  TypeAttributes_.insert(attribute);
347  }
348 
349  void
351  {
352  StringAttributes_.insert(attribute);
353  }
354 
355  bool
356  operator==(const AttributeSet & other) const noexcept
357  {
358  return IntAttributes_ == other.IntAttributes_ && EnumAttributes_ == other.EnumAttributes_
359  && TypeAttributes_ == other.TypeAttributes_ && StringAttributes_ == other.StringAttributes_;
360  }
361 
362  bool
363  operator!=(const AttributeSet & other) const noexcept
364  {
365  return !(*this == other);
366  }
367 
368 private:
373 };
374 
375 }
376 
377 #endif
void InsertTypeAttribute(const TypeAttribute &attribute)
Definition: attribute.hpp:344
void InsertStringAttribute(const StringAttribute &attribute)
Definition: attribute.hpp:350
EnumAttributeHashSet EnumAttributes_
Definition: attribute.hpp:369
IntAttributeHashSet IntAttributes_
Definition: attribute.hpp:370
StringAttributeHashSet StringAttributes_
Definition: attribute.hpp:372
EnumAttributeRange EnumAttributes() const
Definition: attribute.cpp:50
TypeAttributeHashSet TypeAttributes_
Definition: attribute.hpp:371
void InsertEnumAttribute(const EnumAttribute &attribute)
Definition: attribute.hpp:332
StringAttributeRange StringAttributes() const
Definition: attribute.cpp:68
TypeAttributeRange TypeAttributes() const
Definition: attribute.cpp:62
IntAttributeRange IntAttributes() const
Definition: attribute.cpp:56
bool operator==(const AttributeSet &other) const noexcept
Definition: attribute.hpp:356
bool operator!=(const AttributeSet &other) const noexcept
Definition: attribute.hpp:363
void InsertIntAttribute(const IntAttribute &attribute)
Definition: attribute.hpp:338
virtual bool operator==(const Attribute &) const =0
@ None
No attributes have been set.
@ EndAttrKinds
Sentinel value useful for loops.
virtual ~Attribute() noexcept
~EnumAttribute() noexcept override
const Attribute::kind & kind() const noexcept
Definition: attribute.hpp:186
Attribute::kind kind_
Definition: attribute.hpp:195
bool operator==(const Attribute &) const override
Definition: attribute.cpp:25
Integer attribute.
Definition: attribute.hpp:201
~IntAttribute() noexcept override
bool operator==(const Attribute &) const override
Definition: attribute.cpp:34
uint64_t value() const noexcept
Definition: attribute.hpp:211
String attribute.
Definition: attribute.hpp:145
const std::string & value() const noexcept
Definition: attribute.hpp:161
bool operator==(const Attribute &) const override
Definition: attribute.cpp:16
~StringAttribute() noexcept override
const std::string & kind() const noexcept
Definition: attribute.hpp:155
std::shared_ptr< const jlm::rvsdg::Type > type_
Definition: attribute.hpp:245
bool operator==(const Attribute &) const override
Definition: attribute.cpp:43
const jlm::rvsdg::Type & type() const noexcept
Definition: attribute.hpp:236
~TypeAttribute() noexcept override
bool insert(ItemType item)
Definition: HashSet.hpp:210
Global memory state passed between functions.
std::size_t CombineHashes(std::size_t hash, Args... args)
Definition: Hash.hpp:63
std::size_t operator()(const jlm::llvm::EnumAttribute &attribute) const noexcept
Definition: attribute.hpp:257
std::size_t operator()(const jlm::llvm::IntAttribute &attribute) const noexcept
Definition: attribute.hpp:267
std::size_t operator()(const jlm::llvm::StringAttribute &attribute) const noexcept
Definition: attribute.hpp:279
std::size_t operator()(const jlm::llvm::TypeAttribute &attribute) const noexcept
Definition: attribute.hpp:291