LCOV - code coverage report
Current view: top level - gcc - rtl-tests.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 100.0 % 106 106
Test Date: 2026-10-17 16:30:31 Functions: 100.0 % 9 9
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Unit tests for RTL-handling.
       2              :    Copyright (C) 2015-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 "tm.h"
      24              : #include "opts.h"
      25              : #include "hash-set.h"
      26              : #include "fixed-value.h"
      27              : #include "alias.h"
      28              : #include "flags.h"
      29              : #include "symtab.h"
      30              : #include "tree-core.h"
      31              : #include "stor-layout.h"
      32              : #include "tree.h"
      33              : #include "stringpool.h"
      34              : #include "rtl.h"
      35              : #include "pretty-print.h"
      36              : #include "cfgbuild.h"
      37              : #include "print-rtl.h"
      38              : #include "selftest.h"
      39              : #include "selftest-rtl.h"
      40              : #include "function.h"
      41              : #include "memmodel.h"
      42              : #include "emit-rtl.h"
      43              : 
      44              : #if CHECKING_P
      45              : 
      46              : namespace selftest {
      47              : 
      48              : /* Verify that PAT is printed as EXPECTED.  Helper function for
      49              :    selftests.  */
      50              : 
      51              : static void
      52            4 : verify_print_pattern (const char *expected, rtx pat)
      53              : {
      54            4 :   pretty_printer pp;
      55            4 :   print_pattern (&pp, pat, 1);
      56            4 :   ASSERT_STREQ (expected, pp_formatted_text (&pp));
      57            4 : }
      58              : 
      59              : /* Verify that X is dumped as EXPECTED_DUMP, using compact mode.
      60              :    Use LOC as the effective location when reporting errors.  */
      61              : 
      62              : void
      63          100 : assert_rtl_dump_eq (const location &loc, const char *expected_dump, rtx x,
      64              :                     rtx_reuse_manager *reuse_manager)
      65              : {
      66          100 :   named_temp_file tmp_out (".rtl");
      67          100 :   FILE *outfile = fopen (tmp_out.get_filename (), "w");
      68          100 :   rtx_writer w (outfile, 0, false, true, reuse_manager);
      69          100 :   w.print_rtl (x);
      70          100 :   fclose (outfile);
      71              : 
      72          100 :   char *dump = read_file (SELFTEST_LOCATION, tmp_out.get_filename ());
      73          100 :   ASSERT_STREQ_AT (loc, expected_dump, dump);
      74          100 :   free (dump);
      75          100 : }
      76              : 
      77              : /* Verify that regs are dumped as expected (in compact mode).  */
      78              : 
      79              : static void
      80            4 : test_dumping_regs ()
      81              : {
      82              :   /* Dumps of hard regs contain a target-specific name, so we don't test
      83              :      it here; this can be tested in target-specific selftests.  */
      84              : 
      85              :   /* Test dumping of virtual regs.  The various virtual regs are inited as
      86              :      Pmode, so this is target-specific.  The tests below assume DImode, so
      87              :      only run the tests for targets where Pmode is DImode.  */
      88            4 :   if (Pmode == DImode)
      89              :     {
      90            4 :       ASSERT_RTL_DUMP_EQ ("(reg:DI virtual-incoming-args)",
      91              :                           virtual_incoming_args_rtx);
      92            4 :       ASSERT_RTL_DUMP_EQ ("(reg:DI virtual-stack-vars)",
      93              :                           virtual_stack_vars_rtx);
      94            4 :       ASSERT_RTL_DUMP_EQ ("(reg:DI virtual-stack-dynamic)",
      95              :                           virtual_stack_dynamic_rtx);
      96            4 :       ASSERT_RTL_DUMP_EQ ("(reg:DI virtual-outgoing-args)",
      97              :                           virtual_outgoing_args_rtx);
      98            4 :       ASSERT_RTL_DUMP_EQ ("(reg:DI virtual-cfa)",
      99              :                           virtual_cfa_rtx);
     100            4 :       ASSERT_RTL_DUMP_EQ ("(reg:DI virtual-preferred-stack-boundary)",
     101              :                           virtual_preferred_stack_boundary_rtx);
     102              :     }
     103              : 
     104              :   /* Test dumping of non-virtual pseudos.  */
     105            4 :   ASSERT_RTL_DUMP_EQ ("(reg:SI <0>)",
     106              :     gen_raw_REG (SImode, LAST_VIRTUAL_REGISTER + 1));
     107            4 :   ASSERT_RTL_DUMP_EQ ("(reg:SI <1>)",
     108              :     gen_raw_REG (SImode, LAST_VIRTUAL_REGISTER + 2));
     109            4 : }
     110              : 
     111              : /* Verify that insns are dumped as expected (in compact mode).  */
     112              : 
     113              : static void
     114            4 : test_dumping_insns ()
     115              : {
     116              :   /* Barriers.  */
     117            4 :   rtx_barrier *barrier = as_a <rtx_barrier *> (rtx_alloc (BARRIER));
     118            4 :   SET_NEXT_INSN (barrier) = NULL;
     119            4 :   ASSERT_RTL_DUMP_EQ ("(cbarrier 0)\n", barrier);
     120              : 
     121              :   /* Labels.  */
     122            4 :   rtx_insn *label = gen_label_rtx ();
     123            4 :   CODE_LABEL_NUMBER (label) = 42;
     124            4 :   ASSERT_RTL_DUMP_EQ ("(clabel 0 42)\n", label);
     125              : 
     126            4 :   LABEL_NAME (label)= "some_label";
     127            4 :   ASSERT_RTL_DUMP_EQ ("(clabel 0 42 (\"some_label\"))\n", label);
     128            4 : }
     129              : 
     130              : /* Manually exercise the rtx_reuse_manager code.  */
     131              : 
     132              : static void
     133            4 : test_dumping_rtx_reuse ()
     134              : {
     135            4 :   rtx_reuse_manager r;
     136              : 
     137            4 :   rtx x = rtx_alloc (SCRATCH);
     138            4 :   rtx y = rtx_alloc (SCRATCH);
     139            4 :   rtx z = rtx_alloc (SCRATCH);
     140              : 
     141              :   /* x and y will be seen more than once.  */
     142            4 :   r.preprocess (x);
     143            4 :   r.preprocess (x);
     144            4 :   r.preprocess (y);
     145            4 :   r.preprocess (y);
     146              : 
     147              :   /* z will be only seen once.  */
     148            4 :   r.preprocess (z);
     149              : 
     150              :   /* Verify that x and y have been assigned reuse IDs.  */
     151            4 :   int reuse_id_for_x;
     152            4 :   ASSERT_TRUE (r.has_reuse_id (x, &reuse_id_for_x));
     153            4 :   ASSERT_EQ (0, reuse_id_for_x);
     154              : 
     155            4 :   int reuse_id_for_y;
     156            4 :   ASSERT_TRUE (r.has_reuse_id (y, &reuse_id_for_y));
     157            4 :   ASSERT_EQ (1, reuse_id_for_y);
     158              : 
     159              :   /* z is only seen once and thus shouldn't get a reuse ID.  */
     160            4 :   ASSERT_FALSE (r.has_reuse_id (z, NULL));
     161              : 
     162              :   /* The first dumps of x and y should be prefixed by reuse ID;
     163              :      all subsequent dumps of them should show up as "reuse_rtx".  */
     164            4 :   ASSERT_RTL_DUMP_EQ_WITH_REUSE ("(0|scratch)", x, &r);
     165            4 :   ASSERT_RTL_DUMP_EQ_WITH_REUSE ("(reuse_rtx 0)", x, &r);
     166            4 :   ASSERT_RTL_DUMP_EQ_WITH_REUSE ("(reuse_rtx 0)", x, &r);
     167              : 
     168            4 :   ASSERT_RTL_DUMP_EQ_WITH_REUSE ("(1|scratch)", y, &r);
     169            4 :   ASSERT_RTL_DUMP_EQ_WITH_REUSE ("(reuse_rtx 1)", y, &r);
     170            4 :   ASSERT_RTL_DUMP_EQ_WITH_REUSE ("(reuse_rtx 1)", y, &r);
     171              : 
     172              :   /* z only appears once and thus shouldn't be prefixed with a
     173              :      reuse ID.  */
     174            4 :   ASSERT_RTL_DUMP_EQ_WITH_REUSE ("(scratch)", z, &r);
     175            4 : }
     176              : 
     177              : /* Unit testing of "single_set".  */
     178              : 
     179              : static void
     180            4 : test_single_set ()
     181              : {
     182              :   /* A label is not a SET.  */
     183            4 :   ASSERT_EQ (NULL_RTX, single_set (gen_label_rtx ()));
     184              : 
     185              :   /* An unconditional jump insn is a single SET.  */
     186            4 :   rtx set_pc = gen_rtx_SET (pc_rtx,
     187              :                             gen_rtx_LABEL_REF (VOIDmode,
     188              :                                                gen_label_rtx ()));
     189            4 :   rtx_insn *jump_insn = emit_jump_insn (set_pc);
     190            4 :   ASSERT_EQ (set_pc, single_set (jump_insn));
     191              : 
     192              :   /* etc */
     193            4 : }
     194              : 
     195              : /* Construct an unconditional jump to a label, and verify that
     196              :    various properties of it are sane.  */
     197              : 
     198              : static void
     199            4 : test_uncond_jump ()
     200              : {
     201            4 :   set_new_first_and_last_insn (NULL, NULL);
     202            4 :   rtx_insn *label = gen_label_rtx ();
     203            4 :   rtx jump_pat = gen_rtx_SET (pc_rtx,
     204              :                               gen_rtx_LABEL_REF (VOIDmode,
     205              :                                                  label));
     206            4 :   ASSERT_EQ (SET, jump_pat->code);
     207            4 :   ASSERT_EQ (LABEL_REF, SET_SRC (jump_pat)->code);
     208            4 :   ASSERT_EQ (label, label_ref_label (SET_SRC (jump_pat)));
     209            4 :   ASSERT_EQ (PC, SET_DEST (jump_pat)->code);
     210              : 
     211            4 :   verify_print_pattern ("pc=L0", jump_pat);
     212              : 
     213            4 :   ASSERT_RTL_DUMP_EQ ("(set (pc)\n"
     214              :                       "    (label_ref 0))",
     215              :                       jump_pat);
     216              : 
     217            4 :   rtx_insn *jump_insn = emit_jump_insn (jump_pat);
     218            4 :   ASSERT_FALSE (any_condjump_p (jump_insn));
     219            4 :   ASSERT_TRUE (any_uncondjump_p (jump_insn));
     220            4 :   ASSERT_TRUE (pc_set (jump_insn));
     221            4 :   ASSERT_TRUE (simplejump_p (jump_insn));
     222            4 :   ASSERT_TRUE (onlyjump_p (jump_insn));
     223            4 :   ASSERT_TRUE (control_flow_insn_p (jump_insn));
     224              : 
     225            4 :   ASSERT_RTL_DUMP_EQ ("(cjump_insn 1 (set (pc)\n"
     226              :                       "        (label_ref 0)))\n",
     227              :                       jump_insn);
     228            4 : }
     229              : 
     230              : template<unsigned int N>
     231              : struct const_poly_int_tests
     232              : {
     233              :   static void run ();
     234              : };
     235              : 
     236              : template<>
     237              : struct const_poly_int_tests<1>
     238              : {
     239              :   static void run () {}
     240              : };
     241              : 
     242              : /* Test various CONST_POLY_INT properties.  */
     243              : 
     244              : template<unsigned int N>
     245              : void
     246              : const_poly_int_tests<N>::run ()
     247              : {
     248              :   using poly_int64 = poly_int<N, HOST_WIDE_INT>;
     249              :   rtx x1 = gen_int_mode (poly_int64 (1, 1), QImode);
     250              :   rtx x255 = gen_int_mode (poly_int64 (1, 255), QImode);
     251              : 
     252              :   /* Test that constants are unique.  */
     253              :   ASSERT_EQ (x1, gen_int_mode (poly_int64 (1, 1), QImode));
     254              :   ASSERT_NE (x1, gen_int_mode (poly_int64 (1, 1), HImode));
     255              :   ASSERT_NE (x1, x255);
     256              : 
     257              :   /* Test const_poly_int_value.  */
     258              :   ASSERT_KNOWN_EQ (const_poly_int_value (x1), poly_int64 (1, 1));
     259              :   ASSERT_KNOWN_EQ (const_poly_int_value (x255), poly_int64 (1, -1));
     260              : 
     261              :   /* Test rtx_to_poly_int64.  */
     262              :   ASSERT_KNOWN_EQ (rtx_to_poly_int64 (x1), poly_int64 (1, 1));
     263              :   ASSERT_KNOWN_EQ (rtx_to_poly_int64 (x255), poly_int64 (1, -1));
     264              :   ASSERT_MAYBE_NE (rtx_to_poly_int64 (x255), poly_int64 (1, 255));
     265              : 
     266              :   /* Test plus_constant of a symbol.  */
     267              :   rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "foo");
     268              :   rtx offset1 = gen_int_mode (poly_int64 (9, 11), Pmode);
     269              :   rtx sum1 = gen_rtx_CONST (Pmode, gen_rtx_PLUS (Pmode, symbol, offset1));
     270              :   ASSERT_RTX_EQ (plus_constant (Pmode, symbol, poly_int64 (9, 11)), sum1);
     271              : 
     272              :   /* Test plus_constant of a CONST.  */
     273              :   rtx offset2 = gen_int_mode (poly_int64 (12, 20), Pmode);
     274              :   rtx sum2 = gen_rtx_CONST (Pmode, gen_rtx_PLUS (Pmode, symbol, offset2));
     275              :   ASSERT_RTX_EQ (plus_constant (Pmode, sum1, poly_int64 (3, 9)), sum2);
     276              : 
     277              :   /* Test a cancelling plus_constant.  */
     278              :   ASSERT_EQ (plus_constant (Pmode, sum2, poly_int64 (-12, -20)), symbol);
     279              : 
     280              :   /* Test plus_constant on integer constants.  */
     281              :   ASSERT_EQ (plus_constant (QImode, const1_rtx, poly_int64 (4, -2)),
     282              :              gen_int_mode (poly_int64 (5, -2), QImode));
     283              :   ASSERT_EQ (plus_constant (QImode, x1, poly_int64 (4, -2)),
     284              :              gen_int_mode (poly_int64 (5, -1), QImode));
     285              : }
     286              : 
     287              : /* Check dumping of repeated RTL vectors.  */
     288              : 
     289              : static void
     290            4 : test_dumping_repeat ()
     291              : {
     292            4 :   rtx p = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (3));
     293            4 :   XVECEXP (p, 0, 0) = const0_rtx;
     294            4 :   XVECEXP (p, 0, 1) = const0_rtx;
     295            4 :   XVECEXP (p, 0, 2) = const0_rtx;
     296            4 :   ASSERT_RTL_DUMP_EQ ("(parallel [\n"
     297              :                       "        (const_int 0) repeated x3\n"
     298              :                       "    ])",
     299              :                       p);
     300              : 
     301            4 :   XVECEXP (p, 0, 1) = const1_rtx;
     302            4 :   ASSERT_RTL_DUMP_EQ ("(parallel [\n"
     303              :                       "        (const_int 0)\n"
     304              :                       "        (const_int 1)\n"
     305              :                       "        (const_int 0)\n"
     306              :                       "    ])",
     307              :                       p);
     308            4 : }
     309              : 
     310              : /* Run all of the selftests within this file.  */
     311              : 
     312              : void
     313            4 : rtl_tests_cc_tests ()
     314              : {
     315            4 :   test_dumping_regs ();
     316            4 :   test_dumping_insns ();
     317            4 :   test_dumping_rtx_reuse ();
     318            4 :   test_single_set ();
     319            4 :   test_uncond_jump ();
     320            4 :   const_poly_int_tests<NUM_POLY_INT_COEFFS>::run ();
     321            4 :   test_dumping_repeat ();
     322              : 
     323              :   /* Purge state.  */
     324            4 :   set_first_insn (NULL);
     325            4 :   set_last_insn (NULL);
     326            4 : }
     327              : 
     328              : } // namespace selftest
     329              : #endif /* #if CHECKING_P */
        

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.