Jlm
Loading...
Searching...
No Matches
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
18namespace jlm::llvm
19{
20
24{
25public:
26 enum class kind
27 {
28 None,
29
34 Builtin,
35 Cold,
41 Hot,
42 ImmArg,
43 InReg,
46 Memory,
47 MinSize,
49 Naked,
50 Nest,
51 NoAlias,
57 NoFree,
60 NoMerge,
67 NoSync,
68 NoUndef,
71 NonNull,
82 SExt,
101 Writable,
102 WriteOnly,
103 ZExt,
105
107 ByRef,
108 ByVal,
110 InAlloca,
112 StructRet,
114
116 Alignment,
117 AllocKind,
118 AllocSize,
121 NoFPClass,
123 UWTable,
126
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
144class StringAttribute final : public Attribute
145{
146public:
147 ~StringAttribute() noexcept override;
148
149 StringAttribute(const std::string & kind, const std::string & value)
150 : kind_(kind),
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
169private:
170 std::string kind_;
171 std::string value_;
172};
173
177{
178public:
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
194private:
196};
197
200class IntAttribute final : public EnumAttribute
201{
202public:
203 ~IntAttribute() noexcept override;
204
209
210 [[nodiscard]] uint64_t
211 value() const noexcept
212 {
213 return value_;
214 }
215
216 bool
217 operator==(const Attribute &) const override;
218
219private:
220 uint64_t value_;
221};
222
225class TypeAttribute final : public EnumAttribute
226{
227public:
228 ~TypeAttribute() noexcept override;
229
230 TypeAttribute(Attribute::kind kind, std::shared_ptr<const jlm::rvsdg::Type> type)
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
244private:
245 std::shared_ptr<const jlm::rvsdg::Type> type_;
246};
247
248}
249
250namespace jlm::util
251{
252
253template<>
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
263template<>
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
275template<>
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
287template<>
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
301namespace jlm::llvm
302{
303
306class AttributeSet final
307{
312
317
318public:
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
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 [[nodiscard]] size_t
369 numAttributes() const noexcept
370 {
373 }
374
375private:
380};
381
382class AttributeList final
383{
384public:
386 AttributeSet functionAttributes,
387 AttributeSet returnAttributes,
388 std::vector<AttributeSet> parameterAttributes)
389 : returnAttributes_(std::move(returnAttributes)),
390 functionAttributes_(std::move(functionAttributes)),
391 parameterAttributes_(std::move(parameterAttributes))
392 {}
393
394 [[nodiscard]] const AttributeSet &
395 getFunctionAttributes() const noexcept
396 {
397 return functionAttributes_;
398 }
399
400 [[nodiscard]] const AttributeSet &
401 getReturnAttributes() const noexcept
402 {
403 return returnAttributes_;
404 }
405
406 [[nodiscard]] const std::vector<AttributeSet> &
407 getParameterAttributes() const noexcept
408 {
410 }
411
412 bool
413 operator==(const AttributeList & other) const noexcept
414 {
415 return returnAttributes_ == other.returnAttributes_
416 && functionAttributes_ == other.functionAttributes_
417 && parameterAttributes_ == other.parameterAttributes_;
418 }
419
420 bool
421 operator!=(const AttributeList & other) const noexcept
422 {
423 return !(*this == other);
424 }
425
426 static AttributeList
428 {
429 return AttributeList(AttributeSet(), AttributeSet(), std::vector<AttributeSet>());
430 }
431
432private:
435 std::vector<AttributeSet> parameterAttributes_{};
436};
437
438}
439
440#endif
AttributeSet functionAttributes_
bool operator==(const AttributeList &other) const noexcept
AttributeSet returnAttributes_
const std::vector< AttributeSet > & getParameterAttributes() const noexcept
AttributeList(AttributeSet functionAttributes, AttributeSet returnAttributes, std::vector< AttributeSet > parameterAttributes)
const AttributeSet & getReturnAttributes() const noexcept
const AttributeSet & getFunctionAttributes() const noexcept
bool operator!=(const AttributeList &other) const noexcept
static AttributeList createEmptyList()
std::vector< AttributeSet > parameterAttributes_
void InsertTypeAttribute(const TypeAttribute &attribute)
void InsertStringAttribute(const StringAttribute &attribute)
EnumAttributeHashSet EnumAttributes_
size_t numAttributes() const noexcept
IntAttributeHashSet IntAttributes_
StringAttributeHashSet StringAttributes_
EnumAttributeRange EnumAttributes() const
Definition attribute.cpp:50
TypeAttributeHashSet TypeAttributes_
void InsertEnumAttribute(const EnumAttribute &attribute)
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
bool operator!=(const AttributeSet &other) const noexcept
void InsertIntAttribute(const IntAttribute &attribute)
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
Attribute::kind kind_
bool operator==(const Attribute &) const override
Definition attribute.cpp:25
Integer attribute.
~IntAttribute() noexcept override
bool operator==(const Attribute &) const override
Definition attribute.cpp:34
uint64_t value() const noexcept
const std::string & kind() const noexcept
bool operator==(const Attribute &) const override
Definition attribute.cpp:16
~StringAttribute() noexcept override
const std::string & value() const noexcept
std::shared_ptr< const jlm::rvsdg::Type > type_
bool operator==(const Attribute &) const override
Definition attribute.cpp:43
const jlm::rvsdg::Type & type() const noexcept
~TypeAttribute() noexcept override
bool insert(ItemType item)
Definition HashSet.hpp:210
std::size_t Size() const noexcept
Definition HashSet.hpp:187
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
std::size_t operator()(const jlm::llvm::IntAttribute &attribute) const noexcept
std::size_t operator()(const jlm::llvm::StringAttribute &attribute) const noexcept
std::size_t operator()(const jlm::llvm::TypeAttribute &attribute) const noexcept