Branch data Line data Source code
1 : : /* Unit tests for function-handling.
2 : : Copyright (C) 2015-2024 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 "stor-layout.h"
35 : : #include "rtl.h"
36 : : #include "predict.h"
37 : : #include "vec.h"
38 : : #include "hashtab.h"
39 : : #include "hash-set.h"
40 : : #include "hard-reg-set.h"
41 : : #include "input.h"
42 : : #include "function.h"
43 : : #include "dominance.h"
44 : : #include "cfg.h"
45 : : #include "bitmap.h"
46 : : #include "cfganal.h"
47 : : #include "basic-block.h"
48 : : #include "tree-ssa-alias.h"
49 : : #include "internal-fn.h"
50 : : #include "gimple.h"
51 : : #include "gimple-iterator.h"
52 : : #include "gimple-fold.h"
53 : : #include "gimple-expr.h"
54 : : #include "toplev.h"
55 : : #include "print-tree.h"
56 : : #include "tree-iterator.h"
57 : : #include "gimplify.h"
58 : : #include "tree-cfg.h"
59 : : #include "basic-block.h"
60 : : #include "alias.h"
61 : : #include "symtab.h"
62 : : #include "inchash.h"
63 : : #include "tree.h"
64 : : #include "fold-const.h"
65 : : #include "stor-layout.h"
66 : : #include "stmt.h"
67 : : #include "hash-table.h"
68 : : #include "tree-ssa-alias.h"
69 : : #include "internal-fn.h"
70 : : #include "gimple-expr.h"
71 : : #include "is-a.h"
72 : : #include "tree-pass.h"
73 : : #include "context.h"
74 : : #include "hash-map.h"
75 : : #include "plugin-api.h"
76 : : #include "ipa-ref.h"
77 : : #include "cgraph.h"
78 : : #include "selftest.h"
79 : : #include "print-rtl.h"
80 : :
81 : : #if CHECKING_P
82 : :
83 : : namespace selftest {
84 : :
85 : : /* Helper function for selftests of function-creation. */
86 : :
87 : : tree
88 : 48 : make_fndecl (tree return_type,
89 : : const char *name,
90 : : vec <tree> ¶m_types,
91 : : bool is_variadic)
92 : : {
93 : 48 : tree fn_type;
94 : 48 : if (is_variadic)
95 : 0 : fn_type = build_varargs_function_type_array (return_type,
96 : 0 : param_types.length (),
97 : : param_types.address ());
98 : : else
99 : 52 : fn_type = build_function_type_array (return_type,
100 : 48 : param_types.length (),
101 : : param_types.address ());
102 : : /* FIXME: this uses input_location: */
103 : 48 : tree fndecl = build_fn_decl (name, fn_type);
104 : :
105 : 48 : return fndecl;
106 : : }
107 : :
108 : : /* Verify creating a function declaration equivalent to the following
109 : : int test_fndecl_int_void (void);
110 : : C declaration. */
111 : :
112 : : static void
113 : 4 : test_fndecl_int_void ()
114 : : {
115 : 4 : auto_vec <tree> param_types;
116 : 4 : const char *name = "test_fndecl_int_void";
117 : 4 : tree fndecl = make_fndecl (integer_type_node,
118 : : name,
119 : : param_types);
120 : 4 : ASSERT_TRUE (fndecl != NULL);
121 : :
122 : : /* Verify name of decl. */
123 : 4 : tree declname = DECL_NAME (fndecl);
124 : 4 : ASSERT_TRUE (declname != NULL);
125 : 4 : ASSERT_EQ (IDENTIFIER_NODE, TREE_CODE (declname));
126 : : /* We expect it to use a *copy* of the string we passed in. */
127 : 4 : const char *identifier_ptr = IDENTIFIER_POINTER (declname);
128 : 4 : ASSERT_NE (name, identifier_ptr);
129 : 4 : ASSERT_EQ (0, strcmp ("test_fndecl_int_void", identifier_ptr));
130 : :
131 : : /* Verify type of fndecl. */
132 : 4 : ASSERT_EQ (FUNCTION_DECL, TREE_CODE (fndecl));
133 : 4 : tree fntype = TREE_TYPE (fndecl);
134 : 4 : ASSERT_EQ (FUNCTION_TYPE, TREE_CODE (fntype));
135 : :
136 : : /* Verify return type. */
137 : 4 : ASSERT_EQ (integer_type_node, TREE_TYPE (fntype));
138 : :
139 : : /* Verify "void" args. */
140 : 4 : tree argtypes = TYPE_ARG_TYPES (fntype);
141 : 4 : ASSERT_EQ (TREE_LIST, TREE_CODE (argtypes));
142 : 4 : ASSERT_EQ (void_type_node, TREE_VALUE (argtypes));
143 : 4 : ASSERT_EQ (NULL, TREE_CHAIN (argtypes));
144 : 4 : }
145 : :
146 : : /* Verify creating a function declaration equivalent to the following
147 : : float test_fndecl_float_intchar (int, char);
148 : : C declaration. */
149 : :
150 : : static void
151 : 4 : test_fndecl_float_intchar ()
152 : : {
153 : 4 : auto_vec <tree> param_types;
154 : 4 : param_types.safe_push (integer_type_node);
155 : 4 : param_types.safe_push (char_type_node);
156 : 4 : const char *name = "test_fndecl_float_intchar";
157 : 4 : tree fndecl = make_fndecl (float_type_node,
158 : : name,
159 : : param_types);
160 : 4 : ASSERT_TRUE (fndecl != NULL);
161 : :
162 : : /* Verify name of decl. */
163 : 4 : tree declname = DECL_NAME (fndecl);
164 : 4 : ASSERT_TRUE (declname != NULL);
165 : 4 : ASSERT_EQ (IDENTIFIER_NODE, TREE_CODE (declname));
166 : : /* We expect it to use a *copy* of the string we passed in. */
167 : 4 : const char *identifier_ptr = IDENTIFIER_POINTER (declname);
168 : 4 : ASSERT_NE (name, identifier_ptr);
169 : 4 : ASSERT_EQ (0, strcmp (name, identifier_ptr));
170 : :
171 : : /* Verify type of fndecl. */
172 : 4 : ASSERT_EQ (FUNCTION_DECL, TREE_CODE (fndecl));
173 : 4 : tree fntype = TREE_TYPE (fndecl);
174 : 4 : ASSERT_EQ (FUNCTION_TYPE, TREE_CODE (fntype));
175 : :
176 : : /* Verify return type. */
177 : 4 : ASSERT_EQ (float_type_node, TREE_TYPE (fntype));
178 : :
179 : : /* Verify "(int, char)" args. */
180 : 4 : tree arg0 = TYPE_ARG_TYPES (fntype);
181 : 4 : ASSERT_EQ (TREE_LIST, TREE_CODE (arg0));
182 : 4 : ASSERT_EQ (integer_type_node, TREE_VALUE (arg0));
183 : 4 : tree arg1 = TREE_CHAIN (arg0);
184 : 4 : ASSERT_TRUE (arg1 != NULL);
185 : 4 : ASSERT_EQ (TREE_LIST, TREE_CODE (arg1));
186 : 4 : ASSERT_EQ (char_type_node, TREE_VALUE (arg1));
187 : 4 : tree argterm = TREE_CHAIN (arg1);
188 : 4 : ASSERT_TRUE (argterm != NULL);
189 : 4 : ASSERT_EQ (TREE_LIST, TREE_CODE (argterm));
190 : 4 : ASSERT_EQ (void_type_node, TREE_VALUE (argterm));
191 : 4 : ASSERT_EQ (NULL, TREE_CHAIN (argterm));
192 : 4 : }
193 : :
194 : : /* The test cases using these helper functions take a trivial function:
195 : :
196 : : int test_fn (void) { return 42; }
197 : :
198 : : and test various conversions done to it:
199 : :
200 : : - gimplification
201 : : - construction of the CFG
202 : : - conversion to SSA form
203 : : - expansion to RTL form
204 : :
205 : : In avoid having one overlong test case, this is broken
206 : : up into separate test cases for each stage, with helper functions
207 : : to minimize code duplication.
208 : :
209 : : Another approach would be to attempt to directly construct a function
210 : : in the appropriate representation at each stage, though presumably
211 : : that would exhibit different kinds of failure compared to this
212 : : approach. */
213 : :
214 : : /* Construct this function:
215 : : int test_fn (void) { return 42; }
216 : : in generic tree form. Return the fndecl. */
217 : :
218 : : static tree
219 : 20 : build_trivial_generic_function ()
220 : : {
221 : 20 : auto_vec <tree> param_types;
222 : 20 : tree fndecl = make_fndecl (integer_type_node,
223 : : "test_fn",
224 : : param_types);
225 : 20 : ASSERT_TRUE (fndecl != NULL);
226 : :
227 : : /* Populate the function. */
228 : 20 : tree retval = build_decl (UNKNOWN_LOCATION, RESULT_DECL,
229 : : NULL_TREE, integer_type_node);
230 : 20 : DECL_ARTIFICIAL (retval) = 1;
231 : 20 : DECL_IGNORED_P (retval) = 1;
232 : 20 : DECL_RESULT (fndecl) = retval;
233 : :
234 : : /* Create a BIND_EXPR, and within it, a statement list. */
235 : 20 : tree stmt_list = alloc_stmt_list ();
236 : 20 : tree_stmt_iterator stmt_iter = tsi_start (stmt_list);
237 : 20 : tree block = make_node (BLOCK);
238 : 20 : tree bind_expr
239 : 20 : = build3 (BIND_EXPR, void_type_node, NULL, stmt_list, block);
240 : :
241 : 20 : tree modify_retval = build2 (MODIFY_EXPR,
242 : : integer_type_node,
243 : : retval,
244 : 20 : build_int_cst (integer_type_node, 42));
245 : 20 : tree return_stmt = build1 (RETURN_EXPR,
246 : : integer_type_node,
247 : : modify_retval);
248 : 20 : tsi_link_after (&stmt_iter, return_stmt, TSI_CONTINUE_LINKING);
249 : :
250 : 20 : DECL_INITIAL (fndecl) = block;
251 : 20 : BLOCK_SUPERCONTEXT (block) = fndecl;
252 : :
253 : : /* how to add to function? the following appears to be how to
254 : : set the body of a fndecl: */
255 : 20 : DECL_SAVED_TREE(fndecl) = bind_expr;
256 : :
257 : : /* Ensure that locals appear in the debuginfo. */
258 : 20 : BLOCK_VARS (block) = BIND_EXPR_VARS (bind_expr);
259 : :
260 : 20 : return fndecl;
261 : 20 : }
262 : :
263 : : /* Construct this function:
264 : : int test_fn (void) { return 42; }
265 : : in "high gimple" form. Return the fndecl. */
266 : :
267 : : static tree
268 : 16 : build_trivial_high_gimple_function ()
269 : : {
270 : : /* Construct a trivial function, and gimplify it: */
271 : 0 : tree fndecl = build_trivial_generic_function ();
272 : 16 : gimplify_function_tree (fndecl);
273 : 16 : return fndecl;
274 : : }
275 : :
276 : : /* Build a CFG for a function in gimple form. */
277 : :
278 : : static void
279 : 16 : build_cfg (tree fndecl)
280 : : {
281 : 16 : function *fun = DECL_STRUCT_FUNCTION (fndecl);
282 : 16 : ASSERT_TRUE (fun != NULL);
283 : 16 : ASSERT_EQ (fndecl, fun->decl);
284 : :
285 : : /* We first have to lower control flow; for our trivial test function
286 : : this gives us:
287 : : test_fn ()
288 : : {
289 : : D.56 = 42;
290 : : goto <D.57>;
291 : : <D.57>:
292 : : return D.56;
293 : : }
294 : : */
295 : 16 : gimple_opt_pass *lower_cf_pass = make_pass_lower_cf (g);
296 : 16 : push_cfun (fun);
297 : 16 : lower_cf_pass->execute (fun);
298 : 16 : pop_cfun ();
299 : 16 : delete lower_cf_pass;
300 : :
301 : : /* We can now convert to CFG form; for our trivial test function this
302 : : gives us:
303 : : test_fn ()
304 : : {
305 : : <bb 2>:
306 : : D.56 = 42;
307 : : return D.56;
308 : : }
309 : : */
310 : 16 : gimple_opt_pass *build_cfg_pass = make_pass_build_cfg (g);
311 : 16 : push_cfun (fun);
312 : 16 : build_cfg_pass->execute (fun);
313 : 16 : pop_cfun ();
314 : 16 : delete build_cfg_pass;
315 : 16 : }
316 : :
317 : : /* Convert a gimple+CFG function to SSA form. */
318 : :
319 : : static void
320 : 12 : convert_to_ssa (tree fndecl)
321 : : {
322 : 12 : function *fun = DECL_STRUCT_FUNCTION (fndecl);
323 : 12 : ASSERT_TRUE (fun != NULL);
324 : 12 : ASSERT_EQ (fndecl, fun->decl);
325 : :
326 : 12 : gimple_opt_pass *build_ssa_pass = make_pass_build_ssa (g);
327 : 12 : push_cfun (fun);
328 : 12 : build_ssa_pass->execute (fun);
329 : 12 : pop_cfun ();
330 : 12 : delete build_ssa_pass;
331 : 12 : }
332 : :
333 : : /* Assuming we have a simple 3-block CFG like this:
334 : : [ENTRY] -> [block2] -> [EXIT]
335 : : get the "real" basic block (block 2). */
336 : :
337 : : static basic_block
338 : 52 : get_real_block (function *fun)
339 : : {
340 : 52 : ASSERT_TRUE (fun->cfg != NULL);
341 : 52 : ASSERT_EQ (3, n_basic_blocks_for_fn (fun));
342 : 52 : basic_block bb2 = (*fun->cfg->x_basic_block_info)[2];
343 : 52 : ASSERT_TRUE (bb2 != NULL);
344 : 52 : return bb2;
345 : : }
346 : :
347 : : /* Verify that we have a simple 3-block CFG: the two "fake" ones, and
348 : : a "real" one:
349 : : [ENTRY] -> [block2] -> [EXIT]. */
350 : :
351 : : static void
352 : 20 : verify_three_block_cfg (function *fun)
353 : : {
354 : 20 : ASSERT_TRUE (fun->cfg != NULL);
355 : 20 : ASSERT_EQ (3, n_basic_blocks_for_fn (fun));
356 : 20 : ASSERT_EQ (2, n_edges_for_fn (fun));
357 : :
358 : : /* The "fake" basic blocks. */
359 : 20 : basic_block entry = ENTRY_BLOCK_PTR_FOR_FN (fun);
360 : 20 : ASSERT_TRUE (entry != NULL);
361 : 20 : ASSERT_EQ (ENTRY_BLOCK, entry->index);
362 : :
363 : 20 : basic_block exit = EXIT_BLOCK_PTR_FOR_FN (fun);
364 : 20 : ASSERT_TRUE (exit != NULL);
365 : 20 : ASSERT_EQ (EXIT_BLOCK, exit->index);
366 : :
367 : : /* The "real" basic block. */
368 : 20 : basic_block bb2 = get_real_block (fun);
369 : 20 : ASSERT_TRUE (bb2 != NULL);
370 : 20 : ASSERT_EQ (2, bb2->index);
371 : :
372 : : /* Verify connectivity. */
373 : 20 : ASSERT_EQ (NULL, entry->preds);
374 : 20 : ASSERT_EQ (1, entry->succs->length ());
375 : :
376 : 20 : edge from_entry_to_bb2 = (*entry->succs)[0];
377 : 20 : ASSERT_EQ (entry, from_entry_to_bb2->src);
378 : 20 : ASSERT_EQ (bb2, from_entry_to_bb2->dest);
379 : :
380 : 20 : ASSERT_EQ (1, bb2->preds->length ());
381 : 20 : ASSERT_EQ (from_entry_to_bb2, (*bb2->preds)[0]);
382 : 20 : ASSERT_EQ (1, bb2->succs->length ());
383 : :
384 : 20 : edge from_bb2_to_exit = (*bb2->succs)[0];
385 : 20 : ASSERT_EQ (bb2, from_bb2_to_exit->src);
386 : 20 : ASSERT_EQ (exit, from_bb2_to_exit->dest);
387 : :
388 : 20 : ASSERT_EQ (1, exit->preds->length ());
389 : 20 : ASSERT_EQ (from_bb2_to_exit, (*exit->preds)[0]);
390 : 20 : ASSERT_EQ (NULL, exit->succs);
391 : 20 : }
392 : :
393 : : /* As above, but additionally verify the gimple statements are sane. */
394 : :
395 : : static void
396 : 8 : verify_three_block_gimple_cfg (function *fun)
397 : : {
398 : 8 : verify_three_block_cfg (fun);
399 : :
400 : : /* The "fake" basic blocks should be flagged as gimple, but with have no
401 : : statements. */
402 : 8 : basic_block entry = ENTRY_BLOCK_PTR_FOR_FN (fun);
403 : 8 : ASSERT_TRUE (entry != NULL);
404 : 8 : ASSERT_EQ (0, entry->flags & BB_RTL);
405 : 8 : ASSERT_EQ (NULL, bb_seq (entry));
406 : :
407 : 8 : basic_block exit = EXIT_BLOCK_PTR_FOR_FN (fun);
408 : 8 : ASSERT_TRUE (exit != NULL);
409 : 8 : ASSERT_EQ (0, entry->flags & BB_RTL);
410 : 8 : ASSERT_EQ (NULL, bb_seq (exit));
411 : :
412 : : /* The "real" basic block should be flagged as gimple, and have one
413 : : or more statements. */
414 : 8 : basic_block bb2 = get_real_block (fun);
415 : 8 : ASSERT_TRUE (bb2 != NULL);
416 : 8 : ASSERT_EQ (0, entry->flags & BB_RTL);
417 : 8 : ASSERT_TRUE (bb_seq (bb2) != NULL);
418 : 8 : }
419 : :
420 : : /* As above, but additionally verify the RTL insns are sane. */
421 : :
422 : : void
423 : 12 : verify_three_block_rtl_cfg (function *fun)
424 : : {
425 : 12 : verify_three_block_cfg (fun);
426 : :
427 : : /* The "fake" basic blocks should be flagged as RTL, but with no
428 : : insns. */
429 : 12 : basic_block entry = ENTRY_BLOCK_PTR_FOR_FN (fun);
430 : 12 : ASSERT_TRUE (entry != NULL);
431 : 12 : ASSERT_EQ (BB_RTL, entry->flags & BB_RTL);
432 : 12 : ASSERT_EQ (NULL, BB_HEAD (entry));
433 : :
434 : 12 : basic_block exit = EXIT_BLOCK_PTR_FOR_FN (fun);
435 : 12 : ASSERT_TRUE (exit != NULL);
436 : 12 : ASSERT_EQ (BB_RTL, exit->flags & BB_RTL);
437 : 12 : ASSERT_EQ (NULL, BB_HEAD (exit));
438 : :
439 : : /* The "real" basic block should be flagged as RTL, and have one
440 : : or more insns. */
441 : 12 : basic_block bb2 = get_real_block (fun);
442 : 12 : ASSERT_TRUE (bb2 != NULL);
443 : 12 : ASSERT_EQ (BB_RTL, bb2->flags & BB_RTL);
444 : 12 : ASSERT_TRUE (BB_HEAD (bb2) != NULL);
445 : 12 : }
446 : :
447 : : /* Test converting our trivial function:
448 : : int test_fn (void) { return 42; }
449 : : to gimple form. */
450 : :
451 : : static void
452 : 4 : test_gimplification ()
453 : : {
454 : 4 : tree fndecl = build_trivial_generic_function ();
455 : :
456 : : /* Convert to gimple: */
457 : 4 : gimplify_function_tree (fndecl);
458 : :
459 : : /* Verify that we got gimple out of it. */
460 : :
461 : : /* The function is now in GIMPLE form but the CFG has not been
462 : : built yet. */
463 : :
464 : : /* We should have a struct function for the decl. */
465 : 4 : function *fun = DECL_STRUCT_FUNCTION (fndecl);
466 : 4 : ASSERT_TRUE (fun != NULL);
467 : 4 : ASSERT_EQ (fndecl, fun->decl);
468 : :
469 : : /* We expect a GIMPLE_BIND, with two gimple statements within it:
470 : : tmp = 42;
471 : : return tmp; */
472 : :
473 : 4 : gimple_seq seq_fn_body = gimple_body (fndecl);
474 : 4 : ASSERT_TRUE (seq_fn_body != NULL);
475 : 4 : gimple *bind_stmt = gimple_seq_first_stmt (seq_fn_body);
476 : 4 : ASSERT_EQ (GIMPLE_BIND, gimple_code (bind_stmt));
477 : 4 : ASSERT_EQ (NULL, bind_stmt->next);
478 : :
479 : 4 : gimple_seq seq_bind_body = gimple_bind_body (as_a <gbind *> (bind_stmt));
480 : :
481 : : /* Verify that we have the 2 statements we expect. */
482 : 4 : ASSERT_TRUE (seq_bind_body != NULL);
483 : 4 : gimple *stmt1 = gimple_seq_first_stmt (seq_bind_body);
484 : 4 : ASSERT_TRUE (stmt1 != NULL);
485 : 4 : ASSERT_EQ (GIMPLE_ASSIGN, gimple_code (stmt1));
486 : 4 : gimple *stmt2 = stmt1->next;
487 : 4 : ASSERT_TRUE (stmt2 != NULL);
488 : 4 : ASSERT_EQ (stmt1, stmt2->prev);
489 : 4 : ASSERT_EQ (GIMPLE_RETURN, gimple_code (stmt2));
490 : 4 : }
491 : :
492 : : /* Test of building a CFG for a function in high gimple form. */
493 : :
494 : : static void
495 : 4 : test_building_cfg ()
496 : : {
497 : : /* Construct a trivial function, and gimplify it: */
498 : 4 : tree fndecl = build_trivial_high_gimple_function ();
499 : 4 : function *fun = DECL_STRUCT_FUNCTION (fndecl);
500 : 4 : ASSERT_TRUE (fun != NULL);
501 : :
502 : : /* Build a CFG. */
503 : 4 : build_cfg (fndecl);
504 : :
505 : : /* The CFG-building code constructs a 4-block cfg (with
506 : : ENTRY and EXIT):
507 : : test_fn ()
508 : : {
509 : : <bb 2>:
510 : : D.65 = 42;
511 : :
512 : : <bb 3>:
513 : : return D.65;
514 : : }
515 : : and then ought to merge blocks 2 and 3 in cleanup_tree_cfg.
516 : :
517 : : Hence we should end up with a simple 3-block cfg, the two "fake" ones,
518 : : and a "real" one:
519 : : [ENTRY] -> [block2] -> [EXIT]
520 : : with code like this:
521 : : test_fn ()
522 : : {
523 : : <bb 2>:
524 : : D.56 = 42;
525 : : return D.56;
526 : : }
527 : : */
528 : 4 : verify_three_block_gimple_cfg (fun);
529 : :
530 : : /* Verify the statements within the "real" block. */
531 : 4 : basic_block bb2 = get_real_block (fun);
532 : 8 : gimple *stmt_a = gimple_seq_first_stmt (bb_seq (bb2));
533 : 4 : ASSERT_EQ (GIMPLE_ASSIGN, gimple_code (stmt_a));
534 : 4 : gimple *stmt_b = stmt_a->next;
535 : 4 : ASSERT_EQ (GIMPLE_RETURN, gimple_code (stmt_b));
536 : 4 : ASSERT_EQ (NULL, stmt_b->next);
537 : 4 : }
538 : :
539 : : /* Test of conversion of gimple to SSA form. */
540 : :
541 : : static void
542 : 4 : test_conversion_to_ssa ()
543 : : {
544 : : /* As above, construct a trivial function, gimplify it, and build a CFG: */
545 : 4 : tree fndecl = build_trivial_high_gimple_function ();
546 : 4 : function *fun = DECL_STRUCT_FUNCTION (fndecl);
547 : 4 : ASSERT_TRUE (fun != NULL);
548 : 4 : build_cfg (fndecl);
549 : :
550 : 4 : convert_to_ssa (fndecl);
551 : :
552 : 4 : verify_three_block_gimple_cfg (fun);
553 : :
554 : : /* For out trivial test function we should now have something like
555 : : this:
556 : : test_fn ()
557 : : {
558 : : <bb 2>:
559 : : _1 = 42;
560 : : return _1;
561 : : }
562 : : */
563 : 4 : basic_block bb2 = get_real_block (fun);
564 : 8 : gimple *stmt_a = gimple_seq_first_stmt (bb_seq (bb2));
565 : 4 : ASSERT_EQ (GIMPLE_ASSIGN, gimple_code (stmt_a));
566 : :
567 : 4 : gimple *stmt_b = stmt_a->next;
568 : 4 : ASSERT_EQ (GIMPLE_RETURN, gimple_code (stmt_b));
569 : 4 : ASSERT_EQ (NULL, stmt_b->next);
570 : :
571 : 4 : greturn *return_stmt = as_a <greturn *> (stmt_b);
572 : 4 : ASSERT_EQ (SSA_NAME, TREE_CODE (gimple_return_retval (return_stmt)));
573 : 4 : }
574 : :
575 : : /* Test range folding. We must start this here because we need cfun
576 : : set. */
577 : :
578 : : static void
579 : 4 : test_ranges ()
580 : : {
581 : 4 : tree fndecl = build_trivial_high_gimple_function ();
582 : 4 : function *fun = DECL_STRUCT_FUNCTION (fndecl);
583 : 4 : push_cfun (fun);
584 : 4 : range_tests ();
585 : 4 : range_op_tests ();
586 : 4 : relation_tests ();
587 : :
588 : 4 : build_cfg (fndecl);
589 : 4 : convert_to_ssa (fndecl);
590 : 4 : gimple_range_tests ();
591 : :
592 : 4 : pop_cfun ();
593 : 4 : }
594 : :
595 : : /* Test of expansion from gimple-ssa to RTL. */
596 : :
597 : : static void
598 : 4 : test_expansion_to_rtl ()
599 : : {
600 : : /* As above, construct a trivial function, gimplify it, build a CFG,
601 : : and convert to SSA: */
602 : 4 : tree fndecl = build_trivial_high_gimple_function ();
603 : 4 : function *fun = DECL_STRUCT_FUNCTION (fndecl);
604 : 4 : ASSERT_TRUE (fun != NULL);
605 : 4 : build_cfg (fndecl);
606 : 4 : convert_to_ssa (fndecl);
607 : :
608 : : /* We need a cgraph_node for it. */
609 : 4 : cgraph_node::get_create (fndecl);
610 : : /* Normally, cgraph_node::expand () would call
611 : : init_function_start (and a bunch of other stuff),
612 : : and invoke the expand pass, but it also runs
613 : : all of the other passes. So just do the minimum
614 : : needed to get from gimple-SSA to RTL. */
615 : 4 : rtl_opt_pass *expand_pass = make_pass_expand (g);
616 : 4 : push_cfun (fun);
617 : 4 : init_function_start (fndecl);
618 : 4 : expand_pass->execute (fun);
619 : 4 : pop_cfun ();
620 : 4 : delete expand_pass;
621 : :
622 : : /* On x86_64, I get this:
623 : : (note 3 1 2 2 [bb 2] NOTE_INSN_BASIC_BLOCK)
624 : : (note 2 3 5 2 NOTE_INSN_FUNCTION_BEG)
625 : : (insn 5 2 6 2 (set (reg:SI 87 [ D.59 ])
626 : : (const_int 42 [0x2a])) -1 (nil))
627 : : (insn 6 5 10 2 (set (reg:SI 88 [ <retval> ])
628 : : (reg:SI 87 [ D.59 ])) -1 (nil))
629 : : (insn 10 6 11 2 (set (reg/i:SI 0 ax)
630 : : (reg:SI 88 [ <retval> ])) -1 (nil))
631 : : (insn 11 10 0 2 (use (reg/i:SI 0 ax)) -1 (nil)). */
632 : 4 : verify_three_block_rtl_cfg (fun);
633 : :
634 : : /* Verify as much of the RTL as we can whilst avoiding
635 : : target-specific behavior. */
636 : 4 : basic_block bb2 = get_real_block (fun);
637 : :
638 : : /* Expect a NOTE_INSN_BASIC_BLOCK... */
639 : 4 : rtx_insn *insn = BB_HEAD (bb2);
640 : 4 : ASSERT_TRUE (insn != NULL);
641 : 4 : ASSERT_EQ (NOTE, insn->code);
642 : 4 : ASSERT_EQ (NOTE_INSN_BASIC_BLOCK, NOTE_KIND (insn));
643 : 4 : ASSERT_EQ (bb2, NOTE_BASIC_BLOCK (insn));
644 : :
645 : : /* ...etc; any further checks are likely to over-specify things
646 : : and run us into target dependencies. */
647 : :
648 : : /* Verify that print_rtl_function is sane. */
649 : 4 : named_temp_file tmp_out (".rtl");
650 : 4 : FILE *outfile = fopen (tmp_out.get_filename (), "w");
651 : 4 : print_rtx_function (outfile, fun, true);
652 : 4 : fclose (outfile);
653 : :
654 : 4 : char *dump = read_file (SELFTEST_LOCATION, tmp_out.get_filename ());
655 : 4 : ASSERT_STR_CONTAINS (dump, "(function \"test_fn\"\n");
656 : 4 : ASSERT_STR_CONTAINS (dump, " (insn-chain\n");
657 : 4 : ASSERT_STR_CONTAINS (dump, " (block 2\n");
658 : 4 : ASSERT_STR_CONTAINS (dump, " (edge-from entry (flags \"FALLTHRU\"))\n");
659 : 4 : ASSERT_STR_CONTAINS (dump, " (cinsn "); /* ...etc. */
660 : 4 : ASSERT_STR_CONTAINS (dump, " (edge-to exit (flags \"FALLTHRU\"))\n");
661 : 4 : ASSERT_STR_CONTAINS (dump, " ) ;; block 2\n");
662 : 4 : ASSERT_STR_CONTAINS (dump, " ) ;; insn-chain\n");
663 : 4 : ASSERT_STR_CONTAINS (dump, " (crtl\n");
664 : 4 : ASSERT_STR_CONTAINS (dump, " ) ;; crtl\n");
665 : 4 : ASSERT_STR_CONTAINS (dump, ") ;; function \"test_fn\"\n");
666 : :
667 : 4 : free (dump);
668 : 4 : free_after_compilation (fun);
669 : 4 : }
670 : :
671 : : /* Run all of the selftests within this file. */
672 : :
673 : : void
674 : 4 : function_tests_cc_tests ()
675 : : {
676 : 4 : test_fndecl_int_void ();
677 : 4 : test_fndecl_float_intchar ();
678 : 4 : test_gimplification ();
679 : 4 : test_building_cfg ();
680 : 4 : test_conversion_to_ssa ();
681 : 4 : test_ranges ();
682 : 4 : test_expansion_to_rtl ();
683 : 4 : }
684 : :
685 : : } // namespace selftest
686 : :
687 : : #endif /* #if CHECKING_P */
|