Jlm
Loading...
Searching...
No Matches
control.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2010 2011 2012 2014 Helge Bahmann <hcb@chaoticmind.net>
3 * Copyright 2013 2014 Nico Reißmann <nico.reissmann@gmail.com>
4 * See COPYING for terms of redistribution.
5 */
6
9#include <jlm/rvsdg/Trace.hpp>
10#include <jlm/util/Hash.hpp>
11
12namespace jlm::rvsdg
13{
14
16
17ControlType::ControlType(size_t nalternatives)
18 : Type(),
19 nalternatives_(nalternatives)
20{}
21
22std::string
24{
25 return jlm::util::strfmt("ctl(", nalternatives_, ")");
26}
27
28bool
29ControlType::operator==(const Type & other) const noexcept
30{
31 auto type = dynamic_cast<const ControlType *>(&other);
32 return type && type->nalternatives_ == nalternatives_;
33}
34
35std::size_t
42
48
49std::shared_ptr<const ControlType>
50ControlType::Create(std::size_t nalternatives)
51{
52 static const ControlType static_instances[4] = {
53 // ControlType(0) is not valid, but put it in here so
54 // the static array indexing works correctly
55 ControlType(0),
56 ControlType(1),
57 ControlType(2),
59 };
60
61 if (nalternatives < 4)
62 {
63 if (nalternatives == 0)
64 {
65 throw util::Error("Alternatives of a control type must be non-zero.");
66 }
67 return std::shared_ptr<const ControlType>(
68 std::shared_ptr<void>(),
70 }
71 else
72 {
73 return std::make_shared<ControlType>(nalternatives);
74 }
75}
76
77ControlValueRepresentation::ControlValueRepresentation(size_t alternative, size_t nalternatives)
78 : alternative_(alternative),
79 nalternatives_(nalternatives)
80{
82 throw util::Error("Alternative is bigger than the number of possible alternatives.");
83}
84
86
88 : NullaryOperation(ControlType::Create(value.nalternatives())),
89 value_(std::move(value))
90{}
91
92bool
94{
95 const auto operation = dynamic_cast<const ControlConstantOperation *>(&other);
96 return operation && operation->value_ == value_;
97}
98
99std::string
104
105std::unique_ptr<Operation>
107{
108 return std::make_unique<ControlConstantOperation>(value_);
109}
110
112
114 size_t nbits,
116 uint64_t default_alternative,
117 size_t nalternatives)
118 : UnaryOperation(BitType::Create(nbits), ControlType::Create(nalternatives)),
119 default_alternative_(default_alternative),
120 mapping_(mapping)
121{}
122
123bool
125{
126 auto op = dynamic_cast<const MatchOperation *>(&other);
127 return op && op->default_alternative_ == default_alternative_ && op->mapping_ == mapping_
128 && op->nbits() == nbits() && op->nalternatives() == nalternatives();
129}
130
131std::string
133{
134 size_t n = 0;
135 std::ostringstream oss;
136 constexpr size_t maxAlternatives = 3;
137 for (auto [input, subregionId] : mapping_)
138 {
139 if (n < maxAlternatives)
140 {
141 oss << input << " -> " << subregionId << ", ";
142 n++;
143 }
144 else
145 {
146 oss << "..., ";
147 break;
148 }
149 }
151
152 return "MATCH[" + oss.str() + "]";
153}
154
155std::unique_ptr<Operation>
157{
158 return std::make_unique<MatchOperation>(*this);
159}
160
161}
ControlValueRepresentation value_
Definition control.hpp:146
~ControlConstantOperation() noexcept override
std::string debug_string() const override
Definition control.cpp:100
std::unique_ptr< Operation > copy() const override
Definition control.cpp:106
bool operator==(const Operation &other) const noexcept override
Definition control.cpp:93
std::string debug_string() const override
Definition control.cpp:23
std::size_t ComputeHash() const noexcept override
Definition control.cpp:36
~ControlType() noexcept override
bool operator==(const jlm::rvsdg::Type &other) const noexcept override
Definition control.cpp:29
size_t nalternatives() const noexcept
Definition control.hpp:42
TypeKind Kind() const noexcept override
Return the kind of this type.
Definition control.cpp:44
static std::shared_ptr< const ControlType > Create(std::size_t nalternatives)
Instantiates control type.
Definition control.cpp:50
ControlValueRepresentation(size_t alternative, size_t nalternatives)
Definition control.cpp:77
size_t nalternatives() const noexcept
Definition control.hpp:89
size_t alternative() const noexcept
Definition control.hpp:83
std::unordered_map< uint64_t, uint64_t > mapping_
Definition control.hpp:258
~MatchOperation() noexcept override
bool operator==(const Operation &other) const noexcept override
Definition control.cpp:124
std::unique_ptr< Operation > copy() const override
Definition control.cpp:156
std::string debug_string() const override
Definition control.cpp:132
Nullary operator (operator taking no formal arguments)
Definition nullary.hpp:22
Unary operator.
Definition unary.hpp:24
TypeKind
The kinds of types supported in rvsdg.
Definition type.hpp:22
@ Value
Designate a value type.
NodeType * TryGetOwnerNode(const rvsdg::Input &input) noexcept
Checks if this is an input to a node of specified type.
Definition node.hpp:872
static std::string strfmt(Args... args)
Definition strfmt.hpp:35
std::size_t CombineHashes(std::size_t hash, Args... args)
Definition Hash.hpp:63