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 public:
186  ~StructType() noexcept override;
187 
189  std::string name,
190  std::vector<std::shared_ptr<const Type>> types,
191  const bool isPacked,
192  const bool isLiteral)
193  : name_(std::move(name)),
194  types_(std::move(types)),
195  isPacked_(isPacked),
196  isLiteral_(isLiteral)
197  {}
198 
199  StructType(const StructType &) = delete;
200 
201  StructType(StructType &&) = delete;
202 
203  StructType &
204  operator=(const StructType &) = delete;
205 
206  StructType &
207  operator=(StructType &&) = delete;
208 
209  bool
210  operator==(const Type & other) const noexcept override;
211 
212  [[nodiscard]] std::size_t
213  ComputeHash() const noexcept override;
214 
215  rvsdg::TypeKind
216  Kind() const noexcept override;
217 
218  [[nodiscard]] size_t
219  numElements() const noexcept
220  {
221  return types_.size();
222  }
223 
224  [[nodiscard]] std::shared_ptr<const Type>
225  getElementType(const size_t index) const noexcept
226  {
227  JLM_ASSERT(index < numElements());
228  return types_[index];
229  }
230 
231  [[nodiscard]] std::string
232  debug_string() const override;
233 
239  [[nodiscard]] bool
240  HasName() const noexcept
241  {
242  if (isLiteral_)
243  JLM_ASSERT(name_.empty());
244  return !name_.empty();
245  }
246 
254  [[nodiscard]] const std::string &
255  GetName() const noexcept
256  {
257  JLM_ASSERT(!isLiteral_);
258  return name_;
259  }
260 
261  [[nodiscard]] bool
262  IsPacked() const noexcept
263  {
264  return isPacked_;
265  }
266 
272  [[nodiscard]] bool
273  IsLiteral() const noexcept
274  {
275  return isLiteral_;
276  }
277 
285  [[nodiscard]] size_t
286  GetFieldOffset(size_t fieldIndex) const;
287 
295  static std::shared_ptr<const StructType>
297  const std::string & name,
298  std::vector<std::shared_ptr<const Type>> types,
299  bool isPacked)
300  {
301  return std::make_shared<StructType>(name, std::move(types), isPacked, false);
302  }
303 
310  static std::shared_ptr<const StructType>
311  CreateIdentified(std::vector<std::shared_ptr<const Type>> types, bool isPacked)
312  {
313  return CreateIdentified("", std::move(types), isPacked);
314  }
315 
322  static std::shared_ptr<const StructType>
323  CreateLiteral(std::vector<std::shared_ptr<const Type>> types, bool isPacked)
324  {
325  // Literal structs don't have names, so always use the empty string
326  return std::make_shared<StructType>("", std::move(types), isPacked, true);
327  }
328 
329 private:
330  std::string name_;
331  std::vector<std::shared_ptr<const Type>> types_{};
332  bool isPacked_;
334 };
335 
336 class VectorType : public rvsdg::Type
337 {
338 public:
339  VectorType(std::shared_ptr<const Type> type, size_t size)
340  : size_(size),
341  type_(std::move(type))
342  {}
343 
344  VectorType(const VectorType & other) = default;
345 
346  VectorType(VectorType && other) = default;
347 
348  VectorType &
349  operator=(const VectorType & other) = default;
350 
351  VectorType &
352  operator=(VectorType && other) = default;
353 
354  bool
355  operator==(const jlm::rvsdg::Type & other) const noexcept override;
356 
358  Kind() const noexcept override;
359 
360  size_t
361  size() const noexcept
362  {
363  return size_;
364  }
365 
366  [[nodiscard]] const rvsdg::Type &
367  type() const noexcept
368  {
369  return *type_;
370  }
371 
372  [[nodiscard]] const std::shared_ptr<const rvsdg::Type> &
373  Type() const noexcept
374  {
375  return type_;
376  }
377 
378 private:
379  size_t size_;
380  std::shared_ptr<const rvsdg::Type> type_;
381 };
382 
383 class FixedVectorType final : public VectorType
384 {
385 public:
386  ~FixedVectorType() noexcept override;
387 
388  FixedVectorType(std::shared_ptr<const rvsdg::Type> type, size_t size)
389  : VectorType(std::move(type), size)
390  {}
391 
392  bool
393  operator==(const jlm::rvsdg::Type & other) const noexcept override;
394 
395  [[nodiscard]] std::size_t
396  ComputeHash() const noexcept override;
397 
398  [[nodiscard]] std::string
399  debug_string() const override;
400 
401  static std::shared_ptr<const FixedVectorType>
402  Create(std::shared_ptr<const rvsdg::Type> type, size_t size)
403  {
404  return std::make_shared<FixedVectorType>(std::move(type), size);
405  }
406 };
407 
408 class ScalableVectorType final : public VectorType
409 {
410 public:
411  ~ScalableVectorType() noexcept override;
412 
413  ScalableVectorType(std::shared_ptr<const rvsdg::Type> type, size_t size)
414  : VectorType(std::move(type), size)
415  {}
416 
417  bool
418  operator==(const jlm::rvsdg::Type & other) const noexcept override;
419 
420  [[nodiscard]] std::size_t
421  ComputeHash() const noexcept override;
422 
423  [[nodiscard]] std::string
424  debug_string() const override;
425 
426  static std::shared_ptr<const ScalableVectorType>
427  Create(std::shared_ptr<const rvsdg::Type> type, size_t size)
428  {
429  return std::make_shared<ScalableVectorType>(std::move(type), size);
430  }
431 };
432 
437 class IOStateType final : public rvsdg::Type
438 {
439 public:
440  ~IOStateType() noexcept override;
441 
442  constexpr IOStateType() noexcept = default;
443 
444  bool
445  operator==(const jlm::rvsdg::Type & other) const noexcept override;
446 
447  [[nodiscard]] std::size_t
448  ComputeHash() const noexcept override;
449 
450  [[nodiscard]] std::string
451  debug_string() const override;
452 
453  rvsdg::TypeKind
454  Kind() const noexcept override;
455 
456  static std::shared_ptr<const IOStateType>
457  Create();
458 };
459 
465 class MemoryStateType final : public rvsdg::Type
466 {
467 public:
468  ~MemoryStateType() noexcept override;
469 
470  constexpr MemoryStateType() noexcept = default;
471 
472  [[nodiscard]] std::string
473  debug_string() const override;
474 
475  bool
476  operator==(const jlm::rvsdg::Type & other) const noexcept override;
477 
478  [[nodiscard]] std::size_t
479  ComputeHash() const noexcept override;
480 
481  rvsdg::TypeKind
482  Kind() const noexcept override;
483 
484  static std::shared_ptr<const MemoryStateType>
485  Create();
486 };
487 
488 template<class ELEMENTYPE>
489 inline bool
490 IsOrContains(const jlm::rvsdg::Type & type)
491 {
492  if (jlm::rvsdg::is<ELEMENTYPE>(type))
493  return true;
494 
495  if (auto arrayType = dynamic_cast<const ArrayType *>(&type))
496  return IsOrContains<ELEMENTYPE>(arrayType->element_type());
497 
498  if (const auto structType = dynamic_cast<const StructType *>(&type))
499  {
500  for (size_t n = 0; n < structType->numElements(); n++)
501  if (IsOrContains<ELEMENTYPE>(*structType->getElementType(n)))
502  return true;
503 
504  return false;
505  }
506 
507  if (const auto vectorType = dynamic_cast<const VectorType *>(&type))
508  return IsOrContains<ELEMENTYPE>(vectorType->type());
509 
510  return false;
511 }
512 
519 inline bool
521 {
522  return jlm::rvsdg::is<ArrayType>(type) || jlm::rvsdg::is<StructType>(type);
523 }
524 
533 [[nodiscard]] size_t
535 
545 [[nodiscard]] size_t
547 
556 [[nodiscard]] size_t
558 
559 }
560 
561 #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:438
~IOStateType() noexcept override
Memory state type class.
Definition: types.hpp:466
~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
std::shared_ptr< const Type > getElementType(const size_t index) const noexcept
Definition: types.hpp:225
StructType & operator=(StructType &&)=delete
StructType(const StructType &)=delete
bool IsLiteral() const noexcept
Definition: types.hpp:273
StructType(StructType &&)=delete
std::string name_
Definition: types.hpp:330
bool IsPacked() const noexcept
Definition: types.hpp:262
bool HasName() const noexcept
Definition: types.hpp:240
static std::shared_ptr< const StructType > CreateIdentified(std::vector< std::shared_ptr< const Type >> types, bool isPacked)
Definition: types.hpp:311
static std::shared_ptr< const StructType > CreateIdentified(const std::string &name, std::vector< std::shared_ptr< const Type >> types, bool isPacked)
Definition: types.hpp:296
static std::shared_ptr< const StructType > CreateLiteral(std::vector< std::shared_ptr< const Type >> types, bool isPacked)
Definition: types.hpp:323
StructType & operator=(const StructType &)=delete
const std::string & GetName() const noexcept
Definition: types.hpp:255
~StructType() noexcept override
~VariableArgumentType() noexcept override
const rvsdg::Type & type() const noexcept
Definition: types.hpp:367
VectorType(std::shared_ptr< const Type > type, size_t size)
Definition: types.hpp:339
VectorType & operator=(const VectorType &other)=default
VectorType(VectorType &&other)=default
const std::shared_ptr< const rvsdg::Type > & Type() const noexcept
Definition: types.hpp:373
std::shared_ptr< const rvsdg::Type > type_
Definition: types.hpp:380
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:490
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:520
static std::string type(const Node *n)
Definition: view.cpp:255
TypeKind
The kinds of types supported in rvsdg.
Definition: type.hpp:22