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 ();
478 /* When run from selftests or "rtl1", cfun is NULL.
479 When run from "cc1" for a C function tagged with __RTL, cfun is the
483 tree fn_name
= get_identifier (m_name
? m_name
: "test_1");
484 tree int_type
= integer_type_node
;
485 tree return_type
= int_type
;
486 tree arg_types
[3] = {int_type
, int_type
, int_type
};
487 tree fn_type
= build_function_type_array (return_type
, 3, arg_types
);
488 tree fndecl
= build_decl (UNKNOWN_LOCATION
, FUNCTION_DECL
, fn_name
, fn_type
);
489 tree resdecl
= build_decl (UNKNOWN_LOCATION
, RESULT_DECL
, NULL_TREE
,
491 DECL_ARTIFICIAL (resdecl
) = 1;
492 DECL_IGNORED_P (resdecl
) = 1;
493 DECL_RESULT (fndecl
) = resdecl
;
494 allocate_struct_function (fndecl
, false);
495 /* This sets cfun. */
496 current_function_decl
= fndecl
;
500 gcc_assert (current_function_decl
);
501 tree fndecl
= current_function_decl
;
503 /* Mark this function as being specified as __RTL. */
504 cfun
->curr_properties
|= PROP_rtl
;
506 /* cc1 normally inits DECL_INITIAL (fndecl) to be error_mark_node.
507 Create a dummy block for it. */
508 DECL_INITIAL (fndecl
) = make_node (BLOCK
);
510 cfun
->curr_properties
= (PROP_cfg
| PROP_rtl
);
512 /* Do we need this to force cgraphunit.c to output the function? */
513 DECL_EXTERNAL (fndecl
) = 0;
514 DECL_PRESERVE_P (fndecl
) = 1;
517 cgraph_node::finalize_function (fndecl
, false);
519 /* Create bare-bones cfg. This creates the entry and exit blocks. */
520 init_empty_tree_cfg_for_function (cfun
);
521 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->flags
|= BB_RTL
;
522 EXIT_BLOCK_PTR_FOR_FN (cfun
)->flags
|= BB_RTL
;
523 init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
524 init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun
));
525 m_bb_to_insert_after
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
529 /* Look within the the params of FNDECL for a param named NAME.
530 Return NULL_TREE if one isn't found. */
533 find_param_by_name (tree fndecl
, const char *name
)
535 for (tree arg
= DECL_ARGUMENTS (fndecl
); arg
; arg
= TREE_CHAIN (arg
))
536 if (strcmp (name
, IDENTIFIER_POINTER (DECL_NAME (arg
))) == 0)
541 /* Parse the content of a "param" directive, having already parsed the
542 "(param". Consume the trailing ')'. */
545 function_reader::parse_param ()
547 require_char_ws ('"');
548 file_location loc
= get_current_location ();
549 char *name
= read_quoted_string ();
551 /* Lookup param by name. */
552 tree t_param
= find_param_by_name (cfun
->decl
, name
);
554 fatal_at (loc
, "param not found: %s", name
);
556 /* Parse DECL_RTL. */
557 require_char_ws ('(');
558 require_word_ws ("DECL_RTL");
559 DECL_WRTL_CHECK (t_param
)->decl_with_rtl
.rtl
= parse_rtx ();
560 require_char_ws (')');
562 /* Parse DECL_RTL_INCOMING. */
563 require_char_ws ('(');
564 require_word_ws ("DECL_RTL_INCOMING");
565 DECL_INCOMING_RTL (t_param
) = parse_rtx ();
566 require_char_ws (')');
568 require_char_ws (')');
571 /* Parse zero or more child insn elements within an
572 "insn-chain" element. Consume the trailing ')'. */
575 function_reader::parse_insn_chain ()
579 int c
= read_skip_spaces ();
580 file_location loc
= get_current_location ();
585 struct md_name directive
;
586 read_name (&directive
);
587 if (strcmp (directive
.string
, "block") == 0)
590 parse_insn (loc
, directive
.string
);
593 fatal_at (loc
, "expected '(' or ')'");
599 /* Parse zero or more child directives (edges and insns) within a
600 "block" directive, having already parsed the "(block " heading.
601 Consume the trailing ')'. */
604 function_reader::parse_block ()
606 /* Parse the index value from the dump. This will be an integer;
607 we don't support "entry" or "exit" here (unlike for edges). */
610 int bb_idx
= atoi (name
.string
);
612 /* The term "index" has two meanings for basic blocks in a CFG:
613 (a) the "index" field within struct basic_block_def.
614 (b) the index of a basic_block within the cfg's x_basic_block_info
615 vector, as accessed via BASIC_BLOCK_FOR_FN.
617 These can get out-of-sync when basic blocks are optimized away.
618 They get back in sync by "compact_blocks".
619 We reconstruct cfun->cfg->x_basic_block_info->m_vecdata with NULL
620 values in it for any missing basic blocks, so that (a) == (b) for
621 all of the blocks we create. The doubly-linked list of basic
622 blocks (next_bb/prev_bb) skips over these "holes". */
624 if (m_highest_bb_idx
< bb_idx
)
625 m_highest_bb_idx
= bb_idx
;
627 size_t new_size
= m_highest_bb_idx
+ 1;
628 if (basic_block_info_for_fn (cfun
)->length () < new_size
)
629 vec_safe_grow_cleared (basic_block_info_for_fn (cfun
), new_size
);
631 last_basic_block_for_fn (cfun
) = new_size
;
633 /* Create the basic block.
635 We can't call create_basic_block and use the regular RTL block-creation
636 hooks, since this creates NOTE_INSN_BASIC_BLOCK instances. We don't
637 want to do that; we want to use the notes we were provided with. */
638 basic_block bb
= alloc_block ();
639 init_rtl_bb_info (bb
);
641 bb
->flags
= BB_NEW
| BB_RTL
;
642 link_block (bb
, m_bb_to_insert_after
);
643 m_bb_to_insert_after
= bb
;
645 n_basic_blocks_for_fn (cfun
)++;
646 SET_BASIC_BLOCK_FOR_FN (cfun
, bb_idx
, bb
);
647 BB_SET_PARTITION (bb
, BB_UNPARTITIONED
);
649 /* Handle insns, edge-from and edge-to directives. */
652 int c
= read_skip_spaces ();
653 file_location loc
= get_current_location ();
658 struct md_name directive
;
659 read_name (&directive
);
660 if (strcmp (directive
.string
, "edge-from") == 0)
661 parse_edge (bb
, true);
662 else if (strcmp (directive
.string
, "edge-to") == 0)
663 parse_edge (bb
, false);
666 rtx_insn
*insn
= parse_insn (loc
, directive
.string
);
667 set_block_for_insn (insn
, bb
);
674 fatal_at (loc
, "expected '(' or ')'");
678 /* Subroutine of function_reader::parse_edge.
679 Parse a basic block index, handling "entry" and "exit". */
682 function_reader::parse_bb_idx ()
686 if (strcmp (name
.string
, "entry") == 0)
688 if (strcmp (name
.string
, "exit") == 0)
690 return atoi (name
.string
);
693 /* Subroutine of parse_edge_flags.
694 Parse TOK, a token such as "FALLTHRU", converting to the flag value.
695 Issue an error if the token is unrecognized. */
698 parse_edge_flag_token (const char *tok
)
700 #define DEF_EDGE_FLAG(NAME,IDX) \
702 if (strcmp (tok, #NAME) == 0) \
703 return EDGE_##NAME; \
705 #include "cfg-flags.def"
707 error ("unrecognized edge flag: '%s'", tok
);
711 /* Subroutine of function_reader::parse_edge.
712 Parse STR and convert to a flag value (or issue an error).
713 The parser uses strtok and hence modifiers STR in-place. */
716 parse_edge_flags (char *str
)
720 char *tok
= strtok (str
, "| ");
723 result
|= parse_edge_flag_token (tok
);
724 tok
= strtok (NULL
, "| ");
730 /* Parse an "edge-from" or "edge-to" directive within the "block"
731 directive for BLOCK, having already parsed the "(edge" heading.
732 Consume the final ")". Record the edge within m_deferred_edges.
733 FROM is true for an "edge-from" directive, false for an "edge-to"
737 function_reader::parse_edge (basic_block block
, bool from
)
740 int this_bb_idx
= block
->index
;
741 file_location loc
= get_current_location ();
742 int other_bb_idx
= parse_bb_idx ();
744 /* "(edge-from 2)" means src = 2, dest = this_bb_idx, whereas
745 "(edge-to 3)" means src = this_bb_idx, dest = 3. */
746 int src_idx
= from
? other_bb_idx
: this_bb_idx
;
747 int dest_idx
= from
? this_bb_idx
: other_bb_idx
;
749 /* Optional "(flags)". */
751 int c
= read_skip_spaces ();
754 require_word_ws ("flags");
755 require_char_ws ('"');
756 char *str
= read_quoted_string ();
757 flags
= parse_edge_flags (str
);
758 require_char_ws (')');
763 require_char_ws (')');
765 /* This BB already exists, but the other BB might not yet.
766 For now, save the edges, and create them at the end of insn-chain
768 /* For now, only process the (edge-from) to this BB, and (edge-to)
769 that go to the exit block.
770 FIXME: we don't yet verify that the edge-from and edge-to directives
772 if (from
|| dest_idx
== EXIT_BLOCK
)
773 m_deferred_edges
.safe_push (deferred_edge (loc
, src_idx
, dest_idx
, flags
));
776 /* Parse an rtx instruction, having parsed the opening and parenthesis, and
777 name NAME, seen at START_LOC, by calling read_rtx_code, calling
778 set_first_insn and set_last_insn as appropriate, and
779 adding the insn to the insn chain.
780 Consume the trailing ')'. */
783 function_reader::parse_insn (file_location start_loc
, const char *name
)
785 rtx x
= read_rtx_code (name
);
787 fatal_at (start_loc
, "expected insn type; got '%s'", name
);
788 rtx_insn
*insn
= dyn_cast
<rtx_insn
*> (x
);
790 fatal_at (start_loc
, "expected insn type; got '%s'", name
);
792 /* Consume the trailing ')'. */
793 require_char_ws (')');
795 rtx_insn
*last_insn
= get_last_insn ();
797 /* Add "insn" to the insn chain. */
800 gcc_assert (NEXT_INSN (last_insn
) == NULL
);
801 SET_NEXT_INSN (last_insn
) = insn
;
803 SET_PREV_INSN (insn
) = last_insn
;
805 /* Add it to the sequence. */
806 set_last_insn (insn
);
810 set_first_insn (insn
);
813 if (rtx_code_label
*label
= dyn_cast
<rtx_code_label
*> (insn
))
814 maybe_set_max_label_num (label
);
819 /* Postprocessing subroutine for parse_insn_chain: all the basic blocks
820 should have been created by now; create the edges that were seen. */
823 function_reader::create_edges ()
827 FOR_EACH_VEC_ELT (m_deferred_edges
, i
, de
)
829 /* The BBs should already have been created by parse_block. */
830 basic_block src
= BASIC_BLOCK_FOR_FN (cfun
, de
->m_src_bb_idx
);
832 fatal_at (de
->m_loc
, "error: block index %i not found",
834 basic_block dst
= BASIC_BLOCK_FOR_FN (cfun
, de
->m_dest_bb_idx
);
836 fatal_at (de
->m_loc
, "error: block with index %i not found",
838 unchecked_make_edge (src
, dst
, de
->m_flags
);
842 /* Parse a "crtl" directive, having already parsed the "(crtl" heading
844 Consume the final ")". */
847 function_reader::parse_crtl (file_location loc
)
849 if (m_have_crtl_directive
)
850 error_at (loc
, "more than one 'crtl' directive");
851 m_have_crtl_directive
= true;
854 require_char_ws ('(');
855 require_word_ws ("return_rtx");
856 crtl
->return_rtx
= parse_rtx ();
857 require_char_ws (')');
859 require_char_ws (')');
862 /* Parse operand IDX of X, returning X, or an equivalent rtx
863 expression (for consolidating singletons).
864 This is an overridden implementation of rtx_reader::read_rtx_operand for
865 function_reader, handling various extra data printed by print_rtx,
866 and sometimes calling the base class implementation. */
869 function_reader::read_rtx_operand (rtx x
, int idx
)
871 RTX_CODE code
= GET_CODE (x
);
872 const char *format_ptr
= GET_RTX_FORMAT (code
);
873 const char format_char
= format_ptr
[idx
];
876 /* Override the regular parser for some format codes. */
880 if (idx
== 7 && CALL_P (x
))
882 m_in_call_function_usage
= true;
883 return rtx_reader::read_rtx_operand (x
, idx
);
884 m_in_call_function_usage
= false;
887 return rtx_reader::read_rtx_operand (x
, idx
);
891 read_rtx_operand_u (x
, idx
);
892 /* Don't run regular parser for 'u'. */
897 read_rtx_operand_i_or_n (x
, idx
, format_char
);
898 /* Don't run regular parser for these codes. */
902 gcc_assert (is_compact ());
903 /* Compact mode doesn't store BBs. */
904 /* Don't run regular parser. */
908 /* Don't run regular parser for 'r'. */
909 return read_rtx_operand_r (x
);
915 /* Call base class implementation. */
916 x
= rtx_reader::read_rtx_operand (x
, idx
);
918 /* Handle any additional parsing needed to handle what the dump
923 extra_parsing_for_operand_code_0 (x
, idx
);
929 /* Strip away the redundant hex dump of the value. */
930 require_char_ws ('[');
932 require_char_ws (']');
943 /* Parse operand IDX of X, of code 'u', when reading function dumps.
945 The RTL file recorded the ID of an insn (or 0 for NULL); we
946 must store this as a pointer, but the insn might not have
947 been loaded yet. Store the ID away for now, via a fixup. */
950 function_reader::read_rtx_operand_u (rtx x
, int idx
)
952 /* In compact mode, the PREV/NEXT insn uids are not dumped, so skip
953 the "uu" when reading. */
954 if (is_compact () && GET_CODE (x
) != LABEL_REF
)
958 file_location loc
= read_name (&name
);
959 int insn_id
= atoi (name
.string
);
961 add_fixup_insn_uid (loc
, x
, idx
, insn_id
);
964 /* Read a name, looking for a match against a string found in array
965 STRINGS of size NUM_VALUES.
966 Return the index of the the matched string, or emit an error. */
969 function_reader::parse_enum_value (int num_values
, const char *const *strings
)
973 for (int i
= 0; i
< num_values
; i
++)
975 if (strcmp (name
.string
, strings
[i
]) == 0)
978 error ("unrecognized enum value: '%s'", name
.string
);
982 /* Parse operand IDX of X, of code 'i' or 'n' (as specified by FORMAT_CHAR).
983 Special-cased handling of these, for reading function dumps. */
986 function_reader::read_rtx_operand_i_or_n (rtx x
, int idx
,
989 /* Handle some of the extra information that print_rtx
990 can write out for these cases. */
991 /* print_rtx only writes out operand 5 for notes
992 for NOTE_KIND values NOTE_INSN_DELETED_LABEL
993 and NOTE_INSN_DELETED_DEBUG_LABEL. */
994 if (idx
== 5 && NOTE_P (x
))
997 if (idx
== 4 && INSN_P (x
))
999 maybe_read_location (as_a
<rtx_insn
*> (x
));
1003 /* INSN_CODEs aren't printed in compact mode, so don't attempt to
1007 && &INSN_CODE (x
) == &XINT (x
, idx
))
1013 /* Handle UNSPEC and UNSPEC_VOLATILE's operand 1. */
1014 #if !defined(GENERATOR_FILE) && NUM_UNSPECV_VALUES > 0
1016 && GET_CODE (x
) == UNSPEC_VOLATILE
)
1019 = parse_enum_value (NUM_UNSPECV_VALUES
, unspecv_strings
);
1023 #if !defined(GENERATOR_FILE) && NUM_UNSPEC_VALUES > 0
1025 && (GET_CODE (x
) == UNSPEC
1026 || GET_CODE (x
) == UNSPEC_VOLATILE
))
1029 = parse_enum_value (NUM_UNSPEC_VALUES
, unspec_strings
);
1034 struct md_name name
;
1037 if (format_char
== 'n')
1038 value
= parse_note_insn_name (name
.string
);
1040 value
= atoi (name
.string
);
1041 XINT (x
, idx
) = value
;
1044 /* Parse the 'r' operand of X, returning X, or an equivalent rtx
1045 expression (for consolidating singletons).
1046 Special-cased handling of code 'r' for reading function dumps. */
1049 function_reader::read_rtx_operand_r (rtx x
)
1051 struct md_name name
;
1052 file_location loc
= read_name (&name
);
1053 int regno
= lookup_reg_by_dump_name (name
.string
);
1055 fatal_at (loc
, "unrecognized register: '%s'", name
.string
);
1057 set_regno_raw (x
, regno
, 1);
1059 /* Consolidate singletons. */
1060 x
= consolidate_singletons (x
);
1062 ORIGINAL_REGNO (x
) = regno
;
1064 /* Parse extra stuff at end of 'r'.
1065 We may have zero, one, or two sections marked by square
1067 int ch
= read_skip_spaces ();
1068 bool expect_original_regno
= false;
1071 file_location loc
= get_current_location ();
1072 char *desc
= read_until ("]", true);
1073 strip_trailing_whitespace (desc
);
1074 const char *desc_start
= desc
;
1075 /* If ORIGINAL_REGNO (rtx) != regno, we will have:
1076 "orig:%i", ORIGINAL_REGNO (rtx).
1077 Consume it, we don't set ORIGINAL_REGNO, since we can
1078 get that from the 2nd copy later. */
1079 if (0 == strncmp (desc
, "orig:", 5))
1081 expect_original_regno
= true;
1083 /* Skip to any whitespace following the integer. */
1084 const char *space
= strchr (desc_start
, ' ');
1086 desc_start
= space
+ 1;
1088 /* Any remaining text may be the REG_EXPR. Alternatively we have
1089 no REG_ATTRS, and instead we have ORIGINAL_REGNO. */
1090 if (ISDIGIT (*desc_start
))
1092 /* Assume we have ORIGINAL_REGNO. */
1093 ORIGINAL_REGNO (x
) = atoi (desc_start
);
1097 /* Assume we have REG_EXPR. */
1098 add_fixup_expr (loc
, x
, desc_start
);
1104 if (expect_original_regno
)
1106 require_char_ws ('[');
1107 char *desc
= read_until ("]", true);
1108 ORIGINAL_REGNO (x
) = atoi (desc
);
1115 /* Additional parsing for format code '0' in dumps, handling a variety
1116 of special-cases in print_rtx, when parsing operand IDX of X. */
1119 function_reader::extra_parsing_for_operand_code_0 (rtx x
, int idx
)
1121 RTX_CODE code
= GET_CODE (x
);
1123 struct md_name name
;
1125 if (idx
== 1 && code
== SYMBOL_REF
)
1127 /* Possibly wrote " [flags %#x]", SYMBOL_REF_FLAGS (in_rtx). */
1128 c
= read_skip_spaces ();
1131 file_location loc
= read_name (&name
);
1132 if (strcmp (name
.string
, "flags"))
1133 error_at (loc
, "was expecting `%s'", "flags");
1135 SYMBOL_REF_FLAGS (x
) = strtol (name
.string
, NULL
, 16);
1137 /* We can't reconstruct SYMBOL_REF_BLOCK; set it to NULL. */
1138 if (SYMBOL_REF_HAS_BLOCK_INFO_P (x
))
1139 SYMBOL_REF_BLOCK (x
) = NULL
;
1146 /* If X had a non-NULL SYMBOL_REF_DECL,
1147 rtx_writer::print_rtx_operand_code_0 would have dumped it
1148 using print_node_brief.
1149 Skip the content for now. */
1150 c
= read_skip_spaces ();
1155 char ch
= read_char ();
1163 else if (idx
== 3 && code
== NOTE
)
1165 /* Note-specific data appears for operand 3, which annoyingly
1166 is before the enum specifying which kind of note we have
1168 c
= read_skip_spaces ();
1171 /* Possibly data for a NOTE_INSN_BASIC_BLOCK, of the form:
1173 file_location bb_loc
= read_name (&name
);
1174 if (strcmp (name
.string
, "bb"))
1175 error_at (bb_loc
, "was expecting `%s'", "bb");
1177 int bb_idx
= atoi (name
.string
);
1178 add_fixup_note_insn_basic_block (bb_loc
, x
, idx
,
1180 require_char_ws (']');
1187 /* Implementation of rtx_reader::handle_any_trailing_information.
1188 Handle the various additional information that print-rtl.c can
1189 write after the regular fields, when parsing X. */
1192 function_reader::handle_any_trailing_information (rtx x
)
1194 struct md_name name
;
1196 switch (GET_CODE (x
))
1201 require_char_ws ('[');
1203 set_mem_alias_set (x
, atoi (name
.string
));
1204 /* We have either a MEM_EXPR, or a space. */
1205 if (peek_char () != ' ')
1207 file_location loc
= get_current_location ();
1208 char *desc
= read_until (" +", false);
1209 add_fixup_expr (loc
, consolidate_singletons (x
), desc
);
1215 /* We may optionally have '+' for MEM_OFFSET_KNOWN_P. */
1216 ch
= read_skip_spaces ();
1220 set_mem_offset (x
, atoi (name
.string
));
1225 /* Handle optional " S" for MEM_SIZE. */
1226 ch
= read_skip_spaces ();
1230 set_mem_size (x
, atoi (name
.string
));
1235 /* Handle optional " A" for MEM_ALIGN. */
1236 ch
= read_skip_spaces ();
1237 if (ch
== 'A' && peek_char () != 'S')
1240 set_mem_align (x
, atoi (name
.string
));
1245 /* Handle optional " AS" for MEM_ADDR_SPACE. */
1246 ch
= read_skip_spaces ();
1247 if (ch
== 'A' && peek_char () == 'S')
1251 set_mem_addr_space (x
, atoi (name
.string
));
1261 /* Assume that LABEL_NUSES was not dumped. */
1262 /* TODO: parse LABEL_KIND. */
1263 /* For now, skip until closing ')'. */
1266 char ch
= read_char ();
1281 /* Parse a tree dump for a MEM_EXPR in DESC and turn it back into a tree.
1282 We handle "<retval>" and param names within cfun, but for anything else
1283 we "cheat" by building a global VAR_DECL of type "int" with that name
1284 (returning the same global for a name if we see the same name more
1288 function_reader::parse_mem_expr (const char *desc
)
1290 tree fndecl
= cfun
->decl
;
1292 if (0 == strcmp (desc
, "<retval>"))
1293 return DECL_RESULT (fndecl
);
1295 tree param
= find_param_by_name (fndecl
, desc
);
1299 /* Search within decls we already created.
1300 FIXME: use a hash rather than linear search. */
1303 FOR_EACH_VEC_ELT (m_fake_scope
, i
, t
)
1304 if (strcmp (desc
, IDENTIFIER_POINTER (DECL_NAME (t
))) == 0)
1307 /* Not found? Create it.
1308 This allows mimicking of real data but avoids having to specify
1309 e.g. names of locals, params etc.
1310 Though this way we don't know if we have a PARM_DECL vs a VAR_DECL,
1311 and we don't know the types. Fake it by making everything be
1312 a VAR_DECL of "int" type. */
1313 t
= build_decl (UNKNOWN_LOCATION
, VAR_DECL
,
1314 get_identifier (desc
),
1316 m_fake_scope
.safe_push (t
);
1320 /* Record that at LOC we saw an insn uid INSN_UID for the operand with index
1321 OPERAND_IDX within INSN, so that the pointer value can be fixed up in
1322 later post-processing. */
1325 function_reader::add_fixup_insn_uid (file_location loc
, rtx insn
, int operand_idx
,
1328 m_fixups
.safe_push (new fixup_insn_uid (loc
, insn
, operand_idx
, insn_uid
));
1331 /* Record that at LOC we saw an basic block index BB_IDX for the operand with index
1332 OPERAND_IDX within INSN, so that the pointer value can be fixed up in
1333 later post-processing. */
1336 function_reader::add_fixup_note_insn_basic_block (file_location loc
, rtx insn
,
1337 int operand_idx
, int bb_idx
)
1339 m_fixups
.safe_push (new fixup_note_insn_basic_block (loc
, insn
, operand_idx
,
1343 /* Placeholder hook for recording source location information seen in a dump.
1344 This is empty for now. */
1347 function_reader::add_fixup_source_location (file_location
, rtx_insn
*,
1352 /* Record that at LOC we saw textual description DESC of the MEM_EXPR or REG_EXPR
1353 of INSN, so that the fields can be fixed up in later post-processing. */
1356 function_reader::add_fixup_expr (file_location loc
, rtx insn
,
1360 /* Fail early if the RTL reader erroneously hands us an int. */
1361 gcc_assert (!ISDIGIT (desc
[0]));
1363 m_fixups
.safe_push (new fixup_expr (loc
, insn
, desc
));
1366 /* Helper function for consolidate_reg. Return the global rtx for
1367 the register with regno REGNO. */
1370 lookup_global_register (int regno
)
1372 /* We can't use a switch here, as some of the REGNUMs might not be constants
1373 for some targets. */
1374 if (regno
== STACK_POINTER_REGNUM
)
1375 return stack_pointer_rtx
;
1376 else if (regno
== FRAME_POINTER_REGNUM
)
1377 return frame_pointer_rtx
;
1378 else if (regno
== HARD_FRAME_POINTER_REGNUM
)
1379 return hard_frame_pointer_rtx
;
1380 else if (regno
== ARG_POINTER_REGNUM
)
1381 return arg_pointer_rtx
;
1382 else if (regno
== VIRTUAL_INCOMING_ARGS_REGNUM
)
1383 return virtual_incoming_args_rtx
;
1384 else if (regno
== VIRTUAL_STACK_VARS_REGNUM
)
1385 return virtual_stack_vars_rtx
;
1386 else if (regno
== VIRTUAL_STACK_DYNAMIC_REGNUM
)
1387 return virtual_stack_dynamic_rtx
;
1388 else if (regno
== VIRTUAL_OUTGOING_ARGS_REGNUM
)
1389 return virtual_outgoing_args_rtx
;
1390 else if (regno
== VIRTUAL_CFA_REGNUM
)
1391 return virtual_cfa_rtx
;
1392 else if (regno
== VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM
)
1393 return virtual_preferred_stack_boundary_rtx
;
1394 #ifdef return_ADDRESS_POINTER_REGNUM
1395 else if (regno
== RETURN_ADDRESS_POINTER_REGNUM
)
1396 return return_address_pointer_rtx
;
1402 /* Ensure that the backend can cope with a REG with regno REGNO.
1403 Normally REG instances are created by gen_reg_rtx which updates
1404 regno_reg_rtx, growing it as necessary.
1405 The REG instances created from the dumpfile weren't created this
1406 way, so we need to manually update regno_reg_rtx. */
1409 ensure_regno (int regno
)
1411 if (reg_rtx_no
< regno
+ 1)
1412 reg_rtx_no
= regno
+ 1;
1414 crtl
->emit
.ensure_regno_capacity ();
1415 gcc_assert (regno
< crtl
->emit
.regno_pointer_align_length
);
1418 /* Helper function for consolidate_singletons, for handling REG instances.
1419 Given REG instance X of some regno, return the singleton rtx for that
1420 regno, if it exists, or X. */
1423 consolidate_reg (rtx x
)
1425 gcc_assert (GET_CODE (x
) == REG
);
1427 unsigned int regno
= REGNO (x
);
1429 ensure_regno (regno
);
1431 /* Some register numbers have their rtx created in init_emit_regs
1432 e.g. stack_pointer_rtx for STACK_POINTER_REGNUM.
1433 Consolidate on this. */
1434 rtx global_reg
= lookup_global_register (regno
);
1438 /* Populate regno_reg_rtx if necessary. */
1439 if (regno_reg_rtx
[regno
] == NULL
)
1440 regno_reg_rtx
[regno
] = x
;
1442 gcc_assert (GET_CODE (regno_reg_rtx
[regno
]) == REG
);
1443 gcc_assert (REGNO (regno_reg_rtx
[regno
]) == regno
);
1444 if (GET_MODE (x
) == GET_MODE (regno_reg_rtx
[regno
]))
1445 return regno_reg_rtx
[regno
];
1450 /* When reading RTL function dumps, we must consolidate some
1451 rtx so that we use singletons where singletons are expected
1452 (e.g. we don't want multiple "(const_int 0 [0])" rtx, since
1453 these are tested via pointer equality against const0_rtx.
1455 Return the equivalent singleton rtx for X, if any, otherwise X. */
1458 function_reader::consolidate_singletons (rtx x
)
1463 switch (GET_CODE (x
))
1465 case PC
: return pc_rtx
;
1466 case RETURN
: return ret_rtx
;
1467 case SIMPLE_RETURN
: return simple_return_rtx
;
1468 case CC0
: return cc0_rtx
;
1471 return consolidate_reg (x
);
1474 return gen_rtx_CONST_INT (GET_MODE (x
), INTVAL (x
));
1483 /* Parse an rtx directive, including both the opening/closing parentheses,
1487 function_reader::parse_rtx ()
1489 require_char_ws ('(');
1490 struct md_name directive
;
1491 read_name (&directive
);
1493 = consolidate_singletons (read_rtx_code (directive
.string
));
1494 require_char_ws (')');
1499 /* Implementation of rtx_reader::postprocess for reading function dumps.
1500 Return the equivalent singleton rtx for X, if any, otherwise X. */
1503 function_reader::postprocess (rtx x
)
1505 return consolidate_singletons (x
);
1508 /* Implementation of rtx_reader::finalize_string for reading function dumps.
1509 Make a GC-managed copy of STRINGBUF. */
1512 function_reader::finalize_string (char *stringbuf
)
1514 return ggc_strdup (stringbuf
);
1517 /* Attempt to parse optional location information for insn INSN, as
1518 potentially written out by rtx_writer::print_rtx_operand_code_i.
1519 We look for a quoted string followed by a colon. */
1522 function_reader::maybe_read_location (rtx_insn
*insn
)
1524 file_location loc
= get_current_location ();
1526 /* Attempt to parse a quoted string. */
1527 int ch
= read_skip_spaces ();
1530 char *filename
= read_quoted_string ();
1532 struct md_name line_num
;
1533 read_name (&line_num
);
1534 add_fixup_source_location (loc
, insn
, filename
, atoi (line_num
.string
));
1540 /* Postprocessing subroutine of function_reader::parse_function.
1541 Populate m_insns_by_uid. */
1544 function_reader::handle_insn_uids ()
1546 /* Locate the currently assigned INSN_UID values, storing
1547 them in m_insns_by_uid. */
1549 for (rtx_insn
*insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
1551 if (m_insns_by_uid
.get (INSN_UID (insn
)))
1552 error ("duplicate insn UID: %i", INSN_UID (insn
));
1553 m_insns_by_uid
.put (INSN_UID (insn
), insn
);
1554 if (INSN_UID (insn
) > max_uid
)
1555 max_uid
= INSN_UID (insn
);
1558 /* Ensure x_cur_insn_uid is 1 more than the biggest insn UID seen.
1559 This is normally updated by the various make_*insn_raw functions. */
1560 crtl
->emit
.x_cur_insn_uid
= max_uid
+ 1;
1563 /* Apply all of the recorded fixups. */
1566 function_reader::apply_fixups ()
1570 FOR_EACH_VEC_ELT (m_fixups
, i
, f
)
1574 /* Given a UID value, try to locate a pointer to the corresponding
1575 rtx_insn *, or NULL if if can't be found. */
1578 function_reader::get_insn_by_uid (int uid
)
1580 return m_insns_by_uid
.get (uid
);
1583 /* Run the RTL dump parser, parsing a dump located at PATH.
1584 Return true iff the file was successfully parsed. */
1587 read_rtl_function_body (const char *path
)
1591 init_varasm_status ();
1593 function_reader reader
;
1594 if (!reader
.read_file (path
))
1600 /* Run the RTL dump parser on the range of lines between START_LOC and
1601 END_LOC (including those lines). */
1604 read_rtl_function_body_from_file_range (location_t start_loc
,
1607 expanded_location exploc_start
= expand_location (start_loc
);
1608 expanded_location exploc_end
= expand_location (end_loc
);
1610 if (exploc_start
.file
!= exploc_end
.file
)
1612 error_at (end_loc
, "start/end of RTL fragment are in different files");
1615 if (exploc_start
.line
>= exploc_end
.line
)
1618 "start of RTL fragment must be on an earlier line than end");
1624 init_varasm_status ();
1626 function_reader reader
;
1627 if (!reader
.read_file_fragment (exploc_start
.file
, exploc_start
.line
,
1628 exploc_end
.line
- 1))
1636 namespace selftest
{
1638 /* Verify that parse_edge_flags works. */
1643 /* parse_edge_flags modifies its input (due to strtok), so we must make
1644 a copy of the literals. */
1645 #define ASSERT_PARSE_EDGE_FLAGS(EXPECTED, STR) \
1647 char *str = xstrdup (STR); \
1648 ASSERT_EQ (EXPECTED, parse_edge_flags (str)); \
1652 ASSERT_PARSE_EDGE_FLAGS (0, "");
1653 ASSERT_PARSE_EDGE_FLAGS (EDGE_FALLTHRU
, "FALLTHRU");
1654 ASSERT_PARSE_EDGE_FLAGS (EDGE_ABNORMAL_CALL
, "ABNORMAL_CALL");
1655 ASSERT_PARSE_EDGE_FLAGS (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
,
1656 "ABNORMAL | ABNORMAL_CALL");
1658 #undef ASSERT_PARSE_EDGE_FLAGS
1661 /* Verify that lookup_reg_by_dump_name works. */
1664 test_parsing_regnos ()
1666 ASSERT_EQ (-1, lookup_reg_by_dump_name ("this is not a register"));
1668 /* Verify lookup of virtual registers. */
1669 ASSERT_EQ (VIRTUAL_INCOMING_ARGS_REGNUM
,
1670 lookup_reg_by_dump_name ("virtual-incoming-args"));
1671 ASSERT_EQ (VIRTUAL_STACK_VARS_REGNUM
,
1672 lookup_reg_by_dump_name ("virtual-stack-vars"));
1673 ASSERT_EQ (VIRTUAL_STACK_DYNAMIC_REGNUM
,
1674 lookup_reg_by_dump_name ("virtual-stack-dynamic"));
1675 ASSERT_EQ (VIRTUAL_OUTGOING_ARGS_REGNUM
,
1676 lookup_reg_by_dump_name ("virtual-outgoing-args"));
1677 ASSERT_EQ (VIRTUAL_CFA_REGNUM
,
1678 lookup_reg_by_dump_name ("virtual-cfa"));
1679 ASSERT_EQ (VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM
,
1680 lookup_reg_by_dump_name ("virtual-preferred-stack-boundary"));
1682 /* Verify lookup of non-virtual pseudos. */
1683 ASSERT_EQ (LAST_VIRTUAL_REGISTER
+ 1, lookup_reg_by_dump_name ("<0>"));
1684 ASSERT_EQ (LAST_VIRTUAL_REGISTER
+ 2, lookup_reg_by_dump_name ("<1>"));
1687 /* Verify that edge E is as expected, with the src and dest basic blocks
1688 having indices EXPECTED_SRC_IDX and EXPECTED_DEST_IDX respectively, and
1689 the edge having flags equal to EXPECTED_FLAGS.
1690 Use LOC as the effective location when reporting failures. */
1693 assert_edge_at (const location
&loc
, edge e
, int expected_src_idx
,
1694 int expected_dest_idx
, int expected_flags
)
1696 ASSERT_EQ_AT (loc
, expected_src_idx
, e
->src
->index
);
1697 ASSERT_EQ_AT (loc
, expected_dest_idx
, e
->dest
->index
);
1698 ASSERT_EQ_AT (loc
, expected_flags
, e
->flags
);
1701 /* Verify that edge EDGE is as expected, with the src and dest basic blocks
1702 having indices EXPECTED_SRC_IDX and EXPECTED_DEST_IDX respectively, and
1703 the edge having flags equal to EXPECTED_FLAGS. */
1705 #define ASSERT_EDGE(EDGE, EXPECTED_SRC_IDX, EXPECTED_DEST_IDX, \
1707 assert_edge_at (SELFTEST_LOCATION, EDGE, EXPECTED_SRC_IDX, \
1708 EXPECTED_DEST_IDX, EXPECTED_FLAGS)
1710 /* Verify that we can load RTL dumps. */
1713 test_loading_dump_fragment_1 ()
1715 // TODO: filter on target?
1716 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("asr_div1.rtl"));
1718 /* Verify that the insns were loaded correctly. */
1719 rtx_insn
*insn_1
= get_insns ();
1720 ASSERT_TRUE (insn_1
);
1721 ASSERT_EQ (1, INSN_UID (insn_1
));
1722 ASSERT_EQ (INSN
, GET_CODE (insn_1
));
1723 ASSERT_EQ (SET
, GET_CODE (PATTERN (insn_1
)));
1724 ASSERT_EQ (NULL
, PREV_INSN (insn_1
));
1726 rtx_insn
*insn_2
= NEXT_INSN (insn_1
);
1727 ASSERT_TRUE (insn_2
);
1728 ASSERT_EQ (2, INSN_UID (insn_2
));
1729 ASSERT_EQ (INSN
, GET_CODE (insn_2
));
1730 ASSERT_EQ (insn_1
, PREV_INSN (insn_2
));
1731 ASSERT_EQ (NULL
, NEXT_INSN (insn_2
));
1733 /* Verify that registers were loaded correctly. */
1734 rtx insn_1_dest
= SET_DEST (PATTERN (insn_1
));
1735 ASSERT_EQ (REG
, GET_CODE (insn_1_dest
));
1736 ASSERT_EQ ((LAST_VIRTUAL_REGISTER
+ 1) + 2, REGNO (insn_1_dest
));
1737 rtx insn_1_src
= SET_SRC (PATTERN (insn_1
));
1738 ASSERT_EQ (LSHIFTRT
, GET_CODE (insn_1_src
));
1739 rtx reg
= XEXP (insn_1_src
, 0);
1740 ASSERT_EQ (REG
, GET_CODE (reg
));
1741 ASSERT_EQ (LAST_VIRTUAL_REGISTER
+ 1, REGNO (reg
));
1743 /* Verify that get_insn_by_uid works. */
1744 ASSERT_EQ (insn_1
, get_insn_by_uid (1));
1745 ASSERT_EQ (insn_2
, get_insn_by_uid (2));
1747 /* Verify that basic blocks were created. */
1748 ASSERT_EQ (2, BLOCK_FOR_INSN (insn_1
)->index
);
1749 ASSERT_EQ (2, BLOCK_FOR_INSN (insn_2
)->index
);
1751 /* Verify that the CFG was recreated. */
1753 verify_three_block_rtl_cfg (cfun
);
1754 basic_block bb2
= BASIC_BLOCK_FOR_FN (cfun
, 2);
1755 ASSERT_TRUE (bb2
!= NULL
);
1756 ASSERT_EQ (BB_RTL
, bb2
->flags
& BB_RTL
);
1757 ASSERT_EQ (2, bb2
->index
);
1758 ASSERT_EQ (insn_1
, BB_HEAD (bb2
));
1759 ASSERT_EQ (insn_2
, BB_END (bb2
));
1762 /* Verify loading another RTL dump. */
1765 test_loading_dump_fragment_2 ()
1767 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("simple-cse.rtl"));
1769 rtx_insn
*insn_1
= get_insn_by_uid (1);
1770 rtx_insn
*insn_2
= get_insn_by_uid (2);
1771 rtx_insn
*insn_3
= get_insn_by_uid (3);
1773 rtx set1
= single_set (insn_1
);
1774 ASSERT_NE (NULL
, set1
);
1775 rtx set2
= single_set (insn_2
);
1776 ASSERT_NE (NULL
, set2
);
1777 rtx set3
= single_set (insn_3
);
1778 ASSERT_NE (NULL
, set3
);
1780 rtx src1
= SET_SRC (set1
);
1781 ASSERT_EQ (PLUS
, GET_CODE (src1
));
1783 rtx src2
= SET_SRC (set2
);
1784 ASSERT_EQ (PLUS
, GET_CODE (src2
));
1786 /* Both src1 and src2 refer to "(reg:SI %0)".
1787 Verify that we have pointer equality. */
1788 rtx lhs1
= XEXP (src1
, 0);
1789 rtx lhs2
= XEXP (src2
, 0);
1790 ASSERT_EQ (lhs1
, lhs2
);
1792 /* Verify that the CFG was recreated. */
1794 verify_three_block_rtl_cfg (cfun
);
1797 /* Verify that CODE_LABEL insns are loaded correctly. */
1800 test_loading_labels ()
1802 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("example-labels.rtl"));
1804 rtx_insn
*insn_100
= get_insn_by_uid (100);
1805 ASSERT_EQ (CODE_LABEL
, GET_CODE (insn_100
));
1806 ASSERT_EQ (100, INSN_UID (insn_100
));
1807 ASSERT_EQ (NULL
, LABEL_NAME (insn_100
));
1808 ASSERT_EQ (0, LABEL_NUSES (insn_100
));
1809 ASSERT_EQ (30, CODE_LABEL_NUMBER (insn_100
));
1811 rtx_insn
*insn_200
= get_insn_by_uid (200);
1812 ASSERT_EQ (CODE_LABEL
, GET_CODE (insn_200
));
1813 ASSERT_EQ (200, INSN_UID (insn_200
));
1814 ASSERT_STREQ ("some_label_name", LABEL_NAME (insn_200
));
1815 ASSERT_EQ (0, LABEL_NUSES (insn_200
));
1816 ASSERT_EQ (40, CODE_LABEL_NUMBER (insn_200
));
1818 /* Ensure that the presence of CODE_LABEL_NUMBER == 40
1819 means that the next label num to be handed out will be 41. */
1820 ASSERT_EQ (41, max_label_num ());
1822 /* Ensure that label names read from a dump are GC-managed
1823 and are found through the insn. */
1824 forcibly_ggc_collect ();
1825 ASSERT_TRUE (ggc_marked_p (insn_200
));
1826 ASSERT_TRUE (ggc_marked_p (LABEL_NAME (insn_200
)));
1829 /* Verify that the loader copes with an insn with a mode. */
1832 test_loading_insn_with_mode ()
1834 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("insn-with-mode.rtl"));
1835 rtx_insn
*insn
= get_insns ();
1836 ASSERT_EQ (INSN
, GET_CODE (insn
));
1838 /* Verify that the "TI" mode was set from "insn:TI". */
1839 ASSERT_EQ (TImode
, GET_MODE (insn
));
1842 /* Verify that the loader copes with a jump_insn to a label_ref. */
1845 test_loading_jump_to_label_ref ()
1847 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("jump-to-label-ref.rtl"));
1849 rtx_insn
*jump_insn
= get_insn_by_uid (1);
1850 ASSERT_EQ (JUMP_INSN
, GET_CODE (jump_insn
));
1852 rtx_insn
*barrier
= get_insn_by_uid (2);
1853 ASSERT_EQ (BARRIER
, GET_CODE (barrier
));
1855 rtx_insn
*code_label
= get_insn_by_uid (100);
1856 ASSERT_EQ (CODE_LABEL
, GET_CODE (code_label
));
1858 /* Verify the jump_insn. */
1859 ASSERT_EQ (4, BLOCK_FOR_INSN (jump_insn
)->index
);
1860 ASSERT_EQ (SET
, GET_CODE (PATTERN (jump_insn
)));
1861 /* Ensure that the "(pc)" is using the global singleton. */
1862 ASSERT_RTX_PTR_EQ (pc_rtx
, SET_DEST (PATTERN (jump_insn
)));
1863 rtx label_ref
= SET_SRC (PATTERN (jump_insn
));
1864 ASSERT_EQ (LABEL_REF
, GET_CODE (label_ref
));
1865 ASSERT_EQ (code_label
, label_ref_label (label_ref
));
1866 ASSERT_EQ (code_label
, JUMP_LABEL (jump_insn
));
1868 /* Verify the code_label. */
1869 ASSERT_EQ (5, BLOCK_FOR_INSN (code_label
)->index
);
1870 ASSERT_EQ (NULL
, LABEL_NAME (code_label
));
1871 ASSERT_EQ (1, LABEL_NUSES (code_label
));
1873 /* Verify the generated CFG. */
1875 /* Locate blocks. */
1876 basic_block entry
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
1877 ASSERT_TRUE (entry
!= NULL
);
1878 ASSERT_EQ (ENTRY_BLOCK
, entry
->index
);
1880 basic_block exit
= EXIT_BLOCK_PTR_FOR_FN (cfun
);
1881 ASSERT_TRUE (exit
!= NULL
);
1882 ASSERT_EQ (EXIT_BLOCK
, exit
->index
);
1884 basic_block bb4
= (*cfun
->cfg
->x_basic_block_info
)[4];
1885 basic_block bb5
= (*cfun
->cfg
->x_basic_block_info
)[5];
1886 ASSERT_EQ (4, bb4
->index
);
1887 ASSERT_EQ (5, bb5
->index
);
1890 ASSERT_EQ (NULL
, entry
->preds
);
1891 ASSERT_EQ (1, entry
->succs
->length ());
1892 ASSERT_EDGE ((*entry
->succs
)[0], 0, 4, EDGE_FALLTHRU
);
1895 ASSERT_EQ (1, bb4
->preds
->length ());
1896 ASSERT_EDGE ((*bb4
->preds
)[0], 0, 4, EDGE_FALLTHRU
);
1897 ASSERT_EQ (1, bb4
->succs
->length ());
1898 ASSERT_EDGE ((*bb4
->succs
)[0], 4, 5, 0x0);
1901 ASSERT_EQ (1, bb5
->preds
->length ());
1902 ASSERT_EDGE ((*bb5
->preds
)[0], 4, 5, 0x0);
1903 ASSERT_EQ (1, bb5
->succs
->length ());
1904 ASSERT_EDGE ((*bb5
->succs
)[0], 5, 1, EDGE_FALLTHRU
);
1907 ASSERT_EQ (1, exit
->preds
->length ());
1908 ASSERT_EDGE ((*exit
->preds
)[0], 5, 1, EDGE_FALLTHRU
);
1909 ASSERT_EQ (NULL
, exit
->succs
);
1912 /* Verify that the loader copes with a jump_insn to a label_ref
1916 test_loading_jump_to_return ()
1918 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("jump-to-return.rtl"));
1920 rtx_insn
*jump_insn
= get_insn_by_uid (1);
1921 ASSERT_EQ (JUMP_INSN
, GET_CODE (jump_insn
));
1922 ASSERT_RTX_PTR_EQ (ret_rtx
, JUMP_LABEL (jump_insn
));
1925 /* Verify that the loader copes with a jump_insn to a label_ref
1926 marked "simple_return". */
1929 test_loading_jump_to_simple_return ()
1931 rtl_dump_test
t (SELFTEST_LOCATION
,
1932 locate_file ("jump-to-simple-return.rtl"));
1934 rtx_insn
*jump_insn
= get_insn_by_uid (1);
1935 ASSERT_EQ (JUMP_INSN
, GET_CODE (jump_insn
));
1936 ASSERT_RTX_PTR_EQ (simple_return_rtx
, JUMP_LABEL (jump_insn
));
1939 /* Verify that the loader copes with a NOTE_INSN_BASIC_BLOCK. */
1942 test_loading_note_insn_basic_block ()
1944 rtl_dump_test
t (SELFTEST_LOCATION
,
1945 locate_file ("note_insn_basic_block.rtl"));
1947 rtx_insn
*note
= get_insn_by_uid (1);
1948 ASSERT_EQ (NOTE
, GET_CODE (note
));
1949 ASSERT_EQ (2, BLOCK_FOR_INSN (note
)->index
);
1951 ASSERT_EQ (NOTE_INSN_BASIC_BLOCK
, NOTE_KIND (note
));
1952 ASSERT_EQ (2, NOTE_BASIC_BLOCK (note
)->index
);
1953 ASSERT_EQ (BASIC_BLOCK_FOR_FN (cfun
, 2), NOTE_BASIC_BLOCK (note
));
1956 /* Verify that the loader copes with a NOTE_INSN_DELETED. */
1959 test_loading_note_insn_deleted ()
1961 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("note-insn-deleted.rtl"));
1963 rtx_insn
*note
= get_insn_by_uid (1);
1964 ASSERT_EQ (NOTE
, GET_CODE (note
));
1965 ASSERT_EQ (NOTE_INSN_DELETED
, NOTE_KIND (note
));
1968 /* Verify that the const_int values are consolidated, since
1969 pointer equality corresponds to value equality.
1970 TODO: do this for all in CASE_CONST_UNIQUE. */
1973 test_loading_const_int ()
1975 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("const-int.rtl"));
1977 /* Verify that const_int values below MAX_SAVED_CONST_INT use
1978 the global values. */
1979 ASSERT_EQ (const0_rtx
, SET_SRC (PATTERN (get_insn_by_uid (1))));
1980 ASSERT_EQ (const1_rtx
, SET_SRC (PATTERN (get_insn_by_uid (2))));
1981 ASSERT_EQ (constm1_rtx
, SET_SRC (PATTERN (get_insn_by_uid (3))));
1983 /* Verify that other const_int values are consolidated. */
1984 rtx int256
= gen_rtx_CONST_INT (SImode
, 256);
1985 ASSERT_EQ (int256
, SET_SRC (PATTERN (get_insn_by_uid (4))));
1988 /* Verify that the loader copes with a SYMBOL_REF. */
1991 test_loading_symbol_ref ()
1993 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("symbol-ref.rtl"));
1995 rtx_insn
*insn
= get_insns ();
1997 rtx high
= SET_SRC (PATTERN (insn
));
1998 ASSERT_EQ (HIGH
, GET_CODE (high
));
2000 rtx symbol_ref
= XEXP (high
, 0);
2001 ASSERT_EQ (SYMBOL_REF
, GET_CODE (symbol_ref
));
2003 /* Verify that "[flags 0xc0]" was parsed. */
2004 ASSERT_EQ (0xc0, SYMBOL_REF_FLAGS (symbol_ref
));
2005 /* TODO: we don't yet load SYMBOL_REF_DECL. */
2008 /* Verify that the loader can rebuild a CFG. */
2013 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("cfg-test.rtl"));
2015 ASSERT_STREQ ("cfg_test", IDENTIFIER_POINTER (DECL_NAME (cfun
->decl
)));
2019 ASSERT_TRUE (cfun
->cfg
!= NULL
);
2020 ASSERT_EQ (6, n_basic_blocks_for_fn (cfun
));
2021 ASSERT_EQ (6, n_edges_for_fn (cfun
));
2023 /* The "fake" basic blocks. */
2024 basic_block entry
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
2025 ASSERT_TRUE (entry
!= NULL
);
2026 ASSERT_EQ (ENTRY_BLOCK
, entry
->index
);
2028 basic_block exit
= EXIT_BLOCK_PTR_FOR_FN (cfun
);
2029 ASSERT_TRUE (exit
!= NULL
);
2030 ASSERT_EQ (EXIT_BLOCK
, exit
->index
);
2032 /* The "real" basic blocks. */
2033 basic_block bb2
= (*cfun
->cfg
->x_basic_block_info
)[2];
2034 basic_block bb3
= (*cfun
->cfg
->x_basic_block_info
)[3];
2035 basic_block bb4
= (*cfun
->cfg
->x_basic_block_info
)[4];
2036 basic_block bb5
= (*cfun
->cfg
->x_basic_block_info
)[5];
2038 ASSERT_EQ (2, bb2
->index
);
2039 ASSERT_EQ (3, bb3
->index
);
2040 ASSERT_EQ (4, bb4
->index
);
2041 ASSERT_EQ (5, bb5
->index
);
2043 /* Verify connectivity. */
2046 ASSERT_EQ (NULL
, entry
->preds
);
2047 ASSERT_EQ (1, entry
->succs
->length ());
2048 ASSERT_EDGE ((*entry
->succs
)[0], 0, 2, EDGE_FALLTHRU
);
2051 ASSERT_EQ (1, bb2
->preds
->length ());
2052 ASSERT_EDGE ((*bb2
->preds
)[0], 0, 2, EDGE_FALLTHRU
);
2053 ASSERT_EQ (2, bb2
->succs
->length ());
2054 ASSERT_EDGE ((*bb2
->succs
)[0], 2, 3, EDGE_TRUE_VALUE
);
2055 ASSERT_EDGE ((*bb2
->succs
)[1], 2, 4, EDGE_FALSE_VALUE
);
2058 ASSERT_EQ (1, bb3
->preds
->length ());
2059 ASSERT_EDGE ((*bb3
->preds
)[0], 2, 3, EDGE_TRUE_VALUE
);
2060 ASSERT_EQ (1, bb3
->succs
->length ());
2061 ASSERT_EDGE ((*bb3
->succs
)[0], 3, 5, EDGE_FALLTHRU
);
2064 ASSERT_EQ (1, bb4
->preds
->length ());
2065 ASSERT_EDGE ((*bb4
->preds
)[0], 2, 4, EDGE_FALSE_VALUE
);
2066 ASSERT_EQ (1, bb4
->succs
->length ());
2067 ASSERT_EDGE ((*bb4
->succs
)[0], 4, 5, EDGE_FALLTHRU
);
2070 ASSERT_EQ (2, bb5
->preds
->length ());
2071 ASSERT_EDGE ((*bb5
->preds
)[0], 3, 5, EDGE_FALLTHRU
);
2072 ASSERT_EDGE ((*bb5
->preds
)[1], 4, 5, EDGE_FALLTHRU
);
2073 ASSERT_EQ (1, bb5
->succs
->length ());
2074 ASSERT_EDGE ((*bb5
->succs
)[0], 5, 1, EDGE_FALLTHRU
);
2077 ASSERT_EQ (1, exit
->preds
->length ());
2078 ASSERT_EDGE ((*exit
->preds
)[0], 5, 1, EDGE_FALLTHRU
);
2079 ASSERT_EQ (NULL
, exit
->succs
);
2082 /* Verify that the loader copes with sparse block indices.
2083 This testcase loads a file with a "(block 42)". */
2086 test_loading_bb_index ()
2088 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("bb-index.rtl"));
2090 ASSERT_STREQ ("test_bb_index", IDENTIFIER_POINTER (DECL_NAME (cfun
->decl
)));
2094 ASSERT_TRUE (cfun
->cfg
!= NULL
);
2095 ASSERT_EQ (3, n_basic_blocks_for_fn (cfun
));
2096 ASSERT_EQ (43, basic_block_info_for_fn (cfun
)->length ());
2097 ASSERT_EQ (2, n_edges_for_fn (cfun
));
2099 ASSERT_EQ (NULL
, (*cfun
->cfg
->x_basic_block_info
)[41]);
2100 basic_block bb42
= (*cfun
->cfg
->x_basic_block_info
)[42];
2101 ASSERT_NE (NULL
, bb42
);
2102 ASSERT_EQ (42, bb42
->index
);
2105 /* Verify that function_reader::handle_any_trailing_information correctly
2106 parses all the possible items emitted for a MEM. */
2111 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("mem.rtl"));
2113 ASSERT_STREQ ("test_mem", IDENTIFIER_POINTER (DECL_NAME (cfun
->decl
)));
2116 /* Verify parsing of "[42 i+17 S8 A128 AS5]". */
2117 rtx_insn
*insn_1
= get_insn_by_uid (1);
2118 rtx set1
= single_set (insn_1
);
2119 rtx mem1
= SET_DEST (set1
);
2120 ASSERT_EQ (42, MEM_ALIAS_SET (mem1
));
2122 ASSERT_TRUE (MEM_OFFSET_KNOWN_P (mem1
));
2123 ASSERT_EQ (17, MEM_OFFSET (mem1
));
2125 ASSERT_EQ (8, MEM_SIZE (mem1
));
2127 ASSERT_EQ (128, MEM_ALIGN (mem1
));
2129 ASSERT_EQ (5, MEM_ADDR_SPACE (mem1
));
2131 /* Verify parsing of "43 i+18 S9 AS6"
2132 (an address space without an alignment). */
2133 rtx_insn
*insn_2
= get_insn_by_uid (2);
2134 rtx set2
= single_set (insn_2
);
2135 rtx mem2
= SET_DEST (set2
);
2136 ASSERT_EQ (43, MEM_ALIAS_SET (mem2
));
2138 ASSERT_TRUE (MEM_OFFSET_KNOWN_P (mem2
));
2139 ASSERT_EQ (18, MEM_OFFSET (mem2
));
2141 ASSERT_EQ (9, MEM_SIZE (mem2
));
2143 ASSERT_EQ (6, MEM_ADDR_SPACE (mem2
));
2146 /* Run all of the selftests within this file. */
2149 read_rtl_function_c_tests ()
2152 test_parsing_regnos ();
2153 test_loading_dump_fragment_1 ();
2154 test_loading_dump_fragment_2 ();
2155 test_loading_labels ();
2156 test_loading_insn_with_mode ();
2157 test_loading_jump_to_label_ref ();
2158 test_loading_jump_to_return ();
2159 test_loading_jump_to_simple_return ();
2160 test_loading_note_insn_basic_block ();
2161 test_loading_note_insn_deleted ();
2162 test_loading_const_int ();
2163 test_loading_symbol_ref ();
2164 test_loading_cfg ();
2165 test_loading_bb_index ();
2166 test_loading_mem ();
2169 } // namespace selftest
2171 #endif /* #if CHECKING_P */