Jlm
Loading...
Searching...
No Matches
tac.hpp
Go to the documentation of this file.
1/*
2 * Copyright 2014 2015 Nico Reißmann <nico.reissmann@gmail.com>
3 * See COPYING for terms of redistribution.
4 */
5
6#ifndef JLM_LLVM_IR_TAC_HPP
7#define JLM_LLVM_IR_TAC_HPP
8
11#include <jlm/rvsdg/region.hpp>
12#include <jlm/util/common.hpp>
13
14#include <list>
15#include <memory>
16#include <optional>
17#include <vector>
18
19namespace jlm::llvm
20{
21
22class ThreeAddressCode;
23
25{
26public:
27 ~ThreeAddressCodeVariable() noexcept override;
28
31 std::shared_ptr<const jlm::rvsdg::Type> type,
32 const std::string & name)
33 : Variable(std::move(type), name),
34 tac_(tac)
35 {}
36
37 [[nodiscard]] llvm::ThreeAddressCode *
38 tac() const noexcept
39 {
40 return tac_;
41 }
42
43 static std::unique_ptr<ThreeAddressCodeVariable>
46 std::shared_ptr<const jlm::rvsdg::Type> type,
47 const std::string & name)
48 {
49 return std::make_unique<ThreeAddressCodeVariable>(tac, std::move(type), name);
50 }
51
52private:
54};
55
61
63{
64public:
65 ~ThreeAddressCode() noexcept = default;
66
68 std::unique_ptr<rvsdg::SimpleOperation> operation,
69 const std::vector<const Variable *> & operands);
70
72 std::unique_ptr<rvsdg::SimpleOperation> operation,
73 const std::vector<const Variable *> & operands,
74 const std::vector<std::string> & names);
75
77 std::unique_ptr<rvsdg::SimpleOperation> operation,
78 const std::vector<const Variable *> & operands,
79 std::vector<std::unique_ptr<ThreeAddressCodeVariable>> results);
80
82
84
86 operator=(const llvm::ThreeAddressCode &) = delete;
87
89 operator=(llvm::ThreeAddressCode &&) = delete;
90
91 inline const rvsdg::SimpleOperation &
92 operation() const noexcept
93 {
94 return *util::assertedCast<rvsdg::SimpleOperation>(operation_.get());
95 }
96
97 inline size_t
98 noperands() const noexcept
99 {
100 return operands_.size();
101 }
102
103 inline const Variable *
104 operand(size_t index) const noexcept
105 {
106 JLM_ASSERT(index < operands_.size());
107 return operands_[index];
108 }
109
110 inline size_t
111 nresults() const noexcept
112 {
113 return results_.size();
114 }
115
116 [[nodiscard]] const ThreeAddressCodeVariable *
117 result(size_t index) const noexcept
118 {
119 JLM_ASSERT(index < results_.size());
120 return results_[index].get();
121 }
122
123 /*
124 FIXME: I am really not happy with this function exposing
125 the results, but we need these results for the SSA destruction.
126 */
127 std::vector<std::unique_ptr<ThreeAddressCodeVariable>>
129 {
130 return std::move(results_);
131 }
132
133 void
134 replace(const rvsdg::SimpleOperation & operation, const std::vector<const Variable *> & operands);
135
136 void
137 convert(const rvsdg::SimpleOperation & operation, const std::vector<const Variable *> & operands);
138
139 void
141 {
142 rvsdgNodeLocation_ = { node.region()->getRegionId(), node.GetNodeId() };
143 }
144
145 [[nodiscard]] const std::optional<RvsdgNodeLocation> &
146 getRvsdgNodeLocation() const noexcept
147 {
148 return rvsdgNodeLocation_;
149 }
150
151 static std::string
152 ToAscii(const ThreeAddressCode & threeAddressCode);
153
154 static std::unique_ptr<llvm::ThreeAddressCode>
156 std::unique_ptr<rvsdg::SimpleOperation> operation,
157 const std::vector<const Variable *> & operands)
158 {
159 return std::make_unique<llvm::ThreeAddressCode>(std::move(operation), operands);
160 }
161
162 static std::unique_ptr<llvm::ThreeAddressCode>
164 std::unique_ptr<rvsdg::SimpleOperation> operation,
165 const std::vector<const Variable *> & operands,
166 const std::vector<std::string> & names)
167 {
168 return std::make_unique<llvm::ThreeAddressCode>(std::move(operation), operands, names);
169 }
170
171 static std::unique_ptr<llvm::ThreeAddressCode>
173 std::unique_ptr<rvsdg::SimpleOperation> operation,
174 const std::vector<const Variable *> & operands,
175 std::vector<std::unique_ptr<ThreeAddressCodeVariable>> results)
176 {
177 return std::make_unique<llvm::ThreeAddressCode>(
178 std::move(operation),
179 operands,
180 std::move(results));
181 }
182
183private:
184 void
185 create_results(const rvsdg::SimpleOperation & operation, const std::vector<std::string> & names)
186 {
187 JLM_ASSERT(names.size() == operation.nresults());
188
189 for (size_t n = 0; n < operation.nresults(); n++)
190 {
191 auto & type = operation.result(n);
192 results_.push_back(ThreeAddressCodeVariable::create(this, type, names[n]));
193 }
194 }
195
196 static std::vector<std::string>
197 create_names(size_t nnames)
198 {
199 static size_t c = 0;
200 std::vector<std::string> names;
201 for (size_t n = 0; n < nnames; n++)
202 names.push_back(jlm::util::strfmt("tv", c++));
203
204 return names;
205 }
206
207 std::optional<RvsdgNodeLocation> rvsdgNodeLocation_;
208 std::vector<const Variable *> operands_;
209 std::unique_ptr<rvsdg::Operation> operation_;
210 std::vector<std::unique_ptr<ThreeAddressCodeVariable>> results_;
211};
212
213template<class T>
214static inline bool
216{
217 return tac && is<T>(tac->operation());
218}
219
220/* FIXME: Replace all occurences of tacsvector_t with ThreeAddressCodeList
221 and then remove tacsvector_t.
222*/
223typedef std::vector<std::unique_ptr<llvm::ThreeAddressCode>> tacsvector_t;
224
226{
227public:
228 typedef std::list<ThreeAddressCode *>::const_iterator const_iterator;
229 typedef std::list<ThreeAddressCode *>::const_reverse_iterator const_reverse_iterator;
230
231 ~ThreeAddressCodeList() noexcept;
232
234
236
238 : tacs_(std::move(other.tacs_))
239 {}
240
243
246 {
247 if (this == &other)
248 return *this;
249
250 for (const auto & tac : tacs_)
251 delete tac;
252
253 tacs_.clear();
254 tacs_ = std::move(other.tacs_);
255
256 return *this;
257 }
258
259 inline const_iterator
260 begin() const noexcept
261 {
262 return tacs_.begin();
263 }
264
266 rbegin() const noexcept
267 {
268 return tacs_.rbegin();
269 }
270
271 inline const_iterator
272 end() const noexcept
273 {
274 return tacs_.end();
275 }
276
278 rend() const noexcept
279 {
280 return tacs_.rend();
281 }
282
283 inline ThreeAddressCode *
284 insert_before(const const_iterator & it, std::unique_ptr<llvm::ThreeAddressCode> tac)
285 {
286 return *tacs_.insert(it, tac.release());
287 }
288
289 inline void
291 {
292 tacs_.insert(it, tl.begin(), tl.end());
293 }
294
295 inline void
296 append_last(std::unique_ptr<llvm::ThreeAddressCode> tac)
297 {
298 tacs_.push_back(tac.release());
299 }
300
301 inline void
302 append_first(std::unique_ptr<llvm::ThreeAddressCode> tac)
303 {
304 tacs_.push_front(tac.release());
305 }
306
307 inline void
309 {
310 tacs_.insert(tacs_.begin(), tl.begin(), tl.end());
311 tl.tacs_.clear();
312 }
313
314 inline size_t
315 ntacs() const noexcept
316 {
317 return tacs_.size();
318 }
319
320 inline ThreeAddressCode *
321 first() const noexcept
322 {
323 return ntacs() != 0 ? tacs_.front() : nullptr;
324 }
325
326 inline ThreeAddressCode *
327 last() const noexcept
328 {
329 return ntacs() != 0 ? tacs_.back() : nullptr;
330 }
331
332 std::unique_ptr<ThreeAddressCode>
333 pop_first() noexcept
334 {
335 std::unique_ptr<ThreeAddressCode> tac(tacs_.front());
336 tacs_.pop_front();
337 return tac;
338 }
339
340 std::unique_ptr<ThreeAddressCode>
341 pop_last() noexcept
342 {
343 std::unique_ptr<ThreeAddressCode> tac(tacs_.back());
344 tacs_.pop_back();
345 return tac;
346 }
347
348 inline void
350 {
351 delete tacs_.front();
352 tacs_.pop_front();
353 }
354
355 inline void
357 {
358 delete tacs_.back();
359 tacs_.pop_back();
360 }
361
362private:
363 std::list<ThreeAddressCode *> tacs_;
364};
365
366}
367
368#endif
~ThreeAddressCodeList() noexcept
Definition tac.cpp:15
const_iterator end() const noexcept
Definition tac.hpp:272
const_iterator begin() const noexcept
Definition tac.hpp:260
std::list< ThreeAddressCode * >::const_iterator const_iterator
Definition tac.hpp:228
void append_first(std::unique_ptr< llvm::ThreeAddressCode > tac)
Definition tac.hpp:302
void insert_before(const const_iterator &it, ThreeAddressCodeList &tl)
Definition tac.hpp:290
const_reverse_iterator rbegin() const noexcept
Definition tac.hpp:266
std::list< ThreeAddressCode * >::const_reverse_iterator const_reverse_iterator
Definition tac.hpp:229
std::list< ThreeAddressCode * > tacs_
Definition tac.hpp:363
std::unique_ptr< ThreeAddressCode > pop_first() noexcept
Definition tac.hpp:333
ThreeAddressCode * insert_before(const const_iterator &it, std::unique_ptr< llvm::ThreeAddressCode > tac)
Definition tac.hpp:284
std::unique_ptr< ThreeAddressCode > pop_last() noexcept
Definition tac.hpp:341
ThreeAddressCodeList & operator=(ThreeAddressCodeList &&other) noexcept
Definition tac.hpp:245
void append_first(ThreeAddressCodeList &tl)
Definition tac.hpp:308
size_t ntacs() const noexcept
Definition tac.hpp:315
ThreeAddressCode * last() const noexcept
Definition tac.hpp:327
void append_last(std::unique_ptr< llvm::ThreeAddressCode > tac)
Definition tac.hpp:296
ThreeAddressCodeList & operator=(const ThreeAddressCodeList &)=delete
const_reverse_iterator rend() const noexcept
Definition tac.hpp:278
ThreeAddressCode * first() const noexcept
Definition tac.hpp:321
llvm::ThreeAddressCode * tac_
Definition tac.hpp:53
static std::unique_ptr< ThreeAddressCodeVariable > create(llvm::ThreeAddressCode *tac, std::shared_ptr< const jlm::rvsdg::Type > type, const std::string &name)
Definition tac.hpp:44
llvm::ThreeAddressCode * tac() const noexcept
Definition tac.hpp:38
~ThreeAddressCodeVariable() noexcept override
void create_results(const rvsdg::SimpleOperation &operation, const std::vector< std::string > &names)
Definition tac.hpp:185
const rvsdg::SimpleOperation & operation() const noexcept
Definition tac.hpp:92
std::unique_ptr< rvsdg::Operation > operation_
Definition tac.hpp:209
std::vector< const Variable * > operands_
Definition tac.hpp:208
void replace(const rvsdg::SimpleOperation &operation, const std::vector< const Variable * > &operands)
Definition tac.cpp:108
std::optional< RvsdgNodeLocation > rvsdgNodeLocation_
Definition tac.hpp:207
std::vector< std::unique_ptr< ThreeAddressCodeVariable > > results()
Definition tac.hpp:128
void convert(const rvsdg::SimpleOperation &operation, const std::vector< const Variable * > &operands)
Definition tac.cpp:93
size_t nresults() const noexcept
Definition tac.hpp:111
const std::optional< RvsdgNodeLocation > & getRvsdgNodeLocation() const noexcept
Definition tac.hpp:146
void setRvsdgNodeLocation(const rvsdg::Node &node)
Definition tac.hpp:140
std::vector< std::unique_ptr< ThreeAddressCodeVariable > > results_
Definition tac.hpp:210
static std::string ToAscii(const ThreeAddressCode &threeAddressCode)
Definition tac.cpp:120
const Variable * operand(size_t index) const noexcept
Definition tac.hpp:104
static std::unique_ptr< llvm::ThreeAddressCode > create(std::unique_ptr< rvsdg::SimpleOperation > operation, const std::vector< const Variable * > &operands, std::vector< std::unique_ptr< ThreeAddressCodeVariable > > results)
Definition tac.hpp:172
static std::vector< std::string > create_names(size_t nnames)
Definition tac.hpp:197
static std::unique_ptr< llvm::ThreeAddressCode > create(std::unique_ptr< rvsdg::SimpleOperation > operation, const std::vector< const Variable * > &operands)
Definition tac.hpp:155
~ThreeAddressCode() noexcept=default
const ThreeAddressCodeVariable * result(size_t index) const noexcept
Definition tac.hpp:117
size_t noperands() const noexcept
Definition tac.hpp:98
static std::unique_ptr< llvm::ThreeAddressCode > create(std::unique_ptr< rvsdg::SimpleOperation > operation, const std::vector< const Variable * > &operands, const std::vector< std::string > &names)
Definition tac.hpp:163
const std::string & name() const noexcept
Definition variable.hpp:50
const jlm::rvsdg::Type & type() const noexcept
Definition variable.hpp:56
const std::shared_ptr< const jlm::rvsdg::Type > Type() const noexcept
Definition variable.hpp:62
uint64_t Id
Definition node.hpp:583
Id GetNodeId() const noexcept
Definition node.hpp:600
rvsdg::Region * region() const noexcept
Definition node.hpp:761
Id getRegionId() const noexcept
Definition region.hpp:278
const std::shared_ptr< const rvsdg::Type > & result(size_t index) const noexcept
Definition operation.cpp:36
size_t nresults() const noexcept
Definition operation.cpp:30
#define JLM_ASSERT(x)
Definition common.hpp:16
Global memory state passed between functions.
std::vector< std::unique_ptr< llvm::ThreeAddressCode > > tacsvector_t
Definition tac.hpp:223
static bool is(const AggregationNode *node)
static std::string strfmt(Args... args)
Definition strfmt.hpp:35
rvsdg::Region::Id regionId
Definition tac.hpp:58
rvsdg::Node::Id nodeId
Definition tac.hpp:59