LCOV - code coverage report
Current view: top level - gcc - dce.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 91.8 % 538 494
Test Date: 2026-11-14 16:39:50 Functions: 100.0 % 34 34
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* RTL dead code elimination.
       2              :    Copyright (C) 2005-2026 Free Software Foundation, Inc.
       3              : 
       4              : This file is part of GCC.
       5              : 
       6              : GCC is free software; you can redistribute it and/or modify it under
       7              : the terms of the GNU General Public License as published by the Free
       8              : Software Foundation; either version 3, or (at your option) any later
       9              : version.
      10              : 
      11              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      12              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14              : for more details.
      15              : 
      16              : You should have received a copy of the GNU General Public License
      17              : along with GCC; see the file COPYING3.  If not see
      18              : <http://www.gnu.org/licenses/>.  */
      19              : 
      20              : #include "config.h"
      21              : #include "system.h"
      22              : #include "coretypes.h"
      23              : #include "backend.h"
      24              : #include "rtl.h"
      25              : #include "tree.h"
      26              : #include "predict.h"
      27              : #include "df.h"
      28              : #include "memmodel.h"
      29              : #include "tm_p.h"
      30              : #include "emit-rtl.h"  /* FIXME: Can go away once crtl is moved to rtl.h.  */
      31              : #include "cfgrtl.h"
      32              : #include "cfgbuild.h"
      33              : #include "cfgcleanup.h"
      34              : #include "dce.h"
      35              : #include "valtrack.h"
      36              : #include "tree-pass.h"
      37              : #include "dbgcnt.h"
      38              : #include "rtl-iter.h"
      39              : 
      40              : 
      41              : /* -------------------------------------------------------------------------
      42              :    Core mark/delete routines
      43              :    ------------------------------------------------------------------------- */
      44              : 
      45              : /* True if we are invoked while the df engine is running; in this case,
      46              :    we don't want to reenter it.  */
      47              : static bool df_in_progress = false;
      48              : 
      49              : /* True if we are allowed to alter the CFG in this pass.  */
      50              : static bool can_alter_cfg = false;
      51              : 
      52              : /* Instructions that have been marked but whose dependencies have not
      53              :    yet been processed.  */
      54              : static vec<rtx_insn *> worklist;
      55              : 
      56              : /* Bitmap of instructions marked as needed indexed by INSN_UID.  */
      57              : static sbitmap marked;
      58              : 
      59              : /* Bitmap obstacks used for block processing by the fast algorithm.  */
      60              : static bitmap_obstack dce_blocks_bitmap_obstack;
      61              : static bitmap_obstack dce_tmp_bitmap_obstack;
      62              : 
      63              : static bool find_call_stack_args (rtx_call_insn *, bool, bool, bitmap);
      64              : 
      65              : /* A subroutine for which BODY is part of the instruction being tested;
      66              :    either the top-level pattern, or an element of a PARALLEL.  The
      67              :    instruction is known not to be a bare USE or CLOBBER.  */
      68              : 
      69              : static bool
      70    943321942 : deletable_insn_p_1 (rtx body)
      71              : {
      72    943321942 :   switch (GET_CODE (body))
      73              :     {
      74              :     case PREFETCH:
      75              :     case TRAP_IF:
      76              :       /* The UNSPEC case was added here because the ia-64 claims that
      77              :          USEs do not work after reload and generates UNSPECS rather
      78              :          than USEs.  Since dce is run after reload we need to avoid
      79              :          deleting these even if they are dead.  If it turns out that
      80              :          USEs really do work after reload, the ia-64 should be
      81              :          changed, and the UNSPEC case can be removed.  */
      82              :     case UNSPEC:
      83              :       return false;
      84              : 
      85    942958960 :     default:
      86    942958960 :       return !volatile_refs_p (body);
      87              :     }
      88              : }
      89              : 
      90              : /* Don't delete calls that may throw if we cannot do so.  */
      91              : 
      92              : static bool
      93      2166701 : can_delete_call (rtx_insn *insn)
      94              : {
      95      2166701 :   if (cfun->can_delete_dead_exceptions && can_alter_cfg)
      96              :     return true;
      97      1529228 :   if (!insn_nothrow_p (insn))
      98              :     return false;
      99      1521798 :   if (can_alter_cfg)
     100              :     return true;
     101              :   /* If we can't alter cfg, even when the call can't throw exceptions, it
     102              :      might have EDGE_ABNORMAL_CALL edges and so we shouldn't delete such
     103              :      calls.  */
     104      1400396 :   gcc_assert (CALL_P (insn));
     105      1400396 :   if (BLOCK_FOR_INSN (insn) && BB_END (BLOCK_FOR_INSN (insn)) == insn)
     106              :     {
     107          715 :       edge e;
     108          715 :       edge_iterator ei;
     109              : 
     110         1184 :       FOR_EACH_EDGE (e, ei, BLOCK_FOR_INSN (insn)->succs)
     111          715 :         if ((e->flags & EDGE_ABNORMAL_CALL) != 0)
     112          246 :           return false;
     113              :     }
     114              :   return true;
     115              : }
     116              : 
     117              : /* Return true if INSN is a normal instruction that can be deleted by
     118              :    the DCE pass.  */
     119              : 
     120              : static bool
     121   1012880301 : deletable_insn_p (rtx_insn *insn, bool fast, bitmap arg_stores)
     122              : {
     123   1012880301 :   rtx body, x;
     124   1012880301 :   int i;
     125   1012880301 :   df_ref def;
     126              : 
     127   1012880301 :   auto call = dyn_cast<rtx_call_insn *> (insn);
     128     76517215 :   if (call
     129              :       /* We cannot delete calls inside of the recursive dce because
     130              :          this may cause basic blocks to be deleted and this messes up
     131              :          the rest of the stack of optimization passes.  */
     132     76517215 :       && (!df_in_progress)
     133              :       /* We cannot delete pure or const sibling calls because it is
     134              :          hard to see the result.  */
     135     14181637 :       && (!SIBLING_CALL_P (call))
     136              :       /* We can delete dead const or pure calls as long as they do not
     137              :          infinite loop.  */
     138     13889833 :       && (RTL_CONST_OR_PURE_CALL_P (call)
     139      1181405 :           && !RTL_LOOPING_CONST_OR_PURE_CALL_P (call))
     140              :       /* Don't delete calls that may throw if we cannot do so.  */
     141      1085173 :       && can_delete_call (call))
     142      1081335 :     return find_call_stack_args (call, false, fast, arg_stores);
     143              : 
     144              :   /* Don't delete jumps, notes and the like.  */
     145   1011798966 :   if (!NONJUMP_INSN_P (insn))
     146              :     return false;
     147              : 
     148              :   /* Don't delete insns that may throw if we cannot do so.  */
     149    475168268 :   if (!(cfun->can_delete_dead_exceptions && can_alter_cfg)
     150   1281616521 :       && !insn_nothrow_p (insn))
     151              :     return false;
     152              : 
     153              :   /* If INSN sets a global_reg, leave it untouched.  */
     154   1627014205 :   FOR_EACH_INSN_DEF (def, insn)
     155    821628912 :     if (HARD_REGISTER_NUM_P (DF_REF_REGNO (def))
     156    547401514 :         && global_regs[DF_REF_REGNO (def)])
     157              :       return false;
     158              :     /* Initialization of pseudo PIC register should never be removed.  */
     159    821628308 :     else if (DF_REF_REG (def) == pic_offset_table_rtx
     160    821628308 :              && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
     161              :       return false;
     162              : 
     163              :   /* Callee-save restores are needed.  */
     164    805385293 :   if (RTX_FRAME_RELATED_P (insn)
     165     12556697 :       && crtl->shrink_wrapped_separate
     166    807750336 :       && find_reg_note (insn, REG_CFA_RESTORE, NULL))
     167              :     return false;
     168              : 
     169    804617407 :   body = PATTERN (insn);
     170    804617407 :   switch (GET_CODE (body))
     171              :     {
     172              :     case USE:
     173              :     case VAR_LOCATION:
     174              :       return false;
     175              : 
     176      1041960 :     case CLOBBER:
     177      1041960 :       if (fast)
     178              :         {
     179              :           /* A CLOBBER of a dead pseudo register serves no purpose.
     180              :              That is not necessarily true for hard registers until
     181              :              after reload.  */
     182       843742 :           x = XEXP (body, 0);
     183       843742 :           return REG_P (x) && (!HARD_REGISTER_P (x) || reload_completed);
     184              :         }
     185              :       else
     186              :         /* Because of the way that use-def chains are built, it is not
     187              :            possible to tell if the clobber is dead because it can
     188              :            never be the target of a use-def chain.  */
     189              :         return false;
     190              : 
     191    138580251 :     case PARALLEL:
     192    421251694 :       for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
     193    284645010 :         if (!deletable_insn_p_1 (XVECEXP (body, 0, i)))
     194              :           return false;
     195              :       return true;
     196              : 
     197    658676932 :     default:
     198    658676932 :       return deletable_insn_p_1 (body);
     199              :     }
     200              : }
     201              : 
     202              : 
     203              : /* Return true if INSN has been marked as needed.  */
     204              : 
     205              : static inline int
     206   3034166810 : marked_insn_p (rtx_insn *insn)
     207              : {
     208              :   /* Artificial defs are always needed and they do not have an insn.
     209              :      We should never see them here.  */
     210   3034166810 :   gcc_assert (insn);
     211   3034166810 :   return bitmap_bit_p (marked, INSN_UID (insn));
     212              : }
     213              : 
     214              : 
     215              : /* If INSN has not yet been marked as needed, mark it now, and add it to
     216              :    the worklist.  */
     217              : 
     218              : static void
     219   1053948355 : mark_insn (rtx_insn *insn, bool fast)
     220              : {
     221   1053948355 :   if (!marked_insn_p (insn))
     222              :     {
     223   1009644381 :       if (!fast)
     224     56450714 :         worklist.safe_push (insn);
     225   1009644381 :       bitmap_set_bit (marked, INSN_UID (insn));
     226   1009644381 :       if (dump_file)
     227        12446 :         fprintf (dump_file, "  Adding insn %d to worklist\n", INSN_UID (insn));
     228   1009644381 :       auto call = dyn_cast<rtx_call_insn *> (insn);
     229     76513570 :       if (call
     230     76513570 :           && !df_in_progress
     231     14177992 :           && !SIBLING_CALL_P (call)
     232     13886188 :           && (RTL_CONST_OR_PURE_CALL_P (call)
     233      1177760 :               && !RTL_LOOPING_CONST_OR_PURE_CALL_P (call))
     234      1081528 :           && can_delete_call (call))
     235      1077690 :         find_call_stack_args (call, true, fast, NULL);
     236              :     }
     237   1053948355 : }
     238              : 
     239              : 
     240              : /* A note_stores callback used by mark_nonreg_stores.  DATA is the
     241              :    instruction containing DEST.  */
     242              : 
     243              : static void
     244    874634625 : mark_nonreg_stores_1 (rtx dest, const_rtx pattern, void *data)
     245              : {
     246    874634625 :   if (GET_CODE (pattern) != CLOBBER && !REG_P (dest))
     247    142948121 :     mark_insn ((rtx_insn *) data, true);
     248    874634625 : }
     249              : 
     250              : 
     251              : /* A note_stores callback used by mark_nonreg_stores.  DATA is the
     252              :    instruction containing DEST.  */
     253              : 
     254              : static void
     255     52963367 : mark_nonreg_stores_2 (rtx dest, const_rtx pattern, void *data)
     256              : {
     257     52963367 :   if (GET_CODE (pattern) != CLOBBER && !REG_P (dest))
     258      7360475 :     mark_insn ((rtx_insn *) data, false);
     259     52963367 : }
     260              : 
     261              : 
     262              : /* Mark INSN if it stores to a non-register destination.  */
     263              : 
     264              : static void
     265    786109619 : mark_nonreg_stores (rtx_insn *insn, bool fast)
     266              : {
     267    786109619 :   if (fast)
     268    741453092 :     note_stores (insn, mark_nonreg_stores_1, insn);
     269              :   else
     270     44656527 :     note_stores (insn, mark_nonreg_stores_2, insn);
     271    786109619 : }
     272              : 
     273              : 
     274              : /* Return true if a store to SIZE bytes, starting OFF bytes from stack pointer,
     275              :    is a call argument store, and clear corresponding bits from SP_BYTES
     276              :    bitmap if it is.  */
     277              : 
     278              : static bool
     279          276 : check_argument_store (HOST_WIDE_INT size, HOST_WIDE_INT off,
     280              :                       HOST_WIDE_INT min_sp_off, HOST_WIDE_INT max_sp_off,
     281              :                       bitmap sp_bytes)
     282              : {
     283          276 :   HOST_WIDE_INT byte;
     284         1384 :   for (byte = off; byte < off + size; byte++)
     285              :     {
     286         1112 :       if (byte < min_sp_off
     287         1112 :           || byte >= max_sp_off
     288         1112 :           || !bitmap_clear_bit (sp_bytes, byte - min_sp_off))
     289              :         return false;
     290              :     }
     291              :   return true;
     292              : }
     293              : 
     294              : /* If MEM has sp address, return 0, if it has sp + const address,
     295              :    return that const, if it has reg address where reg is set to sp + const
     296              :    and FAST is false, return const, otherwise return
     297              :    INTTYPE_MINUMUM (HOST_WIDE_INT).  */
     298              : 
     299              : static HOST_WIDE_INT
     300          832 : sp_based_mem_offset (rtx_call_insn *call_insn, const_rtx mem, bool fast)
     301              : {
     302          832 :   HOST_WIDE_INT off = 0;
     303          832 :   rtx addr = XEXP (mem, 0);
     304          832 :   if (GET_CODE (addr) == PLUS
     305          676 :       && REG_P (XEXP (addr, 0))
     306          676 :       && CONST_INT_P (XEXP (addr, 1)))
     307              :     {
     308          676 :       off = INTVAL (XEXP (addr, 1));
     309          676 :       addr = XEXP (addr, 0);
     310              :     }
     311          832 :   if (addr == stack_pointer_rtx)
     312              :     return off;
     313              : 
     314            0 :   if (!REG_P (addr) || fast)
     315              :     return INTTYPE_MINIMUM (HOST_WIDE_INT);
     316              : 
     317              :   /* If not fast, use chains to see if addr wasn't set to sp + offset.  */
     318            0 :   df_ref use;
     319            0 :   FOR_EACH_INSN_USE (use, call_insn)
     320            0 :   if (rtx_equal_p (addr, DF_REF_REG (use)))
     321              :     break;
     322              : 
     323            0 :   if (use == NULL)
     324              :     return INTTYPE_MINIMUM (HOST_WIDE_INT);
     325              : 
     326            0 :   struct df_link *defs;
     327            0 :   for (defs = DF_REF_CHAIN (use); defs; defs = defs->next)
     328            0 :     if (! DF_REF_IS_ARTIFICIAL (defs->ref))
     329              :       break;
     330              : 
     331            0 :   if (defs == NULL)
     332              :     return INTTYPE_MINIMUM (HOST_WIDE_INT);
     333              : 
     334            0 :   rtx set = single_set (DF_REF_INSN (defs->ref));
     335            0 :   if (!set)
     336              :     return INTTYPE_MINIMUM (HOST_WIDE_INT);
     337              : 
     338            0 :   if (GET_CODE (SET_SRC (set)) != PLUS
     339            0 :       || XEXP (SET_SRC (set), 0) != stack_pointer_rtx
     340            0 :       || !CONST_INT_P (XEXP (SET_SRC (set), 1)))
     341              :     return INTTYPE_MINIMUM (HOST_WIDE_INT);
     342              : 
     343            0 :   off += INTVAL (XEXP (SET_SRC (set), 1));
     344            0 :   return off;
     345              : }
     346              : 
     347              : /* Data for check_argument_load called via note_uses.  */
     348              : struct check_argument_load_data {
     349              :   bitmap sp_bytes;
     350              :   HOST_WIDE_INT min_sp_off, max_sp_off;
     351              :   rtx_call_insn *call_insn;
     352              :   bool fast;
     353              :   bool load_found;
     354              : };
     355              : 
     356              : /* Helper function for find_call_stack_args.  Check if there are
     357              :    any loads from the argument slots in between the const/pure call
     358              :    and store to the argument slot, set LOAD_FOUND if any is found.  */
     359              : 
     360              : static void
     361          838 : check_argument_load (rtx *loc, void *data)
     362              : {
     363          838 :   struct check_argument_load_data *d
     364              :     = (struct check_argument_load_data *) data;
     365          838 :   subrtx_iterator::array_type array;
     366         2144 :   FOR_EACH_SUBRTX (iter, array, *loc, NONCONST)
     367              :     {
     368         1306 :       const_rtx mem = *iter;
     369         1306 :       HOST_WIDE_INT size;
     370         1306 :       if (MEM_P (mem)
     371            4 :           && MEM_SIZE_KNOWN_P (mem)
     372         1310 :           && MEM_SIZE (mem).is_constant (&size))
     373              :         {
     374            4 :           HOST_WIDE_INT off = sp_based_mem_offset (d->call_insn, mem, d->fast);
     375            4 :           if (off != INTTYPE_MINIMUM (HOST_WIDE_INT)
     376            4 :               && off < d->max_sp_off
     377            0 :               && off + size > d->min_sp_off)
     378            0 :             for (HOST_WIDE_INT byte = MAX (off, d->min_sp_off);
     379            0 :                  byte < MIN (off + size, d->max_sp_off); byte++)
     380            0 :               if (bitmap_bit_p (d->sp_bytes, byte - d->min_sp_off))
     381              :                 {
     382            0 :                   d->load_found = true;
     383            0 :                   return;
     384              :                 }
     385              :         }
     386              :     }
     387          838 : }
     388              : 
     389              : /* Try to find all stack stores of CALL_INSN arguments if
     390              :    ACCUMULATE_OUTGOING_ARGS.  If all stack stores have been found
     391              :    and it is therefore safe to eliminate the call, return true,
     392              :    otherwise return false.  This function should be first called
     393              :    with DO_MARK false, and only when the CALL_INSN is actually
     394              :    going to be marked called again with DO_MARK true.  */
     395              : 
     396              : static bool
     397      2159025 : find_call_stack_args (rtx_call_insn *call_insn, bool do_mark, bool fast,
     398              :                       bitmap arg_stores)
     399              : {
     400      2159025 :   rtx p;
     401      2159025 :   rtx_insn *insn, *prev_insn;
     402      2159025 :   bool ret;
     403      2159025 :   HOST_WIDE_INT min_sp_off, max_sp_off;
     404      2159025 :   bitmap sp_bytes;
     405              : 
     406      2159025 :   gcc_assert (CALL_P (call_insn));
     407      2159025 :   if (!ACCUMULATE_OUTGOING_ARGS)
     408              :     return true;
     409              : 
     410          584 :   if (!do_mark)
     411              :     {
     412          292 :       gcc_assert (arg_stores);
     413          292 :       bitmap_clear (arg_stores);
     414              :     }
     415              : 
     416          584 :   min_sp_off = INTTYPE_MAXIMUM (HOST_WIDE_INT);
     417          584 :   max_sp_off = 0;
     418              : 
     419              :   /* First determine the minimum and maximum offset from sp for
     420              :      stored arguments.  */
     421         1972 :   for (p = CALL_INSN_FUNCTION_USAGE (call_insn); p; p = XEXP (p, 1))
     422         1388 :     if (GET_CODE (XEXP (p, 0)) == USE
     423         1388 :         && MEM_P (XEXP (XEXP (p, 0), 0)))
     424              :       {
     425          276 :         rtx mem = XEXP (XEXP (p, 0), 0);
     426          276 :         HOST_WIDE_INT size;
     427          276 :         if (!MEM_SIZE_KNOWN_P (mem) || !MEM_SIZE (mem).is_constant (&size))
     428      2159025 :           return false;
     429          276 :         HOST_WIDE_INT off = sp_based_mem_offset (call_insn, mem, fast);
     430          276 :         if (off == INTTYPE_MINIMUM (HOST_WIDE_INT))
     431              :           return false;
     432          276 :         min_sp_off = MIN (min_sp_off, off);
     433          276 :         max_sp_off = MAX (max_sp_off, off + size);
     434              :       }
     435              : 
     436          584 :   if (min_sp_off >= max_sp_off)
     437              :     return true;
     438           52 :   sp_bytes = BITMAP_ALLOC (NULL);
     439              : 
     440              :   /* Set bits in SP_BYTES bitmap for bytes relative to sp + min_sp_off
     441              :      which contain arguments.  Checking has been done in the previous
     442              :      loop.  */
     443          640 :   for (p = CALL_INSN_FUNCTION_USAGE (call_insn); p; p = XEXP (p, 1))
     444          588 :     if (GET_CODE (XEXP (p, 0)) == USE
     445          588 :         && MEM_P (XEXP (XEXP (p, 0), 0)))
     446              :       {
     447          276 :         rtx mem = XEXP (XEXP (p, 0), 0);
     448              :         /* Checked in the previous iteration.  */
     449          276 :         HOST_WIDE_INT size = MEM_SIZE (mem).to_constant ();
     450          276 :         HOST_WIDE_INT off = sp_based_mem_offset (call_insn, mem, fast);
     451          276 :         gcc_checking_assert (off != INTTYPE_MINIMUM (HOST_WIDE_INT));
     452         1384 :         for (HOST_WIDE_INT byte = off; byte < off + size; byte++)
     453         1108 :           if (!bitmap_set_bit (sp_bytes, byte - min_sp_off))
     454            0 :             gcc_unreachable ();
     455              :       }
     456              : 
     457              :   /* Walk backwards, looking for argument stores.  The search stops
     458              :      when seeing another call, sp adjustment, memory store other than
     459              :      argument store or a read from an argument stack slot.  */
     460           52 :   struct check_argument_load_data data
     461           52 :     = { sp_bytes, min_sp_off, max_sp_off, call_insn, fast, false };
     462           52 :   ret = false;
     463          614 :   for (insn = PREV_INSN (call_insn); insn; insn = prev_insn)
     464              :     {
     465          562 :       if (insn == BB_HEAD (BLOCK_FOR_INSN (call_insn)))
     466              :         prev_insn = NULL;
     467              :       else
     468          562 :         prev_insn = PREV_INSN (insn);
     469              : 
     470          562 :       if (CALL_P (insn))
     471              :         break;
     472              : 
     473          562 :       if (!NONDEBUG_INSN_P (insn))
     474            0 :         continue;
     475              : 
     476          562 :       rtx set = single_set (insn);
     477          562 :       if (!set || SET_DEST (set) == stack_pointer_rtx)
     478              :         break;
     479              : 
     480          562 :       note_uses (&PATTERN (insn), check_argument_load, &data);
     481          562 :       if (data.load_found)
     482              :         break;
     483              : 
     484          562 :       if (!MEM_P (SET_DEST (set)))
     485          286 :         continue;
     486              : 
     487          276 :       rtx mem = SET_DEST (set);
     488          276 :       HOST_WIDE_INT off = sp_based_mem_offset (call_insn, mem, fast);
     489          276 :       if (off == INTTYPE_MINIMUM (HOST_WIDE_INT))
     490              :         break;
     491              : 
     492          276 :       HOST_WIDE_INT size;
     493          276 :       if (!MEM_SIZE_KNOWN_P (mem)
     494          276 :           || !MEM_SIZE (mem).is_constant (&size)
     495          276 :           || !check_argument_store (size, off, min_sp_off,
     496              :                                     max_sp_off, sp_bytes))
     497              :         break;
     498              : 
     499          272 :       if (!deletable_insn_p (insn, fast, NULL))
     500              :         break;
     501              : 
     502          272 :       if (do_mark)
     503          136 :         mark_insn (insn, fast);
     504              :       else
     505          136 :         bitmap_set_bit (arg_stores, INSN_UID (insn));
     506              : 
     507          272 :       if (bitmap_empty_p (sp_bytes))
     508              :         {
     509              :           ret = true;
     510              :           break;
     511              :         }
     512              :     }
     513              : 
     514           52 :   BITMAP_FREE (sp_bytes);
     515           52 :   if (!ret && arg_stores)
     516            2 :     bitmap_clear (arg_stores);
     517              : 
     518              :   return ret;
     519              : }
     520              : 
     521              : 
     522              : /* Remove all REG_EQUAL and REG_EQUIV notes referring to the registers INSN
     523              :    writes to.  */
     524              : 
     525              : static void
     526      3248379 : remove_reg_equal_equiv_notes_for_defs (rtx_insn *insn)
     527              : {
     528      3248379 :   df_ref def;
     529              : 
     530      6892828 :   FOR_EACH_INSN_DEF (def, insn)
     531      3644449 :     remove_reg_equal_equiv_notes_for_regno (DF_REF_REGNO (def));
     532      3248379 : }
     533              : 
     534              : /* Scan all BBs for debug insns and reset those that reference values
     535              :    defined in unmarked insns.  */
     536              : 
     537              : static void
     538       490229 : reset_unmarked_insns_debug_uses (void)
     539              : {
     540       490229 :   basic_block bb;
     541       490229 :   rtx_insn *insn, *next;
     542              : 
     543      7456802 :   FOR_EACH_BB_REVERSE_FN (bb, cfun)
     544    209954293 :     FOR_BB_INSNS_REVERSE_SAFE (bb, insn, next)
     545    101493860 :       if (DEBUG_INSN_P (insn))
     546              :         {
     547     52459340 :           df_ref use;
     548              : 
     549     63239358 :           FOR_EACH_INSN_USE (use, insn)
     550              :             {
     551     10780632 :               struct df_link *defs;
     552     25129181 :               for (defs = DF_REF_CHAIN (use); defs; defs = defs->next)
     553              :                 {
     554     14349163 :                   rtx_insn *ref_insn;
     555     14349163 :                   if (DF_REF_IS_ARTIFICIAL (defs->ref))
     556      3416878 :                     continue;
     557     10932285 :                   ref_insn = DF_REF_INSN (defs->ref);
     558     10932285 :                   if (!marked_insn_p (ref_insn))
     559              :                     break;
     560              :                 }
     561     10780018 :               if (!defs)
     562     10780018 :                 continue;
     563              :               /* ??? FIXME could we propagate the values assigned to
     564              :                  each of the DEFs?  */
     565          614 :               INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
     566          614 :               df_insn_rescan_debug_internal (insn);
     567          614 :               break;
     568              :             }
     569              :         }
     570       490229 : }
     571              : 
     572              : /* Delete every instruction that hasn't been marked.  */
     573              : 
     574              : static void
     575     11176543 : delete_unmarked_insns (void)
     576              : {
     577     11176543 :   basic_block bb;
     578     11176543 :   rtx_insn *insn, *next;
     579     11176543 :   bool must_clean = false;
     580              : 
     581    199844408 :   FOR_EACH_BB_REVERSE_FN (bb, cfun)
     582   4866546837 :     FOR_BB_INSNS_REVERSE_SAFE (bb, insn, next)
     583   2338939486 :       if (NONDEBUG_INSN_P (insn))
     584              :         {
     585   1012880165 :           rtx turn_into_use = NULL_RTX;
     586              : 
     587              :           /* Always delete no-op moves.  */
     588   1012880165 :           if (noop_move_p (insn)
     589              :               /* Unless the no-op move can throw and we are not allowed
     590              :                  to alter cfg.  */
     591   1012880165 :               && (!cfun->can_throw_non_call_exceptions
     592         3226 :                   || (cfun->can_delete_dead_exceptions && can_alter_cfg)
     593         3226 :                   || insn_nothrow_p (insn)))
     594              :             {
     595        12661 :               if (RTX_FRAME_RELATED_P (insn))
     596            0 :                 turn_into_use
     597            0 :                   = find_reg_note (insn, REG_CFA_RESTORE, NULL);
     598            0 :               if (turn_into_use && REG_P (XEXP (turn_into_use, 0)))
     599      3248379 :                 turn_into_use = XEXP (turn_into_use, 0);
     600              :               else
     601              :                 turn_into_use = NULL_RTX;
     602              :             }
     603              : 
     604              :           /* Otherwise rely only on the DCE algorithm.  */
     605   1012867504 :           else if (marked_insn_p (insn))
     606   1009631786 :             continue;
     607              : 
     608              :           /* Beware that reaching a dbg counter limit here can result
     609              :              in miscompiled file.  This occurs when a group of insns
     610              :              must be deleted together, typically because the kept insn
     611              :              depends on the output from the deleted insn.  Deleting
     612              :              this insns in reverse order (both at the bb level and
     613              :              when looking at the blocks) minimizes this, but does not
     614              :              eliminate it, since it is possible for the using insn to
     615              :              be top of a block and the producer to be at the bottom of
     616              :              the block.  However, in most cases this will only result
     617              :              in an uninitialized use of an insn that is dead anyway.
     618              : 
     619              :              However, there is one rare case that will cause a
     620              :              miscompile: deletion of non-looping pure and constant
     621              :              calls on a machine where ACCUMULATE_OUTGOING_ARGS is true.
     622              :              In this case it is possible to remove the call, but leave
     623              :              the argument pushes to the stack.  Because of the changes
     624              :              to the stack pointer, this will almost always lead to a
     625              :              miscompile.  */
     626      3248379 :           if (!dbg_cnt (dce))
     627            0 :             continue;
     628              : 
     629      3248379 :           if (dump_file)
     630           80 :             fprintf (dump_file, "DCE: Deleting insn %d\n", INSN_UID (insn));
     631              : 
     632              :           /* Before we delete the insn we have to remove the REG_EQUAL notes
     633              :              for the destination regs in order to avoid dangling notes.  */
     634      3248379 :           remove_reg_equal_equiv_notes_for_defs (insn);
     635              : 
     636      3248379 :           if (turn_into_use)
     637              :             {
     638              :               /* Don't remove frame related noop moves if they cary
     639              :                  REG_CFA_RESTORE note, while we don't need to emit any code,
     640              :                  we need it to emit the CFI restore note.  */
     641            0 :               PATTERN (insn)
     642            0 :                 = gen_rtx_USE (GET_MODE (turn_into_use), turn_into_use);
     643            0 :               INSN_CODE (insn) = -1;
     644            0 :               df_insn_rescan (insn);
     645              :             }
     646              :           else
     647              :             /* Now delete the insn.  */
     648      3248379 :             must_clean |= delete_insn_and_edges (insn);
     649              :         }
     650              : 
     651              :   /* Deleted a pure or const call.  */
     652     11176543 :   if (must_clean)
     653              :     {
     654            5 :       gcc_assert (can_alter_cfg);
     655            5 :       delete_unreachable_blocks ();
     656            5 :       free_dominance_info (CDI_DOMINATORS);
     657              :     }
     658     11176543 : }
     659              : 
     660              : 
     661              : /* Go through the instructions and mark those whose necessity is not
     662              :    dependent on inter-instruction information.  Make sure all other
     663              :    instructions are not marked.  */
     664              : 
     665              : static void
     666     11176543 : prescan_insns_for_dce (bool fast)
     667              : {
     668     11176543 :   basic_block bb;
     669     11176543 :   rtx_insn *insn, *prev;
     670     11176543 :   bitmap arg_stores = NULL;
     671              : 
     672     11176543 :   if (dump_file)
     673          367 :     fprintf (dump_file, "Finding needed instructions:\n");
     674              : 
     675     11176543 :   if (!df_in_progress && ACCUMULATE_OUTGOING_ARGS)
     676       139407 :     arg_stores = BITMAP_ALLOC (NULL);
     677              : 
     678    199844408 :   FOR_EACH_BB_FN (bb, cfun)
     679              :     {
     680   4865922141 :       FOR_BB_INSNS_REVERSE_SAFE (bb, insn, prev)
     681   2338627138 :         if (NONDEBUG_INSN_P (insn))
     682              :           {
     683              :             /* Don't mark argument stores now.  They will be marked
     684              :                if needed when the associated CALL is marked.  */
     685   1012880165 :             if (arg_stores && bitmap_bit_p (arg_stores, INSN_UID (insn)))
     686          136 :               continue;
     687   1012880029 :             if (deletable_insn_p (insn, fast, arg_stores))
     688    786109619 :               mark_nonreg_stores (insn, fast);
     689              :             else
     690    226770410 :               mark_insn (insn, fast);
     691              :           }
     692              :       /* find_call_stack_args only looks at argument stores in the
     693              :          same bb.  */
     694    188667865 :       if (arg_stores)
     695       169768 :         bitmap_clear (arg_stores);
     696              :     }
     697              : 
     698     11176543 :   if (arg_stores)
     699       139407 :     BITMAP_FREE (arg_stores);
     700              : 
     701     11176543 :   if (dump_file)
     702          367 :     fprintf (dump_file, "Finished finding needed instructions:\n");
     703     11176543 : }
     704              : 
     705              : 
     706              : /* UD-based DSE routines. */
     707              : 
     708              : /* Mark instructions that define artificially-used registers, such as
     709              :    the frame pointer and the stack pointer.  */
     710              : 
     711              : static void
     712       982664 : mark_artificial_uses (void)
     713              : {
     714       982664 :   basic_block bb;
     715       982664 :   struct df_link *defs;
     716       982664 :   df_ref use;
     717              : 
     718     12972273 :   FOR_ALL_BB_FN (bb, cfun)
     719     67614182 :     FOR_EACH_ARTIFICIAL_USE (use, bb->index)
     720     90366184 :       for (defs = DF_REF_CHAIN (use); defs; defs = defs->next)
     721     46731220 :         if (!DF_REF_IS_ARTIFICIAL (defs->ref))
     722      5887462 :           mark_insn (DF_REF_INSN (defs->ref), false);
     723       982664 : }
     724              : 
     725              : 
     726              : /* Mark every instruction that defines a register value that INSN uses.  */
     727              : 
     728              : static void
     729     56450714 : mark_reg_dependencies (rtx_insn *insn)
     730              : {
     731     56450714 :   struct df_link *defs;
     732     56450714 :   df_ref use;
     733              : 
     734     56450714 :   if (DEBUG_INSN_P (insn))
     735              :     return;
     736              : 
     737    128644453 :   FOR_EACH_INSN_USE (use, insn)
     738              :     {
     739     72193739 :       if (dump_file)
     740              :         {
     741         1230 :           fprintf (dump_file, "Processing use of ");
     742         1230 :           print_simple_rtl (dump_file, DF_REF_REG (use));
     743         1230 :           fprintf (dump_file, " in insn %d:\n", INSN_UID (insn));
     744              :         }
     745    156067615 :       for (defs = DF_REF_CHAIN (use); defs; defs = defs->next)
     746     83873876 :         if (! DF_REF_IS_ARTIFICIAL (defs->ref))
     747     72083713 :           mark_insn (DF_REF_INSN (defs->ref), false);
     748              :     }
     749              : }
     750              : 
     751              : 
     752              : /* Initialize global variables for a new DCE pass.  */
     753              : 
     754              : static void
     755     11176301 : init_dce (bool fast)
     756              : {
     757     11176301 :   if (!df_in_progress)
     758              :     {
     759      2400702 :       if (!fast)
     760              :         {
     761       982664 :           df_set_flags (DF_RD_PRUNE_DEAD_DEFS);
     762       982664 :           df_chain_add_problem (DF_UD_CHAIN);
     763              :         }
     764      2400702 :       df_analyze ();
     765              :     }
     766              : 
     767     11176301 :   if (dump_file)
     768          367 :     df_dump (dump_file);
     769              : 
     770     11176301 :   if (fast)
     771              :     {
     772     10193637 :       bitmap_obstack_initialize (&dce_blocks_bitmap_obstack);
     773     10193637 :       bitmap_obstack_initialize (&dce_tmp_bitmap_obstack);
     774     10193637 :       can_alter_cfg = false;
     775              :     }
     776              :   else
     777       982664 :     can_alter_cfg = true;
     778              : 
     779     11176301 :   marked = sbitmap_alloc (get_max_uid () + 1);
     780     11176301 :   bitmap_clear (marked);
     781     11176301 : }
     782              : 
     783              : 
     784              : /* Free the data allocated by init_dce.  */
     785              : 
     786              : static void
     787     11176301 : fini_dce (bool fast)
     788              : {
     789     10193637 :   sbitmap_free (marked);
     790              : 
     791     10193637 :   if (fast)
     792              :     {
     793     10193637 :       bitmap_obstack_release (&dce_blocks_bitmap_obstack);
     794     10193637 :       bitmap_obstack_release (&dce_tmp_bitmap_obstack);
     795              :     }
     796     10193637 : }
     797              : 
     798              : 
     799              : /* UD-chain based DCE.  */
     800              : 
     801              : static unsigned int
     802       982664 : rest_of_handle_ud_dce (void)
     803              : {
     804       982664 :   rtx_insn *insn;
     805              : 
     806       982664 :   init_dce (false);
     807              : 
     808       982664 :   prescan_insns_for_dce (false);
     809       982664 :   mark_artificial_uses ();
     810     58416042 :   while (worklist.length () > 0)
     811              :     {
     812     56450714 :       insn = worklist.pop ();
     813     56450714 :       mark_reg_dependencies (insn);
     814              :     }
     815       982664 :   worklist.release ();
     816              : 
     817       982664 :   if (MAY_HAVE_DEBUG_BIND_INSNS)
     818       490229 :     reset_unmarked_insns_debug_uses ();
     819              : 
     820              :   /* Before any insns are deleted, we must remove the chains since
     821              :      they are not bidirectional.  */
     822       982664 :   df_remove_problem (df_chain);
     823       982664 :   delete_unmarked_insns ();
     824              : 
     825       982664 :   fini_dce (false);
     826       982664 :   return 0;
     827              : }
     828              : 
     829              : 
     830              : namespace {
     831              : 
     832              : const pass_data pass_data_ud_rtl_dce =
     833              : {
     834              :   RTL_PASS, /* type */
     835              :   "ud_dce", /* name */
     836              :   OPTGROUP_NONE, /* optinfo_flags */
     837              :   TV_DCE, /* tv_id */
     838              :   0, /* properties_required */
     839              :   0, /* properties_provided */
     840              :   0, /* properties_destroyed */
     841              :   0, /* todo_flags_start */
     842              :   TODO_df_finish, /* todo_flags_finish */
     843              : };
     844              : 
     845              : class pass_ud_rtl_dce : public rtl_opt_pass
     846              : {
     847              : public:
     848       297180 :   pass_ud_rtl_dce (gcc::context *ctxt)
     849       594360 :     : rtl_opt_pass (pass_data_ud_rtl_dce, ctxt)
     850              :   {}
     851              : 
     852              :   /* opt_pass methods: */
     853      1517135 :   bool gate (function *) final override
     854              :     {
     855      1517135 :       return optimize > 1 && flag_dce && dbg_cnt (dce_ud);
     856              :     }
     857              : 
     858       982664 :   unsigned int execute (function *) final override
     859              :     {
     860       982664 :       return rest_of_handle_ud_dce ();
     861              :     }
     862              : 
     863              : }; // class pass_ud_rtl_dce
     864              : 
     865              : } // anon namespace
     866              : 
     867              : rtl_opt_pass *
     868       297180 : make_pass_ud_rtl_dce (gcc::context *ctxt)
     869              : {
     870       297180 :   return new pass_ud_rtl_dce (ctxt);
     871              : }
     872              : 
     873              : 
     874              : /* -------------------------------------------------------------------------
     875              :    Fast DCE functions
     876              :    ------------------------------------------------------------------------- */
     877              : 
     878              : /* Process basic block BB.  Return true if the live_in set has
     879              :    changed. REDO_OUT is true if the info at the bottom of the block
     880              :    needs to be recalculated before starting.  AU is the proper set of
     881              :    artificial uses.  Track global substitution of uses of dead pseudos
     882              :    in debug insns using GLOBAL_DEBUG.  */
     883              : 
     884              : static bool
     885      3689826 : word_dce_process_block (basic_block bb, bool redo_out,
     886              :                         struct dead_debug_global *global_debug)
     887              : {
     888      3689826 :   bitmap local_live = BITMAP_ALLOC (&dce_tmp_bitmap_obstack);
     889      3689826 :   rtx_insn *insn;
     890      3689826 :   bool block_changed;
     891      3689826 :   struct dead_debug_local debug;
     892              : 
     893      3689826 :   if (redo_out)
     894              :     {
     895              :       /* Need to redo the live_out set of this block if when one of
     896              :          the succs of this block has had a change in it live in
     897              :          set.  */
     898            6 :       edge e;
     899            6 :       edge_iterator ei;
     900            6 :       df_confluence_function_n con_fun_n = df_word_lr->problem->con_fun_n;
     901           12 :       bitmap_clear (DF_WORD_LR_OUT (bb));
     902           17 :       FOR_EACH_EDGE (e, ei, bb->succs)
     903           11 :         (*con_fun_n) (e);
     904              :     }
     905              : 
     906      3689826 :   if (dump_file)
     907              :     {
     908            0 :       fprintf (dump_file, "processing block %d live out = ", bb->index);
     909            0 :       df_print_word_regset (dump_file, DF_WORD_LR_OUT (bb));
     910              :     }
     911              : 
     912      7379652 :   bitmap_copy (local_live, DF_WORD_LR_OUT (bb));
     913      3689826 :   dead_debug_local_init (&debug, NULL, global_debug);
     914              : 
     915     48657586 :   FOR_BB_INSNS_REVERSE (bb, insn)
     916     44967760 :     if (DEBUG_INSN_P (insn))
     917              :       {
     918     16358327 :         df_ref use;
     919     20212165 :         FOR_EACH_INSN_USE (use, insn)
     920      3853838 :           if (DF_REF_REGNO (use) >= FIRST_PSEUDO_REGISTER
     921      5665057 :               && known_eq (GET_MODE_SIZE (GET_MODE (DF_REF_REAL_REG (use))),
     922              :                            2 * UNITS_PER_WORD)
     923       179924 :               && !bitmap_bit_p (local_live, 2 * DF_REF_REGNO (use))
     924      3857337 :               && !bitmap_bit_p (local_live, 2 * DF_REF_REGNO (use) + 1))
     925            0 :             dead_debug_add (&debug, use, DF_REF_REGNO (use));
     926              :       }
     927     28609433 :     else if (INSN_P (insn))
     928              :       {
     929     21699872 :         bool any_changed;
     930              : 
     931              :         /* No matter if the instruction is needed or not, we remove
     932              :            any regno in the defs from the live set.  */
     933     21699872 :         any_changed = df_word_lr_simulate_defs (insn, local_live);
     934     21699872 :         if (any_changed)
     935     16136249 :           mark_insn (insn, true);
     936              : 
     937              :         /* On the other hand, we do not allow the dead uses to set
     938              :            anything in local_live.  */
     939     21699872 :         if (marked_insn_p (insn))
     940     21685155 :           df_word_lr_simulate_uses (insn, local_live);
     941              : 
     942              :         /* Insert debug temps for dead REGs used in subsequent debug
     943              :            insns.  We may have to emit a debug temp even if the insn
     944              :            was marked, in case the debug use was after the point of
     945              :            death.  */
     946     21699872 :         if (debug.used && !bitmap_empty_p (debug.used))
     947              :           {
     948            0 :             df_ref def;
     949              : 
     950            0 :             FOR_EACH_INSN_DEF (def, insn)
     951            0 :               dead_debug_insert_temp (&debug, DF_REF_REGNO (def), insn,
     952            0 :                                       marked_insn_p (insn)
     953            0 :                                       && !control_flow_insn_p (insn)
     954              :                                       ? DEBUG_TEMP_AFTER_WITH_REG_FORCE
     955              :                                       : DEBUG_TEMP_BEFORE_WITH_VALUE);
     956              :           }
     957              : 
     958     21699872 :         if (dump_file)
     959              :           {
     960            0 :             fprintf (dump_file, "finished processing insn %d live out = ",
     961            0 :                      INSN_UID (insn));
     962            0 :             df_print_word_regset (dump_file, local_live);
     963              :           }
     964              :       }
     965              : 
     966      7379652 :   block_changed = !bitmap_equal_p (local_live, DF_WORD_LR_IN (bb));
     967      3689826 :   if (block_changed)
     968           10 :     bitmap_copy (DF_WORD_LR_IN (bb), local_live);
     969              : 
     970      3689826 :   dead_debug_local_finish (&debug, NULL);
     971      3689826 :   BITMAP_FREE (local_live);
     972      3689826 :   return block_changed;
     973              : }
     974              : 
     975              : 
     976              : /* Process basic block BB.  Return true if the live_in set has
     977              :    changed. REDO_OUT is true if the info at the bottom of the block
     978              :    needs to be recalculated before starting.  AU is the proper set of
     979              :    artificial uses.  Track global substitution of uses of dead pseudos
     980              :    in debug insns using GLOBAL_DEBUG.  */
     981              : 
     982              : static bool
     983    174953758 : dce_process_block (basic_block bb, bool redo_out, bitmap au,
     984              :                    struct dead_debug_global *global_debug)
     985              : {
     986    174953758 :   bitmap local_live = BITMAP_ALLOC (&dce_tmp_bitmap_obstack);
     987    174953758 :   rtx_insn *insn;
     988    174953758 :   bool block_changed;
     989    174953758 :   df_ref def;
     990    174953758 :   struct dead_debug_local debug;
     991              : 
     992    174953758 :   if (redo_out)
     993              :     {
     994              :       /* Need to redo the live_out set of this block if when one of
     995              :          the succs of this block has had a change in it live in
     996              :          set.  */
     997        38213 :       edge e;
     998        38213 :       edge_iterator ei;
     999        38213 :       df_confluence_function_n con_fun_n = df_lr->problem->con_fun_n;
    1000        76426 :       bitmap_clear (DF_LR_OUT (bb));
    1001       104472 :       FOR_EACH_EDGE (e, ei, bb->succs)
    1002        66259 :         (*con_fun_n) (e);
    1003              :     }
    1004              : 
    1005    174953758 :   if (dump_file)
    1006              :     {
    1007         2638 :       fprintf (dump_file, "processing block %d lr out = ", bb->index);
    1008         5276 :       df_print_regset (dump_file, DF_LR_OUT (bb));
    1009              :     }
    1010              : 
    1011    349907516 :   bitmap_copy (local_live, DF_LR_OUT (bb));
    1012              : 
    1013    174953758 :   df_simulate_initialize_backwards (bb, local_live);
    1014    174953758 :   dead_debug_local_init (&debug, NULL, global_debug);
    1015              : 
    1016   2344000055 :   FOR_BB_INSNS_REVERSE (bb, insn)
    1017   2169046297 :     if (DEBUG_INSN_P (insn))
    1018              :       {
    1019    918839885 :         df_ref use;
    1020   1110373535 :         FOR_EACH_INSN_USE (use, insn)
    1021    191533650 :           if (!bitmap_bit_p (local_live, DF_REF_REGNO (use))
    1022    191533650 :               && !bitmap_bit_p (au, DF_REF_REGNO (use)))
    1023       294825 :             dead_debug_add (&debug, use, DF_REF_REGNO (use));
    1024              :       }
    1025   1250206412 :     else if (INSN_P (insn))
    1026              :       {
    1027    934718794 :         bool needed = marked_insn_p (insn);
    1028              : 
    1029              :         /* The insn is needed if there is someone who uses the output.  */
    1030    934718794 :         if (!needed)
    1031    716883705 :           FOR_EACH_INSN_DEF (def, insn)
    1032    713673423 :             if (bitmap_bit_p (local_live, DF_REF_REGNO (def))
    1033    713673423 :                 || bitmap_bit_p (au, DF_REF_REGNO (def)))
    1034              :               {
    1035    582761789 :                 needed = true;
    1036    582761789 :                 mark_insn (insn, true);
    1037    582761789 :                 break;
    1038              :               }
    1039              : 
    1040              :         /* No matter if the instruction is needed or not, we remove
    1041              :            any regno in the defs from the live set.  */
    1042    934718794 :         df_simulate_defs (insn, local_live);
    1043              : 
    1044              :         /* On the other hand, we do not allow the dead uses to set
    1045              :            anything in local_live.  */
    1046    934718794 :         if (needed)
    1047    931508512 :           df_simulate_uses (insn, local_live);
    1048              : 
    1049              :         /* Insert debug temps for dead REGs used in subsequent debug
    1050              :            insns.  We may have to emit a debug temp even if the insn
    1051              :            was marked, in case the debug use was after the point of
    1052              :            death.  */
    1053    934718794 :         if (debug.used && !bitmap_empty_p (debug.used))
    1054     88976700 :           FOR_EACH_INSN_DEF (def, insn)
    1055    147607722 :             dead_debug_insert_temp (&debug, DF_REF_REGNO (def), insn,
    1056     73674075 :                                     needed && !control_flow_insn_p (insn)
    1057              :                                     ? DEBUG_TEMP_AFTER_WITH_REG_FORCE
    1058              :                                     : DEBUG_TEMP_BEFORE_WITH_VALUE);
    1059              :       }
    1060              : 
    1061    174953758 :   dead_debug_local_finish (&debug, NULL);
    1062    174953758 :   df_simulate_finalize_backwards (bb, local_live);
    1063              : 
    1064    349907516 :   block_changed = !bitmap_equal_p (local_live, DF_LR_IN (bb));
    1065    174953758 :   if (block_changed)
    1066        69744 :     bitmap_copy (DF_LR_IN (bb), local_live);
    1067              : 
    1068    174953758 :   BITMAP_FREE (local_live);
    1069    174953758 :   return block_changed;
    1070              : }
    1071              : 
    1072              : 
    1073              : /* Perform fast DCE once initialization is done.  If WORD_LEVEL is
    1074              :    true, use the word level dce, otherwise do it at the pseudo
    1075              :    level.  */
    1076              : 
    1077              : static void
    1078     10193637 : fast_dce (bool word_level)
    1079              : {
    1080     10193637 :   int *postorder = df_get_postorder (DF_BACKWARD);
    1081     10193637 :   int n_blocks = df_get_n_blocks (DF_BACKWARD);
    1082              :   /* The set of blocks that have been seen on this iteration.  */
    1083     10193637 :   bitmap processed = BITMAP_ALLOC (&dce_blocks_bitmap_obstack);
    1084              :   /* The set of blocks that need to have the out vectors reset because
    1085              :      the in of one of their successors has changed.  */
    1086     10193637 :   bitmap redo_out = BITMAP_ALLOC (&dce_blocks_bitmap_obstack);
    1087     10193637 :   bitmap all_blocks = BITMAP_ALLOC (&dce_blocks_bitmap_obstack);
    1088     10193637 :   bool global_changed = true;
    1089              : 
    1090              :   /* These regs are considered always live so if they end up dying
    1091              :      because of some def, we need to bring the back again.  Calling
    1092              :      df_simulate_fixup_sets has the disadvantage of calling
    1093              :      bb_has_eh_pred once per insn, so we cache the information
    1094              :      here.  */
    1095     10193637 :   bitmap au = &df->regular_block_artificial_uses;
    1096     10193637 :   bitmap au_eh = &df->eh_block_artificial_uses;
    1097     10193637 :   int i;
    1098     10193637 :   struct dead_debug_global global_debug;
    1099              : 
    1100     10193637 :   prescan_insns_for_dce (true);
    1101              : 
    1102    219400417 :   for (i = 0; i < n_blocks; i++)
    1103    199013143 :     bitmap_set_bit (all_blocks, postorder[i]);
    1104              : 
    1105     10193637 :   dead_debug_global_init (&global_debug, NULL);
    1106              : 
    1107     20387516 :   while (global_changed)
    1108              :     {
    1109              :       global_changed = false;
    1110              : 
    1111    209225221 :       for (i = 0; i < n_blocks; i++)
    1112              :         {
    1113    199031342 :           int index = postorder[i];
    1114    199031342 :           basic_block bb = BASIC_BLOCK_FOR_FN (cfun, index);
    1115    199031342 :           bool local_changed;
    1116              : 
    1117    199031342 :           if (index < NUM_FIXED_BLOCKS)
    1118              :             {
    1119     20387758 :               bitmap_set_bit (processed, index);
    1120     20387758 :               continue;
    1121              :             }
    1122              : 
    1123    178643584 :           if (word_level)
    1124      3689826 :             local_changed
    1125      3689826 :               = word_dce_process_block (bb, bitmap_bit_p (redo_out, index),
    1126              :                                         &global_debug);
    1127              :           else
    1128    174953758 :             local_changed
    1129    346420912 :               = dce_process_block (bb, bitmap_bit_p (redo_out, index),
    1130    174953758 :                                    bb_has_eh_pred (bb) ? au_eh : au,
    1131              :                                    &global_debug);
    1132    178643584 :           bitmap_set_bit (processed, index);
    1133              : 
    1134    178643584 :           if (local_changed)
    1135              :             {
    1136        34877 :               edge e;
    1137        34877 :               edge_iterator ei;
    1138        78634 :               FOR_EACH_EDGE (e, ei, bb->preds)
    1139        43757 :                 if (bitmap_bit_p (processed, e->src->index))
    1140              :                   /* Be tricky about when we need to iterate the
    1141              :                      analysis.  We only have redo the analysis if the
    1142              :                      bitmaps change at the top of a block that is the
    1143              :                      entry to a loop.  */
    1144              :                   global_changed = true;
    1145              :                 else
    1146        43486 :                   bitmap_set_bit (redo_out, e->src->index);
    1147              :             }
    1148              :         }
    1149              : 
    1150     10193879 :       if (global_changed)
    1151              :         {
    1152              :           /* Turn off the RUN_DCE flag to prevent recursive calls to
    1153              :              dce.  */
    1154          242 :           int old_flag = df_clear_flags (DF_LR_RUN_DCE);
    1155              : 
    1156              :           /* So something was deleted that requires a redo.  Do it on
    1157              :              the cheap.  */
    1158          242 :           delete_unmarked_insns ();
    1159          242 :           bitmap_clear (marked);
    1160          242 :           bitmap_clear (processed);
    1161          242 :           bitmap_clear (redo_out);
    1162              : 
    1163              :           /* We do not need to rescan any instructions.  We only need
    1164              :              to redo the dataflow equations for the blocks that had a
    1165              :              change at the top of the block.  Then we need to redo the
    1166              :              iteration.  */
    1167          242 :           if (word_level)
    1168            0 :             df_analyze_problem (df_word_lr, all_blocks, postorder, n_blocks);
    1169              :           else
    1170          242 :             df_analyze_problem (df_lr, all_blocks, postorder, n_blocks);
    1171              : 
    1172          242 :           if (old_flag & DF_LR_RUN_DCE)
    1173          237 :             df_set_flags (DF_LR_RUN_DCE);
    1174              : 
    1175          242 :           prescan_insns_for_dce (true);
    1176              :         }
    1177              :     }
    1178              : 
    1179     10193637 :   dead_debug_global_finish (&global_debug, NULL);
    1180              : 
    1181     10193637 :   delete_unmarked_insns ();
    1182              : 
    1183     10193637 :   BITMAP_FREE (processed);
    1184     10193637 :   BITMAP_FREE (redo_out);
    1185     10193637 :   BITMAP_FREE (all_blocks);
    1186              : 
    1187              :   /* Both forms of DCE should make further DCE unnecessary.  */
    1188     10193637 :   df_lr_dce->solutions_dirty = false;
    1189     10193637 : }
    1190              : 
    1191              : 
    1192              : /* Fast register level DCE.  */
    1193              : 
    1194              : static unsigned int
    1195     10045026 : rest_of_handle_fast_dce (void)
    1196              : {
    1197     10045026 :   init_dce (true);
    1198     10045026 :   fast_dce (false);
    1199     10045026 :   fini_dce (true);
    1200     10045026 :   return 0;
    1201              : }
    1202              : 
    1203              : 
    1204              : /* Fast byte level DCE.  */
    1205              : 
    1206              : void
    1207       148623 : run_word_dce (void)
    1208              : {
    1209       148623 :   int old_flags;
    1210              : 
    1211       148623 :   if (!flag_dce)
    1212              :     return;
    1213              : 
    1214       148611 :   timevar_push (TV_DCE);
    1215       148611 :   old_flags = df_clear_flags (DF_DEFER_INSN_RESCAN + DF_NO_INSN_RESCAN);
    1216       148611 :   df_word_lr_add_problem ();
    1217       148611 :   init_dce (true);
    1218       148611 :   fast_dce (true);
    1219       148611 :   fini_dce (true);
    1220       148611 :   df_set_flags (old_flags);
    1221       148611 :   timevar_pop (TV_DCE);
    1222              : }
    1223              : 
    1224              : 
    1225              : /* This is an internal call that is used by the df live register
    1226              :    problem to run fast dce as a side effect of creating the live
    1227              :    information.  The stack is organized so that the lr problem is run,
    1228              :    this pass is run, which updates the live info and the df scanning
    1229              :    info, and then returns to allow the rest of the problems to be run.
    1230              : 
    1231              :    This can be called by elsewhere but it will not update the bit
    1232              :    vectors for any other problems than LR.  */
    1233              : 
    1234              : void
    1235      8776854 : run_fast_df_dce (void)
    1236              : {
    1237      8776854 :   if (flag_dce)
    1238              :     {
    1239              :       /* If dce is able to delete something, it has to happen
    1240              :          immediately.  Otherwise there will be problems handling the
    1241              :          eq_notes.  */
    1242      8775599 :       int old_flags =
    1243      8775599 :         df_clear_flags (DF_DEFER_INSN_RESCAN + DF_NO_INSN_RESCAN);
    1244              : 
    1245      8775599 :       df_in_progress = true;
    1246      8775599 :       rest_of_handle_fast_dce ();
    1247      8775599 :       df_in_progress = false;
    1248              : 
    1249      8775599 :       df_set_flags (old_flags);
    1250              :     }
    1251      8776854 : }
    1252              : 
    1253              : 
    1254              : /* Run a fast DCE pass.  */
    1255              : 
    1256              : void
    1257       204592 : run_fast_dce (void)
    1258              : {
    1259       204592 :   if (flag_dce)
    1260       204569 :     rest_of_handle_fast_dce ();
    1261       204592 : }
    1262              : 
    1263              : 
    1264              : namespace {
    1265              : 
    1266              : const pass_data pass_data_fast_rtl_dce =
    1267              : {
    1268              :   RTL_PASS, /* type */
    1269              :   "rtl_dce", /* name */
    1270              :   OPTGROUP_NONE, /* optinfo_flags */
    1271              :   TV_DCE, /* tv_id */
    1272              :   0, /* properties_required */
    1273              :   0, /* properties_provided */
    1274              :   0, /* properties_destroyed */
    1275              :   0, /* todo_flags_start */
    1276              :   TODO_df_finish, /* todo_flags_finish */
    1277              : };
    1278              : 
    1279              : class pass_fast_rtl_dce : public rtl_opt_pass
    1280              : {
    1281              : public:
    1282       297180 :   pass_fast_rtl_dce (gcc::context *ctxt)
    1283       594360 :     : rtl_opt_pass (pass_data_fast_rtl_dce, ctxt)
    1284              :   {}
    1285              : 
    1286              :   /* opt_pass methods: */
    1287      1517135 :   bool gate (function *) final override
    1288              :     {
    1289      1517135 :       return optimize > 0 && flag_dce && dbg_cnt (dce_fast);
    1290              :     }
    1291              : 
    1292      1064858 :   unsigned int execute (function *) final override
    1293              :     {
    1294      1064858 :       return rest_of_handle_fast_dce ();
    1295              :     }
    1296              : 
    1297              : }; // class pass_fast_rtl_dce
    1298              : 
    1299              : } // anon namespace
    1300              : 
    1301              : rtl_opt_pass *
    1302       297180 : make_pass_fast_rtl_dce (gcc::context *ctxt)
    1303              : {
    1304       297180 :   return new pass_fast_rtl_dce (ctxt);
    1305              : }
        

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.