Jlm
types.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Nico Reißmann <nico.reissmann@gmail.com>
3  * See COPYING for terms of redistribution.
4  */
5 
6 #ifndef JLM_LLVM_IR_TYPES_HPP
7 #define JLM_LLVM_IR_TYPES_HPP
8 
10 #include <jlm/rvsdg/type.hpp>
11 #include <jlm/util/common.hpp>
13 
14 #include <memory>
15 #include <vector>
16 
17 namespace jlm::llvm
18 {
19 
24 class PointerType final : public jlm::rvsdg::Type
25 {
26 public:
27  ~PointerType() noexcept override;
28 
29  PointerType() = default;
30 
31  [[nodiscard]] std::string
32  debug_string() const override;
33 
34  bool
35  operator==(const jlm::rvsdg::Type & other) const noexcept override;
36 
37  [[nodiscard]] std::size_t
38  ComputeHash() const noexcept override;
39 
40  rvsdg::TypeKind
41  Kind() const noexcept override;
42 
43  static std::shared_ptr<const PointerType>
44  Create();
45 };
46 
47 class ArrayType final : public rvsdg::Type
48 {
49 public:
50  ~ArrayType() noexcept override;
51 
52  ArrayType(std::shared_ptr<const Type> type, size_t nelements)
53  : nelements_(nelements),
54  type_(std::move(type))
55  {}
56 
57  ArrayType(const ArrayType & other) = default;
58 
59  ArrayType(ArrayType && other) = default;
60 
61  ArrayType &
62  operator=(const ArrayType &) = delete;
63 
64  ArrayType &
65  operator=(ArrayType &&) = delete;
66 
67  [[nodiscard]] std::string
68  debug_string() const override;
69 
70  bool
71  operator==(const jlm::rvsdg::Type & other) const noexcept override;
72 
73  [[nodiscard]] std::size_t
74  ComputeHash() const noexcept override;
75 
76  rvsdg::TypeKind
77  Kind() const noexcept override;
78 
79  inline size_t
80  nelements() const noexcept
81  {
82  return nelements_;
83  }
84 
85  [[nodiscard]] const rvsdg::Type &
86  element_type() const noexcept
87  {
88  return *type_;
89  }
90 
91  [[nodiscard]] const std::shared_ptr<const rvsdg::Type> &
92  GetElementType() const noexcept
93  {
94  return type_;
95  }
96 
97  static std::shared_ptr<const ArrayType>
98  Create(std::shared_ptr<const Type> type, size_t nelements)
99  {
100  return std::make_shared<ArrayType>(std::move(type), nelements);
101  }
102 
103 private:
104  size_t nelements_;
105  std::shared_ptr<const rvsdg::Type> type_;
106 };
107 
108 /* floating point type */
109 
110 enum class fpsize
111 {
112  half,
113  flt,
114  dbl,
115  x86fp80,
116  fp128
117 };
118 
119 class FloatingPointType final : public rvsdg::Type
120 {
121 public:
122  ~FloatingPointType() noexcept override;
123 
124  explicit FloatingPointType(const fpsize & size)
125  : size_(size)
126  {}
127 
128  [[nodiscard]] std::string
129  debug_string() const override;
130 
131  bool
132  operator==(const jlm::rvsdg::Type & other) const noexcept override;
133 
134  [[nodiscard]] std::size_t
135  ComputeHash() const noexcept override;
136 
137  rvsdg::TypeKind
138  Kind() const noexcept override;
139 
140  inline const fpsize &
141  size() const noexcept
142  {
143  return size_;
144  }
145 
146  static std::shared_ptr<const FloatingPointType>
147  Create(fpsize size);
148 
149 private:
151 };
152 
153 class VariableArgumentType final : public rvsdg::Type
154 {
155 public:
156  ~VariableArgumentType() noexcept override;
157 
158  constexpr VariableArgumentType() = default;
159 
160  bool
161  operator==(const jlm::rvsdg::Type & other) const noexcept override;
162 
163  [[nodiscard]] std::size_t
164  ComputeHash() const noexcept override;
165 
166  rvsdg::TypeKind
167  Kind() const noexcept override;
168 
169  [[nodiscard]] std::string
170  debug_string() const override;
171 
172  static std::shared_ptr<const VariableArgumentType>
173  Create();
174 };
175 
183 class StructType final : public rvsdg::Type
184 {
185  using ElementTypeConstIterator = std::vector<std::shared_ptr<const Type>>::const_iterator;
187 
188 public:
189  ~StructType() noexcept override;
190 
192  std::string name,
193  std::vector<std::shared_ptr<const Type>> types,
194  const bool isPacked,
195  const bool isLiteral)
196  : name_(std::move(name)),
197  types_(std::move(types)),
198  isPacked_(isPacked),
199  isLiteral_(isLiteral)
200  {}
201 
202  StructType(const StructType &) = delete;
203 
204  StructType(StructType &&) = delete;
205 
206  StructType &
207  operator=(const StructType &) = delete;
208 
209  StructType &
210  operator=(StructType &&) = delete;
211 
212  bool
213  operator==(const Type & other) const noexcept override;
214 
215  [[nodiscard]] std::size_t
216  ComputeHash() const noexcept override;
217 
218  rvsdg::TypeKind
219  Kind() const noexcept override;
220 
221  [[nodiscard]] ElementTypeConstRange
222  elementTypes() const noexcept
223  {
224  return ElementTypeConstRange(
225  ElementTypeConstIterator(types_.begin()),
226  ElementTypeConstIterator(types_.end()));
227  }
228 
229  [[nodiscard]] size_t
230  numElements() const noexcept
231  {
232  return types_.size();
233  }
234 
235  [[nodiscard]] std::shared_ptr<const Type>
236  getElementType(const size_t index) const noexcept
237  {
238  JLM_ASSERT(index < numElements());
239  return types_[index];
240  }
241 
242  [[nodiscard]] std::string
243  debug_string() const override;
244 
250  [[nodiscard]] bool
251  HasName() const noexcept
252  {
253  if (isLiteral_)
254  JLM_ASSERT(name_.empty());
255  return !name_.empty();
256  }
257 
265  [[nodiscard]] const std::string &
266  GetName() const noexcept
267  {
268  JLM_ASSERT(!isLiteral_);
269  return name_;
270  }
271 
272  [[nodiscard]] bool
273  IsPacked() const noexcept
274  {
275  return isPacked_;
276  }
277 
283  [[nodiscard]] bool
284  IsLiteral() const noexcept
285  {
286  return isLiteral_;
287  }
288 
296  [[nodiscard]] size_t
297  GetFieldOffset(size_t fieldIndex) const;
298 
306  static std::shared_ptr<const StructType>
308  const std::string & name,
309  std::vector<std::shared_ptr<const Type>> types,
310  bool isPacked)
311  {
312  return std::make_shared<StructType>(name, std::move(types), isPacked, false);
313  }
314 
321  static std::shared_ptr<const StructType>
322  CreateIdentified(std::vector<std::shared_ptr<const Type>> types, bool isPacked)
323  {
324  return CreateIdentified("", std::move(types), isPacked);
325  }
326 
333  static std::shared_ptr<const StructType>
334  CreateLiteral(std::vector<std::shared_ptr<const Type>> types, bool isPacked)
335  {
336  // Literal structs don't have names, so always use the empty string
337  return std::make_shared<StructType>("", std::move(types), isPacked, true);
338  }
339 
340 private:
341  std::string name_;
342  std::vector<std::shared_ptr<const Type>> types_{};
343  bool isPacked_;
345 };
346 
347 class VectorType : public rvsdg::Type
348 {
349 public:
350  VectorType(std::shared_ptr<const Type> type, size_t size)
351  : size_(size),
352  type_(std::move(type))
353  {}
354 
355  VectorType(const VectorType & other) = default;
356 
357  VectorType(VectorType && other) = default;
358 
359  VectorType &
360  operator=(const VectorType & other) = default;
361 
362  VectorType &
363  operator=(VectorType && other) = default;
364 
365  bool
366  operator==(const jlm::rvsdg::Type & other) const noexcept override;
367 
369  Kind() const noexcept override;
370 
371  size_t
372  size() const noexcept
373  {
374  return size_;
375  }
376 
377  [[nodiscard]] const rvsdg::Type &
378  type() const noexcept
379  {
380  return *type_;
381  }
382 
383  [[nodiscard]] const std::shared_ptr<const rvsdg::Type> &
384  Type() const noexcept
385  {
386  return type_;
387  }
388 
389 private:
390  size_t size_;
391  std::shared_ptr<const rvsdg::Type> type_;
392 };
393 
394 class FixedVectorType final : public VectorType
395 {
396 public:
397  ~FixedVectorType() noexcept override;
398 
399  FixedVectorType(std::shared_ptr<const rvsdg::Type> type, size_t size)
400  : VectorType(std::move(type), size)
401  {}
402 
403  bool
404  operator==(const jlm::rvsdg::Type & other) const noexcept override;
405 
406  [[nodiscard]] std::size_t
407  ComputeHash() const noexcept override;
408 
409  [[nodiscard]] std::string
410  debug_string() const override;
411 
412  static std::shared_ptr<const FixedVectorType>
413  Create(std::shared_ptr<const rvsdg::Type> type, size_t size)
414  {
415  return std::make_shared<FixedVectorType>(std::move(type), size);
416  }
417 };
418 
419 class ScalableVectorType final : public VectorType
420 {
421 public:
422  ~ScalableVectorType() noexcept override;
423 
424  ScalableVectorType(std::shared_ptr<const rvsdg::Type> type, size_t size)
425  : VectorType(std::move(type), size)
426  {}
427 
428  bool
429  operator==(const jlm::rvsdg::Type & other) const noexcept override;
430 
431  [[nodiscard]] std::size_t
432  ComputeHash() const noexcept override;
433 
434  [[nodiscard]] std::string
435  debug_string() const override;
436 
437  static std::shared_ptr<const ScalableVectorType>
438  Create(std::shared_ptr<const rvsdg::Type> type, size_t size)
439  {
440  return std::make_shared<ScalableVectorType>(std::move(type), size);
441  }
442 };
443 
448 class IOStateType final : public rvsdg::Type
449 {
450 public:
451  ~IOStateType() noexcept override;
452 
453  constexpr IOStateType() noexcept = default;
454 
455  bool
456  operator==(const jlm::rvsdg::Type & other) const noexcept override;
457 
458  [[nodiscard]] std::size_t
459  ComputeHash() const noexcept override;
460 
461  [[nodiscard]] std::string
462  debug_string() const override;
463 
464  rvsdg::TypeKind
465  Kind() const noexcept override;
466 
467  static std::shared_ptr<const IOStateType>
468  Create();
469 };
470 
476 class MemoryStateType final : public rvsdg::Type
477 {
478 public:
479  ~MemoryStateType() noexcept override;
480 
481  constexpr MemoryStateType() noexcept = default;
482 
483  [[nodiscard]] std::string
484  debug_string() const override;
485 
486  bool
487  operator==(const jlm::rvsdg::Type & other) const noexcept override;
488 
489  [[nodiscard]] std::size_t
490  ComputeHash() const noexcept override;
491 
492  rvsdg::TypeKind
493  Kind() const noexcept override;
494 
495  static std::shared_ptr<const MemoryStateType>
496  Create();
497 };
498 
499 template<class ELEMENTYPE>
500 inline bool
501 IsOrContains(const jlm::rvsdg::Type & type)
502 {
503  if (jlm::rvsdg::is<ELEMENTYPE>(type))
504  return true;
505 
506  if (auto arrayType = dynamic_cast<const ArrayType *>(&type))
507  return IsOrContains<ELEMENTYPE>(arrayType->element_type());
508 
509  if (const auto structType = dynamic_cast<const StructType *>(&type))
510  {
511  for (size_t n = 0; n < structType->numElements(); n++)
512  if (IsOrContains<ELEMENTYPE>(*structType->getElementType(n)))
513  return true;
514 
515  return false;
516  }
517 
518  if (const auto vectorType = dynamic_cast<const VectorType *>(&type))
519  return IsOrContains<ELEMENTYPE>(vectorType->type());
520 
521  return false;
522 }
523 
530 inline bool
532 {
533  return jlm::rvsdg::is<ArrayType>(type) || jlm::rvsdg::is<StructType>(type);
534 }
535 
544 [[nodiscard]] size_t
545 GetTypeStoreSize(const rvsdg::Type & type);
546 
556 [[nodiscard]] size_t
557 GetTypeAllocSize(const rvsdg::Type & type);
558 
567 [[nodiscard]] size_t
568 GetTypeAlignment(const rvsdg::Type & type);
569 
570 }
571 
572 #endif
ArrayType(const ArrayType &other)=default
ArrayType & operator=(ArrayType &&)=delete
ArrayType & operator=(const ArrayType &)=delete
ArrayType(ArrayType &&other)=default
std::shared_ptr< const rvsdg::Type > type_
Definition: types.hpp:105
static std::shared_ptr< const ArrayType > Create(std::shared_ptr< const Type > type, size_t nelements)
Definition: types.hpp:98
~ArrayType() noexcept override
const std::shared_ptr< const rvsdg::Type > & GetElementType() const noexcept
Definition: types.hpp:92
const rvsdg::Type & element_type() const noexcept
Definition: types.hpp:86
~FixedVectorType() noexcept override
~FloatingPointType() noexcept override
Input/Output state type.
Definition: types.hpp:449
~IOStateType() noexcept override
Memory state type class.
Definition: types.hpp:477
~MemoryStateType() noexcept override
PointerType class.
Definition: types.hpp:25
static std::shared_ptr< const PointerType > Create()
Definition: types.cpp:45
~PointerType() noexcept override
bool operator==(const jlm::rvsdg::Type &other) const noexcept override
Definition: types.cpp:27
std::string debug_string() const override
Definition: types.cpp:21
std::size_t ComputeHash() const noexcept override
Definition: types.cpp:33
rvsdg::TypeKind Kind() const noexcept override
Return the kind of this type.
Definition: types.cpp:39
~ScalableVectorType() noexcept override
StructType class.
Definition: types.hpp:184
size_t numElements() const noexcept
Definition: types.hpp:230
std::shared_ptr< const Type > getElementType(const size_t index) const noexcept
Definition: types.hpp:236
StructType & operator=(StructType &&)=delete
StructType(const StructType &)=delete
bool IsLiteral() const noexcept
Definition: types.hpp:284
StructType(StructType &&)=delete
std::string name_
Definition: types.hpp:341
bool IsPacked() const noexcept
Definition: types.hpp:273
bool HasName() const noexcept
Definition: types.hpp:251
static std::shared_ptr< const StructType > CreateIdentified(std::vector< std::shared_ptr< const Type >> types, bool isPacked)
Definition: types.hpp:322
std::vector< std::shared_ptr< const Type > >::const_iterator ElementTypeConstIterator
Definition: types.hpp:185
static std::shared_ptr< const StructType > CreateIdentified(const std::string &name, std::vector< std::shared_ptr< const Type >> types, bool isPacked)
Definition: types.hpp:307
static std::shared_ptr< const StructType > CreateLiteral(std::vector< std::shared_ptr< const Type >> types, bool isPacked)
Definition: types.hpp:334
StructType & operator=(const StructType &)=delete
const std::string & GetName() const noexcept
Definition: types.hpp:266
~StructType() noexcept override
~VariableArgumentType() noexcept override
const rvsdg::Type & type() const noexcept
Definition: types.hpp:378
VectorType(std::shared_ptr< const Type > type, size_t size)
Definition: types.hpp:350
VectorType & operator=(const VectorType &other)=default
VectorType(VectorType &&other)=default
const std::shared_ptr< const rvsdg::Type > & Type() const noexcept
Definition: types.hpp:384
std::shared_ptr< const rvsdg::Type > type_
Definition: types.hpp:391
VectorType(const VectorType &other)=default
VectorType & operator=(VectorType &&other)=default
constexpr Type() noexcept
Definition: type.hpp:46
#define JLM_ASSERT(x)
Definition: common.hpp:16
Global memory state passed between functions.
size_t GetTypeAlignment(const rvsdg::Type &type)
Definition: types.cpp:485
bool IsOrContains(const jlm::rvsdg::Type &type)
Definition: types.hpp:501
size_t GetTypeAllocSize(const rvsdg::Type &type)
Definition: types.cpp:473
size_t GetTypeStoreSize(const rvsdg::Type &type)
Definition: types.cpp:386
bool IsAggregateType(const jlm::rvsdg::Type &type)
Definition: types.hpp:531
TypeKind
The kinds of types supported in rvsdg.
Definition: type.hpp:22