1/* read-rtl-function.cc - Reader for RTL function dumps
2 Copyright (C) 2016-2023 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
23#include "target.h"
24#include "tree.h"
25#include "diagnostic.h"
26#include "read-md.h"
27#include "rtl.h"
28#include "cfghooks.h"
29#include "stringpool.h"
30#include "function.h"
31#include "tree-cfg.h"
32#include "cfg.h"
33#include "basic-block.h"
34#include "cfgrtl.h"
35#include "memmodel.h"
36#include "emit-rtl.h"
37#include "cgraph.h"
38#include "tree-pass.h"
39#include "toplev.h"
40#include "varasm.h"
41#include "read-rtl-function.h"
42#include "selftest.h"
43#include "selftest-rtl.h"
44#include "regs.h"
45#include "function-abi.h"
46
47/* Forward decls. */
48class function_reader;
49class fixup;
50
51/* Edges are recorded when parsing the "insn-chain" directive,
52 and created at the end when all the blocks ought to exist.
53 This struct records an "edge-from" or "edge-to" directive seen
54 at LOC, which will be turned into an actual CFG edge once
55 the "insn-chain" is fully parsed. */
56
57class deferred_edge
58{
59public:
60 deferred_edge (file_location loc, int src_bb_idx, int dest_bb_idx, int flags)
61 : m_loc (loc), m_src_bb_idx (src_bb_idx), m_dest_bb_idx (dest_bb_idx),
62 m_flags (flags)
63 {}
64
65 file_location m_loc;
66 int m_src_bb_idx;
67 int m_dest_bb_idx;
68 int m_flags;
69};
70
71/* Subclass of rtx_reader for reading function dumps. */
72
73class function_reader : public rtx_reader
74{
75 public:
76 function_reader ();
77 ~function_reader ();
78
79 /* Overridden vfuncs of class md_reader. */
80 void handle_unknown_directive (file_location, const char *) final override;
81
82 /* Overridden vfuncs of class rtx_reader. */
83 rtx read_rtx_operand (rtx x, int idx) final override;
84 void handle_any_trailing_information (rtx x) final override;
85 rtx postprocess (rtx) final override;
86 const char *finalize_string (char *stringbuf) final override;
87
88 rtx_insn **get_insn_by_uid (int uid);
89 tree parse_mem_expr (const char *desc);
90
91 private:
92 void parse_function ();
93 void create_function ();
94 void parse_param ();
95 void parse_insn_chain ();
96 void parse_block ();
97 int parse_bb_idx ();
98 void parse_edge (basic_block block, bool from);
99 rtx_insn *parse_insn (file_location loc, const char *name);
100 void parse_cfg (file_location loc);
101 void parse_crtl (file_location loc);
102 void create_edges ();
103
104 int parse_enum_value (int num_values, const char *const *strings);
105
106 void read_rtx_operand_u (rtx x, int idx);
107 void read_rtx_operand_i_or_n (rtx x, int idx, char format_char);
108 rtx read_rtx_operand_r (rtx x);
109 rtx extra_parsing_for_operand_code_0 (rtx x, int idx);
110
111 void add_fixup_insn_uid (file_location loc, rtx insn, int operand_idx,
112 int insn_uid);
113
114 void add_fixup_note_insn_basic_block (file_location loc, rtx insn,
115 int operand_idx, int bb_idx);
116
117 void add_fixup_source_location (file_location loc, rtx_insn *insn,
118 const char *filename, int lineno, int colno);
119
120 void add_fixup_expr (file_location loc, rtx x,
121 const char *desc);
122
123 rtx consolidate_singletons (rtx x);
124 rtx parse_rtx ();
125 void maybe_read_location (rtx_insn *insn);
126
127 void handle_insn_uids ();
128 void apply_fixups ();
129
130 private:
131 struct uid_hash : int_hash <int, -1, -2> {};
132 hash_map<uid_hash, rtx_insn *> m_insns_by_uid;
133 auto_vec<fixup *> m_fixups;
134 rtx_insn *m_first_insn;
135 auto_vec<tree> m_fake_scope;
136 char *m_name;
137 bool m_have_crtl_directive;
138 basic_block m_bb_to_insert_after;
139 auto_vec <deferred_edge> m_deferred_edges;
140 int m_highest_bb_idx;
141};
142
143/* Abstract base class for recording post-processing steps that must be
144 done after reading a .rtl file. */
145
146class fixup
147{
148 public:
149 /* Constructor for a fixup at LOC affecting X. */
150 fixup (file_location loc, rtx x)
151 : m_loc (loc), m_rtx (x)
152 {}
153 virtual ~fixup () {}
154
155 virtual void apply (function_reader *reader) const = 0;
156
157 protected:
158 file_location m_loc;
159 rtx m_rtx;
160};
161
162/* An abstract subclass of fixup for post-processing steps that
163 act on a specific operand of a specific instruction. */
164
165class operand_fixup : public fixup
166{
167 public:
168 /* Constructor for a fixup at LOC affecting INSN's operand
169 with index OPERAND_IDX. */
170 operand_fixup (file_location loc, rtx insn, int operand_idx)
171 : fixup (loc, insn), m_operand_idx (operand_idx)
172 {}
173
174 protected:
175 int m_operand_idx;
176};
177
178/* A concrete subclass of operand_fixup: fixup an rtx_insn *
179 field based on an integer UID. */
180
181class fixup_insn_uid : public operand_fixup
182{
183 public:
184 /* Constructor for a fixup at LOC affecting INSN's operand
185 with index OPERAND_IDX. Record INSN_UID as the uid. */
186 fixup_insn_uid (file_location loc, rtx insn, int operand_idx, int insn_uid)
187 : operand_fixup (loc, insn, operand_idx),
188 m_insn_uid (insn_uid)
189 {}
190
191 void apply (function_reader *reader) const final override;
192
193 private:
194 int m_insn_uid;
195};
196
197/* A concrete subclass of operand_fixup: fix up a
198 NOTE_INSN_BASIC_BLOCK based on an integer block ID. */
199
200class fixup_note_insn_basic_block : public operand_fixup
201{
202 public:
203 fixup_note_insn_basic_block (file_location loc, rtx insn, int operand_idx,
204 int bb_idx)
205 : operand_fixup (loc, insn, operand_idx),
206 m_bb_idx (bb_idx)
207 {}
208
209 void apply (function_reader *reader) const final override;
210
211 private:
212 int m_bb_idx;
213};
214
215/* A concrete subclass of fixup (not operand_fixup): fix up
216 the expr of an rtx (REG or MEM) based on a textual dump. */
217
218class fixup_expr : public fixup
219{
220 public:
221 fixup_expr (file_location loc, rtx x, const char *desc)
222 : fixup (loc, x),
223 m_desc (xstrdup (desc))
224 {}
225
226 ~fixup_expr () { free (ptr: m_desc); }
227
228 void apply (function_reader *reader) const final override;
229
230 private:
231 char *m_desc;
232};
233
234/* Return a textual description of the operand of INSN with
235 index OPERAND_IDX. */
236
237static const char *
238get_operand_name (rtx insn, int operand_idx)
239{
240 gcc_assert (is_a <rtx_insn *> (insn));
241 switch (operand_idx)
242 {
243 case 0:
244 return "PREV_INSN";
245 case 1:
246 return "NEXT_INSN";
247 default:
248 return NULL;
249 }
250}
251
252/* Fixup an rtx_insn * field based on an integer UID, as read by READER. */
253
254void
255fixup_insn_uid::apply (function_reader *reader) const
256{
257 rtx_insn **insn_from_uid = reader->get_insn_by_uid (uid: m_insn_uid);
258 if (insn_from_uid)
259 XEXP (m_rtx, m_operand_idx) = *insn_from_uid;
260 else
261 {
262 const char *op_name = get_operand_name (insn: m_rtx, operand_idx: m_operand_idx);
263 if (op_name)
264 error_at (m_loc,
265 "insn with UID %i not found for operand %i (`%s') of insn %i",
266 m_insn_uid, m_operand_idx, op_name, INSN_UID (insn: m_rtx));
267 else
268 error_at (m_loc,
269 "insn with UID %i not found for operand %i of insn %i",
270 m_insn_uid, m_operand_idx, INSN_UID (insn: m_rtx));
271 }
272}
273
274/* Fix up a NOTE_INSN_BASIC_BLOCK based on an integer block ID. */
275
276void
277fixup_note_insn_basic_block::apply (function_reader *) const
278{
279 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, m_bb_idx);
280 gcc_assert (bb);
281 NOTE_BASIC_BLOCK (m_rtx) = bb;
282}
283
284/* Fix up the expr of an rtx (REG or MEM) based on a textual dump
285 read by READER. */
286
287void
288fixup_expr::apply (function_reader *reader) const
289{
290 tree expr = reader->parse_mem_expr (desc: m_desc);
291 switch (GET_CODE (m_rtx))
292 {
293 case REG:
294 set_reg_attrs_for_decl_rtl (t: expr, x: m_rtx);
295 break;
296 case MEM:
297 set_mem_expr (m_rtx, expr);
298 break;
299 default:
300 gcc_unreachable ();
301 }
302}
303
304/* Strip trailing whitespace from DESC. */
305
306static void
307strip_trailing_whitespace (char *desc)
308{
309 char *terminator = desc + strlen (s: desc);
310 while (desc < terminator)
311 {
312 terminator--;
313 if (ISSPACE (*terminator))
314 *terminator = '\0';
315 else
316 break;
317 }
318}
319
320/* Return the numeric value n for GET_NOTE_INSN_NAME (n) for STRING,
321 or fail if STRING isn't recognized. */
322
323static int
324parse_note_insn_name (const char *string)
325{
326 for (int i = 0; i < NOTE_INSN_MAX; i++)
327 if (strcmp (s1: string, GET_NOTE_INSN_NAME (i)) == 0)
328 return i;
329 fatal_with_file_and_line ("unrecognized NOTE_INSN name: `%s'", string);
330}
331
332/* Return the register number for NAME, or return -1 if it isn't
333 recognized. */
334
335static int
336lookup_reg_by_dump_name (const char *name)
337{
338 for (int i = 0; i < FIRST_PSEUDO_REGISTER; i++)
339 if (reg_names[i][0]
340 && ! strcmp (s1: name, reg_names[i]))
341 return i;
342
343 /* Also lookup virtuals. */
344 if (!strcmp (s1: name, s2: "virtual-incoming-args"))
345 return VIRTUAL_INCOMING_ARGS_REGNUM;
346 if (!strcmp (s1: name, s2: "virtual-stack-vars"))
347 return VIRTUAL_STACK_VARS_REGNUM;
348 if (!strcmp (s1: name, s2: "virtual-stack-dynamic"))
349 return VIRTUAL_STACK_DYNAMIC_REGNUM;
350 if (!strcmp (s1: name, s2: "virtual-outgoing-args"))
351 return VIRTUAL_OUTGOING_ARGS_REGNUM;
352 if (!strcmp (s1: name, s2: "virtual-cfa"))
353 return VIRTUAL_CFA_REGNUM;
354 if (!strcmp (s1: name, s2: "virtual-preferred-stack-boundary"))
355 return VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM;
356 /* TODO: handle "virtual-reg-%d". */
357
358 /* In compact mode, pseudos are printed with '< and '>' wrapping the regno,
359 offseting it by (LAST_VIRTUAL_REGISTER + 1), so that the
360 first non-virtual pseudo is dumped as "<0>". */
361 if (name[0] == '<' && name[strlen (s: name) - 1] == '>')
362 {
363 int dump_num = atoi (nptr: name + 1);
364 return dump_num + LAST_VIRTUAL_REGISTER + 1;
365 }
366
367 /* Not found. */
368 return -1;
369}
370
371/* class function_reader : public rtx_reader */
372
373/* function_reader's constructor. */
374
375function_reader::function_reader ()
376: rtx_reader (true),
377 m_first_insn (NULL),
378 m_name (NULL),
379 m_have_crtl_directive (false),
380 m_bb_to_insert_after (NULL),
381 m_highest_bb_idx (EXIT_BLOCK)
382{
383}
384
385/* function_reader's destructor. */
386
387function_reader::~function_reader ()
388{
389 int i;
390 fixup *f;
391 FOR_EACH_VEC_ELT (m_fixups, i, f)
392 delete f;
393
394 free (ptr: m_name);
395}
396
397/* Implementation of rtx_reader::handle_unknown_directive,
398 for parsing the remainder of a directive with name NAME
399 seen at START_LOC.
400
401 Require a top-level "function" directive, as emitted by
402 print_rtx_function, and parse it. */
403
404void
405function_reader::handle_unknown_directive (file_location start_loc,
406 const char *name)
407{
408 if (strcmp (s1: name, s2: "function"))
409 fatal_at (start_loc, "expected 'function'");
410
411 if (flag_lto)
412 error ("%<__RTL%> function cannot be compiled with %<-flto%>");
413
414 parse_function ();
415}
416
417/* Parse the output of print_rtx_function (or hand-written data in the
418 same format), having already parsed the "(function" heading, and
419 finishing immediately before the final ")".
420
421 The "param" and "crtl" clauses are optional. */
422
423void
424function_reader::parse_function ()
425{
426 m_name = xstrdup (read_string (star_if_braced: 0));
427
428 create_function ();
429
430 while (1)
431 {
432 int c = read_skip_spaces ();
433 if (c == ')')
434 {
435 unread_char (ch: c);
436 break;
437 }
438 unread_char (ch: c);
439 require_char (expected: '(');
440 file_location loc = get_current_location ();
441 struct md_name directive;
442 read_name (name: &directive);
443 if (strcmp (s1: directive.string, s2: "param") == 0)
444 parse_param ();
445 else if (strcmp (s1: directive.string, s2: "insn-chain") == 0)
446 parse_insn_chain ();
447 else if (strcmp (s1: directive.string, s2: "crtl") == 0)
448 parse_crtl (loc);
449 else
450 fatal_with_file_and_line ("unrecognized directive: %s",
451 directive.string);
452 }
453
454 handle_insn_uids ();
455
456 apply_fixups ();
457
458 /* Rebuild the JUMP_LABEL field of any JUMP_INSNs in the chain, and the
459 LABEL_NUSES of any CODE_LABELs.
460
461 This has to happen after apply_fixups, since only after then do
462 LABEL_REFs have their label_ref_label set up. */
463 rebuild_jump_labels (get_insns ());
464
465 crtl->init_stack_alignment ();
466}
467
468/* Set up state for the function *before* fixups are applied.
469
470 Create "cfun" and a decl for the function.
471 By default, every function decl is hardcoded as
472 int test_1 (int i, int j, int k);
473 Set up various other state:
474 - the cfg and basic blocks (edges are created later, *after* fixups
475 are applied).
476 - add the function to the callgraph. */
477
478void
479function_reader::create_function ()
480{
481 /* We start in cfgrtl mode, rather than cfglayout mode. */
482 rtl_register_cfg_hooks ();
483
484 /* When run from selftests or "rtl1", cfun is NULL.
485 When run from "cc1" for a C function tagged with __RTL, cfun is the
486 tagged function. */
487 if (!cfun)
488 {
489 tree fn_name = get_identifier (m_name ? m_name : "test_1");
490 tree int_type = integer_type_node;
491 tree return_type = int_type;
492 tree arg_types[3] = {int_type, int_type, int_type};
493 tree fn_type = build_function_type_array (return_type, 3, arg_types);
494 tree fndecl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn_name, fn_type);
495 tree resdecl = build_decl (UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
496 return_type);
497 DECL_ARTIFICIAL (resdecl) = 1;
498 DECL_IGNORED_P (resdecl) = 1;
499 DECL_RESULT (fndecl) = resdecl;
500 allocate_struct_function (fndecl, false);
501 /* This sets cfun. */
502 current_function_decl = fndecl;
503 }
504
505 gcc_assert (cfun);
506 gcc_assert (current_function_decl);
507 tree fndecl = current_function_decl;
508
509 /* Mark this function as being specified as __RTL. */
510 cfun->curr_properties |= PROP_rtl;
511
512 /* cc1 normally inits DECL_INITIAL (fndecl) to be error_mark_node.
513 Create a dummy block for it. */
514 DECL_INITIAL (fndecl) = make_node (BLOCK);
515
516 cfun->curr_properties = (PROP_cfg | PROP_rtl);
517
518 /* Do we need this to force cgraphunit.cc to output the function? */
519 DECL_EXTERNAL (fndecl) = 0;
520 DECL_PRESERVE_P (fndecl) = 1;
521
522 /* Add to cgraph. */
523 cgraph_node::finalize_function (fndecl, false);
524
525 /* Create bare-bones cfg. This creates the entry and exit blocks. */
526 init_empty_tree_cfg_for_function (cfun);
527 ENTRY_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
528 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
529 init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun));
530 init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun));
531 m_bb_to_insert_after = ENTRY_BLOCK_PTR_FOR_FN (cfun);
532
533}
534
535/* Look within the params of FNDECL for a param named NAME.
536 Return NULL_TREE if one isn't found. */
537
538static tree
539find_param_by_name (tree fndecl, const char *name)
540{
541 for (tree arg = DECL_ARGUMENTS (fndecl); arg; arg = TREE_CHAIN (arg))
542 if (id_equal (DECL_NAME (arg), str: name))
543 return arg;
544 return NULL_TREE;
545}
546
547/* Parse the content of a "param" directive, having already parsed the
548 "(param". Consume the trailing ')'. */
549
550void
551function_reader::parse_param ()
552{
553 require_char_ws (expected: '"');
554 file_location loc = get_current_location ();
555 char *name = read_quoted_string ();
556
557 /* Lookup param by name. */
558 tree t_param = find_param_by_name (cfun->decl, name);
559 if (!t_param)
560 fatal_at (loc, "param not found: %s", name);
561
562 /* Parse DECL_RTL. */
563 require_char_ws (expected: '(');
564 require_word_ws (expected: "DECL_RTL");
565 DECL_WRTL_CHECK (t_param)->decl_with_rtl.rtl = parse_rtx ();
566 require_char_ws (expected: ')');
567
568 /* Parse DECL_RTL_INCOMING. */
569 require_char_ws (expected: '(');
570 require_word_ws (expected: "DECL_RTL_INCOMING");
571 DECL_INCOMING_RTL (t_param) = parse_rtx ();
572 require_char_ws (expected: ')');
573
574 require_char_ws (expected: ')');
575}
576
577/* Parse zero or more child insn elements within an
578 "insn-chain" element. Consume the trailing ')'. */
579
580void
581function_reader::parse_insn_chain ()
582{
583 while (1)
584 {
585 int c = read_skip_spaces ();
586 file_location loc = get_current_location ();
587 if (c == ')')
588 break;
589 else if (c == '(')
590 {
591 struct md_name directive;
592 read_name (name: &directive);
593 if (strcmp (s1: directive.string, s2: "block") == 0)
594 parse_block ();
595 else
596 parse_insn (loc, name: directive.string);
597 }
598 else
599 fatal_at (loc, "expected '(' or ')'");
600 }
601
602 create_edges ();
603}
604
605/* Parse zero or more child directives (edges and insns) within a
606 "block" directive, having already parsed the "(block " heading.
607 Consume the trailing ')'. */
608
609void
610function_reader::parse_block ()
611{
612 /* Parse the index value from the dump. This will be an integer;
613 we don't support "entry" or "exit" here (unlike for edges). */
614 struct md_name name;
615 read_name (name: &name);
616 int bb_idx = atoi (nptr: name.string);
617
618 /* The term "index" has two meanings for basic blocks in a CFG:
619 (a) the "index" field within struct basic_block_def.
620 (b) the index of a basic_block within the cfg's x_basic_block_info
621 vector, as accessed via BASIC_BLOCK_FOR_FN.
622
623 These can get out-of-sync when basic blocks are optimized away.
624 They get back in sync by "compact_blocks".
625 We reconstruct cfun->cfg->x_basic_block_info->address () pointed
626 vector elements with NULL values in it for any missing basic blocks,
627 so that (a) == (b) for all of the blocks we create. The
628 doubly-linked list of basic blocks (next_bb/prev_bb) skips over
629 these "holes". */
630
631 if (m_highest_bb_idx < bb_idx)
632 m_highest_bb_idx = bb_idx;
633
634 size_t new_size = m_highest_bb_idx + 1;
635 if (basic_block_info_for_fn (cfun)->length () < new_size)
636 vec_safe_grow_cleared (basic_block_info_for_fn (cfun), len: new_size, exact: true);
637
638 last_basic_block_for_fn (cfun) = new_size;
639
640 /* Create the basic block.
641
642 We can't call create_basic_block and use the regular RTL block-creation
643 hooks, since this creates NOTE_INSN_BASIC_BLOCK instances. We don't
644 want to do that; we want to use the notes we were provided with. */
645 basic_block bb = alloc_block ();
646 init_rtl_bb_info (bb);
647 bb->index = bb_idx;
648 bb->flags = BB_NEW | BB_RTL;
649 link_block (bb, m_bb_to_insert_after);
650 m_bb_to_insert_after = bb;
651
652 n_basic_blocks_for_fn (cfun)++;
653 SET_BASIC_BLOCK_FOR_FN (cfun, bb_idx, bb);
654 BB_SET_PARTITION (bb, BB_UNPARTITIONED);
655
656 /* Handle insns, edge-from and edge-to directives. */
657 while (1)
658 {
659 int c = read_skip_spaces ();
660 file_location loc = get_current_location ();
661 if (c == ')')
662 break;
663 else if (c == '(')
664 {
665 struct md_name directive;
666 read_name (name: &directive);
667 if (strcmp (s1: directive.string, s2: "edge-from") == 0)
668 parse_edge (block: bb, from: true);
669 else if (strcmp (s1: directive.string, s2: "edge-to") == 0)
670 parse_edge (block: bb, from: false);
671 else
672 {
673 rtx_insn *insn = parse_insn (loc, name: directive.string);
674 set_block_for_insn (insn, bb);
675 if (!BB_HEAD (bb))
676 BB_HEAD (bb) = insn;
677 BB_END (bb) = insn;
678 }
679 }
680 else
681 fatal_at (loc, "expected '(' or ')'");
682 }
683}
684
685/* Subroutine of function_reader::parse_edge.
686 Parse a basic block index, handling "entry" and "exit". */
687
688int
689function_reader::parse_bb_idx ()
690{
691 struct md_name name;
692 read_name (name: &name);
693 if (strcmp (s1: name.string, s2: "entry") == 0)
694 return ENTRY_BLOCK;
695 if (strcmp (s1: name.string, s2: "exit") == 0)
696 return EXIT_BLOCK;
697 return atoi (nptr: name.string);
698}
699
700/* Subroutine of parse_edge_flags.
701 Parse TOK, a token such as "FALLTHRU", converting to the flag value.
702 Issue an error if the token is unrecognized. */
703
704static int
705parse_edge_flag_token (const char *tok)
706{
707#define DEF_EDGE_FLAG(NAME,IDX) \
708 do { \
709 if (strcmp (tok, #NAME) == 0) \
710 return EDGE_##NAME; \
711 } while (0);
712#include "cfg-flags.def"
713#undef DEF_EDGE_FLAG
714 error ("unrecognized edge flag: %qs", tok);
715 return 0;
716}
717
718/* Subroutine of function_reader::parse_edge.
719 Parse STR and convert to a flag value (or issue an error).
720 The parser uses strtok and hence modifiers STR in-place. */
721
722static int
723parse_edge_flags (char *str)
724{
725 int result = 0;
726
727 char *tok = strtok (s: str, delim: "| ");
728 while (tok)
729 {
730 result |= parse_edge_flag_token (tok);
731 tok = strtok (NULL, delim: "| ");
732 }
733
734 return result;
735}
736
737/* Parse an "edge-from" or "edge-to" directive within the "block"
738 directive for BLOCK, having already parsed the "(edge" heading.
739 Consume the final ")". Record the edge within m_deferred_edges.
740 FROM is true for an "edge-from" directive, false for an "edge-to"
741 directive. */
742
743void
744function_reader::parse_edge (basic_block block, bool from)
745{
746 gcc_assert (block);
747 int this_bb_idx = block->index;
748 file_location loc = get_current_location ();
749 int other_bb_idx = parse_bb_idx ();
750
751 /* "(edge-from 2)" means src = 2, dest = this_bb_idx, whereas
752 "(edge-to 3)" means src = this_bb_idx, dest = 3. */
753 int src_idx = from ? other_bb_idx : this_bb_idx;
754 int dest_idx = from ? this_bb_idx : other_bb_idx;
755
756 /* Optional "(flags)". */
757 int flags = 0;
758 int c = read_skip_spaces ();
759 if (c == '(')
760 {
761 require_word_ws (expected: "flags");
762 require_char_ws (expected: '"');
763 char *str = read_quoted_string ();
764 flags = parse_edge_flags (str);
765 require_char_ws (expected: ')');
766 }
767 else
768 unread_char (ch: c);
769
770 require_char_ws (expected: ')');
771
772 /* This BB already exists, but the other BB might not yet.
773 For now, save the edges, and create them at the end of insn-chain
774 processing. */
775 /* For now, only process the (edge-from) to this BB, and (edge-to)
776 that go to the exit block.
777 FIXME: we don't yet verify that the edge-from and edge-to directives
778 are consistent. */
779 if (from || dest_idx == EXIT_BLOCK)
780 m_deferred_edges.safe_push (obj: deferred_edge (loc, src_idx, dest_idx, flags));
781}
782
783/* Parse an rtx instruction, having parsed the opening and parenthesis, and
784 name NAME, seen at START_LOC, by calling read_rtx_code, calling
785 set_first_insn and set_last_insn as appropriate, and
786 adding the insn to the insn chain.
787 Consume the trailing ')'. */
788
789rtx_insn *
790function_reader::parse_insn (file_location start_loc, const char *name)
791{
792 rtx x = read_rtx_code (code_name: name);
793 if (!x)
794 fatal_at (start_loc, "expected insn type; got '%s'", name);
795 rtx_insn *insn = dyn_cast <rtx_insn *> (p: x);
796 if (!insn)
797 fatal_at (start_loc, "expected insn type; got '%s'", name);
798
799 /* Consume the trailing ')'. */
800 require_char_ws (expected: ')');
801
802 rtx_insn *last_insn = get_last_insn ();
803
804 /* Add "insn" to the insn chain. */
805 if (last_insn)
806 {
807 gcc_assert (NEXT_INSN (last_insn) == NULL);
808 SET_NEXT_INSN (last_insn) = insn;
809 }
810 SET_PREV_INSN (insn) = last_insn;
811
812 /* Add it to the sequence. */
813 set_last_insn (insn);
814 if (!m_first_insn)
815 {
816 m_first_insn = insn;
817 set_first_insn (insn);
818 }
819
820 if (rtx_code_label *label = dyn_cast <rtx_code_label *> (p: insn))
821 maybe_set_max_label_num (x: label);
822
823 return insn;
824}
825
826/* Postprocessing subroutine for parse_insn_chain: all the basic blocks
827 should have been created by now; create the edges that were seen. */
828
829void
830function_reader::create_edges ()
831{
832 int i;
833 deferred_edge *de;
834 FOR_EACH_VEC_ELT (m_deferred_edges, i, de)
835 {
836 /* The BBs should already have been created by parse_block. */
837 basic_block src = BASIC_BLOCK_FOR_FN (cfun, de->m_src_bb_idx);
838 if (!src)
839 fatal_at (de->m_loc, "error: block index %i not found",
840 de->m_src_bb_idx);
841 basic_block dst = BASIC_BLOCK_FOR_FN (cfun, de->m_dest_bb_idx);
842 if (!dst)
843 fatal_at (de->m_loc, "error: block with index %i not found",
844 de->m_dest_bb_idx);
845 unchecked_make_edge (src, dst, de->m_flags);
846 }
847}
848
849/* Parse a "crtl" directive, having already parsed the "(crtl" heading
850 at location LOC.
851 Consume the final ")". */
852
853void
854function_reader::parse_crtl (file_location loc)
855{
856 if (m_have_crtl_directive)
857 error_at (loc, "more than one 'crtl' directive");
858 m_have_crtl_directive = true;
859
860 /* return_rtx. */
861 require_char_ws (expected: '(');
862 require_word_ws (expected: "return_rtx");
863 crtl->return_rtx = parse_rtx ();
864 require_char_ws (expected: ')');
865
866 require_char_ws (expected: ')');
867}
868
869/* Parse operand IDX of X, returning X, or an equivalent rtx
870 expression (for consolidating singletons).
871 This is an overridden implementation of rtx_reader::read_rtx_operand for
872 function_reader, handling various extra data printed by print_rtx,
873 and sometimes calling the base class implementation. */
874
875rtx
876function_reader::read_rtx_operand (rtx x, int idx)
877{
878 RTX_CODE code = GET_CODE (x);
879 const char *format_ptr = GET_RTX_FORMAT (code);
880 const char format_char = format_ptr[idx];
881 struct md_name name;
882
883 /* Override the regular parser for some format codes. */
884 switch (format_char)
885 {
886 case 'e':
887 if (idx == 7 && CALL_P (x))
888 {
889 m_in_call_function_usage = true;
890 rtx tem = rtx_reader::read_rtx_operand (return_rtx: x, idx);
891 m_in_call_function_usage = false;
892 return tem;
893 }
894 else
895 return rtx_reader::read_rtx_operand (return_rtx: x, idx);
896 break;
897
898 case 'u':
899 read_rtx_operand_u (x, idx);
900 /* Don't run regular parser for 'u'. */
901 return x;
902
903 case 'i':
904 case 'n':
905 read_rtx_operand_i_or_n (x, idx, format_char);
906 /* Don't run regular parser for these codes. */
907 return x;
908
909 case 'B':
910 gcc_assert (is_compact ());
911 /* Compact mode doesn't store BBs. */
912 /* Don't run regular parser. */
913 return x;
914
915 case 'r':
916 /* Don't run regular parser for 'r'. */
917 return read_rtx_operand_r (x);
918
919 default:
920 break;
921 }
922
923 /* Call base class implementation. */
924 x = rtx_reader::read_rtx_operand (return_rtx: x, idx);
925
926 /* Handle any additional parsing needed to handle what the dump
927 could contain. */
928 switch (format_char)
929 {
930 case '0':
931 x = extra_parsing_for_operand_code_0 (x, idx);
932 break;
933
934 case 'w':
935 if (!is_compact ())
936 {
937 /* Strip away the redundant hex dump of the value. */
938 require_char_ws (expected: '[');
939 read_name (name: &name);
940 require_char_ws (expected: ']');
941 }
942 break;
943
944 default:
945 break;
946 }
947
948 return x;
949}
950
951/* Parse operand IDX of X, of code 'u', when reading function dumps.
952
953 The RTL file recorded the ID of an insn (or 0 for NULL); we
954 must store this as a pointer, but the insn might not have
955 been loaded yet. Store the ID away for now, via a fixup. */
956
957void
958function_reader::read_rtx_operand_u (rtx x, int idx)
959{
960 /* In compact mode, the PREV/NEXT insn uids are not dumped, so skip
961 the "uu" when reading. */
962 if (is_compact () && GET_CODE (x) != LABEL_REF)
963 return;
964
965 struct md_name name;
966 file_location loc = read_name (name: &name);
967 int insn_id = atoi (nptr: name.string);
968 if (insn_id)
969 add_fixup_insn_uid (loc, insn: x, operand_idx: idx, insn_uid: insn_id);
970}
971
972/* Read a name, looking for a match against a string found in array
973 STRINGS of size NUM_VALUES.
974 Return the index of the matched string, or emit an error. */
975
976int
977function_reader::parse_enum_value (int num_values, const char *const *strings)
978{
979 struct md_name name;
980 read_name (name: &name);
981 for (int i = 0; i < num_values; i++)
982 {
983 if (strcmp (s1: name.string, s2: strings[i]) == 0)
984 return i;
985 }
986 error ("unrecognized enum value: %qs", name.string);
987 return 0;
988}
989
990/* Parse operand IDX of X, of code 'i' or 'n' (as specified by FORMAT_CHAR).
991 Special-cased handling of these, for reading function dumps. */
992
993void
994function_reader::read_rtx_operand_i_or_n (rtx x, int idx,
995 char format_char)
996{
997 /* Handle some of the extra information that print_rtx
998 can write out for these cases. */
999 /* print_rtx only writes out operand 5 for notes
1000 for NOTE_KIND values NOTE_INSN_DELETED_LABEL
1001 and NOTE_INSN_DELETED_DEBUG_LABEL. */
1002 if (idx == 5 && NOTE_P (x))
1003 return;
1004
1005 if (idx == 4 && INSN_P (x))
1006 {
1007 maybe_read_location (insn: as_a <rtx_insn *> (p: x));
1008 return;
1009 }
1010
1011 /* INSN_CODEs aren't printed in compact mode, so don't attempt to
1012 parse them. */
1013 if (is_compact ()
1014 && INSN_P (x)
1015 && &INSN_CODE (x) == &XINT (x, idx))
1016 {
1017 INSN_CODE (x) = -1;
1018 return;
1019 }
1020
1021 /* Handle UNSPEC and UNSPEC_VOLATILE's operand 1. */
1022#if !defined(GENERATOR_FILE) && NUM_UNSPECV_VALUES > 0
1023 if (idx == 1
1024 && GET_CODE (x) == UNSPEC_VOLATILE)
1025 {
1026 XINT (x, 1)
1027 = parse_enum_value (NUM_UNSPECV_VALUES, strings: unspecv_strings);
1028 return;
1029 }
1030#endif
1031#if !defined(GENERATOR_FILE) && NUM_UNSPEC_VALUES > 0
1032 if (idx == 1
1033 && (GET_CODE (x) == UNSPEC
1034 || GET_CODE (x) == UNSPEC_VOLATILE))
1035 {
1036 XINT (x, 1)
1037 = parse_enum_value (NUM_UNSPEC_VALUES, strings: unspec_strings);
1038 return;
1039 }
1040#endif
1041
1042 struct md_name name;
1043 read_name (name: &name);
1044 int value;
1045 if (format_char == 'n')
1046 value = parse_note_insn_name (string: name.string);
1047 else
1048 value = atoi (nptr: name.string);
1049 XINT (x, idx) = value;
1050}
1051
1052/* Parse the 'r' operand of X, returning X, or an equivalent rtx
1053 expression (for consolidating singletons).
1054 Special-cased handling of code 'r' for reading function dumps. */
1055
1056rtx
1057function_reader::read_rtx_operand_r (rtx x)
1058{
1059 struct md_name name;
1060 file_location loc = read_name (name: &name);
1061 int regno = lookup_reg_by_dump_name (name: name.string);
1062 if (regno == -1)
1063 fatal_at (loc, "unrecognized register: '%s'", name.string);
1064
1065 set_regno_raw (x, regno, nregs: 1);
1066
1067 /* Consolidate singletons. */
1068 x = consolidate_singletons (x);
1069
1070 ORIGINAL_REGNO (x) = regno;
1071
1072 /* Parse extra stuff at end of 'r'.
1073 We may have zero, one, or two sections marked by square
1074 brackets. */
1075 int ch = read_skip_spaces ();
1076 bool expect_original_regno = false;
1077 if (ch == '[')
1078 {
1079 file_location loc = get_current_location ();
1080 char *desc = read_until (terminator_chars: "]", consume_terminator: true);
1081 strip_trailing_whitespace (desc);
1082 const char *desc_start = desc;
1083 /* If ORIGINAL_REGNO (rtx) != regno, we will have:
1084 "orig:%i", ORIGINAL_REGNO (rtx).
1085 Consume it, we don't set ORIGINAL_REGNO, since we can
1086 get that from the 2nd copy later. */
1087 if (startswith (str: desc, prefix: "orig:"))
1088 {
1089 expect_original_regno = true;
1090 desc_start += 5;
1091 /* Skip to any whitespace following the integer. */
1092 const char *space = strchr (s: desc_start, c: ' ');
1093 if (space)
1094 desc_start = space + 1;
1095 }
1096 /* Any remaining text may be the REG_EXPR. Alternatively we have
1097 no REG_ATTRS, and instead we have ORIGINAL_REGNO. */
1098 if (ISDIGIT (*desc_start))
1099 {
1100 /* Assume we have ORIGINAL_REGNO. */
1101 ORIGINAL_REGNO (x) = atoi (nptr: desc_start);
1102 }
1103 else
1104 {
1105 /* Assume we have REG_EXPR. */
1106 add_fixup_expr (loc, x, desc: desc_start);
1107 }
1108 free (ptr: desc);
1109 }
1110 else
1111 unread_char (ch);
1112 if (expect_original_regno)
1113 {
1114 require_char_ws (expected: '[');
1115 char *desc = read_until (terminator_chars: "]", consume_terminator: true);
1116 ORIGINAL_REGNO (x) = atoi (nptr: desc);
1117 free (ptr: desc);
1118 }
1119
1120 return x;
1121}
1122
1123/* Additional parsing for format code '0' in dumps, handling a variety
1124 of special-cases in print_rtx, when parsing operand IDX of X.
1125 Return X, or possibly a reallocated copy of X. */
1126
1127rtx
1128function_reader::extra_parsing_for_operand_code_0 (rtx x, int idx)
1129{
1130 RTX_CODE code = GET_CODE (x);
1131 int c;
1132 struct md_name name;
1133
1134 if (idx == 1 && code == SYMBOL_REF)
1135 {
1136 /* Possibly wrote " [flags %#x]", SYMBOL_REF_FLAGS (in_rtx). */
1137 c = read_skip_spaces ();
1138 if (c == '[')
1139 {
1140 file_location loc = read_name (name: &name);
1141 if (strcmp (s1: name.string, s2: "flags"))
1142 error_at (loc, "was expecting `%s'", "flags");
1143 read_name (name: &name);
1144 SYMBOL_REF_FLAGS (x) = strtol (nptr: name.string, NULL, base: 16);
1145
1146 /* The standard RTX_CODE_SIZE (SYMBOL_REF) used when allocating
1147 x doesn't have space for the block_symbol information, so
1148 we must reallocate it if this flag is set. */
1149 if (SYMBOL_REF_HAS_BLOCK_INFO_P (x))
1150 {
1151 /* Emulate the allocation normally done by
1152 varasm.cc:create_block_symbol. */
1153 unsigned int size = RTX_HDR_SIZE + sizeof (struct block_symbol);
1154 rtx new_x = (rtx) ggc_internal_alloc (s: size);
1155
1156 /* Copy data over from the smaller SYMBOL_REF. */
1157 memcpy (dest: new_x, src: x, RTX_CODE_SIZE (SYMBOL_REF));
1158 x = new_x;
1159
1160 /* We can't reconstruct SYMBOL_REF_BLOCK; set it to NULL. */
1161 SYMBOL_REF_BLOCK (x) = NULL;
1162
1163 /* Zero the offset. */
1164 SYMBOL_REF_BLOCK_OFFSET (x) = 0;
1165 }
1166
1167 require_char (expected: ']');
1168 }
1169 else
1170 unread_char (ch: c);
1171
1172 /* If X had a non-NULL SYMBOL_REF_DECL,
1173 rtx_writer::print_rtx_operand_code_0 would have dumped it
1174 using print_node_brief.
1175 Skip the content for now. */
1176 c = read_skip_spaces ();
1177 if (c == '<')
1178 {
1179 while (1)
1180 {
1181 char ch = read_char ();
1182 if (ch == '>')
1183 break;
1184 }
1185 }
1186 else
1187 unread_char (ch: c);
1188 }
1189 else if (idx == 3 && code == NOTE)
1190 {
1191 /* Note-specific data appears for operand 3, which annoyingly
1192 is before the enum specifying which kind of note we have
1193 (operand 4). */
1194 c = read_skip_spaces ();
1195 if (c == '[')
1196 {
1197 /* Possibly data for a NOTE_INSN_BASIC_BLOCK, of the form:
1198 [bb %d]. */
1199 file_location bb_loc = read_name (name: &name);
1200 if (strcmp (s1: name.string, s2: "bb"))
1201 error_at (bb_loc, "was expecting `%s'", "bb");
1202 read_name (name: &name);
1203 int bb_idx = atoi (nptr: name.string);
1204 add_fixup_note_insn_basic_block (loc: bb_loc, insn: x, operand_idx: idx,
1205 bb_idx);
1206 require_char_ws (expected: ']');
1207 }
1208 else
1209 unread_char (ch: c);
1210 }
1211
1212 return x;
1213}
1214
1215/* Implementation of rtx_reader::handle_any_trailing_information.
1216 Handle the various additional information that print-rtl.cc can
1217 write after the regular fields, when parsing X. */
1218
1219void
1220function_reader::handle_any_trailing_information (rtx x)
1221{
1222 struct md_name name;
1223
1224 switch (GET_CODE (x))
1225 {
1226 case MEM:
1227 {
1228 int ch;
1229 require_char_ws (expected: '[');
1230 read_name (name: &name);
1231 set_mem_alias_set (x, atoi (nptr: name.string));
1232 /* We have either a MEM_EXPR, or a space. */
1233 if (peek_char () != ' ')
1234 {
1235 file_location loc = get_current_location ();
1236 char *desc = read_until (terminator_chars: " +", consume_terminator: false);
1237 add_fixup_expr (loc, x: consolidate_singletons (x), desc);
1238 free (ptr: desc);
1239 }
1240 else
1241 read_char ();
1242
1243 /* We may optionally have '+' for MEM_OFFSET_KNOWN_P. */
1244 ch = read_skip_spaces ();
1245 if (ch == '+')
1246 {
1247 read_name (name: &name);
1248 set_mem_offset (x, atoi (nptr: name.string));
1249 }
1250 else
1251 unread_char (ch);
1252
1253 /* Handle optional " S" for MEM_SIZE. */
1254 ch = read_skip_spaces ();
1255 if (ch == 'S')
1256 {
1257 read_name (name: &name);
1258 set_mem_size (x, atoi (nptr: name.string));
1259 }
1260 else
1261 unread_char (ch);
1262
1263 /* Handle optional " A" for MEM_ALIGN. */
1264 ch = read_skip_spaces ();
1265 if (ch == 'A' && peek_char () != 'S')
1266 {
1267 read_name (name: &name);
1268 set_mem_align (x, atoi (nptr: name.string));
1269 }
1270 else
1271 unread_char (ch);
1272
1273 /* Handle optional " AS" for MEM_ADDR_SPACE. */
1274 ch = read_skip_spaces ();
1275 if (ch == 'A' && peek_char () == 'S')
1276 {
1277 read_char ();
1278 read_name (name: &name);
1279 set_mem_addr_space (x, atoi (nptr: name.string));
1280 }
1281 else
1282 unread_char (ch);
1283
1284 require_char (expected: ']');
1285 }
1286 break;
1287
1288 case CODE_LABEL:
1289 /* Assume that LABEL_NUSES was not dumped. */
1290 /* TODO: parse LABEL_KIND. */
1291 /* For now, skip until closing ')'. */
1292 do
1293 {
1294 char ch = read_char ();
1295 if (ch == ')')
1296 {
1297 unread_char (ch);
1298 break;
1299 }
1300 }
1301 while (1);
1302 break;
1303
1304 default:
1305 break;
1306 }
1307}
1308
1309/* Parse a tree dump for a MEM_EXPR in DESC and turn it back into a tree.
1310 We handle "<retval>" and param names within cfun, but for anything else
1311 we "cheat" by building a global VAR_DECL of type "int" with that name
1312 (returning the same global for a name if we see the same name more
1313 than once). */
1314
1315tree
1316function_reader::parse_mem_expr (const char *desc)
1317{
1318 tree fndecl = cfun->decl;
1319
1320 if (strcmp (s1: desc, s2: "<retval>") == 0)
1321 return DECL_RESULT (fndecl);
1322
1323 tree param = find_param_by_name (fndecl, name: desc);
1324 if (param)
1325 return param;
1326
1327 /* Search within decls we already created.
1328 FIXME: use a hash rather than linear search. */
1329 int i;
1330 tree t;
1331 FOR_EACH_VEC_ELT (m_fake_scope, i, t)
1332 if (id_equal (DECL_NAME (t), str: desc))
1333 return t;
1334
1335 /* Not found? Create it.
1336 This allows mimicking of real data but avoids having to specify
1337 e.g. names of locals, params etc.
1338 Though this way we don't know if we have a PARM_DECL vs a VAR_DECL,
1339 and we don't know the types. Fake it by making everything be
1340 a VAR_DECL of "int" type. */
1341 t = build_decl (UNKNOWN_LOCATION, VAR_DECL,
1342 get_identifier (desc),
1343 integer_type_node);
1344 m_fake_scope.safe_push (obj: t);
1345 return t;
1346}
1347
1348/* Record that at LOC we saw an insn uid INSN_UID for the operand with index
1349 OPERAND_IDX within INSN, so that the pointer value can be fixed up in
1350 later post-processing. */
1351
1352void
1353function_reader::add_fixup_insn_uid (file_location loc, rtx insn, int operand_idx,
1354 int insn_uid)
1355{
1356 m_fixups.safe_push (obj: new fixup_insn_uid (loc, insn, operand_idx, insn_uid));
1357}
1358
1359/* Record that at LOC we saw an basic block index BB_IDX for the operand with index
1360 OPERAND_IDX within INSN, so that the pointer value can be fixed up in
1361 later post-processing. */
1362
1363void
1364function_reader::add_fixup_note_insn_basic_block (file_location loc, rtx insn,
1365 int operand_idx, int bb_idx)
1366{
1367 m_fixups.safe_push (obj: new fixup_note_insn_basic_block (loc, insn, operand_idx,
1368 bb_idx));
1369}
1370
1371/* Placeholder hook for recording source location information seen in a dump.
1372 This is empty for now. */
1373
1374void
1375function_reader::add_fixup_source_location (file_location, rtx_insn *,
1376 const char *, int, int)
1377{
1378}
1379
1380/* Record that at LOC we saw textual description DESC of the MEM_EXPR or REG_EXPR
1381 of INSN, so that the fields can be fixed up in later post-processing. */
1382
1383void
1384function_reader::add_fixup_expr (file_location loc, rtx insn,
1385 const char *desc)
1386{
1387 gcc_assert (desc);
1388 /* Fail early if the RTL reader erroneously hands us an int. */
1389 gcc_assert (!ISDIGIT (desc[0]));
1390
1391 m_fixups.safe_push (obj: new fixup_expr (loc, insn, desc));
1392}
1393
1394/* Helper function for consolidate_reg. Return the global rtx for
1395 the register with regno REGNO. */
1396
1397static rtx
1398lookup_global_register (int regno)
1399{
1400 /* We can't use a switch here, as some of the REGNUMs might not be constants
1401 for some targets. */
1402 if (regno == STACK_POINTER_REGNUM)
1403 return stack_pointer_rtx;
1404 else if (regno == FRAME_POINTER_REGNUM)
1405 return frame_pointer_rtx;
1406 else if (regno == HARD_FRAME_POINTER_REGNUM)
1407 return hard_frame_pointer_rtx;
1408 else if (regno == ARG_POINTER_REGNUM)
1409 return arg_pointer_rtx;
1410 else if (regno == VIRTUAL_INCOMING_ARGS_REGNUM)
1411 return virtual_incoming_args_rtx;
1412 else if (regno == VIRTUAL_STACK_VARS_REGNUM)
1413 return virtual_stack_vars_rtx;
1414 else if (regno == VIRTUAL_STACK_DYNAMIC_REGNUM)
1415 return virtual_stack_dynamic_rtx;
1416 else if (regno == VIRTUAL_OUTGOING_ARGS_REGNUM)
1417 return virtual_outgoing_args_rtx;
1418 else if (regno == VIRTUAL_CFA_REGNUM)
1419 return virtual_cfa_rtx;
1420 else if (regno == VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM)
1421 return virtual_preferred_stack_boundary_rtx;
1422#ifdef return_ADDRESS_POINTER_REGNUM
1423 else if (regno == RETURN_ADDRESS_POINTER_REGNUM)
1424 return return_address_pointer_rtx;
1425#endif
1426
1427 return NULL;
1428}
1429
1430/* Ensure that the backend can cope with a REG with regno REGNO.
1431 Normally REG instances are created by gen_reg_rtx which updates
1432 regno_reg_rtx, growing it as necessary.
1433 The REG instances created from the dumpfile weren't created this
1434 way, so we need to manually update regno_reg_rtx. */
1435
1436static void
1437ensure_regno (int regno)
1438{
1439 if (reg_rtx_no < regno + 1)
1440 reg_rtx_no = regno + 1;
1441
1442 crtl->emit.ensure_regno_capacity ();
1443 gcc_assert (regno < crtl->emit.regno_pointer_align_length);
1444}
1445
1446/* Helper function for consolidate_singletons, for handling REG instances.
1447 Given REG instance X of some regno, return the singleton rtx for that
1448 regno, if it exists, or X. */
1449
1450static rtx
1451consolidate_reg (rtx x)
1452{
1453 gcc_assert (GET_CODE (x) == REG);
1454
1455 unsigned int regno = REGNO (x);
1456
1457 ensure_regno (regno);
1458
1459 /* Some register numbers have their rtx created in init_emit_regs
1460 e.g. stack_pointer_rtx for STACK_POINTER_REGNUM.
1461 Consolidate on this. */
1462 rtx global_reg = lookup_global_register (regno);
1463 if (global_reg)
1464 return global_reg;
1465
1466 /* Populate regno_reg_rtx if necessary. */
1467 if (regno_reg_rtx[regno] == NULL)
1468 regno_reg_rtx[regno] = x;
1469 /* Use it. */
1470 gcc_assert (GET_CODE (regno_reg_rtx[regno]) == REG);
1471 gcc_assert (REGNO (regno_reg_rtx[regno]) == regno);
1472 if (GET_MODE (x) == GET_MODE (regno_reg_rtx[regno]))
1473 return regno_reg_rtx[regno];
1474
1475 return x;
1476}
1477
1478/* When reading RTL function dumps, we must consolidate some
1479 rtx so that we use singletons where singletons are expected
1480 (e.g. we don't want multiple "(const_int 0 [0])" rtx, since
1481 these are tested via pointer equality against const0_rtx.
1482
1483 Return the equivalent singleton rtx for X, if any, otherwise X. */
1484
1485rtx
1486function_reader::consolidate_singletons (rtx x)
1487{
1488 if (!x)
1489 return x;
1490
1491 switch (GET_CODE (x))
1492 {
1493 case PC: return pc_rtx;
1494 case RETURN: return ret_rtx;
1495 case SIMPLE_RETURN: return simple_return_rtx;
1496
1497 case REG:
1498 return consolidate_reg (x);
1499
1500 case CONST_INT:
1501 return gen_rtx_CONST_INT (GET_MODE (x), INTVAL (x));
1502
1503 case CONST_VECTOR:
1504 return gen_rtx_CONST_VECTOR (GET_MODE (x), XVEC (x, 0));
1505
1506 default:
1507 break;
1508 }
1509
1510 return x;
1511}
1512
1513/* Parse an rtx directive, including both the opening/closing parentheses,
1514 and the name. */
1515
1516rtx
1517function_reader::parse_rtx ()
1518{
1519 require_char_ws (expected: '(');
1520 struct md_name directive;
1521 read_name (name: &directive);
1522 rtx result
1523 = consolidate_singletons (x: read_rtx_code (code_name: directive.string));
1524 require_char_ws (expected: ')');
1525
1526 return result;
1527}
1528
1529/* Implementation of rtx_reader::postprocess for reading function dumps.
1530 Return the equivalent singleton rtx for X, if any, otherwise X. */
1531
1532rtx
1533function_reader::postprocess (rtx x)
1534{
1535 return consolidate_singletons (x);
1536}
1537
1538/* Implementation of rtx_reader::finalize_string for reading function dumps.
1539 Make a GC-managed copy of STRINGBUF. */
1540
1541const char *
1542function_reader::finalize_string (char *stringbuf)
1543{
1544 return ggc_strdup (stringbuf);
1545}
1546
1547/* Attempt to parse optional location information for insn INSN, as
1548 potentially written out by rtx_writer::print_rtx_operand_code_i.
1549 We look for a quoted string followed by a colon. */
1550
1551void
1552function_reader::maybe_read_location (rtx_insn *insn)
1553{
1554 file_location loc = get_current_location ();
1555
1556 /* Attempt to parse a quoted string. */
1557 int ch = read_skip_spaces ();
1558 if (ch == '"')
1559 {
1560 char *filename = read_quoted_string ();
1561 require_char (expected: ':');
1562 struct md_name line_num;
1563 read_name (name: &line_num);
1564
1565 int column = 0;
1566 int ch = read_char ();
1567 if (ch == ':')
1568 {
1569 struct md_name column_num;
1570 read_name (name: &column_num);
1571 column = atoi (nptr: column_num.string);
1572 }
1573 else
1574 unread_char (ch);
1575 add_fixup_source_location (loc, insn, filename,
1576 atoi (nptr: line_num.string),
1577 column);
1578 }
1579 else
1580 unread_char (ch);
1581}
1582
1583/* Postprocessing subroutine of function_reader::parse_function.
1584 Populate m_insns_by_uid. */
1585
1586void
1587function_reader::handle_insn_uids ()
1588{
1589 /* Locate the currently assigned INSN_UID values, storing
1590 them in m_insns_by_uid. */
1591 int max_uid = 0;
1592 for (rtx_insn *insn = get_insns (); insn; insn = NEXT_INSN (insn))
1593 {
1594 if (m_insns_by_uid.get (k: INSN_UID (insn)))
1595 error ("duplicate insn UID: %i", INSN_UID (insn));
1596 m_insns_by_uid.put (k: INSN_UID (insn), v: insn);
1597 if (INSN_UID (insn) > max_uid)
1598 max_uid = INSN_UID (insn);
1599 }
1600
1601 /* Ensure x_cur_insn_uid is 1 more than the biggest insn UID seen.
1602 This is normally updated by the various make_*insn_raw functions. */
1603 crtl->emit.x_cur_insn_uid = max_uid + 1;
1604}
1605
1606/* Apply all of the recorded fixups. */
1607
1608void
1609function_reader::apply_fixups ()
1610{
1611 int i;
1612 fixup *f;
1613 FOR_EACH_VEC_ELT (m_fixups, i, f)
1614 f->apply (reader: this);
1615}
1616
1617/* Given a UID value, try to locate a pointer to the corresponding
1618 rtx_insn *, or NULL if it can't be found. */
1619
1620rtx_insn **
1621function_reader::get_insn_by_uid (int uid)
1622{
1623 return m_insns_by_uid.get (k: uid);
1624}
1625
1626/* Run the RTL dump parser, parsing a dump located at PATH.
1627 Return true iff the file was successfully parsed. */
1628
1629bool
1630read_rtl_function_body (const char *path)
1631{
1632 initialize_rtl ();
1633 crtl->abi = &default_function_abi;
1634 init_emit ();
1635 init_varasm_status ();
1636
1637 function_reader reader;
1638 if (!reader.read_file (filename: path))
1639 return false;
1640
1641 return true;
1642}
1643
1644/* Run the RTL dump parser on the range of lines between START_LOC and
1645 END_LOC (including those lines). */
1646
1647bool
1648read_rtl_function_body_from_file_range (location_t start_loc,
1649 location_t end_loc)
1650{
1651 expanded_location exploc_start = expand_location (start_loc);
1652 expanded_location exploc_end = expand_location (end_loc);
1653
1654 if (exploc_start.file != exploc_end.file)
1655 {
1656 error_at (end_loc, "start/end of RTL fragment are in different files");
1657 return false;
1658 }
1659 if (exploc_start.line >= exploc_end.line)
1660 {
1661 error_at (end_loc,
1662 "start of RTL fragment must be on an earlier line than end");
1663 return false;
1664 }
1665
1666 initialize_rtl ();
1667 crtl->abi = &fndecl_abi (cfun->decl).base_abi ();
1668 init_emit ();
1669 init_varasm_status ();
1670
1671 function_reader reader;
1672 if (!reader.read_file_fragment (filename: exploc_start.file, first_line: exploc_start.line,
1673 last_line: exploc_end.line - 1))
1674 return false;
1675
1676 return true;
1677}
1678
1679#if CHECKING_P
1680
1681namespace selftest {
1682
1683/* Verify that parse_edge_flags works. */
1684
1685static void
1686test_edge_flags ()
1687{
1688 /* parse_edge_flags modifies its input (due to strtok), so we must make
1689 a copy of the literals. */
1690#define ASSERT_PARSE_EDGE_FLAGS(EXPECTED, STR) \
1691 do { \
1692 char *str = xstrdup (STR); \
1693 ASSERT_EQ (EXPECTED, parse_edge_flags (str)); \
1694 free (str); \
1695 } while (0)
1696
1697 ASSERT_PARSE_EDGE_FLAGS (0, "");
1698 ASSERT_PARSE_EDGE_FLAGS (EDGE_FALLTHRU, "FALLTHRU");
1699 ASSERT_PARSE_EDGE_FLAGS (EDGE_ABNORMAL_CALL, "ABNORMAL_CALL");
1700 ASSERT_PARSE_EDGE_FLAGS (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL,
1701 "ABNORMAL | ABNORMAL_CALL");
1702
1703#undef ASSERT_PARSE_EDGE_FLAGS
1704}
1705
1706/* Verify that lookup_reg_by_dump_name works. */
1707
1708static void
1709test_parsing_regnos ()
1710{
1711 ASSERT_EQ (-1, lookup_reg_by_dump_name ("this is not a register"));
1712
1713 /* Verify lookup of virtual registers. */
1714 ASSERT_EQ (VIRTUAL_INCOMING_ARGS_REGNUM,
1715 lookup_reg_by_dump_name ("virtual-incoming-args"));
1716 ASSERT_EQ (VIRTUAL_STACK_VARS_REGNUM,
1717 lookup_reg_by_dump_name ("virtual-stack-vars"));
1718 ASSERT_EQ (VIRTUAL_STACK_DYNAMIC_REGNUM,
1719 lookup_reg_by_dump_name ("virtual-stack-dynamic"));
1720 ASSERT_EQ (VIRTUAL_OUTGOING_ARGS_REGNUM,
1721 lookup_reg_by_dump_name ("virtual-outgoing-args"));
1722 ASSERT_EQ (VIRTUAL_CFA_REGNUM,
1723 lookup_reg_by_dump_name ("virtual-cfa"));
1724 ASSERT_EQ (VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM,
1725 lookup_reg_by_dump_name ("virtual-preferred-stack-boundary"));
1726
1727 /* Verify lookup of non-virtual pseudos. */
1728 ASSERT_EQ (LAST_VIRTUAL_REGISTER + 1, lookup_reg_by_dump_name ("<0>"));
1729 ASSERT_EQ (LAST_VIRTUAL_REGISTER + 2, lookup_reg_by_dump_name ("<1>"));
1730}
1731
1732/* Verify that edge E is as expected, with the src and dest basic blocks
1733 having indices EXPECTED_SRC_IDX and EXPECTED_DEST_IDX respectively, and
1734 the edge having flags equal to EXPECTED_FLAGS.
1735 Use LOC as the effective location when reporting failures. */
1736
1737static void
1738assert_edge_at (const location &loc, edge e, int expected_src_idx,
1739 int expected_dest_idx, int expected_flags)
1740{
1741 ASSERT_EQ_AT (loc, expected_src_idx, e->src->index);
1742 ASSERT_EQ_AT (loc, expected_dest_idx, e->dest->index);
1743 ASSERT_EQ_AT (loc, expected_flags, e->flags);
1744}
1745
1746/* Verify that edge EDGE is as expected, with the src and dest basic blocks
1747 having indices EXPECTED_SRC_IDX and EXPECTED_DEST_IDX respectively, and
1748 the edge having flags equal to EXPECTED_FLAGS. */
1749
1750#define ASSERT_EDGE(EDGE, EXPECTED_SRC_IDX, EXPECTED_DEST_IDX, \
1751 EXPECTED_FLAGS) \
1752 assert_edge_at (SELFTEST_LOCATION, EDGE, EXPECTED_SRC_IDX, \
1753 EXPECTED_DEST_IDX, EXPECTED_FLAGS)
1754
1755/* Verify that we can load RTL dumps. */
1756
1757static void
1758test_loading_dump_fragment_1 ()
1759{
1760 // TODO: filter on target?
1761 rtl_dump_test t (SELFTEST_LOCATION, locate_file (path: "asr_div1.rtl"));
1762
1763 /* Verify that the insns were loaded correctly. */
1764 rtx_insn *insn_1 = get_insns ();
1765 ASSERT_TRUE (insn_1);
1766 ASSERT_EQ (1, INSN_UID (insn_1));
1767 ASSERT_EQ (INSN, GET_CODE (insn_1));
1768 ASSERT_EQ (SET, GET_CODE (PATTERN (insn_1)));
1769 ASSERT_EQ (NULL, PREV_INSN (insn_1));
1770
1771 rtx_insn *insn_2 = NEXT_INSN (insn: insn_1);
1772 ASSERT_TRUE (insn_2);
1773 ASSERT_EQ (2, INSN_UID (insn_2));
1774 ASSERT_EQ (INSN, GET_CODE (insn_2));
1775 ASSERT_EQ (insn_1, PREV_INSN (insn_2));
1776 ASSERT_EQ (NULL, NEXT_INSN (insn_2));
1777
1778 /* Verify that registers were loaded correctly. */
1779 rtx insn_1_dest = SET_DEST (PATTERN (insn_1));
1780 ASSERT_EQ (REG, GET_CODE (insn_1_dest));
1781 ASSERT_EQ ((LAST_VIRTUAL_REGISTER + 1) + 2, REGNO (insn_1_dest));
1782 rtx insn_1_src = SET_SRC (PATTERN (insn_1));
1783 ASSERT_EQ (LSHIFTRT, GET_CODE (insn_1_src));
1784 rtx reg = XEXP (insn_1_src, 0);
1785 ASSERT_EQ (REG, GET_CODE (reg));
1786 ASSERT_EQ (LAST_VIRTUAL_REGISTER + 1, REGNO (reg));
1787
1788 /* Verify that get_insn_by_uid works. */
1789 ASSERT_EQ (insn_1, get_insn_by_uid (1));
1790 ASSERT_EQ (insn_2, get_insn_by_uid (2));
1791
1792 /* Verify that basic blocks were created. */
1793 ASSERT_EQ (2, BLOCK_FOR_INSN (insn_1)->index);
1794 ASSERT_EQ (2, BLOCK_FOR_INSN (insn_2)->index);
1795
1796 /* Verify that the CFG was recreated. */
1797 ASSERT_TRUE (cfun);
1798 verify_three_block_rtl_cfg (cfun);
1799 basic_block bb2 = BASIC_BLOCK_FOR_FN (cfun, 2);
1800 ASSERT_TRUE (bb2 != NULL);
1801 ASSERT_EQ (BB_RTL, bb2->flags & BB_RTL);
1802 ASSERT_EQ (2, bb2->index);
1803 ASSERT_EQ (insn_1, BB_HEAD (bb2));
1804 ASSERT_EQ (insn_2, BB_END (bb2));
1805}
1806
1807/* Verify loading another RTL dump. */
1808
1809static void
1810test_loading_dump_fragment_2 ()
1811{
1812 rtl_dump_test t (SELFTEST_LOCATION, locate_file (path: "simple-cse.rtl"));
1813
1814 rtx_insn *insn_1 = get_insn_by_uid (uid: 1);
1815 rtx_insn *insn_2 = get_insn_by_uid (uid: 2);
1816 rtx_insn *insn_3 = get_insn_by_uid (uid: 3);
1817
1818 rtx set1 = single_set (insn: insn_1);
1819 ASSERT_NE (NULL, set1);
1820 rtx set2 = single_set (insn: insn_2);
1821 ASSERT_NE (NULL, set2);
1822 rtx set3 = single_set (insn: insn_3);
1823 ASSERT_NE (NULL, set3);
1824
1825 rtx src1 = SET_SRC (set1);
1826 ASSERT_EQ (PLUS, GET_CODE (src1));
1827
1828 rtx src2 = SET_SRC (set2);
1829 ASSERT_EQ (PLUS, GET_CODE (src2));
1830
1831 /* Both src1 and src2 refer to "(reg:SI %0)".
1832 Verify that we have pointer equality. */
1833 rtx lhs1 = XEXP (src1, 0);
1834 rtx lhs2 = XEXP (src2, 0);
1835 ASSERT_EQ (lhs1, lhs2);
1836
1837 /* Verify that the CFG was recreated. */
1838 ASSERT_TRUE (cfun);
1839 verify_three_block_rtl_cfg (cfun);
1840}
1841
1842/* Verify that CODE_LABEL insns are loaded correctly. */
1843
1844static void
1845test_loading_labels ()
1846{
1847 rtl_dump_test t (SELFTEST_LOCATION, locate_file (path: "example-labels.rtl"));
1848
1849 rtx_insn *insn_100 = get_insn_by_uid (uid: 100);
1850 ASSERT_EQ (CODE_LABEL, GET_CODE (insn_100));
1851 ASSERT_EQ (100, INSN_UID (insn_100));
1852 ASSERT_EQ (NULL, LABEL_NAME (insn_100));
1853 ASSERT_EQ (0, LABEL_NUSES (insn_100));
1854 ASSERT_EQ (30, CODE_LABEL_NUMBER (insn_100));
1855
1856 rtx_insn *insn_200 = get_insn_by_uid (uid: 200);
1857 ASSERT_EQ (CODE_LABEL, GET_CODE (insn_200));
1858 ASSERT_EQ (200, INSN_UID (insn_200));
1859 ASSERT_STREQ ("some_label_name", LABEL_NAME (insn_200));
1860 ASSERT_EQ (0, LABEL_NUSES (insn_200));
1861 ASSERT_EQ (40, CODE_LABEL_NUMBER (insn_200));
1862
1863 /* Ensure that the presence of CODE_LABEL_NUMBER == 40
1864 means that the next label num to be handed out will be 41. */
1865 ASSERT_EQ (41, max_label_num ());
1866
1867 /* Ensure that label names read from a dump are GC-managed
1868 and are found through the insn. */
1869 ggc_collect (mode: GGC_COLLECT_FORCE);
1870 ASSERT_TRUE (ggc_marked_p (insn_200));
1871 ASSERT_TRUE (ggc_marked_p (LABEL_NAME (insn_200)));
1872}
1873
1874/* Verify that the loader copes with an insn with a mode. */
1875
1876static void
1877test_loading_insn_with_mode ()
1878{
1879 rtl_dump_test t (SELFTEST_LOCATION, locate_file (path: "insn-with-mode.rtl"));
1880 rtx_insn *insn = get_insns ();
1881 ASSERT_EQ (INSN, GET_CODE (insn));
1882
1883 /* Verify that the "TI" mode was set from "insn:TI". */
1884 ASSERT_EQ (TImode, GET_MODE (insn));
1885}
1886
1887/* Verify that the loader copes with a jump_insn to a label_ref. */
1888
1889static void
1890test_loading_jump_to_label_ref ()
1891{
1892 rtl_dump_test t (SELFTEST_LOCATION, locate_file (path: "jump-to-label-ref.rtl"));
1893
1894 rtx_insn *jump_insn = get_insn_by_uid (uid: 1);
1895 ASSERT_EQ (JUMP_INSN, GET_CODE (jump_insn));
1896
1897 rtx_insn *barrier = get_insn_by_uid (uid: 2);
1898 ASSERT_EQ (BARRIER, GET_CODE (barrier));
1899
1900 rtx_insn *code_label = get_insn_by_uid (uid: 100);
1901 ASSERT_EQ (CODE_LABEL, GET_CODE (code_label));
1902
1903 /* Verify the jump_insn. */
1904 ASSERT_EQ (4, BLOCK_FOR_INSN (jump_insn)->index);
1905 ASSERT_EQ (SET, GET_CODE (PATTERN (jump_insn)));
1906 /* Ensure that the "(pc)" is using the global singleton. */
1907 ASSERT_RTX_PTR_EQ (pc_rtx, SET_DEST (PATTERN (jump_insn)));
1908 rtx label_ref = SET_SRC (PATTERN (jump_insn));
1909 ASSERT_EQ (LABEL_REF, GET_CODE (label_ref));
1910 ASSERT_EQ (code_label, label_ref_label (label_ref));
1911 ASSERT_EQ (code_label, JUMP_LABEL (jump_insn));
1912
1913 /* Verify the code_label. */
1914 ASSERT_EQ (5, BLOCK_FOR_INSN (code_label)->index);
1915 ASSERT_EQ (NULL, LABEL_NAME (code_label));
1916 ASSERT_EQ (1, LABEL_NUSES (code_label));
1917
1918 /* Verify the generated CFG. */
1919
1920 /* Locate blocks. */
1921 basic_block entry = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1922 ASSERT_TRUE (entry != NULL);
1923 ASSERT_EQ (ENTRY_BLOCK, entry->index);
1924
1925 basic_block exit = EXIT_BLOCK_PTR_FOR_FN (cfun);
1926 ASSERT_TRUE (exit != NULL);
1927 ASSERT_EQ (EXIT_BLOCK, exit->index);
1928
1929 basic_block bb4 = (*cfun->cfg->x_basic_block_info)[4];
1930 basic_block bb5 = (*cfun->cfg->x_basic_block_info)[5];
1931 ASSERT_EQ (4, bb4->index);
1932 ASSERT_EQ (5, bb5->index);
1933
1934 /* Entry block. */
1935 ASSERT_EQ (NULL, entry->preds);
1936 ASSERT_EQ (1, entry->succs->length ());
1937 ASSERT_EDGE ((*entry->succs)[0], 0, 4, EDGE_FALLTHRU);
1938
1939 /* bb4. */
1940 ASSERT_EQ (1, bb4->preds->length ());
1941 ASSERT_EDGE ((*bb4->preds)[0], 0, 4, EDGE_FALLTHRU);
1942 ASSERT_EQ (1, bb4->succs->length ());
1943 ASSERT_EDGE ((*bb4->succs)[0], 4, 5, 0x0);
1944
1945 /* bb5. */
1946 ASSERT_EQ (1, bb5->preds->length ());
1947 ASSERT_EDGE ((*bb5->preds)[0], 4, 5, 0x0);
1948 ASSERT_EQ (1, bb5->succs->length ());
1949 ASSERT_EDGE ((*bb5->succs)[0], 5, 1, EDGE_FALLTHRU);
1950
1951 /* Exit block. */
1952 ASSERT_EQ (1, exit->preds->length ());
1953 ASSERT_EDGE ((*exit->preds)[0], 5, 1, EDGE_FALLTHRU);
1954 ASSERT_EQ (NULL, exit->succs);
1955}
1956
1957/* Verify that the loader copes with a jump_insn to a label_ref
1958 marked "return". */
1959
1960static void
1961test_loading_jump_to_return ()
1962{
1963 rtl_dump_test t (SELFTEST_LOCATION, locate_file (path: "jump-to-return.rtl"));
1964
1965 rtx_insn *jump_insn = get_insn_by_uid (uid: 1);
1966 ASSERT_EQ (JUMP_INSN, GET_CODE (jump_insn));
1967 ASSERT_RTX_PTR_EQ (ret_rtx, JUMP_LABEL (jump_insn));
1968}
1969
1970/* Verify that the loader copes with a jump_insn to a label_ref
1971 marked "simple_return". */
1972
1973static void
1974test_loading_jump_to_simple_return ()
1975{
1976 rtl_dump_test t (SELFTEST_LOCATION,
1977 locate_file (path: "jump-to-simple-return.rtl"));
1978
1979 rtx_insn *jump_insn = get_insn_by_uid (uid: 1);
1980 ASSERT_EQ (JUMP_INSN, GET_CODE (jump_insn));
1981 ASSERT_RTX_PTR_EQ (simple_return_rtx, JUMP_LABEL (jump_insn));
1982}
1983
1984/* Verify that the loader copes with a NOTE_INSN_BASIC_BLOCK. */
1985
1986static void
1987test_loading_note_insn_basic_block ()
1988{
1989 rtl_dump_test t (SELFTEST_LOCATION,
1990 locate_file (path: "note_insn_basic_block.rtl"));
1991
1992 rtx_insn *note = get_insn_by_uid (uid: 1);
1993 ASSERT_EQ (NOTE, GET_CODE (note));
1994 ASSERT_EQ (2, BLOCK_FOR_INSN (note)->index);
1995
1996 ASSERT_EQ (NOTE_INSN_BASIC_BLOCK, NOTE_KIND (note));
1997 ASSERT_EQ (2, NOTE_BASIC_BLOCK (note)->index);
1998 ASSERT_EQ (BASIC_BLOCK_FOR_FN (cfun, 2), NOTE_BASIC_BLOCK (note));
1999}
2000
2001/* Verify that the loader copes with a NOTE_INSN_DELETED. */
2002
2003static void
2004test_loading_note_insn_deleted ()
2005{
2006 rtl_dump_test t (SELFTEST_LOCATION, locate_file (path: "note-insn-deleted.rtl"));
2007
2008 rtx_insn *note = get_insn_by_uid (uid: 1);
2009 ASSERT_EQ (NOTE, GET_CODE (note));
2010 ASSERT_EQ (NOTE_INSN_DELETED, NOTE_KIND (note));
2011}
2012
2013/* Verify that the const_int values are consolidated, since
2014 pointer equality corresponds to value equality.
2015 TODO: do this for all in CASE_CONST_UNIQUE. */
2016
2017static void
2018test_loading_const_int ()
2019{
2020 rtl_dump_test t (SELFTEST_LOCATION, locate_file (path: "const-int.rtl"));
2021
2022 /* Verify that const_int values below MAX_SAVED_CONST_INT use
2023 the global values. */
2024 ASSERT_EQ (const0_rtx, SET_SRC (PATTERN (get_insn_by_uid (1))));
2025 ASSERT_EQ (const1_rtx, SET_SRC (PATTERN (get_insn_by_uid (2))));
2026 ASSERT_EQ (constm1_rtx, SET_SRC (PATTERN (get_insn_by_uid (3))));
2027
2028 /* Verify that other const_int values are consolidated. */
2029 rtx int256 = gen_rtx_CONST_INT (SImode, 256);
2030 ASSERT_EQ (int256, SET_SRC (PATTERN (get_insn_by_uid (4))));
2031}
2032
2033/* Verify that the loader copes with a SYMBOL_REF. */
2034
2035static void
2036test_loading_symbol_ref ()
2037{
2038 rtl_dump_test t (SELFTEST_LOCATION, locate_file (path: "symbol-ref.rtl"));
2039
2040 rtx_insn *insn = get_insns ();
2041
2042 rtx high = SET_SRC (PATTERN (insn));
2043 ASSERT_EQ (HIGH, GET_CODE (high));
2044
2045 rtx symbol_ref = XEXP (high, 0);
2046 ASSERT_EQ (SYMBOL_REF, GET_CODE (symbol_ref));
2047
2048 /* Verify that "[flags 0xc0]" was parsed. */
2049 ASSERT_EQ (0xc0, SYMBOL_REF_FLAGS (symbol_ref));
2050 /* TODO: we don't yet load SYMBOL_REF_DECL. */
2051}
2052
2053/* Verify that the loader can rebuild a CFG. */
2054
2055static void
2056test_loading_cfg ()
2057{
2058 rtl_dump_test t (SELFTEST_LOCATION, locate_file (path: "cfg-test.rtl"));
2059
2060 ASSERT_STREQ ("cfg_test", IDENTIFIER_POINTER (DECL_NAME (cfun->decl)));
2061
2062 ASSERT_TRUE (cfun);
2063
2064 ASSERT_TRUE (cfun->cfg != NULL);
2065 ASSERT_EQ (6, n_basic_blocks_for_fn (cfun));
2066 ASSERT_EQ (6, n_edges_for_fn (cfun));
2067
2068 /* The "fake" basic blocks. */
2069 basic_block entry = ENTRY_BLOCK_PTR_FOR_FN (cfun);
2070 ASSERT_TRUE (entry != NULL);
2071 ASSERT_EQ (ENTRY_BLOCK, entry->index);
2072
2073 basic_block exit = EXIT_BLOCK_PTR_FOR_FN (cfun);
2074 ASSERT_TRUE (exit != NULL);
2075 ASSERT_EQ (EXIT_BLOCK, exit->index);
2076
2077 /* The "real" basic blocks. */
2078 basic_block bb2 = (*cfun->cfg->x_basic_block_info)[2];
2079 basic_block bb3 = (*cfun->cfg->x_basic_block_info)[3];
2080 basic_block bb4 = (*cfun->cfg->x_basic_block_info)[4];
2081 basic_block bb5 = (*cfun->cfg->x_basic_block_info)[5];
2082
2083 ASSERT_EQ (2, bb2->index);
2084 ASSERT_EQ (3, bb3->index);
2085 ASSERT_EQ (4, bb4->index);
2086 ASSERT_EQ (5, bb5->index);
2087
2088 /* Verify connectivity. */
2089
2090 /* Entry block. */
2091 ASSERT_EQ (NULL, entry->preds);
2092 ASSERT_EQ (1, entry->succs->length ());
2093 ASSERT_EDGE ((*entry->succs)[0], 0, 2, EDGE_FALLTHRU);
2094
2095 /* bb2. */
2096 ASSERT_EQ (1, bb2->preds->length ());
2097 ASSERT_EDGE ((*bb2->preds)[0], 0, 2, EDGE_FALLTHRU);
2098 ASSERT_EQ (2, bb2->succs->length ());
2099 ASSERT_EDGE ((*bb2->succs)[0], 2, 3, EDGE_TRUE_VALUE);
2100 ASSERT_EDGE ((*bb2->succs)[1], 2, 4, EDGE_FALSE_VALUE);
2101
2102 /* bb3. */
2103 ASSERT_EQ (1, bb3->preds->length ());
2104 ASSERT_EDGE ((*bb3->preds)[0], 2, 3, EDGE_TRUE_VALUE);
2105 ASSERT_EQ (1, bb3->succs->length ());
2106 ASSERT_EDGE ((*bb3->succs)[0], 3, 5, EDGE_FALLTHRU);
2107
2108 /* bb4. */
2109 ASSERT_EQ (1, bb4->preds->length ());
2110 ASSERT_EDGE ((*bb4->preds)[0], 2, 4, EDGE_FALSE_VALUE);
2111 ASSERT_EQ (1, bb4->succs->length ());
2112 ASSERT_EDGE ((*bb4->succs)[0], 4, 5, EDGE_FALLTHRU);
2113
2114 /* bb5. */
2115 ASSERT_EQ (2, bb5->preds->length ());
2116 ASSERT_EDGE ((*bb5->preds)[0], 3, 5, EDGE_FALLTHRU);
2117 ASSERT_EDGE ((*bb5->preds)[1], 4, 5, EDGE_FALLTHRU);
2118 ASSERT_EQ (1, bb5->succs->length ());
2119 ASSERT_EDGE ((*bb5->succs)[0], 5, 1, EDGE_FALLTHRU);
2120
2121 /* Exit block. */
2122 ASSERT_EQ (1, exit->preds->length ());
2123 ASSERT_EDGE ((*exit->preds)[0], 5, 1, EDGE_FALLTHRU);
2124 ASSERT_EQ (NULL, exit->succs);
2125}
2126
2127/* Verify that the loader copes with sparse block indices.
2128 This testcase loads a file with a "(block 42)". */
2129
2130static void
2131test_loading_bb_index ()
2132{
2133 rtl_dump_test t (SELFTEST_LOCATION, locate_file (path: "bb-index.rtl"));
2134
2135 ASSERT_STREQ ("test_bb_index", IDENTIFIER_POINTER (DECL_NAME (cfun->decl)));
2136
2137 ASSERT_TRUE (cfun);
2138
2139 ASSERT_TRUE (cfun->cfg != NULL);
2140 ASSERT_EQ (3, n_basic_blocks_for_fn (cfun));
2141 ASSERT_EQ (43, basic_block_info_for_fn (cfun)->length ());
2142 ASSERT_EQ (2, n_edges_for_fn (cfun));
2143
2144 ASSERT_EQ (NULL, (*cfun->cfg->x_basic_block_info)[41]);
2145 basic_block bb42 = (*cfun->cfg->x_basic_block_info)[42];
2146 ASSERT_NE (NULL, bb42);
2147 ASSERT_EQ (42, bb42->index);
2148}
2149
2150/* Verify that function_reader::handle_any_trailing_information correctly
2151 parses all the possible items emitted for a MEM. */
2152
2153static void
2154test_loading_mem ()
2155{
2156 rtl_dump_test t (SELFTEST_LOCATION, locate_file (path: "mem.rtl"));
2157
2158 ASSERT_STREQ ("test_mem", IDENTIFIER_POINTER (DECL_NAME (cfun->decl)));
2159 ASSERT_TRUE (cfun);
2160
2161 /* Verify parsing of "[42 i+17 S8 A128 AS5]". */
2162 rtx_insn *insn_1 = get_insn_by_uid (uid: 1);
2163 rtx set1 = single_set (insn: insn_1);
2164 rtx mem1 = SET_DEST (set1);
2165 ASSERT_EQ (42, MEM_ALIAS_SET (mem1));
2166 /* "+17". */
2167 ASSERT_TRUE (MEM_OFFSET_KNOWN_P (mem1));
2168 ASSERT_KNOWN_EQ (17, MEM_OFFSET (mem1));
2169 /* "S8". */
2170 ASSERT_KNOWN_EQ (8, MEM_SIZE (mem1));
2171 /* "A128. */
2172 ASSERT_EQ (128, MEM_ALIGN (mem1));
2173 /* "AS5. */
2174 ASSERT_EQ (5, MEM_ADDR_SPACE (mem1));
2175
2176 /* Verify parsing of "43 i+18 S9 AS6"
2177 (an address space without an alignment). */
2178 rtx_insn *insn_2 = get_insn_by_uid (uid: 2);
2179 rtx set2 = single_set (insn: insn_2);
2180 rtx mem2 = SET_DEST (set2);
2181 ASSERT_EQ (43, MEM_ALIAS_SET (mem2));
2182 /* "+18". */
2183 ASSERT_TRUE (MEM_OFFSET_KNOWN_P (mem2));
2184 ASSERT_KNOWN_EQ (18, MEM_OFFSET (mem2));
2185 /* "S9". */
2186 ASSERT_KNOWN_EQ (9, MEM_SIZE (mem2));
2187 /* "AS6. */
2188 ASSERT_EQ (6, MEM_ADDR_SPACE (mem2));
2189}
2190
2191/* Verify that "repeated xN" is read correctly. */
2192
2193static void
2194test_loading_repeat ()
2195{
2196 rtl_dump_test t (SELFTEST_LOCATION, locate_file (path: "repeat.rtl"));
2197
2198 rtx_insn *insn_1 = get_insn_by_uid (uid: 1);
2199 ASSERT_EQ (PARALLEL, GET_CODE (PATTERN (insn_1)));
2200 ASSERT_EQ (64, XVECLEN (PATTERN (insn_1), 0));
2201 for (int i = 0; i < 64; i++)
2202 ASSERT_EQ (const0_rtx, XVECEXP (PATTERN (insn_1), 0, i));
2203}
2204
2205/* Run all of the selftests within this file. */
2206
2207void
2208read_rtl_function_cc_tests ()
2209{
2210 test_edge_flags ();
2211 test_parsing_regnos ();
2212 test_loading_dump_fragment_1 ();
2213 test_loading_dump_fragment_2 ();
2214 test_loading_labels ();
2215 test_loading_insn_with_mode ();
2216 test_loading_jump_to_label_ref ();
2217 test_loading_jump_to_return ();
2218 test_loading_jump_to_simple_return ();
2219 test_loading_note_insn_basic_block ();
2220 test_loading_note_insn_deleted ();
2221 test_loading_const_int ();
2222 test_loading_symbol_ref ();
2223 test_loading_cfg ();
2224 test_loading_bb_index ();
2225 test_loading_mem ();
2226 test_loading_repeat ();
2227}
2228
2229} // namespace selftest
2230
2231#endif /* #if CHECKING_P */
2232

source code of gcc/read-rtl-function.cc