LCOV - code coverage report
Current view: top level - gcc - compare-elim.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 90.6 % 339 307
Test Date: 2026-12-05 16:10:02 Functions: 100.0 % 18 18
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Post-reload compare elimination.
       2              :    Copyright (C) 2010-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              : /* There is a set of targets whose general-purpose move or addition
      21              :    instructions clobber the flags.  These targets cannot split their
      22              :    CBRANCH/CSTORE etc patterns before reload is complete, lest reload
      23              :    itself insert these instructions in between the flags setter and user.
      24              :    Because these targets cannot split the compare from the use, they
      25              :    cannot make use of the comparison elimination offered by the combine pass.
      26              : 
      27              :    This is a small pass intended to provide comparison elimination similar to
      28              :    what was available via NOTICE_UPDATE_CC for cc0 targets.
      29              : 
      30              :    This pass assumes:
      31              : 
      32              :    (0) CBRANCH/CSTORE etc have been split in pass_split_after_reload.
      33              : 
      34              :    (1) All comparison patterns are represented as
      35              : 
      36              :         [(set (reg:CC) (compare:CC (reg) (reg_or_immediate)))]
      37              : 
      38              :    (2) All insn patterns that modify the flags are represented as
      39              : 
      40              :         [(set (reg) (operation)
      41              :          (clobber (reg:CC))]
      42              : 
      43              :    (3) If an insn of form (2) can usefully set the flags, there is
      44              :        another pattern of the form
      45              : 
      46              :         [(set (reg:CCM) (compare:CCM (operation) (immediate)))
      47              :          (set (reg) (operation)]
      48              : 
      49              :        The mode CCM will be chosen as if by SELECT_CC_MODE.
      50              : 
      51              :    Note that unlike NOTICE_UPDATE_CC, we do not handle memory operands.
      52              :    This could be handled as a future enhancement.
      53              : */
      54              : 
      55              : #include "config.h"
      56              : #include "system.h"
      57              : #include "coretypes.h"
      58              : #include "backend.h"
      59              : #include "target.h"
      60              : #include "rtl.h"
      61              : #include "df.h"
      62              : #include "memmodel.h"
      63              : #include "tm_p.h"
      64              : #include "insn-config.h"
      65              : #include "recog.h"
      66              : #include "emit-rtl.h"
      67              : #include "cfgrtl.h"
      68              : #include "tree-pass.h"
      69              : #include "domwalk.h"
      70              : 
      71              : 
      72              : /* These structures describe a comparison and how it is used.  */
      73              : 
      74              : struct comparison_use
      75              : {
      76              :   /* The instruction in which the result of the compare is used.  */
      77              :   rtx_insn *insn;
      78              :   /* The location of the flags register within the use.  */
      79              :   rtx *loc;
      80              :   /* The comparison code applied against the flags register.  */
      81              :   enum rtx_code code;
      82              : };
      83              : 
      84      3504403 : struct comparison
      85              : {
      86              :   /* The comparison instruction.  */
      87              :   rtx_insn *insn;
      88              : 
      89              :   /* The insn prior to the comparison insn that clobbers the flags.  */
      90              :   rtx_insn *prev_clobber;
      91              : 
      92              :   /* The insn prior to the comparison insn that sets in_a REG.  */
      93              :   rtx_insn *in_a_setter;
      94              : 
      95              :   /* The two values being compared.  These will be either REGs or
      96              :      constants.  */
      97              :   rtx in_a, in_b;
      98              : 
      99              :   /* The REG_EH_REGION of the comparison.  */
     100              :   rtx eh_note;
     101              : 
     102              :   /* Information about how this comparison is used.  */
     103              :   auto_vec<comparison_use, 3> uses;
     104              : 
     105              :   /* The original CC_MODE for this comparison.  */
     106              :   machine_mode orig_mode;
     107              : 
     108              :   /* True if not all uses of this comparison have been identified.  */
     109              :   bool missing_uses;
     110              : 
     111              :   /* True if its inputs are still valid at the end of the block.  */
     112              :   bool inputs_valid;
     113              : 
     114              :   /* Whether IN_A is wrapped in a NOT before being compared.  */
     115              :   bool not_in_a;
     116              : };
     117              : 
     118              : static vec<comparison *> all_compares;
     119              : 
     120              : /* Return whether X is a NOT unary expression.  */
     121              : 
     122              : static bool
     123     12598403 : is_not (rtx x)
     124              : {
     125     12598403 :   return GET_CODE (x) == NOT;
     126              : }
     127              : 
     128              : /* Strip a NOT unary expression around X, if any.  */
     129              : 
     130              : static rtx
     131      9086102 : strip_not (rtx x)
     132              : {
     133            0 :   if (is_not (x))
     134            0 :     return XEXP (x, 0);
     135              : 
     136              :   return x;
     137              : }
     138              : 
     139              : /* Look for a "conforming" comparison, as defined above.  If valid, return
     140              :    the rtx for the COMPARE itself.  */
     141              : 
     142              : static rtx
     143     60204448 : conforming_compare (rtx_insn *insn)
     144              : {
     145     60204448 :   rtx set, src, dest;
     146              : 
     147     60204448 :   set = single_set (insn);
     148     60204448 :   if (set == NULL)
     149              :     return NULL;
     150              : 
     151     56014841 :   src = SET_SRC (set);
     152     56014841 :   if (GET_CODE (src) != COMPARE)
     153              :     return NULL;
     154              : 
     155      4869348 :   dest = SET_DEST (set);
     156      4869348 :   if (!REG_P (dest) || REGNO (dest) != targetm.flags_regnum)
     157              :     return NULL;
     158              : 
     159      4869348 :   if (!REG_P (strip_not (XEXP (src, 0))))
     160              :     return NULL;
     161              : 
     162      3668914 :   if (CONSTANT_P (XEXP (src, 1)) || REG_P (XEXP (src, 1)))
     163              :     return src;
     164              : 
     165       156613 :   if (GET_CODE (XEXP (src, 1)) == UNSPEC)
     166              :     {
     167            0 :       for (int i = 0; i < XVECLEN (XEXP (src, 1), 0); i++)
     168            0 :         if (!REG_P (XVECEXP (XEXP (src, 1), 0, i)))
     169              :           return NULL;
     170              :       return src;
     171              :     }
     172              : 
     173              :   return NULL;
     174              : }
     175              : 
     176              : /* Look for a pattern of the "correct" form for an insn with a flags clobber
     177              :    for which we may be able to eliminate a compare later.  We're not looking
     178              :    to validate any inputs at this time, merely see that the basic shape is
     179              :    correct.  The term "arithmetic" may be somewhat misleading...  */
     180              : 
     181              : static bool
     182     12218031 : arithmetic_flags_clobber_p (rtx_insn *insn)
     183              : {
     184     12218031 :   rtx pat, x;
     185              : 
     186     12218031 :   if (!NONJUMP_INSN_P (insn))
     187              :     return false;
     188      7409135 :   pat = PATTERN (insn);
     189      7409135 :   if (asm_noperands (pat) >= 0)
     190              :     return false;
     191              : 
     192      7325958 :   if (GET_CODE (pat) == PARALLEL && XVECLEN (pat, 0) == 2)
     193              :     {
     194      5664915 :       x = XVECEXP (pat, 0, 0);
     195      5664915 :       if (GET_CODE (x) != SET)
     196              :         return false;
     197      5664908 :       x = SET_DEST (x);
     198      5664908 :       if (!REG_P (x))
     199              :         return false;
     200              : 
     201      5512369 :       x = XVECEXP (pat, 0, 1);
     202      5512369 :       if (GET_CODE (x) == CLOBBER)
     203              :         {
     204      5280454 :           x = XEXP (x, 0);
     205      5280454 :           if (REG_P (x) && REGNO (x) == targetm.flags_regnum)
     206      5229842 :             return true;
     207              :         }
     208              :     }
     209              : 
     210              :   return false;
     211              : }
     212              : 
     213              : /* Look for uses of FLAGS in INSN.  If we find one we can analyze, record
     214              :    it in CMP; otherwise indicate that we've missed a use.  */
     215              : 
     216              : static void
     217     14859310 : find_flags_uses_in_insn (struct comparison *cmp, rtx_insn *insn)
     218              : {
     219     14859310 :   df_ref use;
     220              : 
     221              :   /* If we've already lost track of uses, don't bother collecting more.  */
     222     14859310 :   if (cmp->missing_uses)
     223              :     return;
     224              : 
     225              :   /* Find a USE of the flags register.  */
     226     31975509 :   FOR_EACH_INSN_USE (use, insn)
     227     17144068 :     if (DF_REF_REGNO (use) == targetm.flags_regnum)
     228              :       {
     229      3567530 :         rtx x, *loc;
     230              : 
     231              :         /* If this is an unusual use, quit.  */
     232      3567530 :         if (DF_REF_TYPE (use) != DF_REF_REG_USE)
     233            0 :           goto fail;
     234              : 
     235              :         /* Unfortunately the location of the flags register, while present
     236              :            in the reference structure, doesn't help.  We need to find the
     237              :            comparison code that is outer to the actual flags use.  */
     238      3567530 :         loc = DF_REF_LOC (use);
     239      3567530 :         x = PATTERN (insn);
     240      3567530 :         if (GET_CODE (x) == PARALLEL)
     241        30544 :           x = XVECEXP (x, 0, 0);
     242      3567530 :         if (GET_CODE (x) == SET)
     243      3567530 :           x = SET_SRC (x);
     244      3567530 :         if (GET_CODE (x) == IF_THEN_ELSE)
     245      3447970 :           x = XEXP (x, 0);
     246      3567530 :         if (GET_CODE (x) == COND_EXEC)
     247            0 :           x = COND_EXEC_TEST (x);
     248      3567530 :         if (COMPARISON_P (x)
     249      3546621 :             && loc == &XEXP (x, 0)
     250      3540819 :             && XEXP (x, 1) == const0_rtx)
     251              :           {
     252              :             /* We've found a use of the flags that we understand.  */
     253      3540819 :             cmp->uses.safe_push (comparison_use { insn, loc, GET_CODE (x) });
     254              :           }
     255              :         else
     256        26711 :           goto fail;
     257              :       }
     258              :   return;
     259              : 
     260        26711 :  fail:
     261              :   /* We failed to recognize this use of the flags register.  */
     262        26711 :   cmp->missing_uses = true;
     263              : }
     264              : 
     265      2140454 : class find_comparison_dom_walker : public dom_walker
     266              : {
     267              : public:
     268      1070227 :   find_comparison_dom_walker (cdi_direction direction)
     269      2140454 :     : dom_walker (direction) {}
     270              : 
     271              :   edge before_dom_children (basic_block) final override;
     272              : };
     273              : 
     274              : /* Return true if conforming COMPARE with EH_NOTE is redundant with comparison
     275              :    CMP and can thus be eliminated.  */
     276              : 
     277              : static bool
     278       712351 : can_eliminate_compare (rtx compare, rtx eh_note, struct comparison *cmp)
     279              : {
     280              :   /* Take care that it's in the same EH region.  */
     281       712351 :   if (cfun->can_throw_non_call_exceptions
     282       712351 :       && !rtx_equal_p (eh_note, cmp->eh_note))
     283              :     return false;
     284              : 
     285              :   /* Make sure the compare is redundant with the previous.  */
     286       712351 :   if (!rtx_equal_p (strip_not (XEXP (compare, 0)), cmp->in_a)
     287       712351 :       || !rtx_equal_p (XEXP (compare, 1), cmp->in_b))
     288              :     return false;
     289              : 
     290         7898 :   if (is_not (XEXP (compare, 0)) != cmp->not_in_a)
     291              :     return false;
     292              : 
     293              :   /* New mode must be compatible with the previous compare mode.  */
     294         7898 :   machine_mode new_mode
     295         7898 :     = targetm.cc_modes_compatible (GET_MODE (compare), cmp->orig_mode);
     296              : 
     297         7898 :   if (new_mode == VOIDmode)
     298              :     return false;
     299              : 
     300         7898 :   if (cmp->orig_mode != new_mode)
     301              :     {
     302              :       /* Generate new comparison for substitution.  */
     303         1477 :       rtx flags = gen_rtx_REG (new_mode, targetm.flags_regnum);
     304         1477 :       rtx x = gen_rtx_COMPARE (new_mode, cmp->in_a, cmp->in_b);
     305         1477 :       x = gen_rtx_SET (flags, x);
     306              : 
     307         1477 :       if (!validate_change (cmp->insn, &PATTERN (cmp->insn), x, false))
     308              :         return false;
     309              : 
     310         1477 :       cmp->orig_mode = new_mode;
     311              :     }
     312              : 
     313              :   return true;
     314              : }
     315              : 
     316              : /* Identify comparison instructions within BB.  If the flags from the last
     317              :    compare in the BB is live at the end of the block, install the compare
     318              :    in BB->AUX.  Called via dom_walker.walk ().  */
     319              : 
     320              : edge
     321     12316899 : find_comparison_dom_walker::before_dom_children (basic_block bb)
     322              : {
     323     12316899 :   rtx_insn *insn, *next;
     324     12316899 :   bool need_purge = false;
     325     12316899 :   rtx_insn *last_setter[FIRST_PSEUDO_REGISTER];
     326              : 
     327              :   /* The last comparison that was made.  Will be reset to NULL
     328              :      once the flags are clobbered.  */
     329     12316899 :   struct comparison *last_cmp = NULL;
     330              : 
     331              :   /* True iff the last comparison has not been clobbered, nor
     332              :      have its inputs.  Used to eliminate duplicate compares.  */
     333     12316899 :   bool last_cmp_valid = false;
     334              : 
     335              :   /* The last insn that clobbered the flags, if that insn is of
     336              :      a form that may be valid for eliminating a following compare.
     337              :      To be reset to NULL once the flags are set otherwise.  */
     338     12316899 :   rtx_insn *last_clobber = NULL;
     339              : 
     340              :   /* Propagate the last live comparison throughout the extended basic block. */
     341     12316899 :   if (single_pred_p (bb))
     342              :     {
     343      8493438 :       last_cmp = (struct comparison *) single_pred (bb)->aux;
     344      8493438 :       if (last_cmp)
     345      4275772 :         last_cmp_valid = last_cmp->inputs_valid;
     346              :     }
     347              : 
     348     12316899 :   memset (last_setter, 0, sizeof (last_setter));
     349    148168176 :   for (insn = BB_HEAD (bb); insn; insn = next)
     350              :     {
     351    135851277 :       rtx src;
     352              : 
     353    135851277 :       next = (insn == BB_END (bb) ? NULL : NEXT_INSN (insn));
     354    135851277 :       if (!NONDEBUG_INSN_P (insn))
     355     75646829 :         continue;
     356              : 
     357     60204448 :       src = conforming_compare (insn);
     358     60204448 :       if (src)
     359              :         {
     360      3512301 :           rtx eh_note = NULL;
     361              : 
     362      3512301 :           if (cfun->can_throw_non_call_exceptions)
     363       595341 :             eh_note = find_reg_note (insn, REG_EH_REGION, NULL);
     364              : 
     365      3512301 :           if (last_cmp_valid && can_eliminate_compare (src, eh_note, last_cmp))
     366              :             {
     367         7898 :               if (eh_note)
     368            0 :                 need_purge = true;
     369         7898 :               delete_insn (insn);
     370         7898 :               continue;
     371              :             }
     372              : 
     373      3504403 :           last_cmp = new comparison ();
     374      3504403 :           last_cmp->insn = insn;
     375      3504403 :           last_cmp->prev_clobber = last_clobber;
     376      3504403 :           last_cmp->in_a = strip_not (XEXP (src, 0));
     377      3504403 :           last_cmp->in_b = XEXP (src, 1);
     378      3504403 :           last_cmp->not_in_a = is_not (XEXP (src, 0));
     379      3504403 :           last_cmp->eh_note = eh_note;
     380      3504403 :           last_cmp->orig_mode = GET_MODE (src);
     381      3504403 :           if (last_cmp->in_b == const0_rtx
     382      3504403 :               && last_setter[REGNO (last_cmp->in_a)])
     383              :             {
     384       971675 :               rtx set = single_set (last_setter[REGNO (last_cmp->in_a)]);
     385       971675 :               if (set && rtx_equal_p (SET_DEST (set), last_cmp->in_a))
     386       894925 :                 last_cmp->in_a_setter = last_setter[REGNO (last_cmp->in_a)];
     387              :             }
     388      3504403 :           all_compares.safe_push (last_cmp);
     389              : 
     390              :           /* It's unusual, but be prepared for comparison patterns that
     391              :              also clobber an input, or perhaps a scratch.  */
     392      3504403 :           last_clobber = NULL;
     393      3504403 :           last_cmp_valid = true;
     394              :         }
     395              : 
     396              :       else
     397              :         {
     398              :           /* Notice if this instruction uses the flags register.  */
     399     56692147 :           if (last_cmp)
     400     14859310 :             find_flags_uses_in_insn (last_cmp, insn);
     401              : 
     402              :           /* Notice if this instruction kills the flags register.  */
     403     56692147 :           df_ref def;
     404    148017238 :           FOR_EACH_INSN_DEF (def, insn)
     405    103543122 :             if (DF_REF_REGNO (def) == targetm.flags_regnum)
     406              :               {
     407              :                 /* See if this insn could be the "clobber" that eliminates
     408              :                    a future comparison.   */
     409     17447873 :                 last_clobber = (arithmetic_flags_clobber_p (insn)
     410     12218031 :                                 ? insn : NULL);
     411              : 
     412              :                 /* In either case, the previous compare is no longer valid.  */
     413     12218031 :                 last_cmp = NULL;
     414     12218031 :                 last_cmp_valid = false;
     415     12218031 :                 break;
     416              :               }
     417              :         }
     418              : 
     419              :       /* Notice if any of the inputs to the comparison have changed
     420              :          and remember last insn that sets each register.  */
     421     60196550 :       df_ref def;
     422    507097980 :       FOR_EACH_INSN_DEF (def, insn)
     423              :         {
     424    446901430 :           if (last_cmp_valid
     425    446901430 :               && (DF_REF_REGNO (def) == REGNO (last_cmp->in_a)
     426      8327383 :                   || (REG_P (last_cmp->in_b)
     427      2275157 :                       && DF_REF_REGNO (def) == REGNO (last_cmp->in_b))))
     428              :             last_cmp_valid = false;
     429    446901430 :           last_setter[DF_REF_REGNO (def)] = insn;
     430              :         }
     431              :     }
     432              : 
     433              :   /* Remember the live comparison for subsequent members of
     434              :      the extended basic block.  */
     435     12316899 :   if (last_cmp)
     436              :     {
     437      4607578 :       bb->aux = last_cmp;
     438      4607578 :       last_cmp->inputs_valid = last_cmp_valid;
     439              : 
     440              :       /* Look to see if the flags register is live outgoing here, and
     441              :          incoming to any successor not part of the extended basic block.  */
     442      4607578 :       if (bitmap_bit_p (df_get_live_out (bb), targetm.flags_regnum))
     443              :         {
     444        23655 :           edge e;
     445        23655 :           edge_iterator ei;
     446              : 
     447        70716 :           FOR_EACH_EDGE (e, ei, bb->succs)
     448              :             {
     449        47073 :               basic_block dest = e->dest;
     450        47073 :               if (bitmap_bit_p (df_get_live_in (dest), targetm.flags_regnum)
     451        70728 :                   && !single_pred_p (dest))
     452              :                 {
     453           12 :                   last_cmp->missing_uses = true;
     454           12 :                   break;
     455              :                 }
     456              :             }
     457              :         }
     458              :     }
     459              : 
     460              :   /* If we deleted a compare with a REG_EH_REGION note, we may need to
     461              :      remove EH edges.  */
     462     12316899 :   if (need_purge)
     463            0 :     purge_dead_edges (bb);
     464              : 
     465     12316899 :   return NULL;
     466              : }
     467              : 
     468              : /* Find all comparisons in the function.  */
     469              : 
     470              : static void
     471      1070227 : find_comparisons (void)
     472              : {
     473      1070227 :   calculate_dominance_info (CDI_DOMINATORS);
     474              : 
     475      1070227 :   find_comparison_dom_walker (CDI_DOMINATORS)
     476      1070227 :     .walk (cfun->cfg->x_entry_block_ptr);
     477              : 
     478      1070227 :   clear_aux_for_blocks ();
     479      1070227 :   free_dominance_info (CDI_DOMINATORS);
     480      1070227 : }
     481              : 
     482              : /* Select an alternate CC_MODE for a comparison insn comparing A and B.
     483              :    Note that inputs are almost certainly different than the IN_A and IN_B
     484              :    stored in CMP -- we're called while attempting to eliminate the compare
     485              :    after all.  Return the new FLAGS rtx if successful, else return NULL.
     486              :    Note that this function may start a change group.  */
     487              : 
     488              : static rtx
     489       792357 : maybe_select_cc_mode (struct comparison *cmp, rtx a ATTRIBUTE_UNUSED,
     490              :                       rtx b ATTRIBUTE_UNUSED)
     491              : {
     492       792357 :   machine_mode sel_mode;
     493       792357 :   const int n = cmp->uses.length ();
     494       792357 :   rtx flags = NULL;
     495              : 
     496              : #ifndef SELECT_CC_MODE
     497              :   /* Minimize code differences when this target macro is undefined.  */
     498              :   return NULL;
     499              : #define SELECT_CC_MODE(A,B,C) (gcc_unreachable (), VOIDmode)
     500              : #endif
     501              : 
     502              :   /* If we don't have access to all of the uses, we can't validate.  */
     503       792357 :   if (cmp->missing_uses || n == 0)
     504              :     return NULL;
     505              : 
     506              :   /* Find a new mode that works for all of the uses.  Special case the
     507              :      common case of exactly one use.  */
     508       791767 :   if (n == 1)
     509              :     {
     510       784947 :       sel_mode = SELECT_CC_MODE (cmp->uses[0].code, a, b);
     511       784947 :       if (sel_mode != cmp->orig_mode)
     512              :         {
     513         1363 :           flags = gen_rtx_REG (sel_mode, targetm.flags_regnum);
     514         1363 :           validate_change (cmp->uses[0].insn, cmp->uses[0].loc, flags, true);
     515              :         }
     516              :     }
     517              :   else
     518              :     {
     519         6820 :       int i;
     520              : 
     521         6820 :       sel_mode = SELECT_CC_MODE (cmp->uses[0].code, a, b);
     522        20844 :       for (i = 1; i < n; ++i)
     523              :         {
     524         7204 :           machine_mode new_mode = SELECT_CC_MODE (cmp->uses[i].code, a, b);
     525         7204 :           if (new_mode != sel_mode)
     526              :             {
     527         4877 :               sel_mode = targetm.cc_modes_compatible (sel_mode, new_mode);
     528         4877 :               if (sel_mode == VOIDmode)
     529              :                 return NULL;
     530              :             }
     531              :         }
     532              : 
     533         6820 :       if (sel_mode != cmp->orig_mode)
     534              :         {
     535            0 :           flags = gen_rtx_REG (sel_mode, targetm.flags_regnum);
     536            0 :           for (i = 0; i < n; ++i)
     537            0 :             validate_change (cmp->uses[i].insn, cmp->uses[i].loc, flags, true);
     538              :         }
     539              :     }
     540              : 
     541              :   return flags;
     542              : }
     543              : 
     544              : /* Return a register RTX holding the same value at START as REG at END, or
     545              :    NULL_RTX if there is none.  */
     546              : 
     547              : static rtx
     548      1414994 : equivalent_reg_at_start (rtx reg, rtx_insn *end, rtx_insn *start)
     549              : {
     550      1414994 :   machine_mode orig_mode = GET_MODE (reg);
     551      1414994 :   rtx_insn *bb_head = BB_HEAD (BLOCK_FOR_INSN (end));
     552              : 
     553      3190733 :   for (rtx_insn *insn = PREV_INSN (end);
     554      3190733 :        insn != start;
     555      1775739 :        insn = PREV_INSN (insn))
     556              :     {
     557      1898636 :       const int abnormal_flags
     558              :         = (DF_REF_CONDITIONAL | DF_REF_PARTIAL | DF_REF_MAY_CLOBBER
     559              :            | DF_REF_MUST_CLOBBER | DF_REF_SIGN_EXTRACT
     560              :            | DF_REF_ZERO_EXTRACT | DF_REF_STRICT_LOW_PART
     561              :            | DF_REF_PRE_POST_MODIFY);
     562      1898636 :       df_ref def;
     563              : 
     564              :       /* Note that the BB_HEAD is always either a note or a label, but in
     565              :          any case it means that REG is defined outside the block.  */
     566      1898636 :       if (insn == bb_head)
     567              :         return NULL_RTX;
     568      1898636 :       if (NOTE_P (insn) || DEBUG_INSN_P (insn))
     569      1498343 :         continue;
     570              : 
     571              :       /* Find a possible def of REG in INSN.  */
     572       573455 :       FOR_EACH_INSN_DEF (def, insn)
     573       306181 :         if (DF_REF_REGNO (def) == REGNO (reg))
     574              :           break;
     575              : 
     576              :       /* No definitions of REG; continue searching.  */
     577       400293 :       if (def == NULL)
     578       267274 :         continue;
     579              : 
     580              :       /* Bail if this is not a totally normal set of REG.  */
     581       133019 :       if (DF_REF_IS_ARTIFICIAL (def))
     582              :         return NULL_RTX;
     583       133019 :       if (DF_REF_FLAGS (def) & abnormal_flags)
     584              :         return NULL_RTX;
     585              : 
     586              :       /* We've found an insn between the compare and the clobber that sets
     587              :          REG.  Given that pass_cprop_hardreg has not yet run, we still find
     588              :          situations in which we can usefully look through a copy insn.  */
     589       133004 :       rtx x = single_set (insn);
     590       133004 :       if (x == NULL_RTX)
     591              :         return NULL_RTX;
     592       132645 :       reg = SET_SRC (x);
     593       132645 :       if (!REG_P (reg))
     594              :         return NULL_RTX;
     595              :     }
     596              : 
     597      1292097 :   if (GET_MODE (reg) != orig_mode)
     598          276 :     return NULL_RTX;
     599              : 
     600              :   return reg;
     601              : }
     602              : 
     603              : /* Return true if it is okay to merge the comparison CMP_INSN with
     604              :    the instruction ARITH_INSN.  Both instructions are assumed to be in the
     605              :    same basic block with ARITH_INSN appearing before CMP_INSN.  This checks
     606              :    that there are no uses or defs of the condition flags or control flow
     607              :    changes between the two instructions.  */
     608              : 
     609              : static bool
     610       894897 : can_merge_compare_into_arith (rtx_insn *cmp_insn, rtx_insn *arith_insn)
     611              : {
     612      2733703 :   for (rtx_insn *insn = PREV_INSN (cmp_insn);
     613      2733703 :        insn && insn != arith_insn;
     614      1838806 :        insn = PREV_INSN (insn))
     615              :     {
     616      1963700 :       if (!NONDEBUG_INSN_P (insn))
     617      1608068 :         continue;
     618              :       /* Bail if there are jumps or calls in between.  */
     619       355632 :       if (!NONJUMP_INSN_P (insn))
     620              :         return false;
     621              : 
     622              :       /* Bail on old-style asm statements because they lack
     623              :          data flow information.  */
     624       333387 :       if (GET_CODE (PATTERN (insn)) == ASM_INPUT)
     625              :         return false;
     626              : 
     627       333386 :       df_ref ref;
     628              :       /* Find a USE of the flags register.  */
     629       803952 :       FOR_EACH_INSN_USE (ref, insn)
     630       477766 :         if (DF_REF_REGNO (ref) == targetm.flags_regnum)
     631              :           return false;
     632              : 
     633              :       /* Find a DEF of the flags register.  */
     634       535265 :       FOR_EACH_INSN_DEF (ref, insn)
     635       304527 :         if (DF_REF_REGNO (ref) == targetm.flags_regnum)
     636              :           return false;
     637              :     }
     638              :   return true;
     639              : }
     640              : 
     641              : /* Given two SET expressions, SET_A and SET_B determine whether they form
     642              :    a recognizable pattern when emitted in parallel.  Return that parallel
     643              :    if so.  Otherwise return NULL.  */
     644              : 
     645              : static rtx_insn *
     646            2 : try_validate_parallel (rtx set_a, rtx set_b)
     647              : {
     648            2 :   rtx par = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set_a, set_b));
     649            2 :   rtx_insn *insn = make_insn_raw (par);
     650              : 
     651            2 :   if (insn_invalid_p (insn, false))
     652              :     {
     653            2 :       crtl->emit.x_cur_insn_uid--;
     654            2 :       return nullptr;
     655              :     }
     656              : 
     657            0 :   SET_PREV_INSN (insn) = NULL_RTX;
     658            0 :   SET_NEXT_INSN (insn) = NULL_RTX;
     659            0 :   insn->set_location (0);
     660            0 :   return insn;
     661              : }
     662              : 
     663              : /* For a comparison instruction described by CMP check if it compares a
     664              :    register with zero i.e. it is of the form CC := CMP R1, 0.
     665              :    If it is, find the instruction defining R1 (say I1) and try to create a
     666              :    PARALLEL consisting of I1 and the comparison, representing a flag-setting
     667              :    arithmetic instruction.  Example:
     668              :    I1: R1 := R2 + R3
     669              :    <instructions that don't read the condition register>
     670              :    I2: CC := CMP R1 0
     671              :    I2 can be merged with I1 into:
     672              :    I1: { CC := CMP (R2 + R3) 0 ; R1 := R2 + R3 }
     673              :    This catches cases where R1 is used between I1 and I2 and therefore
     674              :    combine and other RTL optimisations will not try to propagate it into
     675              :    I2.  Return true if we succeeded in merging CMP.  */
     676              : 
     677              : static bool
     678      3504403 : try_merge_compare (struct comparison *cmp)
     679              : {
     680      3504403 :   rtx_insn *cmp_insn = cmp->insn;
     681              : 
     682      3504403 :   if (cmp->in_b != const0_rtx || cmp->in_a_setter == NULL)
     683              :     return false;
     684       894925 :   rtx in_a = cmp->in_a;
     685       894925 :   df_ref use;
     686              : 
     687       894925 :   FOR_EACH_INSN_USE (use, cmp_insn)
     688       894925 :     if (DF_REF_REGNO (use) == REGNO (in_a))
     689              :       break;
     690       894925 :   if (!use)
     691              :     return false;
     692              : 
     693       894925 :   rtx_insn *def_insn = cmp->in_a_setter;
     694       894925 :   rtx set = single_set (def_insn);
     695       894925 :   if (!set)
     696              :     return false;
     697              : 
     698       894897 :   if (!can_merge_compare_into_arith (cmp_insn, def_insn))
     699              :     return false;
     700              : 
     701       770003 :   rtx src = SET_SRC (set);
     702              : 
     703              :   /* If the source uses addressing modes with side effects, we can't
     704              :      do the merge because we'd end up with a PARALLEL that has two
     705              :      instances of that side effect in it.  */
     706       770003 :   if (side_effects_p (src))
     707              :     return false;
     708              : 
     709       407411 :   rtx flags = maybe_select_cc_mode (cmp, src, CONST0_RTX (GET_MODE (src)));
     710       407411 :   if (!flags)
     711              :     {
     712              :     /* We may already have a change group going through maybe_select_cc_mode.
     713              :        Discard it properly.  */
     714       407409 :       cancel_changes (0);
     715       407409 :       return false;
     716              :     }
     717              : 
     718            2 :   rtx flag_set
     719            2 :     = gen_rtx_SET (flags, gen_rtx_COMPARE (GET_MODE (flags),
     720              :                                            copy_rtx (src),
     721              :                                            CONST0_RTX (GET_MODE (src))));
     722            2 :   rtx arith_set = copy_rtx (PATTERN (def_insn));
     723            2 :   rtx_insn *par = try_validate_parallel (flag_set, arith_set);
     724            2 :   if (!par)
     725              :     {
     726              :       /* We may already have a change group going through maybe_select_cc_mode.
     727              :          Discard it properly.  */
     728            2 :       cancel_changes (0);
     729            2 :       return false;
     730              :     }
     731            0 :   if (!apply_change_group ())
     732              :     return false;
     733            0 :   emit_insn_after (par, def_insn);
     734            0 :   delete_insn (def_insn);
     735            0 :   delete_insn (cmp->insn);
     736            0 :   return true;
     737              : }
     738              : 
     739              : /* Attempt to replace a comparison with a prior arithmetic insn that can
     740              :    compute the same flags value as the comparison itself.  Return true if
     741              :    successful, having made all rtl modifications necessary.  */
     742              : 
     743              : static bool
     744      3504403 : try_eliminate_compare (struct comparison *cmp)
     745              : {
     746      3504403 :   rtx flags, in_a, in_b, cmp_a, cmp_b;
     747              : 
     748      3504403 :   if (try_merge_compare (cmp))
     749              :     return true;
     750              : 
     751              :   /* We must have found an interesting "clobber" preceding the compare.  */
     752      3504403 :   if (cmp->prev_clobber == NULL)
     753              :     return false;
     754              : 
     755              :   /* Verify that IN_A is not clobbered in between CMP and PREV_CLOBBER.
     756              :      Given that this target requires this pass, we can assume that most
     757              :      insns do clobber the flags, and so the distance between the compare
     758              :      and the clobber is likely to be small.  */
     759              :   /* ??? This is one point at which one could argue that DF_REF_CHAIN would
     760              :      be useful, but it is thought to be too heavy-weight a solution here.  */
     761      1005152 :   in_a = equivalent_reg_at_start (cmp->in_a, cmp->insn, cmp->prev_clobber);
     762      1005152 :   if (!in_a)
     763              :     return false;
     764              : 
     765              :   /* Likewise for IN_B if need be.  */
     766       924993 :   if (CONSTANT_P (cmp->in_b))
     767              :     in_b = cmp->in_b;
     768       409842 :   else if (REG_P (cmp->in_b))
     769              :     {
     770       409842 :       in_b = equivalent_reg_at_start (cmp->in_b, cmp->insn, cmp->prev_clobber);
     771       409842 :       if (!in_b)
     772              :         return false;
     773              :     }
     774            0 :   else if (GET_CODE (cmp->in_b) == UNSPEC)
     775              :     {
     776            0 :       const int len = XVECLEN (cmp->in_b, 0);
     777            0 :       rtvec v = rtvec_alloc (len);
     778            0 :       for (int i = 0; i < len; i++)
     779              :         {
     780            0 :           rtx r = equivalent_reg_at_start (XVECEXP (cmp->in_b, 0, i),
     781              :                                            cmp->insn, cmp->prev_clobber);
     782            0 :           if (!r)
     783              :             return false;
     784            0 :           RTVEC_ELT (v, i) = r;
     785              :         }
     786            0 :       in_b = gen_rtx_UNSPEC (GET_MODE (cmp->in_b), v, XINT (cmp->in_b, 1));
     787              :     }
     788              :   else
     789            0 :     gcc_unreachable ();
     790              : 
     791              :   /* We've reached PREV_CLOBBER without finding a modification of IN_A.
     792              :      Validate that PREV_CLOBBER itself does in fact refer to IN_A.  Do
     793              :      recall that we've already validated the shape of PREV_CLOBBER.  */
     794       881979 :   rtx_insn *insn = cmp->prev_clobber;
     795              : 
     796       881979 :   rtx x = XVECEXP (PATTERN (insn), 0, 0);
     797       881979 :   if (rtx_equal_p (SET_DEST (x), in_a))
     798       381240 :     cmp_a = SET_SRC (x);
     799              : 
     800              :   /* Also check operations with implicit extensions, e.g.:
     801              :      [(set (reg:DI)
     802              :            (zero_extend:DI (plus:SI (reg:SI) (reg:SI))))
     803              :       (set (reg:CCZ flags)
     804              :            (compare:CCZ (plus:SI (reg:SI) (reg:SI))
     805              :                         (const_int 0)))] */
     806       500739 :   else if (REG_P (SET_DEST (x))
     807       500739 :            && REG_P (in_a)
     808       500739 :            && REGNO (SET_DEST (x)) == REGNO (in_a)
     809        26899 :            && (GET_CODE (SET_SRC (x)) == ZERO_EXTEND
     810        26899 :                || GET_CODE (SET_SRC (x)) == SIGN_EXTEND)
     811       504693 :            && GET_MODE (XEXP (SET_SRC (x), 0)) == GET_MODE (in_a))
     812              :     cmp_a = XEXP (SET_SRC (x), 0);
     813              : 
     814              :   /* Also check fully redundant comparisons, e.g.:
     815              :      [(set (reg:SI)
     816              :            (minus:SI (reg:SI) (reg:SI))))
     817              :       (set (reg:CC flags)
     818              :            (compare:CC (reg:SI) (reg:SI)))] */
     819       496789 :   else if (REG_P (in_b)
     820       251241 :            && GET_CODE (SET_SRC (x)) == MINUS
     821        13257 :            && rtx_equal_p (XEXP (SET_SRC (x), 0), in_a)
     822       496789 :            && rtx_equal_p (XEXP (SET_SRC (x), 1), in_b))
     823              :     cmp_a = in_a;
     824              : 
     825              :   else
     826              :     return false;
     827              : 
     828              :   /* If the source uses addressing modes with side effects, we can't
     829              :      do the merge because we'd end up with a PARALLEL that has two
     830              :      instances of that side effect in it.  */
     831       385190 :   if (side_effects_p (cmp_a))
     832              :     return false;
     833              : 
     834       384946 :   if (in_a == in_b)
     835              :     cmp_b = cmp_a;
     836       384941 :   else if (rtx_equal_p (SET_DEST (x), in_b))
     837            0 :     cmp_b = SET_SRC (x);
     838              :   else
     839              :     cmp_b = in_b;
     840       384946 :   if (side_effects_p (cmp_b))
     841              :     return false;
     842              : 
     843              :   /* Determine if we ought to use a different CC_MODE here.  */
     844       384946 :   flags = maybe_select_cc_mode (cmp, cmp_a, cmp_b);
     845       384946 :   if (flags == NULL)
     846       383585 :     flags = gen_rtx_REG (cmp->orig_mode, targetm.flags_regnum);
     847              : 
     848              :   /* Generate a new comparison for installation in the setter.  */
     849       384946 :   rtx y = cmp->not_in_a
     850       384946 :           ? gen_rtx_NOT (GET_MODE (cmp_a), copy_rtx (cmp_a))
     851       384946 :           : copy_rtx (cmp_a);
     852       384946 :   y = gen_rtx_COMPARE (GET_MODE (flags), y, copy_rtx (cmp_b));
     853       384946 :   y = gen_rtx_SET (flags, y);
     854              : 
     855              :   /* Canonicalize instruction to:
     856              :      [(set (reg:CCM) (compare:CCM (operation) (immediate)))
     857              :       (set (reg) (operation)]  */
     858              : 
     859       384946 :   rtvec v = rtvec_alloc (2);
     860       384946 :   RTVEC_ELT (v, 0) = y;
     861       384946 :   RTVEC_ELT (v, 1) = x;
     862              : 
     863       384946 :   rtx pat = gen_rtx_PARALLEL (VOIDmode, v);
     864              : 
     865              :   /* Succeed if the new instruction is valid.  Note that we may have started
     866              :      a change group within maybe_select_cc_mode, therefore we must continue. */
     867       384946 :   validate_change (insn, &PATTERN (insn), pat, true);
     868              : 
     869       384946 :   if (!apply_change_group ())
     870              :     return false;
     871              : 
     872              :   /* Success.  Delete the compare insn...  */
     873        38490 :   delete_insn (cmp->insn);
     874              : 
     875              :   /* ... and any notes that are now invalid due to multiple sets.  */
     876        38490 :   x = find_regno_note (insn, REG_UNUSED, targetm.flags_regnum);
     877        38490 :   if (x)
     878            0 :     remove_note (insn, x);
     879        38490 :   x = find_reg_note (insn, REG_EQUAL, NULL);
     880        38490 :   if (x)
     881         4147 :     remove_note (insn, x);
     882        38490 :   x = find_reg_note (insn, REG_EQUIV, NULL);
     883        38490 :   if (x)
     884            0 :     remove_note (insn, x);
     885              : 
     886              :   return true;
     887              : }
     888              : 
     889              : /* Main entry point to the pass.  */
     890              : 
     891              : static unsigned int
     892      1070227 : execute_compare_elim_after_reload (void)
     893              : {
     894      1070227 :   df_set_flags (DF_LR_RUN_DCE);
     895      1070227 :   df_analyze ();
     896              : 
     897      1070227 :   gcc_checking_assert (!all_compares.exists ());
     898              : 
     899              :   /* Locate all comparisons and their uses, and eliminate duplicates.  */
     900      1070227 :   find_comparisons ();
     901      1070227 :   if (all_compares.exists ())
     902              :     {
     903              :       /* Eliminate comparisons that are redundant with flags computation.  */
     904      3976467 :       for (auto &cmp : all_compares)
     905              :         {
     906      3504403 :           try_eliminate_compare (cmp);
     907      7008806 :           delete cmp;
     908      3504403 :           cmp = nullptr;
     909              :         }
     910              : 
     911       472064 :       all_compares.release ();
     912              :     }
     913              : 
     914      1070227 :   return 0;
     915              : }
     916              : 
     917              : namespace {
     918              : 
     919              : const pass_data pass_data_compare_elim_after_reload =
     920              : {
     921              :   RTL_PASS, /* type */
     922              :   "cmpelim", /* name */
     923              :   OPTGROUP_NONE, /* optinfo_flags */
     924              :   TV_NONE, /* tv_id */
     925              :   0, /* properties_required */
     926              :   0, /* properties_provided */
     927              :   0, /* properties_destroyed */
     928              :   0, /* todo_flags_start */
     929              :   ( TODO_df_finish | TODO_df_verify ), /* todo_flags_finish */
     930              : };
     931              : 
     932              : class pass_compare_elim_after_reload : public rtl_opt_pass
     933              : {
     934              : public:
     935       302985 :   pass_compare_elim_after_reload (gcc::context *ctxt)
     936       605970 :     : rtl_opt_pass (pass_data_compare_elim_after_reload, ctxt)
     937              :   {}
     938              : 
     939              :   /* opt_pass methods: */
     940      1531131 :   bool gate (function *) final override
     941              :     {
     942              :       /* Setting this target hook value is how a backend indicates the need.  */
     943      1531131 :       if (targetm.flags_regnum == INVALID_REGNUM)
     944              :         return false;
     945      1531131 :       return flag_compare_elim_after_reload;
     946              :     }
     947              : 
     948      1070227 :   unsigned int execute (function *) final override
     949              :     {
     950      1070227 :       return execute_compare_elim_after_reload ();
     951              :     }
     952              : 
     953              : }; // class pass_compare_elim_after_reload
     954              : 
     955              : } // anon namespace
     956              : 
     957              : rtl_opt_pass *
     958       302985 : make_pass_compare_elim_after_reload (gcc::context *ctxt)
     959              : {
     960       302985 :   return new pass_compare_elim_after_reload (ctxt);
     961              : }
        

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.