xref: /dflybsd-src/contrib/gcc-8.0/gcc/selftest-rtl.c (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj /* Selftest support for RTL.
2*38fd1498Szrj    Copyright (C) 2016-2018 Free Software Foundation, Inc.
3*38fd1498Szrj 
4*38fd1498Szrj This file is part of GCC.
5*38fd1498Szrj 
6*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
7*38fd1498Szrj the terms of the GNU General Public License as published by the Free
8*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
9*38fd1498Szrj version.
10*38fd1498Szrj 
11*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14*38fd1498Szrj for more details.
15*38fd1498Szrj 
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with GCC; see the file COPYING3.  If not see
18*38fd1498Szrj <http://www.gnu.org/licenses/>.  */
19*38fd1498Szrj 
20*38fd1498Szrj #include "config.h"
21*38fd1498Szrj #include "system.h"
22*38fd1498Szrj #include "coretypes.h"
23*38fd1498Szrj #include "selftest.h"
24*38fd1498Szrj #include "backend.h"
25*38fd1498Szrj #include "target.h"
26*38fd1498Szrj #include "rtl.h"
27*38fd1498Szrj #include "read-rtl-function.h"
28*38fd1498Szrj #include "read-md.h"
29*38fd1498Szrj #include "tree-core.h"
30*38fd1498Szrj #include "memmodel.h"
31*38fd1498Szrj #include "emit-rtl.h"
32*38fd1498Szrj #include "selftest-rtl.h"
33*38fd1498Szrj 
34*38fd1498Szrj #if CHECKING_P
35*38fd1498Szrj 
36*38fd1498Szrj namespace selftest {
37*38fd1498Szrj 
38*38fd1498Szrj /* Compare rtx EXPECTED and ACTUAL using rtx_equal_p, calling
39*38fd1498Szrj    ::selftest::pass if they are equal, aborting if they are non-equal.
40*38fd1498Szrj    LOC is the effective location of the assertion, MSG describes it.  */
41*38fd1498Szrj 
42*38fd1498Szrj void
assert_rtx_eq_at(const location & loc,const char * msg,rtx expected,rtx actual)43*38fd1498Szrj assert_rtx_eq_at (const location &loc, const char *msg,
44*38fd1498Szrj 		  rtx expected, rtx actual)
45*38fd1498Szrj {
46*38fd1498Szrj   if (rtx_equal_p (expected, actual))
47*38fd1498Szrj     ::selftest::pass (loc, msg);
48*38fd1498Szrj   else
49*38fd1498Szrj     {
50*38fd1498Szrj       fprintf (stderr, "%s:%i: %s: FAIL: %s\n", loc.m_file, loc.m_line,
51*38fd1498Szrj 	       loc.m_function, msg);
52*38fd1498Szrj       fprintf (stderr, "  expected: ");
53*38fd1498Szrj       print_rtl (stderr, expected);
54*38fd1498Szrj       fprintf (stderr, "\n  actual: ");
55*38fd1498Szrj       print_rtl (stderr, actual);
56*38fd1498Szrj       fprintf (stderr, "\n");
57*38fd1498Szrj       abort ();
58*38fd1498Szrj     }
59*38fd1498Szrj }
60*38fd1498Szrj 
61*38fd1498Szrj /* Compare rtx EXPECTED and ACTUAL by pointer equality, calling
62*38fd1498Szrj    ::selftest::pass if they are equal, aborting if they are non-equal.
63*38fd1498Szrj    LOC is the effective location of the assertion, MSG describes it.  */
64*38fd1498Szrj 
65*38fd1498Szrj void
assert_rtx_ptr_eq_at(const location & loc,const char * msg,rtx expected,rtx actual)66*38fd1498Szrj assert_rtx_ptr_eq_at (const location &loc, const char *msg,
67*38fd1498Szrj 		      rtx expected, rtx actual)
68*38fd1498Szrj {
69*38fd1498Szrj   if (expected == actual)
70*38fd1498Szrj     ::selftest::pass (loc, msg);
71*38fd1498Szrj   else
72*38fd1498Szrj     {
73*38fd1498Szrj       fprintf (stderr, "%s:%i: %s: FAIL: %s\n", loc.m_file, loc.m_line,
74*38fd1498Szrj 	       loc.m_function, msg);
75*38fd1498Szrj       fprintf (stderr, "  expected (at %p): ", (void *)expected);
76*38fd1498Szrj       print_rtl (stderr, expected);
77*38fd1498Szrj       fprintf (stderr, "\n  actual (at %p): ", (void *)actual);
78*38fd1498Szrj       print_rtl (stderr, actual);
79*38fd1498Szrj       fprintf (stderr, "\n");
80*38fd1498Szrj       abort ();
81*38fd1498Szrj     }
82*38fd1498Szrj }
83*38fd1498Szrj 
84*38fd1498Szrj /* Constructor for selftest::rtl_dump_test.
85*38fd1498Szrj    Read a dumped RTL function from PATH.
86*38fd1498Szrj    Takes ownership of PATH, freeing in dtor.
87*38fd1498Szrj    Use LOC as the effective location when reporting failures.  */
88*38fd1498Szrj 
rtl_dump_test(const location & loc,char * path)89*38fd1498Szrj rtl_dump_test::rtl_dump_test (const location &loc, char *path)
90*38fd1498Szrj   : m_path (path)
91*38fd1498Szrj {
92*38fd1498Szrj   bool read_ok = read_rtl_function_body (path);
93*38fd1498Szrj   ASSERT_TRUE_AT (loc, read_ok);
94*38fd1498Szrj }
95*38fd1498Szrj 
96*38fd1498Szrj /* Destructor for selftest::rtl_dump_test.
97*38fd1498Szrj    Cleanup global state relating to the function, and free the path.  */
98*38fd1498Szrj 
~rtl_dump_test()99*38fd1498Szrj selftest::rtl_dump_test::~rtl_dump_test ()
100*38fd1498Szrj {
101*38fd1498Szrj   /* Cleanups.  */
102*38fd1498Szrj   current_function_decl = NULL;
103*38fd1498Szrj   free_after_compilation (cfun);
104*38fd1498Szrj   set_cfun (NULL);
105*38fd1498Szrj   free (m_path);
106*38fd1498Szrj }
107*38fd1498Szrj 
108*38fd1498Szrj /* Get the insn with the given uid, or NULL if not found.  */
109*38fd1498Szrj 
110*38fd1498Szrj rtx_insn *
get_insn_by_uid(int uid)111*38fd1498Szrj get_insn_by_uid (int uid)
112*38fd1498Szrj {
113*38fd1498Szrj   for (rtx_insn *insn = get_insns (); insn; insn = NEXT_INSN (insn))
114*38fd1498Szrj     if (INSN_UID (insn) == uid)
115*38fd1498Szrj       return insn;
116*38fd1498Szrj 
117*38fd1498Szrj   /* Not found.  */
118*38fd1498Szrj   return NULL;
119*38fd1498Szrj }
120*38fd1498Szrj 
121*38fd1498Szrj } // namespace selftest
122*38fd1498Szrj 
123*38fd1498Szrj #endif /* #if CHECKING_P */
124