Require target lra in gcc.dg/pr108095.c
[official-gcc.git] / gcc / analyzer / supergraph.cc
blob31707e743a583d0e6db225af437738bfa2ae7aa6
1 /* "Supergraph" classes that combine CFGs and callgraph into one digraph.
2 Copyright (C) 2019-2023 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 #define INCLUDE_MEMORY
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tree.h"
26 #include "tm.h"
27 #include "toplev.h"
28 #include "hash-table.h"
29 #include "vec.h"
30 #include "ggc.h"
31 #include "basic-block.h"
32 #include "function.h"
33 #include "gimple.h"
34 #include "gimple-iterator.h"
35 #include "gimple-fold.h"
36 #include "tree-eh.h"
37 #include "gimple-expr.h"
38 #include "is-a.h"
39 #include "timevar.h"
40 #include "gimple-pretty-print.h"
41 #include "tree-pretty-print.h"
42 #include "graphviz.h"
43 #include "cgraph.h"
44 #include "tree-dfa.h"
45 #include "bitmap.h"
46 #include "cfganal.h"
47 #include "function.h"
48 #include "analyzer/analyzer.h"
49 #include "ordered-hash-map.h"
50 #include "options.h"
51 #include "cgraph.h"
52 #include "cfg.h"
53 #include "digraph.h"
54 #include "tree-cfg.h"
55 #include "analyzer/supergraph.h"
56 #include "analyzer/analyzer-logging.h"
58 #if ENABLE_ANALYZER
60 namespace ana {
62 /* Get the function of the ultimate alias target being called at EDGE,
63 if any. */
65 function *
66 get_ultimate_function_for_cgraph_edge (cgraph_edge *edge)
68 cgraph_node *ultimate_node = edge->callee->ultimate_alias_target ();
69 if (!ultimate_node)
70 return NULL;
71 return ultimate_node->get_fun ();
74 /* Get the cgraph_edge, but only if there's an underlying function body. */
76 cgraph_edge *
77 supergraph_call_edge (function *fun, const gimple *stmt)
79 const gcall *call = dyn_cast<const gcall *> (stmt);
80 if (!call)
81 return NULL;
82 cgraph_edge *edge
83 = cgraph_node::get (fun->decl)->get_edge (const_cast <gimple *> (stmt));
84 if (!edge)
85 return NULL;
86 if (!edge->callee)
87 return NULL; /* e.g. for a function pointer. */
88 if (!get_ultimate_function_for_cgraph_edge (edge))
89 return NULL;
90 return edge;
93 /* class saved_uids.
95 In order to ensure consistent results without relying on the ordering
96 of pointer values we assign a uid to each gimple stmt, globally unique
97 across all functions.
99 Normally, the stmt uids are a scratch space that each pass can freely
100 assign its own values to. However, in the case of LTO, the uids are
101 used to associate call stmts with callgraph edges between the WPA phase
102 (where the analyzer runs in LTO mode) and the LTRANS phase; if the
103 analyzer changes them in the WPA phase, it leads to errors when
104 streaming the code back in at LTRANS.
105 lto_prepare_function_for_streaming has code to renumber the stmt UIDs
106 when the code is streamed back out, but for some reason this isn't
107 called for clones.
109 Hence, as a workaround, this class has responsibility for tracking
110 the original uids and restoring them once the pass is complete
111 (in the supergraph dtor). */
113 /* Give STMT a globally unique uid, storing its original uid so it can
114 later be restored. */
116 void
117 saved_uids::make_uid_unique (gimple *stmt)
119 unsigned next_uid = m_old_stmt_uids.length ();
120 unsigned old_stmt_uid = stmt->uid;
121 stmt->uid = next_uid;
122 m_old_stmt_uids.safe_push
123 (std::pair<gimple *, unsigned> (stmt, old_stmt_uid));
126 /* Restore the saved uids of all stmts. */
128 void
129 saved_uids::restore_uids () const
131 unsigned i;
132 std::pair<gimple *, unsigned> *pair;
133 FOR_EACH_VEC_ELT (m_old_stmt_uids, i, pair)
134 pair->first->uid = pair->second;
137 /* supergraph's ctor. Walk the callgraph, building supernodes for each
138 CFG basic block, splitting the basic blocks at callsites. Join
139 together the supernodes with interprocedural and intraprocedural
140 superedges as appropriate.
141 Assign UIDs to the gimple stmts. */
143 supergraph::supergraph (logger *logger)
145 auto_timevar tv (TV_ANALYZER_SUPERGRAPH);
147 LOG_FUNC (logger);
149 /* First pass: make supernodes (and assign UIDs to the gimple stmts). */
151 /* Sort the cgraph_nodes? */
152 cgraph_node *node;
153 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
155 function *fun = node->get_fun ();
157 /* Ensure that EDGE_DFS_BACK is correct for every CFG edge in
158 the supergraph (by doing it per-function). */
159 auto_cfun sentinel (fun);
160 mark_dfs_back_edges ();
162 const int start_idx = m_nodes.length ();
164 basic_block bb;
165 FOR_ALL_BB_FN (bb, fun)
167 /* The initial supernode for the BB gets the phi nodes (if any). */
168 supernode *node_for_stmts = add_node (fun, bb, NULL, phi_nodes (bb));
169 m_bb_to_initial_node.put (bb, node_for_stmts);
170 for (gphi_iterator gpi = gsi_start_phis (bb); !gsi_end_p (gpi);
171 gsi_next (&gpi))
173 gimple *stmt = gsi_stmt (gpi);
174 m_stmt_to_node_t.put (stmt, node_for_stmts);
175 m_stmt_uids.make_uid_unique (stmt);
178 /* Append statements from BB to the current supernode, splitting
179 them into a new supernode at each call site; such call statements
180 appear in both supernodes (representing call and return). */
181 gimple_stmt_iterator gsi;
182 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
184 gimple *stmt = gsi_stmt (gsi);
185 node_for_stmts->m_stmts.safe_push (stmt);
186 m_stmt_to_node_t.put (stmt, node_for_stmts);
187 m_stmt_uids.make_uid_unique (stmt);
188 if (cgraph_edge *edge = supergraph_call_edge (fun, stmt))
190 m_cgraph_edge_to_caller_prev_node.put(edge, node_for_stmts);
191 node_for_stmts = add_node (fun, bb, as_a <gcall *> (stmt),
192 NULL);
193 m_cgraph_edge_to_caller_next_node.put (edge, node_for_stmts);
195 else
197 // maybe call is via a function pointer
198 if (gcall *call = dyn_cast<gcall *> (stmt))
200 cgraph_edge *edge
201 = cgraph_node::get (fun->decl)->get_edge (stmt);
202 if (!edge || !edge->callee)
204 supernode *old_node_for_stmts = node_for_stmts;
205 node_for_stmts = add_node (fun, bb, call, NULL);
207 superedge *sedge
208 = new callgraph_superedge (old_node_for_stmts,
209 node_for_stmts,
210 SUPEREDGE_INTRAPROCEDURAL_CALL,
211 NULL);
212 add_edge (sedge);
218 m_bb_to_final_node.put (bb, node_for_stmts);
221 const unsigned num_snodes = m_nodes.length () - start_idx;
222 m_function_to_num_snodes.put (fun, num_snodes);
224 if (logger)
226 const int end_idx = m_nodes.length () - 1;
227 logger->log ("SN: %i...%i: function %qD",
228 start_idx, end_idx, fun->decl);
233 /* Second pass: make superedges. */
235 /* Make superedges for CFG edges. */
236 for (bb_to_node_t::iterator iter = m_bb_to_final_node.begin ();
237 iter != m_bb_to_final_node.end ();
238 ++iter)
240 basic_block bb = (*iter).first;
241 supernode *src_supernode = (*iter).second;
243 ::edge cfg_edge;
244 int idx;
245 if (bb->succs)
246 FOR_EACH_VEC_ELT (*bb->succs, idx, cfg_edge)
248 basic_block dest_cfg_block = cfg_edge->dest;
249 supernode *dest_supernode
250 = *m_bb_to_initial_node.get (dest_cfg_block);
251 cfg_superedge *cfg_sedge
252 = add_cfg_edge (src_supernode, dest_supernode, cfg_edge);
253 m_cfg_edge_to_cfg_superedge.put (cfg_edge, cfg_sedge);
257 /* Make interprocedural superedges for calls. */
259 for (cgraph_edge_to_node_t::iterator iter
260 = m_cgraph_edge_to_caller_prev_node.begin ();
261 iter != m_cgraph_edge_to_caller_prev_node.end ();
262 ++iter)
264 cgraph_edge *edge = (*iter).first;
265 supernode *caller_prev_supernode = (*iter).second;
266 function* callee_fn = get_ultimate_function_for_cgraph_edge (edge);
267 if (!callee_fn || !callee_fn->cfg)
268 continue;
269 basic_block callee_cfg_block = ENTRY_BLOCK_PTR_FOR_FN (callee_fn);
270 supernode *callee_supernode
271 = *m_bb_to_initial_node.get (callee_cfg_block);
272 call_superedge *sedge
273 = add_call_superedge (caller_prev_supernode,
274 callee_supernode,
275 edge);
276 m_cgraph_edge_to_call_superedge.put (edge, sedge);
280 /* Make interprocedural superedges for returns. */
282 for (cgraph_edge_to_node_t::iterator iter
283 = m_cgraph_edge_to_caller_next_node.begin ();
284 iter != m_cgraph_edge_to_caller_next_node.end ();
285 ++iter)
287 cgraph_edge *edge = (*iter).first;
288 supernode *caller_next_supernode = (*iter).second;
289 function* callee_fn = get_ultimate_function_for_cgraph_edge (edge);
290 if (!callee_fn || !callee_fn->cfg)
291 continue;
292 basic_block callee_cfg_block = EXIT_BLOCK_PTR_FOR_FN (callee_fn);
293 supernode *callee_supernode
294 = *m_bb_to_initial_node.get (callee_cfg_block);
295 return_superedge *sedge
296 = add_return_superedge (callee_supernode,
297 caller_next_supernode,
298 edge);
299 m_cgraph_edge_to_return_superedge.put (edge, sedge);
303 /* Make intraprocedural superedges linking the two halves of a call. */
305 for (cgraph_edge_to_node_t::iterator iter
306 = m_cgraph_edge_to_caller_prev_node.begin ();
307 iter != m_cgraph_edge_to_caller_prev_node.end ();
308 ++iter)
310 cgraph_edge *edge = (*iter).first;
311 supernode *caller_prev_supernode = (*iter).second;
312 supernode *caller_next_supernode
313 = *m_cgraph_edge_to_caller_next_node.get (edge);
314 superedge *sedge
315 = new callgraph_superedge (caller_prev_supernode,
316 caller_next_supernode,
317 SUPEREDGE_INTRAPROCEDURAL_CALL,
318 edge);
319 add_edge (sedge);
320 m_cgraph_edge_to_intraproc_superedge.put (edge, sedge);
327 /* supergraph's dtor. Reset stmt uids. */
329 supergraph::~supergraph ()
331 m_stmt_uids.restore_uids ();
334 /* Dump this graph in .dot format to PP, using DUMP_ARGS.
335 Cluster the supernodes by function, then by BB from original CFG. */
337 void
338 supergraph::dump_dot_to_pp (pretty_printer *pp,
339 const dump_args_t &dump_args) const
341 graphviz_out gv (pp);
343 pp_string (pp, "digraph \"");
344 pp_write_text_to_stream (pp);
345 pp_string (pp, "supergraph");
346 pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/false);
347 pp_string (pp, "\" {\n");
348 gv.indent ();
350 gv.println ("overlap=false;");
351 gv.println ("compound=true;");
353 /* TODO: maybe (optionally) sub-subdivide by TU, for LTO; see also:
354 https://gcc-python-plugin.readthedocs.io/en/latest/_images/sample-supergraph.png
357 /* Break out the supernodes into clusters by function. */
359 cgraph_node *node;
360 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
362 function *fun = node->get_fun ();
363 const char *funcname = function_name (fun);
364 gv.println ("subgraph \"cluster_%s\" {",
365 funcname);
366 gv.indent ();
367 pp_printf (pp,
368 ("style=\"dashed\";"
369 " color=\"black\";"
370 " label=\"%s\";\n"),
371 funcname);
373 /* Break out the nodes into clusters by BB from original CFG. */
375 basic_block bb;
376 FOR_ALL_BB_FN (bb, fun)
378 if (dump_args.m_flags & SUPERGRAPH_DOT_SHOW_BBS)
380 gv.println ("subgraph \"cluster_%s_bb_%i\" {",
381 funcname, bb->index);
382 gv.indent ();
383 pp_printf (pp,
384 ("style=\"dashed\";"
385 " color=\"black\";"
386 " label=\"bb: %i\";\n"),
387 bb->index);
390 // TODO: maybe keep an index per-function/per-bb to speed this up???
391 int i;
392 supernode *n;
393 FOR_EACH_VEC_ELT (m_nodes, i, n)
394 if (n->m_fun == fun && n->m_bb == bb)
395 n->dump_dot (&gv, dump_args);
397 if (dump_args.m_flags & SUPERGRAPH_DOT_SHOW_BBS)
399 /* Terminate per-bb "subgraph" */
400 gv.outdent ();
401 gv.println ("}");
406 /* Add an invisible edge from ENTRY to EXIT, to improve the graph layout. */
407 pp_string (pp, "\t");
408 get_node_for_function_entry (fun)->dump_dot_id (pp);
409 pp_string (pp, ":s -> ");
410 get_node_for_function_exit (fun)->dump_dot_id (pp);
411 pp_string (pp, ":n [style=\"invis\",constraint=true];\n");
413 /* Terminate per-function "subgraph" */
414 gv.outdent ();
415 gv.println ("}");
419 /* Superedges. */
420 int i;
421 superedge *e;
422 FOR_EACH_VEC_ELT (m_edges, i, e)
423 e->dump_dot (&gv, dump_args);
425 /* Terminate "digraph" */
426 gv.outdent ();
427 gv.println ("}");
430 /* Dump this graph in .dot format to FP, using DUMP_ARGS. */
432 void
433 supergraph::dump_dot_to_file (FILE *fp, const dump_args_t &dump_args) const
435 pretty_printer *pp = global_dc->printer->clone ();
436 pp_show_color (pp) = 0;
437 /* %qE in logs for SSA_NAMEs should show the ssa names, rather than
438 trying to prettify things by showing the underlying var. */
439 pp_format_decoder (pp) = default_tree_printer;
441 pp->buffer->stream = fp;
442 dump_dot_to_pp (pp, dump_args);
443 pp_flush (pp);
444 delete pp;
447 /* Dump this graph in .dot format to PATH, using DUMP_ARGS. */
449 void
450 supergraph::dump_dot (const char *path, const dump_args_t &dump_args) const
452 FILE *fp = fopen (path, "w");
453 dump_dot_to_file (fp, dump_args);
454 fclose (fp);
457 /* Return a new json::object of the form
458 {"nodes" : [objs for snodes],
459 "edges" : [objs for sedges]}. */
461 json::object *
462 supergraph::to_json () const
464 json::object *sgraph_obj = new json::object ();
466 /* Nodes. */
468 json::array *nodes_arr = new json::array ();
469 unsigned i;
470 supernode *n;
471 FOR_EACH_VEC_ELT (m_nodes, i, n)
472 nodes_arr->append (n->to_json ());
473 sgraph_obj->set ("nodes", nodes_arr);
476 /* Edges. */
478 json::array *edges_arr = new json::array ();
479 unsigned i;
480 superedge *n;
481 FOR_EACH_VEC_ELT (m_edges, i, n)
482 edges_arr->append (n->to_json ());
483 sgraph_obj->set ("edges", edges_arr);
486 return sgraph_obj;
489 /* Create a supernode for BB within FUN and add it to this supergraph.
491 If RETURNING_CALL is non-NULL, the supernode represents the resumption
492 of the basic block after returning from that call.
494 If PHI_NODES is non-NULL, this is the initial supernode for the basic
495 block, and is responsible for any handling of the phi nodes. */
497 supernode *
498 supergraph::add_node (function *fun, basic_block bb, gcall *returning_call,
499 gimple_seq phi_nodes)
501 supernode *n = new supernode (fun, bb, returning_call, phi_nodes,
502 m_nodes.length ());
503 m_nodes.safe_push (n);
504 return n;
507 /* Create a new cfg_superedge from SRC to DEST for the underlying CFG edge E,
508 adding it to this supergraph.
510 If the edge is for a switch statement, create a switch_cfg_superedge
511 subclass. */
513 cfg_superedge *
514 supergraph::add_cfg_edge (supernode *src, supernode *dest, ::edge e)
516 /* Special-case switch edges. */
517 gimple *stmt = src->get_last_stmt ();
518 cfg_superedge *new_edge;
519 if (stmt && stmt->code == GIMPLE_SWITCH)
520 new_edge = new switch_cfg_superedge (src, dest, e);
521 else
522 new_edge = new cfg_superedge (src, dest, e);
523 add_edge (new_edge);
524 return new_edge;
527 /* Create and add a call_superedge representing an interprocedural call
528 from SRC to DEST, using CEDGE. */
530 call_superedge *
531 supergraph::add_call_superedge (supernode *src, supernode *dest,
532 cgraph_edge *cedge)
534 call_superedge *new_edge = new call_superedge (src, dest, cedge);
535 add_edge (new_edge);
536 return new_edge;
539 /* Create and add a return_superedge representing returning from an
540 interprocedural call, returning from SRC to DEST, using CEDGE. */
542 return_superedge *
543 supergraph::add_return_superedge (supernode *src, supernode *dest,
544 cgraph_edge *cedge)
546 return_superedge *new_edge = new return_superedge (src, dest, cedge);
547 add_edge (new_edge);
548 return new_edge;
551 /* Implementation of dnode::dump_dot vfunc for supernodes.
553 Write a cluster for the node, and within it a .dot node showing
554 the phi nodes and stmts. Call into any node annotator from ARGS to
555 potentially add other records to the cluster. */
557 void
558 supernode::dump_dot (graphviz_out *gv, const dump_args_t &args) const
560 gv->println ("subgraph cluster_node_%i {",
561 m_index);
562 gv->indent ();
564 gv->println("style=\"solid\";");
565 gv->println("color=\"black\";");
566 gv->println("fillcolor=\"lightgrey\";");
567 gv->println("label=\"sn: %i (bb: %i)\";", m_index, m_bb->index);
569 pretty_printer *pp = gv->get_pp ();
571 if (args.m_node_annotator)
572 args.m_node_annotator->add_node_annotations (gv, *this, false);
574 gv->write_indent ();
575 dump_dot_id (pp);
576 pp_printf (pp,
577 " [shape=none,margin=0,style=filled,fillcolor=%s,label=<",
578 "lightgrey");
579 pp_string (pp, "<TABLE BORDER=\"0\">");
580 pp_write_text_to_stream (pp);
582 bool had_row = false;
584 /* Give any annotator the chance to add its own per-node TR elements. */
585 if (args.m_node_annotator)
586 if (args.m_node_annotator->add_node_annotations (gv, *this, true))
587 had_row = true;
589 if (m_returning_call)
591 gv->begin_trtd ();
592 pp_string (pp, "returning call: ");
593 gv->end_tdtr ();
595 gv->begin_tr ();
596 gv->begin_td ();
597 pp_gimple_stmt_1 (pp, m_returning_call, 0, (dump_flags_t)0);
598 pp_write_text_as_html_like_dot_to_stream (pp);
599 gv->end_td ();
600 /* Give any annotator the chance to add per-stmt TD elements to
601 this row. */
602 if (args.m_node_annotator)
603 args.m_node_annotator->add_stmt_annotations (gv, m_returning_call,
604 true);
605 gv->end_tr ();
607 /* Give any annotator the chance to add per-stmt TR elements. */
608 if (args.m_node_annotator)
609 args.m_node_annotator->add_stmt_annotations (gv, m_returning_call,
610 false);
611 pp_newline (pp);
613 had_row = true;
616 if (entry_p ())
618 pp_string (pp, "<TR><TD>ENTRY</TD></TR>");
619 pp_newline (pp);
620 had_row = true;
623 if (return_p ())
625 pp_string (pp, "<TR><TD>EXIT</TD></TR>");
626 pp_newline (pp);
627 had_row = true;
630 /* Phi nodes. */
631 for (gphi_iterator gpi = const_cast<supernode *> (this)->start_phis ();
632 !gsi_end_p (gpi); gsi_next (&gpi))
634 const gimple *stmt = gsi_stmt (gpi);
635 gv->begin_tr ();
636 gv->begin_td ();
637 pp_gimple_stmt_1 (pp, stmt, 0, (dump_flags_t)0);
638 pp_write_text_as_html_like_dot_to_stream (pp);
639 gv->end_td ();
640 /* Give any annotator the chance to add per-phi TD elements to
641 this row. */
642 if (args.m_node_annotator)
643 args.m_node_annotator->add_stmt_annotations (gv, stmt, true);
644 gv->end_tr ();
646 /* Give any annotator the chance to add per-phi TR elements. */
647 if (args.m_node_annotator)
648 args.m_node_annotator->add_stmt_annotations (gv, stmt, false);
650 pp_newline (pp);
651 had_row = true;
654 /* Statements. */
655 int i;
656 gimple *stmt;
657 FOR_EACH_VEC_ELT (m_stmts, i, stmt)
659 gv->begin_tr ();
660 gv->begin_td ();
661 pp_gimple_stmt_1 (pp, stmt, 0, (dump_flags_t)0);
662 pp_write_text_as_html_like_dot_to_stream (pp);
663 gv->end_td ();
664 /* Give any annotator the chance to add per-stmt TD elements to
665 this row. */
666 if (args.m_node_annotator)
667 args.m_node_annotator->add_stmt_annotations (gv, stmt, true);
668 gv->end_tr ();
670 /* Give any annotator the chance to add per-stmt TR elements. */
671 if (args.m_node_annotator)
672 args.m_node_annotator->add_stmt_annotations (gv, stmt, false);
674 pp_newline (pp);
675 had_row = true;
678 /* Give any annotator the chance to add additional per-node TR elements
679 to the end of the TABLE. */
680 if (args.m_node_annotator)
681 if (args.m_node_annotator->add_after_node_annotations (gv, *this))
682 had_row = true;
684 /* Graphviz requires a TABLE element to have at least one TR
685 (and each TR to have at least one TD). */
686 if (!had_row)
688 pp_string (pp, "<TR><TD>(empty)</TD></TR>");
689 pp_newline (pp);
692 pp_string (pp, "</TABLE>>];\n\n");
693 pp_flush (pp);
695 /* Terminate "subgraph" */
696 gv->outdent ();
697 gv->println ("}");
700 /* Write an ID for this node to PP, for use in .dot output. */
702 void
703 supernode::dump_dot_id (pretty_printer *pp) const
705 pp_printf (pp, "node_%i", m_index);
708 /* Return a new json::object of the form
709 {"idx": int,
710 "fun": optional str
711 "bb_idx": int,
712 "returning_call": optional str,
713 "phis": [str],
714 "stmts" : [str]}. */
716 json::object *
717 supernode::to_json () const
719 json::object *snode_obj = new json::object ();
721 snode_obj->set ("idx", new json::integer_number (m_index));
722 snode_obj->set ("bb_idx", new json::integer_number (m_bb->index));
723 if (function *fun = get_function ())
724 snode_obj->set ("fun", new json::string (function_name (fun)));
726 if (m_returning_call)
728 pretty_printer pp;
729 pp_format_decoder (&pp) = default_tree_printer;
730 pp_gimple_stmt_1 (&pp, m_returning_call, 0, (dump_flags_t)0);
731 snode_obj->set ("returning_call",
732 new json::string (pp_formatted_text (&pp)));
735 /* Phi nodes. */
737 json::array *phi_arr = new json::array ();
738 for (gphi_iterator gpi = const_cast<supernode *> (this)->start_phis ();
739 !gsi_end_p (gpi); gsi_next (&gpi))
741 const gimple *stmt = gsi_stmt (gpi);
742 pretty_printer pp;
743 pp_format_decoder (&pp) = default_tree_printer;
744 pp_gimple_stmt_1 (&pp, stmt, 0, (dump_flags_t)0);
745 phi_arr->append (new json::string (pp_formatted_text (&pp)));
747 snode_obj->set ("phis", phi_arr);
750 /* Statements. */
752 json::array *stmt_arr = new json::array ();
753 int i;
754 gimple *stmt;
755 FOR_EACH_VEC_ELT (m_stmts, i, stmt)
757 pretty_printer pp;
758 pp_format_decoder (&pp) = default_tree_printer;
759 pp_gimple_stmt_1 (&pp, stmt, 0, (dump_flags_t)0);
760 stmt_arr->append (new json::string (pp_formatted_text (&pp)));
762 snode_obj->set ("stmts", stmt_arr);
765 return snode_obj;
768 /* Get a location_t for the start of this supernode. */
770 location_t
771 supernode::get_start_location () const
773 if (m_returning_call
774 && get_pure_location (m_returning_call->location) != UNKNOWN_LOCATION)
775 return m_returning_call->location;
777 int i;
778 gimple *stmt;
779 FOR_EACH_VEC_ELT (m_stmts, i, stmt)
780 if (get_pure_location (stmt->location) != UNKNOWN_LOCATION)
781 return stmt->location;
783 if (entry_p ())
785 // TWEAK: show the decl instead; this leads to more readable output:
786 return DECL_SOURCE_LOCATION (m_fun->decl);
788 return m_fun->function_start_locus;
790 if (return_p ())
791 return m_fun->function_end_locus;
793 return UNKNOWN_LOCATION;
796 /* Get a location_t for the end of this supernode. */
798 location_t
799 supernode::get_end_location () const
801 int i;
802 gimple *stmt;
803 FOR_EACH_VEC_ELT_REVERSE (m_stmts, i, stmt)
804 if (get_pure_location (stmt->location) != UNKNOWN_LOCATION)
805 return stmt->location;
807 if (m_returning_call
808 && get_pure_location (m_returning_call->location) != UNKNOWN_LOCATION)
809 return m_returning_call->location;
811 if (entry_p ())
812 return m_fun->function_start_locus;
813 if (return_p ())
814 return m_fun->function_end_locus;
816 return UNKNOWN_LOCATION;
819 /* Given STMT within this supernode, return its index within m_stmts. */
821 unsigned int
822 supernode::get_stmt_index (const gimple *stmt) const
824 unsigned i;
825 gimple *iter_stmt;
826 FOR_EACH_VEC_ELT (m_stmts, i, iter_stmt)
827 if (iter_stmt == stmt)
828 return i;
829 gcc_unreachable ();
832 /* Get any label_decl for this supernode, or NULL_TREE if there isn't one. */
834 tree
835 supernode::get_label () const
837 if (m_stmts.length () == 0)
838 return NULL_TREE;
839 const glabel *label_stmt = dyn_cast<const glabel *> (m_stmts[0]);
840 if (!label_stmt)
841 return NULL_TREE;
842 return gimple_label_label (label_stmt);
845 /* Get a string for PK. */
847 static const char *
848 edge_kind_to_string (enum edge_kind kind)
850 switch (kind)
852 default:
853 gcc_unreachable ();
854 case SUPEREDGE_CFG_EDGE:
855 return "SUPEREDGE_CFG_EDGE";
856 case SUPEREDGE_CALL:
857 return "SUPEREDGE_CALL";
858 case SUPEREDGE_RETURN:
859 return "SUPEREDGE_RETURN";
860 case SUPEREDGE_INTRAPROCEDURAL_CALL:
861 return "SUPEREDGE_INTRAPROCEDURAL_CALL";
865 /* Dump this superedge to PP. */
867 void
868 superedge::dump (pretty_printer *pp) const
870 pp_printf (pp, "edge: SN: %i -> SN: %i", m_src->m_index, m_dest->m_index);
871 label_text desc (get_description (false));
872 if (strlen (desc.get ()) > 0)
874 pp_space (pp);
875 pp_string (pp, desc.get ());
879 /* Dump this superedge to stderr. */
881 DEBUG_FUNCTION void
882 superedge::dump () const
884 pretty_printer pp;
885 pp_format_decoder (&pp) = default_tree_printer;
886 pp_show_color (&pp) = pp_show_color (global_dc->printer);
887 pp.buffer->stream = stderr;
888 dump (&pp);
889 pp_newline (&pp);
890 pp_flush (&pp);
893 /* Implementation of dedge::dump_dot for superedges.
894 Write a .dot edge to GV representing this superedge. */
896 void
897 superedge::dump_dot (graphviz_out *gv, const dump_args_t &) const
899 const char *style = "\"solid,bold\"";
900 const char *color = "black";
901 int weight = 10;
902 const char *constraint = "true";
904 switch (m_kind)
906 default:
907 gcc_unreachable ();
908 case SUPEREDGE_CFG_EDGE:
909 break;
910 case SUPEREDGE_CALL:
911 color = "red";
912 break;
913 case SUPEREDGE_RETURN:
914 color = "green";
915 break;
916 case SUPEREDGE_INTRAPROCEDURAL_CALL:
917 style = "\"dotted\"";
918 break;
921 /* Adapted from graph.cc:draw_cfg_node_succ_edges. */
922 if (::edge cfg_edge = get_any_cfg_edge ())
924 if (cfg_edge->flags & EDGE_FAKE)
926 style = "dotted";
927 color = "green";
928 weight = 0;
930 else if (cfg_edge->flags & EDGE_DFS_BACK)
932 style = "\"dotted,bold\"";
933 color = "blue";
934 weight = 10;
936 else if (cfg_edge->flags & EDGE_FALLTHRU)
938 color = "blue";
939 weight = 100;
942 if (cfg_edge->flags & EDGE_ABNORMAL)
943 color = "red";
946 gv->write_indent ();
948 pretty_printer *pp = gv->get_pp ();
950 m_src->dump_dot_id (pp);
951 pp_string (pp, " -> ");
952 m_dest->dump_dot_id (pp);
953 pp_printf (pp,
954 (" [style=%s, color=%s, weight=%d, constraint=%s,"
955 " ltail=\"cluster_node_%i\", lhead=\"cluster_node_%i\""
956 " headlabel=\""),
957 style, color, weight, constraint,
958 m_src->m_index, m_dest->m_index);
960 dump_label_to_pp (pp, false);
962 pp_printf (pp, "\"];\n");
965 /* Return a new json::object of the form
966 {"kind" : str,
967 "src_idx": int, the index of the source supernode,
968 "dst_idx": int, the index of the destination supernode,
969 "desc" : str. */
971 json::object *
972 superedge::to_json () const
974 json::object *sedge_obj = new json::object ();
975 sedge_obj->set ("kind", new json::string (edge_kind_to_string (m_kind)));
976 sedge_obj->set ("src_idx", new json::integer_number (m_src->m_index));
977 sedge_obj->set ("dst_idx", new json::integer_number (m_dest->m_index));
980 pretty_printer pp;
981 pp_format_decoder (&pp) = default_tree_printer;
982 dump_label_to_pp (&pp, false);
983 sedge_obj->set ("desc", new json::string (pp_formatted_text (&pp)));
986 return sedge_obj;
989 /* If this is an intraprocedural superedge, return the associated
990 CFG edge. Otherwise, return NULL. */
992 ::edge
993 superedge::get_any_cfg_edge () const
995 if (const cfg_superedge *sub = dyn_cast_cfg_superedge ())
996 return sub->get_cfg_edge ();
997 return NULL;
1000 /* If this is an interprocedural superedge, return the associated
1001 cgraph_edge *. Otherwise, return NULL. */
1003 cgraph_edge *
1004 superedge::get_any_callgraph_edge () const
1006 if (const callgraph_superedge *sub = dyn_cast_callgraph_superedge ())
1007 return sub->m_cedge;
1008 return NULL;
1011 /* Build a description of this superedge (e.g. "true" for the true
1012 edge of a conditional, or "case 42:" for a switch case).
1014 If USER_FACING is false, the result also contains any underlying
1015 CFG edge flags. e.g. " (flags FALLTHRU | DFS_BACK)". */
1017 label_text
1018 superedge::get_description (bool user_facing) const
1020 pretty_printer pp;
1021 dump_label_to_pp (&pp, user_facing);
1022 return label_text::take (xstrdup (pp_formatted_text (&pp)));
1025 /* Implementation of superedge::dump_label_to_pp for non-switch CFG
1026 superedges.
1028 For true/false edges, print "true" or "false" to PP.
1030 If USER_FACING is false, also print flags on the underlying CFG edge to
1031 PP. */
1033 void
1034 cfg_superedge::dump_label_to_pp (pretty_printer *pp,
1035 bool user_facing) const
1037 if (true_value_p ())
1038 pp_printf (pp, "true");
1039 else if (false_value_p ())
1040 pp_printf (pp, "false");
1042 if (user_facing)
1043 return;
1045 /* Express edge flags as a string with " | " separator.
1046 e.g. " (flags FALLTHRU | DFS_BACK)". */
1047 if (get_flags ())
1049 pp_string (pp, " (flags ");
1050 bool seen_flag = false;
1051 #define DEF_EDGE_FLAG(NAME,IDX) \
1052 do { \
1053 if (get_flags () & EDGE_##NAME) \
1055 if (seen_flag) \
1056 pp_string (pp, " | "); \
1057 pp_printf (pp, "%s", (#NAME)); \
1058 seen_flag = true; \
1060 } while (0);
1061 #include "cfg-flags.def"
1062 #undef DEF_EDGE_FLAG
1063 pp_string (pp, ")");
1066 /* Otherwise, no label. */
1069 /* Get the index number for this edge for use in phi stmts
1070 in its destination. */
1072 size_t
1073 cfg_superedge::get_phi_arg_idx () const
1075 return m_cfg_edge->dest_idx;
1078 /* Get the phi argument for PHI for this CFG edge. */
1080 tree
1081 cfg_superedge::get_phi_arg (const gphi *phi) const
1083 size_t index = get_phi_arg_idx ();
1084 return gimple_phi_arg_def (phi, index);
1087 switch_cfg_superedge::switch_cfg_superedge (supernode *src,
1088 supernode *dst,
1089 ::edge e)
1090 : cfg_superedge (src, dst, e)
1092 /* Populate m_case_labels with all cases which go to DST. */
1093 const gswitch *gswitch = get_switch_stmt ();
1094 for (unsigned i = 0; i < gimple_switch_num_labels (gswitch); i++)
1096 tree case_ = gimple_switch_label (gswitch, i);
1097 basic_block bb = label_to_block (src->get_function (),
1098 CASE_LABEL (case_));
1099 if (bb == dst->m_bb)
1100 m_case_labels.safe_push (case_);
1104 /* Implementation of superedge::dump_label_to_pp for CFG superedges for
1105 "switch" statements.
1107 Print "case VAL:", "case LOWER ... UPPER:", or "default:" to PP. */
1109 void
1110 switch_cfg_superedge::dump_label_to_pp (pretty_printer *pp,
1111 bool user_facing ATTRIBUTE_UNUSED) const
1113 if (user_facing)
1115 for (unsigned i = 0; i < m_case_labels.length (); ++i)
1117 if (i > 0)
1118 pp_string (pp, ", ");
1119 tree case_label = m_case_labels[i];
1120 gcc_assert (TREE_CODE (case_label) == CASE_LABEL_EXPR);
1121 tree lower_bound = CASE_LOW (case_label);
1122 tree upper_bound = CASE_HIGH (case_label);
1123 if (lower_bound)
1125 pp_printf (pp, "case ");
1126 dump_generic_node (pp, lower_bound, 0, (dump_flags_t)0, false);
1127 if (upper_bound)
1129 pp_printf (pp, " ... ");
1130 dump_generic_node (pp, upper_bound, 0, (dump_flags_t)0,
1131 false);
1133 pp_printf (pp, ":");
1135 else
1136 pp_printf (pp, "default:");
1139 else
1141 pp_character (pp, '{');
1142 for (unsigned i = 0; i < m_case_labels.length (); ++i)
1144 if (i > 0)
1145 pp_string (pp, ", ");
1146 tree case_label = m_case_labels[i];
1147 gcc_assert (TREE_CODE (case_label) == CASE_LABEL_EXPR);
1148 tree lower_bound = CASE_LOW (case_label);
1149 tree upper_bound = CASE_HIGH (case_label);
1150 if (lower_bound)
1152 if (upper_bound)
1154 pp_character (pp, '[');
1155 dump_generic_node (pp, lower_bound, 0, (dump_flags_t)0,
1156 false);
1157 pp_string (pp, ", ");
1158 dump_generic_node (pp, upper_bound, 0, (dump_flags_t)0,
1159 false);
1160 pp_character (pp, ']');
1162 else
1163 dump_generic_node (pp, lower_bound, 0, (dump_flags_t)0, false);
1165 else
1166 pp_printf (pp, "default");
1168 pp_character (pp, '}');
1169 if (implicitly_created_default_p ())
1171 pp_string (pp, " IMPLICITLY CREATED");
1176 /* Return true iff this edge is purely for an implicitly-created "default". */
1178 bool
1179 switch_cfg_superedge::implicitly_created_default_p () const
1181 if (m_case_labels.length () != 1)
1182 return false;
1184 tree case_label = m_case_labels[0];
1185 gcc_assert (TREE_CODE (case_label) == CASE_LABEL_EXPR);
1186 if (CASE_LOW (case_label))
1187 return false;
1189 /* We have a single "default" case.
1190 Assume that it was implicitly created if it has UNKNOWN_LOCATION. */
1191 return EXPR_LOCATION (case_label) == UNKNOWN_LOCATION;
1194 /* Implementation of superedge::dump_label_to_pp for interprocedural
1195 superedges. */
1197 void
1198 callgraph_superedge::dump_label_to_pp (pretty_printer *pp,
1199 bool user_facing ATTRIBUTE_UNUSED) const
1201 switch (m_kind)
1203 default:
1204 case SUPEREDGE_CFG_EDGE:
1205 gcc_unreachable ();
1207 case SUPEREDGE_CALL:
1208 pp_printf (pp, "call");
1209 break;
1211 case SUPEREDGE_RETURN:
1212 pp_printf (pp, "return");
1213 break;
1215 case SUPEREDGE_INTRAPROCEDURAL_CALL:
1216 pp_printf (pp, "intraproc link");
1217 break;
1221 /* Get the function that was called at this interprocedural call/return
1222 edge. */
1224 function *
1225 callgraph_superedge::get_callee_function () const
1227 return get_ultimate_function_for_cgraph_edge (m_cedge);
1230 /* Get the calling function at this interprocedural call/return edge. */
1232 function *
1233 callgraph_superedge::get_caller_function () const
1235 return m_cedge->caller->get_fun ();
1238 /* Get the fndecl that was called at this interprocedural call/return
1239 edge. */
1241 tree
1242 callgraph_superedge::get_callee_decl () const
1244 return get_callee_function ()->decl;
1247 /* Get the gcall * of this interprocedural call/return edge. */
1249 gcall *
1250 callgraph_superedge::get_call_stmt () const
1252 if (m_cedge)
1253 return m_cedge->call_stmt;
1255 return m_src->get_final_call ();
1258 /* Get the calling fndecl at this interprocedural call/return edge. */
1260 tree
1261 callgraph_superedge::get_caller_decl () const
1263 return get_caller_function ()->decl;
1266 /* Given PARM_TO_FIND, a PARM_DECL, identify its index (writing it
1267 to *OUT if OUT is non-NULL), and return the corresponding argument
1268 at the callsite. */
1270 tree
1271 callgraph_superedge::get_arg_for_parm (tree parm_to_find,
1272 callsite_expr *out) const
1274 gcc_assert (TREE_CODE (parm_to_find) == PARM_DECL);
1276 tree callee = get_callee_decl ();
1277 const gcall *call_stmt = get_call_stmt ();
1279 unsigned i = 0;
1280 for (tree iter_parm = DECL_ARGUMENTS (callee); iter_parm;
1281 iter_parm = DECL_CHAIN (iter_parm), ++i)
1283 if (i >= gimple_call_num_args (call_stmt))
1284 return NULL_TREE;
1285 if (iter_parm == parm_to_find)
1287 if (out)
1288 *out = callsite_expr::from_zero_based_param (i);
1289 return gimple_call_arg (call_stmt, i);
1293 /* Not found. */
1294 return NULL_TREE;
1297 /* Look for a use of ARG_TO_FIND as an argument at this callsite.
1298 If found, return the default SSA def of the corresponding parm within
1299 the callee, and if OUT is non-NULL, write the index to *OUT.
1300 Only the first match is handled. */
1302 tree
1303 callgraph_superedge::get_parm_for_arg (tree arg_to_find,
1304 callsite_expr *out) const
1306 tree callee = get_callee_decl ();
1307 const gcall *call_stmt = get_call_stmt ();
1309 unsigned i = 0;
1310 for (tree iter_parm = DECL_ARGUMENTS (callee); iter_parm;
1311 iter_parm = DECL_CHAIN (iter_parm), ++i)
1313 if (i >= gimple_call_num_args (call_stmt))
1314 return NULL_TREE;
1315 tree param = gimple_call_arg (call_stmt, i);
1316 if (arg_to_find == param)
1318 if (out)
1319 *out = callsite_expr::from_zero_based_param (i);
1320 return ssa_default_def (get_callee_function (), iter_parm);
1324 /* Not found. */
1325 return NULL_TREE;
1328 /* Map caller_expr back to an expr within the callee, or return NULL_TREE.
1329 If non-NULL is returned, populate OUT. */
1331 tree
1332 callgraph_superedge::map_expr_from_caller_to_callee (tree caller_expr,
1333 callsite_expr *out) const
1335 /* Is it an argument (actual param)? If so, convert to
1336 parameter (formal param). */
1337 tree parm = get_parm_for_arg (caller_expr, out);
1338 if (parm)
1339 return parm;
1340 /* Otherwise try return value. */
1341 if (caller_expr == gimple_call_lhs (get_call_stmt ()))
1343 if (out)
1344 *out = callsite_expr::from_return_value ();
1345 return DECL_RESULT (get_callee_decl ());
1348 return NULL_TREE;
1351 /* Map callee_expr back to an expr within the caller, or return NULL_TREE.
1352 If non-NULL is returned, populate OUT. */
1354 tree
1355 callgraph_superedge::map_expr_from_callee_to_caller (tree callee_expr,
1356 callsite_expr *out) const
1358 if (callee_expr == NULL_TREE)
1359 return NULL_TREE;
1361 /* If it's a parameter (formal param), get the argument (actual param). */
1362 if (TREE_CODE (callee_expr) == PARM_DECL)
1363 return get_arg_for_parm (callee_expr, out);
1365 /* Similar for the default SSA name of the PARM_DECL. */
1366 if (TREE_CODE (callee_expr) == SSA_NAME
1367 && SSA_NAME_IS_DEFAULT_DEF (callee_expr)
1368 && TREE_CODE (SSA_NAME_VAR (callee_expr)) == PARM_DECL)
1369 return get_arg_for_parm (SSA_NAME_VAR (callee_expr), out);
1371 /* Otherwise try return value. */
1372 if (callee_expr == DECL_RESULT (get_callee_decl ()))
1374 if (out)
1375 *out = callsite_expr::from_return_value ();
1376 return gimple_call_lhs (get_call_stmt ());
1379 return NULL_TREE;
1382 } // namespace ana
1384 #endif /* #if ENABLE_ANALYZER */