12#include "ir/indexed_vector.h"
13#include "ir/json_generator.h"
14#include "ir/namemap.h"
15#include "ir/nodemap.h"
16#include "ir/visitor.h"
17#include "lib/ordered_map.h"
21#define DEFINE_APPLY_FUNCTIONS(CLASS, TEMPLATE, TT, INLINE) \
22 TEMPLATE INLINE bool IR::CLASS TT::apply_visitor_preorder(Modifier &v) { \
23 Node::traceVisit("Mod pre"); \
24 return v.preorder(this); \
26 TEMPLATE INLINE void IR::CLASS TT::apply_visitor_postorder(Modifier &v) { \
27 Node::traceVisit("Mod post"); \
30 TEMPLATE INLINE void IR::CLASS TT::apply_visitor_revisit(Modifier &v, const Node *n) const { \
31 Node::traceVisit("Mod revisit"); \
34 TEMPLATE INLINE void IR::CLASS TT::apply_visitor_loop_revisit(Modifier &v) const { \
35 Node::traceVisit("Mod loop_revisit"); \
36 v.loop_revisit(this); \
38 TEMPLATE INLINE bool IR::CLASS TT::apply_visitor_preorder(Inspector &v) const { \
39 Node::traceVisit("Insp pre"); \
40 return v.preorder(this); \
42 TEMPLATE INLINE void IR::CLASS TT::apply_visitor_postorder(Inspector &v) const { \
43 Node::traceVisit("Insp post"); \
46 TEMPLATE INLINE void IR::CLASS TT::apply_visitor_revisit(Inspector &v) const { \
47 Node::traceVisit("Insp revisit"); \
50 TEMPLATE INLINE void IR::CLASS TT::apply_visitor_loop_revisit(Inspector &v) const { \
51 Node::traceVisit("Insp loop_revisit"); \
52 v.loop_revisit(this); \
54 TEMPLATE INLINE const IR::Node *IR::CLASS TT::apply_visitor_preorder(Transform &v) { \
55 Node::traceVisit("Trans pre"); \
56 return v.preorder(this); \
58 TEMPLATE INLINE const IR::Node *IR::CLASS TT::apply_visitor_postorder(Transform &v) { \
59 Node::traceVisit("Trans post"); \
60 return v.postorder(this); \
62 TEMPLATE INLINE void IR::CLASS TT::apply_visitor_revisit(Transform &v, const Node *n) const { \
63 Node::traceVisit("Trans revisit"); \
66 TEMPLATE INLINE void IR::CLASS TT::apply_visitor_loop_revisit(Transform &v) const { \
67 Node::traceVisit("Trans loop_revisit"); \
68 v.loop_revisit(this); \
71IRNODE_ALL_TEMPLATES(DEFINE_APPLY_FUNCTIONS,
inline)
74void IR::Vector<T>::visit_children(
Visitor &v, const
char *name) {
75 for (
auto i = vec.begin(); i != vec.end();) {
76 const IR::Node *n = v.apply_visitor(*i, name);
86 if (
auto l = n->to<Vector<T>>()) {
88 i = insert(i, l->vec.begin(), l->vec.end());
92 if (
const auto *v = n->to<VectorBase>()) {
96 i = insert(i, v->size() - 1, nullptr);
97 for (const auto *el : *v) {
99 if (auto e = el->template to<T>()) {
102 BUG(
"visitor returned invalid type %s for Vector<%s>", el->node_type_name(),
103 T::static_type_name());
109 if (
auto e = n->to<T>()) {
113 BUG(
"visitor returned invalid type %s for Vector<%s>", n->node_type_name(),
114 T::static_type_name());
118void IR::Vector<T>::visit_children(Visitor &v,
const char *name)
const {
119 for (
auto &a : vec) v.visit(a, name);
122void IR::Vector<T>::parallel_visit_children(Visitor &v,
const char *) {
123 SplitFlowVisitVector<T>(v, *
this).run_visit();
126void IR::Vector<T>::parallel_visit_children(Visitor &v,
const char *)
const {
127 SplitFlowVisitVector<T>(v, *
this).run_visit();
129IRNODE_DEFINE_APPLY_OVERLOAD(Vector, template <class T>, <T>)
131void IR::Vector<T>::toJSON(JSONGenerator &
json)
const {
133 json.emit_tag(
"vec");
134 auto state =
json.begin_vector();
135 for (
auto &k : vec)
json.emit(k);
136 json.end_vector(state);
139std::ostream &operator<<(std::ostream &out,
const IR::Vector<IR::Expression> &v);
140std::ostream &operator<<(std::ostream &out,
const IR::Vector<IR::Annotation> &v);
143void IR::IndexedVector<T>::visit_children(Visitor &v,
const char *name) {
144 for (
auto i = begin(); i != end();) {
145 auto n = v.apply_visitor(*i, name);
155 if (
auto l = n->template to<Vector<T>>()) {
157 i = insert(i, l->begin(), l->end());
158 i += l->Vector<T>::size();
161 if (
auto e = n->template to<T>()) {
165 BUG(
"visitor returned invalid type %s for IndexedVector<%s>", n->node_type_name(),
166 T::static_type_name());
170void IR::IndexedVector<T>::visit_children(Visitor &v,
const char *name)
const {
171 for (
auto &a : *
this) v.visit(a, name);
174void IR::IndexedVector<T>::toJSON(JSONGenerator &
json)
const {
175 Vector<T>::toJSON(
json);
176 json.emit_tag(
"declarations");
177 auto state =
json.begin_object();
178 for (
auto &k : declarations)
json.emit(k.first, k.second);
179 json.end_object(state);
181IRNODE_DEFINE_APPLY_OVERLOAD(IndexedVector, template <class T>, <T>)
184static inline
void namemap_insert_helper(typename MAP::
iterator, typename MAP::key_type k,
185 typename MAP::mapped_type v, MAP &, MAP &new_symbols) {
186 new_symbols.emplace(std::move(k), std::move(v));
189template <
class MAP,
class InputIterator>
190static inline void namemap_insert_helper(
typename MAP::iterator, InputIterator b, InputIterator e,
191 MAP &, MAP &new_symbols) {
192 new_symbols.insert(b, e);
196static inline void namemap_insert_helper(
typename ordered_map<cstring, T>::iterator it, cstring k,
197 T v, ordered_map<cstring, T> &symbols,
198 ordered_map<cstring, T> &) {
199 symbols.emplace_hint(it, std::move(k), std::move(v));
202template <
class T,
class InputIterator>
203static inline void namemap_insert_helper(
typename ordered_map<cstring, T>::iterator it,
204 InputIterator b, InputIterator e,
205 ordered_map<cstring, T> &symbols,
206 ordered_map<cstring, T> &) {
207 symbols.insert(it, b, e);
210template <
class T,
template <
class K,
class V,
class COMP,
class ALLOC>
class MAP ,
213void IR::NameMap<T, MAP, COMP, ALLOC>::visit_children(Visitor &v,
const char *) {
215 for (
auto i = symbols.begin(); i != symbols.end();) {
216 const IR::Node *n = v.apply_visitor(i->second, i->first.c_str());
217 if (!n && i->second) {
218 i = symbols.erase(i);
222 if (n == i->second) {
226 if (
auto m = n->to<NameMap>()) {
227 namemap_insert_helper(i, m->symbols.begin(), m->symbols.end(), symbols, new_symbols);
228 i = symbols.erase(i);
231 if (
auto s = n->to<T>()) {
232 if (match_name(i->first, s)) {
236 namemap_insert_helper(i, cstring(obj_name(s)), std::move(s), symbols, new_symbols);
237 i = symbols.erase(i);
241 BUG(
"visitor returned invalid type %s for NameMap<%s>", n->node_type_name(),
242 T::static_type_name());
244 symbols.insert(new_symbols.begin(), new_symbols.end());
246template <
class T,
template <
class K,
class V,
class COMP,
class ALLOC>
class MAP ,
249void IR::NameMap<T, MAP, COMP, ALLOC>::visit_children(Visitor &v,
const char *)
const {
250 for (
auto &k : symbols) {
251 v.visit(k.second, k.first.c_str());
254template <
class T,
template <
class K,
class V,
class COMP,
class ALLOC>
class MAP ,
257void IR::NameMap<T, MAP, COMP, ALLOC>::toJSON(JSONGenerator &
json)
const {
259 json.emit_tag(
"symbols");
260 auto state =
json.begin_object();
261 for (
auto &k : symbols)
json.emit(k.first, k.second);
262 json.end_object(state);
265template <
class KEY,
class VALUE,
266 template <
class K,
class V,
class COMP,
class ALLOC>
class MAP ,
269void IR::NodeMap<KEY, VALUE, MAP, COMP, ALLOC>::visit_children(Visitor &v,
const char *name) {
271 for (
auto i = symbols.begin(); i != symbols.end();) {
274 if (!nk && i->first) {
275 i = symbols.erase(i);
276 }
else if (nk == i->first) {
281 i = symbols.erase(i);
286 if (nv) new_symbols.emplace(nk, nv);
287 i = symbols.erase(i);
290 symbols.insert(new_symbols.begin(), new_symbols.end());
292template <
class KEY,
class VALUE,
293 template <
class K,
class V,
class COMP,
class ALLOC>
class MAP ,
296void IR::NodeMap<KEY, VALUE, MAP, COMP, ALLOC>::visit_children(Visitor &v,
const char *name)
const {
297 for (
auto &k : symbols) {
298 v.visit(k.first, name);
299 v.visit(k.second, name);
The namespace encapsulating IR node classes.
Definition constantParsing.h:13
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13