LCOV - code coverage report
Current view: top level - gcc - hierarchical_discriminator.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.3 % 128 122
Test Date: 2026-12-05 16:10:02 Functions: 100.0 % 8 8
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Copyright The GNU Toolchain Authors
       2              : 
       3              : This file is part of GCC.
       4              : 
       5              : GCC is free software; you can redistribute it and/or modify it under
       6              : the terms of the GNU General Public License as published by the Free
       7              : Software Foundation; either version 3, or (at your option) any later
       8              : version.
       9              : 
      10              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      11              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      13              : for more details.
      14              : 
      15              : You should have received a copy of the GNU General Public License
      16              : along with GCC; see the file COPYING3.  If not see
      17              : <http://www.gnu.org/licenses/>.  */
      18              : 
      19              : #include "config.h"
      20              : #include "system.h"
      21              : #include "coretypes.h"
      22              : #include "backend.h"
      23              : #include "tree.h"
      24              : #include "gimple.h"
      25              : #include "tree-pass.h"
      26              : #include "ssa.h"
      27              : #include "gimple-iterator.h"
      28              : #include "tree-cfg.h"
      29              : #include "cfgloop.h"
      30              : #include "hierarchical_discriminator.h"
      31              : #include "cfghooks.h"
      32              : #include "diagnostic-core.h"
      33              : #include "function.h"
      34              : #include "rtl.h"
      35              : #include "basic-block.h"
      36              : 
      37              : /* Helper to update a location with new discriminator components.  If
      38              :    multiplicity_factor is non-zero, multiply existing multiplicity.
      39              :    If copyid is non-zero, set it (otherwise preserve existing).  */
      40              : 
      41              : static location_t
      42     11773915 : update_location_discriminator (location_t loc,
      43              :                                unsigned int multiplicity_factor,
      44              :                                unsigned int copyid)
      45              : {
      46     11773915 :   if (loc == UNKNOWN_LOCATION)
      47              :     return loc;
      48              : 
      49     11773915 :   discriminator_components comp = get_discriminator_components_from_loc (loc);
      50              : 
      51              :   /* Multiply existing multiplicity if requested.  */
      52     11773915 :   if (multiplicity_factor > 0)
      53              :     {
      54            2 :       unsigned int new_mult = (comp.multiplicity == 0)
      55       629058 :         ? multiplicity_factor
      56              :         : comp.multiplicity * multiplicity_factor;
      57       629058 :       if (new_mult > DISCR_MULTIPLICITY_MAX)
      58              :         new_mult = DISCR_MULTIPLICITY_MAX;
      59       629058 :       comp.multiplicity = new_mult;
      60              :     }
      61              : 
      62              :   /* Update copyid if requested.  */
      63     11773915 :   if (copyid > 0)
      64     11773915 :     comp.copyid = copyid;
      65              : 
      66     11773915 :   return location_with_discriminator_components (loc, comp);
      67              : }
      68              : 
      69              : /* Assign discriminators to a statement
      70              :    Updates the multiplicity and/or copyid discriminator components for
      71              :    all statements in the given basic block, while preserving the base
      72              :    discriminator.
      73              : 
      74              :    If multiplicity_factor > 0, multiply existing multiplicity by this factor.
      75              :    If copyid > 0, set it to this value.  */
      76              : 
      77              : void
      78        33728 : assign_discriminators_to_stmt (gimple* stmt,
      79              :                                unsigned int multiplicity_factor,
      80              :                                unsigned int copyid)
      81              : {
      82        33728 :   location_t loc = gimple_location (stmt);
      83              : 
      84        33728 :   if (loc != UNKNOWN_LOCATION)
      85              :     {
      86        33719 :       location_t new_loc
      87        33719 :         = update_location_discriminator (loc,
      88              :                                          multiplicity_factor,
      89              :                                          copyid);
      90        67438 :       gimple_set_location (stmt, new_loc);
      91              :     }
      92        33728 : }
      93              : 
      94              : 
      95              : /* Assign discriminators to all statements in a basic block.  This
      96              :    function updates the multiplicity and/or copyid discriminator components for
      97              :    all statements in the given basic block, while preserving the base
      98              :    discriminator.
      99              : 
     100              :    If multiplicity_factor > 0, multiply existing multiplicity by this factor.
     101              :    If copyid > 0, set it to this value.  */
     102              : 
     103              : void
     104      4740988 : assign_discriminators_to_bb (basic_block bb,
     105              :                              unsigned int multiplicity_factor,
     106              :                              unsigned int copyid)
     107              : {
     108      4740988 :   if (current_ir_type () != IR_GIMPLE)
     109              :     {
     110       713746 :       rtx_insn *insn;
     111      5438738 :       FOR_BB_INSNS (bb, insn)
     112              :         {
     113      4724992 :           if (!INSN_P (insn) || !insn->has_location ())
     114      1644964 :             continue;
     115      3080028 :           location_t loc = insn->location ();
     116      3080028 :           insn->set_location
     117      3080028 :             (update_location_discriminator (loc, multiplicity_factor,
     118              :                                                      copyid));
     119              :         }
     120       713746 :       return;
     121              :     }
     122              : 
     123      4027242 :   gimple_stmt_iterator gsi;
     124      4027242 :   gphi_iterator phi_gsi;
     125      4027242 :   edge e;
     126      4027242 :   edge_iterator ei;
     127              : 
     128              :   /* Update PHI statements.  */
     129      9403030 :   for (phi_gsi = gsi_start_phis (bb); !gsi_end_p (phi_gsi);
     130      5375788 :        gsi_next (&phi_gsi))
     131              :     {
     132      5375788 :       gphi *phi = phi_gsi.phi ();
     133      5375788 :       location_t loc = gimple_location (phi);
     134              : 
     135      5375788 :       if (loc != UNKNOWN_LOCATION)
     136              :         {
     137            0 :           location_t new_loc
     138            0 :             = update_location_discriminator (loc,
     139              :                                              multiplicity_factor,
     140              :                                              copyid);
     141            0 :           gimple_set_location (phi, new_loc);
     142              :         }
     143              :     }
     144              : 
     145              :   /* Update regular statements.  */
     146     32648011 :   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
     147              :     {
     148     24593527 :       gimple *stmt = gsi_stmt (gsi);
     149              : 
     150     24593527 :       if (is_gimple_debug (stmt))
     151     15515770 :         continue;
     152              : 
     153      9077757 :       location_t loc = gimple_location (stmt);
     154      9077757 :       if (loc != UNKNOWN_LOCATION)
     155              :         {
     156      8652635 :           location_t new_loc
     157      8652635 :             = update_location_discriminator (loc,
     158              :                                              multiplicity_factor,
     159              :                                              copyid);
     160     17305270 :           gimple_set_location (stmt, new_loc);
     161              :         }
     162              :     }
     163              : 
     164              :   /* Update goto/edge locations.  */
     165     11332139 :   FOR_EACH_EDGE (e, ei, bb->succs)
     166              :     {
     167      7304897 :       location_t loc = e->goto_locus;
     168      7304897 :       if (loc != UNKNOWN_LOCATION)
     169              :         {
     170         7533 :           location_t new_loc
     171         7533 :             = update_location_discriminator (loc,
     172              :                                              multiplicity_factor,
     173              :                                              copyid);
     174         7533 :           e->goto_locus = new_loc;
     175              :         }
     176              :     }
     177              : }
     178              : 
     179              : /* Assign discriminators to all basic blocks in a loop.  This function is
     180              :    used by loop versioning passes to assign version IDs and vectorization
     181              :    factors to all statements in a loop version.  */
     182              : 
     183              : void
     184        54479 : assign_discriminators_to_loop (class loop *loop,
     185              :                                unsigned int multiplicity_factor,
     186              :                                unsigned int copyid)
     187              : {
     188        54479 :   basic_block *bbs;
     189        54479 :   unsigned int i;
     190              : 
     191              :   /* Get all basic blocks in the loop.  */
     192        54479 :   bbs = get_loop_body (loop);
     193              : 
     194              :   /* Assign discriminators to all blocks in the loop.  */
     195       220129 :   for (i = 0; i < loop->num_nodes; i++)
     196       111171 :     assign_discriminators_to_bb (bbs[i], multiplicity_factor, copyid);
     197              : 
     198        54479 :   free (bbs);
     199        54479 : }
     200              : 
     201              : 
     202              : /* Helper to update the copyid allocator map with a location's existing copyid.
     203              :    If the location has a non-zero copyid, record that we need to start
     204              :    allocating from copyid+1 for this location.  */
     205              : 
     206              : static void
     207     91617823 : record_existing_copyid (location_t loc, struct function *fn)
     208              : {
     209     91617823 :   if (loc == UNKNOWN_LOCATION)
     210     91601503 :     return;
     211              : 
     212     66736468 :   location_t pure_loc = get_pure_location (loc);
     213     66736468 :   unsigned int discr = get_discriminator_from_loc (loc);
     214     66736468 :   if (discr == 0)
     215              :     return;
     216              : 
     217              :   /* Extract copyid from discriminator.  */
     218     24361178 :   unsigned int copyid = (discr >> DISCR_COPYID_SHIFT) & DISCR_COPYID_MASK;
     219     24361178 :   if (copyid == 0)
     220              :     return;
     221              : 
     222              :   /* Update max copyid for this location.  */
     223        16320 :   unsigned int next_copyid = copyid + 1;
     224        16320 :   if (next_copyid > DISCR_COPYID_MAX)
     225            0 :     next_copyid = DISCR_COPYID_MAX;
     226              : 
     227        16320 :   unsigned int *existing = fn->copyid_alloc->location_map->get (pure_loc);
     228        16320 :   if (!existing || *existing <= copyid)
     229         9256 :     fn->copyid_alloc->location_map->put (pure_loc, next_copyid);
     230              : }
     231              : 
     232              : /* Initialize the per-function copyid allocator.  Walks the function
     233              :    body to find existing max copyids per location.  */
     234              : 
     235              : static void
     236      3144197 : init_copyid_allocator (struct function *fn)
     237              : {
     238      3144197 :   if (!fn)
     239              :     return;
     240              : 
     241      3144197 :   if (fn->copyid_alloc && fn->copyid_alloc->initialized)
     242              :     return;  /* Already initialized.  */
     243              : 
     244       388032 :   if (!fn->copyid_alloc)
     245              :     {
     246       388032 :       fn->copyid_alloc = XNEW (struct copyid_allocator);
     247       776064 :       fn->copyid_alloc->location_map
     248       388032 :         = new hash_map<int_hash<location_t, UNKNOWN_LOCATION,
     249              :                                UNKNOWN_LOCATION>, unsigned int>;
     250       388032 :       fn->copyid_alloc->initialized = false;
     251              :     }
     252              : 
     253              :   /* Only walk the body if not yet initialized.  */
     254       388032 :   if (fn->copyid_alloc->initialized)
     255              :     return;
     256              : 
     257              :   /* Walk the function body to find existing max copyids per location.
     258              :      This ensures we don't reuse copyids that were allocated in previous
     259              :      passes, during LTO, or brought in by inlining.  */
     260       388032 :   basic_block bb;
     261     11219599 :   FOR_EACH_BB_FN (bb, fn)
     262              :     {
     263     10831567 :       if (current_ir_type () == IR_GIMPLE)
     264              :         {
     265              :           /* Process PHI nodes.  */
     266     10142391 :           gphi_iterator phi_gsi;
     267     14094281 :           for (phi_gsi = gsi_start_phis (bb); !gsi_end_p (phi_gsi);
     268      3951890 :                gsi_next (&phi_gsi))
     269      3951890 :             record_existing_copyid (gimple_location (phi_gsi.phi ()), fn);
     270              : 
     271              :           /* Process regular statements.  */
     272     10142391 :           gimple_stmt_iterator gsi;
     273     89013887 :           for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
     274     68729105 :             record_existing_copyid (gimple_location (gsi_stmt (gsi)), fn);
     275              : 
     276              :           /* Process edge goto_locus locations.  */
     277     10142391 :           edge e;
     278     10142391 :           edge_iterator ei;
     279     24952740 :           FOR_EACH_EDGE (e, ei, bb->succs)
     280     14810349 :             record_existing_copyid (e->goto_locus, fn);
     281              :         }
     282              :       else
     283              :         {
     284              :           /* For RTL mode.  */
     285       689176 :           rtx_insn *insn;
     286      6345502 :           FOR_BB_INSNS (bb, insn)
     287              :             {
     288      5656326 :               if (INSN_P (insn))
     289      4126479 :                 record_existing_copyid (insn->location (), fn);
     290              :             }
     291              :         }
     292              :     }
     293              : 
     294       388032 :   fn->copyid_alloc->initialized = true;
     295              : }
     296              : 
     297              : /* Allocate a unique copy_id base for the given location.
     298              :    STRIDE indicates how many copy_ids to reserve (for unrolling N times,
     299              :    use stride=N).  Returns the base copy_id.  */
     300              : 
     301              : unsigned int
     302      3144197 : allocate_copyid_base (location_t loc, unsigned int stride)
     303              : {
     304              :   /* Need current function context.  */
     305      3144197 :   if (!cfun)
     306              :     return 1;  /* No function context, return default.  */
     307              : 
     308      3144197 :   init_copyid_allocator (cfun);
     309              : 
     310      3144197 :   loc = get_pure_location (loc);
     311              : 
     312              :   /* Cannot allocate copyid for unknown location.  */
     313      3144197 :   if (loc == UNKNOWN_LOCATION)
     314              :     return 1;  /* Return default base copyid.  */
     315              : 
     316              :   /* Check if this location has been seen before.  */
     317      3143976 :   unsigned int *existing = cfun->copyid_alloc->location_map->get (loc);
     318      3143976 :   if (existing)
     319              :     {
     320              :       /* Location already duplicated before.  Allocate next copy_id for it.  */
     321      1886610 :       unsigned int base = *existing;
     322      1886610 :       *existing += stride;  /* Update for next duplication.  */
     323              : 
     324              :       /* Clamp to maximum.  */
     325      1886610 :       if (*existing > DISCR_COPYID_MAX)
     326            0 :         *existing = DISCR_COPYID_MAX;
     327              :       return base;
     328              :     }
     329              :   else
     330              :     {
     331              :       /* First duplication at this location in this function.
     332              :          Start at 1 (not 0, which means "no copyid").  */
     333      1257366 :       unsigned int base = 1;
     334      1257366 :       unsigned int next = base + stride;
     335      1257366 :       if (next > DISCR_COPYID_MAX)
     336            0 :         next = DISCR_COPYID_MAX;
     337              :       /* Record this location for future duplications.  */
     338      1257366 :       cfun->copyid_alloc->location_map->put (loc, next);
     339              : 
     340      1257366 :       return base;
     341              :     }
     342              : }
     343              : 
     344              : /* Free the copy_id allocator for a function.  Called when the function
     345              :    is being destroyed.  */
     346              : 
     347              : void
     348      1763786 : free_copyid_allocator (struct function *fn)
     349              : {
     350      1763786 :   if (fn && fn->copyid_alloc)
     351              :     {
     352       685696 :       delete fn->copyid_alloc->location_map;
     353       342848 :       XDELETE (fn->copyid_alloc);
     354       342848 :       fn->copyid_alloc = NULL;
     355              :     }
     356      1763786 : }
        

Generated by: LCOV version 2.6-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.