LCOV - code coverage report
Current view: top level - gcc/analyzer - supergraph.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 87.6 % 579 507
Test Date: 2026-11-14 16:39:50 Functions: 82.9 % 35 29
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* "Supergraph" classes that combine CFGs and callgraph into one digraph.
       2              :    Copyright (C) 2019-2026 Free Software Foundation, Inc.
       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              : #define INCLUDE_DEQUE
      22              : #include "analyzer/common.h"
      23              : 
      24              : #include "timevar.h"
      25              : #include "gimple-pretty-print.h"
      26              : #include "ordered-hash-map.h"
      27              : #include "options.h"
      28              : #include "cgraph.h"
      29              : #include "cfg.h"
      30              : #include "digraph.h"
      31              : #include "tree-cfg.h"
      32              : #include "cfganal.h"
      33              : #include "except.h"
      34              : #include "tree-logical-location.h"
      35              : 
      36              : #include "diagnostics/file-cache.h"
      37              : #include "diagnostics/digraphs.h"
      38              : 
      39              : #include "analyzer/supergraph.h"
      40              : #include "analyzer/analyzer-logging.h"
      41              : #include "analyzer/region-model.h"
      42              : #include "analyzer/exploded-graph.h"
      43              : 
      44              : #if ENABLE_ANALYZER
      45              : 
      46              : namespace ana {
      47              : 
      48              : /* Get the function of the ultimate alias target being called at EDGE,
      49              :    if any.  */
      50              : 
      51              : function *
      52            0 : get_ultimate_function_for_cgraph_edge (cgraph_edge *edge)
      53              : {
      54            0 :   cgraph_node *ultimate_node = edge->callee->ultimate_alias_target ();
      55            0 :   if (!ultimate_node)
      56              :     return nullptr;
      57            0 :   return ultimate_node->get_fun ();
      58              : }
      59              : 
      60              : /* class saved_uids.
      61              : 
      62              :    In order to ensure consistent results without relying on the ordering
      63              :    of pointer values we assign a uid to each gimple stmt, globally unique
      64              :    across all functions.
      65              : 
      66              :    Normally, the stmt uids are a scratch space that each pass can freely
      67              :    assign its own values to.  However, in the case of LTO, the uids are
      68              :    used to associate call stmts with callgraph edges between the WPA phase
      69              :    (where the analyzer runs in LTO mode) and the LTRANS phase; if the
      70              :    analyzer changes them in the WPA phase, it leads to errors when
      71              :    streaming the code back in at LTRANS.
      72              :    lto_prepare_function_for_streaming has code to renumber the stmt UIDs
      73              :    when the code is streamed back out, but for some reason this isn't
      74              :    called for clones.
      75              : 
      76              :    Hence, as a workaround, this class has responsibility for tracking
      77              :    the original uids and restoring them once the pass is complete
      78              :    (in the supergraph dtor).  */
      79              : 
      80              : /* Give STMT a globally unique uid, storing its original uid so it can
      81              :    later be restored.  */
      82              : 
      83              : void
      84       140818 : saved_uids::make_uid_unique (gimple *stmt)
      85              : {
      86       140818 :   unsigned next_uid = m_old_stmt_uids.length ();
      87       140818 :   unsigned old_stmt_uid = stmt->uid;
      88       140818 :   stmt->uid = next_uid;
      89       140818 :   m_old_stmt_uids.safe_push
      90       140818 :     (std::pair<gimple *, unsigned> (stmt, old_stmt_uid));
      91       140818 : }
      92              : 
      93              : /* Restore the saved uids of all stmts.  */
      94              : 
      95              : void
      96         3573 : saved_uids::restore_uids () const
      97              : {
      98         3573 :   unsigned i;
      99         3573 :   std::pair<gimple *, unsigned> *pair;
     100       144391 :   FOR_EACH_VEC_ELT (m_old_stmt_uids, i, pair)
     101       140818 :     pair->first->uid = pair->second;
     102         3573 : }
     103              : 
     104              : /* When building the supergraph, should STMT be handled
     105              :    along each out-edge in the CFG, or as separate superedge
     106              :    "within" the BB.  */
     107              : 
     108              : static bool
     109       135101 : control_flow_stmt_p (const gimple &stmt)
     110              : {
     111       135101 :   switch (gimple_code (&stmt))
     112              :     {
     113              :     case GIMPLE_COND:
     114              :     case GIMPLE_EH_DISPATCH:
     115              :     case GIMPLE_GOTO:
     116              :     case GIMPLE_SWITCH:
     117              :       return true;
     118              : 
     119       126655 :     case GIMPLE_ASM:
     120       126655 :     case GIMPLE_ASSIGN:
     121       126655 :     case GIMPLE_CALL:
     122       126655 :     case GIMPLE_DEBUG:
     123       126655 :     case GIMPLE_LABEL:
     124       126655 :     case GIMPLE_NOP:
     125       126655 :     case GIMPLE_PREDICT:
     126       126655 :     case GIMPLE_RESX:
     127       126655 :     case GIMPLE_RETURN:
     128       126655 :       return false;
     129              : 
     130              :     /* We don't expect to see any other statement kinds in the analyzer.  */
     131            0 :     default:
     132            0 :       internal_error ("unexpected gimple stmt code: %qs",
     133            0 :                       gimple_code_name[gimple_code (&stmt)]);
     134              :       break;
     135              :     }
     136              : }
     137              : 
     138              : /* supergraph's ctor.  Walk the callgraph, building supernodes for each
     139              :    CFG basic block, splitting the basic blocks at statements.  Join
     140              :    together the supernodes with interprocedural superedges as appropriate.
     141              :    Assign UIDs to the gimple stmts.  */
     142              : 
     143         3573 : supergraph::supergraph (region_model_manager &mgr,
     144              :                         logger *logger)
     145         3573 : : m_next_snode_id (0)
     146              : {
     147         3573 :   auto_timevar tv (TV_ANALYZER_SUPERGRAPH_CREATION);
     148         3573 :   LOG_FUNC (logger);
     149              : 
     150              :   /* For each BB, if present, the stmt that terminates it.  */
     151         3573 :   typedef ordered_hash_map<basic_block, gimple *> bb_to_stmt_t;
     152         3573 :   bb_to_stmt_t control_stmt_ending_bbs;
     153              : 
     154              :   /* First pass: make supernodes (and assign UIDs to the gimple stmts).  */
     155         3573 :   {
     156              :     /* Sort the cgraph_nodes?  */
     157         3573 :     cgraph_node *node;
     158        15234 :     FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
     159              :       {
     160        11661 :         function *fun = node->get_fun ();
     161              : 
     162        11661 :         log_nesting_level log_sentinel (logger, "function: %qD", fun->decl);
     163              : 
     164              :         /* Ensure that EDGE_DFS_BACK is correct for every CFG edge in
     165              :            the supergraph (by doing it per-function).  */
     166        11661 :         auto_cfun cfun_sentinel (fun);
     167        11661 :         mark_dfs_back_edges ();
     168              : 
     169        11661 :         const int start_id = m_nodes.length ();
     170              : 
     171        11661 :         basic_block bb;
     172        74025 :         FOR_ALL_BB_FN (bb, fun)
     173       124728 :           if (gimple *final_control_stmt
     174        62364 :                 = populate_for_basic_block (bb, fun, logger))
     175         8446 :             control_stmt_ending_bbs.put (bb, final_control_stmt);
     176              : 
     177        11661 :         const unsigned num_snodes = m_nodes.length () - start_id;
     178        11661 :         m_function_to_num_snodes.put (fun, num_snodes);
     179              : 
     180        11661 :         if (logger)
     181              :           {
     182            6 :             const int end_id = m_nodes.length () - 1;
     183            6 :             logger->log ("SN: %i...%i: function %qD",
     184              :                          start_id, end_id, fun->decl);
     185              :           }
     186        11667 :       }
     187              :   }
     188              : 
     189              :   /* Second pass: make superedges between basic blocks.  */
     190         3573 :   {
     191              :     /* Make superedges for CFG edges.  */
     192         3573 :     for (bb_to_node_t::iterator iter = m_bb_to_final_node.begin ();
     193       131870 :          iter != m_bb_to_final_node.end ();
     194        62364 :          ++iter)
     195              :       {
     196        62364 :         basic_block bb = (*iter).first;
     197        62364 :         supernode *src_supernode = (*iter).second;
     198              : 
     199        62364 :         gimple *control_stmt_ending_bb = nullptr;
     200        62364 :         if (auto control_stmt_iter = control_stmt_ending_bbs.get (bb))
     201         8446 :           control_stmt_ending_bb = *control_stmt_iter;
     202              : 
     203        62364 :         ::edge cfg_edge;
     204        62364 :         int idx;
     205        62364 :         if (bb->succs)
     206       123443 :           FOR_EACH_VEC_ELT (*bb->succs, idx, cfg_edge)
     207              :             {
     208        61079 :               basic_block dest_cfg_block = cfg_edge->dest;
     209        61079 :               supernode *dest_supernode
     210        61079 :                 = *m_bb_to_initial_node.get (dest_cfg_block);
     211        61079 :               add_sedges_for_cfg_edge (src_supernode,
     212              :                                        dest_supernode,
     213              :                                        cfg_edge,
     214              :                                        control_stmt_ending_bb,
     215              :                                        mgr,
     216              :                                        logger);
     217              :             }
     218              :       }
     219              :   }
     220         3573 : }
     221              : 
     222              : /* Create a run of supernodes and superedges for the BB within FUN
     223              :    expressing all of the stmts apart from the final control flow stmt (if any).
     224              :    Return the control stmt that ends this bb, if any.  */
     225              : 
     226              : gimple *
     227        62364 : supergraph::populate_for_basic_block (basic_block bb,
     228              :                                       function *fun,
     229              :                                       logger *logger)
     230              : {
     231        62364 :   log_nesting_level sentinel (logger, "bb %i", bb->index);
     232              : 
     233        62364 :   supernode *initial_snode_in_bb = add_node (fun, bb, logger);
     234        62364 :   m_bb_to_initial_node.put (bb, initial_snode_in_bb);
     235              : 
     236        62364 :   if (bb->index == ENTRY_BLOCK)
     237              :     /* Use the decl's location, rather than fun->function_start_locus,
     238              :        which leads to more readable output.  */
     239        11661 :     initial_snode_in_bb->m_loc = DECL_SOURCE_LOCATION (fun->decl);
     240        50703 :   else if (bb->index == EXIT_BLOCK)
     241        11661 :     initial_snode_in_bb->m_loc = fun->function_end_locus;
     242        78084 :   else if (gsi_end_p (gsi_start_bb (bb)))
     243              :     {
     244              :       /* BB has no stmts, and isn't the ENTRY or EXIT node.
     245              :          Try to find a source location for it.  */
     246          783 :       if (bb->succs->length () == 1)
     247              :         {
     248          783 :           auto outedge = (*bb->succs)[0];
     249          783 :           if (useful_location_p (outedge->goto_locus))
     250              :             {
     251              :               /* We have an empty basic block with one out-edge,
     252              :                  perhaps part of an empty infinite loop.  */
     253          483 :               if (logger)
     254            0 :                 logger->log ("using location 0x%lx from outedge",
     255              :                              outedge->goto_locus);
     256          483 :               initial_snode_in_bb->m_loc = outedge->goto_locus;
     257              :             }
     258              :         }
     259              :     }
     260              : 
     261        62364 :   initial_snode_in_bb->m_state_merger_node = true;
     262              : 
     263        62364 :   gimple *final_control_flow_stmt = nullptr;
     264              : 
     265              :   /* Create a run of supernodes for the stmts in BB,
     266              :      connected by stmt_superedge.  */
     267        62364 :   gimple_stmt_iterator gsi;
     268        62364 :   supernode *prev_snode_in_bb = initial_snode_in_bb;
     269       257100 :   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
     270              :     {
     271       140818 :       gimple *next_stmt = gsi_stmt (gsi);
     272              : 
     273       140818 :       if (logger)
     274              :         {
     275           48 :           logger->start_log_line ();
     276           48 :           logger->log_partial ("next_stmt: ");
     277           48 :           pp_gimple_stmt_1 (logger->get_printer (), next_stmt,
     278              :                             0, (dump_flags_t)0);
     279           48 :           logger->end_log_line ();
     280              :         }
     281       140818 :       prev_snode_in_bb->m_loc = get_stmt_location (next_stmt, fun);
     282       140818 :       prev_snode_in_bb->m_stmt_loc = next_stmt->location;
     283       140818 :       m_node_for_stmt.insert ({next_stmt, prev_snode_in_bb});
     284              : 
     285       140818 :       m_stmt_uids.make_uid_unique (next_stmt);
     286              : 
     287       140818 :       if (auto glabel_ = dyn_cast<const glabel *>(next_stmt))
     288              :         {
     289              :           /* Associate the GIMPLE_LABEL with its snode.  */
     290         8776 :           prev_snode_in_bb->m_label = gimple_label_label (glabel_);
     291              : 
     292              :           /* Only create an snode for the label if it has location
     293              :              information.  */
     294         8776 :           if (glabel_->location == UNKNOWN_LOCATION)
     295         5717 :             continue;
     296              :         }
     297              : 
     298              :       // handle control flow stmts on the edges
     299       135101 :       if (control_flow_stmt_p (*next_stmt))
     300              :         {
     301              :           final_control_flow_stmt = next_stmt;
     302        62364 :           break;
     303              :         }
     304              : 
     305       126655 :       supernode *snode_after_next_stmt = add_node (fun, bb, logger);
     306       126655 :       if (prev_snode_in_bb)
     307              :         {
     308       126655 :           std::unique_ptr<operation> op;
     309       126655 :           switch (gimple_code (next_stmt))
     310              :             {
     311            0 :             default:
     312            0 :               gcc_unreachable ();
     313          210 :               break;
     314          210 :             case GIMPLE_ASM:
     315          210 :               op = std::make_unique<gasm_op>
     316          210 :                 (*as_a <const gasm *> (next_stmt));
     317          210 :               break;
     318        61269 :             case GIMPLE_ASSIGN:
     319        61269 :               op = std::make_unique<gassign_op>
     320        61269 :                 (*as_a <const gassign *> (next_stmt));
     321        61269 :               break;
     322        45320 :             case GIMPLE_CALL:
     323        45320 :               op = call_and_return_op::make
     324        45320 :                 (*as_a <const gcall *> (next_stmt));
     325        45320 :               break;
     326         1883 :             case GIMPLE_PREDICT:
     327         1883 :               op = std::make_unique<predict_op> (*next_stmt);
     328         1883 :               break;
     329          661 :             case GIMPLE_RESX:
     330          661 :               op = std::make_unique<resx_op>
     331          661 :                 (*as_a <const gresx *> (next_stmt));
     332          661 :               break;
     333        11462 :             case GIMPLE_RETURN:
     334        11462 :               op = std::make_unique<greturn_op>
     335        11462 :                 (*as_a <const greturn *> (next_stmt));
     336        11462 :               break;
     337              : 
     338              :             case GIMPLE_DEBUG:
     339              :             case GIMPLE_LABEL:
     340              :             case GIMPLE_NOP:
     341              :               /* Treat all of these as no-ops within analyzer; though
     342              :                  perhaps we care about their locations.  */
     343              :               break;
     344              :             }
     345              : 
     346       126655 :           superedge *sedge
     347              :             = new superedge (prev_snode_in_bb,
     348              :                              snode_after_next_stmt,
     349              :                              std::move (op),
     350       126655 :                              nullptr);
     351       126655 :           add_edge (sedge);
     352       126655 :         }
     353       126655 :       prev_snode_in_bb = snode_after_next_stmt;
     354              :     }
     355              : 
     356        62364 :   m_bb_to_final_node.put (bb, prev_snode_in_bb);
     357              : 
     358        62364 :   return final_control_flow_stmt;
     359        62364 : }
     360              : 
     361              : /* supergraph's dtor.  Reset stmt uids.  */
     362              : 
     363         3573 : supergraph::~supergraph ()
     364              : {
     365         3573 :   m_stmt_uids.restore_uids ();
     366         3573 : }
     367              : 
     368              : /* Dump this graph in .dot format to PP, using DUMP_ARGS.
     369              :    Cluster the supernodes by function, then by BB from original CFG.  */
     370              : 
     371              : void
     372           24 : supergraph::dump_dot_to_pp (pretty_printer *pp,
     373              :                             const dump_args_t &dump_args) const
     374              : {
     375           24 :   graphviz_out gv (pp);
     376              : 
     377           24 :   pp_string (pp, "digraph \"");
     378           24 :   pp_write_text_to_stream (pp);
     379           24 :   pp_string (pp, "supergraph");
     380           24 :   pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/false);
     381           24 :   pp_string (pp, "\" {\n");
     382           24 :   gv.indent ();
     383              : 
     384           24 :   gv.println ("overlap=false;");
     385           24 :   gv.println ("compound=true;");
     386              : 
     387              :   /* TODO: maybe (optionally) sub-subdivide by TU, for LTO; see also:
     388              :      https://gcc-python-plugin.readthedocs.io/en/latest/_images/sample-supergraph.png
     389              :   */
     390              : 
     391              :   /* Break out the supernodes into clusters by function.  */
     392           24 :   {
     393           24 :     cgraph_node *node;
     394           96 :     FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
     395              :     {
     396           72 :       function *fun = node->get_fun ();
     397           72 :       gcc_assert (fun);
     398           72 :       auto_cfun sentinel (fun);
     399              : 
     400           72 :       const char *funcname = function_name (fun);
     401           72 :       gv.println ("subgraph \"cluster_%s\" {",
     402              :                   funcname);
     403           72 :       gv.indent ();
     404           72 :       pp_printf (pp,
     405              :                  ("style=\"dashed\";"
     406              :                   " color=\"black\";"
     407              :                   " label=\"%s\";\n"),
     408              :                  funcname);
     409              : 
     410           72 :       if (loops_for_fn (fun))
     411           72 :         dump_dot_to_gv_for_loop (gv, dump_args, get_loop (fun, 0), fun);
     412              :       else
     413              :         {
     414            0 :           basic_block bb;
     415            0 :           FOR_ALL_BB_FN (bb, fun)
     416            0 :             dump_dot_to_gv_for_bb (gv, dump_args, bb, fun);
     417              :         }
     418              : 
     419              :       /* Terminate per-function "subgraph" */
     420           72 :       gv.outdent ();
     421           72 :       gv.println ("}");
     422              :     }
     423              :   }
     424              : 
     425              :   /* Superedges.  */
     426              :   int i;
     427              :   superedge *e;
     428         1178 :   FOR_EACH_VEC_ELT (m_edges, i, e)
     429         1154 :     e->dump_dot (&gv, dump_args);
     430              : 
     431           24 :   if (dump_args.m_node_annotator)
     432            8 :     dump_args.m_node_annotator->add_extra_objects (&gv);
     433              : 
     434              :   /* Terminate "digraph" */
     435           24 :   gv.outdent ();
     436           24 :   gv.println ("}");
     437           24 : }
     438              : 
     439              : /* Recursively dump all the snodes within LOOP and the loops
     440              :    within it.  */
     441              : 
     442              : void
     443          120 : supergraph::dump_dot_to_gv_for_loop (graphviz_out &gv,
     444              :                                      const dump_args_t &dump_args,
     445              :                                      class loop *loop,
     446              :                                      function *fun) const
     447              : {
     448          120 :   pretty_printer *pp = gv.get_pp ();
     449              : 
     450          120 :   basic_block *body;
     451          120 :   unsigned int i;
     452              :   // Adapted from graph.cc:draw_cfg_nodes_for_loop
     453          120 :   const char *fillcolors[3] = { "grey88", "grey77", "grey66" };
     454              : 
     455          120 :   if (loop->header != NULL
     456          120 :       && loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun))
     457           48 :     pp_printf (pp,
     458              :                "\tsubgraph cluster_%d_%d {\n"
     459              :                "\tstyle=\"filled\";\n"
     460              :                "\tcolor=\"darkgreen\";\n"
     461              :                "\tfillcolor=\"%s\";\n"
     462              :                "\tlabel=\"loop %d\";\n"
     463              :                "\tlabeljust=l;\n"
     464              :                "\tpenwidth=2;\n",
     465              :                fun->funcdef_no, loop->num,
     466           48 :                fillcolors[(loop_depth (loop) - 1) % 3],
     467              :                loop->num);
     468              : 
     469              :   // Recurse
     470          168 :   for (class loop *inner = loop->inner; inner; inner = inner->next)
     471           48 :     dump_dot_to_gv_for_loop (gv, dump_args, inner, fun);
     472              : 
     473          120 :   if (loop->header == NULL)
     474            0 :     return;
     475              : 
     476          120 :   if (loop->latch == EXIT_BLOCK_PTR_FOR_FN (cfun))
     477           72 :     body = get_loop_body (loop);
     478              :   else
     479           48 :     body = get_loop_body_in_bfs_order (loop);
     480              : 
     481          654 :   for (i = 0; i < loop->num_nodes; i++)
     482              :     {
     483          534 :       basic_block bb = body[i];
     484          534 :       if (bb->loop_father == loop)
     485          438 :         dump_dot_to_gv_for_bb (gv, dump_args, bb, fun);
     486              :     }
     487              : 
     488          120 :   free (body);
     489              : 
     490          120 :   if (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun))
     491           48 :     pp_printf (pp, "\t}\n");
     492              : }
     493              : 
     494              : /* Dump all the snodes from BB.  */
     495              : 
     496              : void
     497          438 : supergraph::dump_dot_to_gv_for_bb (graphviz_out &gv,
     498              :                                    const dump_args_t &dump_args,
     499              :                                    basic_block bb,
     500              :                                    function *fun) const
     501              : {
     502          438 :   pretty_printer *pp = gv.get_pp ();
     503              : 
     504          438 :   if (dump_args.m_flags & SUPERGRAPH_DOT_SHOW_BBS)
     505              :     {
     506          365 :       const char *funcname = function_name (fun);
     507          365 :       gv.println ("subgraph \"cluster_%s_bb_%i\" {",
     508              :                   funcname, bb->index);
     509          365 :       gv.indent ();
     510          365 :       pp_printf (pp,
     511              :                  ("style=\"dashed\";"
     512              :                   " color=\"black\";"
     513              :                   " label=\"bb: %i\";\n"),
     514              :                  bb->index);
     515              :     }
     516              : 
     517              :   // TODO: maybe keep an index per-function/per-bb to speed this up???
     518          438 :   int i;
     519          438 :   supernode *n;
     520        21518 :   FOR_EACH_VEC_ELT (m_nodes, i, n)
     521        21080 :     if (n->m_fun == fun && n->m_bb == bb)
     522         1154 :       n->dump_dot (&gv, dump_args);
     523              : 
     524          438 :   if (dump_args.m_flags & SUPERGRAPH_DOT_SHOW_BBS)
     525              :     {
     526              :       /* Terminate per-bb "subgraph" */
     527          365 :       gv.outdent ();
     528          365 :       gv.println ("}");
     529              :     }
     530          438 : }
     531              : 
     532              : 
     533              : /* Dump this graph in .dot format to FP, using DUMP_ARGS.  */
     534              : 
     535              : void
     536           24 : supergraph::dump_dot_to_file (FILE *fp, const dump_args_t &dump_args) const
     537              : {
     538           24 :   std::unique_ptr<pretty_printer> pp (global_dc->clone_printer ());
     539           24 :   pp_show_color (pp.get ()) = 0;
     540              :   /* %qE in logs for SSA_NAMEs should show the ssa names, rather than
     541              :      trying to prettify things by showing the underlying var.  */
     542           24 :   pp_format_decoder (pp.get ()) = default_tree_printer;
     543              : 
     544           24 :   pp->set_output_stream (fp);
     545           24 :   dump_dot_to_pp (pp.get (), dump_args);
     546           24 :   pp_flush (pp.get ());
     547           24 : }
     548              : 
     549              : /* Dump this graph in .dot format to PATH, using DUMP_ARGS.  */
     550              : 
     551              : void
     552           24 : supergraph::dump_dot (const char *path, const dump_args_t &dump_args) const
     553              : {
     554           24 :   FILE *fp = fopen (path, "w");
     555           24 :   dump_dot_to_file (fp, dump_args);
     556           24 :   fclose (fp);
     557           24 : }
     558              : 
     559              : /* A way to export the supergraph as a diagnostics::digraphs::digraph, and thus
     560              :    to export it to SARIF diagnostic sinks.  */
     561              : 
     562              : class supergraph_diagnostic_digraph : public diagnostics::digraphs::digraph
     563              : {
     564              : public:
     565            1 :   supergraph_diagnostic_digraph (const supergraph &sg)
     566            1 :   : m_sg (sg)
     567              :   {
     568            1 :     set_graph_kind ("supergraph");
     569              : 
     570              :     /* TODO: maybe (optionally) sub-subdivide by TU, for LTO; see also:
     571              :        https://gcc-python-plugin.readthedocs.io/en/latest/_images/sample-supergraph.png
     572              :     */
     573              : 
     574              :     /* Break out the supernodes into clusters by function.  */
     575            1 :     {
     576            1 :       cgraph_node *node;
     577            2 :       FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
     578              :         {
     579            1 :           function *fun = node->get_fun ();
     580            1 :           gcc_assert (fun);
     581            1 :           auto_cfun sentinel (fun);
     582            1 :           const char *funcname = function_name (fun);
     583            1 :           pretty_printer pp;
     584            1 :           pp_printf (&pp, "cluster for %s", funcname);
     585            1 :           std::string id = pp_formatted_text (&pp);
     586              : 
     587            1 :           auto diag_node_for_func
     588            1 :             = std::make_unique<diagnostics::digraphs::node> (*this, id);
     589            1 :           diag_node_for_func->set_logical_loc
     590            1 :             (tree_logical_location_manager::key_from_tree (fun->decl));
     591              : 
     592            1 :           if (loops_for_fn (fun))
     593            1 :             add_nodes_for_loop (diag_node_for_func.get (),
     594              :                                 get_loop (fun, 0),
     595              :                                 fun);
     596              :           else
     597              :             {
     598            0 :               basic_block bb;
     599            0 :               FOR_ALL_BB_FN (bb, fun)
     600            0 :                 add_nodes_for_bb (*diag_node_for_func, bb, fun);
     601              :             }
     602              : 
     603            1 :           add_node (std::move (diag_node_for_func));
     604            1 :         }
     605              :     }
     606              : 
     607              :     /* Superedges.  */
     608              :     int i;
     609              :     superedge *e;
     610           11 :     FOR_EACH_VEC_ELT (m_sg.m_edges, i, e)
     611           10 :       add_edge_for_superedge (*e);
     612            1 :   }
     613              : 
     614              : private:
     615              :   /* Recursively add nodes into PARENT for all the snodes within LOOP
     616              :      and the loops within it.  */
     617              :   void
     618            1 :   add_nodes_for_loop (diagnostics::digraphs::node *parent,
     619              :                       class loop *loop,
     620              :                       function *fun)
     621              :   {
     622            1 :     gcc_assert (parent);
     623            1 :     gcc_assert (loop);
     624            1 :     gcc_assert (fun);
     625              : 
     626            1 :     basic_block *body;
     627            1 :     unsigned int i;
     628              : 
     629            1 :     if (loop->header != NULL
     630            1 :         && loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun))
     631              :       {
     632            0 :         pretty_printer pp;
     633            0 :         pp_printf (&pp, "cluster for loop %d of %d",
     634              :                    loop->num, fun->funcdef_no);
     635              : 
     636            0 :         auto loop_node
     637              :           = std::make_unique<diagnostics::digraphs::node> (*this,
     638            0 :                                                            pp_formatted_text (&pp));
     639            0 :         loop_node->set_logical_loc
     640            0 :           (tree_logical_location_manager::key_from_tree (fun->decl));
     641              : 
     642            0 :         auto new_parent = loop_node.get ();
     643            0 :         parent->add_child (std::move (loop_node));
     644            0 :         parent = new_parent;
     645            0 :       }
     646              : 
     647              :     // Recurse
     648            1 :     for (class loop *inner = loop->inner; inner; inner = inner->next)
     649            0 :       add_nodes_for_loop (parent, inner, fun);
     650              : 
     651            1 :     if (loop->header == nullptr)
     652              :       return;
     653              : 
     654            1 :     if (loop->latch == EXIT_BLOCK_PTR_FOR_FN (cfun))
     655            1 :       body = get_loop_body (loop);
     656              :     else
     657            0 :       body = get_loop_body_in_bfs_order (loop);
     658              : 
     659            6 :     for (i = 0; i < loop->num_nodes; i++)
     660              :       {
     661            5 :         basic_block bb = body[i];
     662            5 :         if (bb->loop_father == loop)
     663            5 :           add_nodes_for_bb (*parent, bb, fun);
     664              :       }
     665              : 
     666            1 :     free (body);
     667              :   }
     668              : 
     669              :   /* Add nodes for all the snodes from BB.  */
     670              :   void
     671            5 :   add_nodes_for_bb (diagnostics::digraphs::node &parent,
     672              :                     basic_block bb,
     673              :                     function *fun)
     674              :   {
     675            5 :     pretty_printer pp;
     676            5 :     const char *funcname = function_name (fun);
     677            5 :     pp_printf (&pp, "%s_bb_%i", funcname, bb->index);
     678            5 :     auto node_for_bb
     679              :       = std::make_unique<diagnostics::digraphs::node> (*this,
     680            5 :                                                        pp_formatted_text (&pp));
     681            5 :     node_for_bb->set_logical_loc
     682            5 :       (tree_logical_location_manager::key_from_tree (fun->decl));
     683              : 
     684              :     // TODO: maybe keep an index per-function/per-bb to speed this up???
     685            5 :     int i;
     686            5 :     supernode *snode;
     687           55 :     FOR_EACH_VEC_ELT (m_sg.m_nodes, i, snode)
     688           50 :       if (snode->m_fun == fun && snode->m_bb == bb)
     689           10 :         add_node_for_supernode (*node_for_bb, *snode);
     690            5 :     parent.add_child (std::move (node_for_bb));
     691            5 :   }
     692              : 
     693              :   void
     694           10 :   add_node_for_supernode (diagnostics::digraphs::node &bb_node,
     695              :                           const supernode &snode)
     696              :   {
     697           10 :     pretty_printer pp;
     698           10 :     snode.dump_dot_id (&pp);
     699           10 :     auto node_for_snode
     700              :       = std::make_unique<diagnostics::digraphs::node> (*this,
     701           10 :                                                        pp_formatted_text (&pp));
     702           10 :     m_node_for_snode.insert ({&snode, node_for_snode.get ()});
     703              : 
     704           10 :     node_for_snode->set_logical_loc
     705           10 :       (tree_logical_location_manager::key_from_tree (snode.m_fun->decl));
     706              :     /* TODO: if we have an m_label, it would be nice to use this as the
     707              :        logical location, within the function.  However SARIF doesn't support
     708              :        this yet (https://github.com/oasis-tcs/sarif-spec/issues/802).  */
     709              : 
     710           10 :     node_for_snode->set_physical_loc (snode.m_loc);
     711              : 
     712           10 :     bb_node.add_child (std::move (node_for_snode));
     713           10 :   }
     714              : 
     715              :   void
     716           10 :   add_edge_for_superedge (const superedge &sedge)
     717              :   {
     718           10 :     diagnostics::digraphs::node *src_node
     719           10 :       = get_node_for_snode (sedge.m_src);
     720           10 :     diagnostics::digraphs::node *dst_node
     721           10 :       = get_node_for_snode (sedge.m_dest);
     722           10 :     label_text desc = sedge.get_description (false);
     723           10 :     add_edge (nullptr, *src_node, *dst_node, desc.get ());
     724           10 :   }
     725              : 
     726              :   diagnostics::digraphs::node *
     727           20 :   get_node_for_snode (const supernode *snode)
     728              :   {
     729           20 :     auto iter = m_node_for_snode.find (snode);
     730           20 :     gcc_assert (iter != m_node_for_snode.end ());
     731           20 :     return iter->second;
     732              :   }
     733              : 
     734              :   const supergraph &m_sg;
     735              :   std::map<const supernode *, diagnostics::digraphs::node *> m_node_for_snode;
     736              : };
     737              : 
     738              : std::unique_ptr<diagnostics::digraphs::digraph>
     739            1 : supergraph::make_diagnostics_digraph () const
     740              : {
     741            1 :   return std::make_unique<supergraph_diagnostic_digraph> (*this);
     742              : }
     743              : 
     744              : /* Return a new json::object of the form
     745              :    {"nodes" : [objs for snodes],
     746              :     "edges" : [objs for sedges]}.  */
     747              : 
     748              : std::unique_ptr<json::object>
     749            0 : supergraph::to_json () const
     750              : {
     751            0 :   auto sgraph_obj = std::make_unique<json::object> ();
     752              : 
     753              :   /* Nodes.  */
     754            0 :   {
     755            0 :     auto nodes_arr = std::make_unique<json::array> ();
     756            0 :     unsigned i;
     757            0 :     supernode *n;
     758            0 :     FOR_EACH_VEC_ELT (m_nodes, i, n)
     759            0 :       nodes_arr->append (n->to_json ());
     760            0 :     sgraph_obj->set ("nodes", std::move (nodes_arr));
     761            0 :   }
     762              : 
     763              :   /* Edges.  */
     764            0 :   {
     765            0 :     auto edges_arr = std::make_unique<json::array> ();
     766            0 :     unsigned i;
     767            0 :     superedge *n;
     768            0 :     FOR_EACH_VEC_ELT (m_edges, i, n)
     769            0 :       edges_arr->append (n->to_json ());
     770            0 :     sgraph_obj->set ("edges", std::move (edges_arr));
     771            0 :   }
     772              : 
     773            0 :   return sgraph_obj;
     774              : }
     775              : 
     776              : /* Create a supernode within BB within FUN and add it to this supergraph.  */
     777              : 
     778              : supernode *
     779       207617 : supergraph::add_node (function *fun, basic_block bb, logger *logger)
     780              : {
     781       207617 :   supernode *n = new supernode (fun, bb, m_next_snode_id++);
     782       207617 :   m_snode_by_id.push_back (n);
     783       207617 :   m_nodes.safe_push (n);
     784       207617 :   if (logger)
     785           82 :     logger->log ("created SN %i", n->m_id);
     786       207617 :   return n;
     787              : }
     788              : 
     789              : void
     790         3573 : supergraph::delete_nodes (const std::set<supernode *> &snodes_to_delete)
     791              : {
     792              :   /* Remove nodes from m_nodes.  */
     793         3573 :   unsigned read_index, write_index;
     794         3573 :   supernode **elem_ptr;
     795       211190 :   VEC_ORDERED_REMOVE_IF
     796              :     (m_nodes, read_index, write_index, elem_ptr,
     797              :      snodes_to_delete.find (*elem_ptr) != snodes_to_delete.end ()
     798         3573 :      );
     799              : 
     800              :   /* Remove nodes from m_snode_by_id, and delete them.  */
     801         9749 :   for (auto iter : snodes_to_delete)
     802              :     {
     803         6176 :       gcc_assert (iter->m_preds.length () == 0);
     804         6176 :       gcc_assert (iter->m_succs.length () == 0);
     805         6176 :       m_snode_by_id[iter->m_id] = nullptr;
     806         6176 :       delete iter;
     807              :     }
     808         3573 : }
     809              : 
     810              : /* Create a chain of nodes and edges in this supergraph from SRC to DEST
     811              :    to handle CFG_EDGE in the underlying CFG:
     812              : 
     813              :     +---+
     814              :     |SRC|
     815              :     +---+
     816              :       |
     817              :       | optional edge for ctrlflow_stmt (if CTRLFLOW_STMT is non-null)
     818              :       | (e.g. checking conditions on a GIMPLE_COND)
     819              :       |
     820              :       V
     821              :    +------+
     822              :    |(node)|
     823              :    +------+
     824              :       |
     825              :       | optional edge for any phi nodes in the destination basic block
     826              :       |
     827              :       V
     828              :    +------+
     829              :    |(node)|
     830              :    +------+
     831              :       |
     832              :       | optional edge for state merging at CFG join points
     833              :       |
     834              :       V
     835              :    +----+
     836              :    |DEST|
     837              :    +----+
     838              : 
     839              :    adding nodes where necessary between the edges, and adding a no-op edge
     840              :    for the case where there is no CTRLFLOW_STMT, phi nodes, or
     841              :    state merging.  */
     842              : 
     843              : void
     844        61079 : supergraph::add_sedges_for_cfg_edge (supernode *src,
     845              :                                      supernode *dest,
     846              :                                      ::edge cfg_edge,
     847              :                                      gimple *ctrlflow_stmt,
     848              :                                      region_model_manager &mgr,
     849              :                                      logger *logger)
     850              : {
     851        61079 :   log_nesting_level sentinel (logger,
     852              :                               "edge: bb %i -> bb %i",
     853        61079 :                               cfg_edge->src->index,
     854        61079 :                               cfg_edge->dest->index);
     855        61079 :   std::unique_ptr<operation> ctrlflow_op;
     856        61079 :   if (ctrlflow_stmt)
     857        19221 :     switch (gimple_code (ctrlflow_stmt))
     858              :       {
     859            0 :       default:
     860            0 :         gcc_unreachable ();
     861        15936 :         break;
     862        15936 :       case GIMPLE_COND:
     863        15936 :         ctrlflow_op = std::make_unique<gcond_edge_op>
     864        31872 :           (cfg_edge,
     865        31872 :            *as_a <gcond *> (ctrlflow_stmt));
     866        15936 :         break;
     867          265 :       case GIMPLE_EH_DISPATCH:
     868          265 :         ctrlflow_op
     869          530 :           = eh_dispatch_edge_op::make (src, dest,
     870              :                                        cfg_edge,
     871          530 :                                        *as_a <geh_dispatch *> (ctrlflow_stmt));
     872          265 :         break;
     873           36 :       case GIMPLE_GOTO:
     874           36 :         {
     875           36 :           ctrlflow_op = std::make_unique<ggoto_edge_op>
     876           72 :             (cfg_edge,
     877           36 :              *as_a <ggoto *> (ctrlflow_stmt),
     878           72 :              dest->m_label);
     879              :         }
     880           36 :         break;
     881         2984 :       case GIMPLE_SWITCH:
     882         2984 :         ctrlflow_op = std::make_unique<switch_case_op>
     883         5968 :           (*src->get_function (),
     884              :            cfg_edge,
     885         2984 :            *as_a <gswitch *> (ctrlflow_stmt),
     886         5968 :            *mgr.get_range_manager ());
     887         2984 :         break;
     888              :       }
     889              :   else
     890              :     {
     891        41858 :       if (cfg_edge->flags & EDGE_ABNORMAL)
     892              :         /* Don't create superedges for such CFG edges (though
     893              :            computed gotos are handled by the GIMPLE_GOTO clause above).  */
     894          124 :         return;
     895              :     }
     896              : 
     897              :   /* Determine a location to use for any snodes within the CFG edge.  */
     898        60955 :   location_t dest_loc = dest->m_loc;
     899        60955 :   if (useful_location_p (cfg_edge->goto_locus))
     900        19951 :     dest_loc = cfg_edge->goto_locus;
     901              : 
     902              :   /* If the dest is a control flow join point, then for each CFG in-edge
     903              :      add an extra snode/sedge before DEST and route to it.
     904              :      We hope this will help state-merging keep the
     905              :      different in-edges separately.  */
     906        60955 :   if (cfg_edge->dest->preds->length () > 1
     907        60955 :       && cfg_edge->dest->index != EXIT_BLOCK)
     908              :     {
     909        17428 :       auto extra_snode = add_node (src->get_function (),
     910              :                                    cfg_edge->dest,
     911              :                                    logger);
     912        17428 :       extra_snode->m_loc = dest_loc;
     913        17428 :       extra_snode->m_preserve_p = true;
     914        17428 :       extra_snode->m_state_merger_node = true;
     915        17428 :       add_edge (new superedge (extra_snode, dest, nullptr, nullptr));
     916        17428 :       dest = extra_snode;
     917              :     }
     918              : 
     919        60955 :   std::unique_ptr<operation> phi_op;
     920        60955 :   if (phi_nodes (cfg_edge->dest))
     921        16441 :     phi_op = phis_for_edge_op::maybe_make (cfg_edge);
     922        60955 :   if (phi_op)
     923              :     {
     924         9652 :       superedge *edge_for_phis_op;
     925         9652 :       if (ctrlflow_op)
     926              :         {
     927              :           /* We have two ops; add an edge for each:
     928              :              SRC --{ctrlflow_op}--> before_phi_nodes --{phi_op}--> DEST.  */
     929         1170 :           supernode *before_phi_nodes
     930         1170 :             = add_node (src->get_function (), cfg_edge->src, logger);
     931         1170 :           before_phi_nodes->m_loc = dest_loc;
     932         1170 :           add_edge (new superedge (src, before_phi_nodes,
     933              :                                    std::move (ctrlflow_op),
     934         2340 :                                    cfg_edge));
     935         2340 :           edge_for_phis_op = new superedge (before_phi_nodes, dest,
     936              :                                             std::move (phi_op),
     937         1170 :                                             cfg_edge);
     938              :         }
     939              :       else
     940              :         /* We just have a phi_op; add: SRC --{phi_op}--> DEST.  */
     941         8482 :         edge_for_phis_op
     942         8482 :           = new superedge (src, dest, std::move (phi_op), cfg_edge);
     943         9652 :       add_edge (edge_for_phis_op);
     944         9652 :       m_edges_for_phis.insert ({cfg_edge, edge_for_phis_op});
     945              :     }
     946              :   else
     947              :     /* We don't have a phi op, create this edge:
     948              :        SRC --{ctrlflow_op}--> DEST
     949              :        where ctrlflow_op might be nullptr (for a no-op edge).  */
     950        51303 :     add_edge (new superedge (src, dest, std::move (ctrlflow_op), cfg_edge));
     951        61109 : }
     952              : 
     953              : // class supernode : public dnode<supergraph_traits>
     954              : 
     955              : /* Implementation of dnode::dump_dot vfunc for supernodes.
     956              : 
     957              :    Write a cluster for the node, and within it a .dot node showing
     958              :    the phi nodes and stmts.  Call into any node annotator from ARGS to
     959              :    potentially add other records to the cluster.  */
     960              : 
     961              : void
     962         1154 : supernode::dump_dot (graphviz_out *gv, const dump_args_t &args) const
     963              : {
     964         1154 :   pretty_printer *pp = gv->get_pp ();
     965              : 
     966         1154 :   gv->write_indent ();
     967         1154 :   dump_dot_id (pp);
     968              : 
     969         1154 :   pp_printf (pp,
     970              :              " [shape=none,margin=0,style=filled,label=<");
     971         1154 :   pp_string (pp, "<TABLE BORDER=\"0\">");
     972         1154 :   pp_write_text_to_stream (pp);
     973              : 
     974         1154 :   pp_printf (pp, "<TR><TD>sn: %i (bb: %i)",
     975         1154 :              m_id, m_bb->index);
     976         1154 :   if (args.m_eg)
     977          191 :     pp_printf (pp, "; scc: %i", args.m_eg->get_scc_id (*this));
     978         1154 :   pp_string (pp, "</TD></TR>");
     979         1154 :   pp_newline (pp);
     980              : 
     981         1154 :   if (m_preserve_p)
     982              :     {
     983          144 :       pp_string (pp, "<TR><TD>(preserve)</TD></TR>");
     984          144 :       pp_newline (pp);
     985              :     }
     986         1154 :   if (m_state_merger_node)
     987              :     {
     988          566 :       pp_string (pp, "<TR><TD BGCOLOR=\"#ec7a08\">"
     989              :                  "<FONT COLOR=\"white\">STATE MERGER</FONT></TD></TR>");
     990          566 :       pp_newline (pp);
     991              :     }
     992         1154 :   if (entry_p ())
     993              :     {
     994           72 :       pp_string (pp, "<TR><TD>ENTRY</TD></TR>");
     995           72 :       pp_newline (pp);
     996              :     }
     997         1082 :   else if (exit_p ())
     998              :     {
     999           72 :       pp_string (pp, "<TR><TD>EXIT</TD></TR>");
    1000           72 :       pp_newline (pp);
    1001              :     }
    1002              : 
    1003              :   /* Source location.  */
    1004              :   /* Highlight nodes where we're missing source location information.
    1005              :      Ideally this all gets fixed up by supergraph::fixup_locations.  */
    1006         1154 :   if (m_loc == UNKNOWN_LOCATION)
    1007           33 :     pp_string (pp, "<TR><TD>UNKNOWN_LOCATION</TD></TR>");
    1008         1121 :   else if (get_pure_location (m_loc) == UNKNOWN_LOCATION)
    1009              :     {
    1010            0 :       pp_printf (pp, "<TR><TD>location: 0x%lx</TD></TR>", m_loc);
    1011            0 :       pp_string (pp, "<TR><TD>UNKNOWN_LOCATION</TD></TR>");
    1012              :     }
    1013              :   else
    1014              :     {
    1015              :       /* Show the source location, but skip it for the case where it's
    1016              :          the same as all previous snodes (and there's a single in-edge).  */
    1017         1121 :       bool show_location = true;
    1018         1121 :       location_t prev_loc = UNKNOWN_LOCATION;
    1019         1121 :       if (m_preds.length () == 1)
    1020              :         {
    1021          977 :           prev_loc = m_preds[0]->m_src->m_loc;
    1022          977 :           if (prev_loc == m_loc)
    1023              :             show_location = false;
    1024              :         }
    1025              :       if (show_location)
    1026              :         {
    1027          870 :           pp_printf (pp, "<TR><TD>location: 0x%lx</TD></TR>", m_loc);
    1028              : 
    1029              :           /* Print changes to the expanded location (or all of it if
    1030              :              we have multiple in-edges).  */
    1031          870 :           auto prev_exploc = expand_location (prev_loc);
    1032          870 :           auto exploc = expand_location (m_loc);
    1033              : 
    1034          870 :           if ((exploc.file != prev_exploc.file)
    1035          177 :               && exploc.file)
    1036              :             {
    1037          177 :               pp_string (pp, "<TR><TD>");
    1038          177 :               pp_flush (pp);
    1039          177 :               pp_printf (pp, "%s", exploc.file);
    1040              :               /* Escape, to handle cases like "<built-in>".  */
    1041          177 :               pp_write_text_as_html_like_dot_to_stream (pp);
    1042          177 :               pp_printf (pp, ":%i:</TD></TR>", exploc.line);
    1043              :             }
    1044          870 :           if (exploc.line != prev_exploc.line)
    1045         1120 :             if (const diagnostics::char_span line
    1046          560 :                 = global_dc->get_file_cache ().get_source_line (exploc.file,
    1047          560 :                                                                 exploc.line))
    1048              :               {
    1049              :                 /* Print the source line.  */
    1050          560 :                 pp_string (pp, "<TR><TD>");
    1051          560 :                 pp_string (pp, "<TABLE BORDER=\"0\"><TR>");
    1052              : 
    1053              :                 // Line number:
    1054          560 :                 pp_printf (pp, ("<TD ALIGN=\"RIGHT\" BGCOLOR=\"#0088ce\">"
    1055              :                                 "<FONT COLOR=\"white\"> %i</FONT></TD>"),
    1056              :                            exploc.line);
    1057              : 
    1058              :                 // Line contents:
    1059          560 :                 pp_string (pp, "<TD ALIGN=\"LEFT\" BGCOLOR=\"white\">");
    1060          560 :                 pp_flush (pp);
    1061        12562 :                 for (size_t i = 0; i < line.length (); ++i)
    1062        11442 :                   pp_character (pp, line[i]);
    1063          560 :                 pp_write_text_as_html_like_dot_to_stream (pp);
    1064          560 :                 pp_string (pp, "</TD>");
    1065              : 
    1066          560 :                 pp_string (pp, "</TR></TABLE>");
    1067          560 :                 pp_string (pp, "</TD></TR>");
    1068              :               }
    1069              :         }
    1070              :     }
    1071              : 
    1072         1154 :   pp_flush (pp);
    1073              : 
    1074         1154 :   if (args.m_node_annotator)
    1075          382 :     args.m_node_annotator->add_node_annotations (gv, *this);
    1076              : 
    1077         1154 :   pp_printf (pp, "<TR><TD>m_stmt_loc: 0x%lx</TD></TR>", m_stmt_loc);
    1078              : 
    1079         1154 :   pp_string (pp, "</TABLE>>];\n\n");
    1080         1154 :   pp_flush (pp);
    1081         1154 : }
    1082              : 
    1083              : /* Write an ID for this node to PP, for use in .dot output.  */
    1084              : 
    1085              : void
    1086         4380 : supernode::dump_dot_id (pretty_printer *pp) const
    1087              : {
    1088         4380 :   pp_printf (pp, "supernode_%i", m_id);
    1089         4380 : }
    1090              : 
    1091              : /* Return a new json::object of the form
    1092              :    {"id": int,
    1093              :     "fun": optional str
    1094              :     "bb_idx": int}.  */
    1095              : 
    1096              : std::unique_ptr<json::object>
    1097            0 : supernode::to_json () const
    1098              : {
    1099            0 :   auto snode_obj = std::make_unique<json::object> ();
    1100              : 
    1101            0 :   snode_obj->set_integer ("id", m_id);
    1102            0 :   snode_obj->set_integer ("bb_idx", m_bb->index);
    1103            0 :   if (function *fun = get_function ())
    1104            0 :     snode_obj->set_string ("fun", function_name (fun));
    1105              : 
    1106            0 :   return snode_obj;
    1107              : }
    1108              : 
    1109              : /* Dump this superedge to PP.  */
    1110              : 
    1111              : void
    1112            0 : superedge::dump (pretty_printer *pp) const
    1113              : {
    1114            0 :   pp_printf (pp, "edge: SN: %i -> SN: %i", m_src->m_id, m_dest->m_id);
    1115            0 :   label_text desc (get_description (false));
    1116            0 :   if (strlen (desc.get ()) > 0)
    1117              :     {
    1118            0 :       pp_space (pp);
    1119            0 :       pp_string (pp, desc.get ());
    1120              :     }
    1121            0 : }
    1122              : 
    1123              : /* Dump this superedge to stderr.  */
    1124              : 
    1125              : DEBUG_FUNCTION void
    1126            0 : superedge::dump () const
    1127              : {
    1128            0 :   tree_dump_pretty_printer pp (stderr);
    1129            0 :   dump (&pp);
    1130            0 :   pp_newline (&pp);
    1131            0 : }
    1132              : 
    1133              : /* Implementation of dedge::dump_dot for superedges.
    1134              :    Write a .dot edge to GV representing this superedge.  */
    1135              : 
    1136              : void
    1137         1154 : superedge::dump_dot (graphviz_out *gv, const dump_args_t &) const
    1138              : {
    1139         1154 :   const char *style = "\"solid,bold\"";
    1140         1154 :   const char *color = "black";
    1141         1154 :   int weight = 10;
    1142         1154 :   const char *constraint = "true";
    1143              : 
    1144              :   /* Adapted from graph.cc:draw_cfg_node_succ_edges.  */
    1145         1154 :   if (::edge cfg_edge = get_any_cfg_edge ())
    1146              :     {
    1147          422 :       if (cfg_edge->flags & EDGE_FAKE)
    1148              :         {
    1149              :           style = "dotted";
    1150              :           color = "green";
    1151              :           weight = 0;
    1152              :         }
    1153          422 :       else if (cfg_edge->flags & EDGE_DFS_BACK)
    1154              :         {
    1155              :           style = "\"dotted,bold\"";
    1156              :           color = "blue";
    1157              :           weight = 10;
    1158              :         }
    1159          374 :       else if (cfg_edge->flags & EDGE_FALLTHRU)
    1160              :         {
    1161          182 :           color = "blue";
    1162          182 :           weight = 100;
    1163              :         }
    1164              : 
    1165          422 :       if (cfg_edge->flags & EDGE_ABNORMAL)
    1166            0 :         color = "red";
    1167              :     }
    1168              : 
    1169         1154 :   gv->write_indent ();
    1170              : 
    1171         1154 :   pretty_printer *pp = gv->get_pp ();
    1172              : 
    1173         1154 :   m_src->dump_dot_id (pp);
    1174         1154 :   pp_string (pp, " -> ");
    1175         1154 :   m_dest->dump_dot_id (pp);
    1176         1154 :   pp_printf (pp,
    1177              :              (" [style=%s, color=%s, weight=%d, constraint=%s,"
    1178              :               " headlabel=\""),
    1179              :              style, color, weight, constraint);
    1180         1154 :   pp_flush (pp);
    1181              : 
    1182         1154 :   dump_label_to_pp (pp, false);
    1183         1154 :   pp_write_text_as_dot_label_to_stream (pp, false);
    1184              : 
    1185         1154 :   pp_printf (pp, "\"];\n");
    1186         1154 : }
    1187              : 
    1188              : /* Return a new json::object of the form
    1189              :    {"src_id": int, the index of the source supernode,
    1190              :     "dst_id": int, the index of the destination supernode} */
    1191              : 
    1192              : std::unique_ptr<json::object>
    1193           51 : superedge::to_json () const
    1194              : {
    1195           51 :   auto sedge_obj = std::make_unique<json::object> ();
    1196           51 :   sedge_obj->set_integer ("src_id", m_src->m_id);
    1197           51 :   sedge_obj->set_integer ("dst_id", m_dest->m_id);
    1198           51 :   return sedge_obj;
    1199              : }
    1200              : 
    1201              : /* Return true iff this edge needs to be preserved during simplification.  */
    1202              : 
    1203              : bool
    1204       180054 : superedge::preserve_p () const
    1205              : {
    1206       180054 :   if (m_cfg_edge)
    1207        46700 :     if (m_cfg_edge->flags & (EDGE_EH | EDGE_DFS_BACK))
    1208              :       {
    1209              :         /* We use EDGE_EH in get_eh_outedge, and EDGE_DFS_BACK
    1210              :            for detecting infinite loops.  */
    1211         1799 :         return true;
    1212              :       }
    1213              :   return false;
    1214              : }
    1215              : 
    1216              : /* Build a description of this superedge (e.g. "true" for the true
    1217              :    edge of a conditional, or "case 42:" for a switch case).
    1218              : 
    1219              :    If USER_FACING is false, the result also contains any underlying
    1220              :    CFG edge flags. e.g. " (flags FALLTHRU | DFS_BACK)".  */
    1221              : 
    1222              : label_text
    1223         7516 : superedge::get_description (bool user_facing) const
    1224              : {
    1225         7516 :   pretty_printer pp;
    1226         7516 :   pp_format_decoder (&pp) = default_tree_printer;
    1227         7516 :   dump_label_to_pp (&pp, user_facing);
    1228         7516 :   return label_text::take (xstrdup (pp_formatted_text (&pp)));
    1229         7516 : }
    1230              : 
    1231              : void
    1232         9233 : superedge::dump_label_to_pp (pretty_printer *pp, bool user_facing) const
    1233              : {
    1234         9233 :   if (get_op ())
    1235         8685 :     get_op ()->print_as_edge_label (pp, user_facing);
    1236              :   else
    1237          548 :     pp_printf (pp, "no-op");
    1238              : 
    1239         9233 :   if (user_facing)
    1240              :     return;
    1241              : 
    1242         1887 :   if (::edge cfg_edge = get_any_cfg_edge ())
    1243              :     {
    1244          640 :       if (cfg_edge->flags)
    1245              :         {
    1246          530 :           pp_string (pp, " (flags ");
    1247          530 :           bool seen_flag = false;
    1248              : #define DEF_EDGE_FLAG(NAME,IDX)                 \
    1249              :           do {                                          \
    1250              :             if (cfg_edge->flags & EDGE_##NAME)           \
    1251              :               {                                         \
    1252              :                 if (seen_flag)                                  \
    1253              :                   pp_string (pp, " | ");                      \
    1254              :                 pp_printf (pp, "%s", (#NAME));                        \
    1255              :                 seen_flag = true;                               \
    1256              :               }                                         \
    1257              :           } while (0);
    1258              : #include "cfg-flags.def"
    1259              : #undef DEF_EDGE_FLAG
    1260          530 :           pp_string (pp, ")");
    1261              :         }
    1262          640 :       if (cfg_edge->goto_locus > BUILTINS_LOCATION)
    1263          256 :         pp_printf (pp, " (has goto_locus: 0x%lx)", cfg_edge->goto_locus);
    1264              :     }
    1265              : }
    1266              : 
    1267              : bool
    1268       118918 : superedge::supports_bulk_merge_p () const
    1269              : {
    1270       118918 :   if (!m_op)
    1271              :     return true;
    1272        64771 :   return m_op->supports_bulk_merge_p ();
    1273              : }
    1274              : 
    1275              : } // namespace ana
    1276              : 
    1277              : #endif /* #if ENABLE_ANALYZER */
        

Generated by: LCOV version 2.5.0-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.