Jlm
view.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2010 2011 2012 Helge Bahmann <hcb@chaoticmind.net>
3  * Copyright 2014 2015 Nico Reißmann <nico.reissmann@gmail.com>
4  * See COPYING for terms of redistribution.
5  */
6 
7 #include <jlm/rvsdg/gamma.hpp>
8 #include <jlm/rvsdg/theta.hpp>
10 #include <jlm/rvsdg/view.hpp>
11 
12 namespace jlm::rvsdg
13 {
14 
15 static std::string
17  const rvsdg::Region * region,
18  size_t depth,
19  std::unordered_map<const Output *, std::string> &);
20 
21 static std::string
22 indent(size_t depth)
23 {
24  return std::string(depth * 2, ' ');
25 }
26 
27 static std::string
29  const jlm::rvsdg::Output * port,
30  std::unordered_map<const Output *, std::string> & map)
31 {
32  std::string name = dynamic_cast<const rvsdg::RegionArgument *>(port) ? "a" : "o";
33  name += jlm::util::strfmt(map.size());
34  return name;
35 }
36 
37 static std::string
39  const Node * node,
40  size_t depth,
41  std::unordered_map<const Output *, std::string> & map)
42 {
43  std::string s(indent(depth));
44  for (size_t n = 0; n < node->noutputs(); n++)
45  {
46  auto name = create_port_name(node->output(n), map);
47  map[node->output(n)] = name;
48  s = s + name + " ";
49  }
50 
51  s += ":= " + node->DebugString() + " ";
52 
53  for (size_t n = 0; n < node->ninputs(); n++)
54  {
55  s += map[node->input(n)->origin()];
56  if (n <= node->ninputs() - 1)
57  s += " ";
58  }
59  s += "\n";
60 
61  if (auto snode = dynamic_cast<const rvsdg::StructuralNode *>(node))
62  {
63  for (size_t n = 0; n < snode->nsubregions(); n++)
64  s += region_to_string(snode->subregion(n), depth + 1, map);
65  }
66 
67  return s;
68 }
69 
70 static std::string
71 region_header(const rvsdg::Region * region, std::unordered_map<const Output *, std::string> & map)
72 {
73  std::string header("[");
74  for (size_t n = 0; n < region->narguments(); n++)
75  {
76  auto argument = region->argument(n);
77  auto pname = create_port_name(argument, map);
78  map[argument] = pname;
79 
80  header += pname;
81  if (argument->input())
82  header += jlm::util::strfmt(" <= ", map[argument->input()->origin()]);
83 
84  if (n < region->narguments() - 1)
85  header += ", ";
86  }
87  header += "]{";
88 
89  return header;
90 }
91 
92 static std::string
94  const Region * region,
95  const size_t depth,
96  std::unordered_map<const Output *, std::string> & map)
97 {
98  std::string body;
99  for (const auto node : TopDownConstTraverser(region))
100  {
101  body += node_to_string(node, depth, map);
102  }
103 
104  return body;
105 }
106 
107 static std::string
108 region_footer(const rvsdg::Region * region, std::unordered_map<const Output *, std::string> & map)
109 {
110  std::string footer("}[");
111  for (size_t n = 0; n < region->nresults(); n++)
112  {
113  auto result = region->result(n);
114  auto pname = map[result->origin()];
115 
116  if (result->output())
117  footer += map[result->output()] + " <= ";
118  footer += pname;
119 
120  if (n < region->nresults() - 1)
121  footer += ", ";
122  }
123  footer += "]";
124 
125  return footer;
126 }
127 
128 static std::string
130  const rvsdg::Region * region,
131  size_t depth,
132  std::unordered_map<const Output *, std::string> & map)
133 {
134  std::string s;
135  s = indent(depth) + region_header(region, map) + "\n";
136  s = s + region_body(region, depth + 1, map);
137  s = s + indent(depth) + region_footer(region, map) + "\n";
138  return s;
139 }
140 
141 std::string
142 view(const rvsdg::Region * region)
143 {
144  std::unordered_map<const Output *, std::string> map;
145  return view(region, map);
146 }
147 
148 std::string
149 view(const rvsdg::Region * region, std::unordered_map<const Output *, std::string> & map)
150 {
151  return region_to_string(region, 0, map);
152 }
153 
154 void
155 view(const rvsdg::Region * region, FILE * out)
156 {
157  fputs(view(region).c_str(), out);
158  fflush(out);
159 }
160 
161 }
Output * origin() const noexcept
Definition: node.hpp:58
virtual std::string DebugString() const =0
NodeInput * input(size_t index) const noexcept
Definition: node.hpp:615
NodeOutput * output(size_t index) const noexcept
Definition: node.hpp:650
size_t ninputs() const noexcept
Definition: node.hpp:609
size_t noutputs() const noexcept
Definition: node.hpp:644
Represents the argument of a region.
Definition: region.hpp:41
Represent acyclic RVSDG subgraphs.
Definition: region.hpp:213
RegionResult * result(size_t index) const noexcept
Definition: region.hpp:471
size_t nresults() const noexcept
Definition: region.hpp:465
RegionArgument * argument(size_t index) const noexcept
Definition: region.hpp:437
size_t narguments() const noexcept
Definition: region.hpp:431
static std::string region_header(const rvsdg::Region *region, std::unordered_map< const Output *, std::string > &map)
Definition: view.cpp:71
static std::string indent(size_t depth)
Definition: view.cpp:22
static std::string node_to_string(const Node *node, size_t depth, std::unordered_map< const Output *, std::string > &map)
Definition: view.cpp:38
static std::string region_footer(const rvsdg::Region *region, std::unordered_map< const Output *, std::string > &map)
Definition: view.cpp:108
static std::string region_to_string(const rvsdg::Region *region, size_t depth, std::unordered_map< const Output *, std::string > &)
Definition: view.cpp:129
detail::TopDownTraverserGeneric< true > TopDownConstTraverser
Traverser for visiting every node in a const region in a top down order.
Definition: traverser.hpp:314
std::string view(const rvsdg::Region *region)
Definition: view.cpp:142
static std::string region_body(const Region *region, const size_t depth, std::unordered_map< const Output *, std::string > &map)
Definition: view.cpp:93
size_t ninputs(const rvsdg::Region *region) noexcept
Definition: region.cpp:838
static std::string create_port_name(const jlm::rvsdg::Output *port, std::unordered_map< const Output *, std::string > &map)
Definition: view.cpp:28
static std::string strfmt(Args... args)
Definition: strfmt.hpp:35