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
113bool
115{
116 const auto operation = dynamic_cast<const UndefControlConstantOperation *>(&other);
117 return operation && operation->numAlternatives() == numAlternatives();
118}
119
120std::string
122{
123 return util::strfmt("UndefCtlConstant[", numAlternatives(), "]");
124}
125
126std::unique_ptr<Operation>
128{
129 return std::make_unique<UndefControlConstantOperation>(numAlternatives());
130}
131
133
135 size_t nbits,
137 uint64_t default_alternative,
138 size_t nalternatives)
139 : UnaryOperation(BitType::Create(nbits), ControlType::Create(nalternatives)),
140 default_alternative_(default_alternative),
141 mapping_(mapping)
142{}
143
144bool
146{
147 auto op = dynamic_cast<const MatchOperation *>(&other);
148 return op && op->default_alternative_ == default_alternative_ && op->mapping_ == mapping_
149 && op->nbits() == nbits() && op->nalternatives() == nalternatives();
150}
151
152std::string
154{
155 size_t n = 0;
156 std::ostringstream oss;
157 constexpr size_t maxAlternatives = 3;
158 for (auto [input, subregionId] : mapping_)
159 {
160 if (n < maxAlternatives)
161 {
162 oss << input << " -> " << subregionId << ", ";
163 n++;
164 }
165 else
166 {
167 oss << "..., ";
168 break;
169 }
170 }
172
173 return "MATCH[" + oss.str() + "]";
174}
175
176std::unique_ptr<Operation>
178{
179 return std::make_unique<MatchOperation>(*this);
180}
181
182}
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:300
~MatchOperation() noexcept override
bool operator==(const Operation &other) const noexcept override
Definition control.cpp:145
std::unique_ptr< Operation > copy() const override
Definition control.cpp:177
std::string debug_string() const override
Definition control.cpp:153
Nullary operator (operator taking no formal arguments)
Definition nullary.hpp:22
Unary operator.
Definition unary.hpp:24
~UndefControlConstantOperation() noexcept override
std::unique_ptr< Operation > copy() const override
Definition control.cpp:127
size_t numAlternatives() const noexcept
Definition control.hpp:178
std::string debug_string() const override
Definition control.cpp:121
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