1 /* read-rtl-function.c - Reader for RTL function dumps
2 Copyright (C) 2016-2018 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 rtx
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 (strcmp (string
, GET_NOTE_INSN_NAME (i
)) == 0)
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 (id_equal (DECL_NAME (arg
), name
))
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 x
= 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 (strncmp (desc
, "orig:", 5) == 0)
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.
1120 Return X, or possibly a reallocated copy of X. */
1123 function_reader::extra_parsing_for_operand_code_0 (rtx x
, int idx
)
1125 RTX_CODE code
= GET_CODE (x
);
1127 struct md_name name
;
1129 if (idx
== 1 && code
== SYMBOL_REF
)
1131 /* Possibly wrote " [flags %#x]", SYMBOL_REF_FLAGS (in_rtx). */
1132 c
= read_skip_spaces ();
1135 file_location loc
= read_name (&name
);
1136 if (strcmp (name
.string
, "flags"))
1137 error_at (loc
, "was expecting `%s'", "flags");
1139 SYMBOL_REF_FLAGS (x
) = strtol (name
.string
, NULL
, 16);
1141 /* The standard RTX_CODE_SIZE (SYMBOL_REF) used when allocating
1142 x doesn't have space for the block_symbol information, so
1143 we must reallocate it if this flag is set. */
1144 if (SYMBOL_REF_HAS_BLOCK_INFO_P (x
))
1146 /* Emulate the allocation normally done by
1147 varasm.c:create_block_symbol. */
1148 unsigned int size
= RTX_HDR_SIZE
+ sizeof (struct block_symbol
);
1149 rtx new_x
= (rtx
) ggc_internal_alloc (size
);
1151 /* Copy data over from the smaller SYMBOL_REF. */
1152 memcpy (new_x
, x
, RTX_CODE_SIZE (SYMBOL_REF
));
1155 /* We can't reconstruct SYMBOL_REF_BLOCK; set it to NULL. */
1156 SYMBOL_REF_BLOCK (x
) = NULL
;
1158 /* Zero the offset. */
1159 SYMBOL_REF_BLOCK_OFFSET (x
) = 0;
1167 /* If X had a non-NULL SYMBOL_REF_DECL,
1168 rtx_writer::print_rtx_operand_code_0 would have dumped it
1169 using print_node_brief.
1170 Skip the content for now. */
1171 c
= read_skip_spaces ();
1176 char ch
= read_char ();
1184 else if (idx
== 3 && code
== NOTE
)
1186 /* Note-specific data appears for operand 3, which annoyingly
1187 is before the enum specifying which kind of note we have
1189 c
= read_skip_spaces ();
1192 /* Possibly data for a NOTE_INSN_BASIC_BLOCK, of the form:
1194 file_location bb_loc
= read_name (&name
);
1195 if (strcmp (name
.string
, "bb"))
1196 error_at (bb_loc
, "was expecting `%s'", "bb");
1198 int bb_idx
= atoi (name
.string
);
1199 add_fixup_note_insn_basic_block (bb_loc
, x
, idx
,
1201 require_char_ws (']');
1210 /* Implementation of rtx_reader::handle_any_trailing_information.
1211 Handle the various additional information that print-rtl.c can
1212 write after the regular fields, when parsing X. */
1215 function_reader::handle_any_trailing_information (rtx x
)
1217 struct md_name name
;
1219 switch (GET_CODE (x
))
1224 require_char_ws ('[');
1226 set_mem_alias_set (x
, atoi (name
.string
));
1227 /* We have either a MEM_EXPR, or a space. */
1228 if (peek_char () != ' ')
1230 file_location loc
= get_current_location ();
1231 char *desc
= read_until (" +", false);
1232 add_fixup_expr (loc
, consolidate_singletons (x
), desc
);
1238 /* We may optionally have '+' for MEM_OFFSET_KNOWN_P. */
1239 ch
= read_skip_spaces ();
1243 set_mem_offset (x
, atoi (name
.string
));
1248 /* Handle optional " S" for MEM_SIZE. */
1249 ch
= read_skip_spaces ();
1253 set_mem_size (x
, atoi (name
.string
));
1258 /* Handle optional " A" for MEM_ALIGN. */
1259 ch
= read_skip_spaces ();
1260 if (ch
== 'A' && peek_char () != 'S')
1263 set_mem_align (x
, atoi (name
.string
));
1268 /* Handle optional " AS" for MEM_ADDR_SPACE. */
1269 ch
= read_skip_spaces ();
1270 if (ch
== 'A' && peek_char () == 'S')
1274 set_mem_addr_space (x
, atoi (name
.string
));
1284 /* Assume that LABEL_NUSES was not dumped. */
1285 /* TODO: parse LABEL_KIND. */
1286 /* For now, skip until closing ')'. */
1289 char ch
= read_char ();
1304 /* Parse a tree dump for a MEM_EXPR in DESC and turn it back into a tree.
1305 We handle "<retval>" and param names within cfun, but for anything else
1306 we "cheat" by building a global VAR_DECL of type "int" with that name
1307 (returning the same global for a name if we see the same name more
1311 function_reader::parse_mem_expr (const char *desc
)
1313 tree fndecl
= cfun
->decl
;
1315 if (strcmp (desc
, "<retval>") == 0)
1316 return DECL_RESULT (fndecl
);
1318 tree param
= find_param_by_name (fndecl
, desc
);
1322 /* Search within decls we already created.
1323 FIXME: use a hash rather than linear search. */
1326 FOR_EACH_VEC_ELT (m_fake_scope
, i
, t
)
1327 if (id_equal (DECL_NAME (t
), desc
))
1330 /* Not found? Create it.
1331 This allows mimicking of real data but avoids having to specify
1332 e.g. names of locals, params etc.
1333 Though this way we don't know if we have a PARM_DECL vs a VAR_DECL,
1334 and we don't know the types. Fake it by making everything be
1335 a VAR_DECL of "int" type. */
1336 t
= build_decl (UNKNOWN_LOCATION
, VAR_DECL
,
1337 get_identifier (desc
),
1339 m_fake_scope
.safe_push (t
);
1343 /* Record that at LOC we saw an insn uid INSN_UID for the operand with index
1344 OPERAND_IDX within INSN, so that the pointer value can be fixed up in
1345 later post-processing. */
1348 function_reader::add_fixup_insn_uid (file_location loc
, rtx insn
, int operand_idx
,
1351 m_fixups
.safe_push (new fixup_insn_uid (loc
, insn
, operand_idx
, insn_uid
));
1354 /* Record that at LOC we saw an basic block index BB_IDX for the operand with index
1355 OPERAND_IDX within INSN, so that the pointer value can be fixed up in
1356 later post-processing. */
1359 function_reader::add_fixup_note_insn_basic_block (file_location loc
, rtx insn
,
1360 int operand_idx
, int bb_idx
)
1362 m_fixups
.safe_push (new fixup_note_insn_basic_block (loc
, insn
, operand_idx
,
1366 /* Placeholder hook for recording source location information seen in a dump.
1367 This is empty for now. */
1370 function_reader::add_fixup_source_location (file_location
, rtx_insn
*,
1375 /* Record that at LOC we saw textual description DESC of the MEM_EXPR or REG_EXPR
1376 of INSN, so that the fields can be fixed up in later post-processing. */
1379 function_reader::add_fixup_expr (file_location loc
, rtx insn
,
1383 /* Fail early if the RTL reader erroneously hands us an int. */
1384 gcc_assert (!ISDIGIT (desc
[0]));
1386 m_fixups
.safe_push (new fixup_expr (loc
, insn
, desc
));
1389 /* Helper function for consolidate_reg. Return the global rtx for
1390 the register with regno REGNO. */
1393 lookup_global_register (int regno
)
1395 /* We can't use a switch here, as some of the REGNUMs might not be constants
1396 for some targets. */
1397 if (regno
== STACK_POINTER_REGNUM
)
1398 return stack_pointer_rtx
;
1399 else if (regno
== FRAME_POINTER_REGNUM
)
1400 return frame_pointer_rtx
;
1401 else if (regno
== HARD_FRAME_POINTER_REGNUM
)
1402 return hard_frame_pointer_rtx
;
1403 else if (regno
== ARG_POINTER_REGNUM
)
1404 return arg_pointer_rtx
;
1405 else if (regno
== VIRTUAL_INCOMING_ARGS_REGNUM
)
1406 return virtual_incoming_args_rtx
;
1407 else if (regno
== VIRTUAL_STACK_VARS_REGNUM
)
1408 return virtual_stack_vars_rtx
;
1409 else if (regno
== VIRTUAL_STACK_DYNAMIC_REGNUM
)
1410 return virtual_stack_dynamic_rtx
;
1411 else if (regno
== VIRTUAL_OUTGOING_ARGS_REGNUM
)
1412 return virtual_outgoing_args_rtx
;
1413 else if (regno
== VIRTUAL_CFA_REGNUM
)
1414 return virtual_cfa_rtx
;
1415 else if (regno
== VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM
)
1416 return virtual_preferred_stack_boundary_rtx
;
1417 #ifdef return_ADDRESS_POINTER_REGNUM
1418 else if (regno
== RETURN_ADDRESS_POINTER_REGNUM
)
1419 return return_address_pointer_rtx
;
1425 /* Ensure that the backend can cope with a REG with regno REGNO.
1426 Normally REG instances are created by gen_reg_rtx which updates
1427 regno_reg_rtx, growing it as necessary.
1428 The REG instances created from the dumpfile weren't created this
1429 way, so we need to manually update regno_reg_rtx. */
1432 ensure_regno (int regno
)
1434 if (reg_rtx_no
< regno
+ 1)
1435 reg_rtx_no
= regno
+ 1;
1437 crtl
->emit
.ensure_regno_capacity ();
1438 gcc_assert (regno
< crtl
->emit
.regno_pointer_align_length
);
1441 /* Helper function for consolidate_singletons, for handling REG instances.
1442 Given REG instance X of some regno, return the singleton rtx for that
1443 regno, if it exists, or X. */
1446 consolidate_reg (rtx x
)
1448 gcc_assert (GET_CODE (x
) == REG
);
1450 unsigned int regno
= REGNO (x
);
1452 ensure_regno (regno
);
1454 /* Some register numbers have their rtx created in init_emit_regs
1455 e.g. stack_pointer_rtx for STACK_POINTER_REGNUM.
1456 Consolidate on this. */
1457 rtx global_reg
= lookup_global_register (regno
);
1461 /* Populate regno_reg_rtx if necessary. */
1462 if (regno_reg_rtx
[regno
] == NULL
)
1463 regno_reg_rtx
[regno
] = x
;
1465 gcc_assert (GET_CODE (regno_reg_rtx
[regno
]) == REG
);
1466 gcc_assert (REGNO (regno_reg_rtx
[regno
]) == regno
);
1467 if (GET_MODE (x
) == GET_MODE (regno_reg_rtx
[regno
]))
1468 return regno_reg_rtx
[regno
];
1473 /* When reading RTL function dumps, we must consolidate some
1474 rtx so that we use singletons where singletons are expected
1475 (e.g. we don't want multiple "(const_int 0 [0])" rtx, since
1476 these are tested via pointer equality against const0_rtx.
1478 Return the equivalent singleton rtx for X, if any, otherwise X. */
1481 function_reader::consolidate_singletons (rtx x
)
1486 switch (GET_CODE (x
))
1488 case PC
: return pc_rtx
;
1489 case RETURN
: return ret_rtx
;
1490 case SIMPLE_RETURN
: return simple_return_rtx
;
1491 case CC0
: return cc0_rtx
;
1494 return consolidate_reg (x
);
1497 return gen_rtx_CONST_INT (GET_MODE (x
), INTVAL (x
));
1506 /* Parse an rtx directive, including both the opening/closing parentheses,
1510 function_reader::parse_rtx ()
1512 require_char_ws ('(');
1513 struct md_name directive
;
1514 read_name (&directive
);
1516 = consolidate_singletons (read_rtx_code (directive
.string
));
1517 require_char_ws (')');
1522 /* Implementation of rtx_reader::postprocess for reading function dumps.
1523 Return the equivalent singleton rtx for X, if any, otherwise X. */
1526 function_reader::postprocess (rtx x
)
1528 return consolidate_singletons (x
);
1531 /* Implementation of rtx_reader::finalize_string for reading function dumps.
1532 Make a GC-managed copy of STRINGBUF. */
1535 function_reader::finalize_string (char *stringbuf
)
1537 return ggc_strdup (stringbuf
);
1540 /* Attempt to parse optional location information for insn INSN, as
1541 potentially written out by rtx_writer::print_rtx_operand_code_i.
1542 We look for a quoted string followed by a colon. */
1545 function_reader::maybe_read_location (rtx_insn
*insn
)
1547 file_location loc
= get_current_location ();
1549 /* Attempt to parse a quoted string. */
1550 int ch
= read_skip_spaces ();
1553 char *filename
= read_quoted_string ();
1555 struct md_name line_num
;
1556 read_name (&line_num
);
1557 add_fixup_source_location (loc
, insn
, filename
, atoi (line_num
.string
));
1563 /* Postprocessing subroutine of function_reader::parse_function.
1564 Populate m_insns_by_uid. */
1567 function_reader::handle_insn_uids ()
1569 /* Locate the currently assigned INSN_UID values, storing
1570 them in m_insns_by_uid. */
1572 for (rtx_insn
*insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
1574 if (m_insns_by_uid
.get (INSN_UID (insn
)))
1575 error ("duplicate insn UID: %i", INSN_UID (insn
));
1576 m_insns_by_uid
.put (INSN_UID (insn
), insn
);
1577 if (INSN_UID (insn
) > max_uid
)
1578 max_uid
= INSN_UID (insn
);
1581 /* Ensure x_cur_insn_uid is 1 more than the biggest insn UID seen.
1582 This is normally updated by the various make_*insn_raw functions. */
1583 crtl
->emit
.x_cur_insn_uid
= max_uid
+ 1;
1586 /* Apply all of the recorded fixups. */
1589 function_reader::apply_fixups ()
1593 FOR_EACH_VEC_ELT (m_fixups
, i
, f
)
1597 /* Given a UID value, try to locate a pointer to the corresponding
1598 rtx_insn *, or NULL if if can't be found. */
1601 function_reader::get_insn_by_uid (int uid
)
1603 return m_insns_by_uid
.get (uid
);
1606 /* Run the RTL dump parser, parsing a dump located at PATH.
1607 Return true iff the file was successfully parsed. */
1610 read_rtl_function_body (const char *path
)
1614 init_varasm_status ();
1616 function_reader reader
;
1617 if (!reader
.read_file (path
))
1623 /* Run the RTL dump parser on the range of lines between START_LOC and
1624 END_LOC (including those lines). */
1627 read_rtl_function_body_from_file_range (location_t start_loc
,
1630 expanded_location exploc_start
= expand_location (start_loc
);
1631 expanded_location exploc_end
= expand_location (end_loc
);
1633 if (exploc_start
.file
!= exploc_end
.file
)
1635 error_at (end_loc
, "start/end of RTL fragment are in different files");
1638 if (exploc_start
.line
>= exploc_end
.line
)
1641 "start of RTL fragment must be on an earlier line than end");
1647 init_varasm_status ();
1649 function_reader reader
;
1650 if (!reader
.read_file_fragment (exploc_start
.file
, exploc_start
.line
,
1651 exploc_end
.line
- 1))
1659 namespace selftest
{
1661 /* Verify that parse_edge_flags works. */
1666 /* parse_edge_flags modifies its input (due to strtok), so we must make
1667 a copy of the literals. */
1668 #define ASSERT_PARSE_EDGE_FLAGS(EXPECTED, STR) \
1670 char *str = xstrdup (STR); \
1671 ASSERT_EQ (EXPECTED, parse_edge_flags (str)); \
1675 ASSERT_PARSE_EDGE_FLAGS (0, "");
1676 ASSERT_PARSE_EDGE_FLAGS (EDGE_FALLTHRU
, "FALLTHRU");
1677 ASSERT_PARSE_EDGE_FLAGS (EDGE_ABNORMAL_CALL
, "ABNORMAL_CALL");
1678 ASSERT_PARSE_EDGE_FLAGS (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
,
1679 "ABNORMAL | ABNORMAL_CALL");
1681 #undef ASSERT_PARSE_EDGE_FLAGS
1684 /* Verify that lookup_reg_by_dump_name works. */
1687 test_parsing_regnos ()
1689 ASSERT_EQ (-1, lookup_reg_by_dump_name ("this is not a register"));
1691 /* Verify lookup of virtual registers. */
1692 ASSERT_EQ (VIRTUAL_INCOMING_ARGS_REGNUM
,
1693 lookup_reg_by_dump_name ("virtual-incoming-args"));
1694 ASSERT_EQ (VIRTUAL_STACK_VARS_REGNUM
,
1695 lookup_reg_by_dump_name ("virtual-stack-vars"));
1696 ASSERT_EQ (VIRTUAL_STACK_DYNAMIC_REGNUM
,
1697 lookup_reg_by_dump_name ("virtual-stack-dynamic"));
1698 ASSERT_EQ (VIRTUAL_OUTGOING_ARGS_REGNUM
,
1699 lookup_reg_by_dump_name ("virtual-outgoing-args"));
1700 ASSERT_EQ (VIRTUAL_CFA_REGNUM
,
1701 lookup_reg_by_dump_name ("virtual-cfa"));
1702 ASSERT_EQ (VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM
,
1703 lookup_reg_by_dump_name ("virtual-preferred-stack-boundary"));
1705 /* Verify lookup of non-virtual pseudos. */
1706 ASSERT_EQ (LAST_VIRTUAL_REGISTER
+ 1, lookup_reg_by_dump_name ("<0>"));
1707 ASSERT_EQ (LAST_VIRTUAL_REGISTER
+ 2, lookup_reg_by_dump_name ("<1>"));
1710 /* Verify that edge E is as expected, with the src and dest basic blocks
1711 having indices EXPECTED_SRC_IDX and EXPECTED_DEST_IDX respectively, and
1712 the edge having flags equal to EXPECTED_FLAGS.
1713 Use LOC as the effective location when reporting failures. */
1716 assert_edge_at (const location
&loc
, edge e
, int expected_src_idx
,
1717 int expected_dest_idx
, int expected_flags
)
1719 ASSERT_EQ_AT (loc
, expected_src_idx
, e
->src
->index
);
1720 ASSERT_EQ_AT (loc
, expected_dest_idx
, e
->dest
->index
);
1721 ASSERT_EQ_AT (loc
, expected_flags
, e
->flags
);
1724 /* Verify that edge EDGE is as expected, with the src and dest basic blocks
1725 having indices EXPECTED_SRC_IDX and EXPECTED_DEST_IDX respectively, and
1726 the edge having flags equal to EXPECTED_FLAGS. */
1728 #define ASSERT_EDGE(EDGE, EXPECTED_SRC_IDX, EXPECTED_DEST_IDX, \
1730 assert_edge_at (SELFTEST_LOCATION, EDGE, EXPECTED_SRC_IDX, \
1731 EXPECTED_DEST_IDX, EXPECTED_FLAGS)
1733 /* Verify that we can load RTL dumps. */
1736 test_loading_dump_fragment_1 ()
1738 // TODO: filter on target?
1739 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("asr_div1.rtl"));
1741 /* Verify that the insns were loaded correctly. */
1742 rtx_insn
*insn_1
= get_insns ();
1743 ASSERT_TRUE (insn_1
);
1744 ASSERT_EQ (1, INSN_UID (insn_1
));
1745 ASSERT_EQ (INSN
, GET_CODE (insn_1
));
1746 ASSERT_EQ (SET
, GET_CODE (PATTERN (insn_1
)));
1747 ASSERT_EQ (NULL
, PREV_INSN (insn_1
));
1749 rtx_insn
*insn_2
= NEXT_INSN (insn_1
);
1750 ASSERT_TRUE (insn_2
);
1751 ASSERT_EQ (2, INSN_UID (insn_2
));
1752 ASSERT_EQ (INSN
, GET_CODE (insn_2
));
1753 ASSERT_EQ (insn_1
, PREV_INSN (insn_2
));
1754 ASSERT_EQ (NULL
, NEXT_INSN (insn_2
));
1756 /* Verify that registers were loaded correctly. */
1757 rtx insn_1_dest
= SET_DEST (PATTERN (insn_1
));
1758 ASSERT_EQ (REG
, GET_CODE (insn_1_dest
));
1759 ASSERT_EQ ((LAST_VIRTUAL_REGISTER
+ 1) + 2, REGNO (insn_1_dest
));
1760 rtx insn_1_src
= SET_SRC (PATTERN (insn_1
));
1761 ASSERT_EQ (LSHIFTRT
, GET_CODE (insn_1_src
));
1762 rtx reg
= XEXP (insn_1_src
, 0);
1763 ASSERT_EQ (REG
, GET_CODE (reg
));
1764 ASSERT_EQ (LAST_VIRTUAL_REGISTER
+ 1, REGNO (reg
));
1766 /* Verify that get_insn_by_uid works. */
1767 ASSERT_EQ (insn_1
, get_insn_by_uid (1));
1768 ASSERT_EQ (insn_2
, get_insn_by_uid (2));
1770 /* Verify that basic blocks were created. */
1771 ASSERT_EQ (2, BLOCK_FOR_INSN (insn_1
)->index
);
1772 ASSERT_EQ (2, BLOCK_FOR_INSN (insn_2
)->index
);
1774 /* Verify that the CFG was recreated. */
1776 verify_three_block_rtl_cfg (cfun
);
1777 basic_block bb2
= BASIC_BLOCK_FOR_FN (cfun
, 2);
1778 ASSERT_TRUE (bb2
!= NULL
);
1779 ASSERT_EQ (BB_RTL
, bb2
->flags
& BB_RTL
);
1780 ASSERT_EQ (2, bb2
->index
);
1781 ASSERT_EQ (insn_1
, BB_HEAD (bb2
));
1782 ASSERT_EQ (insn_2
, BB_END (bb2
));
1785 /* Verify loading another RTL dump. */
1788 test_loading_dump_fragment_2 ()
1790 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("simple-cse.rtl"));
1792 rtx_insn
*insn_1
= get_insn_by_uid (1);
1793 rtx_insn
*insn_2
= get_insn_by_uid (2);
1794 rtx_insn
*insn_3
= get_insn_by_uid (3);
1796 rtx set1
= single_set (insn_1
);
1797 ASSERT_NE (NULL
, set1
);
1798 rtx set2
= single_set (insn_2
);
1799 ASSERT_NE (NULL
, set2
);
1800 rtx set3
= single_set (insn_3
);
1801 ASSERT_NE (NULL
, set3
);
1803 rtx src1
= SET_SRC (set1
);
1804 ASSERT_EQ (PLUS
, GET_CODE (src1
));
1806 rtx src2
= SET_SRC (set2
);
1807 ASSERT_EQ (PLUS
, GET_CODE (src2
));
1809 /* Both src1 and src2 refer to "(reg:SI %0)".
1810 Verify that we have pointer equality. */
1811 rtx lhs1
= XEXP (src1
, 0);
1812 rtx lhs2
= XEXP (src2
, 0);
1813 ASSERT_EQ (lhs1
, lhs2
);
1815 /* Verify that the CFG was recreated. */
1817 verify_three_block_rtl_cfg (cfun
);
1820 /* Verify that CODE_LABEL insns are loaded correctly. */
1823 test_loading_labels ()
1825 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("example-labels.rtl"));
1827 rtx_insn
*insn_100
= get_insn_by_uid (100);
1828 ASSERT_EQ (CODE_LABEL
, GET_CODE (insn_100
));
1829 ASSERT_EQ (100, INSN_UID (insn_100
));
1830 ASSERT_EQ (NULL
, LABEL_NAME (insn_100
));
1831 ASSERT_EQ (0, LABEL_NUSES (insn_100
));
1832 ASSERT_EQ (30, CODE_LABEL_NUMBER (insn_100
));
1834 rtx_insn
*insn_200
= get_insn_by_uid (200);
1835 ASSERT_EQ (CODE_LABEL
, GET_CODE (insn_200
));
1836 ASSERT_EQ (200, INSN_UID (insn_200
));
1837 ASSERT_STREQ ("some_label_name", LABEL_NAME (insn_200
));
1838 ASSERT_EQ (0, LABEL_NUSES (insn_200
));
1839 ASSERT_EQ (40, CODE_LABEL_NUMBER (insn_200
));
1841 /* Ensure that the presence of CODE_LABEL_NUMBER == 40
1842 means that the next label num to be handed out will be 41. */
1843 ASSERT_EQ (41, max_label_num ());
1845 /* Ensure that label names read from a dump are GC-managed
1846 and are found through the insn. */
1847 forcibly_ggc_collect ();
1848 ASSERT_TRUE (ggc_marked_p (insn_200
));
1849 ASSERT_TRUE (ggc_marked_p (LABEL_NAME (insn_200
)));
1852 /* Verify that the loader copes with an insn with a mode. */
1855 test_loading_insn_with_mode ()
1857 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("insn-with-mode.rtl"));
1858 rtx_insn
*insn
= get_insns ();
1859 ASSERT_EQ (INSN
, GET_CODE (insn
));
1861 /* Verify that the "TI" mode was set from "insn:TI". */
1862 ASSERT_EQ (TImode
, GET_MODE (insn
));
1865 /* Verify that the loader copes with a jump_insn to a label_ref. */
1868 test_loading_jump_to_label_ref ()
1870 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("jump-to-label-ref.rtl"));
1872 rtx_insn
*jump_insn
= get_insn_by_uid (1);
1873 ASSERT_EQ (JUMP_INSN
, GET_CODE (jump_insn
));
1875 rtx_insn
*barrier
= get_insn_by_uid (2);
1876 ASSERT_EQ (BARRIER
, GET_CODE (barrier
));
1878 rtx_insn
*code_label
= get_insn_by_uid (100);
1879 ASSERT_EQ (CODE_LABEL
, GET_CODE (code_label
));
1881 /* Verify the jump_insn. */
1882 ASSERT_EQ (4, BLOCK_FOR_INSN (jump_insn
)->index
);
1883 ASSERT_EQ (SET
, GET_CODE (PATTERN (jump_insn
)));
1884 /* Ensure that the "(pc)" is using the global singleton. */
1885 ASSERT_RTX_PTR_EQ (pc_rtx
, SET_DEST (PATTERN (jump_insn
)));
1886 rtx label_ref
= SET_SRC (PATTERN (jump_insn
));
1887 ASSERT_EQ (LABEL_REF
, GET_CODE (label_ref
));
1888 ASSERT_EQ (code_label
, label_ref_label (label_ref
));
1889 ASSERT_EQ (code_label
, JUMP_LABEL (jump_insn
));
1891 /* Verify the code_label. */
1892 ASSERT_EQ (5, BLOCK_FOR_INSN (code_label
)->index
);
1893 ASSERT_EQ (NULL
, LABEL_NAME (code_label
));
1894 ASSERT_EQ (1, LABEL_NUSES (code_label
));
1896 /* Verify the generated CFG. */
1898 /* Locate blocks. */
1899 basic_block entry
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
1900 ASSERT_TRUE (entry
!= NULL
);
1901 ASSERT_EQ (ENTRY_BLOCK
, entry
->index
);
1903 basic_block exit
= EXIT_BLOCK_PTR_FOR_FN (cfun
);
1904 ASSERT_TRUE (exit
!= NULL
);
1905 ASSERT_EQ (EXIT_BLOCK
, exit
->index
);
1907 basic_block bb4
= (*cfun
->cfg
->x_basic_block_info
)[4];
1908 basic_block bb5
= (*cfun
->cfg
->x_basic_block_info
)[5];
1909 ASSERT_EQ (4, bb4
->index
);
1910 ASSERT_EQ (5, bb5
->index
);
1913 ASSERT_EQ (NULL
, entry
->preds
);
1914 ASSERT_EQ (1, entry
->succs
->length ());
1915 ASSERT_EDGE ((*entry
->succs
)[0], 0, 4, EDGE_FALLTHRU
);
1918 ASSERT_EQ (1, bb4
->preds
->length ());
1919 ASSERT_EDGE ((*bb4
->preds
)[0], 0, 4, EDGE_FALLTHRU
);
1920 ASSERT_EQ (1, bb4
->succs
->length ());
1921 ASSERT_EDGE ((*bb4
->succs
)[0], 4, 5, 0x0);
1924 ASSERT_EQ (1, bb5
->preds
->length ());
1925 ASSERT_EDGE ((*bb5
->preds
)[0], 4, 5, 0x0);
1926 ASSERT_EQ (1, bb5
->succs
->length ());
1927 ASSERT_EDGE ((*bb5
->succs
)[0], 5, 1, EDGE_FALLTHRU
);
1930 ASSERT_EQ (1, exit
->preds
->length ());
1931 ASSERT_EDGE ((*exit
->preds
)[0], 5, 1, EDGE_FALLTHRU
);
1932 ASSERT_EQ (NULL
, exit
->succs
);
1935 /* Verify that the loader copes with a jump_insn to a label_ref
1939 test_loading_jump_to_return ()
1941 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("jump-to-return.rtl"));
1943 rtx_insn
*jump_insn
= get_insn_by_uid (1);
1944 ASSERT_EQ (JUMP_INSN
, GET_CODE (jump_insn
));
1945 ASSERT_RTX_PTR_EQ (ret_rtx
, JUMP_LABEL (jump_insn
));
1948 /* Verify that the loader copes with a jump_insn to a label_ref
1949 marked "simple_return". */
1952 test_loading_jump_to_simple_return ()
1954 rtl_dump_test
t (SELFTEST_LOCATION
,
1955 locate_file ("jump-to-simple-return.rtl"));
1957 rtx_insn
*jump_insn
= get_insn_by_uid (1);
1958 ASSERT_EQ (JUMP_INSN
, GET_CODE (jump_insn
));
1959 ASSERT_RTX_PTR_EQ (simple_return_rtx
, JUMP_LABEL (jump_insn
));
1962 /* Verify that the loader copes with a NOTE_INSN_BASIC_BLOCK. */
1965 test_loading_note_insn_basic_block ()
1967 rtl_dump_test
t (SELFTEST_LOCATION
,
1968 locate_file ("note_insn_basic_block.rtl"));
1970 rtx_insn
*note
= get_insn_by_uid (1);
1971 ASSERT_EQ (NOTE
, GET_CODE (note
));
1972 ASSERT_EQ (2, BLOCK_FOR_INSN (note
)->index
);
1974 ASSERT_EQ (NOTE_INSN_BASIC_BLOCK
, NOTE_KIND (note
));
1975 ASSERT_EQ (2, NOTE_BASIC_BLOCK (note
)->index
);
1976 ASSERT_EQ (BASIC_BLOCK_FOR_FN (cfun
, 2), NOTE_BASIC_BLOCK (note
));
1979 /* Verify that the loader copes with a NOTE_INSN_DELETED. */
1982 test_loading_note_insn_deleted ()
1984 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("note-insn-deleted.rtl"));
1986 rtx_insn
*note
= get_insn_by_uid (1);
1987 ASSERT_EQ (NOTE
, GET_CODE (note
));
1988 ASSERT_EQ (NOTE_INSN_DELETED
, NOTE_KIND (note
));
1991 /* Verify that the const_int values are consolidated, since
1992 pointer equality corresponds to value equality.
1993 TODO: do this for all in CASE_CONST_UNIQUE. */
1996 test_loading_const_int ()
1998 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("const-int.rtl"));
2000 /* Verify that const_int values below MAX_SAVED_CONST_INT use
2001 the global values. */
2002 ASSERT_EQ (const0_rtx
, SET_SRC (PATTERN (get_insn_by_uid (1))));
2003 ASSERT_EQ (const1_rtx
, SET_SRC (PATTERN (get_insn_by_uid (2))));
2004 ASSERT_EQ (constm1_rtx
, SET_SRC (PATTERN (get_insn_by_uid (3))));
2006 /* Verify that other const_int values are consolidated. */
2007 rtx int256
= gen_rtx_CONST_INT (SImode
, 256);
2008 ASSERT_EQ (int256
, SET_SRC (PATTERN (get_insn_by_uid (4))));
2011 /* Verify that the loader copes with a SYMBOL_REF. */
2014 test_loading_symbol_ref ()
2016 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("symbol-ref.rtl"));
2018 rtx_insn
*insn
= get_insns ();
2020 rtx high
= SET_SRC (PATTERN (insn
));
2021 ASSERT_EQ (HIGH
, GET_CODE (high
));
2023 rtx symbol_ref
= XEXP (high
, 0);
2024 ASSERT_EQ (SYMBOL_REF
, GET_CODE (symbol_ref
));
2026 /* Verify that "[flags 0xc0]" was parsed. */
2027 ASSERT_EQ (0xc0, SYMBOL_REF_FLAGS (symbol_ref
));
2028 /* TODO: we don't yet load SYMBOL_REF_DECL. */
2031 /* Verify that the loader can rebuild a CFG. */
2036 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("cfg-test.rtl"));
2038 ASSERT_STREQ ("cfg_test", IDENTIFIER_POINTER (DECL_NAME (cfun
->decl
)));
2042 ASSERT_TRUE (cfun
->cfg
!= NULL
);
2043 ASSERT_EQ (6, n_basic_blocks_for_fn (cfun
));
2044 ASSERT_EQ (6, n_edges_for_fn (cfun
));
2046 /* The "fake" basic blocks. */
2047 basic_block entry
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
2048 ASSERT_TRUE (entry
!= NULL
);
2049 ASSERT_EQ (ENTRY_BLOCK
, entry
->index
);
2051 basic_block exit
= EXIT_BLOCK_PTR_FOR_FN (cfun
);
2052 ASSERT_TRUE (exit
!= NULL
);
2053 ASSERT_EQ (EXIT_BLOCK
, exit
->index
);
2055 /* The "real" basic blocks. */
2056 basic_block bb2
= (*cfun
->cfg
->x_basic_block_info
)[2];
2057 basic_block bb3
= (*cfun
->cfg
->x_basic_block_info
)[3];
2058 basic_block bb4
= (*cfun
->cfg
->x_basic_block_info
)[4];
2059 basic_block bb5
= (*cfun
->cfg
->x_basic_block_info
)[5];
2061 ASSERT_EQ (2, bb2
->index
);
2062 ASSERT_EQ (3, bb3
->index
);
2063 ASSERT_EQ (4, bb4
->index
);
2064 ASSERT_EQ (5, bb5
->index
);
2066 /* Verify connectivity. */
2069 ASSERT_EQ (NULL
, entry
->preds
);
2070 ASSERT_EQ (1, entry
->succs
->length ());
2071 ASSERT_EDGE ((*entry
->succs
)[0], 0, 2, EDGE_FALLTHRU
);
2074 ASSERT_EQ (1, bb2
->preds
->length ());
2075 ASSERT_EDGE ((*bb2
->preds
)[0], 0, 2, EDGE_FALLTHRU
);
2076 ASSERT_EQ (2, bb2
->succs
->length ());
2077 ASSERT_EDGE ((*bb2
->succs
)[0], 2, 3, EDGE_TRUE_VALUE
);
2078 ASSERT_EDGE ((*bb2
->succs
)[1], 2, 4, EDGE_FALSE_VALUE
);
2081 ASSERT_EQ (1, bb3
->preds
->length ());
2082 ASSERT_EDGE ((*bb3
->preds
)[0], 2, 3, EDGE_TRUE_VALUE
);
2083 ASSERT_EQ (1, bb3
->succs
->length ());
2084 ASSERT_EDGE ((*bb3
->succs
)[0], 3, 5, EDGE_FALLTHRU
);
2087 ASSERT_EQ (1, bb4
->preds
->length ());
2088 ASSERT_EDGE ((*bb4
->preds
)[0], 2, 4, EDGE_FALSE_VALUE
);
2089 ASSERT_EQ (1, bb4
->succs
->length ());
2090 ASSERT_EDGE ((*bb4
->succs
)[0], 4, 5, EDGE_FALLTHRU
);
2093 ASSERT_EQ (2, bb5
->preds
->length ());
2094 ASSERT_EDGE ((*bb5
->preds
)[0], 3, 5, EDGE_FALLTHRU
);
2095 ASSERT_EDGE ((*bb5
->preds
)[1], 4, 5, EDGE_FALLTHRU
);
2096 ASSERT_EQ (1, bb5
->succs
->length ());
2097 ASSERT_EDGE ((*bb5
->succs
)[0], 5, 1, EDGE_FALLTHRU
);
2100 ASSERT_EQ (1, exit
->preds
->length ());
2101 ASSERT_EDGE ((*exit
->preds
)[0], 5, 1, EDGE_FALLTHRU
);
2102 ASSERT_EQ (NULL
, exit
->succs
);
2105 /* Verify that the loader copes with sparse block indices.
2106 This testcase loads a file with a "(block 42)". */
2109 test_loading_bb_index ()
2111 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("bb-index.rtl"));
2113 ASSERT_STREQ ("test_bb_index", IDENTIFIER_POINTER (DECL_NAME (cfun
->decl
)));
2117 ASSERT_TRUE (cfun
->cfg
!= NULL
);
2118 ASSERT_EQ (3, n_basic_blocks_for_fn (cfun
));
2119 ASSERT_EQ (43, basic_block_info_for_fn (cfun
)->length ());
2120 ASSERT_EQ (2, n_edges_for_fn (cfun
));
2122 ASSERT_EQ (NULL
, (*cfun
->cfg
->x_basic_block_info
)[41]);
2123 basic_block bb42
= (*cfun
->cfg
->x_basic_block_info
)[42];
2124 ASSERT_NE (NULL
, bb42
);
2125 ASSERT_EQ (42, bb42
->index
);
2128 /* Verify that function_reader::handle_any_trailing_information correctly
2129 parses all the possible items emitted for a MEM. */
2134 rtl_dump_test
t (SELFTEST_LOCATION
, locate_file ("mem.rtl"));
2136 ASSERT_STREQ ("test_mem", IDENTIFIER_POINTER (DECL_NAME (cfun
->decl
)));
2139 /* Verify parsing of "[42 i+17 S8 A128 AS5]". */
2140 rtx_insn
*insn_1
= get_insn_by_uid (1);
2141 rtx set1
= single_set (insn_1
);
2142 rtx mem1
= SET_DEST (set1
);
2143 ASSERT_EQ (42, MEM_ALIAS_SET (mem1
));
2145 ASSERT_TRUE (MEM_OFFSET_KNOWN_P (mem1
));
2146 ASSERT_KNOWN_EQ (17, MEM_OFFSET (mem1
));
2148 ASSERT_KNOWN_EQ (8, MEM_SIZE (mem1
));
2150 ASSERT_EQ (128, MEM_ALIGN (mem1
));
2152 ASSERT_EQ (5, MEM_ADDR_SPACE (mem1
));
2154 /* Verify parsing of "43 i+18 S9 AS6"
2155 (an address space without an alignment). */
2156 rtx_insn
*insn_2
= get_insn_by_uid (2);
2157 rtx set2
= single_set (insn_2
);
2158 rtx mem2
= SET_DEST (set2
);
2159 ASSERT_EQ (43, MEM_ALIAS_SET (mem2
));
2161 ASSERT_TRUE (MEM_OFFSET_KNOWN_P (mem2
));
2162 ASSERT_KNOWN_EQ (18, MEM_OFFSET (mem2
));
2164 ASSERT_KNOWN_EQ (9, MEM_SIZE (mem2
));
2166 ASSERT_EQ (6, MEM_ADDR_SPACE (mem2
));
2169 /* Run all of the selftests within this file. */
2172 read_rtl_function_c_tests ()
2175 test_parsing_regnos ();
2176 test_loading_dump_fragment_1 ();
2177 test_loading_dump_fragment_2 ();
2178 test_loading_labels ();
2179 test_loading_insn_with_mode ();
2180 test_loading_jump_to_label_ref ();
2181 test_loading_jump_to_return ();
2182 test_loading_jump_to_simple_return ();
2183 test_loading_note_insn_basic_block ();
2184 test_loading_note_insn_deleted ();
2185 test_loading_const_int ();
2186 test_loading_symbol_ref ();
2187 test_loading_cfg ();
2188 test_loading_bb_index ();
2189 test_loading_mem ();
2192 } // namespace selftest
2194 #endif /* #if CHECKING_P */