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