1 /* read-rtl-function.cc - Reader for RTL function dumps
2 Copyright (C) 2016-2025 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"
25 #include "diagnostic.h"
29 #include "stringpool.h"
33 #include "basic-block.h"
38 #include "tree-pass.h"
41 #include "read-rtl-function.h"
43 #include "selftest-rtl.h"
45 #include "function-abi.h"
48 class function_reader
;
51 /* Edges are recorded when parsing the "insn-chain" directive,
52 and created at the end when all the blocks ought to exist.
53 This struct records an "edge-from" or "edge-to" directive seen
54 at LOC, which will be turned into an actual CFG edge once
55 the "insn-chain" is fully parsed. */
60 deferred_edge (file_location loc
, int src_bb_idx
, int dest_bb_idx
, int flags
)
61 : m_loc (loc
), m_src_bb_idx (src_bb_idx
), m_dest_bb_idx (dest_bb_idx
),
71 /* Subclass of rtx_reader for reading function dumps. */
73 class function_reader
: public rtx_reader
79 /* Overridden vfuncs of class md_reader. */
80 void handle_unknown_directive (file_location
, const char *) final override
;
82 /* Overridden vfuncs of class rtx_reader. */
83 rtx
read_rtx_operand (rtx x
, int idx
) final override
;
84 void handle_any_trailing_information (rtx x
) final override
;
85 rtx
postprocess (rtx
) final override
;
86 const char *finalize_string (char *stringbuf
) final override
;
88 rtx_insn
**get_insn_by_uid (int uid
);
89 tree
parse_mem_expr (const char *desc
);
92 void parse_function ();
93 void create_function ();
95 void parse_insn_chain ();
98 void parse_edge (basic_block block
, bool from
);
99 rtx_insn
*parse_insn (file_location loc
, const char *name
);
100 void parse_cfg (file_location loc
);
101 void parse_crtl (file_location loc
);
102 void create_edges ();
104 int parse_enum_value (int num_values
, const char *const *strings
);
106 void read_rtx_operand_u (rtx x
, int idx
);
107 void read_rtx_operand_inL (rtx x
, int idx
, char format_char
);
108 rtx
read_rtx_operand_r (rtx x
);
109 rtx
extra_parsing_for_operand_code_0 (rtx x
, int idx
);
111 void add_fixup_insn_uid (file_location loc
, rtx insn
, int operand_idx
,
114 void add_fixup_note_insn_basic_block (file_location loc
, rtx insn
,
115 int operand_idx
, int bb_idx
);
117 void add_fixup_source_location (file_location loc
, rtx_insn
*insn
,
118 const char *filename
, int lineno
, int colno
);
120 void add_fixup_expr (file_location loc
, rtx x
,
123 rtx
consolidate_singletons (rtx x
);
125 void maybe_read_location (rtx_insn
*insn
);
127 void handle_insn_uids ();
128 void apply_fixups ();
131 struct uid_hash
: int_hash
<int, -1, -2> {};
132 hash_map
<uid_hash
, rtx_insn
*> m_insns_by_uid
;
133 auto_vec
<fixup
*> m_fixups
;
134 rtx_insn
*m_first_insn
;
135 auto_vec
<tree
> m_fake_scope
;
137 bool m_have_crtl_directive
;
138 basic_block m_bb_to_insert_after
;
139 auto_vec
<deferred_edge
> m_deferred_edges
;
140 int m_highest_bb_idx
;
143 /* Abstract base class for recording post-processing steps that must be
144 done after reading a .rtl file. */
149 /* Constructor for a fixup at LOC affecting X. */
150 fixup (file_location loc
, rtx x
)
151 : m_loc (loc
), m_rtx (x
)
155 virtual void apply (function_reader
*reader
) const = 0;
162 /* An abstract subclass of fixup for post-processing steps that
163 act on a specific operand of a specific instruction. */
165 class operand_fixup
: public fixup
168 /* Constructor for a fixup at LOC affecting INSN's operand
169 with index OPERAND_IDX. */
170 operand_fixup (file_location loc
, rtx insn
, int operand_idx
)
171 : fixup (loc
, insn
), m_operand_idx (operand_idx
)
178 /* A concrete subclass of operand_fixup: fixup an rtx_insn *
179 field based on an integer UID. */
181 class fixup_insn_uid
: public operand_fixup
184 /* Constructor for a fixup at LOC affecting INSN's operand
185 with index OPERAND_IDX. Record INSN_UID as the uid. */
186 fixup_insn_uid (file_location loc
, rtx insn
, int operand_idx
, int insn_uid
)
187 : operand_fixup (loc
, insn
, operand_idx
),
188 m_insn_uid (insn_uid
)
191 void apply (function_reader
*reader
) const final override
;
197 /* A concrete subclass of operand_fixup: fix up a
198 NOTE_INSN_BASIC_BLOCK based on an integer block ID. */
200 class fixup_note_insn_basic_block
: public operand_fixup
203 fixup_note_insn_basic_block (file_location loc
, rtx insn
, int operand_idx
,
205 : operand_fixup (loc
, insn
, operand_idx
),
209 void apply (function_reader
*reader
) const final override
;
215 /* A concrete subclass of fixup (not operand_fixup): fix up
216 the expr of an rtx (REG or MEM) based on a textual dump. */
218 class fixup_expr
: public fixup
221 fixup_expr (file_location loc
, rtx x
, const char *desc
)
223 m_desc (xstrdup (desc
))
226 ~fixup_expr () { free (m_desc
); }
228 void apply (function_reader
*reader
) const final override
;
234 /* Return a textual description of the operand of INSN with
235 index OPERAND_IDX. */
238 get_operand_name (rtx insn
, int operand_idx
)
240 gcc_assert (is_a
<rtx_insn
*> (insn
));
252 /* Fixup an rtx_insn * field based on an integer UID, as read by READER. */
255 fixup_insn_uid::apply (function_reader
*reader
) const
257 rtx_insn
**insn_from_uid
= reader
->get_insn_by_uid (m_insn_uid
);
259 XEXP (m_rtx
, m_operand_idx
) = *insn_from_uid
;
262 const char *op_name
= get_operand_name (m_rtx
, m_operand_idx
);
265 "insn with UID %i not found for operand %i (`%s') of insn %i",
266 m_insn_uid
, m_operand_idx
, op_name
, INSN_UID (m_rtx
));
269 "insn with UID %i not found for operand %i of insn %i",
270 m_insn_uid
, m_operand_idx
, INSN_UID (m_rtx
));
274 /* Fix up a NOTE_INSN_BASIC_BLOCK based on an integer block ID. */
277 fixup_note_insn_basic_block::apply (function_reader
*) const
279 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, m_bb_idx
);
281 NOTE_BASIC_BLOCK (m_rtx
) = bb
;
284 /* Fix up the expr of an rtx (REG or MEM) based on a textual dump
288 fixup_expr::apply (function_reader
*reader
) const
290 tree expr
= reader
->parse_mem_expr (m_desc
);
291 switch (GET_CODE (m_rtx
))
294 set_reg_attrs_for_decl_rtl (expr
, m_rtx
);
297 set_mem_expr (m_rtx
, expr
);
304 /* Strip trailing whitespace from DESC. */
307 strip_trailing_whitespace (char *desc
)
309 char *terminator
= desc
+ strlen (desc
);
310 while (desc
< terminator
)
313 if (ISSPACE (*terminator
))
320 /* Return the numeric value n for GET_NOTE_INSN_NAME (n) for STRING,
321 or fail if STRING isn't recognized. */
324 parse_note_insn_name (const char *string
)
326 for (int i
= 0; i
< NOTE_INSN_MAX
; i
++)
327 if (strcmp (string
, GET_NOTE_INSN_NAME (i
)) == 0)
329 fatal_with_file_and_line ("unrecognized NOTE_INSN name: `%s'", string
);
332 /* Return the register number for NAME, or return -1 if it isn't
336 lookup_reg_by_dump_name (const char *name
)
338 for (int i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
340 && ! strcmp (name
, reg_names
[i
]))
343 /* Also lookup virtuals. */
344 if (!strcmp (name
, "virtual-incoming-args"))
345 return VIRTUAL_INCOMING_ARGS_REGNUM
;
346 if (!strcmp (name
, "virtual-stack-vars"))
347 return VIRTUAL_STACK_VARS_REGNUM
;
348 if (!strcmp (name
, "virtual-stack-dynamic"))
349 return VIRTUAL_STACK_DYNAMIC_REGNUM
;
350 if (!strcmp (name
, "virtual-outgoing-args"))
351 return VIRTUAL_OUTGOING_ARGS_REGNUM
;
352 if (!strcmp (name
, "virtual-cfa"))
353 return VIRTUAL_CFA_REGNUM
;
354 if (!strcmp (name
, "virtual-preferred-stack-boundary"))
355 return VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM
;
356 /* TODO: handle "virtual-reg-%d". */
358 /* In compact mode, pseudos are printed with '< and '>' wrapping the regno,
359 offseting it by (LAST_VIRTUAL_REGISTER + 1), so that the
360 first non-virtual pseudo is dumped as "<0>". */
361 if (name
[0] == '<' && name
[strlen (name
) - 1] == '>')
363 int dump_num
= atoi (name
+ 1);
364 return dump_num
+ LAST_VIRTUAL_REGISTER
+ 1;
371 /* class function_reader : public rtx_reader */
373 /* function_reader's constructor. */
375 function_reader::function_reader ()
379 m_have_crtl_directive (false),
380 m_bb_to_insert_after (NULL
),
381 m_highest_bb_idx (EXIT_BLOCK
)
385 /* function_reader's destructor. */
387 function_reader::~function_reader ()
391 FOR_EACH_VEC_ELT (m_fixups
, i
, f
)
397 /* Implementation of rtx_reader::handle_unknown_directive,
398 for parsing the remainder of a directive with name NAME
401 Require a top-level "function" directive, as emitted by
402 print_rtx_function, and parse it. */
405 function_reader::handle_unknown_directive (file_location start_loc
,
408 if (strcmp (name
, "function"))
409 fatal_at (start_loc
, "expected 'function'");
412 error ("%<__RTL%> function cannot be compiled with %<-flto%>");
417 /* Parse the output of print_rtx_function (or hand-written data in the
418 same format), having already parsed the "(function" heading, and
419 finishing immediately before the final ")".
421 The "param" and "crtl" clauses are optional. */
424 function_reader::parse_function ()
426 m_name
= xstrdup (read_string (0));
432 int c
= read_skip_spaces ();
440 file_location loc
= get_current_location ();
441 struct md_name directive
;
442 read_name (&directive
);
443 if (strcmp (directive
.string
, "param") == 0)
445 else if (strcmp (directive
.string
, "insn-chain") == 0)
447 else if (strcmp (directive
.string
, "crtl") == 0)
450 fatal_with_file_and_line ("unrecognized directive: %s",
458 /* Rebuild the JUMP_LABEL field of any JUMP_INSNs in the chain, and the
459 LABEL_NUSES of any CODE_LABELs.
461 This has to happen after apply_fixups, since only after then do
462 LABEL_REFs have their label_ref_label set up. */
463 rebuild_jump_labels (get_insns ());
465 crtl
->init_stack_alignment ();
468 /* Set up state for the function *before* fixups are applied.
470 Create "cfun" and a decl for the function.
471 By default, every function decl is hardcoded as
472 int test_1 (int i, int j, int k);
473 Set up various other state:
474 - the cfg and basic blocks (edges are created later, *after* fixups
476 - add the function to the callgraph. */
479 function_reader::create_function ()
481 /* We start in cfgrtl mode, rather than cfglayout mode. */
482 rtl_register_cfg_hooks ();
484 /* When run from selftests or "rtl1", cfun is NULL.
485 When run from "cc1" for a C function tagged with __RTL, cfun is the
489 tree fn_name
= get_identifier (m_name
? m_name
: "test_1");
490 tree int_type
= integer_type_node
;
491 tree return_type
= int_type
;
492 tree arg_types
[3] = {int_type
, int_type
, int_type
};
493 tree fn_type
= build_function_type_array (return_type
, 3, arg_types
);
494 tree fndecl
= build_decl (UNKNOWN_LOCATION
, FUNCTION_DECL
, fn_name
, fn_type
);
495 tree resdecl
= build_decl (UNKNOWN_LOCATION
, RESULT_DECL
, NULL_TREE
,
497 DECL_ARTIFICIAL (resdecl
) = 1;
498 DECL_IGNORED_P (resdecl
) = 1;
499 DECL_RESULT (fndecl
) = resdecl
;
500 allocate_struct_function (fndecl
, false);
501 /* This sets cfun. */
502 current_function_decl
= fndecl
;
506 gcc_assert (current_function_decl
);
507 tree fndecl
= current_function_decl
;
509 /* Mark this function as being specified as __RTL. */
510 cfun
->curr_properties
|= PROP_rtl
;
512 /* cc1 normally inits DECL_INITIAL (fndecl) to be error_mark_node.
513 Create a dummy block for it. */
514 DECL_INITIAL (fndecl
) = make_node (BLOCK
);
516 cfun
->curr_properties
= (PROP_cfg
| PROP_rtl
);
518 /* Do we need this to force cgraphunit.cc to output the function? */
519 DECL_EXTERNAL (fndecl
) = 0;
520 DECL_PRESERVE_P (fndecl
) = 1;
523 cgraph_node::finalize_function (fndecl
, false);
525 /* Create bare-bones cfg. This creates the entry and exit blocks. */
526 init_empty_tree_cfg_for_function (cfun
);
527 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->flags
|= BB_RTL
;
528 EXIT_BLOCK_PTR_FOR_FN (cfun
)->flags
|= BB_RTL
;
529 init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
530 init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun
));
531 m_bb_to_insert_after
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
535 /* Look within the params of FNDECL for a param named NAME.
536 Return NULL_TREE if one isn't found. */
539 find_param_by_name (tree fndecl
, const char *name
)
541 for (tree arg
= DECL_ARGUMENTS (fndecl
); arg
; arg
= TREE_CHAIN (arg
))
542 if (id_equal (DECL_NAME (arg
), name
))
547 /* Parse the content of a "param" directive, having already parsed the
548 "(param". Consume the trailing ')'. */
551 function_reader::parse_param ()
553 require_char_ws ('"');
554 file_location loc
= get_current_location ();
555 char *name
= read_quoted_string ();
557 /* Lookup param by name. */
558 tree t_param
= find_param_by_name (cfun
->decl
, name
);
560 fatal_at (loc
, "param not found: %s", name
);
562 /* Parse DECL_RTL. */
563 require_char_ws ('(');
564 require_word_ws ("DECL_RTL");
565 DECL_WRTL_CHECK (t_param
)->decl_with_rtl
.rtl
= parse_rtx ();
566 require_char_ws (')');
568 /* Parse DECL_RTL_INCOMING. */
569 require_char_ws ('(');
570 require_word_ws ("DECL_RTL_INCOMING");
571 DECL_INCOMING_RTL (t_param
) = parse_rtx ();
572 require_char_ws (')');
574 require_char_ws (')');
577 /* Parse zero or more child insn elements within an
578 "insn-chain" element. Consume the trailing ')'. */
581 function_reader::parse_insn_chain ()
585 int c
= read_skip_spaces ();
586 file_location loc
= get_current_location ();
591 struct md_name directive
;
592 read_name (&directive
);
593 if (strcmp (directive
.string
, "block") == 0)
596 parse_insn (loc
, directive
.string
);
599 fatal_at (loc
, "expected '(' or ')'");
605 /* Parse zero or more child directives (edges and insns) within a
606 "block" directive, having already parsed the "(block " heading.
607 Consume the trailing ')'. */
610 function_reader::parse_block ()
612 /* Parse the index value from the dump. This will be an integer;
613 we don't support "entry" or "exit" here (unlike for edges). */
616 int bb_idx
= atoi (name
.string
);
618 /* The term "index" has two meanings for basic blocks in a CFG:
619 (a) the "index" field within struct basic_block_def.
620 (b) the index of a basic_block within the cfg's x_basic_block_info
621 vector, as accessed via BASIC_BLOCK_FOR_FN.
623 These can get out-of-sync when basic blocks are optimized away.
624 They get back in sync by "compact_blocks".
625 We reconstruct cfun->cfg->x_basic_block_info->address () pointed
626 vector elements with NULL values in it for any missing basic blocks,
627 so that (a) == (b) for all of the blocks we create. The
628 doubly-linked list of basic blocks (next_bb/prev_bb) skips over
631 if (m_highest_bb_idx
< bb_idx
)
632 m_highest_bb_idx
= bb_idx
;
634 size_t new_size
= m_highest_bb_idx
+ 1;
635 if (basic_block_info_for_fn (cfun
)->length () < new_size
)
636 vec_safe_grow_cleared (basic_block_info_for_fn (cfun
), new_size
, true);
638 last_basic_block_for_fn (cfun
) = new_size
;
640 /* Create the basic block.
642 We can't call create_basic_block and use the regular RTL block-creation
643 hooks, since this creates NOTE_INSN_BASIC_BLOCK instances. We don't
644 want to do that; we want to use the notes we were provided with. */
645 basic_block bb
= alloc_block ();
646 init_rtl_bb_info (bb
);
648 bb
->flags
= BB_NEW
| BB_RTL
;
649 link_block (bb
, m_bb_to_insert_after
);
650 m_bb_to_insert_after
= bb
;
652 n_basic_blocks_for_fn (cfun
)++;
653 SET_BASIC_BLOCK_FOR_FN (cfun
, bb_idx
, bb
);
654 BB_SET_PARTITION (bb
, BB_UNPARTITIONED
);
656 /* Handle insns, edge-from and edge-to directives. */
659 int c
= read_skip_spaces ();
660 file_location loc
= get_current_location ();
665 struct md_name directive
;
666 read_name (&directive
);
667 if (strcmp (directive
.string
, "edge-from") == 0)
668 parse_edge (bb
, true);
669 else if (strcmp (directive
.string
, "edge-to") == 0)
670 parse_edge (bb
, false);
673 rtx_insn
*insn
= parse_insn (loc
, directive
.string
);
674 set_block_for_insn (insn
, bb
);
681 fatal_at (loc
, "expected '(' or ')'");
685 /* Subroutine of function_reader::parse_edge.
686 Parse a basic block index, handling "entry" and "exit". */
689 function_reader::parse_bb_idx ()
693 if (strcmp (name
.string
, "entry") == 0)
695 if (strcmp (name
.string
, "exit") == 0)
697 return atoi (name
.string
);
700 /* Subroutine of parse_edge_flags.
701 Parse TOK, a token such as "FALLTHRU", converting to the flag value.
702 Issue an error if the token is unrecognized. */
705 parse_edge_flag_token (const char *tok
)
707 #define DEF_EDGE_FLAG(NAME,IDX) \
709 if (strcmp (tok, #NAME) == 0) \
710 return EDGE_##NAME; \
712 #include "cfg-flags.def"
714 error ("unrecognized edge flag: %qs", tok
);
718 /* Subroutine of function_reader::parse_edge.
719 Parse STR and convert to a flag value (or issue an error).
720 The parser uses strtok and hence modifiers STR in-place. */
723 parse_edge_flags (char *str
)
727 char *tok
= strtok (str
, "| ");
730 result
|= parse_edge_flag_token (tok
);
731 tok
= strtok (NULL
, "| ");
737 /* Parse an "edge-from" or "edge-to" directive within the "block"
738 directive for BLOCK, having already parsed the "(edge" heading.
739 Consume the final ")". Record the edge within m_deferred_edges.
740 FROM is true for an "edge-from" directive, false for an "edge-to"
744 function_reader::parse_edge (basic_block block
, bool from
)
747 int this_bb_idx
= block
->index
;
748 file_location loc
= get_current_location ();
749 int other_bb_idx
= parse_bb_idx ();
751 /* "(edge-from 2)" means src = 2, dest = this_bb_idx, whereas
752 "(edge-to 3)" means src = this_bb_idx, dest = 3. */
753 int src_idx
= from
? other_bb_idx
: this_bb_idx
;
754 int dest_idx
= from
? this_bb_idx
: other_bb_idx
;
756 /* Optional "(flags)". */
758 int c
= read_skip_spaces ();
761 require_word_ws ("flags");
762 require_char_ws ('"');
763 char *str
= read_quoted_string ();
764 flags
= parse_edge_flags (str
);
765 require_char_ws (')');
770 require_char_ws (')');
772 /* This BB already exists, but the other BB might not yet.
773 For now, save the edges, and create them at the end of insn-chain
775 /* For now, only process the (edge-from) to this BB, and (edge-to)
776 that go to the exit block.
777 FIXME: we don't yet verify that the edge-from and edge-to directives
779 if (from
|| dest_idx
== EXIT_BLOCK
)
780 m_deferred_edges
.safe_push (deferred_edge (loc
, src_idx
, dest_idx
, flags
));
783 /* Parse an rtx instruction, having parsed the opening and parenthesis, and
784 name NAME, seen at START_LOC, by calling read_rtx_code, calling
785 set_first_insn and set_last_insn as appropriate, and
786 adding the insn to the insn chain.
787 Consume the trailing ')'. */
790 function_reader::parse_insn (file_location start_loc
, const char *name
)
792 rtx x
= read_rtx_code (name
);
794 fatal_at (start_loc
, "expected insn type; got '%s'", name
);
795 rtx_insn
*insn
= dyn_cast
<rtx_insn
*> (x
);
797 fatal_at (start_loc
, "expected insn type; got '%s'", name
);
799 /* Consume the trailing ')'. */
800 require_char_ws (')');
802 rtx_insn
*last_insn
= get_last_insn ();
804 /* Add "insn" to the insn chain. */
807 gcc_assert (NEXT_INSN (last_insn
) == NULL
);
808 SET_NEXT_INSN (last_insn
) = insn
;
810 SET_PREV_INSN (insn
) = last_insn
;
812 /* Add it to the sequence. */
813 set_last_insn (insn
);
817 set_first_insn (insn
);
820 if (rtx_code_label
*label
= dyn_cast
<rtx_code_label
*> (insn
))
821 maybe_set_max_label_num (label
);
826 /* Postprocessing subroutine for parse_insn_chain: all the basic blocks
827 should have been created by now; create the edges that were seen. */
830 function_reader::create_edges ()
834 FOR_EACH_VEC_ELT (m_deferred_edges
, i
, de
)
836 /* The BBs should already have been created by parse_block. */
837 basic_block src
= BASIC_BLOCK_FOR_FN (cfun
, de
->m_src_bb_idx
);
839 fatal_at (de
->m_loc
, "error: block index %i not found",
841 basic_block dst
= BASIC_BLOCK_FOR_FN (cfun
, de
->m_dest_bb_idx
);
843 fatal_at (de
->m_loc
, "error: block with index %i not found",
845 unchecked_make_edge (src
, dst
, de
->m_flags
);
849 /* Parse a "crtl" directive, having already parsed the "(crtl" heading
851 Consume the final ")". */
854 function_reader::parse_crtl (file_location loc
)
856 if (m_have_crtl_directive
)
857 error_at (loc
, "more than one 'crtl' directive");
858 m_have_crtl_directive
= true;
861 require_char_ws ('(');
862 require_word_ws ("return_rtx");
863 crtl
->return_rtx
= parse_rtx ();
864 require_char_ws (')');
866 require_char_ws (')');
869 /* Parse operand IDX of X, returning X, or an equivalent rtx
870 expression (for consolidating singletons).
871 This is an overridden implementation of rtx_reader::read_rtx_operand for
872 function_reader, handling various extra data printed by print_rtx,
873 and sometimes calling the base class implementation. */
876 function_reader::read_rtx_operand (rtx x
, int idx
)
878 RTX_CODE code
= GET_CODE (x
);
879 const char *format_ptr
= GET_RTX_FORMAT (code
);
880 const char format_char
= format_ptr
[idx
];
883 /* Override the regular parser for some format codes. */
887 if (idx
== 7 && CALL_P (x
))
889 m_in_call_function_usage
= true;
890 rtx tem
= rtx_reader::read_rtx_operand (x
, idx
);
891 m_in_call_function_usage
= false;
895 return rtx_reader::read_rtx_operand (x
, idx
);
899 read_rtx_operand_u (x
, idx
);
900 /* Don't run regular parser for 'u'. */
906 read_rtx_operand_inL (x
, idx
, format_char
);
907 /* Don't run regular parser for these codes. */
911 gcc_assert (is_compact ());
912 /* Compact mode doesn't store BBs. */
913 /* Don't run regular parser. */
917 /* Don't run regular parser for 'r'. */
918 return read_rtx_operand_r (x
);
924 /* Call base class implementation. */
925 x
= rtx_reader::read_rtx_operand (x
, idx
);
927 /* Handle any additional parsing needed to handle what the dump
932 x
= extra_parsing_for_operand_code_0 (x
, idx
);
938 /* Strip away the redundant hex dump of the value. */
939 require_char_ws ('[');
941 require_char_ws (']');
952 /* Parse operand IDX of X, of code 'u', when reading function dumps.
954 The RTL file recorded the ID of an insn (or 0 for NULL); we
955 must store this as a pointer, but the insn might not have
956 been loaded yet. Store the ID away for now, via a fixup. */
959 function_reader::read_rtx_operand_u (rtx x
, int idx
)
961 /* In compact mode, the PREV/NEXT insn uids are not dumped, so skip
962 the "uu" when reading. */
963 if (is_compact () && GET_CODE (x
) != LABEL_REF
)
967 file_location loc
= read_name (&name
);
968 int insn_id
= atoi (name
.string
);
970 add_fixup_insn_uid (loc
, x
, idx
, insn_id
);
973 /* Read a name, looking for a match against a string found in array
974 STRINGS of size NUM_VALUES.
975 Return the index of the matched string, or emit an error. */
978 function_reader::parse_enum_value (int num_values
, const char *const *strings
)
982 for (int i
= 0; i
< num_values
; i
++)
984 if (strcmp (name
.string
, strings
[i
]) == 0)
987 error ("unrecognized enum value: %qs", name
.string
);
991 /* Parse operand IDX of X, of code 'i' or 'n' (as specified by FORMAT_CHAR).
992 Special-cased handling of these, for reading function dumps. */
995 function_reader::read_rtx_operand_inL (rtx x
, int idx
, char format_char
)
997 /* Handle some of the extra information that print_rtx
998 can write out for these cases. */
999 /* print_rtx only writes out operand 5 for notes
1000 for NOTE_KIND values NOTE_INSN_DELETED_LABEL
1001 and NOTE_INSN_DELETED_DEBUG_LABEL. */
1002 if (idx
== 5 && NOTE_P (x
))
1005 if (idx
== 4 && INSN_P (x
))
1007 maybe_read_location (as_a
<rtx_insn
*> (x
));
1011 /* INSN_CODEs aren't printed in compact mode, so don't attempt to
1015 && &INSN_CODE (x
) == &XINT (x
, idx
))
1021 /* Handle UNSPEC and UNSPEC_VOLATILE's operand 1. */
1022 #if !defined(GENERATOR_FILE) && NUM_UNSPECV_VALUES > 0
1024 && GET_CODE (x
) == UNSPEC_VOLATILE
)
1027 = parse_enum_value (NUM_UNSPECV_VALUES
, unspecv_strings
);
1031 #if !defined(GENERATOR_FILE) && NUM_UNSPEC_VALUES > 0
1033 && (GET_CODE (x
) == UNSPEC
1034 || GET_CODE (x
) == UNSPEC_VOLATILE
))
1037 = parse_enum_value (NUM_UNSPEC_VALUES
, unspec_strings
);
1042 struct md_name name
;
1045 if (format_char
== 'n')
1046 value
= parse_note_insn_name (name
.string
);
1049 gcc_checking_assert (format_char
== 'i');
1050 value
= atoi (name
.string
);
1052 XINT (x
, idx
) = value
;
1055 /* Parse the 'r' operand of X, returning X, or an equivalent rtx
1056 expression (for consolidating singletons).
1057 Special-cased handling of code 'r' for reading function dumps. */
1060 function_reader::read_rtx_operand_r (rtx x
)
1062 struct md_name name
;
1063 file_location loc
= read_name (&name
);
1064 int regno
= lookup_reg_by_dump_name (name
.string
);
1066 fatal_at (loc
, "unrecognized register: '%s'", name
.string
);
1068 set_regno_raw (x
, regno
, 1);
1070 /* Consolidate singletons. */
1071 x
= consolidate_singletons (x
);
1073 ORIGINAL_REGNO (x
) = regno
;
1075 /* Parse extra stuff at end of 'r'.
1076 We may have zero, one, or two sections marked by square
1078 int ch
= read_skip_spaces ();
1079 bool expect_original_regno
= false;
1082 file_location loc
= get_current_location ();
1083 char *desc
= read_until ("]", true);
1084 strip_trailing_whitespace (desc
);
1085 const char *desc_start
= desc
;
1086 /* If ORIGINAL_REGNO (rtx) != regno, we will have:
1087 "orig:%i", ORIGINAL_REGNO (rtx).
1088 Consume it, we don't set ORIGINAL_REGNO, since we can
1089 get that from the 2nd copy later. */
1090 if (startswith (desc
, "orig:"))
1092 expect_original_regno
= true;
1094 /* Skip to any whitespace following the integer. */
1095 const char *space
= strchr (desc_start
, ' ');
1097 desc_start
= space
+ 1;
1099 /* Any remaining text may be the REG_EXPR. Alternatively we have
1100 no REG_ATTRS, and instead we have ORIGINAL_REGNO. */
1101 if (ISDIGIT (*desc_start
))
1103 /* Assume we have ORIGINAL_REGNO. */
1104 ORIGINAL_REGNO (x
) = atoi (desc_start
);
1108 /* Assume we have REG_EXPR. */
1109 add_fixup_expr (loc
, x
, desc_start
);
1115 if (expect_original_regno
)
1117 require_char_ws ('[');
1118 char *desc
= read_until ("]", true);
1119 ORIGINAL_REGNO (x
) = atoi (desc
);
1126 /* Additional parsing for format code '0' in dumps, handling a variety
1127 of special-cases in print_rtx, when parsing operand IDX of X.
1128 Return X, or possibly a reallocated copy of X. */
1131 function_reader::extra_parsing_for_operand_code_0 (rtx x
, int idx
)
1133 RTX_CODE code
= GET_CODE (x
);
1135 struct md_name name
;
1137 if (idx
== 1 && code
== SYMBOL_REF
)
1139 /* Possibly wrote " [flags %#x]", SYMBOL_REF_FLAGS (in_rtx). */
1140 c
= read_skip_spaces ();
1143 file_location loc
= read_name (&name
);
1144 if (strcmp (name
.string
, "flags"))
1145 error_at (loc
, "was expecting `%s'", "flags");
1147 SYMBOL_REF_FLAGS (x
) = strtol (name
.string
, NULL
, 16);
1149 /* The standard RTX_CODE_SIZE (SYMBOL_REF) used when allocating
1150 x doesn't have space for the block_symbol information, so
1151 we must reallocate it if this flag is set. */
1152 if (SYMBOL_REF_HAS_BLOCK_INFO_P (x
))
1154 /* Emulate the allocation normally done by
1155 varasm.cc:create_block_symbol. */
1156 unsigned int size
= RTX_HDR_SIZE
+ sizeof (struct block_symbol
);
1157 rtx new_x
= (rtx
) ggc_internal_alloc (size
);
1159 /* Copy data over from the smaller SYMBOL_REF. */
1160 memcpy (new_x
, x
, RTX_CODE_SIZE (SYMBOL_REF
));
1163 /* We can't reconstruct SYMBOL_REF_BLOCK; set it to NULL. */
1164 SYMBOL_REF_BLOCK (x
) = NULL
;
1166 /* Zero the offset. */
1167 SYMBOL_REF_BLOCK_OFFSET (x
) = 0;
1175 /* If X had a non-NULL SYMBOL_REF_DECL,
1176 rtx_writer::print_rtx_operand_code_0 would have dumped it
1177 using print_node_brief.
1178 Skip the content for now. */
1179 c
= read_skip_spaces ();
1184 char ch
= read_char ();
1192 else if (idx
== 3 && code
== NOTE
)
1194 /* Note-specific data appears for operand 3, which annoyingly
1195 is before the enum specifying which kind of note we have
1197 c
= read_skip_spaces ();
1200 /* Possibly data for a NOTE_INSN_BASIC_BLOCK, of the form:
1202 file_location bb_loc
= read_name (&name
);
1203 if (strcmp (name
.string
, "bb"))
1204 error_at (bb_loc
, "was expecting `%s'", "bb");
1206 int bb_idx
= atoi (name
.string
);
1207 add_fixup_note_insn_basic_block (bb_loc
, x
, idx
,
1209 require_char_ws (']');
1218 /* Implementation of rtx_reader::handle_any_trailing_information.
1219 Handle the various additional information that print-rtl.cc can
1220 write after the regular fields, when parsing X. */
1223 function_reader::handle_any_trailing_information (rtx x
)
1225 struct md_name name
;
1227 switch (GET_CODE (x
))
1232 require_char_ws ('[');
1234 set_mem_alias_set (x
, atoi (name
.string
));
1235 /* We have either a MEM_EXPR, or a space. */
1236 if (peek_char () != ' ')
1238 file_location loc
= get_current_location ();
1239 char *desc
= read_until (" +", false);
1240 add_fixup_expr (loc
, consolidate_singletons (x
), desc
);
1246 /* We may optionally have '+' for MEM_OFFSET_KNOWN_P. */
1247 ch
= read_skip_spaces ();
1251 set_mem_offset (x
, atoi (name
.string
));
1256 /* Handle optional " S" for MEM_SIZE. */
1257 ch
= read_skip_spaces ();
1261 set_mem_size (x
, atoi (name
.string
));
1266 /* Handle optional " A" for MEM_ALIGN. */
1267 ch
= read_skip_spaces ();
1268 if (ch
== 'A' && peek_char () != 'S')
1271 set_mem_align (x
, atoi (name
.string
));
1276 /* Handle optional " AS" for MEM_ADDR_SPACE. */
1277 ch
= read_skip_spaces ();
1278 if (ch
== 'A' && peek_char () == 'S')
1282 set_mem_addr_space (x
, atoi (name
.string
));
1292 /* Assume that LABEL_NUSES was not dumped. */
1293 /* TODO: parse LABEL_KIND. */
1294 /* For now, skip until closing ')'. */
1297 char ch
= read_char ();
1312 /* Parse a tree dump for a MEM_EXPR in DESC and turn it back into a tree.
1313 We handle "<retval>" and param names within cfun, but for anything else
1314 we "cheat" by building a global VAR_DECL of type "int" with that name
1315 (returning the same global for a name if we see the same name more
1319 function_reader::parse_mem_expr (const char *desc
)
1321 tree fndecl
= cfun
->decl
;
1323 if (strcmp (desc
, "<retval>") == 0)
1324 return DECL_RESULT (fndecl
);
1326 tree param
= find_param_by_name (fndecl
, desc
);
1330 /* Search within decls we already created.
1331 FIXME: use a hash rather than linear search. */
1334 FOR_EACH_VEC_ELT (m_fake_scope
, i
, t
)
1335 if (id_equal (DECL_NAME (t
), desc
))
1338 /* Not found? Create it.
1339 This allows mimicking of real data but avoids having to specify
1340 e.g. names of locals, params etc.
1341 Though this way we don't know if we have a PARM_DECL vs a VAR_DECL,
1342 and we don't know the types. Fake it by making everything be
1343 a VAR_DECL of "int" type. */
1344 t
= build_decl (UNKNOWN_LOCATION
, VAR_DECL
,
1345 get_identifier (desc
),
1347 m_fake_scope
.safe_push (t
);
1351 /* Record that at LOC we saw an insn uid INSN_UID for the operand with index
1352 OPERAND_IDX within INSN, so that the pointer value can be fixed up in
1353 later post-processing. */
1356 function_reader::add_fixup_insn_uid (file_location loc
, rtx insn
, int operand_idx
,
1359 m_fixups
.safe_push (new fixup_insn_uid (loc
, insn
, operand_idx
, insn_uid
));
1362 /* Record that at LOC we saw an basic block index BB_IDX for the operand with index
1363 OPERAND_IDX within INSN, so that the pointer value can be fixed up in
1364 later post-processing. */
1367 function_reader::add_fixup_note_insn_basic_block (file_location loc
, rtx insn
,
1368 int operand_idx
, int bb_idx
)
1370 m_fixups
.safe_push (new fixup_note_insn_basic_block (loc
, insn
, operand_idx
,
1374 /* Placeholder hook for recording source location information seen in a dump.
1375 This is empty for now. */
1378 function_reader::add_fixup_source_location (file_location
, rtx_insn
*,
1379 const char *, int, int)
1383 /* Record that at LOC we saw textual description DESC of the MEM_EXPR or REG_EXPR
1384 of INSN, so that the fields can be fixed up in later post-processing. */
1387 function_reader::add_fixup_expr (file_location loc
, rtx insn
,
1391 /* Fail early if the RTL reader erroneously hands us an int. */
1392 gcc_assert (!ISDIGIT (desc
[0]));
1394 m_fixups
.safe_push (new fixup_expr (loc
, insn
, desc
));
1397 /* Helper function for consolidate_reg. Return the global rtx for
1398 the register with regno REGNO. */
1401 lookup_global_register (int regno
)
1403 /* We can't use a switch here, as some of the REGNUMs might not be constants
1404 for some targets. */
1405 if (regno
== STACK_POINTER_REGNUM
)
1406 return stack_pointer_rtx
;
1407 else if (regno
== FRAME_POINTER_REGNUM
)
1408 return frame_pointer_rtx
;
1409 else if (regno
== HARD_FRAME_POINTER_REGNUM
)
1410 return hard_frame_pointer_rtx
;
1411 else if (regno
== ARG_POINTER_REGNUM
)
1412 return arg_pointer_rtx
;
1413 else if (regno
== VIRTUAL_INCOMING_ARGS_REGNUM
)
1414 return virtual_incoming_args_rtx
;
1415 else if (regno
== VIRTUAL_STACK_VARS_REGNUM
)
1416 return virtual_stack_vars_rtx
;
1417 else if (regno
== VIRTUAL_STACK_DYNAMIC_REGNUM
)
1418 return virtual_stack_dynamic_rtx
;
1419 else if (regno
== VIRTUAL_OUTGOING_ARGS_REGNUM
)
1420 return virtual_outgoing_args_rtx
;
1421 else if (regno
== VIRTUAL_CFA_REGNUM
)
1422 return virtual_cfa_rtx
;
1423 else if (regno
== VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM
)
1424 return virtual_preferred_stack_boundary_rtx
;
1425 #ifdef return_ADDRESS_POINTER_REGNUM
1426 else if (regno
== RETURN_ADDRESS_POINTER_REGNUM
)
1427 return return_address_pointer_rtx
;
1433 /* Ensure that the backend can cope with a REG with regno REGNO.
1434 Normally REG instances are created by gen_reg_rtx which updates
1435 regno_reg_rtx, growing it as necessary.
1436 The REG instances created from the dumpfile weren't created this
1437 way, so we need to manually update regno_reg_rtx. */
1440 ensure_regno (int regno
)
1442 if (reg_rtx_no
< regno
+ 1)
1443 reg_rtx_no
= regno
+ 1;
1445 crtl
->emit
.ensure_regno_capacity ();
1446 gcc_assert (regno
< crtl
->emit
.regno_pointer_align_length
);
1449 /* Helper function for consolidate_singletons, for handling REG instances.
1450 Given REG instance X of some regno, return the singleton rtx for that
1451 regno, if it exists, or X. */
1454 consolidate_reg (rtx x
)
1456 gcc_assert (GET_CODE (x
) == REG
);
1458 unsigned int regno
= REGNO (x
);
1460 ensure_regno (regno
);
1462 /* Some register numbers have their rtx created in init_emit_regs
1463 e.g. stack_pointer_rtx for STACK_POINTER_REGNUM.
1464 Consolidate on this. */
1465 rtx global_reg
= lookup_global_register (regno
);
1469 /* Populate regno_reg_rtx if necessary. */
1470 if (regno_reg_rtx
[regno
] == NULL
)
1471 regno_reg_rtx
[regno
] = x
;
1473 gcc_assert (GET_CODE (regno_reg_rtx
[regno
]) == REG
);
1474 gcc_assert (REGNO (regno_reg_rtx
[regno
]) == regno
);
1475 if (GET_MODE (x
) == GET_MODE (regno_reg_rtx
[regno
]))
1476 return regno_reg_rtx
[regno
];
1481 /* When reading RTL function dumps, we must consolidate some
1482 rtx so that we use singletons where singletons are expected
1483 (e.g. we don't want multiple "(const_int 0 [0])" rtx, since
1484 these are tested via pointer equality against const0_rtx.
1486 Return the equivalent singleton rtx for X, if any, otherwise X. */
1489 function_reader::consolidate_singletons (rtx x
)
1494 switch (GET_CODE (x
))
1496 case PC
: return pc_rtx
;
1497 case RETURN
: return ret_rtx
;
1498 case SIMPLE_RETURN
: return simple_return_rtx
;
1501 return consolidate_reg (x
);
1504 return gen_rtx_CONST_INT (GET_MODE (x
), INTVAL (x
));
1507 return gen_rtx_CONST_VECTOR (GET_MODE (x
), XVEC (x
, 0));
1516 /* Parse an rtx directive, including both the opening/closing parentheses,
1520 function_reader::parse_rtx ()
1522 require_char_ws ('(');
1523 struct md_name directive
;
1524 read_name (&directive
);
1526 = consolidate_singletons (read_rtx_code (directive
.string
));
1527 require_char_ws (')');
1532 /* Implementation of rtx_reader::postprocess for reading function dumps.
1533 Return the equivalent singleton rtx for X, if any, otherwise X. */
1536 function_reader::postprocess (rtx x
)
1538 return consolidate_singletons (x
);
1541 /* Implementation of rtx_reader::finalize_string for reading function dumps.
1542 Make a GC-managed copy of STRINGBUF. */
1545 function_reader::finalize_string (char *stringbuf
)
1547 return ggc_strdup (stringbuf
);
1550 /* Attempt to parse optional location information for insn INSN, as
1551 potentially written out by rtx_writer::print_rtx_operand_code_i.
1552 We look for a quoted string followed by a colon. */
1555 function_reader::maybe_read_location (rtx_insn
*insn
)
1557 file_location loc
= get_current_location ();
1559 /* Attempt to parse a quoted string. */
1560 int ch
= read_skip_spaces ();
1563 char *filename
= read_quoted_string ();
1565 struct md_name line_num
;
1566 read_name (&line_num
);
1569 int ch
= read_char ();
1572 struct md_name column_num
;
1573 read_name (&column_num
);
1574 column
= atoi (column_num
.string
);
1578 add_fixup_source_location (loc
, insn
, filename
,
1579 atoi (line_num
.string
),
1586 /* Postprocessing subroutine of function_reader::parse_function.
1587 Populate m_insns_by_uid. */
1590 function_reader::handle_insn_uids ()
1592 /* Locate the currently assigned INSN_UID values, storing
1593 them in m_insns_by_uid. */
1595 for (rtx_insn
*insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
1597 if (m_insns_by_uid
.get (INSN_UID (insn
)))
1598 error ("duplicate insn UID: %i", INSN_UID (insn
));
1599 m_insns_by_uid
.put (INSN_UID (insn
), insn
);
1600 if (INSN_UID (insn
) > max_uid
)
1601 max_uid
= INSN_UID (insn
);
1604 /* Ensure x_cur_insn_uid is 1 more than the biggest insn UID seen.
1605 This is normally updated by the various make_*insn_raw functions. */
1606 crtl
->emit
.x_cur_insn_uid
= max_uid
+ 1;
1609 /* Apply all of the recorded fixups. */
1612 function_reader::apply_fixups ()
1616 FOR_EACH_VEC_ELT (m_fixups
, i
, f
)
1620 /* Given a UID value, try to locate a pointer to the corresponding
1621 rtx_insn *, or NULL if it can't be found. */
1624 function_reader::get_insn_by_uid (int uid
)
1626 return m_insns_by_uid
.get (uid
);
1629 /* Run the RTL dump parser, parsing a dump located at PATH.
1630 Return true iff the file was successfully parsed. */
1633 read_rtl_function_body (const char *path
)
1636 crtl
->abi
= &default_function_abi
;
1638 init_varasm_status ();
1640 function_reader reader
;
1641 if (!reader
.read_file (path
))
1647 /* Run the RTL dump parser on the range of lines between START_LOC and
1648 END_LOC (including those lines). */
1651 read_rtl_function_body_from_file_range (location_t start_loc
,
1654 expanded_location exploc_start
= expand_location (start_loc
);
1655 expanded_location exploc_end
= expand_location (end_loc
);
1657 if (exploc_start
.file
!= exploc_end
.file
)
1659 error_at (end_loc
, "start/end of RTL fragment are in different files");
1662 if (exploc_start
.line
>= exploc_end
.line
)
1665 "start of RTL fragment must be on an earlier line than end");
1670 crtl
->abi
= &fndecl_abi (cfun
->decl
).base_abi ();
1672 init_varasm_status ();
1674 function_reader reader
;
1675 if (!reader
.read_file_fragment (exploc_start
.file
, exploc_start
.line
,
1676 exploc_end
.line
- 1))
1684 namespace selftest
{
1686 /* Verify that parse_edge_flags works. */
1691 /* parse_edge_flags modifies its input (due to strtok), so we must make
1692 a copy of the literals. */
1693 #define ASSERT_PARSE_EDGE_FLAGS(EXPECTED, STR) \
1695 char *str = xstrdup (STR); \
1696 ASSERT_EQ (EXPECTED, parse_edge_flags (str)); \
1700 ASSERT_PARSE_EDGE_FLAGS (0, "");
1701 ASSERT_PARSE_EDGE_FLAGS (EDGE_FALLTHRU
, "FALLTHRU");
1702 ASSERT_PARSE_EDGE_FLAGS (EDGE_ABNORMAL_CALL
, "ABNORMAL_CALL");
1703 ASSERT_PARSE_EDGE_FLAGS (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
,
1704 "ABNORMAL | ABNORMAL_CALL");
1706 #undef ASSERT_PARSE_EDGE_FLAGS
1709 /* Verify that lookup_reg_by_dump_name works. */
1712 test_parsing_regnos ()
1714 ASSERT_EQ (-1, lookup_reg_by_dump_name ("this is not a register"));
1716 /* Verify lookup of virtual registers. */
1717 ASSERT_EQ (VIRTUAL_INCOMING_ARGS_REGNUM
,
1718 lookup_reg_by_dump_name ("virtual-incoming-args"));
1719 ASSERT_EQ (VIRTUAL_STACK_VARS_REGNUM
,
1720 lookup_reg_by_dump_name ("virtual-stack-vars"));
1721 ASSERT_EQ (VIRTUAL_STACK_DYNAMIC_REGNUM
,
1722 lookup_reg_by_dump_name ("virtual-stack-dynamic"));
1723 ASSERT_EQ (VIRTUAL_OUTGOING_ARGS_REGNUM
,
1724 lookup_reg_by_dump_name ("virtual-outgoing-args"));
1725 ASSERT_EQ (VIRTUAL_CFA_REGNUM
,
1726 lookup_reg_by_dump_name ("virtual-cfa"));
1727 ASSERT_EQ (VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM
,
1728 lookup_reg_by_dump_name ("virtual-preferred-stack-boundary"));
1730 /* Verify lookup of non-virtual pseudos. */
1731 ASSERT_EQ (LAST_VIRTUAL_REGISTER
+ 1, lookup_reg_by_dump_name ("<0>"));
1732 ASSERT_EQ (LAST_VIRTUAL_REGISTER
+ 2, lookup_reg_by_dump_name ("<1>"));
1735 /* Verify that edge E is as expected, with the src and dest basic blocks
1736 having indices EXPECTED_SRC_IDX and EXPECTED_DEST_IDX respectively, and
1737 the edge having flags equal to EXPECTED_FLAGS.
1738 Use LOC as the effective location when reporting failures. */
1741 assert_edge_at (const location
&loc
, edge e
, int expected_src_idx
,
1742 int expected_dest_idx
, int expected_flags
)
1744 ASSERT_EQ_AT (loc
, expected_src_idx
, e
->src
->index
);
1745 ASSERT_EQ_AT (loc
, expected_dest_idx
, e
->dest
->index
);
1746 ASSERT_EQ_AT (loc
, expected_flags
, e
->flags
);
1749 /* Verify that edge EDGE is as expected, with the src and dest basic blocks
1750 having indices EXPECTED_SRC_IDX and EXPECTED_DEST_IDX respectively, and
1751 the edge having flags equal to EXPECTED_FLAGS. */
1753 #define ASSERT_EDGE(EDGE, EXPECTED_SRC_IDX, EXPECTED_DEST_IDX, \
1755 assert_edge_at (SELFTEST_LOCATION, EDGE, EXPECTED_SRC_IDX, \
1756 EXPECTED_DEST_IDX, EXPECTED_FLAGS)
1758 /* Verify that we can load RTL dumps. */
1761 test_loading_dump_fragment_1 ()
1763 // TODO: filter on target?
1764 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("asr_div1.rtl"));
1766 /* Verify that the insns were loaded correctly. */
1767 rtx_insn
*insn_1
= get_insns ();
1768 ASSERT_TRUE (insn_1
);
1769 ASSERT_EQ (1, INSN_UID (insn_1
));
1770 ASSERT_EQ (INSN
, GET_CODE (insn_1
));
1771 ASSERT_EQ (SET
, GET_CODE (PATTERN (insn_1
)));
1772 ASSERT_EQ (NULL
, PREV_INSN (insn_1
));
1774 rtx_insn
*insn_2
= NEXT_INSN (insn_1
);
1775 ASSERT_TRUE (insn_2
);
1776 ASSERT_EQ (2, INSN_UID (insn_2
));
1777 ASSERT_EQ (INSN
, GET_CODE (insn_2
));
1778 ASSERT_EQ (insn_1
, PREV_INSN (insn_2
));
1779 ASSERT_EQ (NULL
, NEXT_INSN (insn_2
));
1781 /* Verify that registers were loaded correctly. */
1782 rtx insn_1_dest
= SET_DEST (PATTERN (insn_1
));
1783 ASSERT_EQ (REG
, GET_CODE (insn_1_dest
));
1784 ASSERT_EQ ((LAST_VIRTUAL_REGISTER
+ 1) + 2, REGNO (insn_1_dest
));
1785 rtx insn_1_src
= SET_SRC (PATTERN (insn_1
));
1786 ASSERT_EQ (LSHIFTRT
, GET_CODE (insn_1_src
));
1787 rtx reg
= XEXP (insn_1_src
, 0);
1788 ASSERT_EQ (REG
, GET_CODE (reg
));
1789 ASSERT_EQ (LAST_VIRTUAL_REGISTER
+ 1, REGNO (reg
));
1791 /* Verify that get_insn_by_uid works. */
1792 ASSERT_EQ (insn_1
, get_insn_by_uid (1));
1793 ASSERT_EQ (insn_2
, get_insn_by_uid (2));
1795 /* Verify that basic blocks were created. */
1796 ASSERT_EQ (2, BLOCK_FOR_INSN (insn_1
)->index
);
1797 ASSERT_EQ (2, BLOCK_FOR_INSN (insn_2
)->index
);
1799 /* Verify that the CFG was recreated. */
1801 verify_three_block_rtl_cfg (cfun
);
1802 basic_block bb2
= BASIC_BLOCK_FOR_FN (cfun
, 2);
1803 ASSERT_TRUE (bb2
!= NULL
);
1804 ASSERT_EQ (BB_RTL
, bb2
->flags
& BB_RTL
);
1805 ASSERT_EQ (2, bb2
->index
);
1806 ASSERT_EQ (insn_1
, BB_HEAD (bb2
));
1807 ASSERT_EQ (insn_2
, BB_END (bb2
));
1810 /* Verify loading another RTL dump. */
1813 test_loading_dump_fragment_2 ()
1815 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("simple-cse.rtl"));
1817 rtx_insn
*insn_1
= get_insn_by_uid (1);
1818 rtx_insn
*insn_2
= get_insn_by_uid (2);
1819 rtx_insn
*insn_3
= get_insn_by_uid (3);
1821 rtx set1
= single_set (insn_1
);
1822 ASSERT_NE (NULL
, set1
);
1823 rtx set2
= single_set (insn_2
);
1824 ASSERT_NE (NULL
, set2
);
1825 rtx set3
= single_set (insn_3
);
1826 ASSERT_NE (NULL
, set3
);
1828 rtx src1
= SET_SRC (set1
);
1829 ASSERT_EQ (PLUS
, GET_CODE (src1
));
1831 rtx src2
= SET_SRC (set2
);
1832 ASSERT_EQ (PLUS
, GET_CODE (src2
));
1834 /* Both src1 and src2 refer to "(reg:SI %0)".
1835 Verify that we have pointer equality. */
1836 rtx lhs1
= XEXP (src1
, 0);
1837 rtx lhs2
= XEXP (src2
, 0);
1838 ASSERT_EQ (lhs1
, lhs2
);
1840 /* Verify that the CFG was recreated. */
1842 verify_three_block_rtl_cfg (cfun
);
1845 /* Verify that CODE_LABEL insns are loaded correctly. */
1848 test_loading_labels ()
1850 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("example-labels.rtl"));
1852 rtx_insn
*insn_100
= get_insn_by_uid (100);
1853 ASSERT_EQ (CODE_LABEL
, GET_CODE (insn_100
));
1854 ASSERT_EQ (100, INSN_UID (insn_100
));
1855 ASSERT_EQ (NULL
, LABEL_NAME (insn_100
));
1856 ASSERT_EQ (0, LABEL_NUSES (insn_100
));
1857 ASSERT_EQ (30, CODE_LABEL_NUMBER (insn_100
));
1859 rtx_insn
*insn_200
= get_insn_by_uid (200);
1860 ASSERT_EQ (CODE_LABEL
, GET_CODE (insn_200
));
1861 ASSERT_EQ (200, INSN_UID (insn_200
));
1862 ASSERT_STREQ ("some_label_name", LABEL_NAME (insn_200
));
1863 ASSERT_EQ (0, LABEL_NUSES (insn_200
));
1864 ASSERT_EQ (40, CODE_LABEL_NUMBER (insn_200
));
1866 /* Ensure that the presence of CODE_LABEL_NUMBER == 40
1867 means that the next label num to be handed out will be 41. */
1868 ASSERT_EQ (41, max_label_num ());
1870 /* Ensure that label names read from a dump are GC-managed
1871 and are found through the insn. */
1872 ggc_collect (GGC_COLLECT_FORCE
);
1873 ASSERT_TRUE (ggc_marked_p (insn_200
));
1874 ASSERT_TRUE (ggc_marked_p (LABEL_NAME (insn_200
)));
1877 /* Verify that the loader copes with an insn with a mode. */
1880 test_loading_insn_with_mode ()
1882 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("insn-with-mode.rtl"));
1883 rtx_insn
*insn
= get_insns ();
1884 ASSERT_EQ (INSN
, GET_CODE (insn
));
1886 /* Verify that the "TI" mode was set from "insn:TI". */
1887 ASSERT_EQ (TImode
, GET_MODE (insn
));
1890 /* Verify that the loader copes with a jump_insn to a label_ref. */
1893 test_loading_jump_to_label_ref ()
1895 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("jump-to-label-ref.rtl"));
1897 rtx_insn
*jump_insn
= get_insn_by_uid (1);
1898 ASSERT_EQ (JUMP_INSN
, GET_CODE (jump_insn
));
1900 rtx_insn
*barrier
= get_insn_by_uid (2);
1901 ASSERT_EQ (BARRIER
, GET_CODE (barrier
));
1903 rtx_insn
*code_label
= get_insn_by_uid (100);
1904 ASSERT_EQ (CODE_LABEL
, GET_CODE (code_label
));
1906 /* Verify the jump_insn. */
1907 ASSERT_EQ (4, BLOCK_FOR_INSN (jump_insn
)->index
);
1908 ASSERT_EQ (SET
, GET_CODE (PATTERN (jump_insn
)));
1909 /* Ensure that the "(pc)" is using the global singleton. */
1910 ASSERT_RTX_PTR_EQ (pc_rtx
, SET_DEST (PATTERN (jump_insn
)));
1911 rtx label_ref
= SET_SRC (PATTERN (jump_insn
));
1912 ASSERT_EQ (LABEL_REF
, GET_CODE (label_ref
));
1913 ASSERT_EQ (code_label
, label_ref_label (label_ref
));
1914 ASSERT_EQ (code_label
, JUMP_LABEL (jump_insn
));
1916 /* Verify the code_label. */
1917 ASSERT_EQ (5, BLOCK_FOR_INSN (code_label
)->index
);
1918 ASSERT_EQ (NULL
, LABEL_NAME (code_label
));
1919 ASSERT_EQ (1, LABEL_NUSES (code_label
));
1921 /* Verify the generated CFG. */
1923 /* Locate blocks. */
1924 basic_block entry
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
1925 ASSERT_TRUE (entry
!= NULL
);
1926 ASSERT_EQ (ENTRY_BLOCK
, entry
->index
);
1928 basic_block exit
= EXIT_BLOCK_PTR_FOR_FN (cfun
);
1929 ASSERT_TRUE (exit
!= NULL
);
1930 ASSERT_EQ (EXIT_BLOCK
, exit
->index
);
1932 basic_block bb4
= (*cfun
->cfg
->x_basic_block_info
)[4];
1933 basic_block bb5
= (*cfun
->cfg
->x_basic_block_info
)[5];
1934 ASSERT_EQ (4, bb4
->index
);
1935 ASSERT_EQ (5, bb5
->index
);
1938 ASSERT_EQ (NULL
, entry
->preds
);
1939 ASSERT_EQ (1, entry
->succs
->length ());
1940 ASSERT_EDGE ((*entry
->succs
)[0], 0, 4, EDGE_FALLTHRU
);
1943 ASSERT_EQ (1, bb4
->preds
->length ());
1944 ASSERT_EDGE ((*bb4
->preds
)[0], 0, 4, EDGE_FALLTHRU
);
1945 ASSERT_EQ (1, bb4
->succs
->length ());
1946 ASSERT_EDGE ((*bb4
->succs
)[0], 4, 5, 0x0);
1949 ASSERT_EQ (1, bb5
->preds
->length ());
1950 ASSERT_EDGE ((*bb5
->preds
)[0], 4, 5, 0x0);
1951 ASSERT_EQ (1, bb5
->succs
->length ());
1952 ASSERT_EDGE ((*bb5
->succs
)[0], 5, 1, EDGE_FALLTHRU
);
1955 ASSERT_EQ (1, exit
->preds
->length ());
1956 ASSERT_EDGE ((*exit
->preds
)[0], 5, 1, EDGE_FALLTHRU
);
1957 ASSERT_EQ (NULL
, exit
->succs
);
1960 /* Verify that the loader copes with a jump_insn to a label_ref
1964 test_loading_jump_to_return ()
1966 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("jump-to-return.rtl"));
1968 rtx_insn
*jump_insn
= get_insn_by_uid (1);
1969 ASSERT_EQ (JUMP_INSN
, GET_CODE (jump_insn
));
1970 ASSERT_RTX_PTR_EQ (ret_rtx
, JUMP_LABEL (jump_insn
));
1973 /* Verify that the loader copes with a jump_insn to a label_ref
1974 marked "simple_return". */
1977 test_loading_jump_to_simple_return ()
1979 rtl_dump_test
t (SELFTEST_LOCATION
,
1980 locate_file ("jump-to-simple-return.rtl"));
1982 rtx_insn
*jump_insn
= get_insn_by_uid (1);
1983 ASSERT_EQ (JUMP_INSN
, GET_CODE (jump_insn
));
1984 ASSERT_RTX_PTR_EQ (simple_return_rtx
, JUMP_LABEL (jump_insn
));
1987 /* Verify that the loader copes with a NOTE_INSN_BASIC_BLOCK. */
1990 test_loading_note_insn_basic_block ()
1992 rtl_dump_test
t (SELFTEST_LOCATION
,
1993 locate_file ("note_insn_basic_block.rtl"));
1995 rtx_insn
*note
= get_insn_by_uid (1);
1996 ASSERT_EQ (NOTE
, GET_CODE (note
));
1997 ASSERT_EQ (2, BLOCK_FOR_INSN (note
)->index
);
1999 ASSERT_EQ (NOTE_INSN_BASIC_BLOCK
, NOTE_KIND (note
));
2000 ASSERT_EQ (2, NOTE_BASIC_BLOCK (note
)->index
);
2001 ASSERT_EQ (BASIC_BLOCK_FOR_FN (cfun
, 2), NOTE_BASIC_BLOCK (note
));
2004 /* Verify that the loader copes with a NOTE_INSN_DELETED. */
2007 test_loading_note_insn_deleted ()
2009 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("note-insn-deleted.rtl"));
2011 rtx_insn
*note
= get_insn_by_uid (1);
2012 ASSERT_EQ (NOTE
, GET_CODE (note
));
2013 ASSERT_EQ (NOTE_INSN_DELETED
, NOTE_KIND (note
));
2016 /* Verify that the const_int values are consolidated, since
2017 pointer equality corresponds to value equality.
2018 TODO: do this for all in CASE_CONST_UNIQUE. */
2021 test_loading_const_int ()
2023 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("const-int.rtl"));
2025 /* Verify that const_int values below MAX_SAVED_CONST_INT use
2026 the global values. */
2027 ASSERT_EQ (const0_rtx
, SET_SRC (PATTERN (get_insn_by_uid (1))));
2028 ASSERT_EQ (const1_rtx
, SET_SRC (PATTERN (get_insn_by_uid (2))));
2029 ASSERT_EQ (constm1_rtx
, SET_SRC (PATTERN (get_insn_by_uid (3))));
2031 /* Verify that other const_int values are consolidated. */
2032 rtx int256
= gen_rtx_CONST_INT (SImode
, 256);
2033 ASSERT_EQ (int256
, SET_SRC (PATTERN (get_insn_by_uid (4))));
2036 /* Verify that the loader copes with a SYMBOL_REF. */
2039 test_loading_symbol_ref ()
2041 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("symbol-ref.rtl"));
2043 rtx_insn
*insn
= get_insns ();
2045 rtx high
= SET_SRC (PATTERN (insn
));
2046 ASSERT_EQ (HIGH
, GET_CODE (high
));
2048 rtx symbol_ref
= XEXP (high
, 0);
2049 ASSERT_EQ (SYMBOL_REF
, GET_CODE (symbol_ref
));
2051 /* Verify that "[flags 0xc0]" was parsed. */
2052 ASSERT_EQ (0xc0, SYMBOL_REF_FLAGS (symbol_ref
));
2053 /* TODO: we don't yet load SYMBOL_REF_DECL. */
2056 /* Verify that the loader can rebuild a CFG. */
2061 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("cfg-test.rtl"));
2063 ASSERT_STREQ ("cfg_test", IDENTIFIER_POINTER (DECL_NAME (cfun
->decl
)));
2067 ASSERT_TRUE (cfun
->cfg
!= NULL
);
2068 ASSERT_EQ (6, n_basic_blocks_for_fn (cfun
));
2069 ASSERT_EQ (6, n_edges_for_fn (cfun
));
2071 /* The "fake" basic blocks. */
2072 basic_block entry
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
2073 ASSERT_TRUE (entry
!= NULL
);
2074 ASSERT_EQ (ENTRY_BLOCK
, entry
->index
);
2076 basic_block exit
= EXIT_BLOCK_PTR_FOR_FN (cfun
);
2077 ASSERT_TRUE (exit
!= NULL
);
2078 ASSERT_EQ (EXIT_BLOCK
, exit
->index
);
2080 /* The "real" basic blocks. */
2081 basic_block bb2
= (*cfun
->cfg
->x_basic_block_info
)[2];
2082 basic_block bb3
= (*cfun
->cfg
->x_basic_block_info
)[3];
2083 basic_block bb4
= (*cfun
->cfg
->x_basic_block_info
)[4];
2084 basic_block bb5
= (*cfun
->cfg
->x_basic_block_info
)[5];
2086 ASSERT_EQ (2, bb2
->index
);
2087 ASSERT_EQ (3, bb3
->index
);
2088 ASSERT_EQ (4, bb4
->index
);
2089 ASSERT_EQ (5, bb5
->index
);
2091 /* Verify connectivity. */
2094 ASSERT_EQ (NULL
, entry
->preds
);
2095 ASSERT_EQ (1, entry
->succs
->length ());
2096 ASSERT_EDGE ((*entry
->succs
)[0], 0, 2, EDGE_FALLTHRU
);
2099 ASSERT_EQ (1, bb2
->preds
->length ());
2100 ASSERT_EDGE ((*bb2
->preds
)[0], 0, 2, EDGE_FALLTHRU
);
2101 ASSERT_EQ (2, bb2
->succs
->length ());
2102 ASSERT_EDGE ((*bb2
->succs
)[0], 2, 3, EDGE_TRUE_VALUE
);
2103 ASSERT_EDGE ((*bb2
->succs
)[1], 2, 4, EDGE_FALSE_VALUE
);
2106 ASSERT_EQ (1, bb3
->preds
->length ());
2107 ASSERT_EDGE ((*bb3
->preds
)[0], 2, 3, EDGE_TRUE_VALUE
);
2108 ASSERT_EQ (1, bb3
->succs
->length ());
2109 ASSERT_EDGE ((*bb3
->succs
)[0], 3, 5, EDGE_FALLTHRU
);
2112 ASSERT_EQ (1, bb4
->preds
->length ());
2113 ASSERT_EDGE ((*bb4
->preds
)[0], 2, 4, EDGE_FALSE_VALUE
);
2114 ASSERT_EQ (1, bb4
->succs
->length ());
2115 ASSERT_EDGE ((*bb4
->succs
)[0], 4, 5, EDGE_FALLTHRU
);
2118 ASSERT_EQ (2, bb5
->preds
->length ());
2119 ASSERT_EDGE ((*bb5
->preds
)[0], 3, 5, EDGE_FALLTHRU
);
2120 ASSERT_EDGE ((*bb5
->preds
)[1], 4, 5, EDGE_FALLTHRU
);
2121 ASSERT_EQ (1, bb5
->succs
->length ());
2122 ASSERT_EDGE ((*bb5
->succs
)[0], 5, 1, EDGE_FALLTHRU
);
2125 ASSERT_EQ (1, exit
->preds
->length ());
2126 ASSERT_EDGE ((*exit
->preds
)[0], 5, 1, EDGE_FALLTHRU
);
2127 ASSERT_EQ (NULL
, exit
->succs
);
2130 /* Verify that the loader copes with sparse block indices.
2131 This testcase loads a file with a "(block 42)". */
2134 test_loading_bb_index ()
2136 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("bb-index.rtl"));
2138 ASSERT_STREQ ("test_bb_index", IDENTIFIER_POINTER (DECL_NAME (cfun
->decl
)));
2142 ASSERT_TRUE (cfun
->cfg
!= NULL
);
2143 ASSERT_EQ (3, n_basic_blocks_for_fn (cfun
));
2144 ASSERT_EQ (43, basic_block_info_for_fn (cfun
)->length ());
2145 ASSERT_EQ (2, n_edges_for_fn (cfun
));
2147 ASSERT_EQ (NULL
, (*cfun
->cfg
->x_basic_block_info
)[41]);
2148 basic_block bb42
= (*cfun
->cfg
->x_basic_block_info
)[42];
2149 ASSERT_NE (NULL
, bb42
);
2150 ASSERT_EQ (42, bb42
->index
);
2153 /* Verify that function_reader::handle_any_trailing_information correctly
2154 parses all the possible items emitted for a MEM. */
2159 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("mem.rtl"));
2161 ASSERT_STREQ ("test_mem", IDENTIFIER_POINTER (DECL_NAME (cfun
->decl
)));
2164 /* Verify parsing of "[42 i+17 S8 A128 AS5]". */
2165 rtx_insn
*insn_1
= get_insn_by_uid (1);
2166 rtx set1
= single_set (insn_1
);
2167 rtx mem1
= SET_DEST (set1
);
2168 ASSERT_EQ (42, MEM_ALIAS_SET (mem1
));
2170 ASSERT_TRUE (MEM_OFFSET_KNOWN_P (mem1
));
2171 ASSERT_KNOWN_EQ (17, MEM_OFFSET (mem1
));
2173 ASSERT_KNOWN_EQ (8, MEM_SIZE (mem1
));
2175 ASSERT_EQ (128, MEM_ALIGN (mem1
));
2177 ASSERT_EQ (5, MEM_ADDR_SPACE (mem1
));
2179 /* Verify parsing of "43 i+18 S9 AS6"
2180 (an address space without an alignment). */
2181 rtx_insn
*insn_2
= get_insn_by_uid (2);
2182 rtx set2
= single_set (insn_2
);
2183 rtx mem2
= SET_DEST (set2
);
2184 ASSERT_EQ (43, MEM_ALIAS_SET (mem2
));
2186 ASSERT_TRUE (MEM_OFFSET_KNOWN_P (mem2
));
2187 ASSERT_KNOWN_EQ (18, MEM_OFFSET (mem2
));
2189 ASSERT_KNOWN_EQ (9, MEM_SIZE (mem2
));
2191 ASSERT_EQ (6, MEM_ADDR_SPACE (mem2
));
2194 /* Verify that "repeated xN" is read correctly. */
2197 test_loading_repeat ()
2199 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("repeat.rtl"));
2201 rtx_insn
*insn_1
= get_insn_by_uid (1);
2202 ASSERT_EQ (PARALLEL
, GET_CODE (PATTERN (insn_1
)));
2203 ASSERT_EQ (64, XVECLEN (PATTERN (insn_1
), 0));
2204 for (int i
= 0; i
< 64; i
++)
2205 ASSERT_EQ (const0_rtx
, XVECEXP (PATTERN (insn_1
), 0, i
));
2208 /* Run all of the selftests within this file. */
2211 read_rtl_function_cc_tests ()
2214 test_parsing_regnos ();
2215 test_loading_dump_fragment_1 ();
2216 test_loading_dump_fragment_2 ();
2217 test_loading_labels ();
2218 test_loading_insn_with_mode ();
2219 test_loading_jump_to_label_ref ();
2220 test_loading_jump_to_return ();
2221 test_loading_jump_to_simple_return ();
2222 test_loading_note_insn_basic_block ();
2223 test_loading_note_insn_deleted ();
2224 test_loading_const_int ();
2225 test_loading_symbol_ref ();
2226 test_loading_cfg ();
2227 test_loading_bb_index ();
2228 test_loading_mem ();
2229 test_loading_repeat ();
2232 } // namespace selftest
2234 #endif /* #if CHECKING_P */