1 /* read-rtl-function.c - Reader for RTL function dumps
2 Copyright (C) 2016-2017 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"
46 class function_reader
;
49 /* Edges are recorded when parsing the "insn-chain" directive,
50 and created at the end when all the blocks ought to exist.
51 This struct records an "edge-from" or "edge-to" directive seen
52 at LOC, which will be turned into an actual CFG edge once
53 the "insn-chain" is fully parsed. */
57 deferred_edge (file_location loc
, int src_bb_idx
, int dest_bb_idx
, int flags
)
58 : m_loc (loc
), m_src_bb_idx (src_bb_idx
), m_dest_bb_idx (dest_bb_idx
),
68 /* Subclass of rtx_reader for reading function dumps. */
70 class function_reader
: public rtx_reader
76 /* Overridden vfuncs of class md_reader. */
77 void handle_unknown_directive (file_location
, const char *) FINAL OVERRIDE
;
79 /* Overridden vfuncs of class rtx_reader. */
80 rtx
read_rtx_operand (rtx x
, int idx
) FINAL OVERRIDE
;
81 void handle_any_trailing_information (rtx x
) FINAL OVERRIDE
;
82 rtx
postprocess (rtx
) FINAL OVERRIDE
;
83 const char *finalize_string (char *stringbuf
) FINAL OVERRIDE
;
85 rtx_insn
**get_insn_by_uid (int uid
);
86 tree
parse_mem_expr (const char *desc
);
89 void parse_function ();
90 void create_function ();
92 void parse_insn_chain ();
95 void parse_edge (basic_block block
, bool from
);
96 rtx_insn
*parse_insn (file_location loc
, const char *name
);
97 void parse_cfg (file_location loc
);
98 void parse_crtl (file_location loc
);
101 int parse_enum_value (int num_values
, const char *const *strings
);
103 void read_rtx_operand_u (rtx x
, int idx
);
104 void read_rtx_operand_i_or_n (rtx x
, int idx
, char format_char
);
105 rtx
read_rtx_operand_r (rtx x
);
106 void extra_parsing_for_operand_code_0 (rtx x
, int idx
);
108 void add_fixup_insn_uid (file_location loc
, rtx insn
, int operand_idx
,
111 void add_fixup_note_insn_basic_block (file_location loc
, rtx insn
,
112 int operand_idx
, int bb_idx
);
114 void add_fixup_source_location (file_location loc
, rtx_insn
*insn
,
115 const char *filename
, int lineno
);
117 void add_fixup_expr (file_location loc
, rtx x
,
120 rtx
consolidate_singletons (rtx x
);
122 void maybe_read_location (rtx_insn
*insn
);
124 void handle_insn_uids ();
125 void apply_fixups ();
128 struct uid_hash
: int_hash
<int, -1, -2> {};
129 hash_map
<uid_hash
, rtx_insn
*> m_insns_by_uid
;
130 auto_vec
<fixup
*> m_fixups
;
131 rtx_insn
*m_first_insn
;
132 auto_vec
<tree
> m_fake_scope
;
134 bool m_have_crtl_directive
;
135 basic_block m_bb_to_insert_after
;
136 auto_vec
<deferred_edge
> m_deferred_edges
;
137 int m_highest_bb_idx
;
140 /* Abstract base class for recording post-processing steps that must be
141 done after reading a .rtl file. */
146 /* Constructor for a fixup at LOC affecting X. */
147 fixup (file_location loc
, rtx x
)
148 : m_loc (loc
), m_rtx (x
)
152 virtual void apply (function_reader
*reader
) const = 0;
159 /* An abstract subclass of fixup for post-processing steps that
160 act on a specific operand of a specific instruction. */
162 class operand_fixup
: public fixup
165 /* Constructor for a fixup at LOC affecting INSN's operand
166 with index OPERAND_IDX. */
167 operand_fixup (file_location loc
, rtx insn
, int operand_idx
)
168 : fixup (loc
, insn
), m_operand_idx (operand_idx
)
175 /* A concrete subclass of operand_fixup: fixup an rtx_insn *
176 field based on an integer UID. */
178 class fixup_insn_uid
: public operand_fixup
181 /* Constructor for a fixup at LOC affecting INSN's operand
182 with index OPERAND_IDX. Record INSN_UID as the uid. */
183 fixup_insn_uid (file_location loc
, rtx insn
, int operand_idx
, int insn_uid
)
184 : operand_fixup (loc
, insn
, operand_idx
),
185 m_insn_uid (insn_uid
)
188 void apply (function_reader
*reader
) const;
194 /* A concrete subclass of operand_fixup: fix up a
195 NOTE_INSN_BASIC_BLOCK based on an integer block ID. */
197 class fixup_note_insn_basic_block
: public operand_fixup
200 fixup_note_insn_basic_block (file_location loc
, rtx insn
, int operand_idx
,
202 : operand_fixup (loc
, insn
, operand_idx
),
206 void apply (function_reader
*reader
) const;
212 /* A concrete subclass of fixup (not operand_fixup): fix up
213 the expr of an rtx (REG or MEM) based on a textual dump. */
215 class fixup_expr
: public fixup
218 fixup_expr (file_location loc
, rtx x
, const char *desc
)
220 m_desc (xstrdup (desc
))
223 ~fixup_expr () { free (m_desc
); }
225 void apply (function_reader
*reader
) const;
231 /* Return a textual description of the operand of INSN with
232 index OPERAND_IDX. */
235 get_operand_name (rtx insn
, int operand_idx
)
237 gcc_assert (is_a
<rtx_insn
*> (insn
));
249 /* Fixup an rtx_insn * field based on an integer UID, as read by READER. */
252 fixup_insn_uid::apply (function_reader
*reader
) const
254 rtx_insn
**insn_from_uid
= reader
->get_insn_by_uid (m_insn_uid
);
256 XEXP (m_rtx
, m_operand_idx
) = *insn_from_uid
;
259 const char *op_name
= get_operand_name (m_rtx
, m_operand_idx
);
262 "insn with UID %i not found for operand %i (`%s') of insn %i",
263 m_insn_uid
, m_operand_idx
, op_name
, INSN_UID (m_rtx
));
266 "insn with UID %i not found for operand %i of insn %i",
267 m_insn_uid
, m_operand_idx
, INSN_UID (m_rtx
));
271 /* Fix up a NOTE_INSN_BASIC_BLOCK based on an integer block ID. */
274 fixup_note_insn_basic_block::apply (function_reader
*) const
276 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, m_bb_idx
);
278 NOTE_BASIC_BLOCK (m_rtx
) = bb
;
281 /* Fix up the expr of an rtx (REG or MEM) based on a textual dump
285 fixup_expr::apply (function_reader
*reader
) const
287 tree expr
= reader
->parse_mem_expr (m_desc
);
288 switch (GET_CODE (m_rtx
))
291 set_reg_attrs_for_decl_rtl (expr
, m_rtx
);
294 set_mem_expr (m_rtx
, expr
);
301 /* Strip trailing whitespace from DESC. */
304 strip_trailing_whitespace (char *desc
)
306 char *terminator
= desc
+ strlen (desc
);
307 while (desc
< terminator
)
310 if (ISSPACE (*terminator
))
317 /* Return the numeric value n for GET_NOTE_INSN_NAME (n) for STRING,
318 or fail if STRING isn't recognized. */
321 parse_note_insn_name (const char *string
)
323 for (int i
= 0; i
< NOTE_INSN_MAX
; i
++)
324 if (0 == strcmp (string
, GET_NOTE_INSN_NAME (i
)))
326 fatal_with_file_and_line ("unrecognized NOTE_INSN name: `%s'", string
);
329 /* Return the register number for NAME, or return -1 if it isn't
333 lookup_reg_by_dump_name (const char *name
)
335 for (int i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
337 && ! strcmp (name
, reg_names
[i
]))
340 /* Also lookup virtuals. */
341 if (!strcmp (name
, "virtual-incoming-args"))
342 return VIRTUAL_INCOMING_ARGS_REGNUM
;
343 if (!strcmp (name
, "virtual-stack-vars"))
344 return VIRTUAL_STACK_VARS_REGNUM
;
345 if (!strcmp (name
, "virtual-stack-dynamic"))
346 return VIRTUAL_STACK_DYNAMIC_REGNUM
;
347 if (!strcmp (name
, "virtual-outgoing-args"))
348 return VIRTUAL_OUTGOING_ARGS_REGNUM
;
349 if (!strcmp (name
, "virtual-cfa"))
350 return VIRTUAL_CFA_REGNUM
;
351 if (!strcmp (name
, "virtual-preferred-stack-boundary"))
352 return VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM
;
353 /* TODO: handle "virtual-reg-%d". */
355 /* In compact mode, pseudos are printed with '< and '>' wrapping the regno,
356 offseting it by (LAST_VIRTUAL_REGISTER + 1), so that the
357 first non-virtual pseudo is dumped as "<0>". */
358 if (name
[0] == '<' && name
[strlen (name
) - 1] == '>')
360 int dump_num
= atoi (name
+ 1);
361 return dump_num
+ LAST_VIRTUAL_REGISTER
+ 1;
368 /* class function_reader : public rtx_reader */
370 /* function_reader's constructor. */
372 function_reader::function_reader ()
376 m_have_crtl_directive (false),
377 m_bb_to_insert_after (NULL
),
378 m_highest_bb_idx (EXIT_BLOCK
)
382 /* function_reader's destructor. */
384 function_reader::~function_reader ()
388 FOR_EACH_VEC_ELT (m_fixups
, i
, f
)
394 /* Implementation of rtx_reader::handle_unknown_directive,
395 for parsing the remainder of a directive with name NAME
398 Require a top-level "function" directive, as emitted by
399 print_rtx_function, and parse it. */
402 function_reader::handle_unknown_directive (file_location start_loc
,
405 if (strcmp (name
, "function"))
406 fatal_at (start_loc
, "expected 'function'");
411 /* Parse the output of print_rtx_function (or hand-written data in the
412 same format), having already parsed the "(function" heading, and
413 finishing immediately before the final ")".
415 The "param" and "crtl" clauses are optional. */
418 function_reader::parse_function ()
420 m_name
= xstrdup (read_string (0));
426 int c
= read_skip_spaces ();
434 file_location loc
= get_current_location ();
435 struct md_name directive
;
436 read_name (&directive
);
437 if (strcmp (directive
.string
, "param") == 0)
439 else if (strcmp (directive
.string
, "insn-chain") == 0)
441 else if (strcmp (directive
.string
, "crtl") == 0)
444 fatal_with_file_and_line ("unrecognized directive: %s",
452 /* Rebuild the JUMP_LABEL field of any JUMP_INSNs in the chain, and the
453 LABEL_NUSES of any CODE_LABELs.
455 This has to happen after apply_fixups, since only after then do
456 LABEL_REFs have their label_ref_label set up. */
457 rebuild_jump_labels (get_insns ());
459 crtl
->init_stack_alignment ();
462 /* Set up state for the function *before* fixups are applied.
464 Create "cfun" and a decl for the function.
465 By default, every function decl is hardcoded as
466 int test_1 (int i, int j, int k);
467 Set up various other state:
468 - the cfg and basic blocks (edges are created later, *after* fixups
470 - add the function to the callgraph. */
473 function_reader::create_function ()
475 /* We start in cfgrtl mode, rather than cfglayout mode. */
476 rtl_register_cfg_hooks ();
479 tree fn_name
= get_identifier (m_name
? m_name
: "test_1");
480 tree int_type
= integer_type_node
;
481 tree return_type
= int_type
;
482 tree arg_types
[3] = {int_type
, int_type
, int_type
};
483 tree fn_type
= build_function_type_array (return_type
, 3, arg_types
);
484 tree fndecl
= build_decl_stat (UNKNOWN_LOCATION
, FUNCTION_DECL
, fn_name
,
486 tree resdecl
= build_decl (UNKNOWN_LOCATION
, RESULT_DECL
, NULL_TREE
,
488 DECL_ARTIFICIAL (resdecl
) = 1;
489 DECL_IGNORED_P (resdecl
) = 1;
490 DECL_RESULT (fndecl
) = resdecl
;
491 allocate_struct_function (fndecl
, false);
492 /* This sets cfun. */
494 current_function_decl
= fndecl
;
496 cfun
->curr_properties
= (PROP_cfg
| PROP_rtl
);
498 /* Do we need this to force cgraphunit.c to output the function? */
499 DECL_EXTERNAL (fndecl
) = 0;
500 DECL_PRESERVE_P (fndecl
) = 1;
503 cgraph_node::finalize_function (fndecl
, false);
505 /* Create bare-bones cfg. This creates the entry and exit blocks. */
506 init_empty_tree_cfg_for_function (cfun
);
507 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->flags
|= BB_RTL
;
508 EXIT_BLOCK_PTR_FOR_FN (cfun
)->flags
|= BB_RTL
;
509 init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
510 init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun
));
511 m_bb_to_insert_after
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
515 /* Look within the the params of FNDECL for a param named NAME.
516 Return NULL_TREE if one isn't found. */
519 find_param_by_name (tree fndecl
, const char *name
)
521 for (tree arg
= DECL_ARGUMENTS (fndecl
); arg
; arg
= TREE_CHAIN (arg
))
522 if (strcmp (name
, IDENTIFIER_POINTER (DECL_NAME (arg
))) == 0)
527 /* Parse the content of a "param" directive, having already parsed the
528 "(param". Consume the trailing ')'. */
531 function_reader::parse_param ()
533 require_char_ws ('"');
534 file_location loc
= get_current_location ();
535 char *name
= read_quoted_string ();
537 /* Lookup param by name. */
538 tree t_param
= find_param_by_name (cfun
->decl
, name
);
540 fatal_at (loc
, "param not found: %s", name
);
542 /* Parse DECL_RTL. */
543 require_char_ws ('(');
544 require_word_ws ("DECL_RTL");
545 DECL_WRTL_CHECK (t_param
)->decl_with_rtl
.rtl
= parse_rtx ();
546 require_char_ws (')');
548 /* Parse DECL_RTL_INCOMING. */
549 require_char_ws ('(');
550 require_word_ws ("DECL_RTL_INCOMING");
551 DECL_INCOMING_RTL (t_param
) = parse_rtx ();
552 require_char_ws (')');
554 require_char_ws (')');
557 /* Parse zero or more child insn elements within an
558 "insn-chain" element. Consume the trailing ')'. */
561 function_reader::parse_insn_chain ()
565 int c
= read_skip_spaces ();
566 file_location loc
= get_current_location ();
571 struct md_name directive
;
572 read_name (&directive
);
573 if (strcmp (directive
.string
, "block") == 0)
576 parse_insn (loc
, directive
.string
);
579 fatal_at (loc
, "expected '(' or ')'");
585 /* Parse zero or more child directives (edges and insns) within a
586 "block" directive, having already parsed the "(block " heading.
587 Consume the trailing ')'. */
590 function_reader::parse_block ()
592 /* Parse the index value from the dump. This will be an integer;
593 we don't support "entry" or "exit" here (unlike for edges). */
596 int bb_idx
= atoi (name
.string
);
598 /* The term "index" has two meanings for basic blocks in a CFG:
599 (a) the "index" field within struct basic_block_def.
600 (b) the index of a basic_block within the cfg's x_basic_block_info
601 vector, as accessed via BASIC_BLOCK_FOR_FN.
603 These can get out-of-sync when basic blocks are optimized away.
604 They get back in sync by "compact_blocks".
605 We reconstruct cfun->cfg->x_basic_block_info->m_vecdata with NULL
606 values in it for any missing basic blocks, so that (a) == (b) for
607 all of the blocks we create. The doubly-linked list of basic
608 blocks (next_bb/prev_bb) skips over these "holes". */
610 if (m_highest_bb_idx
< bb_idx
)
611 m_highest_bb_idx
= bb_idx
;
613 size_t new_size
= m_highest_bb_idx
+ 1;
614 if (basic_block_info_for_fn (cfun
)->length () < new_size
)
615 vec_safe_grow_cleared (basic_block_info_for_fn (cfun
), new_size
);
617 last_basic_block_for_fn (cfun
) = new_size
;
619 /* Create the basic block.
621 We can't call create_basic_block and use the regular RTL block-creation
622 hooks, since this creates NOTE_INSN_BASIC_BLOCK instances. We don't
623 want to do that; we want to use the notes we were provided with. */
624 basic_block bb
= alloc_block ();
625 init_rtl_bb_info (bb
);
627 bb
->flags
= BB_NEW
| BB_RTL
;
628 link_block (bb
, m_bb_to_insert_after
);
629 m_bb_to_insert_after
= bb
;
631 n_basic_blocks_for_fn (cfun
)++;
632 SET_BASIC_BLOCK_FOR_FN (cfun
, bb_idx
, bb
);
633 BB_SET_PARTITION (bb
, BB_UNPARTITIONED
);
635 /* Handle insns, edge-from and edge-to directives. */
638 int c
= read_skip_spaces ();
639 file_location loc
= get_current_location ();
644 struct md_name directive
;
645 read_name (&directive
);
646 if (strcmp (directive
.string
, "edge-from") == 0)
647 parse_edge (bb
, true);
648 else if (strcmp (directive
.string
, "edge-to") == 0)
649 parse_edge (bb
, false);
652 rtx_insn
*insn
= parse_insn (loc
, directive
.string
);
653 set_block_for_insn (insn
, bb
);
660 fatal_at (loc
, "expected '(' or ')'");
664 /* Subroutine of function_reader::parse_edge.
665 Parse a basic block index, handling "entry" and "exit". */
668 function_reader::parse_bb_idx ()
672 if (strcmp (name
.string
, "entry") == 0)
674 if (strcmp (name
.string
, "exit") == 0)
676 return atoi (name
.string
);
679 /* Subroutine of parse_edge_flags.
680 Parse TOK, a token such as "FALLTHRU", converting to the flag value.
681 Issue an error if the token is unrecognized. */
684 parse_edge_flag_token (const char *tok
)
686 #define DEF_EDGE_FLAG(NAME,IDX) \
688 if (strcmp (tok, #NAME) == 0) \
689 return EDGE_##NAME; \
691 #include "cfg-flags.def"
693 error ("unrecognized edge flag: '%s'", tok
);
697 /* Subroutine of function_reader::parse_edge.
698 Parse STR and convert to a flag value (or issue an error).
699 The parser uses strtok and hence modifiers STR in-place. */
702 parse_edge_flags (char *str
)
706 char *tok
= strtok (str
, "| ");
709 result
|= parse_edge_flag_token (tok
);
710 tok
= strtok (NULL
, "| ");
716 /* Parse an "edge-from" or "edge-to" directive within the "block"
717 directive for BLOCK, having already parsed the "(edge" heading.
718 Consume the final ")". Record the edge within m_deferred_edges.
719 FROM is true for an "edge-from" directive, false for an "edge-to"
723 function_reader::parse_edge (basic_block block
, bool from
)
726 int this_bb_idx
= block
->index
;
727 file_location loc
= get_current_location ();
728 int other_bb_idx
= parse_bb_idx ();
730 /* "(edge-from 2)" means src = 2, dest = this_bb_idx, whereas
731 "(edge-to 3)" means src = this_bb_idx, dest = 3. */
732 int src_idx
= from
? other_bb_idx
: this_bb_idx
;
733 int dest_idx
= from
? this_bb_idx
: other_bb_idx
;
735 /* Optional "(flags)". */
737 int c
= read_skip_spaces ();
740 require_word_ws ("flags");
741 require_char_ws ('"');
742 char *str
= read_quoted_string ();
743 flags
= parse_edge_flags (str
);
744 require_char_ws (')');
749 require_char_ws (')');
751 /* This BB already exists, but the other BB might not yet.
752 For now, save the edges, and create them at the end of insn-chain
754 /* For now, only process the (edge-from) to this BB, and (edge-to)
755 that go to the exit block.
756 FIXME: we don't yet verify that the edge-from and edge-to directives
758 if (from
|| dest_idx
== EXIT_BLOCK
)
759 m_deferred_edges
.safe_push (deferred_edge (loc
, src_idx
, dest_idx
, flags
));
762 /* Parse an rtx instruction, having parsed the opening and parenthesis, and
763 name NAME, seen at START_LOC, by calling read_rtx_code, calling
764 set_first_insn and set_last_insn as appropriate, and
765 adding the insn to the insn chain.
766 Consume the trailing ')'. */
769 function_reader::parse_insn (file_location start_loc
, const char *name
)
771 rtx x
= read_rtx_code (name
);
773 fatal_at (start_loc
, "expected insn type; got '%s'", name
);
774 rtx_insn
*insn
= dyn_cast
<rtx_insn
*> (x
);
776 fatal_at (start_loc
, "expected insn type; got '%s'", name
);
778 /* Consume the trailing ')'. */
779 require_char_ws (')');
781 rtx_insn
*last_insn
= get_last_insn ();
783 /* Add "insn" to the insn chain. */
786 gcc_assert (NEXT_INSN (last_insn
) == NULL
);
787 SET_NEXT_INSN (last_insn
) = insn
;
789 SET_PREV_INSN (insn
) = last_insn
;
791 /* Add it to the sequence. */
792 set_last_insn (insn
);
796 set_first_insn (insn
);
799 if (rtx_code_label
*label
= dyn_cast
<rtx_code_label
*> (insn
))
800 maybe_set_max_label_num (label
);
805 /* Postprocessing subroutine for parse_insn_chain: all the basic blocks
806 should have been created by now; create the edges that were seen. */
809 function_reader::create_edges ()
813 FOR_EACH_VEC_ELT (m_deferred_edges
, i
, de
)
815 /* The BBs should already have been created by parse_block. */
816 basic_block src
= BASIC_BLOCK_FOR_FN (cfun
, de
->m_src_bb_idx
);
818 fatal_at (de
->m_loc
, "error: block index %i not found",
820 basic_block dst
= BASIC_BLOCK_FOR_FN (cfun
, de
->m_dest_bb_idx
);
822 fatal_at (de
->m_loc
, "error: block with index %i not found",
824 unchecked_make_edge (src
, dst
, de
->m_flags
);
828 /* Parse a "crtl" directive, having already parsed the "(crtl" heading
830 Consume the final ")". */
833 function_reader::parse_crtl (file_location loc
)
835 if (m_have_crtl_directive
)
836 error_at (loc
, "more than one 'crtl' directive");
837 m_have_crtl_directive
= true;
840 require_char_ws ('(');
841 require_word_ws ("return_rtx");
842 crtl
->return_rtx
= parse_rtx ();
843 require_char_ws (')');
845 require_char_ws (')');
848 /* Parse operand IDX of X, returning X, or an equivalent rtx
849 expression (for consolidating singletons).
850 This is an overridden implementation of rtx_reader::read_rtx_operand for
851 function_reader, handling various extra data printed by print_rtx,
852 and sometimes calling the base class implementation. */
855 function_reader::read_rtx_operand (rtx x
, int idx
)
857 RTX_CODE code
= GET_CODE (x
);
858 const char *format_ptr
= GET_RTX_FORMAT (code
);
859 const char format_char
= format_ptr
[idx
];
862 /* Override the regular parser for some format codes. */
866 if (idx
== 7 && CALL_P (x
))
868 m_in_call_function_usage
= true;
869 return rtx_reader::read_rtx_operand (x
, idx
);
870 m_in_call_function_usage
= false;
873 return rtx_reader::read_rtx_operand (x
, idx
);
877 read_rtx_operand_u (x
, idx
);
878 /* Don't run regular parser for 'u'. */
883 read_rtx_operand_i_or_n (x
, idx
, format_char
);
884 /* Don't run regular parser for these codes. */
888 gcc_assert (is_compact ());
889 /* Compact mode doesn't store BBs. */
890 /* Don't run regular parser. */
894 /* Don't run regular parser for 'r'. */
895 return read_rtx_operand_r (x
);
901 /* Call base class implementation. */
902 x
= rtx_reader::read_rtx_operand (x
, idx
);
904 /* Handle any additional parsing needed to handle what the dump
909 extra_parsing_for_operand_code_0 (x
, idx
);
915 /* Strip away the redundant hex dump of the value. */
916 require_char_ws ('[');
918 require_char_ws (']');
929 /* Parse operand IDX of X, of code 'u', when reading function dumps.
931 The RTL file recorded the ID of an insn (or 0 for NULL); we
932 must store this as a pointer, but the insn might not have
933 been loaded yet. Store the ID away for now, via a fixup. */
936 function_reader::read_rtx_operand_u (rtx x
, int idx
)
938 /* In compact mode, the PREV/NEXT insn uids are not dumped, so skip
939 the "uu" when reading. */
940 if (is_compact () && GET_CODE (x
) != LABEL_REF
)
944 file_location loc
= read_name (&name
);
945 int insn_id
= atoi (name
.string
);
947 add_fixup_insn_uid (loc
, x
, idx
, insn_id
);
950 /* Read a name, looking for a match against a string found in array
951 STRINGS of size NUM_VALUES.
952 Return the index of the the matched string, or emit an error. */
955 function_reader::parse_enum_value (int num_values
, const char *const *strings
)
959 for (int i
= 0; i
< num_values
; i
++)
961 if (strcmp (name
.string
, strings
[i
]) == 0)
964 error ("unrecognized enum value: '%s'", name
.string
);
968 /* Parse operand IDX of X, of code 'i' or 'n' (as specified by FORMAT_CHAR).
969 Special-cased handling of these, for reading function dumps. */
972 function_reader::read_rtx_operand_i_or_n (rtx x
, int idx
,
975 /* Handle some of the extra information that print_rtx
976 can write out for these cases. */
977 /* print_rtx only writes out operand 5 for notes
978 for NOTE_KIND values NOTE_INSN_DELETED_LABEL
979 and NOTE_INSN_DELETED_DEBUG_LABEL. */
980 if (idx
== 5 && NOTE_P (x
))
983 if (idx
== 4 && INSN_P (x
))
985 maybe_read_location (as_a
<rtx_insn
*> (x
));
989 /* INSN_CODEs aren't printed in compact mode, so don't attempt to
993 && &INSN_CODE (x
) == &XINT (x
, idx
))
999 /* Handle UNSPEC and UNSPEC_VOLATILE's operand 1. */
1000 #if !defined(GENERATOR_FILE) && NUM_UNSPECV_VALUES > 0
1002 && GET_CODE (x
) == UNSPEC_VOLATILE
)
1005 = parse_enum_value (NUM_UNSPECV_VALUES
, unspecv_strings
);
1009 #if !defined(GENERATOR_FILE) && NUM_UNSPEC_VALUES > 0
1011 && (GET_CODE (x
) == UNSPEC
1012 || GET_CODE (x
) == UNSPEC_VOLATILE
))
1015 = parse_enum_value (NUM_UNSPEC_VALUES
, unspec_strings
);
1020 struct md_name name
;
1023 if (format_char
== 'n')
1024 value
= parse_note_insn_name (name
.string
);
1026 value
= atoi (name
.string
);
1027 XINT (x
, idx
) = value
;
1030 /* Parse the 'r' operand of X, returning X, or an equivalent rtx
1031 expression (for consolidating singletons).
1032 Special-cased handling of code 'r' for reading function dumps. */
1035 function_reader::read_rtx_operand_r (rtx x
)
1037 struct md_name name
;
1038 file_location loc
= read_name (&name
);
1039 int regno
= lookup_reg_by_dump_name (name
.string
);
1041 fatal_at (loc
, "unrecognized register: '%s'", name
.string
);
1043 set_regno_raw (x
, regno
, 1);
1045 /* Consolidate singletons. */
1046 x
= consolidate_singletons (x
);
1048 ORIGINAL_REGNO (x
) = regno
;
1050 /* Parse extra stuff at end of 'r'.
1051 We may have zero, one, or two sections marked by square
1053 int ch
= read_skip_spaces ();
1054 bool expect_original_regno
= false;
1057 file_location loc
= get_current_location ();
1058 char *desc
= read_until ("]", true);
1059 strip_trailing_whitespace (desc
);
1060 const char *desc_start
= desc
;
1061 /* If ORIGINAL_REGNO (rtx) != regno, we will have:
1062 "orig:%i", ORIGINAL_REGNO (rtx).
1063 Consume it, we don't set ORIGINAL_REGNO, since we can
1064 get that from the 2nd copy later. */
1065 if (0 == strncmp (desc
, "orig:", 5))
1067 expect_original_regno
= true;
1069 /* Skip to any whitespace following the integer. */
1070 const char *space
= strchr (desc_start
, ' ');
1072 desc_start
= space
+ 1;
1074 /* Any remaining text may be the REG_EXPR. Alternatively we have
1075 no REG_ATTRS, and instead we have ORIGINAL_REGNO. */
1076 if (ISDIGIT (*desc_start
))
1078 /* Assume we have ORIGINAL_REGNO. */
1079 ORIGINAL_REGNO (x
) = atoi (desc_start
);
1083 /* Assume we have REG_EXPR. */
1084 add_fixup_expr (loc
, x
, desc_start
);
1090 if (expect_original_regno
)
1092 require_char_ws ('[');
1093 char *desc
= read_until ("]", true);
1094 ORIGINAL_REGNO (x
) = atoi (desc
);
1101 /* Additional parsing for format code '0' in dumps, handling a variety
1102 of special-cases in print_rtx, when parsing operand IDX of X. */
1105 function_reader::extra_parsing_for_operand_code_0 (rtx x
, int idx
)
1107 RTX_CODE code
= GET_CODE (x
);
1109 struct md_name name
;
1111 if (idx
== 1 && code
== SYMBOL_REF
)
1113 /* Possibly wrote " [flags %#x]", SYMBOL_REF_FLAGS (in_rtx). */
1114 c
= read_skip_spaces ();
1117 file_location loc
= read_name (&name
);
1118 if (strcmp (name
.string
, "flags"))
1119 error_at (loc
, "was expecting `%s'", "flags");
1121 SYMBOL_REF_FLAGS (x
) = strtol (name
.string
, NULL
, 16);
1123 /* We can't reconstruct SYMBOL_REF_BLOCK; set it to NULL. */
1124 if (SYMBOL_REF_HAS_BLOCK_INFO_P (x
))
1125 SYMBOL_REF_BLOCK (x
) = NULL
;
1132 /* If X had a non-NULL SYMBOL_REF_DECL,
1133 rtx_writer::print_rtx_operand_code_0 would have dumped it
1134 using print_node_brief.
1135 Skip the content for now. */
1136 c
= read_skip_spaces ();
1141 char ch
= read_char ();
1149 else if (idx
== 3 && code
== NOTE
)
1151 /* Note-specific data appears for operand 3, which annoyingly
1152 is before the enum specifying which kind of note we have
1154 c
= read_skip_spaces ();
1157 /* Possibly data for a NOTE_INSN_BASIC_BLOCK, of the form:
1159 file_location bb_loc
= read_name (&name
);
1160 if (strcmp (name
.string
, "bb"))
1161 error_at (bb_loc
, "was expecting `%s'", "bb");
1163 int bb_idx
= atoi (name
.string
);
1164 add_fixup_note_insn_basic_block (bb_loc
, x
, idx
,
1166 require_char_ws (']');
1173 /* Implementation of rtx_reader::handle_any_trailing_information.
1174 Handle the various additional information that print-rtl.c can
1175 write after the regular fields, when parsing X. */
1178 function_reader::handle_any_trailing_information (rtx x
)
1180 struct md_name name
;
1182 switch (GET_CODE (x
))
1187 require_char_ws ('[');
1189 set_mem_alias_set (x
, atoi (name
.string
));
1190 /* We have either a MEM_EXPR, or a space. */
1191 if (peek_char () != ' ')
1193 file_location loc
= get_current_location ();
1194 char *desc
= read_until (" +", false);
1195 add_fixup_expr (loc
, consolidate_singletons (x
), desc
);
1201 /* We may optionally have '+' for MEM_OFFSET_KNOWN_P. */
1202 ch
= read_skip_spaces ();
1206 set_mem_offset (x
, atoi (name
.string
));
1211 /* Handle optional " S" for MEM_SIZE. */
1212 ch
= read_skip_spaces ();
1216 set_mem_size (x
, atoi (name
.string
));
1221 /* Handle optional " A" for MEM_ALIGN. */
1222 ch
= read_skip_spaces ();
1223 if (ch
== 'A' && peek_char () != 'S')
1226 set_mem_align (x
, atoi (name
.string
));
1231 /* Handle optional " AS" for MEM_ADDR_SPACE. */
1232 ch
= read_skip_spaces ();
1233 if (ch
== 'A' && peek_char () == 'S')
1237 set_mem_addr_space (x
, atoi (name
.string
));
1247 /* Assume that LABEL_NUSES was not dumped. */
1248 /* TODO: parse LABEL_KIND. */
1249 /* For now, skip until closing ')'. */
1252 char ch
= read_char ();
1267 /* Parse a tree dump for a MEM_EXPR in DESC and turn it back into a tree.
1268 We handle "<retval>" and param names within cfun, but for anything else
1269 we "cheat" by building a global VAR_DECL of type "int" with that name
1270 (returning the same global for a name if we see the same name more
1274 function_reader::parse_mem_expr (const char *desc
)
1276 tree fndecl
= cfun
->decl
;
1278 if (0 == strcmp (desc
, "<retval>"))
1279 return DECL_RESULT (fndecl
);
1281 tree param
= find_param_by_name (fndecl
, desc
);
1285 /* Search within decls we already created.
1286 FIXME: use a hash rather than linear search. */
1289 FOR_EACH_VEC_ELT (m_fake_scope
, i
, t
)
1290 if (strcmp (desc
, IDENTIFIER_POINTER (DECL_NAME (t
))) == 0)
1293 /* Not found? Create it.
1294 This allows mimicking of real data but avoids having to specify
1295 e.g. names of locals, params etc.
1296 Though this way we don't know if we have a PARM_DECL vs a VAR_DECL,
1297 and we don't know the types. Fake it by making everything be
1298 a VAR_DECL of "int" type. */
1299 t
= build_decl (UNKNOWN_LOCATION
, VAR_DECL
,
1300 get_identifier (desc
),
1302 m_fake_scope
.safe_push (t
);
1306 /* Record that at LOC we saw an insn uid INSN_UID for the operand with index
1307 OPERAND_IDX within INSN, so that the pointer value can be fixed up in
1308 later post-processing. */
1311 function_reader::add_fixup_insn_uid (file_location loc
, rtx insn
, int operand_idx
,
1314 m_fixups
.safe_push (new fixup_insn_uid (loc
, insn
, operand_idx
, insn_uid
));
1317 /* Record that at LOC we saw an basic block index BB_IDX for the operand with index
1318 OPERAND_IDX within INSN, so that the pointer value can be fixed up in
1319 later post-processing. */
1322 function_reader::add_fixup_note_insn_basic_block (file_location loc
, rtx insn
,
1323 int operand_idx
, int bb_idx
)
1325 m_fixups
.safe_push (new fixup_note_insn_basic_block (loc
, insn
, operand_idx
,
1329 /* Placeholder hook for recording source location information seen in a dump.
1330 This is empty for now. */
1333 function_reader::add_fixup_source_location (file_location
, rtx_insn
*,
1338 /* Record that at LOC we saw textual description DESC of the MEM_EXPR or REG_EXPR
1339 of INSN, so that the fields can be fixed up in later post-processing. */
1342 function_reader::add_fixup_expr (file_location loc
, rtx insn
,
1346 /* Fail early if the RTL reader erroneously hands us an int. */
1347 gcc_assert (!ISDIGIT (desc
[0]));
1349 m_fixups
.safe_push (new fixup_expr (loc
, insn
, desc
));
1352 /* Helper function for consolidate_reg. Return the global rtx for
1353 the register with regno REGNO. */
1356 lookup_global_register (int regno
)
1358 /* We can't use a switch here, as some of the REGNUMs might not be constants
1359 for some targets. */
1360 if (regno
== STACK_POINTER_REGNUM
)
1361 return stack_pointer_rtx
;
1362 else if (regno
== FRAME_POINTER_REGNUM
)
1363 return frame_pointer_rtx
;
1364 else if (regno
== HARD_FRAME_POINTER_REGNUM
)
1365 return hard_frame_pointer_rtx
;
1366 else if (regno
== ARG_POINTER_REGNUM
)
1367 return arg_pointer_rtx
;
1368 else if (regno
== VIRTUAL_INCOMING_ARGS_REGNUM
)
1369 return virtual_incoming_args_rtx
;
1370 else if (regno
== VIRTUAL_STACK_VARS_REGNUM
)
1371 return virtual_stack_vars_rtx
;
1372 else if (regno
== VIRTUAL_STACK_DYNAMIC_REGNUM
)
1373 return virtual_stack_dynamic_rtx
;
1374 else if (regno
== VIRTUAL_OUTGOING_ARGS_REGNUM
)
1375 return virtual_outgoing_args_rtx
;
1376 else if (regno
== VIRTUAL_CFA_REGNUM
)
1377 return virtual_cfa_rtx
;
1378 else if (regno
== VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM
)
1379 return virtual_preferred_stack_boundary_rtx
;
1380 #ifdef return_ADDRESS_POINTER_REGNUM
1381 else if (regno
== RETURN_ADDRESS_POINTER_REGNUM
)
1382 return return_address_pointer_rtx
;
1388 /* Ensure that the backend can cope with a REG with regno REGNO.
1389 Normally REG instances are created by gen_reg_rtx which updates
1390 regno_reg_rtx, growing it as necessary.
1391 The REG instances created from the dumpfile weren't created this
1392 way, so we need to manually update regno_reg_rtx. */
1395 ensure_regno (int regno
)
1397 if (reg_rtx_no
< regno
+ 1)
1398 reg_rtx_no
= regno
+ 1;
1400 crtl
->emit
.ensure_regno_capacity ();
1401 gcc_assert (regno
< crtl
->emit
.regno_pointer_align_length
);
1404 /* Helper function for consolidate_singletons, for handling REG instances.
1405 Given REG instance X of some regno, return the singleton rtx for that
1406 regno, if it exists, or X. */
1409 consolidate_reg (rtx x
)
1411 gcc_assert (GET_CODE (x
) == REG
);
1413 unsigned int regno
= REGNO (x
);
1415 ensure_regno (regno
);
1417 /* Some register numbers have their rtx created in init_emit_regs
1418 e.g. stack_pointer_rtx for STACK_POINTER_REGNUM.
1419 Consolidate on this. */
1420 rtx global_reg
= lookup_global_register (regno
);
1424 /* Populate regno_reg_rtx if necessary. */
1425 if (regno_reg_rtx
[regno
] == NULL
)
1426 regno_reg_rtx
[regno
] = x
;
1428 gcc_assert (GET_CODE (regno_reg_rtx
[regno
]) == REG
);
1429 gcc_assert (REGNO (regno_reg_rtx
[regno
]) == regno
);
1430 if (GET_MODE (x
) == GET_MODE (regno_reg_rtx
[regno
]))
1431 return regno_reg_rtx
[regno
];
1436 /* When reading RTL function dumps, we must consolidate some
1437 rtx so that we use singletons where singletons are expected
1438 (e.g. we don't want multiple "(const_int 0 [0])" rtx, since
1439 these are tested via pointer equality against const0_rtx.
1441 Return the equivalent singleton rtx for X, if any, otherwise X. */
1444 function_reader::consolidate_singletons (rtx x
)
1449 switch (GET_CODE (x
))
1451 case PC
: return pc_rtx
;
1452 case RETURN
: return ret_rtx
;
1453 case SIMPLE_RETURN
: return simple_return_rtx
;
1454 case CC0
: return cc0_rtx
;
1457 return consolidate_reg (x
);
1460 return gen_rtx_CONST_INT (GET_MODE (x
), INTVAL (x
));
1469 /* Parse an rtx directive, including both the opening/closing parentheses,
1473 function_reader::parse_rtx ()
1475 require_char_ws ('(');
1476 struct md_name directive
;
1477 read_name (&directive
);
1479 = consolidate_singletons (read_rtx_code (directive
.string
));
1480 require_char_ws (')');
1485 /* Implementation of rtx_reader::postprocess for reading function dumps.
1486 Return the equivalent singleton rtx for X, if any, otherwise X. */
1489 function_reader::postprocess (rtx x
)
1491 return consolidate_singletons (x
);
1494 /* Implementation of rtx_reader::finalize_string for reading function dumps.
1495 Make a GC-managed copy of STRINGBUF. */
1498 function_reader::finalize_string (char *stringbuf
)
1500 return ggc_strdup (stringbuf
);
1503 /* Attempt to parse optional location information for insn INSN, as
1504 potentially written out by rtx_writer::print_rtx_operand_code_i.
1505 We look for a quoted string followed by a colon. */
1508 function_reader::maybe_read_location (rtx_insn
*insn
)
1510 file_location loc
= get_current_location ();
1512 /* Attempt to parse a quoted string. */
1513 int ch
= read_skip_spaces ();
1516 char *filename
= read_quoted_string ();
1518 struct md_name line_num
;
1519 read_name (&line_num
);
1520 add_fixup_source_location (loc
, insn
, filename
, atoi (line_num
.string
));
1526 /* Postprocessing subroutine of function_reader::parse_function.
1527 Populate m_insns_by_uid. */
1530 function_reader::handle_insn_uids ()
1532 /* Locate the currently assigned INSN_UID values, storing
1533 them in m_insns_by_uid. */
1535 for (rtx_insn
*insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
1537 if (m_insns_by_uid
.get (INSN_UID (insn
)))
1538 error ("duplicate insn UID: %i", INSN_UID (insn
));
1539 m_insns_by_uid
.put (INSN_UID (insn
), insn
);
1540 if (INSN_UID (insn
) > max_uid
)
1541 max_uid
= INSN_UID (insn
);
1544 /* Ensure x_cur_insn_uid is 1 more than the biggest insn UID seen.
1545 This is normally updated by the various make_*insn_raw functions. */
1546 crtl
->emit
.x_cur_insn_uid
= max_uid
+ 1;
1549 /* Apply all of the recorded fixups. */
1552 function_reader::apply_fixups ()
1556 FOR_EACH_VEC_ELT (m_fixups
, i
, f
)
1560 /* Given a UID value, try to locate a pointer to the corresponding
1561 rtx_insn *, or NULL if if can't be found. */
1564 function_reader::get_insn_by_uid (int uid
)
1566 return m_insns_by_uid
.get (uid
);
1569 /* Run the RTL dump parser, parsing a dump located at PATH.
1570 Return true iff the file was successfully parsed. */
1573 read_rtl_function_body (const char *path
)
1577 init_varasm_status ();
1579 function_reader reader
;
1580 if (!reader
.read_file (path
))
1588 namespace selftest
{
1590 /* Verify that parse_edge_flags works. */
1595 /* parse_edge_flags modifies its input (due to strtok), so we must make
1596 a copy of the literals. */
1597 #define ASSERT_PARSE_EDGE_FLAGS(EXPECTED, STR) \
1599 char *str = xstrdup (STR); \
1600 ASSERT_EQ (EXPECTED, parse_edge_flags (str)); \
1604 ASSERT_PARSE_EDGE_FLAGS (0, "");
1605 ASSERT_PARSE_EDGE_FLAGS (EDGE_FALLTHRU
, "FALLTHRU");
1606 ASSERT_PARSE_EDGE_FLAGS (EDGE_ABNORMAL_CALL
, "ABNORMAL_CALL");
1607 ASSERT_PARSE_EDGE_FLAGS (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
,
1608 "ABNORMAL | ABNORMAL_CALL");
1610 #undef ASSERT_PARSE_EDGE_FLAGS
1613 /* Verify that lookup_reg_by_dump_name works. */
1616 test_parsing_regnos ()
1618 ASSERT_EQ (-1, lookup_reg_by_dump_name ("this is not a register"));
1620 /* Verify lookup of virtual registers. */
1621 ASSERT_EQ (VIRTUAL_INCOMING_ARGS_REGNUM
,
1622 lookup_reg_by_dump_name ("virtual-incoming-args"));
1623 ASSERT_EQ (VIRTUAL_STACK_VARS_REGNUM
,
1624 lookup_reg_by_dump_name ("virtual-stack-vars"));
1625 ASSERT_EQ (VIRTUAL_STACK_DYNAMIC_REGNUM
,
1626 lookup_reg_by_dump_name ("virtual-stack-dynamic"));
1627 ASSERT_EQ (VIRTUAL_OUTGOING_ARGS_REGNUM
,
1628 lookup_reg_by_dump_name ("virtual-outgoing-args"));
1629 ASSERT_EQ (VIRTUAL_CFA_REGNUM
,
1630 lookup_reg_by_dump_name ("virtual-cfa"));
1631 ASSERT_EQ (VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM
,
1632 lookup_reg_by_dump_name ("virtual-preferred-stack-boundary"));
1634 /* Verify lookup of non-virtual pseudos. */
1635 ASSERT_EQ (LAST_VIRTUAL_REGISTER
+ 1, lookup_reg_by_dump_name ("<0>"));
1636 ASSERT_EQ (LAST_VIRTUAL_REGISTER
+ 2, lookup_reg_by_dump_name ("<1>"));
1639 /* Verify that edge E is as expected, with the src and dest basic blocks
1640 having indices EXPECTED_SRC_IDX and EXPECTED_DEST_IDX respectively, and
1641 the edge having flags equal to EXPECTED_FLAGS.
1642 Use LOC as the effective location when reporting failures. */
1645 assert_edge_at (const location
&loc
, edge e
, int expected_src_idx
,
1646 int expected_dest_idx
, int expected_flags
)
1648 ASSERT_EQ_AT (loc
, expected_src_idx
, e
->src
->index
);
1649 ASSERT_EQ_AT (loc
, expected_dest_idx
, e
->dest
->index
);
1650 ASSERT_EQ_AT (loc
, expected_flags
, e
->flags
);
1653 /* Verify that edge EDGE is as expected, with the src and dest basic blocks
1654 having indices EXPECTED_SRC_IDX and EXPECTED_DEST_IDX respectively, and
1655 the edge having flags equal to EXPECTED_FLAGS. */
1657 #define ASSERT_EDGE(EDGE, EXPECTED_SRC_IDX, EXPECTED_DEST_IDX, \
1659 assert_edge_at (SELFTEST_LOCATION, EDGE, EXPECTED_SRC_IDX, \
1660 EXPECTED_DEST_IDX, EXPECTED_FLAGS)
1662 /* Verify that we can load RTL dumps. */
1665 test_loading_dump_fragment_1 ()
1667 // TODO: filter on target?
1668 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("asr_div1.rtl"));
1670 /* Verify that the insns were loaded correctly. */
1671 rtx_insn
*insn_1
= get_insns ();
1672 ASSERT_TRUE (insn_1
);
1673 ASSERT_EQ (1, INSN_UID (insn_1
));
1674 ASSERT_EQ (INSN
, GET_CODE (insn_1
));
1675 ASSERT_EQ (SET
, GET_CODE (PATTERN (insn_1
)));
1676 ASSERT_EQ (NULL
, PREV_INSN (insn_1
));
1678 rtx_insn
*insn_2
= NEXT_INSN (insn_1
);
1679 ASSERT_TRUE (insn_2
);
1680 ASSERT_EQ (2, INSN_UID (insn_2
));
1681 ASSERT_EQ (INSN
, GET_CODE (insn_2
));
1682 ASSERT_EQ (insn_1
, PREV_INSN (insn_2
));
1683 ASSERT_EQ (NULL
, NEXT_INSN (insn_2
));
1685 /* Verify that registers were loaded correctly. */
1686 rtx insn_1_dest
= SET_DEST (PATTERN (insn_1
));
1687 ASSERT_EQ (REG
, GET_CODE (insn_1_dest
));
1688 ASSERT_EQ ((LAST_VIRTUAL_REGISTER
+ 1) + 2, REGNO (insn_1_dest
));
1689 rtx insn_1_src
= SET_SRC (PATTERN (insn_1
));
1690 ASSERT_EQ (LSHIFTRT
, GET_CODE (insn_1_src
));
1691 rtx reg
= XEXP (insn_1_src
, 0);
1692 ASSERT_EQ (REG
, GET_CODE (reg
));
1693 ASSERT_EQ (LAST_VIRTUAL_REGISTER
+ 1, REGNO (reg
));
1695 /* Verify that get_insn_by_uid works. */
1696 ASSERT_EQ (insn_1
, get_insn_by_uid (1));
1697 ASSERT_EQ (insn_2
, get_insn_by_uid (2));
1699 /* Verify that basic blocks were created. */
1700 ASSERT_EQ (2, BLOCK_FOR_INSN (insn_1
)->index
);
1701 ASSERT_EQ (2, BLOCK_FOR_INSN (insn_2
)->index
);
1703 /* Verify that the CFG was recreated. */
1705 verify_three_block_rtl_cfg (cfun
);
1706 basic_block bb2
= BASIC_BLOCK_FOR_FN (cfun
, 2);
1707 ASSERT_TRUE (bb2
!= NULL
);
1708 ASSERT_EQ (BB_RTL
, bb2
->flags
& BB_RTL
);
1709 ASSERT_EQ (2, bb2
->index
);
1710 ASSERT_EQ (insn_1
, BB_HEAD (bb2
));
1711 ASSERT_EQ (insn_2
, BB_END (bb2
));
1714 /* Verify loading another RTL dump. */
1717 test_loading_dump_fragment_2 ()
1719 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("simple-cse.rtl"));
1721 rtx_insn
*insn_1
= get_insn_by_uid (1);
1722 rtx_insn
*insn_2
= get_insn_by_uid (2);
1723 rtx_insn
*insn_3
= get_insn_by_uid (3);
1725 rtx set1
= single_set (insn_1
);
1726 ASSERT_NE (NULL
, set1
);
1727 rtx set2
= single_set (insn_2
);
1728 ASSERT_NE (NULL
, set2
);
1729 rtx set3
= single_set (insn_3
);
1730 ASSERT_NE (NULL
, set3
);
1732 rtx src1
= SET_SRC (set1
);
1733 ASSERT_EQ (PLUS
, GET_CODE (src1
));
1735 rtx src2
= SET_SRC (set2
);
1736 ASSERT_EQ (PLUS
, GET_CODE (src2
));
1738 /* Both src1 and src2 refer to "(reg:SI %0)".
1739 Verify that we have pointer equality. */
1740 rtx lhs1
= XEXP (src1
, 0);
1741 rtx lhs2
= XEXP (src2
, 0);
1742 ASSERT_EQ (lhs1
, lhs2
);
1744 /* Verify that the CFG was recreated. */
1746 verify_three_block_rtl_cfg (cfun
);
1749 /* Verify that CODE_LABEL insns are loaded correctly. */
1752 test_loading_labels ()
1754 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("example-labels.rtl"));
1756 rtx_insn
*insn_100
= get_insn_by_uid (100);
1757 ASSERT_EQ (CODE_LABEL
, GET_CODE (insn_100
));
1758 ASSERT_EQ (100, INSN_UID (insn_100
));
1759 ASSERT_EQ (NULL
, LABEL_NAME (insn_100
));
1760 ASSERT_EQ (0, LABEL_NUSES (insn_100
));
1761 ASSERT_EQ (30, CODE_LABEL_NUMBER (insn_100
));
1763 rtx_insn
*insn_200
= get_insn_by_uid (200);
1764 ASSERT_EQ (CODE_LABEL
, GET_CODE (insn_200
));
1765 ASSERT_EQ (200, INSN_UID (insn_200
));
1766 ASSERT_STREQ ("some_label_name", LABEL_NAME (insn_200
));
1767 ASSERT_EQ (0, LABEL_NUSES (insn_200
));
1768 ASSERT_EQ (40, CODE_LABEL_NUMBER (insn_200
));
1770 /* Ensure that the presence of CODE_LABEL_NUMBER == 40
1771 means that the next label num to be handed out will be 41. */
1772 ASSERT_EQ (41, max_label_num ());
1774 /* Ensure that label names read from a dump are GC-managed
1775 and are found through the insn. */
1776 forcibly_ggc_collect ();
1777 ASSERT_TRUE (ggc_marked_p (insn_200
));
1778 ASSERT_TRUE (ggc_marked_p (LABEL_NAME (insn_200
)));
1781 /* Verify that the loader copes with an insn with a mode. */
1784 test_loading_insn_with_mode ()
1786 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("insn-with-mode.rtl"));
1787 rtx_insn
*insn
= get_insns ();
1788 ASSERT_EQ (INSN
, GET_CODE (insn
));
1790 /* Verify that the "TI" mode was set from "insn:TI". */
1791 ASSERT_EQ (TImode
, GET_MODE (insn
));
1794 /* Verify that the loader copes with a jump_insn to a label_ref. */
1797 test_loading_jump_to_label_ref ()
1799 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("jump-to-label-ref.rtl"));
1801 rtx_insn
*jump_insn
= get_insn_by_uid (1);
1802 ASSERT_EQ (JUMP_INSN
, GET_CODE (jump_insn
));
1804 rtx_insn
*barrier
= get_insn_by_uid (2);
1805 ASSERT_EQ (BARRIER
, GET_CODE (barrier
));
1807 rtx_insn
*code_label
= get_insn_by_uid (100);
1808 ASSERT_EQ (CODE_LABEL
, GET_CODE (code_label
));
1810 /* Verify the jump_insn. */
1811 ASSERT_EQ (4, BLOCK_FOR_INSN (jump_insn
)->index
);
1812 ASSERT_EQ (SET
, GET_CODE (PATTERN (jump_insn
)));
1813 /* Ensure that the "(pc)" is using the global singleton. */
1814 ASSERT_RTX_PTR_EQ (pc_rtx
, SET_DEST (PATTERN (jump_insn
)));
1815 rtx label_ref
= SET_SRC (PATTERN (jump_insn
));
1816 ASSERT_EQ (LABEL_REF
, GET_CODE (label_ref
));
1817 ASSERT_EQ (code_label
, label_ref_label (label_ref
));
1818 ASSERT_EQ (code_label
, JUMP_LABEL (jump_insn
));
1820 /* Verify the code_label. */
1821 ASSERT_EQ (5, BLOCK_FOR_INSN (code_label
)->index
);
1822 ASSERT_EQ (NULL
, LABEL_NAME (code_label
));
1823 ASSERT_EQ (1, LABEL_NUSES (code_label
));
1825 /* Verify the generated CFG. */
1827 /* Locate blocks. */
1828 basic_block entry
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
1829 ASSERT_TRUE (entry
!= NULL
);
1830 ASSERT_EQ (ENTRY_BLOCK
, entry
->index
);
1832 basic_block exit
= EXIT_BLOCK_PTR_FOR_FN (cfun
);
1833 ASSERT_TRUE (exit
!= NULL
);
1834 ASSERT_EQ (EXIT_BLOCK
, exit
->index
);
1836 basic_block bb4
= (*cfun
->cfg
->x_basic_block_info
)[4];
1837 basic_block bb5
= (*cfun
->cfg
->x_basic_block_info
)[5];
1838 ASSERT_EQ (4, bb4
->index
);
1839 ASSERT_EQ (5, bb5
->index
);
1842 ASSERT_EQ (NULL
, entry
->preds
);
1843 ASSERT_EQ (1, entry
->succs
->length ());
1844 ASSERT_EDGE ((*entry
->succs
)[0], 0, 4, EDGE_FALLTHRU
);
1847 ASSERT_EQ (1, bb4
->preds
->length ());
1848 ASSERT_EDGE ((*bb4
->preds
)[0], 0, 4, EDGE_FALLTHRU
);
1849 ASSERT_EQ (1, bb4
->succs
->length ());
1850 ASSERT_EDGE ((*bb4
->succs
)[0], 4, 5, 0x0);
1853 ASSERT_EQ (1, bb5
->preds
->length ());
1854 ASSERT_EDGE ((*bb5
->preds
)[0], 4, 5, 0x0);
1855 ASSERT_EQ (1, bb5
->succs
->length ());
1856 ASSERT_EDGE ((*bb5
->succs
)[0], 5, 1, EDGE_FALLTHRU
);
1859 ASSERT_EQ (1, exit
->preds
->length ());
1860 ASSERT_EDGE ((*exit
->preds
)[0], 5, 1, EDGE_FALLTHRU
);
1861 ASSERT_EQ (NULL
, exit
->succs
);
1864 /* Verify that the loader copes with a jump_insn to a label_ref
1868 test_loading_jump_to_return ()
1870 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("jump-to-return.rtl"));
1872 rtx_insn
*jump_insn
= get_insn_by_uid (1);
1873 ASSERT_EQ (JUMP_INSN
, GET_CODE (jump_insn
));
1874 ASSERT_RTX_PTR_EQ (ret_rtx
, JUMP_LABEL (jump_insn
));
1877 /* Verify that the loader copes with a jump_insn to a label_ref
1878 marked "simple_return". */
1881 test_loading_jump_to_simple_return ()
1883 rtl_dump_test
t (SELFTEST_LOCATION
,
1884 locate_file ("jump-to-simple-return.rtl"));
1886 rtx_insn
*jump_insn
= get_insn_by_uid (1);
1887 ASSERT_EQ (JUMP_INSN
, GET_CODE (jump_insn
));
1888 ASSERT_RTX_PTR_EQ (simple_return_rtx
, JUMP_LABEL (jump_insn
));
1891 /* Verify that the loader copes with a NOTE_INSN_BASIC_BLOCK. */
1894 test_loading_note_insn_basic_block ()
1896 rtl_dump_test
t (SELFTEST_LOCATION
,
1897 locate_file ("note_insn_basic_block.rtl"));
1899 rtx_insn
*note
= get_insn_by_uid (1);
1900 ASSERT_EQ (NOTE
, GET_CODE (note
));
1901 ASSERT_EQ (2, BLOCK_FOR_INSN (note
)->index
);
1903 ASSERT_EQ (NOTE_INSN_BASIC_BLOCK
, NOTE_KIND (note
));
1904 ASSERT_EQ (2, NOTE_BASIC_BLOCK (note
)->index
);
1905 ASSERT_EQ (BASIC_BLOCK_FOR_FN (cfun
, 2), NOTE_BASIC_BLOCK (note
));
1908 /* Verify that the loader copes with a NOTE_INSN_DELETED. */
1911 test_loading_note_insn_deleted ()
1913 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("note-insn-deleted.rtl"));
1915 rtx_insn
*note
= get_insn_by_uid (1);
1916 ASSERT_EQ (NOTE
, GET_CODE (note
));
1917 ASSERT_EQ (NOTE_INSN_DELETED
, NOTE_KIND (note
));
1920 /* Verify that the const_int values are consolidated, since
1921 pointer equality corresponds to value equality.
1922 TODO: do this for all in CASE_CONST_UNIQUE. */
1925 test_loading_const_int ()
1927 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("const-int.rtl"));
1929 /* Verify that const_int values below MAX_SAVED_CONST_INT use
1930 the global values. */
1931 ASSERT_EQ (const0_rtx
, SET_SRC (PATTERN (get_insn_by_uid (1))));
1932 ASSERT_EQ (const1_rtx
, SET_SRC (PATTERN (get_insn_by_uid (2))));
1933 ASSERT_EQ (constm1_rtx
, SET_SRC (PATTERN (get_insn_by_uid (3))));
1935 /* Verify that other const_int values are consolidated. */
1936 rtx int256
= gen_rtx_CONST_INT (SImode
, 256);
1937 ASSERT_EQ (int256
, SET_SRC (PATTERN (get_insn_by_uid (4))));
1940 /* Verify that the loader copes with a SYMBOL_REF. */
1943 test_loading_symbol_ref ()
1945 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("symbol-ref.rtl"));
1947 rtx_insn
*insn
= get_insns ();
1949 rtx high
= SET_SRC (PATTERN (insn
));
1950 ASSERT_EQ (HIGH
, GET_CODE (high
));
1952 rtx symbol_ref
= XEXP (high
, 0);
1953 ASSERT_EQ (SYMBOL_REF
, GET_CODE (symbol_ref
));
1955 /* Verify that "[flags 0xc0]" was parsed. */
1956 ASSERT_EQ (0xc0, SYMBOL_REF_FLAGS (symbol_ref
));
1957 /* TODO: we don't yet load SYMBOL_REF_DECL. */
1960 /* Verify that the loader can rebuild a CFG. */
1965 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("cfg-test.rtl"));
1967 ASSERT_STREQ ("cfg_test", IDENTIFIER_POINTER (DECL_NAME (cfun
->decl
)));
1971 ASSERT_TRUE (cfun
->cfg
!= NULL
);
1972 ASSERT_EQ (6, n_basic_blocks_for_fn (cfun
));
1973 ASSERT_EQ (6, n_edges_for_fn (cfun
));
1975 /* The "fake" basic blocks. */
1976 basic_block entry
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
1977 ASSERT_TRUE (entry
!= NULL
);
1978 ASSERT_EQ (ENTRY_BLOCK
, entry
->index
);
1980 basic_block exit
= EXIT_BLOCK_PTR_FOR_FN (cfun
);
1981 ASSERT_TRUE (exit
!= NULL
);
1982 ASSERT_EQ (EXIT_BLOCK
, exit
->index
);
1984 /* The "real" basic blocks. */
1985 basic_block bb2
= (*cfun
->cfg
->x_basic_block_info
)[2];
1986 basic_block bb3
= (*cfun
->cfg
->x_basic_block_info
)[3];
1987 basic_block bb4
= (*cfun
->cfg
->x_basic_block_info
)[4];
1988 basic_block bb5
= (*cfun
->cfg
->x_basic_block_info
)[5];
1990 ASSERT_EQ (2, bb2
->index
);
1991 ASSERT_EQ (3, bb3
->index
);
1992 ASSERT_EQ (4, bb4
->index
);
1993 ASSERT_EQ (5, bb5
->index
);
1995 /* Verify connectivity. */
1998 ASSERT_EQ (NULL
, entry
->preds
);
1999 ASSERT_EQ (1, entry
->succs
->length ());
2000 ASSERT_EDGE ((*entry
->succs
)[0], 0, 2, EDGE_FALLTHRU
);
2003 ASSERT_EQ (1, bb2
->preds
->length ());
2004 ASSERT_EDGE ((*bb2
->preds
)[0], 0, 2, EDGE_FALLTHRU
);
2005 ASSERT_EQ (2, bb2
->succs
->length ());
2006 ASSERT_EDGE ((*bb2
->succs
)[0], 2, 3, EDGE_TRUE_VALUE
);
2007 ASSERT_EDGE ((*bb2
->succs
)[1], 2, 4, EDGE_FALSE_VALUE
);
2010 ASSERT_EQ (1, bb3
->preds
->length ());
2011 ASSERT_EDGE ((*bb3
->preds
)[0], 2, 3, EDGE_TRUE_VALUE
);
2012 ASSERT_EQ (1, bb3
->succs
->length ());
2013 ASSERT_EDGE ((*bb3
->succs
)[0], 3, 5, EDGE_FALLTHRU
);
2016 ASSERT_EQ (1, bb4
->preds
->length ());
2017 ASSERT_EDGE ((*bb4
->preds
)[0], 2, 4, EDGE_FALSE_VALUE
);
2018 ASSERT_EQ (1, bb4
->succs
->length ());
2019 ASSERT_EDGE ((*bb4
->succs
)[0], 4, 5, EDGE_FALLTHRU
);
2022 ASSERT_EQ (2, bb5
->preds
->length ());
2023 ASSERT_EDGE ((*bb5
->preds
)[0], 3, 5, EDGE_FALLTHRU
);
2024 ASSERT_EDGE ((*bb5
->preds
)[1], 4, 5, EDGE_FALLTHRU
);
2025 ASSERT_EQ (1, bb5
->succs
->length ());
2026 ASSERT_EDGE ((*bb5
->succs
)[0], 5, 1, EDGE_FALLTHRU
);
2029 ASSERT_EQ (1, exit
->preds
->length ());
2030 ASSERT_EDGE ((*exit
->preds
)[0], 5, 1, EDGE_FALLTHRU
);
2031 ASSERT_EQ (NULL
, exit
->succs
);
2034 /* Verify that the loader copes with sparse block indices.
2035 This testcase loads a file with a "(block 42)". */
2038 test_loading_bb_index ()
2040 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("bb-index.rtl"));
2042 ASSERT_STREQ ("test_bb_index", IDENTIFIER_POINTER (DECL_NAME (cfun
->decl
)));
2046 ASSERT_TRUE (cfun
->cfg
!= NULL
);
2047 ASSERT_EQ (3, n_basic_blocks_for_fn (cfun
));
2048 ASSERT_EQ (43, basic_block_info_for_fn (cfun
)->length ());
2049 ASSERT_EQ (2, n_edges_for_fn (cfun
));
2051 ASSERT_EQ (NULL
, (*cfun
->cfg
->x_basic_block_info
)[41]);
2052 basic_block bb42
= (*cfun
->cfg
->x_basic_block_info
)[42];
2053 ASSERT_NE (NULL
, bb42
);
2054 ASSERT_EQ (42, bb42
->index
);
2057 /* Verify that function_reader::handle_any_trailing_information correctly
2058 parses all the possible items emitted for a MEM. */
2063 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("mem.rtl"));
2065 ASSERT_STREQ ("test_mem", IDENTIFIER_POINTER (DECL_NAME (cfun
->decl
)));
2068 /* Verify parsing of "[42 i+17 S8 A128 AS5]". */
2069 rtx_insn
*insn_1
= get_insn_by_uid (1);
2070 rtx set1
= single_set (insn_1
);
2071 rtx mem1
= SET_DEST (set1
);
2072 ASSERT_EQ (42, MEM_ALIAS_SET (mem1
));
2074 ASSERT_TRUE (MEM_OFFSET_KNOWN_P (mem1
));
2075 ASSERT_EQ (17, MEM_OFFSET (mem1
));
2077 ASSERT_EQ (8, MEM_SIZE (mem1
));
2079 ASSERT_EQ (128, MEM_ALIGN (mem1
));
2081 ASSERT_EQ (5, MEM_ADDR_SPACE (mem1
));
2083 /* Verify parsing of "43 i+18 S9 AS6"
2084 (an address space without an alignment). */
2085 rtx_insn
*insn_2
= get_insn_by_uid (2);
2086 rtx set2
= single_set (insn_2
);
2087 rtx mem2
= SET_DEST (set2
);
2088 ASSERT_EQ (43, MEM_ALIAS_SET (mem2
));
2090 ASSERT_TRUE (MEM_OFFSET_KNOWN_P (mem2
));
2091 ASSERT_EQ (18, MEM_OFFSET (mem2
));
2093 ASSERT_EQ (9, MEM_SIZE (mem2
));
2095 ASSERT_EQ (6, MEM_ADDR_SPACE (mem2
));
2098 /* Run all of the selftests within this file. */
2101 read_rtl_function_c_tests ()
2104 test_parsing_regnos ();
2105 test_loading_dump_fragment_1 ();
2106 test_loading_dump_fragment_2 ();
2107 test_loading_labels ();
2108 test_loading_insn_with_mode ();
2109 test_loading_jump_to_label_ref ();
2110 test_loading_jump_to_return ();
2111 test_loading_jump_to_simple_return ();
2112 test_loading_note_insn_basic_block ();
2113 test_loading_note_insn_deleted ();
2114 test_loading_const_int ();
2115 test_loading_symbol_ref ();
2116 test_loading_cfg ();
2117 test_loading_bb_index ();
2118 test_loading_mem ();
2121 } // namespace selftest
2123 #endif /* #if CHECKING_P */