1 /* A graph for exploring trees of feasible paths through the egraph.
2 Copyright (C) 2021-2022 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
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)
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.
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/>. */
22 #define INCLUDE_MEMORY
24 #include "coretypes.h"
26 #include "pretty-print.h"
27 #include "gcc-rich-location.h"
28 #include "gimple-pretty-print.h"
30 #include "diagnostic-core.h"
31 #include "diagnostic-event-id.h"
32 #include "diagnostic-path.h"
34 #include "ordered-hash-map.h"
35 #include "analyzer/analyzer.h"
36 #include "analyzer/analyzer-logging.h"
37 #include "analyzer/sm.h"
38 #include "analyzer/pending-diagnostic.h"
39 #include "analyzer/diagnostic-manager.h"
40 #include "analyzer/call-string.h"
41 #include "analyzer/program-point.h"
42 #include "analyzer/store.h"
43 #include "analyzer/region-model.h"
44 #include "analyzer/constraint-manager.h"
46 #include "basic-block.h"
48 #include "gimple-iterator.h"
51 #include "analyzer/supergraph.h"
52 #include "analyzer/program-state.h"
53 #include "analyzer/exploded-graph.h"
54 #include "analyzer/feasible-graph.h"
60 /* class base_feasible_node : public dnode<fg_traits>. */
62 /* Print an id to PP for this node suitable for use in .dot dumps. */
65 base_feasible_node::dump_dot_id (pretty_printer
*pp
) const
67 pp_printf (pp
, "fnode_%i", m_index
);
70 /* class feasible_node : public base_feasible_node. */
72 /* Implementation of dump_dot vfunc for feasible_node. */
75 feasible_node::dump_dot (graphviz_out
*gv
,
76 const dump_args_t
&) const
78 pretty_printer
*pp
= gv
->get_pp ();
81 pp_printf (pp
, " [shape=none,margin=0,style=filled,fillcolor=%s,label=\"",
82 m_inner_node
->get_dot_fillcolor ());
83 pp_write_text_to_stream (pp
);
85 pp_printf (pp
, "FN: %i (EN: %i); len=%i", m_index
, m_inner_node
->m_index
,
90 m_inner_node
->get_point ().print (pp
, f
);
93 /* Show the model at this point along expansion of the feasible path,
94 rather than the model within the enode. */
95 m_state
.get_model ().dump_to_pp (pp
, true, true);
98 m_inner_node
->dump_processed_stmts (pp
);
99 m_inner_node
->dump_saved_diagnostics (pp
);
101 pp_write_text_as_dot_label_to_stream (pp
, /*for_record=*/true);
103 pp_string (pp
, "\"];\n\n");
107 /* Implementation of dump_dot vfunc for infeasible_node.
108 In particular, show the rejected constraint. */
111 infeasible_node::dump_dot (graphviz_out
*gv
,
112 const dump_args_t
&) const
114 pretty_printer
*pp
= gv
->get_pp ();
117 pp_printf (pp
, " [shape=none,margin=0,style=filled,fillcolor=%s,label=\"",
118 m_inner_node
->get_dot_fillcolor ());
119 pp_write_text_to_stream (pp
);
121 pp_printf (pp
, "infeasible edge to EN: %i", m_inner_node
->m_index
);
124 pp_string (pp
, "rejected constraint:");
126 m_rc
->dump_to_pp (pp
);
128 pp_write_text_as_dot_label_to_stream (pp
, /*for_record=*/true);
130 pp_string (pp
, "\"];\n\n");
134 /* class base_feasible_edge : public dedge<fg_traits>. */
136 /* Implementation of dump_dot vfunc for base_easible_edge. */
139 base_feasible_edge::dump_dot (graphviz_out
*gv
, const dump_args_t
&) const
141 pretty_printer
*pp
= gv
->get_pp ();
143 m_src
->dump_dot_id (pp
);
144 pp_string (pp
, " -> ");
145 m_dest
->dump_dot_id (pp
);
147 m_inner_edge
->dump_dot_label (pp
);
150 /* class feasible_graph : public digraph <fg_traits>. */
152 /* Ctor for feasible_graph. */
154 feasible_graph::feasible_graph ()
155 : m_num_infeasible (0)
159 /* Add a feasible_node to this graph for ENODE, STATE with the
160 given PATH_LENGTH. */
163 feasible_graph::add_node (const exploded_node
*enode
,
164 const feasibility_state
&state
,
165 unsigned path_length
)
167 /* We don't attempt get_or_create here. */
168 feasible_node
*fnode
= new feasible_node (enode
, m_nodes
.length (),
170 digraph
<fg_traits
>::add_node (fnode
);
174 /* Add an infeasible_node to this graph and an infeasible_edge connecting
175 to it from SRC_FNODE, capturing a failure of RC along EEDGE.
176 Takes ownership of RC. */
179 feasible_graph::add_feasibility_problem (feasible_node
*src_fnode
,
180 const exploded_edge
*eedge
,
181 rejected_constraint
*rc
)
183 infeasible_node
*dst_fnode
184 = new infeasible_node (eedge
->m_dest
, m_nodes
.length (), rc
);
185 digraph
<fg_traits
>::add_node (dst_fnode
);
186 add_edge (new infeasible_edge (src_fnode
, dst_fnode
, eedge
));
190 /* Make an exploded_path from the origin to FNODE's exploded_node,
191 following the edges in the feasible_graph. */
193 std::unique_ptr
<exploded_path
>
194 feasible_graph::make_epath (feasible_node
*fnode
) const
196 std::unique_ptr
<exploded_path
> epath (new exploded_path ());
198 /* FG is actually a tree. Built the path backwards, by walking
199 backwards from FNODE until we reach the origin. */
200 while (fnode
->get_inner_node ()->m_index
!= 0)
202 gcc_assert (fnode
->m_preds
.length () == 1);
203 feasible_edge
*pred_fedge
204 = static_cast <feasible_edge
*> (fnode
->m_preds
[0]);
205 epath
->m_edges
.safe_push (pred_fedge
->get_inner_edge ());
206 fnode
= static_cast <feasible_node
*> (pred_fedge
->m_src
);
209 /* Now reverse it. */
210 epath
->m_edges
.reverse ();
215 /* Dump the path to DST_FNODE in textual form to PP. */
218 feasible_graph::dump_feasible_path (const feasible_node
&dst_fnode
,
219 pretty_printer
*pp
) const
221 const feasible_node
*fnode
= &dst_fnode
;
223 auto_vec
<const feasible_edge
*> fpath
;
225 /* FG is actually a tree. Built the path backwards, by walking
226 backwards from FNODE until we reach the origin. */
227 while (fnode
->get_inner_node ()->m_index
!= 0)
229 gcc_assert (fnode
->m_preds
.length () == 1);
230 feasible_edge
*pred_fedge
231 = static_cast <feasible_edge
*> (fnode
->m_preds
[0]);
232 fpath
.safe_push (pred_fedge
);
233 fnode
= static_cast <const feasible_node
*> (pred_fedge
->m_src
);
236 /* Now reverse it. */
239 for (unsigned i
= 0; i
< fpath
.length (); i
++)
241 const feasible_edge
*fedge
= fpath
[i
];
242 const feasible_node
*src_fnode
243 = static_cast <const feasible_node
*> (fedge
->m_src
);
244 const feasible_node
*dest_fnode
245 = static_cast <const feasible_node
*> (fedge
->m_dest
);
247 pp_printf (pp
, "fpath[%i]: FN %i (EN %i) -> FN %i (EN %i)",
249 src_fnode
->get_index (),
250 src_fnode
->get_inner_node ()->m_index
,
251 dest_fnode
->get_index (),
252 dest_fnode
->get_inner_node ()->m_index
);
254 pp_printf (pp
, " FN %i (EN %i):",
255 dest_fnode
->get_index (),
256 dest_fnode
->get_inner_node ()->m_index
);
258 const program_point
&point
= dest_fnode
->get_inner_node ()->get_point ();
259 point
.print (pp
, format (true));
260 dest_fnode
->get_state ().dump_to_pp (pp
, true, true);
265 /* Dump the path to DST_FNODE in textual form to FILENAME. */
268 feasible_graph::dump_feasible_path (const feasible_node
&dst_fnode
,
269 const char *filename
) const
271 FILE *fp
= fopen (filename
, "w");
273 pp_format_decoder (&pp
) = default_tree_printer
;
274 pp
.buffer
->stream
= fp
;
275 dump_feasible_path (dst_fnode
, &pp
);
280 /* Dump stats about this graph to LOGGER. */
283 feasible_graph::log_stats (logger
*logger
) const
285 logger
->log ("#nodes: %i", m_nodes
.length ());
286 logger
->log ("#edges: %i", m_edges
.length ());
287 logger
->log ("#feasible nodes: %i", m_nodes
.length () - m_num_infeasible
);
288 logger
->log ("#feasible edges: %i", m_edges
.length () - m_num_infeasible
);
289 logger
->log ("#infeasible nodes/edges: %i", m_num_infeasible
);
294 #endif /* #if ENABLE_ANALYZER */