]>
Commit | Line | Data |
---|---|---|
3857edb5 | 1 | /* A graph for exploring trees of feasible paths through the egraph. |
7adcbafe | 2 | Copyright (C) 2021-2022 Free Software Foundation, Inc. |
3857edb5 DM |
3 | Contributed by David Malcolm <dmalcolm@redhat.com>. |
4 | ||
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GCC is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GCC; see the file COPYING3. If not see | |
19 | <http://www.gnu.org/licenses/>. */ | |
20 | ||
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
24 | #include "tree.h" | |
25 | #include "pretty-print.h" | |
26 | #include "gcc-rich-location.h" | |
27 | #include "gimple-pretty-print.h" | |
28 | #include "function.h" | |
29 | #include "diagnostic-core.h" | |
30 | #include "diagnostic-event-id.h" | |
31 | #include "diagnostic-path.h" | |
3857edb5 | 32 | #include "bitmap.h" |
3857edb5 | 33 | #include "ordered-hash-map.h" |
3857edb5 DM |
34 | #include "analyzer/analyzer.h" |
35 | #include "analyzer/analyzer-logging.h" | |
36 | #include "analyzer/sm.h" | |
37 | #include "analyzer/pending-diagnostic.h" | |
38 | #include "analyzer/diagnostic-manager.h" | |
39 | #include "analyzer/call-string.h" | |
40 | #include "analyzer/program-point.h" | |
41 | #include "analyzer/store.h" | |
42 | #include "analyzer/region-model.h" | |
43 | #include "analyzer/constraint-manager.h" | |
44 | #include "cfg.h" | |
45 | #include "basic-block.h" | |
46 | #include "gimple.h" | |
47 | #include "gimple-iterator.h" | |
48 | #include "cgraph.h" | |
49 | #include "digraph.h" | |
50 | #include "analyzer/supergraph.h" | |
51 | #include "analyzer/program-state.h" | |
52 | #include "analyzer/exploded-graph.h" | |
53 | #include "analyzer/feasible-graph.h" | |
54 | ||
55 | #if ENABLE_ANALYZER | |
56 | ||
57 | namespace ana { | |
58 | ||
59 | /* class base_feasible_node : public dnode<fg_traits>. */ | |
60 | ||
61 | /* Print an id to PP for this node suitable for use in .dot dumps. */ | |
62 | ||
63 | void | |
64 | base_feasible_node::dump_dot_id (pretty_printer *pp) const | |
65 | { | |
66 | pp_printf (pp, "fnode_%i", m_index); | |
67 | } | |
68 | ||
69 | /* class feasible_node : public base_feasible_node. */ | |
70 | ||
71 | /* Implementation of dump_dot vfunc for feasible_node. */ | |
72 | ||
73 | void | |
74 | feasible_node::dump_dot (graphviz_out *gv, | |
6e943d5a | 75 | const dump_args_t &) const |
3857edb5 DM |
76 | { |
77 | pretty_printer *pp = gv->get_pp (); | |
78 | ||
79 | dump_dot_id (pp); | |
80 | pp_printf (pp, " [shape=none,margin=0,style=filled,fillcolor=%s,label=\"", | |
81 | m_inner_node->get_dot_fillcolor ()); | |
82 | pp_write_text_to_stream (pp); | |
83 | ||
84 | pp_printf (pp, "FN: %i (EN: %i); len=%i", m_index, m_inner_node->m_index, | |
85 | m_path_length); | |
86 | pp_newline (pp); | |
87 | ||
88 | format f (true); | |
89 | m_inner_node->get_point ().print (pp, f); | |
90 | pp_newline (pp); | |
91 | ||
92 | /* Show the model at this point along expansion of the feasible path, | |
93 | rather than the model within the enode. */ | |
94 | m_state.get_model ().dump_to_pp (pp, true, true); | |
95 | pp_newline (pp); | |
96 | ||
97 | m_inner_node->dump_processed_stmts (pp); | |
6e943d5a | 98 | m_inner_node->dump_saved_diagnostics (pp); |
3857edb5 DM |
99 | |
100 | pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/true); | |
101 | ||
102 | pp_string (pp, "\"];\n\n"); | |
103 | pp_flush (pp); | |
104 | } | |
105 | ||
106 | /* Implementation of dump_dot vfunc for infeasible_node. | |
107 | In particular, show the rejected constraint. */ | |
108 | ||
109 | void | |
110 | infeasible_node::dump_dot (graphviz_out *gv, | |
111 | const dump_args_t &) const | |
112 | { | |
113 | pretty_printer *pp = gv->get_pp (); | |
114 | ||
115 | dump_dot_id (pp); | |
116 | pp_printf (pp, " [shape=none,margin=0,style=filled,fillcolor=%s,label=\"", | |
117 | m_inner_node->get_dot_fillcolor ()); | |
118 | pp_write_text_to_stream (pp); | |
119 | ||
120 | pp_printf (pp, "infeasible edge to EN: %i", m_inner_node->m_index); | |
121 | pp_newline (pp); | |
122 | ||
123 | pp_string (pp, "rejected constraint:"); | |
124 | pp_newline (pp); | |
8ca7fa84 | 125 | m_rc->dump_to_pp (pp); |
3857edb5 DM |
126 | |
127 | pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/true); | |
128 | ||
129 | pp_string (pp, "\"];\n\n"); | |
130 | pp_flush (pp); | |
131 | } | |
132 | ||
133 | /* class base_feasible_edge : public dedge<fg_traits>. */ | |
134 | ||
135 | /* Implementation of dump_dot vfunc for base_easible_edge. */ | |
136 | ||
137 | void | |
138 | base_feasible_edge::dump_dot (graphviz_out *gv, const dump_args_t &) const | |
139 | { | |
140 | pretty_printer *pp = gv->get_pp (); | |
141 | ||
142 | m_src->dump_dot_id (pp); | |
143 | pp_string (pp, " -> "); | |
144 | m_dest->dump_dot_id (pp); | |
145 | ||
146 | m_inner_edge->dump_dot_label (pp); | |
147 | } | |
148 | ||
149 | /* class feasible_graph : public digraph <fg_traits>. */ | |
150 | ||
151 | /* Ctor for feasible_graph. */ | |
152 | ||
153 | feasible_graph::feasible_graph () | |
154 | : m_num_infeasible (0) | |
155 | { | |
156 | } | |
157 | ||
158 | /* Add a feasible_node to this graph for ENODE, STATE with the | |
159 | given PATH_LENGTH. */ | |
160 | ||
161 | feasible_node * | |
162 | feasible_graph::add_node (const exploded_node *enode, | |
163 | const feasibility_state &state, | |
164 | unsigned path_length) | |
165 | { | |
166 | /* We don't attempt get_or_create here. */ | |
167 | feasible_node *fnode = new feasible_node (enode, m_nodes.length (), | |
168 | state, path_length); | |
169 | digraph<fg_traits>::add_node (fnode); | |
170 | return fnode; | |
171 | } | |
172 | ||
173 | /* Add an infeasible_node to this graph and an infeasible_edge connecting | |
8ca7fa84 DM |
174 | to it from SRC_FNODE, capturing a failure of RC along EEDGE. |
175 | Takes ownership of RC. */ | |
3857edb5 DM |
176 | |
177 | void | |
178 | feasible_graph::add_feasibility_problem (feasible_node *src_fnode, | |
179 | const exploded_edge *eedge, | |
8ca7fa84 | 180 | rejected_constraint *rc) |
3857edb5 DM |
181 | { |
182 | infeasible_node *dst_fnode | |
183 | = new infeasible_node (eedge->m_dest, m_nodes.length (), rc); | |
184 | digraph<fg_traits>::add_node (dst_fnode); | |
185 | add_edge (new infeasible_edge (src_fnode, dst_fnode, eedge)); | |
186 | m_num_infeasible++; | |
187 | } | |
188 | ||
189 | /* Make an exploded_path from the origin to FNODE's exploded_node, | |
190 | following the edges in the feasible_graph. */ | |
191 | ||
192 | exploded_path * | |
193 | feasible_graph::make_epath (feasible_node *fnode) const | |
194 | { | |
195 | exploded_path *epath = new exploded_path (); | |
196 | ||
197 | /* FG is actually a tree. Built the path backwards, by walking | |
198 | backwards from FNODE until we reach the origin. */ | |
199 | while (fnode->get_inner_node ()->m_index != 0) | |
200 | { | |
201 | gcc_assert (fnode->m_preds.length () == 1); | |
202 | feasible_edge *pred_fedge | |
203 | = static_cast <feasible_edge *> (fnode->m_preds[0]); | |
204 | epath->m_edges.safe_push (pred_fedge->get_inner_edge ()); | |
205 | fnode = static_cast <feasible_node *> (pred_fedge->m_src); | |
206 | } | |
207 | ||
208 | /* Now reverse it. */ | |
209 | epath->m_edges.reverse (); | |
210 | ||
211 | return epath; | |
212 | } | |
213 | ||
d8586b00 DM |
214 | /* Dump the path to DST_FNODE in textual form to PP. */ |
215 | ||
216 | void | |
217 | feasible_graph::dump_feasible_path (const feasible_node &dst_fnode, | |
218 | pretty_printer *pp) const | |
219 | { | |
220 | const feasible_node *fnode = &dst_fnode; | |
221 | ||
222 | auto_vec<const feasible_edge *> fpath; | |
223 | ||
224 | /* FG is actually a tree. Built the path backwards, by walking | |
225 | backwards from FNODE until we reach the origin. */ | |
226 | while (fnode->get_inner_node ()->m_index != 0) | |
227 | { | |
228 | gcc_assert (fnode->m_preds.length () == 1); | |
229 | feasible_edge *pred_fedge | |
230 | = static_cast <feasible_edge *> (fnode->m_preds[0]); | |
231 | fpath.safe_push (pred_fedge); | |
232 | fnode = static_cast <const feasible_node *> (pred_fedge->m_src); | |
233 | } | |
234 | ||
235 | /* Now reverse it. */ | |
236 | fpath.reverse (); | |
237 | ||
238 | for (unsigned i = 0; i < fpath.length (); i++) | |
239 | { | |
240 | const feasible_edge *fedge = fpath[i]; | |
241 | const feasible_node *src_fnode | |
242 | = static_cast <const feasible_node *> (fedge->m_src); | |
243 | const feasible_node *dest_fnode | |
244 | = static_cast <const feasible_node *> (fedge->m_dest); | |
245 | ||
246 | pp_printf (pp, "fpath[%i]: FN %i (EN %i) -> FN %i (EN %i)", | |
247 | i, | |
248 | src_fnode->get_index (), | |
249 | src_fnode->get_inner_node ()->m_index, | |
250 | dest_fnode->get_index (), | |
251 | dest_fnode->get_inner_node ()->m_index); | |
252 | pp_newline (pp); | |
253 | pp_printf (pp, " FN %i (EN %i):", | |
254 | dest_fnode->get_index (), | |
255 | dest_fnode->get_inner_node ()->m_index); | |
256 | pp_newline (pp); | |
257 | const program_point &point = dest_fnode->get_inner_node ()->get_point (); | |
258 | point.print (pp, format (true)); | |
259 | dest_fnode->get_state ().dump_to_pp (pp, true, true); | |
260 | pp_newline (pp); | |
261 | } | |
262 | } | |
263 | ||
264 | /* Dump the path to DST_FNODE in textual form to FILENAME. */ | |
265 | ||
266 | void | |
267 | feasible_graph::dump_feasible_path (const feasible_node &dst_fnode, | |
268 | const char *filename) const | |
269 | { | |
270 | FILE *fp = fopen (filename, "w"); | |
271 | pretty_printer pp; | |
272 | pp_format_decoder (&pp) = default_tree_printer; | |
273 | pp.buffer->stream = fp; | |
274 | dump_feasible_path (dst_fnode, &pp); | |
275 | pp_flush (&pp); | |
276 | fclose (fp); | |
277 | } | |
278 | ||
3857edb5 DM |
279 | /* Dump stats about this graph to LOGGER. */ |
280 | ||
281 | void | |
282 | feasible_graph::log_stats (logger *logger) const | |
283 | { | |
284 | logger->log ("#nodes: %i", m_nodes.length ()); | |
285 | logger->log ("#edges: %i", m_edges.length ()); | |
286 | logger->log ("#feasible nodes: %i", m_nodes.length () - m_num_infeasible); | |
287 | logger->log ("#feasible edges: %i", m_edges.length () - m_num_infeasible); | |
288 | logger->log ("#infeasible nodes/edges: %i", m_num_infeasible); | |
289 | } | |
290 | ||
291 | } // namespace ana | |
292 | ||
293 | #endif /* #if ENABLE_ANALYZER */ |