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