13ad841b2Smrg /* Selftest support for RTL.
2*4c3eb207Smrg Copyright (C) 2016-2020 Free Software Foundation, Inc.
33ad841b2Smrg
43ad841b2Smrg This file is part of GCC.
53ad841b2Smrg
63ad841b2Smrg GCC is free software; you can redistribute it and/or modify it under
73ad841b2Smrg the terms of the GNU General Public License as published by the Free
83ad841b2Smrg Software Foundation; either version 3, or (at your option) any later
93ad841b2Smrg version.
103ad841b2Smrg
113ad841b2Smrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
123ad841b2Smrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
133ad841b2Smrg FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
143ad841b2Smrg for more details.
153ad841b2Smrg
163ad841b2Smrg You should have received a copy of the GNU General Public License
173ad841b2Smrg along with GCC; see the file COPYING3. If not see
183ad841b2Smrg <http://www.gnu.org/licenses/>. */
193ad841b2Smrg
203ad841b2Smrg #include "config.h"
213ad841b2Smrg #include "system.h"
223ad841b2Smrg #include "coretypes.h"
233ad841b2Smrg #include "selftest.h"
243ad841b2Smrg #include "backend.h"
253ad841b2Smrg #include "target.h"
263ad841b2Smrg #include "rtl.h"
273ad841b2Smrg #include "read-rtl-function.h"
283ad841b2Smrg #include "read-md.h"
293ad841b2Smrg #include "tree-core.h"
303ad841b2Smrg #include "memmodel.h"
313ad841b2Smrg #include "emit-rtl.h"
323ad841b2Smrg #include "selftest-rtl.h"
333ad841b2Smrg
343ad841b2Smrg #if CHECKING_P
353ad841b2Smrg
363ad841b2Smrg namespace selftest {
373ad841b2Smrg
38cef8759bSmrg /* Compare rtx EXPECTED and ACTUAL using rtx_equal_p, calling
39cef8759bSmrg ::selftest::pass if they are equal, aborting if they are non-equal.
40cef8759bSmrg LOC is the effective location of the assertion, MSG describes it. */
41cef8759bSmrg
42cef8759bSmrg void
assert_rtx_eq_at(const location & loc,const char * msg,rtx expected,rtx actual)43cef8759bSmrg assert_rtx_eq_at (const location &loc, const char *msg,
44cef8759bSmrg rtx expected, rtx actual)
45cef8759bSmrg {
46cef8759bSmrg if (rtx_equal_p (expected, actual))
47cef8759bSmrg ::selftest::pass (loc, msg);
48cef8759bSmrg else
49cef8759bSmrg {
50cef8759bSmrg fprintf (stderr, "%s:%i: %s: FAIL: %s\n", loc.m_file, loc.m_line,
51cef8759bSmrg loc.m_function, msg);
52cef8759bSmrg fprintf (stderr, " expected: ");
53cef8759bSmrg print_rtl (stderr, expected);
54cef8759bSmrg fprintf (stderr, "\n actual: ");
55cef8759bSmrg print_rtl (stderr, actual);
56cef8759bSmrg fprintf (stderr, "\n");
57cef8759bSmrg abort ();
58cef8759bSmrg }
59cef8759bSmrg }
60cef8759bSmrg
613ad841b2Smrg /* Compare rtx EXPECTED and ACTUAL by pointer equality, calling
623ad841b2Smrg ::selftest::pass if they are equal, aborting if they are non-equal.
633ad841b2Smrg LOC is the effective location of the assertion, MSG describes it. */
643ad841b2Smrg
653ad841b2Smrg void
assert_rtx_ptr_eq_at(const location & loc,const char * msg,rtx expected,rtx actual)663ad841b2Smrg assert_rtx_ptr_eq_at (const location &loc, const char *msg,
673ad841b2Smrg rtx expected, rtx actual)
683ad841b2Smrg {
693ad841b2Smrg if (expected == actual)
703ad841b2Smrg ::selftest::pass (loc, msg);
713ad841b2Smrg else
723ad841b2Smrg {
733ad841b2Smrg fprintf (stderr, "%s:%i: %s: FAIL: %s\n", loc.m_file, loc.m_line,
743ad841b2Smrg loc.m_function, msg);
753ad841b2Smrg fprintf (stderr, " expected (at %p): ", (void *)expected);
763ad841b2Smrg print_rtl (stderr, expected);
773ad841b2Smrg fprintf (stderr, "\n actual (at %p): ", (void *)actual);
783ad841b2Smrg print_rtl (stderr, actual);
793ad841b2Smrg fprintf (stderr, "\n");
803ad841b2Smrg abort ();
813ad841b2Smrg }
823ad841b2Smrg }
833ad841b2Smrg
843ad841b2Smrg /* Constructor for selftest::rtl_dump_test.
853ad841b2Smrg Read a dumped RTL function from PATH.
863ad841b2Smrg Takes ownership of PATH, freeing in dtor.
873ad841b2Smrg Use LOC as the effective location when reporting failures. */
883ad841b2Smrg
rtl_dump_test(const location & loc,char * path)893ad841b2Smrg rtl_dump_test::rtl_dump_test (const location &loc, char *path)
903ad841b2Smrg : m_path (path)
913ad841b2Smrg {
923ad841b2Smrg bool read_ok = read_rtl_function_body (path);
933ad841b2Smrg ASSERT_TRUE_AT (loc, read_ok);
943ad841b2Smrg }
953ad841b2Smrg
963ad841b2Smrg /* Destructor for selftest::rtl_dump_test.
973ad841b2Smrg Cleanup global state relating to the function, and free the path. */
983ad841b2Smrg
~rtl_dump_test()993ad841b2Smrg selftest::rtl_dump_test::~rtl_dump_test ()
1003ad841b2Smrg {
1013ad841b2Smrg /* Cleanups. */
1023ad841b2Smrg current_function_decl = NULL;
1033ad841b2Smrg free_after_compilation (cfun);
1043ad841b2Smrg set_cfun (NULL);
1053ad841b2Smrg free (m_path);
1063ad841b2Smrg }
1073ad841b2Smrg
1083ad841b2Smrg /* Get the insn with the given uid, or NULL if not found. */
1093ad841b2Smrg
1103ad841b2Smrg rtx_insn *
get_insn_by_uid(int uid)1113ad841b2Smrg get_insn_by_uid (int uid)
1123ad841b2Smrg {
1133ad841b2Smrg for (rtx_insn *insn = get_insns (); insn; insn = NEXT_INSN (insn))
1143ad841b2Smrg if (INSN_UID (insn) == uid)
1153ad841b2Smrg return insn;
1163ad841b2Smrg
1173ad841b2Smrg /* Not found. */
1183ad841b2Smrg return NULL;
1193ad841b2Smrg }
1203ad841b2Smrg
1213ad841b2Smrg } // namespace selftest
1223ad841b2Smrg
1233ad841b2Smrg #endif /* #if CHECKING_P */
124