libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / analyzer / supergraph.cc
blob55bd42a89a1add9af39ea5ddd1db3035c375ccf2
1 /* "Supergraph" classes that combine CFGs and callgraph into one digraph.
2 Copyright (C) 2019-2024 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 /* Discard debug stmts here, so we don't have to check for
186 them anywhere within the analyzer. */
187 if (is_gimple_debug (stmt))
188 continue;
189 node_for_stmts->m_stmts.safe_push (stmt);
190 m_stmt_to_node_t.put (stmt, node_for_stmts);
191 m_stmt_uids.make_uid_unique (stmt);
192 if (cgraph_edge *edge = supergraph_call_edge (fun, stmt))
194 m_cgraph_edge_to_caller_prev_node.put(edge, node_for_stmts);
195 node_for_stmts = add_node (fun, bb, as_a <gcall *> (stmt),
196 NULL);
197 m_cgraph_edge_to_caller_next_node.put (edge, node_for_stmts);
199 else
201 // maybe call is via a function pointer
202 if (gcall *call = dyn_cast<gcall *> (stmt))
204 cgraph_edge *edge
205 = cgraph_node::get (fun->decl)->get_edge (stmt);
206 if (!edge || !edge->callee)
208 supernode *old_node_for_stmts = node_for_stmts;
209 node_for_stmts = add_node (fun, bb, call, NULL);
211 superedge *sedge
212 = new callgraph_superedge (old_node_for_stmts,
213 node_for_stmts,
214 SUPEREDGE_INTRAPROCEDURAL_CALL,
215 NULL);
216 add_edge (sedge);
222 m_bb_to_final_node.put (bb, node_for_stmts);
225 const unsigned num_snodes = m_nodes.length () - start_idx;
226 m_function_to_num_snodes.put (fun, num_snodes);
228 if (logger)
230 const int end_idx = m_nodes.length () - 1;
231 logger->log ("SN: %i...%i: function %qD",
232 start_idx, end_idx, fun->decl);
237 /* Second pass: make superedges. */
239 /* Make superedges for CFG edges. */
240 for (bb_to_node_t::iterator iter = m_bb_to_final_node.begin ();
241 iter != m_bb_to_final_node.end ();
242 ++iter)
244 basic_block bb = (*iter).first;
245 supernode *src_supernode = (*iter).second;
247 ::edge cfg_edge;
248 int idx;
249 if (bb->succs)
250 FOR_EACH_VEC_ELT (*bb->succs, idx, cfg_edge)
252 basic_block dest_cfg_block = cfg_edge->dest;
253 supernode *dest_supernode
254 = *m_bb_to_initial_node.get (dest_cfg_block);
255 cfg_superedge *cfg_sedge
256 = add_cfg_edge (src_supernode, dest_supernode, cfg_edge);
257 m_cfg_edge_to_cfg_superedge.put (cfg_edge, cfg_sedge);
261 /* Make interprocedural superedges for calls. */
263 for (cgraph_edge_to_node_t::iterator iter
264 = m_cgraph_edge_to_caller_prev_node.begin ();
265 iter != m_cgraph_edge_to_caller_prev_node.end ();
266 ++iter)
268 cgraph_edge *edge = (*iter).first;
269 supernode *caller_prev_supernode = (*iter).second;
270 function* callee_fn = get_ultimate_function_for_cgraph_edge (edge);
271 if (!callee_fn || !callee_fn->cfg)
272 continue;
273 basic_block callee_cfg_block = ENTRY_BLOCK_PTR_FOR_FN (callee_fn);
274 supernode *callee_supernode
275 = *m_bb_to_initial_node.get (callee_cfg_block);
276 call_superedge *sedge
277 = add_call_superedge (caller_prev_supernode,
278 callee_supernode,
279 edge);
280 m_cgraph_edge_to_call_superedge.put (edge, sedge);
284 /* Make interprocedural superedges for returns. */
286 for (cgraph_edge_to_node_t::iterator iter
287 = m_cgraph_edge_to_caller_next_node.begin ();
288 iter != m_cgraph_edge_to_caller_next_node.end ();
289 ++iter)
291 cgraph_edge *edge = (*iter).first;
292 supernode *caller_next_supernode = (*iter).second;
293 function* callee_fn = get_ultimate_function_for_cgraph_edge (edge);
294 if (!callee_fn || !callee_fn->cfg)
295 continue;
296 basic_block callee_cfg_block = EXIT_BLOCK_PTR_FOR_FN (callee_fn);
297 supernode *callee_supernode
298 = *m_bb_to_initial_node.get (callee_cfg_block);
299 return_superedge *sedge
300 = add_return_superedge (callee_supernode,
301 caller_next_supernode,
302 edge);
303 m_cgraph_edge_to_return_superedge.put (edge, sedge);
307 /* Make intraprocedural superedges linking the two halves of a call. */
309 for (cgraph_edge_to_node_t::iterator iter
310 = m_cgraph_edge_to_caller_prev_node.begin ();
311 iter != m_cgraph_edge_to_caller_prev_node.end ();
312 ++iter)
314 cgraph_edge *edge = (*iter).first;
315 supernode *caller_prev_supernode = (*iter).second;
316 supernode *caller_next_supernode
317 = *m_cgraph_edge_to_caller_next_node.get (edge);
318 superedge *sedge
319 = new callgraph_superedge (caller_prev_supernode,
320 caller_next_supernode,
321 SUPEREDGE_INTRAPROCEDURAL_CALL,
322 edge);
323 add_edge (sedge);
324 m_cgraph_edge_to_intraproc_superedge.put (edge, sedge);
331 /* supergraph's dtor. Reset stmt uids. */
333 supergraph::~supergraph ()
335 m_stmt_uids.restore_uids ();
338 /* Dump this graph in .dot format to PP, using DUMP_ARGS.
339 Cluster the supernodes by function, then by BB from original CFG. */
341 void
342 supergraph::dump_dot_to_pp (pretty_printer *pp,
343 const dump_args_t &dump_args) const
345 graphviz_out gv (pp);
347 pp_string (pp, "digraph \"");
348 pp_write_text_to_stream (pp);
349 pp_string (pp, "supergraph");
350 pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/false);
351 pp_string (pp, "\" {\n");
352 gv.indent ();
354 gv.println ("overlap=false;");
355 gv.println ("compound=true;");
357 /* TODO: maybe (optionally) sub-subdivide by TU, for LTO; see also:
358 https://gcc-python-plugin.readthedocs.io/en/latest/_images/sample-supergraph.png
361 /* Break out the supernodes into clusters by function. */
363 cgraph_node *node;
364 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
366 function *fun = node->get_fun ();
367 gcc_assert (fun);
368 const char *funcname = function_name (fun);
369 gv.println ("subgraph \"cluster_%s\" {",
370 funcname);
371 gv.indent ();
372 pp_printf (pp,
373 ("style=\"dashed\";"
374 " color=\"black\";"
375 " label=\"%s\";\n"),
376 funcname);
378 /* Break out the nodes into clusters by BB from original CFG. */
380 basic_block bb;
381 FOR_ALL_BB_FN (bb, fun)
383 if (dump_args.m_flags & SUPERGRAPH_DOT_SHOW_BBS)
385 gv.println ("subgraph \"cluster_%s_bb_%i\" {",
386 funcname, bb->index);
387 gv.indent ();
388 pp_printf (pp,
389 ("style=\"dashed\";"
390 " color=\"black\";"
391 " label=\"bb: %i\";\n"),
392 bb->index);
395 // TODO: maybe keep an index per-function/per-bb to speed this up???
396 int i;
397 supernode *n;
398 FOR_EACH_VEC_ELT (m_nodes, i, n)
399 if (n->m_fun == fun && n->m_bb == bb)
400 n->dump_dot (&gv, dump_args);
402 if (dump_args.m_flags & SUPERGRAPH_DOT_SHOW_BBS)
404 /* Terminate per-bb "subgraph" */
405 gv.outdent ();
406 gv.println ("}");
411 /* Add an invisible edge from ENTRY to EXIT, to improve the graph layout. */
412 pp_string (pp, "\t");
413 get_node_for_function_entry (*fun)->dump_dot_id (pp);
414 pp_string (pp, ":s -> ");
415 get_node_for_function_exit (*fun)->dump_dot_id (pp);
416 pp_string (pp, ":n [style=\"invis\",constraint=true];\n");
418 /* Terminate per-function "subgraph" */
419 gv.outdent ();
420 gv.println ("}");
424 /* Superedges. */
425 int i;
426 superedge *e;
427 FOR_EACH_VEC_ELT (m_edges, i, e)
428 e->dump_dot (&gv, dump_args);
430 /* Terminate "digraph" */
431 gv.outdent ();
432 gv.println ("}");
435 /* Dump this graph in .dot format to FP, using DUMP_ARGS. */
437 void
438 supergraph::dump_dot_to_file (FILE *fp, const dump_args_t &dump_args) const
440 pretty_printer *pp = global_dc->m_printer->clone ();
441 pp_show_color (pp) = 0;
442 /* %qE in logs for SSA_NAMEs should show the ssa names, rather than
443 trying to prettify things by showing the underlying var. */
444 pp_format_decoder (pp) = default_tree_printer;
446 pp->set_output_stream (fp);
447 dump_dot_to_pp (pp, dump_args);
448 pp_flush (pp);
449 delete pp;
452 /* Dump this graph in .dot format to PATH, using DUMP_ARGS. */
454 void
455 supergraph::dump_dot (const char *path, const dump_args_t &dump_args) const
457 FILE *fp = fopen (path, "w");
458 dump_dot_to_file (fp, dump_args);
459 fclose (fp);
462 /* Return a new json::object of the form
463 {"nodes" : [objs for snodes],
464 "edges" : [objs for sedges]}. */
466 json::object *
467 supergraph::to_json () const
469 json::object *sgraph_obj = new json::object ();
471 /* Nodes. */
473 json::array *nodes_arr = new json::array ();
474 unsigned i;
475 supernode *n;
476 FOR_EACH_VEC_ELT (m_nodes, i, n)
477 nodes_arr->append (n->to_json ());
478 sgraph_obj->set ("nodes", nodes_arr);
481 /* Edges. */
483 json::array *edges_arr = new json::array ();
484 unsigned i;
485 superedge *n;
486 FOR_EACH_VEC_ELT (m_edges, i, n)
487 edges_arr->append (n->to_json ());
488 sgraph_obj->set ("edges", edges_arr);
491 return sgraph_obj;
494 /* Create a supernode for BB within FUN and add it to this supergraph.
496 If RETURNING_CALL is non-NULL, the supernode represents the resumption
497 of the basic block after returning from that call.
499 If PHI_NODES is non-NULL, this is the initial supernode for the basic
500 block, and is responsible for any handling of the phi nodes. */
502 supernode *
503 supergraph::add_node (function *fun, basic_block bb, gcall *returning_call,
504 gimple_seq phi_nodes)
506 supernode *n = new supernode (fun, bb, returning_call, phi_nodes,
507 m_nodes.length ());
508 m_nodes.safe_push (n);
509 return n;
512 /* Create a new cfg_superedge from SRC to DEST for the underlying CFG edge E,
513 adding it to this supergraph.
515 If the edge is for a switch statement, create a switch_cfg_superedge
516 subclass. */
518 cfg_superedge *
519 supergraph::add_cfg_edge (supernode *src, supernode *dest, ::edge e)
521 /* Special-case switch edges. */
522 gimple *stmt = src->get_last_stmt ();
523 cfg_superedge *new_edge;
524 if (stmt && stmt->code == GIMPLE_SWITCH)
525 new_edge = new switch_cfg_superedge (src, dest, e);
526 else
527 new_edge = new cfg_superedge (src, dest, e);
528 add_edge (new_edge);
529 return new_edge;
532 /* Create and add a call_superedge representing an interprocedural call
533 from SRC to DEST, using CEDGE. */
535 call_superedge *
536 supergraph::add_call_superedge (supernode *src, supernode *dest,
537 cgraph_edge *cedge)
539 call_superedge *new_edge = new call_superedge (src, dest, cedge);
540 add_edge (new_edge);
541 return new_edge;
544 /* Create and add a return_superedge representing returning from an
545 interprocedural call, returning from SRC to DEST, using CEDGE. */
547 return_superedge *
548 supergraph::add_return_superedge (supernode *src, supernode *dest,
549 cgraph_edge *cedge)
551 return_superedge *new_edge = new return_superedge (src, dest, cedge);
552 add_edge (new_edge);
553 return new_edge;
556 /* Implementation of dnode::dump_dot vfunc for supernodes.
558 Write a cluster for the node, and within it a .dot node showing
559 the phi nodes and stmts. Call into any node annotator from ARGS to
560 potentially add other records to the cluster. */
562 void
563 supernode::dump_dot (graphviz_out *gv, const dump_args_t &args) const
565 gv->println ("subgraph cluster_node_%i {",
566 m_index);
567 gv->indent ();
569 gv->println("style=\"solid\";");
570 gv->println("color=\"black\";");
571 gv->println("fillcolor=\"lightgrey\";");
572 gv->println("label=\"sn: %i (bb: %i)\";", m_index, m_bb->index);
574 pretty_printer *pp = gv->get_pp ();
576 if (args.m_node_annotator)
577 args.m_node_annotator->add_node_annotations (gv, *this, false);
579 gv->write_indent ();
580 dump_dot_id (pp);
581 pp_printf (pp,
582 " [shape=none,margin=0,style=filled,fillcolor=%s,label=<",
583 "lightgrey");
584 pp_string (pp, "<TABLE BORDER=\"0\">");
585 pp_write_text_to_stream (pp);
587 bool had_row = false;
589 /* Give any annotator the chance to add its own per-node TR elements. */
590 if (args.m_node_annotator)
591 if (args.m_node_annotator->add_node_annotations (gv, *this, true))
592 had_row = true;
594 if (m_returning_call)
596 gv->begin_trtd ();
597 pp_string (pp, "returning call: ");
598 gv->end_tdtr ();
600 gv->begin_tr ();
601 gv->begin_td ();
602 pp_gimple_stmt_1 (pp, m_returning_call, 0, (dump_flags_t)0);
603 pp_write_text_as_html_like_dot_to_stream (pp);
604 gv->end_td ();
605 /* Give any annotator the chance to add per-stmt TD elements to
606 this row. */
607 if (args.m_node_annotator)
608 args.m_node_annotator->add_stmt_annotations (gv, m_returning_call,
609 true);
610 gv->end_tr ();
612 /* Give any annotator the chance to add per-stmt TR elements. */
613 if (args.m_node_annotator)
614 args.m_node_annotator->add_stmt_annotations (gv, m_returning_call,
615 false);
616 pp_newline (pp);
618 had_row = true;
621 if (entry_p ())
623 pp_string (pp, "<TR><TD>ENTRY</TD></TR>");
624 pp_newline (pp);
625 had_row = true;
628 if (return_p ())
630 pp_string (pp, "<TR><TD>EXIT</TD></TR>");
631 pp_newline (pp);
632 had_row = true;
635 /* Phi nodes. */
636 for (gphi_iterator gpi = const_cast<supernode *> (this)->start_phis ();
637 !gsi_end_p (gpi); gsi_next (&gpi))
639 const gimple *stmt = gsi_stmt (gpi);
640 gv->begin_tr ();
641 gv->begin_td ();
642 pp_gimple_stmt_1 (pp, stmt, 0, (dump_flags_t)0);
643 pp_write_text_as_html_like_dot_to_stream (pp);
644 gv->end_td ();
645 /* Give any annotator the chance to add per-phi TD elements to
646 this row. */
647 if (args.m_node_annotator)
648 args.m_node_annotator->add_stmt_annotations (gv, stmt, true);
649 gv->end_tr ();
651 /* Give any annotator the chance to add per-phi TR elements. */
652 if (args.m_node_annotator)
653 args.m_node_annotator->add_stmt_annotations (gv, stmt, false);
655 pp_newline (pp);
656 had_row = true;
659 /* Statements. */
660 int i;
661 gimple *stmt;
662 FOR_EACH_VEC_ELT (m_stmts, i, stmt)
664 gv->begin_tr ();
665 gv->begin_td ();
666 pp_gimple_stmt_1 (pp, stmt, 0, (dump_flags_t)0);
667 pp_write_text_as_html_like_dot_to_stream (pp);
668 gv->end_td ();
669 /* Give any annotator the chance to add per-stmt TD elements to
670 this row. */
671 if (args.m_node_annotator)
672 args.m_node_annotator->add_stmt_annotations (gv, stmt, true);
673 gv->end_tr ();
675 /* Give any annotator the chance to add per-stmt TR elements. */
676 if (args.m_node_annotator)
677 args.m_node_annotator->add_stmt_annotations (gv, stmt, false);
679 pp_newline (pp);
680 had_row = true;
683 /* Give any annotator the chance to add additional per-node TR elements
684 to the end of the TABLE. */
685 if (args.m_node_annotator)
686 if (args.m_node_annotator->add_after_node_annotations (gv, *this))
687 had_row = true;
689 /* Graphviz requires a TABLE element to have at least one TR
690 (and each TR to have at least one TD). */
691 if (!had_row)
693 pp_string (pp, "<TR><TD>(empty)</TD></TR>");
694 pp_newline (pp);
697 pp_string (pp, "</TABLE>>];\n\n");
698 pp_flush (pp);
700 /* Terminate "subgraph" */
701 gv->outdent ();
702 gv->println ("}");
705 /* Write an ID for this node to PP, for use in .dot output. */
707 void
708 supernode::dump_dot_id (pretty_printer *pp) const
710 pp_printf (pp, "node_%i", m_index);
713 /* Return a new json::object of the form
714 {"idx": int,
715 "fun": optional str
716 "bb_idx": int,
717 "returning_call": optional str,
718 "phis": [str],
719 "stmts" : [str]}. */
721 json::object *
722 supernode::to_json () const
724 json::object *snode_obj = new json::object ();
726 snode_obj->set_integer ("idx", m_index);
727 snode_obj->set_integer ("bb_idx", m_bb->index);
728 if (function *fun = get_function ())
729 snode_obj->set_string ("fun", function_name (fun));
731 if (m_returning_call)
733 pretty_printer pp;
734 pp_format_decoder (&pp) = default_tree_printer;
735 pp_gimple_stmt_1 (&pp, m_returning_call, 0, (dump_flags_t)0);
736 snode_obj->set_string ("returning_call", pp_formatted_text (&pp));
739 /* Phi nodes. */
741 json::array *phi_arr = new json::array ();
742 for (gphi_iterator gpi = const_cast<supernode *> (this)->start_phis ();
743 !gsi_end_p (gpi); gsi_next (&gpi))
745 const gimple *stmt = gsi_stmt (gpi);
746 pretty_printer pp;
747 pp_format_decoder (&pp) = default_tree_printer;
748 pp_gimple_stmt_1 (&pp, stmt, 0, (dump_flags_t)0);
749 phi_arr->append_string (pp_formatted_text (&pp));
751 snode_obj->set ("phis", phi_arr);
754 /* Statements. */
756 json::array *stmt_arr = new json::array ();
757 int i;
758 gimple *stmt;
759 FOR_EACH_VEC_ELT (m_stmts, i, stmt)
761 pretty_printer pp;
762 pp_format_decoder (&pp) = default_tree_printer;
763 pp_gimple_stmt_1 (&pp, stmt, 0, (dump_flags_t)0);
764 stmt_arr->append_string (pp_formatted_text (&pp));
766 snode_obj->set ("stmts", stmt_arr);
769 return snode_obj;
772 /* Get a location_t for the start of this supernode. */
774 location_t
775 supernode::get_start_location () const
777 if (m_returning_call
778 && get_pure_location (m_returning_call->location) != UNKNOWN_LOCATION)
779 return m_returning_call->location;
781 int i;
782 gimple *stmt;
783 FOR_EACH_VEC_ELT (m_stmts, i, stmt)
784 if (get_pure_location (stmt->location) != UNKNOWN_LOCATION)
785 return stmt->location;
787 if (entry_p ())
789 // TWEAK: show the decl instead; this leads to more readable output:
790 return DECL_SOURCE_LOCATION (m_fun->decl);
792 return m_fun->function_start_locus;
794 if (return_p ())
795 return m_fun->function_end_locus;
797 /* We have no locations for stmts. If we have a single out-edge that's
798 a CFG edge, the goto_locus of that edge is a better location for this
799 than UNKNOWN_LOCATION. */
800 if (m_succs.length () == 1)
801 if (const cfg_superedge *cfg_sedge = m_succs[0]->dyn_cast_cfg_superedge ())
802 return cfg_sedge->get_goto_locus ();
804 return UNKNOWN_LOCATION;
807 /* Get a location_t for the end of this supernode. */
809 location_t
810 supernode::get_end_location () const
812 int i;
813 gimple *stmt;
814 FOR_EACH_VEC_ELT_REVERSE (m_stmts, i, stmt)
815 if (get_pure_location (stmt->location) != UNKNOWN_LOCATION)
816 return stmt->location;
818 if (m_returning_call
819 && get_pure_location (m_returning_call->location) != UNKNOWN_LOCATION)
820 return m_returning_call->location;
822 if (entry_p ())
823 return m_fun->function_start_locus;
824 if (return_p ())
825 return m_fun->function_end_locus;
827 /* If we have a single out-edge that's a CFG edge, use the goto_locus of
828 that edge. */
829 if (m_succs.length () == 1)
830 if (const cfg_superedge *cfg_sedge = m_succs[0]->dyn_cast_cfg_superedge ())
831 return cfg_sedge->get_goto_locus ();
833 return UNKNOWN_LOCATION;
836 /* Given STMT within this supernode, return its index within m_stmts. */
838 unsigned int
839 supernode::get_stmt_index (const gimple *stmt) const
841 unsigned i;
842 gimple *iter_stmt;
843 FOR_EACH_VEC_ELT (m_stmts, i, iter_stmt)
844 if (iter_stmt == stmt)
845 return i;
846 gcc_unreachable ();
849 /* Get any label_decl for this supernode, or NULL_TREE if there isn't one. */
851 tree
852 supernode::get_label () const
854 if (m_stmts.length () == 0)
855 return NULL_TREE;
856 const glabel *label_stmt = dyn_cast<const glabel *> (m_stmts[0]);
857 if (!label_stmt)
858 return NULL_TREE;
859 return gimple_label_label (label_stmt);
862 /* Get a string for PK. */
864 static const char *
865 edge_kind_to_string (enum edge_kind kind)
867 switch (kind)
869 default:
870 gcc_unreachable ();
871 case SUPEREDGE_CFG_EDGE:
872 return "SUPEREDGE_CFG_EDGE";
873 case SUPEREDGE_CALL:
874 return "SUPEREDGE_CALL";
875 case SUPEREDGE_RETURN:
876 return "SUPEREDGE_RETURN";
877 case SUPEREDGE_INTRAPROCEDURAL_CALL:
878 return "SUPEREDGE_INTRAPROCEDURAL_CALL";
882 /* Dump this superedge to PP. */
884 void
885 superedge::dump (pretty_printer *pp) const
887 pp_printf (pp, "edge: SN: %i -> SN: %i", m_src->m_index, m_dest->m_index);
888 label_text desc (get_description (false));
889 if (strlen (desc.get ()) > 0)
891 pp_space (pp);
892 pp_string (pp, desc.get ());
896 /* Dump this superedge to stderr. */
898 DEBUG_FUNCTION void
899 superedge::dump () const
901 tree_dump_pretty_printer pp (stderr);
902 dump (&pp);
903 pp_newline (&pp);
906 /* Implementation of dedge::dump_dot for superedges.
907 Write a .dot edge to GV representing this superedge. */
909 void
910 superedge::dump_dot (graphviz_out *gv, const dump_args_t &) const
912 const char *style = "\"solid,bold\"";
913 const char *color = "black";
914 int weight = 10;
915 const char *constraint = "true";
917 switch (m_kind)
919 default:
920 gcc_unreachable ();
921 case SUPEREDGE_CFG_EDGE:
922 break;
923 case SUPEREDGE_CALL:
924 color = "red";
925 break;
926 case SUPEREDGE_RETURN:
927 color = "green";
928 break;
929 case SUPEREDGE_INTRAPROCEDURAL_CALL:
930 style = "\"dotted\"";
931 break;
934 /* Adapted from graph.cc:draw_cfg_node_succ_edges. */
935 if (::edge cfg_edge = get_any_cfg_edge ())
937 if (cfg_edge->flags & EDGE_FAKE)
939 style = "dotted";
940 color = "green";
941 weight = 0;
943 else if (cfg_edge->flags & EDGE_DFS_BACK)
945 style = "\"dotted,bold\"";
946 color = "blue";
947 weight = 10;
949 else if (cfg_edge->flags & EDGE_FALLTHRU)
951 color = "blue";
952 weight = 100;
955 if (cfg_edge->flags & EDGE_ABNORMAL)
956 color = "red";
959 gv->write_indent ();
961 pretty_printer *pp = gv->get_pp ();
963 m_src->dump_dot_id (pp);
964 pp_string (pp, " -> ");
965 m_dest->dump_dot_id (pp);
966 pp_printf (pp,
967 (" [style=%s, color=%s, weight=%d, constraint=%s,"
968 " ltail=\"cluster_node_%i\", lhead=\"cluster_node_%i\""
969 " headlabel=\""),
970 style, color, weight, constraint,
971 m_src->m_index, m_dest->m_index);
973 dump_label_to_pp (pp, false);
975 pp_printf (pp, "\"];\n");
978 /* Return a new json::object of the form
979 {"kind" : str,
980 "src_idx": int, the index of the source supernode,
981 "dst_idx": int, the index of the destination supernode,
982 "desc" : str. */
984 json::object *
985 superedge::to_json () const
987 json::object *sedge_obj = new json::object ();
988 sedge_obj->set_string ("kind", edge_kind_to_string (m_kind));
989 sedge_obj->set_integer ("src_idx", m_src->m_index);
990 sedge_obj->set_integer ("dst_idx", m_dest->m_index);
993 pretty_printer pp;
994 pp_format_decoder (&pp) = default_tree_printer;
995 dump_label_to_pp (&pp, false);
996 sedge_obj->set_string ("desc", pp_formatted_text (&pp));
999 return sedge_obj;
1002 /* If this is an intraprocedural superedge, return the associated
1003 CFG edge. Otherwise, return NULL. */
1005 ::edge
1006 superedge::get_any_cfg_edge () const
1008 if (const cfg_superedge *sub = dyn_cast_cfg_superedge ())
1009 return sub->get_cfg_edge ();
1010 return NULL;
1013 /* If this is an interprocedural superedge, return the associated
1014 cgraph_edge *. Otherwise, return NULL. */
1016 cgraph_edge *
1017 superedge::get_any_callgraph_edge () const
1019 if (const callgraph_superedge *sub = dyn_cast_callgraph_superedge ())
1020 return sub->m_cedge;
1021 return NULL;
1024 /* Build a description of this superedge (e.g. "true" for the true
1025 edge of a conditional, or "case 42:" for a switch case).
1027 If USER_FACING is false, the result also contains any underlying
1028 CFG edge flags. e.g. " (flags FALLTHRU | DFS_BACK)". */
1030 label_text
1031 superedge::get_description (bool user_facing) const
1033 pretty_printer pp;
1034 dump_label_to_pp (&pp, user_facing);
1035 return label_text::take (xstrdup (pp_formatted_text (&pp)));
1038 /* Implementation of superedge::dump_label_to_pp for non-switch CFG
1039 superedges.
1041 For true/false edges, print "true" or "false" to PP.
1043 If USER_FACING is false, also print flags on the underlying CFG edge to
1044 PP. */
1046 void
1047 cfg_superedge::dump_label_to_pp (pretty_printer *pp,
1048 bool user_facing) const
1050 if (true_value_p ())
1051 pp_printf (pp, "true");
1052 else if (false_value_p ())
1053 pp_printf (pp, "false");
1055 if (user_facing)
1056 return;
1058 /* Express edge flags as a string with " | " separator.
1059 e.g. " (flags FALLTHRU | DFS_BACK)". */
1060 if (get_flags ())
1062 pp_string (pp, " (flags ");
1063 bool seen_flag = false;
1064 #define DEF_EDGE_FLAG(NAME,IDX) \
1065 do { \
1066 if (get_flags () & EDGE_##NAME) \
1068 if (seen_flag) \
1069 pp_string (pp, " | "); \
1070 pp_printf (pp, "%s", (#NAME)); \
1071 seen_flag = true; \
1073 } while (0);
1074 #include "cfg-flags.def"
1075 #undef DEF_EDGE_FLAG
1076 pp_string (pp, ")");
1079 if (m_cfg_edge->goto_locus > BUILTINS_LOCATION)
1080 pp_string (pp, " (has goto_locus)");
1082 /* Otherwise, no label. */
1085 /* Get the index number for this edge for use in phi stmts
1086 in its destination. */
1088 size_t
1089 cfg_superedge::get_phi_arg_idx () const
1091 return m_cfg_edge->dest_idx;
1094 /* Get the phi argument for PHI for this CFG edge. */
1096 tree
1097 cfg_superedge::get_phi_arg (const gphi *phi) const
1099 size_t index = get_phi_arg_idx ();
1100 return gimple_phi_arg_def (phi, index);
1103 switch_cfg_superedge::switch_cfg_superedge (supernode *src,
1104 supernode *dst,
1105 ::edge e)
1106 : cfg_superedge (src, dst, e)
1108 /* Populate m_case_labels with all cases which go to DST. */
1109 const gswitch *gswitch = get_switch_stmt ();
1110 for (unsigned i = 0; i < gimple_switch_num_labels (gswitch); i++)
1112 tree case_ = gimple_switch_label (gswitch, i);
1113 basic_block bb = label_to_block (src->get_function (),
1114 CASE_LABEL (case_));
1115 if (bb == dst->m_bb)
1116 m_case_labels.safe_push (case_);
1120 /* Implementation of superedge::dump_label_to_pp for CFG superedges for
1121 "switch" statements.
1123 Print "case VAL:", "case LOWER ... UPPER:", or "default:" to PP. */
1125 void
1126 switch_cfg_superedge::dump_label_to_pp (pretty_printer *pp,
1127 bool user_facing ATTRIBUTE_UNUSED) const
1129 if (user_facing)
1131 for (unsigned i = 0; i < m_case_labels.length (); ++i)
1133 if (i > 0)
1134 pp_string (pp, ", ");
1135 tree case_label = m_case_labels[i];
1136 gcc_assert (TREE_CODE (case_label) == CASE_LABEL_EXPR);
1137 tree lower_bound = CASE_LOW (case_label);
1138 tree upper_bound = CASE_HIGH (case_label);
1139 if (lower_bound)
1141 pp_printf (pp, "case ");
1142 dump_generic_node (pp, lower_bound, 0, (dump_flags_t)0, false);
1143 if (upper_bound)
1145 pp_printf (pp, " ... ");
1146 dump_generic_node (pp, upper_bound, 0, (dump_flags_t)0,
1147 false);
1149 pp_printf (pp, ":");
1151 else
1152 pp_printf (pp, "default:");
1155 else
1157 pp_character (pp, '{');
1158 for (unsigned i = 0; i < m_case_labels.length (); ++i)
1160 if (i > 0)
1161 pp_string (pp, ", ");
1162 tree case_label = m_case_labels[i];
1163 gcc_assert (TREE_CODE (case_label) == CASE_LABEL_EXPR);
1164 tree lower_bound = CASE_LOW (case_label);
1165 tree upper_bound = CASE_HIGH (case_label);
1166 if (lower_bound)
1168 if (upper_bound)
1170 pp_character (pp, '[');
1171 dump_generic_node (pp, lower_bound, 0, (dump_flags_t)0,
1172 false);
1173 pp_string (pp, ", ");
1174 dump_generic_node (pp, upper_bound, 0, (dump_flags_t)0,
1175 false);
1176 pp_character (pp, ']');
1178 else
1179 dump_generic_node (pp, lower_bound, 0, (dump_flags_t)0, false);
1181 else
1182 pp_printf (pp, "default");
1184 pp_character (pp, '}');
1185 if (implicitly_created_default_p ())
1187 pp_string (pp, " IMPLICITLY CREATED");
1192 /* Return true iff this edge is purely for an implicitly-created "default". */
1194 bool
1195 switch_cfg_superedge::implicitly_created_default_p () const
1197 if (m_case_labels.length () != 1)
1198 return false;
1200 tree case_label = m_case_labels[0];
1201 gcc_assert (TREE_CODE (case_label) == CASE_LABEL_EXPR);
1202 if (CASE_LOW (case_label))
1203 return false;
1205 /* We have a single "default" case.
1206 Assume that it was implicitly created if it has UNKNOWN_LOCATION. */
1207 return EXPR_LOCATION (case_label) == UNKNOWN_LOCATION;
1210 /* Implementation of superedge::dump_label_to_pp for interprocedural
1211 superedges. */
1213 void
1214 callgraph_superedge::dump_label_to_pp (pretty_printer *pp,
1215 bool user_facing ATTRIBUTE_UNUSED) const
1217 switch (m_kind)
1219 default:
1220 case SUPEREDGE_CFG_EDGE:
1221 gcc_unreachable ();
1223 case SUPEREDGE_CALL:
1224 pp_printf (pp, "call");
1225 break;
1227 case SUPEREDGE_RETURN:
1228 pp_printf (pp, "return");
1229 break;
1231 case SUPEREDGE_INTRAPROCEDURAL_CALL:
1232 pp_printf (pp, "intraproc link");
1233 break;
1237 /* Get the function that was called at this interprocedural call/return
1238 edge. */
1240 function *
1241 callgraph_superedge::get_callee_function () const
1243 return get_ultimate_function_for_cgraph_edge (m_cedge);
1246 /* Get the calling function at this interprocedural call/return edge. */
1248 function *
1249 callgraph_superedge::get_caller_function () const
1251 return m_cedge->caller->get_fun ();
1254 /* Get the fndecl that was called at this interprocedural call/return
1255 edge. */
1257 tree
1258 callgraph_superedge::get_callee_decl () const
1260 return get_callee_function ()->decl;
1263 /* Get the gcall * of this interprocedural call/return edge. */
1265 gcall *
1266 callgraph_superedge::get_call_stmt () const
1268 if (m_cedge)
1269 return m_cedge->call_stmt;
1271 return m_src->get_final_call ();
1274 /* Get the calling fndecl at this interprocedural call/return edge. */
1276 tree
1277 callgraph_superedge::get_caller_decl () const
1279 return get_caller_function ()->decl;
1282 /* Given PARM_TO_FIND, a PARM_DECL, identify its index (writing it
1283 to *OUT if OUT is non-NULL), and return the corresponding argument
1284 at the callsite. */
1286 tree
1287 callgraph_superedge::get_arg_for_parm (tree parm_to_find,
1288 callsite_expr *out) const
1290 gcc_assert (TREE_CODE (parm_to_find) == PARM_DECL);
1292 tree callee = get_callee_decl ();
1293 const gcall *call_stmt = get_call_stmt ();
1295 unsigned i = 0;
1296 for (tree iter_parm = DECL_ARGUMENTS (callee); iter_parm;
1297 iter_parm = DECL_CHAIN (iter_parm), ++i)
1299 if (i >= gimple_call_num_args (call_stmt))
1300 return NULL_TREE;
1301 if (iter_parm == parm_to_find)
1303 if (out)
1304 *out = callsite_expr::from_zero_based_param (i);
1305 return gimple_call_arg (call_stmt, i);
1309 /* Not found. */
1310 return NULL_TREE;
1313 /* Look for a use of ARG_TO_FIND as an argument at this callsite.
1314 If found, return the default SSA def of the corresponding parm within
1315 the callee, and if OUT is non-NULL, write the index to *OUT.
1316 Only the first match is handled. */
1318 tree
1319 callgraph_superedge::get_parm_for_arg (tree arg_to_find,
1320 callsite_expr *out) const
1322 tree callee = get_callee_decl ();
1323 const gcall *call_stmt = get_call_stmt ();
1325 unsigned i = 0;
1326 for (tree iter_parm = DECL_ARGUMENTS (callee); iter_parm;
1327 iter_parm = DECL_CHAIN (iter_parm), ++i)
1329 if (i >= gimple_call_num_args (call_stmt))
1330 return NULL_TREE;
1331 tree param = gimple_call_arg (call_stmt, i);
1332 if (arg_to_find == param)
1334 if (out)
1335 *out = callsite_expr::from_zero_based_param (i);
1336 return ssa_default_def (get_callee_function (), iter_parm);
1340 /* Not found. */
1341 return NULL_TREE;
1344 /* Map caller_expr back to an expr within the callee, or return NULL_TREE.
1345 If non-NULL is returned, populate OUT. */
1347 tree
1348 callgraph_superedge::map_expr_from_caller_to_callee (tree caller_expr,
1349 callsite_expr *out) const
1351 /* Is it an argument (actual param)? If so, convert to
1352 parameter (formal param). */
1353 tree parm = get_parm_for_arg (caller_expr, out);
1354 if (parm)
1355 return parm;
1356 /* Otherwise try return value. */
1357 if (caller_expr == gimple_call_lhs (get_call_stmt ()))
1359 if (out)
1360 *out = callsite_expr::from_return_value ();
1361 return DECL_RESULT (get_callee_decl ());
1364 return NULL_TREE;
1367 /* Map callee_expr back to an expr within the caller, or return NULL_TREE.
1368 If non-NULL is returned, populate OUT. */
1370 tree
1371 callgraph_superedge::map_expr_from_callee_to_caller (tree callee_expr,
1372 callsite_expr *out) const
1374 if (callee_expr == NULL_TREE)
1375 return NULL_TREE;
1377 /* If it's a parameter (formal param), get the argument (actual param). */
1378 if (TREE_CODE (callee_expr) == PARM_DECL)
1379 return get_arg_for_parm (callee_expr, out);
1381 /* Similar for the default SSA name of the PARM_DECL. */
1382 if (TREE_CODE (callee_expr) == SSA_NAME
1383 && SSA_NAME_IS_DEFAULT_DEF (callee_expr)
1384 && TREE_CODE (SSA_NAME_VAR (callee_expr)) == PARM_DECL)
1385 return get_arg_for_parm (SSA_NAME_VAR (callee_expr), out);
1387 /* Otherwise try return value. */
1388 if (callee_expr == DECL_RESULT (get_callee_decl ()))
1390 if (out)
1391 *out = callsite_expr::from_return_value ();
1392 return gimple_call_lhs (get_call_stmt ());
1395 return NULL_TREE;
1398 } // namespace ana
1400 #endif /* #if ENABLE_ANALYZER */