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'");
409 error ("%<__RTL%> function cannot be compiled with %<-flto%>");
414 /* Parse the output of print_rtx_function (or hand-written data in the
415 same format), having already parsed the "(function" heading, and
416 finishing immediately before the final ")".
418 The "param" and "crtl" clauses are optional. */
421 function_reader::parse_function ()
423 m_name
= xstrdup (read_string (0));
429 int c
= read_skip_spaces ();
437 file_location loc
= get_current_location ();
438 struct md_name directive
;
439 read_name (&directive
);
440 if (strcmp (directive
.string
, "param") == 0)
442 else if (strcmp (directive
.string
, "insn-chain") == 0)
444 else if (strcmp (directive
.string
, "crtl") == 0)
447 fatal_with_file_and_line ("unrecognized directive: %s",
455 /* Rebuild the JUMP_LABEL field of any JUMP_INSNs in the chain, and the
456 LABEL_NUSES of any CODE_LABELs.
458 This has to happen after apply_fixups, since only after then do
459 LABEL_REFs have their label_ref_label set up. */
460 rebuild_jump_labels (get_insns ());
462 crtl
->init_stack_alignment ();
465 /* Set up state for the function *before* fixups are applied.
467 Create "cfun" and a decl for the function.
468 By default, every function decl is hardcoded as
469 int test_1 (int i, int j, int k);
470 Set up various other state:
471 - the cfg and basic blocks (edges are created later, *after* fixups
473 - add the function to the callgraph. */
476 function_reader::create_function ()
478 /* We start in cfgrtl mode, rather than cfglayout mode. */
479 rtl_register_cfg_hooks ();
481 /* When run from selftests or "rtl1", cfun is NULL.
482 When run from "cc1" for a C function tagged with __RTL, cfun is the
486 tree fn_name
= get_identifier (m_name
? m_name
: "test_1");
487 tree int_type
= integer_type_node
;
488 tree return_type
= int_type
;
489 tree arg_types
[3] = {int_type
, int_type
, int_type
};
490 tree fn_type
= build_function_type_array (return_type
, 3, arg_types
);
491 tree fndecl
= build_decl (UNKNOWN_LOCATION
, FUNCTION_DECL
, fn_name
, fn_type
);
492 tree resdecl
= build_decl (UNKNOWN_LOCATION
, RESULT_DECL
, NULL_TREE
,
494 DECL_ARTIFICIAL (resdecl
) = 1;
495 DECL_IGNORED_P (resdecl
) = 1;
496 DECL_RESULT (fndecl
) = resdecl
;
497 allocate_struct_function (fndecl
, false);
498 /* This sets cfun. */
499 current_function_decl
= fndecl
;
503 gcc_assert (current_function_decl
);
504 tree fndecl
= current_function_decl
;
506 /* Mark this function as being specified as __RTL. */
507 cfun
->curr_properties
|= PROP_rtl
;
509 /* cc1 normally inits DECL_INITIAL (fndecl) to be error_mark_node.
510 Create a dummy block for it. */
511 DECL_INITIAL (fndecl
) = make_node (BLOCK
);
513 cfun
->curr_properties
= (PROP_cfg
| PROP_rtl
);
515 /* Do we need this to force cgraphunit.c to output the function? */
516 DECL_EXTERNAL (fndecl
) = 0;
517 DECL_PRESERVE_P (fndecl
) = 1;
520 cgraph_node::finalize_function (fndecl
, false);
522 /* Create bare-bones cfg. This creates the entry and exit blocks. */
523 init_empty_tree_cfg_for_function (cfun
);
524 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->flags
|= BB_RTL
;
525 EXIT_BLOCK_PTR_FOR_FN (cfun
)->flags
|= BB_RTL
;
526 init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
527 init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun
));
528 m_bb_to_insert_after
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
532 /* Look within the the params of FNDECL for a param named NAME.
533 Return NULL_TREE if one isn't found. */
536 find_param_by_name (tree fndecl
, const char *name
)
538 for (tree arg
= DECL_ARGUMENTS (fndecl
); arg
; arg
= TREE_CHAIN (arg
))
539 if (strcmp (name
, IDENTIFIER_POINTER (DECL_NAME (arg
))) == 0)
544 /* Parse the content of a "param" directive, having already parsed the
545 "(param". Consume the trailing ')'. */
548 function_reader::parse_param ()
550 require_char_ws ('"');
551 file_location loc
= get_current_location ();
552 char *name
= read_quoted_string ();
554 /* Lookup param by name. */
555 tree t_param
= find_param_by_name (cfun
->decl
, name
);
557 fatal_at (loc
, "param not found: %s", name
);
559 /* Parse DECL_RTL. */
560 require_char_ws ('(');
561 require_word_ws ("DECL_RTL");
562 DECL_WRTL_CHECK (t_param
)->decl_with_rtl
.rtl
= parse_rtx ();
563 require_char_ws (')');
565 /* Parse DECL_RTL_INCOMING. */
566 require_char_ws ('(');
567 require_word_ws ("DECL_RTL_INCOMING");
568 DECL_INCOMING_RTL (t_param
) = parse_rtx ();
569 require_char_ws (')');
571 require_char_ws (')');
574 /* Parse zero or more child insn elements within an
575 "insn-chain" element. Consume the trailing ')'. */
578 function_reader::parse_insn_chain ()
582 int c
= read_skip_spaces ();
583 file_location loc
= get_current_location ();
588 struct md_name directive
;
589 read_name (&directive
);
590 if (strcmp (directive
.string
, "block") == 0)
593 parse_insn (loc
, directive
.string
);
596 fatal_at (loc
, "expected '(' or ')'");
602 /* Parse zero or more child directives (edges and insns) within a
603 "block" directive, having already parsed the "(block " heading.
604 Consume the trailing ')'. */
607 function_reader::parse_block ()
609 /* Parse the index value from the dump. This will be an integer;
610 we don't support "entry" or "exit" here (unlike for edges). */
613 int bb_idx
= atoi (name
.string
);
615 /* The term "index" has two meanings for basic blocks in a CFG:
616 (a) the "index" field within struct basic_block_def.
617 (b) the index of a basic_block within the cfg's x_basic_block_info
618 vector, as accessed via BASIC_BLOCK_FOR_FN.
620 These can get out-of-sync when basic blocks are optimized away.
621 They get back in sync by "compact_blocks".
622 We reconstruct cfun->cfg->x_basic_block_info->m_vecdata with NULL
623 values in it for any missing basic blocks, so that (a) == (b) for
624 all of the blocks we create. The doubly-linked list of basic
625 blocks (next_bb/prev_bb) skips over these "holes". */
627 if (m_highest_bb_idx
< bb_idx
)
628 m_highest_bb_idx
= bb_idx
;
630 size_t new_size
= m_highest_bb_idx
+ 1;
631 if (basic_block_info_for_fn (cfun
)->length () < new_size
)
632 vec_safe_grow_cleared (basic_block_info_for_fn (cfun
), new_size
);
634 last_basic_block_for_fn (cfun
) = new_size
;
636 /* Create the basic block.
638 We can't call create_basic_block and use the regular RTL block-creation
639 hooks, since this creates NOTE_INSN_BASIC_BLOCK instances. We don't
640 want to do that; we want to use the notes we were provided with. */
641 basic_block bb
= alloc_block ();
642 init_rtl_bb_info (bb
);
644 bb
->flags
= BB_NEW
| BB_RTL
;
645 link_block (bb
, m_bb_to_insert_after
);
646 m_bb_to_insert_after
= bb
;
648 n_basic_blocks_for_fn (cfun
)++;
649 SET_BASIC_BLOCK_FOR_FN (cfun
, bb_idx
, bb
);
650 BB_SET_PARTITION (bb
, BB_UNPARTITIONED
);
652 /* Handle insns, edge-from and edge-to directives. */
655 int c
= read_skip_spaces ();
656 file_location loc
= get_current_location ();
661 struct md_name directive
;
662 read_name (&directive
);
663 if (strcmp (directive
.string
, "edge-from") == 0)
664 parse_edge (bb
, true);
665 else if (strcmp (directive
.string
, "edge-to") == 0)
666 parse_edge (bb
, false);
669 rtx_insn
*insn
= parse_insn (loc
, directive
.string
);
670 set_block_for_insn (insn
, bb
);
677 fatal_at (loc
, "expected '(' or ')'");
681 /* Subroutine of function_reader::parse_edge.
682 Parse a basic block index, handling "entry" and "exit". */
685 function_reader::parse_bb_idx ()
689 if (strcmp (name
.string
, "entry") == 0)
691 if (strcmp (name
.string
, "exit") == 0)
693 return atoi (name
.string
);
696 /* Subroutine of parse_edge_flags.
697 Parse TOK, a token such as "FALLTHRU", converting to the flag value.
698 Issue an error if the token is unrecognized. */
701 parse_edge_flag_token (const char *tok
)
703 #define DEF_EDGE_FLAG(NAME,IDX) \
705 if (strcmp (tok, #NAME) == 0) \
706 return EDGE_##NAME; \
708 #include "cfg-flags.def"
710 error ("unrecognized edge flag: '%s'", tok
);
714 /* Subroutine of function_reader::parse_edge.
715 Parse STR and convert to a flag value (or issue an error).
716 The parser uses strtok and hence modifiers STR in-place. */
719 parse_edge_flags (char *str
)
723 char *tok
= strtok (str
, "| ");
726 result
|= parse_edge_flag_token (tok
);
727 tok
= strtok (NULL
, "| ");
733 /* Parse an "edge-from" or "edge-to" directive within the "block"
734 directive for BLOCK, having already parsed the "(edge" heading.
735 Consume the final ")". Record the edge within m_deferred_edges.
736 FROM is true for an "edge-from" directive, false for an "edge-to"
740 function_reader::parse_edge (basic_block block
, bool from
)
743 int this_bb_idx
= block
->index
;
744 file_location loc
= get_current_location ();
745 int other_bb_idx
= parse_bb_idx ();
747 /* "(edge-from 2)" means src = 2, dest = this_bb_idx, whereas
748 "(edge-to 3)" means src = this_bb_idx, dest = 3. */
749 int src_idx
= from
? other_bb_idx
: this_bb_idx
;
750 int dest_idx
= from
? this_bb_idx
: other_bb_idx
;
752 /* Optional "(flags)". */
754 int c
= read_skip_spaces ();
757 require_word_ws ("flags");
758 require_char_ws ('"');
759 char *str
= read_quoted_string ();
760 flags
= parse_edge_flags (str
);
761 require_char_ws (')');
766 require_char_ws (')');
768 /* This BB already exists, but the other BB might not yet.
769 For now, save the edges, and create them at the end of insn-chain
771 /* For now, only process the (edge-from) to this BB, and (edge-to)
772 that go to the exit block.
773 FIXME: we don't yet verify that the edge-from and edge-to directives
775 if (from
|| dest_idx
== EXIT_BLOCK
)
776 m_deferred_edges
.safe_push (deferred_edge (loc
, src_idx
, dest_idx
, flags
));
779 /* Parse an rtx instruction, having parsed the opening and parenthesis, and
780 name NAME, seen at START_LOC, by calling read_rtx_code, calling
781 set_first_insn and set_last_insn as appropriate, and
782 adding the insn to the insn chain.
783 Consume the trailing ')'. */
786 function_reader::parse_insn (file_location start_loc
, const char *name
)
788 rtx x
= read_rtx_code (name
);
790 fatal_at (start_loc
, "expected insn type; got '%s'", name
);
791 rtx_insn
*insn
= dyn_cast
<rtx_insn
*> (x
);
793 fatal_at (start_loc
, "expected insn type; got '%s'", name
);
795 /* Consume the trailing ')'. */
796 require_char_ws (')');
798 rtx_insn
*last_insn
= get_last_insn ();
800 /* Add "insn" to the insn chain. */
803 gcc_assert (NEXT_INSN (last_insn
) == NULL
);
804 SET_NEXT_INSN (last_insn
) = insn
;
806 SET_PREV_INSN (insn
) = last_insn
;
808 /* Add it to the sequence. */
809 set_last_insn (insn
);
813 set_first_insn (insn
);
816 if (rtx_code_label
*label
= dyn_cast
<rtx_code_label
*> (insn
))
817 maybe_set_max_label_num (label
);
822 /* Postprocessing subroutine for parse_insn_chain: all the basic blocks
823 should have been created by now; create the edges that were seen. */
826 function_reader::create_edges ()
830 FOR_EACH_VEC_ELT (m_deferred_edges
, i
, de
)
832 /* The BBs should already have been created by parse_block. */
833 basic_block src
= BASIC_BLOCK_FOR_FN (cfun
, de
->m_src_bb_idx
);
835 fatal_at (de
->m_loc
, "error: block index %i not found",
837 basic_block dst
= BASIC_BLOCK_FOR_FN (cfun
, de
->m_dest_bb_idx
);
839 fatal_at (de
->m_loc
, "error: block with index %i not found",
841 unchecked_make_edge (src
, dst
, de
->m_flags
);
845 /* Parse a "crtl" directive, having already parsed the "(crtl" heading
847 Consume the final ")". */
850 function_reader::parse_crtl (file_location loc
)
852 if (m_have_crtl_directive
)
853 error_at (loc
, "more than one 'crtl' directive");
854 m_have_crtl_directive
= true;
857 require_char_ws ('(');
858 require_word_ws ("return_rtx");
859 crtl
->return_rtx
= parse_rtx ();
860 require_char_ws (')');
862 require_char_ws (')');
865 /* Parse operand IDX of X, returning X, or an equivalent rtx
866 expression (for consolidating singletons).
867 This is an overridden implementation of rtx_reader::read_rtx_operand for
868 function_reader, handling various extra data printed by print_rtx,
869 and sometimes calling the base class implementation. */
872 function_reader::read_rtx_operand (rtx x
, int idx
)
874 RTX_CODE code
= GET_CODE (x
);
875 const char *format_ptr
= GET_RTX_FORMAT (code
);
876 const char format_char
= format_ptr
[idx
];
879 /* Override the regular parser for some format codes. */
883 if (idx
== 7 && CALL_P (x
))
885 m_in_call_function_usage
= true;
886 return rtx_reader::read_rtx_operand (x
, idx
);
887 m_in_call_function_usage
= false;
890 return rtx_reader::read_rtx_operand (x
, idx
);
894 read_rtx_operand_u (x
, idx
);
895 /* Don't run regular parser for 'u'. */
900 read_rtx_operand_i_or_n (x
, idx
, format_char
);
901 /* Don't run regular parser for these codes. */
905 gcc_assert (is_compact ());
906 /* Compact mode doesn't store BBs. */
907 /* Don't run regular parser. */
911 /* Don't run regular parser for 'r'. */
912 return read_rtx_operand_r (x
);
918 /* Call base class implementation. */
919 x
= rtx_reader::read_rtx_operand (x
, idx
);
921 /* Handle any additional parsing needed to handle what the dump
926 extra_parsing_for_operand_code_0 (x
, idx
);
932 /* Strip away the redundant hex dump of the value. */
933 require_char_ws ('[');
935 require_char_ws (']');
946 /* Parse operand IDX of X, of code 'u', when reading function dumps.
948 The RTL file recorded the ID of an insn (or 0 for NULL); we
949 must store this as a pointer, but the insn might not have
950 been loaded yet. Store the ID away for now, via a fixup. */
953 function_reader::read_rtx_operand_u (rtx x
, int idx
)
955 /* In compact mode, the PREV/NEXT insn uids are not dumped, so skip
956 the "uu" when reading. */
957 if (is_compact () && GET_CODE (x
) != LABEL_REF
)
961 file_location loc
= read_name (&name
);
962 int insn_id
= atoi (name
.string
);
964 add_fixup_insn_uid (loc
, x
, idx
, insn_id
);
967 /* Read a name, looking for a match against a string found in array
968 STRINGS of size NUM_VALUES.
969 Return the index of the the matched string, or emit an error. */
972 function_reader::parse_enum_value (int num_values
, const char *const *strings
)
976 for (int i
= 0; i
< num_values
; i
++)
978 if (strcmp (name
.string
, strings
[i
]) == 0)
981 error ("unrecognized enum value: '%s'", name
.string
);
985 /* Parse operand IDX of X, of code 'i' or 'n' (as specified by FORMAT_CHAR).
986 Special-cased handling of these, for reading function dumps. */
989 function_reader::read_rtx_operand_i_or_n (rtx x
, int idx
,
992 /* Handle some of the extra information that print_rtx
993 can write out for these cases. */
994 /* print_rtx only writes out operand 5 for notes
995 for NOTE_KIND values NOTE_INSN_DELETED_LABEL
996 and NOTE_INSN_DELETED_DEBUG_LABEL. */
997 if (idx
== 5 && NOTE_P (x
))
1000 if (idx
== 4 && INSN_P (x
))
1002 maybe_read_location (as_a
<rtx_insn
*> (x
));
1006 /* INSN_CODEs aren't printed in compact mode, so don't attempt to
1010 && &INSN_CODE (x
) == &XINT (x
, idx
))
1016 /* Handle UNSPEC and UNSPEC_VOLATILE's operand 1. */
1017 #if !defined(GENERATOR_FILE) && NUM_UNSPECV_VALUES > 0
1019 && GET_CODE (x
) == UNSPEC_VOLATILE
)
1022 = parse_enum_value (NUM_UNSPECV_VALUES
, unspecv_strings
);
1026 #if !defined(GENERATOR_FILE) && NUM_UNSPEC_VALUES > 0
1028 && (GET_CODE (x
) == UNSPEC
1029 || GET_CODE (x
) == UNSPEC_VOLATILE
))
1032 = parse_enum_value (NUM_UNSPEC_VALUES
, unspec_strings
);
1037 struct md_name name
;
1040 if (format_char
== 'n')
1041 value
= parse_note_insn_name (name
.string
);
1043 value
= atoi (name
.string
);
1044 XINT (x
, idx
) = value
;
1047 /* Parse the 'r' operand of X, returning X, or an equivalent rtx
1048 expression (for consolidating singletons).
1049 Special-cased handling of code 'r' for reading function dumps. */
1052 function_reader::read_rtx_operand_r (rtx x
)
1054 struct md_name name
;
1055 file_location loc
= read_name (&name
);
1056 int regno
= lookup_reg_by_dump_name (name
.string
);
1058 fatal_at (loc
, "unrecognized register: '%s'", name
.string
);
1060 set_regno_raw (x
, regno
, 1);
1062 /* Consolidate singletons. */
1063 x
= consolidate_singletons (x
);
1065 ORIGINAL_REGNO (x
) = regno
;
1067 /* Parse extra stuff at end of 'r'.
1068 We may have zero, one, or two sections marked by square
1070 int ch
= read_skip_spaces ();
1071 bool expect_original_regno
= false;
1074 file_location loc
= get_current_location ();
1075 char *desc
= read_until ("]", true);
1076 strip_trailing_whitespace (desc
);
1077 const char *desc_start
= desc
;
1078 /* If ORIGINAL_REGNO (rtx) != regno, we will have:
1079 "orig:%i", ORIGINAL_REGNO (rtx).
1080 Consume it, we don't set ORIGINAL_REGNO, since we can
1081 get that from the 2nd copy later. */
1082 if (0 == strncmp (desc
, "orig:", 5))
1084 expect_original_regno
= true;
1086 /* Skip to any whitespace following the integer. */
1087 const char *space
= strchr (desc_start
, ' ');
1089 desc_start
= space
+ 1;
1091 /* Any remaining text may be the REG_EXPR. Alternatively we have
1092 no REG_ATTRS, and instead we have ORIGINAL_REGNO. */
1093 if (ISDIGIT (*desc_start
))
1095 /* Assume we have ORIGINAL_REGNO. */
1096 ORIGINAL_REGNO (x
) = atoi (desc_start
);
1100 /* Assume we have REG_EXPR. */
1101 add_fixup_expr (loc
, x
, desc_start
);
1107 if (expect_original_regno
)
1109 require_char_ws ('[');
1110 char *desc
= read_until ("]", true);
1111 ORIGINAL_REGNO (x
) = atoi (desc
);
1118 /* Additional parsing for format code '0' in dumps, handling a variety
1119 of special-cases in print_rtx, when parsing operand IDX of X. */
1122 function_reader::extra_parsing_for_operand_code_0 (rtx x
, int idx
)
1124 RTX_CODE code
= GET_CODE (x
);
1126 struct md_name name
;
1128 if (idx
== 1 && code
== SYMBOL_REF
)
1130 /* Possibly wrote " [flags %#x]", SYMBOL_REF_FLAGS (in_rtx). */
1131 c
= read_skip_spaces ();
1134 file_location loc
= read_name (&name
);
1135 if (strcmp (name
.string
, "flags"))
1136 error_at (loc
, "was expecting `%s'", "flags");
1138 SYMBOL_REF_FLAGS (x
) = strtol (name
.string
, NULL
, 16);
1140 /* We can't reconstruct SYMBOL_REF_BLOCK; set it to NULL. */
1141 if (SYMBOL_REF_HAS_BLOCK_INFO_P (x
))
1142 SYMBOL_REF_BLOCK (x
) = NULL
;
1149 /* If X had a non-NULL SYMBOL_REF_DECL,
1150 rtx_writer::print_rtx_operand_code_0 would have dumped it
1151 using print_node_brief.
1152 Skip the content for now. */
1153 c
= read_skip_spaces ();
1158 char ch
= read_char ();
1166 else if (idx
== 3 && code
== NOTE
)
1168 /* Note-specific data appears for operand 3, which annoyingly
1169 is before the enum specifying which kind of note we have
1171 c
= read_skip_spaces ();
1174 /* Possibly data for a NOTE_INSN_BASIC_BLOCK, of the form:
1176 file_location bb_loc
= read_name (&name
);
1177 if (strcmp (name
.string
, "bb"))
1178 error_at (bb_loc
, "was expecting `%s'", "bb");
1180 int bb_idx
= atoi (name
.string
);
1181 add_fixup_note_insn_basic_block (bb_loc
, x
, idx
,
1183 require_char_ws (']');
1190 /* Implementation of rtx_reader::handle_any_trailing_information.
1191 Handle the various additional information that print-rtl.c can
1192 write after the regular fields, when parsing X. */
1195 function_reader::handle_any_trailing_information (rtx x
)
1197 struct md_name name
;
1199 switch (GET_CODE (x
))
1204 require_char_ws ('[');
1206 set_mem_alias_set (x
, atoi (name
.string
));
1207 /* We have either a MEM_EXPR, or a space. */
1208 if (peek_char () != ' ')
1210 file_location loc
= get_current_location ();
1211 char *desc
= read_until (" +", false);
1212 add_fixup_expr (loc
, consolidate_singletons (x
), desc
);
1218 /* We may optionally have '+' for MEM_OFFSET_KNOWN_P. */
1219 ch
= read_skip_spaces ();
1223 set_mem_offset (x
, atoi (name
.string
));
1228 /* Handle optional " S" for MEM_SIZE. */
1229 ch
= read_skip_spaces ();
1233 set_mem_size (x
, atoi (name
.string
));
1238 /* Handle optional " A" for MEM_ALIGN. */
1239 ch
= read_skip_spaces ();
1240 if (ch
== 'A' && peek_char () != 'S')
1243 set_mem_align (x
, atoi (name
.string
));
1248 /* Handle optional " AS" for MEM_ADDR_SPACE. */
1249 ch
= read_skip_spaces ();
1250 if (ch
== 'A' && peek_char () == 'S')
1254 set_mem_addr_space (x
, atoi (name
.string
));
1264 /* Assume that LABEL_NUSES was not dumped. */
1265 /* TODO: parse LABEL_KIND. */
1266 /* For now, skip until closing ')'. */
1269 char ch
= read_char ();
1284 /* Parse a tree dump for a MEM_EXPR in DESC and turn it back into a tree.
1285 We handle "<retval>" and param names within cfun, but for anything else
1286 we "cheat" by building a global VAR_DECL of type "int" with that name
1287 (returning the same global for a name if we see the same name more
1291 function_reader::parse_mem_expr (const char *desc
)
1293 tree fndecl
= cfun
->decl
;
1295 if (0 == strcmp (desc
, "<retval>"))
1296 return DECL_RESULT (fndecl
);
1298 tree param
= find_param_by_name (fndecl
, desc
);
1302 /* Search within decls we already created.
1303 FIXME: use a hash rather than linear search. */
1306 FOR_EACH_VEC_ELT (m_fake_scope
, i
, t
)
1307 if (strcmp (desc
, IDENTIFIER_POINTER (DECL_NAME (t
))) == 0)
1310 /* Not found? Create it.
1311 This allows mimicking of real data but avoids having to specify
1312 e.g. names of locals, params etc.
1313 Though this way we don't know if we have a PARM_DECL vs a VAR_DECL,
1314 and we don't know the types. Fake it by making everything be
1315 a VAR_DECL of "int" type. */
1316 t
= build_decl (UNKNOWN_LOCATION
, VAR_DECL
,
1317 get_identifier (desc
),
1319 m_fake_scope
.safe_push (t
);
1323 /* Record that at LOC we saw an insn uid INSN_UID for the operand with index
1324 OPERAND_IDX within INSN, so that the pointer value can be fixed up in
1325 later post-processing. */
1328 function_reader::add_fixup_insn_uid (file_location loc
, rtx insn
, int operand_idx
,
1331 m_fixups
.safe_push (new fixup_insn_uid (loc
, insn
, operand_idx
, insn_uid
));
1334 /* Record that at LOC we saw an basic block index BB_IDX for the operand with index
1335 OPERAND_IDX within INSN, so that the pointer value can be fixed up in
1336 later post-processing. */
1339 function_reader::add_fixup_note_insn_basic_block (file_location loc
, rtx insn
,
1340 int operand_idx
, int bb_idx
)
1342 m_fixups
.safe_push (new fixup_note_insn_basic_block (loc
, insn
, operand_idx
,
1346 /* Placeholder hook for recording source location information seen in a dump.
1347 This is empty for now. */
1350 function_reader::add_fixup_source_location (file_location
, rtx_insn
*,
1355 /* Record that at LOC we saw textual description DESC of the MEM_EXPR or REG_EXPR
1356 of INSN, so that the fields can be fixed up in later post-processing. */
1359 function_reader::add_fixup_expr (file_location loc
, rtx insn
,
1363 /* Fail early if the RTL reader erroneously hands us an int. */
1364 gcc_assert (!ISDIGIT (desc
[0]));
1366 m_fixups
.safe_push (new fixup_expr (loc
, insn
, desc
));
1369 /* Helper function for consolidate_reg. Return the global rtx for
1370 the register with regno REGNO. */
1373 lookup_global_register (int regno
)
1375 /* We can't use a switch here, as some of the REGNUMs might not be constants
1376 for some targets. */
1377 if (regno
== STACK_POINTER_REGNUM
)
1378 return stack_pointer_rtx
;
1379 else if (regno
== FRAME_POINTER_REGNUM
)
1380 return frame_pointer_rtx
;
1381 else if (regno
== HARD_FRAME_POINTER_REGNUM
)
1382 return hard_frame_pointer_rtx
;
1383 else if (regno
== ARG_POINTER_REGNUM
)
1384 return arg_pointer_rtx
;
1385 else if (regno
== VIRTUAL_INCOMING_ARGS_REGNUM
)
1386 return virtual_incoming_args_rtx
;
1387 else if (regno
== VIRTUAL_STACK_VARS_REGNUM
)
1388 return virtual_stack_vars_rtx
;
1389 else if (regno
== VIRTUAL_STACK_DYNAMIC_REGNUM
)
1390 return virtual_stack_dynamic_rtx
;
1391 else if (regno
== VIRTUAL_OUTGOING_ARGS_REGNUM
)
1392 return virtual_outgoing_args_rtx
;
1393 else if (regno
== VIRTUAL_CFA_REGNUM
)
1394 return virtual_cfa_rtx
;
1395 else if (regno
== VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM
)
1396 return virtual_preferred_stack_boundary_rtx
;
1397 #ifdef return_ADDRESS_POINTER_REGNUM
1398 else if (regno
== RETURN_ADDRESS_POINTER_REGNUM
)
1399 return return_address_pointer_rtx
;
1405 /* Ensure that the backend can cope with a REG with regno REGNO.
1406 Normally REG instances are created by gen_reg_rtx which updates
1407 regno_reg_rtx, growing it as necessary.
1408 The REG instances created from the dumpfile weren't created this
1409 way, so we need to manually update regno_reg_rtx. */
1412 ensure_regno (int regno
)
1414 if (reg_rtx_no
< regno
+ 1)
1415 reg_rtx_no
= regno
+ 1;
1417 crtl
->emit
.ensure_regno_capacity ();
1418 gcc_assert (regno
< crtl
->emit
.regno_pointer_align_length
);
1421 /* Helper function for consolidate_singletons, for handling REG instances.
1422 Given REG instance X of some regno, return the singleton rtx for that
1423 regno, if it exists, or X. */
1426 consolidate_reg (rtx x
)
1428 gcc_assert (GET_CODE (x
) == REG
);
1430 unsigned int regno
= REGNO (x
);
1432 ensure_regno (regno
);
1434 /* Some register numbers have their rtx created in init_emit_regs
1435 e.g. stack_pointer_rtx for STACK_POINTER_REGNUM.
1436 Consolidate on this. */
1437 rtx global_reg
= lookup_global_register (regno
);
1441 /* Populate regno_reg_rtx if necessary. */
1442 if (regno_reg_rtx
[regno
] == NULL
)
1443 regno_reg_rtx
[regno
] = x
;
1445 gcc_assert (GET_CODE (regno_reg_rtx
[regno
]) == REG
);
1446 gcc_assert (REGNO (regno_reg_rtx
[regno
]) == regno
);
1447 if (GET_MODE (x
) == GET_MODE (regno_reg_rtx
[regno
]))
1448 return regno_reg_rtx
[regno
];
1453 /* When reading RTL function dumps, we must consolidate some
1454 rtx so that we use singletons where singletons are expected
1455 (e.g. we don't want multiple "(const_int 0 [0])" rtx, since
1456 these are tested via pointer equality against const0_rtx.
1458 Return the equivalent singleton rtx for X, if any, otherwise X. */
1461 function_reader::consolidate_singletons (rtx x
)
1466 switch (GET_CODE (x
))
1468 case PC
: return pc_rtx
;
1469 case RETURN
: return ret_rtx
;
1470 case SIMPLE_RETURN
: return simple_return_rtx
;
1471 case CC0
: return cc0_rtx
;
1474 return consolidate_reg (x
);
1477 return gen_rtx_CONST_INT (GET_MODE (x
), INTVAL (x
));
1486 /* Parse an rtx directive, including both the opening/closing parentheses,
1490 function_reader::parse_rtx ()
1492 require_char_ws ('(');
1493 struct md_name directive
;
1494 read_name (&directive
);
1496 = consolidate_singletons (read_rtx_code (directive
.string
));
1497 require_char_ws (')');
1502 /* Implementation of rtx_reader::postprocess for reading function dumps.
1503 Return the equivalent singleton rtx for X, if any, otherwise X. */
1506 function_reader::postprocess (rtx x
)
1508 return consolidate_singletons (x
);
1511 /* Implementation of rtx_reader::finalize_string for reading function dumps.
1512 Make a GC-managed copy of STRINGBUF. */
1515 function_reader::finalize_string (char *stringbuf
)
1517 return ggc_strdup (stringbuf
);
1520 /* Attempt to parse optional location information for insn INSN, as
1521 potentially written out by rtx_writer::print_rtx_operand_code_i.
1522 We look for a quoted string followed by a colon. */
1525 function_reader::maybe_read_location (rtx_insn
*insn
)
1527 file_location loc
= get_current_location ();
1529 /* Attempt to parse a quoted string. */
1530 int ch
= read_skip_spaces ();
1533 char *filename
= read_quoted_string ();
1535 struct md_name line_num
;
1536 read_name (&line_num
);
1537 add_fixup_source_location (loc
, insn
, filename
, atoi (line_num
.string
));
1543 /* Postprocessing subroutine of function_reader::parse_function.
1544 Populate m_insns_by_uid. */
1547 function_reader::handle_insn_uids ()
1549 /* Locate the currently assigned INSN_UID values, storing
1550 them in m_insns_by_uid. */
1552 for (rtx_insn
*insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
1554 if (m_insns_by_uid
.get (INSN_UID (insn
)))
1555 error ("duplicate insn UID: %i", INSN_UID (insn
));
1556 m_insns_by_uid
.put (INSN_UID (insn
), insn
);
1557 if (INSN_UID (insn
) > max_uid
)
1558 max_uid
= INSN_UID (insn
);
1561 /* Ensure x_cur_insn_uid is 1 more than the biggest insn UID seen.
1562 This is normally updated by the various make_*insn_raw functions. */
1563 crtl
->emit
.x_cur_insn_uid
= max_uid
+ 1;
1566 /* Apply all of the recorded fixups. */
1569 function_reader::apply_fixups ()
1573 FOR_EACH_VEC_ELT (m_fixups
, i
, f
)
1577 /* Given a UID value, try to locate a pointer to the corresponding
1578 rtx_insn *, or NULL if if can't be found. */
1581 function_reader::get_insn_by_uid (int uid
)
1583 return m_insns_by_uid
.get (uid
);
1586 /* Run the RTL dump parser, parsing a dump located at PATH.
1587 Return true iff the file was successfully parsed. */
1590 read_rtl_function_body (const char *path
)
1594 init_varasm_status ();
1596 function_reader reader
;
1597 if (!reader
.read_file (path
))
1603 /* Run the RTL dump parser on the range of lines between START_LOC and
1604 END_LOC (including those lines). */
1607 read_rtl_function_body_from_file_range (location_t start_loc
,
1610 expanded_location exploc_start
= expand_location (start_loc
);
1611 expanded_location exploc_end
= expand_location (end_loc
);
1613 if (exploc_start
.file
!= exploc_end
.file
)
1615 error_at (end_loc
, "start/end of RTL fragment are in different files");
1618 if (exploc_start
.line
>= exploc_end
.line
)
1621 "start of RTL fragment must be on an earlier line than end");
1627 init_varasm_status ();
1629 function_reader reader
;
1630 if (!reader
.read_file_fragment (exploc_start
.file
, exploc_start
.line
,
1631 exploc_end
.line
- 1))
1639 namespace selftest
{
1641 /* Verify that parse_edge_flags works. */
1646 /* parse_edge_flags modifies its input (due to strtok), so we must make
1647 a copy of the literals. */
1648 #define ASSERT_PARSE_EDGE_FLAGS(EXPECTED, STR) \
1650 char *str = xstrdup (STR); \
1651 ASSERT_EQ (EXPECTED, parse_edge_flags (str)); \
1655 ASSERT_PARSE_EDGE_FLAGS (0, "");
1656 ASSERT_PARSE_EDGE_FLAGS (EDGE_FALLTHRU
, "FALLTHRU");
1657 ASSERT_PARSE_EDGE_FLAGS (EDGE_ABNORMAL_CALL
, "ABNORMAL_CALL");
1658 ASSERT_PARSE_EDGE_FLAGS (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
,
1659 "ABNORMAL | ABNORMAL_CALL");
1661 #undef ASSERT_PARSE_EDGE_FLAGS
1664 /* Verify that lookup_reg_by_dump_name works. */
1667 test_parsing_regnos ()
1669 ASSERT_EQ (-1, lookup_reg_by_dump_name ("this is not a register"));
1671 /* Verify lookup of virtual registers. */
1672 ASSERT_EQ (VIRTUAL_INCOMING_ARGS_REGNUM
,
1673 lookup_reg_by_dump_name ("virtual-incoming-args"));
1674 ASSERT_EQ (VIRTUAL_STACK_VARS_REGNUM
,
1675 lookup_reg_by_dump_name ("virtual-stack-vars"));
1676 ASSERT_EQ (VIRTUAL_STACK_DYNAMIC_REGNUM
,
1677 lookup_reg_by_dump_name ("virtual-stack-dynamic"));
1678 ASSERT_EQ (VIRTUAL_OUTGOING_ARGS_REGNUM
,
1679 lookup_reg_by_dump_name ("virtual-outgoing-args"));
1680 ASSERT_EQ (VIRTUAL_CFA_REGNUM
,
1681 lookup_reg_by_dump_name ("virtual-cfa"));
1682 ASSERT_EQ (VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM
,
1683 lookup_reg_by_dump_name ("virtual-preferred-stack-boundary"));
1685 /* Verify lookup of non-virtual pseudos. */
1686 ASSERT_EQ (LAST_VIRTUAL_REGISTER
+ 1, lookup_reg_by_dump_name ("<0>"));
1687 ASSERT_EQ (LAST_VIRTUAL_REGISTER
+ 2, lookup_reg_by_dump_name ("<1>"));
1690 /* Verify that edge E is as expected, with the src and dest basic blocks
1691 having indices EXPECTED_SRC_IDX and EXPECTED_DEST_IDX respectively, and
1692 the edge having flags equal to EXPECTED_FLAGS.
1693 Use LOC as the effective location when reporting failures. */
1696 assert_edge_at (const location
&loc
, edge e
, int expected_src_idx
,
1697 int expected_dest_idx
, int expected_flags
)
1699 ASSERT_EQ_AT (loc
, expected_src_idx
, e
->src
->index
);
1700 ASSERT_EQ_AT (loc
, expected_dest_idx
, e
->dest
->index
);
1701 ASSERT_EQ_AT (loc
, expected_flags
, e
->flags
);
1704 /* Verify that edge EDGE is as expected, with the src and dest basic blocks
1705 having indices EXPECTED_SRC_IDX and EXPECTED_DEST_IDX respectively, and
1706 the edge having flags equal to EXPECTED_FLAGS. */
1708 #define ASSERT_EDGE(EDGE, EXPECTED_SRC_IDX, EXPECTED_DEST_IDX, \
1710 assert_edge_at (SELFTEST_LOCATION, EDGE, EXPECTED_SRC_IDX, \
1711 EXPECTED_DEST_IDX, EXPECTED_FLAGS)
1713 /* Verify that we can load RTL dumps. */
1716 test_loading_dump_fragment_1 ()
1718 // TODO: filter on target?
1719 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("asr_div1.rtl"));
1721 /* Verify that the insns were loaded correctly. */
1722 rtx_insn
*insn_1
= get_insns ();
1723 ASSERT_TRUE (insn_1
);
1724 ASSERT_EQ (1, INSN_UID (insn_1
));
1725 ASSERT_EQ (INSN
, GET_CODE (insn_1
));
1726 ASSERT_EQ (SET
, GET_CODE (PATTERN (insn_1
)));
1727 ASSERT_EQ (NULL
, PREV_INSN (insn_1
));
1729 rtx_insn
*insn_2
= NEXT_INSN (insn_1
);
1730 ASSERT_TRUE (insn_2
);
1731 ASSERT_EQ (2, INSN_UID (insn_2
));
1732 ASSERT_EQ (INSN
, GET_CODE (insn_2
));
1733 ASSERT_EQ (insn_1
, PREV_INSN (insn_2
));
1734 ASSERT_EQ (NULL
, NEXT_INSN (insn_2
));
1736 /* Verify that registers were loaded correctly. */
1737 rtx insn_1_dest
= SET_DEST (PATTERN (insn_1
));
1738 ASSERT_EQ (REG
, GET_CODE (insn_1_dest
));
1739 ASSERT_EQ ((LAST_VIRTUAL_REGISTER
+ 1) + 2, REGNO (insn_1_dest
));
1740 rtx insn_1_src
= SET_SRC (PATTERN (insn_1
));
1741 ASSERT_EQ (LSHIFTRT
, GET_CODE (insn_1_src
));
1742 rtx reg
= XEXP (insn_1_src
, 0);
1743 ASSERT_EQ (REG
, GET_CODE (reg
));
1744 ASSERT_EQ (LAST_VIRTUAL_REGISTER
+ 1, REGNO (reg
));
1746 /* Verify that get_insn_by_uid works. */
1747 ASSERT_EQ (insn_1
, get_insn_by_uid (1));
1748 ASSERT_EQ (insn_2
, get_insn_by_uid (2));
1750 /* Verify that basic blocks were created. */
1751 ASSERT_EQ (2, BLOCK_FOR_INSN (insn_1
)->index
);
1752 ASSERT_EQ (2, BLOCK_FOR_INSN (insn_2
)->index
);
1754 /* Verify that the CFG was recreated. */
1756 verify_three_block_rtl_cfg (cfun
);
1757 basic_block bb2
= BASIC_BLOCK_FOR_FN (cfun
, 2);
1758 ASSERT_TRUE (bb2
!= NULL
);
1759 ASSERT_EQ (BB_RTL
, bb2
->flags
& BB_RTL
);
1760 ASSERT_EQ (2, bb2
->index
);
1761 ASSERT_EQ (insn_1
, BB_HEAD (bb2
));
1762 ASSERT_EQ (insn_2
, BB_END (bb2
));
1765 /* Verify loading another RTL dump. */
1768 test_loading_dump_fragment_2 ()
1770 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("simple-cse.rtl"));
1772 rtx_insn
*insn_1
= get_insn_by_uid (1);
1773 rtx_insn
*insn_2
= get_insn_by_uid (2);
1774 rtx_insn
*insn_3
= get_insn_by_uid (3);
1776 rtx set1
= single_set (insn_1
);
1777 ASSERT_NE (NULL
, set1
);
1778 rtx set2
= single_set (insn_2
);
1779 ASSERT_NE (NULL
, set2
);
1780 rtx set3
= single_set (insn_3
);
1781 ASSERT_NE (NULL
, set3
);
1783 rtx src1
= SET_SRC (set1
);
1784 ASSERT_EQ (PLUS
, GET_CODE (src1
));
1786 rtx src2
= SET_SRC (set2
);
1787 ASSERT_EQ (PLUS
, GET_CODE (src2
));
1789 /* Both src1 and src2 refer to "(reg:SI %0)".
1790 Verify that we have pointer equality. */
1791 rtx lhs1
= XEXP (src1
, 0);
1792 rtx lhs2
= XEXP (src2
, 0);
1793 ASSERT_EQ (lhs1
, lhs2
);
1795 /* Verify that the CFG was recreated. */
1797 verify_three_block_rtl_cfg (cfun
);
1800 /* Verify that CODE_LABEL insns are loaded correctly. */
1803 test_loading_labels ()
1805 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("example-labels.rtl"));
1807 rtx_insn
*insn_100
= get_insn_by_uid (100);
1808 ASSERT_EQ (CODE_LABEL
, GET_CODE (insn_100
));
1809 ASSERT_EQ (100, INSN_UID (insn_100
));
1810 ASSERT_EQ (NULL
, LABEL_NAME (insn_100
));
1811 ASSERT_EQ (0, LABEL_NUSES (insn_100
));
1812 ASSERT_EQ (30, CODE_LABEL_NUMBER (insn_100
));
1814 rtx_insn
*insn_200
= get_insn_by_uid (200);
1815 ASSERT_EQ (CODE_LABEL
, GET_CODE (insn_200
));
1816 ASSERT_EQ (200, INSN_UID (insn_200
));
1817 ASSERT_STREQ ("some_label_name", LABEL_NAME (insn_200
));
1818 ASSERT_EQ (0, LABEL_NUSES (insn_200
));
1819 ASSERT_EQ (40, CODE_LABEL_NUMBER (insn_200
));
1821 /* Ensure that the presence of CODE_LABEL_NUMBER == 40
1822 means that the next label num to be handed out will be 41. */
1823 ASSERT_EQ (41, max_label_num ());
1825 /* Ensure that label names read from a dump are GC-managed
1826 and are found through the insn. */
1827 forcibly_ggc_collect ();
1828 ASSERT_TRUE (ggc_marked_p (insn_200
));
1829 ASSERT_TRUE (ggc_marked_p (LABEL_NAME (insn_200
)));
1832 /* Verify that the loader copes with an insn with a mode. */
1835 test_loading_insn_with_mode ()
1837 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("insn-with-mode.rtl"));
1838 rtx_insn
*insn
= get_insns ();
1839 ASSERT_EQ (INSN
, GET_CODE (insn
));
1841 /* Verify that the "TI" mode was set from "insn:TI". */
1842 ASSERT_EQ (TImode
, GET_MODE (insn
));
1845 /* Verify that the loader copes with a jump_insn to a label_ref. */
1848 test_loading_jump_to_label_ref ()
1850 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("jump-to-label-ref.rtl"));
1852 rtx_insn
*jump_insn
= get_insn_by_uid (1);
1853 ASSERT_EQ (JUMP_INSN
, GET_CODE (jump_insn
));
1855 rtx_insn
*barrier
= get_insn_by_uid (2);
1856 ASSERT_EQ (BARRIER
, GET_CODE (barrier
));
1858 rtx_insn
*code_label
= get_insn_by_uid (100);
1859 ASSERT_EQ (CODE_LABEL
, GET_CODE (code_label
));
1861 /* Verify the jump_insn. */
1862 ASSERT_EQ (4, BLOCK_FOR_INSN (jump_insn
)->index
);
1863 ASSERT_EQ (SET
, GET_CODE (PATTERN (jump_insn
)));
1864 /* Ensure that the "(pc)" is using the global singleton. */
1865 ASSERT_RTX_PTR_EQ (pc_rtx
, SET_DEST (PATTERN (jump_insn
)));
1866 rtx label_ref
= SET_SRC (PATTERN (jump_insn
));
1867 ASSERT_EQ (LABEL_REF
, GET_CODE (label_ref
));
1868 ASSERT_EQ (code_label
, label_ref_label (label_ref
));
1869 ASSERT_EQ (code_label
, JUMP_LABEL (jump_insn
));
1871 /* Verify the code_label. */
1872 ASSERT_EQ (5, BLOCK_FOR_INSN (code_label
)->index
);
1873 ASSERT_EQ (NULL
, LABEL_NAME (code_label
));
1874 ASSERT_EQ (1, LABEL_NUSES (code_label
));
1876 /* Verify the generated CFG. */
1878 /* Locate blocks. */
1879 basic_block entry
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
1880 ASSERT_TRUE (entry
!= NULL
);
1881 ASSERT_EQ (ENTRY_BLOCK
, entry
->index
);
1883 basic_block exit
= EXIT_BLOCK_PTR_FOR_FN (cfun
);
1884 ASSERT_TRUE (exit
!= NULL
);
1885 ASSERT_EQ (EXIT_BLOCK
, exit
->index
);
1887 basic_block bb4
= (*cfun
->cfg
->x_basic_block_info
)[4];
1888 basic_block bb5
= (*cfun
->cfg
->x_basic_block_info
)[5];
1889 ASSERT_EQ (4, bb4
->index
);
1890 ASSERT_EQ (5, bb5
->index
);
1893 ASSERT_EQ (NULL
, entry
->preds
);
1894 ASSERT_EQ (1, entry
->succs
->length ());
1895 ASSERT_EDGE ((*entry
->succs
)[0], 0, 4, EDGE_FALLTHRU
);
1898 ASSERT_EQ (1, bb4
->preds
->length ());
1899 ASSERT_EDGE ((*bb4
->preds
)[0], 0, 4, EDGE_FALLTHRU
);
1900 ASSERT_EQ (1, bb4
->succs
->length ());
1901 ASSERT_EDGE ((*bb4
->succs
)[0], 4, 5, 0x0);
1904 ASSERT_EQ (1, bb5
->preds
->length ());
1905 ASSERT_EDGE ((*bb5
->preds
)[0], 4, 5, 0x0);
1906 ASSERT_EQ (1, bb5
->succs
->length ());
1907 ASSERT_EDGE ((*bb5
->succs
)[0], 5, 1, EDGE_FALLTHRU
);
1910 ASSERT_EQ (1, exit
->preds
->length ());
1911 ASSERT_EDGE ((*exit
->preds
)[0], 5, 1, EDGE_FALLTHRU
);
1912 ASSERT_EQ (NULL
, exit
->succs
);
1915 /* Verify that the loader copes with a jump_insn to a label_ref
1919 test_loading_jump_to_return ()
1921 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("jump-to-return.rtl"));
1923 rtx_insn
*jump_insn
= get_insn_by_uid (1);
1924 ASSERT_EQ (JUMP_INSN
, GET_CODE (jump_insn
));
1925 ASSERT_RTX_PTR_EQ (ret_rtx
, JUMP_LABEL (jump_insn
));
1928 /* Verify that the loader copes with a jump_insn to a label_ref
1929 marked "simple_return". */
1932 test_loading_jump_to_simple_return ()
1934 rtl_dump_test
t (SELFTEST_LOCATION
,
1935 locate_file ("jump-to-simple-return.rtl"));
1937 rtx_insn
*jump_insn
= get_insn_by_uid (1);
1938 ASSERT_EQ (JUMP_INSN
, GET_CODE (jump_insn
));
1939 ASSERT_RTX_PTR_EQ (simple_return_rtx
, JUMP_LABEL (jump_insn
));
1942 /* Verify that the loader copes with a NOTE_INSN_BASIC_BLOCK. */
1945 test_loading_note_insn_basic_block ()
1947 rtl_dump_test
t (SELFTEST_LOCATION
,
1948 locate_file ("note_insn_basic_block.rtl"));
1950 rtx_insn
*note
= get_insn_by_uid (1);
1951 ASSERT_EQ (NOTE
, GET_CODE (note
));
1952 ASSERT_EQ (2, BLOCK_FOR_INSN (note
)->index
);
1954 ASSERT_EQ (NOTE_INSN_BASIC_BLOCK
, NOTE_KIND (note
));
1955 ASSERT_EQ (2, NOTE_BASIC_BLOCK (note
)->index
);
1956 ASSERT_EQ (BASIC_BLOCK_FOR_FN (cfun
, 2), NOTE_BASIC_BLOCK (note
));
1959 /* Verify that the loader copes with a NOTE_INSN_DELETED. */
1962 test_loading_note_insn_deleted ()
1964 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("note-insn-deleted.rtl"));
1966 rtx_insn
*note
= get_insn_by_uid (1);
1967 ASSERT_EQ (NOTE
, GET_CODE (note
));
1968 ASSERT_EQ (NOTE_INSN_DELETED
, NOTE_KIND (note
));
1971 /* Verify that the const_int values are consolidated, since
1972 pointer equality corresponds to value equality.
1973 TODO: do this for all in CASE_CONST_UNIQUE. */
1976 test_loading_const_int ()
1978 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("const-int.rtl"));
1980 /* Verify that const_int values below MAX_SAVED_CONST_INT use
1981 the global values. */
1982 ASSERT_EQ (const0_rtx
, SET_SRC (PATTERN (get_insn_by_uid (1))));
1983 ASSERT_EQ (const1_rtx
, SET_SRC (PATTERN (get_insn_by_uid (2))));
1984 ASSERT_EQ (constm1_rtx
, SET_SRC (PATTERN (get_insn_by_uid (3))));
1986 /* Verify that other const_int values are consolidated. */
1987 rtx int256
= gen_rtx_CONST_INT (SImode
, 256);
1988 ASSERT_EQ (int256
, SET_SRC (PATTERN (get_insn_by_uid (4))));
1991 /* Verify that the loader copes with a SYMBOL_REF. */
1994 test_loading_symbol_ref ()
1996 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("symbol-ref.rtl"));
1998 rtx_insn
*insn
= get_insns ();
2000 rtx high
= SET_SRC (PATTERN (insn
));
2001 ASSERT_EQ (HIGH
, GET_CODE (high
));
2003 rtx symbol_ref
= XEXP (high
, 0);
2004 ASSERT_EQ (SYMBOL_REF
, GET_CODE (symbol_ref
));
2006 /* Verify that "[flags 0xc0]" was parsed. */
2007 ASSERT_EQ (0xc0, SYMBOL_REF_FLAGS (symbol_ref
));
2008 /* TODO: we don't yet load SYMBOL_REF_DECL. */
2011 /* Verify that the loader can rebuild a CFG. */
2016 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("cfg-test.rtl"));
2018 ASSERT_STREQ ("cfg_test", IDENTIFIER_POINTER (DECL_NAME (cfun
->decl
)));
2022 ASSERT_TRUE (cfun
->cfg
!= NULL
);
2023 ASSERT_EQ (6, n_basic_blocks_for_fn (cfun
));
2024 ASSERT_EQ (6, n_edges_for_fn (cfun
));
2026 /* The "fake" basic blocks. */
2027 basic_block entry
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
2028 ASSERT_TRUE (entry
!= NULL
);
2029 ASSERT_EQ (ENTRY_BLOCK
, entry
->index
);
2031 basic_block exit
= EXIT_BLOCK_PTR_FOR_FN (cfun
);
2032 ASSERT_TRUE (exit
!= NULL
);
2033 ASSERT_EQ (EXIT_BLOCK
, exit
->index
);
2035 /* The "real" basic blocks. */
2036 basic_block bb2
= (*cfun
->cfg
->x_basic_block_info
)[2];
2037 basic_block bb3
= (*cfun
->cfg
->x_basic_block_info
)[3];
2038 basic_block bb4
= (*cfun
->cfg
->x_basic_block_info
)[4];
2039 basic_block bb5
= (*cfun
->cfg
->x_basic_block_info
)[5];
2041 ASSERT_EQ (2, bb2
->index
);
2042 ASSERT_EQ (3, bb3
->index
);
2043 ASSERT_EQ (4, bb4
->index
);
2044 ASSERT_EQ (5, bb5
->index
);
2046 /* Verify connectivity. */
2049 ASSERT_EQ (NULL
, entry
->preds
);
2050 ASSERT_EQ (1, entry
->succs
->length ());
2051 ASSERT_EDGE ((*entry
->succs
)[0], 0, 2, EDGE_FALLTHRU
);
2054 ASSERT_EQ (1, bb2
->preds
->length ());
2055 ASSERT_EDGE ((*bb2
->preds
)[0], 0, 2, EDGE_FALLTHRU
);
2056 ASSERT_EQ (2, bb2
->succs
->length ());
2057 ASSERT_EDGE ((*bb2
->succs
)[0], 2, 3, EDGE_TRUE_VALUE
);
2058 ASSERT_EDGE ((*bb2
->succs
)[1], 2, 4, EDGE_FALSE_VALUE
);
2061 ASSERT_EQ (1, bb3
->preds
->length ());
2062 ASSERT_EDGE ((*bb3
->preds
)[0], 2, 3, EDGE_TRUE_VALUE
);
2063 ASSERT_EQ (1, bb3
->succs
->length ());
2064 ASSERT_EDGE ((*bb3
->succs
)[0], 3, 5, EDGE_FALLTHRU
);
2067 ASSERT_EQ (1, bb4
->preds
->length ());
2068 ASSERT_EDGE ((*bb4
->preds
)[0], 2, 4, EDGE_FALSE_VALUE
);
2069 ASSERT_EQ (1, bb4
->succs
->length ());
2070 ASSERT_EDGE ((*bb4
->succs
)[0], 4, 5, EDGE_FALLTHRU
);
2073 ASSERT_EQ (2, bb5
->preds
->length ());
2074 ASSERT_EDGE ((*bb5
->preds
)[0], 3, 5, EDGE_FALLTHRU
);
2075 ASSERT_EDGE ((*bb5
->preds
)[1], 4, 5, EDGE_FALLTHRU
);
2076 ASSERT_EQ (1, bb5
->succs
->length ());
2077 ASSERT_EDGE ((*bb5
->succs
)[0], 5, 1, EDGE_FALLTHRU
);
2080 ASSERT_EQ (1, exit
->preds
->length ());
2081 ASSERT_EDGE ((*exit
->preds
)[0], 5, 1, EDGE_FALLTHRU
);
2082 ASSERT_EQ (NULL
, exit
->succs
);
2085 /* Verify that the loader copes with sparse block indices.
2086 This testcase loads a file with a "(block 42)". */
2089 test_loading_bb_index ()
2091 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("bb-index.rtl"));
2093 ASSERT_STREQ ("test_bb_index", IDENTIFIER_POINTER (DECL_NAME (cfun
->decl
)));
2097 ASSERT_TRUE (cfun
->cfg
!= NULL
);
2098 ASSERT_EQ (3, n_basic_blocks_for_fn (cfun
));
2099 ASSERT_EQ (43, basic_block_info_for_fn (cfun
)->length ());
2100 ASSERT_EQ (2, n_edges_for_fn (cfun
));
2102 ASSERT_EQ (NULL
, (*cfun
->cfg
->x_basic_block_info
)[41]);
2103 basic_block bb42
= (*cfun
->cfg
->x_basic_block_info
)[42];
2104 ASSERT_NE (NULL
, bb42
);
2105 ASSERT_EQ (42, bb42
->index
);
2108 /* Verify that function_reader::handle_any_trailing_information correctly
2109 parses all the possible items emitted for a MEM. */
2114 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("mem.rtl"));
2116 ASSERT_STREQ ("test_mem", IDENTIFIER_POINTER (DECL_NAME (cfun
->decl
)));
2119 /* Verify parsing of "[42 i+17 S8 A128 AS5]". */
2120 rtx_insn
*insn_1
= get_insn_by_uid (1);
2121 rtx set1
= single_set (insn_1
);
2122 rtx mem1
= SET_DEST (set1
);
2123 ASSERT_EQ (42, MEM_ALIAS_SET (mem1
));
2125 ASSERT_TRUE (MEM_OFFSET_KNOWN_P (mem1
));
2126 ASSERT_EQ (17, MEM_OFFSET (mem1
));
2128 ASSERT_EQ (8, MEM_SIZE (mem1
));
2130 ASSERT_EQ (128, MEM_ALIGN (mem1
));
2132 ASSERT_EQ (5, MEM_ADDR_SPACE (mem1
));
2134 /* Verify parsing of "43 i+18 S9 AS6"
2135 (an address space without an alignment). */
2136 rtx_insn
*insn_2
= get_insn_by_uid (2);
2137 rtx set2
= single_set (insn_2
);
2138 rtx mem2
= SET_DEST (set2
);
2139 ASSERT_EQ (43, MEM_ALIAS_SET (mem2
));
2141 ASSERT_TRUE (MEM_OFFSET_KNOWN_P (mem2
));
2142 ASSERT_EQ (18, MEM_OFFSET (mem2
));
2144 ASSERT_EQ (9, MEM_SIZE (mem2
));
2146 ASSERT_EQ (6, MEM_ADDR_SPACE (mem2
));
2149 /* Run all of the selftests within this file. */
2152 read_rtl_function_c_tests ()
2155 test_parsing_regnos ();
2156 test_loading_dump_fragment_1 ();
2157 test_loading_dump_fragment_2 ();
2158 test_loading_labels ();
2159 test_loading_insn_with_mode ();
2160 test_loading_jump_to_label_ref ();
2161 test_loading_jump_to_return ();
2162 test_loading_jump_to_simple_return ();
2163 test_loading_note_insn_basic_block ();
2164 test_loading_note_insn_deleted ();
2165 test_loading_const_int ();
2166 test_loading_symbol_ref ();
2167 test_loading_cfg ();
2168 test_loading_bb_index ();
2169 test_loading_mem ();
2172 } // namespace selftest
2174 #endif /* #if CHECKING_P */