]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/rtl-tests.c
Start adding selftests for print_rtx
[thirdparty/gcc.git] / gcc / rtl-tests.c
CommitLineData
99b4f3a2 1/* Unit tests for RTL-handling.
2 Copyright (C) 2015-2016 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along 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 "signop.h"
26#include "hash-set.h"
27#include "fixed-value.h"
28#include "alias.h"
29#include "flags.h"
30#include "symtab.h"
31#include "tree-core.h"
32#include "stor-layout.h"
33#include "tree.h"
34#include "stringpool.h"
35#include "stor-layout.h"
36#include "rtl.h"
37#include "pretty-print.h"
38#include "cfgbuild.h"
39#include "print-rtl.h"
40#include "selftest.h"
41#include "function.h"
ad7b10a2 42#include "memmodel.h"
99b4f3a2 43#include "emit-rtl.h"
44
45#if CHECKING_P
46
47namespace selftest {
48
49/* Verify that PAT is printed as EXPECTED. Helper function for
50 selftests. */
51
52static void
53verify_print_pattern (const char *expected, rtx pat)
54{
55 pretty_printer pp;
56 print_pattern (&pp, pat, 1);
57 ASSERT_STREQ (expected, pp_formatted_text (&pp));
58}
59
90a524f2 60/* Verify that X is dumped as EXPECTED_DUMP, using compact mode.
61 Use LOC as the effective location when reporting errors. */
62
63static void
64assert_rtl_dump_eq (const location &loc, const char *expected_dump, rtx x)
65{
66 named_temp_file tmp_out (".rtl");
67 FILE *outfile = fopen (tmp_out.get_filename (), "w");
68 flag_compact = true;
69 print_rtl (outfile, x);
70 flag_compact = false;
71 fclose (outfile);
72
73 char *dump = read_file (SELFTEST_LOCATION, tmp_out.get_filename ());
74 ASSERT_STREQ_AT (loc, expected_dump, dump);
75 free (dump);
76}
77
78/* Verify that RTX is dumped as EXPECTED_DUMP, using compact mode. */
79
80#define ASSERT_RTL_DUMP_EQ(EXPECTED_DUMP, RTX) \
81 assert_rtl_dump_eq (SELFTEST_LOCATION, (EXPECTED_DUMP), (RTX))
82
83/* Verify that regs are dumped as expected (in compact mode). */
84
85static void
86test_dumping_regs ()
87{
88 /* Dumps of hard regs contain a target-specific name, so we don't test
89 it here. */
90
91 /* Test dumping of virtual regs. The various virtual regs are inited as
92 Pmode, so this is target-specific. The tests below assume DImode, so
93 only run the tests for targets where Pmode is DImode. */
94 if (Pmode == DImode)
95 {
96 ASSERT_RTL_DUMP_EQ ("(reg:DI virtual-incoming-args)",
97 virtual_incoming_args_rtx);
98 ASSERT_RTL_DUMP_EQ ("(reg:DI virtual-stack-vars)",
99 virtual_stack_vars_rtx);
100 ASSERT_RTL_DUMP_EQ ("(reg:DI virtual-stack-dynamic)",
101 virtual_stack_dynamic_rtx);
102 ASSERT_RTL_DUMP_EQ ("(reg:DI virtual-outgoing-args)",
103 virtual_outgoing_args_rtx);
104 ASSERT_RTL_DUMP_EQ ("(reg:DI virtual-cfa)",
105 virtual_cfa_rtx);
106 ASSERT_RTL_DUMP_EQ ("(reg:DI virtual-preferred-stack-boundary)",
107 virtual_preferred_stack_boundary_rtx);
108 }
109
110 /* Test dumping of non-virtual pseudos. */
111 ASSERT_RTL_DUMP_EQ ("(reg:SI %0)",
112 gen_raw_REG (SImode, LAST_VIRTUAL_REGISTER + 1));
113 ASSERT_RTL_DUMP_EQ ("(reg:SI %1)",
114 gen_raw_REG (SImode, LAST_VIRTUAL_REGISTER + 2));
115}
116
117/* Verify that insns are dumped as expected (in compact mode). */
118
119static void
120test_dumping_insns ()
121{
122 /* Barriers. */
123 rtx_barrier *barrier = as_a <rtx_barrier *> (rtx_alloc (BARRIER));
124 SET_NEXT_INSN (barrier) = NULL;
125 ASSERT_RTL_DUMP_EQ ("(cbarrier)\n", barrier);
126
127 /* Labels. */
128 rtx_insn *label = gen_label_rtx ();
129 CODE_LABEL_NUMBER (label) = 42;
130 ASSERT_RTL_DUMP_EQ ("(clabel 0 42 \"\")\n", label);
131
132 LABEL_NAME (label)= "some_label";
133 ASSERT_RTL_DUMP_EQ ("(clabel 0 42 (\"some_label\"))\n", label);
134}
135
99b4f3a2 136/* Unit testing of "single_set". */
137
138static void
139test_single_set ()
140{
141 /* A label is not a SET. */
142 ASSERT_EQ (NULL_RTX, single_set (gen_label_rtx ()));
143
144 /* An unconditional jump insn is a single SET. */
145 rtx set_pc = gen_rtx_SET (pc_rtx,
146 gen_rtx_LABEL_REF (VOIDmode,
147 gen_label_rtx ()));
148 rtx_insn *jump_insn = emit_jump_insn (set_pc);
149 ASSERT_EQ (set_pc, single_set (jump_insn));
150
151 /* etc */
152}
153
154/* Construct an unconditional jump to a label, and verify that
155 various properties of it are sane. */
156
157static void
158test_uncond_jump ()
159{
160 rtx_insn *label = gen_label_rtx ();
161 rtx jump_pat = gen_rtx_SET (pc_rtx,
162 gen_rtx_LABEL_REF (VOIDmode,
163 label));
164 ASSERT_EQ (SET, jump_pat->code);
165 ASSERT_EQ (LABEL_REF, SET_SRC (jump_pat)->code);
c7799456 166 ASSERT_EQ (label, label_ref_label (SET_SRC (jump_pat)));
99b4f3a2 167 ASSERT_EQ (PC, SET_DEST (jump_pat)->code);
168
169 verify_print_pattern ("pc=L0", jump_pat);
170
90a524f2 171 ASSERT_RTL_DUMP_EQ ("(set (pc)\n"
172 " (label_ref 0))",
173 jump_pat);
174
99b4f3a2 175 rtx_insn *jump_insn = emit_jump_insn (jump_pat);
176 ASSERT_FALSE (any_condjump_p (jump_insn));
177 ASSERT_TRUE (any_uncondjump_p (jump_insn));
178 ASSERT_TRUE (pc_set (jump_insn));
179 ASSERT_TRUE (simplejump_p (jump_insn));
180 ASSERT_TRUE (onlyjump_p (jump_insn));
181 ASSERT_TRUE (control_flow_insn_p (jump_insn));
90a524f2 182
183 ASSERT_RTL_DUMP_EQ ("(cjump_insn (set (pc)\n"
184 " (label_ref 0))\n"
185 " (nil))\n",
186 jump_insn);
99b4f3a2 187}
188
189/* Run all of the selftests within this file. */
190
191void
192rtl_tests_c_tests ()
193{
90a524f2 194 test_dumping_regs ();
195 test_dumping_insns ();
99b4f3a2 196 test_single_set ();
197 test_uncond_jump ();
198
199 /* Purge state. */
200 set_first_insn (NULL);
201 set_last_insn (NULL);
202}
203
204} // namespace selftest
205#endif /* #if CHECKING_P */