17#ifndef IR_IR_INLINE_H_
18#define IR_IR_INLINE_H_
21#include "ir/indexed_vector.h"
22#include "ir/json_generator.h"
23#include "ir/namemap.h"
24#include "ir/nodemap.h"
25#include "ir/visitor.h"
26#include "lib/ordered_map.h"
30#define DEFINE_APPLY_FUNCTIONS(CLASS, TEMPLATE, TT, INLINE) \
31 TEMPLATE INLINE bool IR::CLASS TT::apply_visitor_preorder(Modifier &v) { \
32 Node::traceVisit("Mod pre"); \
33 return v.preorder(this); \
35 TEMPLATE INLINE void IR::CLASS TT::apply_visitor_postorder(Modifier &v) { \
36 Node::traceVisit("Mod post"); \
39 TEMPLATE INLINE void IR::CLASS TT::apply_visitor_revisit(Modifier &v, const Node *n) const { \
40 Node::traceVisit("Mod revisit"); \
43 TEMPLATE INLINE void IR::CLASS TT::apply_visitor_loop_revisit(Modifier &v) const { \
44 Node::traceVisit("Mod loop_revisit"); \
45 v.loop_revisit(this); \
47 TEMPLATE INLINE bool IR::CLASS TT::apply_visitor_preorder(Inspector &v) const { \
48 Node::traceVisit("Insp pre"); \
49 return v.preorder(this); \
51 TEMPLATE INLINE void IR::CLASS TT::apply_visitor_postorder(Inspector &v) const { \
52 Node::traceVisit("Insp post"); \
55 TEMPLATE INLINE void IR::CLASS TT::apply_visitor_revisit(Inspector &v) const { \
56 Node::traceVisit("Insp revisit"); \
59 TEMPLATE INLINE void IR::CLASS TT::apply_visitor_loop_revisit(Inspector &v) const { \
60 Node::traceVisit("Insp loop_revisit"); \
61 v.loop_revisit(this); \
63 TEMPLATE INLINE const IR::Node *IR::CLASS TT::apply_visitor_preorder(Transform &v) { \
64 Node::traceVisit("Trans pre"); \
65 return v.preorder(this); \
67 TEMPLATE INLINE const IR::Node *IR::CLASS TT::apply_visitor_postorder(Transform &v) { \
68 Node::traceVisit("Trans post"); \
69 return v.postorder(this); \
71 TEMPLATE INLINE void IR::CLASS TT::apply_visitor_revisit(Transform &v, const Node *n) const { \
72 Node::traceVisit("Trans revisit"); \
75 TEMPLATE INLINE void IR::CLASS TT::apply_visitor_loop_revisit(Transform &v) const { \
76 Node::traceVisit("Trans loop_revisit"); \
77 v.loop_revisit(this); \
80IRNODE_ALL_TEMPLATES(DEFINE_APPLY_FUNCTIONS,
inline)
83void IR::Vector<T>::visit_children(
Visitor &v, const
char *name) {
84 for (
auto i = vec.begin(); i != vec.end();) {
85 const IR::Node *n = v.apply_visitor(*i, name);
95 if (
auto l = n->to<Vector<T>>()) {
97 i = insert(i, l->vec.begin(), l->vec.end());
101 if (
const auto *v = n->to<VectorBase>()) {
105 i = insert(i, v->size() - 1, nullptr);
106 for (const auto *el : *v) {
108 if (auto e = el->template to<T>()) {
111 BUG(
"visitor returned invalid type %s for Vector<%s>", el->node_type_name(),
112 T::static_type_name());
118 if (
auto e = n->to<T>()) {
122 BUG(
"visitor returned invalid type %s for Vector<%s>", n->node_type_name(),
123 T::static_type_name());
127void IR::Vector<T>::visit_children(Visitor &v,
const char *name)
const {
128 for (
auto &a : vec) v.visit(a, name);
131void IR::Vector<T>::parallel_visit_children(Visitor &v,
const char *) {
132 SplitFlowVisitVector<T>(v, *
this).run_visit();
135void IR::Vector<T>::parallel_visit_children(Visitor &v,
const char *)
const {
136 SplitFlowVisitVector<T>(v, *
this).run_visit();
138IRNODE_DEFINE_APPLY_OVERLOAD(Vector, template <class T>, <T>)
140void IR::Vector<T>::toJSON(JSONGenerator &json)
const {
142 json.emit_tag(
"vec");
143 auto state = json.begin_vector();
144 for (
auto &k : vec) json.emit(k);
145 json.end_vector(state);
148std::ostream &operator<<(std::ostream &out,
const IR::Vector<IR::Expression> &v);
149std::ostream &operator<<(std::ostream &out,
const IR::Vector<IR::Annotation> &v);
152void IR::IndexedVector<T>::visit_children(Visitor &v,
const char *name) {
153 for (
auto i = begin(); i != end();) {
154 auto n = v.apply_visitor(*i, name);
164 if (
auto l = n->template to<Vector<T>>()) {
166 i = insert(i, l->begin(), l->end());
167 i += l->Vector<T>::size();
170 if (
auto e = n->template to<T>()) {
174 BUG(
"visitor returned invalid type %s for IndexedVector<%s>", n->node_type_name(),
175 T::static_type_name());
179void IR::IndexedVector<T>::visit_children(Visitor &v,
const char *name)
const {
180 for (
auto &a : *
this) v.visit(a, name);
183void IR::IndexedVector<T>::toJSON(JSONGenerator &json)
const {
184 Vector<T>::toJSON(json);
185 json.emit_tag(
"declarations");
186 auto state = json.begin_object();
187 for (
auto &k : declarations) json.emit(k.first, k.second);
188 json.end_object(state);
190IRNODE_DEFINE_APPLY_OVERLOAD(IndexedVector, template <class T>, <T>)
193static inline
void namemap_insert_helper(typename MAP::iterator, typename MAP::key_type k,
194 typename MAP::mapped_type v, MAP &, MAP &new_symbols) {
195 new_symbols.emplace(std::move(k), std::move(v));
198template <
class MAP,
class InputIterator>
199static inline void namemap_insert_helper(
typename MAP::iterator, InputIterator b, InputIterator e,
200 MAP &, MAP &new_symbols) {
201 new_symbols.insert(b, e);
205static inline void namemap_insert_helper(
typename ordered_map<cstring, T>::iterator it, cstring k,
206 T v, ordered_map<cstring, T> &symbols,
207 ordered_map<cstring, T> &) {
208 symbols.emplace_hint(it, std::move(k), std::move(v));
211template <
class T,
class InputIterator>
212static inline void namemap_insert_helper(
typename ordered_map<cstring, T>::iterator it,
213 InputIterator b, InputIterator e,
214 ordered_map<cstring, T> &symbols,
215 ordered_map<cstring, T> &) {
216 symbols.insert(it, b, e);
219template <
class T,
template <
class K,
class V,
class COMP,
class ALLOC>
class MAP ,
222void IR::NameMap<T, MAP, COMP, ALLOC>::visit_children(Visitor &v,
const char *) {
224 for (
auto i = symbols.begin(); i != symbols.end();) {
225 const IR::Node *n = v.apply_visitor(i->second, i->first.c_str());
226 if (!n && i->second) {
227 i = symbols.erase(i);
231 if (n == i->second) {
235 if (
auto m = n->to<NameMap>()) {
236 namemap_insert_helper(i, m->symbols.begin(), m->symbols.end(), symbols, new_symbols);
237 i = symbols.erase(i);
240 if (
auto s = n->to<T>()) {
241 if (match_name(i->first, s)) {
245 namemap_insert_helper(i, cstring(obj_name(s)), std::move(s), symbols, new_symbols);
246 i = symbols.erase(i);
250 BUG(
"visitor returned invalid type %s for NameMap<%s>", n->node_type_name(),
251 T::static_type_name());
253 symbols.insert(new_symbols.begin(), new_symbols.end());
255template <
class T,
template <
class K,
class V,
class COMP,
class ALLOC>
class MAP ,
258void IR::NameMap<T, MAP, COMP, ALLOC>::visit_children(Visitor &v,
const char *)
const {
259 for (
auto &k : symbols) {
260 v.visit(k.second, k.first.c_str());
263template <
class T,
template <
class K,
class V,
class COMP,
class ALLOC>
class MAP ,
266void IR::NameMap<T, MAP, COMP, ALLOC>::toJSON(JSONGenerator &json)
const {
268 json.emit_tag(
"symbols");
269 auto state = json.begin_object();
270 for (
auto &k : symbols) json.emit(k.first, k.second);
271 json.end_object(state);
274template <
class KEY,
class VALUE,
275 template <
class K,
class V,
class COMP,
class ALLOC>
class MAP ,
278void IR::NodeMap<KEY, VALUE, MAP, COMP, ALLOC>::visit_children(Visitor &v,
const char *name) {
280 for (
auto i = symbols.begin(); i != symbols.end();) {
283 if (!nk && i->first) {
284 i = symbols.erase(i);
285 }
else if (nk == i->first) {
290 i = symbols.erase(i);
295 if (nv) new_symbols.emplace(nk, nv);
296 i = symbols.erase(i);
299 symbols.insert(new_symbols.begin(), new_symbols.end());
301template <
class KEY,
class VALUE,
302 template <
class K,
class V,
class COMP,
class ALLOC>
class MAP ,
305void IR::NodeMap<KEY, VALUE, MAP, COMP, ALLOC>::visit_children(Visitor &v,
const char *name)
const {
306 for (
auto &k : symbols) {
307 v.visit(k.first, name);
308 v.visit(k.second, name);
The namespace encapsulating IR node classes.
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24