1 /* Unit tests for RTL-handling.
2 Copyright (C) 2015-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
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
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
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/>. */
22 #include "coretypes.h"
27 #include "fixed-value.h"
31 #include "tree-core.h"
32 #include "stor-layout.h"
34 #include "stringpool.h"
35 #include "stor-layout.h"
37 #include "pretty-print.h"
39 #include "print-rtl.h"
41 #include "selftest-rtl.h"
50 /* Verify that PAT is printed as EXPECTED. Helper function for
54 verify_print_pattern (const char *expected
, rtx pat
)
57 print_pattern (&pp
, pat
, 1);
58 ASSERT_STREQ (expected
, pp_formatted_text (&pp
));
61 /* Verify that X is dumped as EXPECTED_DUMP, using compact mode.
62 Use LOC as the effective location when reporting errors. */
65 assert_rtl_dump_eq (const location
&loc
, const char *expected_dump
, rtx x
)
67 named_temp_file
tmp_out (".rtl");
68 FILE *outfile
= fopen (tmp_out
.get_filename (), "w");
69 rtx_writer
w (outfile
, 0, false, true);
73 char *dump
= read_file (SELFTEST_LOCATION
, tmp_out
.get_filename ());
74 ASSERT_STREQ_AT (loc
, expected_dump
, dump
);
78 /* Verify that regs are dumped as expected (in compact mode). */
83 /* Dumps of hard regs contain a target-specific name, so we don't test
84 it here; this can be tested in target-specific selftests. */
86 /* Test dumping of virtual regs. The various virtual regs are inited as
87 Pmode, so this is target-specific. The tests below assume DImode, so
88 only run the tests for targets where Pmode is DImode. */
91 ASSERT_RTL_DUMP_EQ ("(reg:DI virtual-incoming-args)",
92 virtual_incoming_args_rtx
);
93 ASSERT_RTL_DUMP_EQ ("(reg:DI virtual-stack-vars)",
94 virtual_stack_vars_rtx
);
95 ASSERT_RTL_DUMP_EQ ("(reg:DI virtual-stack-dynamic)",
96 virtual_stack_dynamic_rtx
);
97 ASSERT_RTL_DUMP_EQ ("(reg:DI virtual-outgoing-args)",
98 virtual_outgoing_args_rtx
);
99 ASSERT_RTL_DUMP_EQ ("(reg:DI virtual-cfa)",
101 ASSERT_RTL_DUMP_EQ ("(reg:DI virtual-preferred-stack-boundary)",
102 virtual_preferred_stack_boundary_rtx
);
105 /* Test dumping of non-virtual pseudos. */
106 ASSERT_RTL_DUMP_EQ ("(reg:SI %0)",
107 gen_raw_REG (SImode
, LAST_VIRTUAL_REGISTER
+ 1));
108 ASSERT_RTL_DUMP_EQ ("(reg:SI %1)",
109 gen_raw_REG (SImode
, LAST_VIRTUAL_REGISTER
+ 2));
112 /* Verify that insns are dumped as expected (in compact mode). */
115 test_dumping_insns ()
118 rtx_barrier
*barrier
= as_a
<rtx_barrier
*> (rtx_alloc (BARRIER
));
119 SET_NEXT_INSN (barrier
) = NULL
;
120 ASSERT_RTL_DUMP_EQ ("(cbarrier 0)\n", barrier
);
123 rtx_insn
*label
= gen_label_rtx ();
124 CODE_LABEL_NUMBER (label
) = 42;
125 ASSERT_RTL_DUMP_EQ ("(clabel 0 42)\n", label
);
127 LABEL_NAME (label
)= "some_label";
128 ASSERT_RTL_DUMP_EQ ("(clabel 0 42 (\"some_label\"))\n", label
);
131 /* Unit testing of "single_set". */
136 /* A label is not a SET. */
137 ASSERT_EQ (NULL_RTX
, single_set (gen_label_rtx ()));
139 /* An unconditional jump insn is a single SET. */
140 rtx set_pc
= gen_rtx_SET (pc_rtx
,
141 gen_rtx_LABEL_REF (VOIDmode
,
143 rtx_insn
*jump_insn
= emit_jump_insn (set_pc
);
144 ASSERT_EQ (set_pc
, single_set (jump_insn
));
149 /* Construct an unconditional jump to a label, and verify that
150 various properties of it are sane. */
155 rtx_insn
*label
= gen_label_rtx ();
156 rtx jump_pat
= gen_rtx_SET (pc_rtx
,
157 gen_rtx_LABEL_REF (VOIDmode
,
159 ASSERT_EQ (SET
, jump_pat
->code
);
160 ASSERT_EQ (LABEL_REF
, SET_SRC (jump_pat
)->code
);
161 ASSERT_EQ (label
, label_ref_label (SET_SRC (jump_pat
)));
162 ASSERT_EQ (PC
, SET_DEST (jump_pat
)->code
);
164 verify_print_pattern ("pc=L0", jump_pat
);
166 ASSERT_RTL_DUMP_EQ ("(set (pc)\n"
170 rtx_insn
*jump_insn
= emit_jump_insn (jump_pat
);
171 ASSERT_FALSE (any_condjump_p (jump_insn
));
172 ASSERT_TRUE (any_uncondjump_p (jump_insn
));
173 ASSERT_TRUE (pc_set (jump_insn
));
174 ASSERT_TRUE (simplejump_p (jump_insn
));
175 ASSERT_TRUE (onlyjump_p (jump_insn
));
176 ASSERT_TRUE (control_flow_insn_p (jump_insn
));
178 ASSERT_RTL_DUMP_EQ ("(cjump_insn 1 (set (pc)\n"
179 " (label_ref 0)))\n",
183 /* Run all of the selftests within this file. */
188 test_dumping_regs ();
189 test_dumping_insns ();
194 set_first_insn (NULL
);
195 set_last_insn (NULL
);
198 } // namespace selftest
199 #endif /* #if CHECKING_P */