[Ada] Fix crash on declaration of overaligned array with constraints
[official-gcc.git] / gcc / analyzer / feasible-graph.cc
blobfe7e79fe90229b7c9dc122d3f1e38ace37f9e8f3
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)
10 any later version.
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/>. */
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"
32 #include "alloc-pool.h"
33 #include "fibonacci_heap.h"
34 #include "shortest-paths.h"
35 #include "sbitmap.h"
36 #include "bitmap.h"
37 #include "tristate.h"
38 #include "selftest.h"
39 #include "ordered-hash-map.h"
40 #include "json.h"
41 #include "analyzer/analyzer.h"
42 #include "analyzer/analyzer-logging.h"
43 #include "analyzer/sm.h"
44 #include "analyzer/pending-diagnostic.h"
45 #include "analyzer/diagnostic-manager.h"
46 #include "analyzer/call-string.h"
47 #include "analyzer/program-point.h"
48 #include "analyzer/store.h"
49 #include "analyzer/region-model.h"
50 #include "analyzer/constraint-manager.h"
51 #include "cfg.h"
52 #include "basic-block.h"
53 #include "gimple.h"
54 #include "gimple-iterator.h"
55 #include "cgraph.h"
56 #include "digraph.h"
57 #include "analyzer/supergraph.h"
58 #include "analyzer/program-state.h"
59 #include "analyzer/exploded-graph.h"
60 #include "analyzer/feasible-graph.h"
62 #if ENABLE_ANALYZER
64 namespace ana {
66 /* class base_feasible_node : public dnode<fg_traits>. */
68 /* Print an id to PP for this node suitable for use in .dot dumps. */
70 void
71 base_feasible_node::dump_dot_id (pretty_printer *pp) const
73 pp_printf (pp, "fnode_%i", m_index);
76 /* class feasible_node : public base_feasible_node. */
78 /* Implementation of dump_dot vfunc for feasible_node. */
80 void
81 feasible_node::dump_dot (graphviz_out *gv,
82 const dump_args_t &) const
84 pretty_printer *pp = gv->get_pp ();
86 dump_dot_id (pp);
87 pp_printf (pp, " [shape=none,margin=0,style=filled,fillcolor=%s,label=\"",
88 m_inner_node->get_dot_fillcolor ());
89 pp_write_text_to_stream (pp);
91 pp_printf (pp, "FN: %i (EN: %i); len=%i", m_index, m_inner_node->m_index,
92 m_path_length);
93 pp_newline (pp);
95 format f (true);
96 m_inner_node->get_point ().print (pp, f);
97 pp_newline (pp);
99 /* Show the model at this point along expansion of the feasible path,
100 rather than the model within the enode. */
101 m_state.get_model ().dump_to_pp (pp, true, true);
102 pp_newline (pp);
104 m_inner_node->dump_processed_stmts (pp);
105 m_inner_node->dump_saved_diagnostics (pp);
107 pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/true);
109 pp_string (pp, "\"];\n\n");
110 pp_flush (pp);
113 /* Implementation of dump_dot vfunc for infeasible_node.
114 In particular, show the rejected constraint. */
116 void
117 infeasible_node::dump_dot (graphviz_out *gv,
118 const dump_args_t &) const
120 pretty_printer *pp = gv->get_pp ();
122 dump_dot_id (pp);
123 pp_printf (pp, " [shape=none,margin=0,style=filled,fillcolor=%s,label=\"",
124 m_inner_node->get_dot_fillcolor ());
125 pp_write_text_to_stream (pp);
127 pp_printf (pp, "infeasible edge to EN: %i", m_inner_node->m_index);
128 pp_newline (pp);
130 pp_string (pp, "rejected constraint:");
131 pp_newline (pp);
132 m_rc->dump_to_pp (pp);
134 pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/true);
136 pp_string (pp, "\"];\n\n");
137 pp_flush (pp);
140 /* class base_feasible_edge : public dedge<fg_traits>. */
142 /* Implementation of dump_dot vfunc for base_easible_edge. */
144 void
145 base_feasible_edge::dump_dot (graphviz_out *gv, const dump_args_t &) const
147 pretty_printer *pp = gv->get_pp ();
149 m_src->dump_dot_id (pp);
150 pp_string (pp, " -> ");
151 m_dest->dump_dot_id (pp);
153 m_inner_edge->dump_dot_label (pp);
156 /* class feasible_graph : public digraph <fg_traits>. */
158 /* Ctor for feasible_graph. */
160 feasible_graph::feasible_graph ()
161 : m_num_infeasible (0)
165 /* Add a feasible_node to this graph for ENODE, STATE with the
166 given PATH_LENGTH. */
168 feasible_node *
169 feasible_graph::add_node (const exploded_node *enode,
170 const feasibility_state &state,
171 unsigned path_length)
173 /* We don't attempt get_or_create here. */
174 feasible_node *fnode = new feasible_node (enode, m_nodes.length (),
175 state, path_length);
176 digraph<fg_traits>::add_node (fnode);
177 return fnode;
180 /* Add an infeasible_node to this graph and an infeasible_edge connecting
181 to it from SRC_FNODE, capturing a failure of RC along EEDGE.
182 Takes ownership of RC. */
184 void
185 feasible_graph::add_feasibility_problem (feasible_node *src_fnode,
186 const exploded_edge *eedge,
187 rejected_constraint *rc)
189 infeasible_node *dst_fnode
190 = new infeasible_node (eedge->m_dest, m_nodes.length (), rc);
191 digraph<fg_traits>::add_node (dst_fnode);
192 add_edge (new infeasible_edge (src_fnode, dst_fnode, eedge));
193 m_num_infeasible++;
196 /* Make an exploded_path from the origin to FNODE's exploded_node,
197 following the edges in the feasible_graph. */
199 exploded_path *
200 feasible_graph::make_epath (feasible_node *fnode) const
202 exploded_path *epath = new exploded_path ();
204 /* FG is actually a tree. Built the path backwards, by walking
205 backwards from FNODE until we reach the origin. */
206 while (fnode->get_inner_node ()->m_index != 0)
208 gcc_assert (fnode->m_preds.length () == 1);
209 feasible_edge *pred_fedge
210 = static_cast <feasible_edge *> (fnode->m_preds[0]);
211 epath->m_edges.safe_push (pred_fedge->get_inner_edge ());
212 fnode = static_cast <feasible_node *> (pred_fedge->m_src);
215 /* Now reverse it. */
216 epath->m_edges.reverse ();
218 return epath;
221 /* Dump the path to DST_FNODE in textual form to PP. */
223 void
224 feasible_graph::dump_feasible_path (const feasible_node &dst_fnode,
225 pretty_printer *pp) const
227 const feasible_node *fnode = &dst_fnode;
229 auto_vec<const feasible_edge *> fpath;
231 /* FG is actually a tree. Built the path backwards, by walking
232 backwards from FNODE until we reach the origin. */
233 while (fnode->get_inner_node ()->m_index != 0)
235 gcc_assert (fnode->m_preds.length () == 1);
236 feasible_edge *pred_fedge
237 = static_cast <feasible_edge *> (fnode->m_preds[0]);
238 fpath.safe_push (pred_fedge);
239 fnode = static_cast <const feasible_node *> (pred_fedge->m_src);
242 /* Now reverse it. */
243 fpath.reverse ();
245 for (unsigned i = 0; i < fpath.length (); i++)
247 const feasible_edge *fedge = fpath[i];
248 const feasible_node *src_fnode
249 = static_cast <const feasible_node *> (fedge->m_src);
250 const feasible_node *dest_fnode
251 = static_cast <const feasible_node *> (fedge->m_dest);
253 pp_printf (pp, "fpath[%i]: FN %i (EN %i) -> FN %i (EN %i)",
255 src_fnode->get_index (),
256 src_fnode->get_inner_node ()->m_index,
257 dest_fnode->get_index (),
258 dest_fnode->get_inner_node ()->m_index);
259 pp_newline (pp);
260 pp_printf (pp, " FN %i (EN %i):",
261 dest_fnode->get_index (),
262 dest_fnode->get_inner_node ()->m_index);
263 pp_newline (pp);
264 const program_point &point = dest_fnode->get_inner_node ()->get_point ();
265 point.print (pp, format (true));
266 dest_fnode->get_state ().dump_to_pp (pp, true, true);
267 pp_newline (pp);
271 /* Dump the path to DST_FNODE in textual form to FILENAME. */
273 void
274 feasible_graph::dump_feasible_path (const feasible_node &dst_fnode,
275 const char *filename) const
277 FILE *fp = fopen (filename, "w");
278 pretty_printer pp;
279 pp_format_decoder (&pp) = default_tree_printer;
280 pp.buffer->stream = fp;
281 dump_feasible_path (dst_fnode, &pp);
282 pp_flush (&pp);
283 fclose (fp);
286 /* Dump stats about this graph to LOGGER. */
288 void
289 feasible_graph::log_stats (logger *logger) const
291 logger->log ("#nodes: %i", m_nodes.length ());
292 logger->log ("#edges: %i", m_edges.length ());
293 logger->log ("#feasible nodes: %i", m_nodes.length () - m_num_infeasible);
294 logger->log ("#feasible edges: %i", m_edges.length () - m_num_infeasible);
295 logger->log ("#infeasible nodes/edges: %i", m_num_infeasible);
298 } // namespace ana
300 #endif /* #if ENABLE_ANALYZER */