1 /* RTL utility routines.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file is compiled twice: once for the generator programs
21 once for the compiler. */
29 #include "coretypes.h"
36 # include "diagnostic-core.h"
40 /* Indexed by rtx code, gives number of operands for an rtx with that code.
41 Does NOT include rtx header data (code and links). */
43 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) sizeof FORMAT - 1 ,
45 const unsigned char rtx_length
[NUM_RTX_CODE
] = {
51 /* Indexed by rtx code, gives the name of that kind of rtx, as a C string. */
53 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
55 const char * const rtx_name
[NUM_RTX_CODE
] = {
56 #include "rtl.def" /* rtl expressions are documented here */
61 /* Indexed by rtx code, gives a sequence of operand-types for
62 rtx's of that code. The sequence is a C string in which
63 each character describes one operand. */
65 const char * const rtx_format
[NUM_RTX_CODE
] = {
67 can cause a warning message
68 "0" field is unused (or used in a phase-dependent manner)
72 "n" like "i", but prints entries from `note_insn_name'
73 "w" an integer of width HOST_BITS_PER_WIDE_INT
75 "s" a pointer to a string
77 "S" like "s", but optional:
78 the containing rtx may end before this operand
79 "T" like "s", but treated specially by the RTL reader;
80 only found in machine description patterns.
81 "e" a pointer to an rtl expression
83 "E" a pointer to a vector that points to a number of rtl expressions
84 prints a list of the rtl expressions
85 "V" like "E", but optional:
86 the containing rtx may end before this operand
87 "u" a pointer to another insn
88 prints the uid of the insn.
89 "b" is a pointer to a bitmap header.
90 "B" is a basic block pointer.
91 "t" is a tree pointer.
94 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
95 #include "rtl.def" /* rtl expressions are defined here */
99 /* Indexed by rtx code, gives a character representing the "class" of
100 that rtx code. See rtl.def for documentation on the defined classes. */
102 const enum rtx_class rtx_class
[NUM_RTX_CODE
] = {
103 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) CLASS,
104 #include "rtl.def" /* rtl expressions are defined here */
108 /* Indexed by rtx code, gives the size of the rtx in bytes. */
110 const unsigned char rtx_code_size
[NUM_RTX_CODE
] = {
111 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) \
112 (((ENUM) == CONST_INT || (ENUM) == CONST_DOUBLE \
113 || (ENUM) == CONST_FIXED || (ENUM) == CONST_WIDE_INT) \
114 ? RTX_HDR_SIZE + (sizeof FORMAT - 1) * sizeof (HOST_WIDE_INT) \
116 ? RTX_HDR_SIZE + sizeof (reg_info) \
117 : RTX_HDR_SIZE + (sizeof FORMAT - 1) * sizeof (rtunion)),
123 /* Names for kinds of NOTEs and REG_NOTEs. */
125 const char * const note_insn_name
[NOTE_INSN_MAX
] =
127 #define DEF_INSN_NOTE(NAME) #NAME,
128 #include "insn-notes.def"
132 const char * const reg_note_name
[REG_NOTE_MAX
] =
134 #define DEF_REG_NOTE(NAME) #NAME,
135 #include "reg-notes.def"
139 static int rtx_alloc_counts
[(int) LAST_AND_UNUSED_RTX_CODE
];
140 static int rtx_alloc_sizes
[(int) LAST_AND_UNUSED_RTX_CODE
];
141 static int rtvec_alloc_counts
;
142 static int rtvec_alloc_sizes
;
145 /* Allocate an rtx vector of N elements.
146 Store the length, and initialize all elements to zero. */
153 rt
= ggc_alloc_rtvec_sized (n
);
154 /* Clear out the vector. */
155 memset (&rt
->elem
[0], 0, n
* sizeof (rtx
));
157 PUT_NUM_ELEM (rt
, n
);
159 if (GATHER_STATISTICS
)
161 rtvec_alloc_counts
++;
162 rtvec_alloc_sizes
+= n
* sizeof (rtx
);
168 /* Create a bitwise copy of VEC. */
171 shallow_copy_rtvec (rtvec vec
)
176 n
= GET_NUM_ELEM (vec
);
177 newvec
= rtvec_alloc (n
);
178 memcpy (&newvec
->elem
[0], &vec
->elem
[0], sizeof (rtx
) * n
);
182 /* Return the number of bytes occupied by rtx value X. */
185 rtx_size (const_rtx x
)
187 if (CONST_WIDE_INT_P (x
))
189 + sizeof (struct hwivec_def
)
190 + ((CONST_WIDE_INT_NUNITS (x
) - 1)
191 * sizeof (HOST_WIDE_INT
)));
192 if (GET_CODE (x
) == SYMBOL_REF
&& SYMBOL_REF_HAS_BLOCK_INFO_P (x
))
193 return RTX_HDR_SIZE
+ sizeof (struct block_symbol
);
194 return RTX_CODE_SIZE (GET_CODE (x
));
197 /* Allocate an rtx of code CODE with EXTRA bytes in it. The CODE is
198 stored in the rtx; all the rest is initialized to zero. */
201 rtx_alloc_stat_v (RTX_CODE code MEM_STAT_DECL
, int extra
)
203 rtx rt
= ggc_alloc_rtx_def_stat (RTX_CODE_SIZE (code
) + extra
206 /* We want to clear everything up to the FLD array. Normally, this
207 is one int, but we don't want to assume that and it isn't very
208 portable anyway; this is. */
210 memset (rt
, 0, RTX_HDR_SIZE
);
213 if (GATHER_STATISTICS
)
215 rtx_alloc_counts
[code
]++;
216 rtx_alloc_sizes
[code
] += RTX_CODE_SIZE (code
);
222 /* Allocate an rtx of code CODE. The CODE is stored in the rtx;
223 all the rest is initialized to zero. */
226 rtx_alloc_stat (RTX_CODE code MEM_STAT_DECL
)
228 return rtx_alloc_stat_v (code PASS_MEM_STAT
, 0);
231 /* Write the wide constant X to OUTFILE. */
234 cwi_output_hex (FILE *outfile
, const_rtx x
)
236 int i
= CWI_GET_NUM_ELEM (x
);
238 if (CWI_ELT (x
, i
- 1) == 0)
239 /* The HOST_WIDE_INT_PRINT_HEX prepends a 0x only if the val is
240 non zero. We want all numbers to have a 0x prefix. */
241 fprintf (outfile
, "0x");
242 fprintf (outfile
, HOST_WIDE_INT_PRINT_HEX
, CWI_ELT (x
, --i
));
244 fprintf (outfile
, HOST_WIDE_INT_PRINT_PADDED_HEX
, CWI_ELT (x
, i
));
248 /* Return true if ORIG is a sharable CONST. */
251 shared_const_p (const_rtx orig
)
253 gcc_assert (GET_CODE (orig
) == CONST
);
255 /* CONST can be shared if it contains a SYMBOL_REF. If it contains
256 a LABEL_REF, it isn't sharable. */
257 return (GET_CODE (XEXP (orig
, 0)) == PLUS
258 && GET_CODE (XEXP (XEXP (orig
, 0), 0)) == SYMBOL_REF
259 && CONST_INT_P (XEXP (XEXP (orig
, 0), 1)));
263 /* Create a new copy of an rtx.
264 Recursively copies the operands of the rtx,
265 except for those few rtx codes that are sharable. */
273 const char *format_ptr
;
275 code
= GET_CODE (orig
);
290 /* SCRATCH must be shared because they represent distinct values. */
293 /* Share clobbers of hard registers (like cc0), but do not share pseudo reg
294 clobbers or clobbers of hard registers that originated as pseudos.
295 This is needed to allow safe register renaming. */
296 if (REG_P (XEXP (orig
, 0)) && REGNO (XEXP (orig
, 0)) < FIRST_PSEUDO_REGISTER
297 && ORIGINAL_REGNO (XEXP (orig
, 0)) == REGNO (XEXP (orig
, 0)))
302 if (shared_const_p (orig
))
306 /* A MEM with a constant address is not sharable. The problem is that
307 the constant address may need to be reloaded. If the mem is shared,
308 then reloading one copy of this mem will cause all copies to appear
309 to have been reloaded. */
315 /* Copy the various flags, fields, and other information. We assume
316 that all fields need copying, and then clear the fields that should
317 not be copied. That is the sensible default behavior, and forces
318 us to explicitly document why we are *not* copying a flag. */
319 copy
= shallow_copy_rtx (orig
);
321 /* We do not copy the USED flag, which is used as a mark bit during
322 walks over the RTL. */
323 RTX_FLAG (copy
, used
) = 0;
325 format_ptr
= GET_RTX_FORMAT (GET_CODE (copy
));
327 for (i
= 0; i
< GET_RTX_LENGTH (GET_CODE (copy
)); i
++)
328 switch (*format_ptr
++)
331 if (XEXP (orig
, i
) != NULL
)
332 XEXP (copy
, i
) = copy_rtx (XEXP (orig
, i
));
337 if (XVEC (orig
, i
) != NULL
)
339 XVEC (copy
, i
) = rtvec_alloc (XVECLEN (orig
, i
));
340 for (j
= 0; j
< XVECLEN (copy
, i
); j
++)
341 XVECEXP (copy
, i
, j
) = copy_rtx (XVECEXP (orig
, i
, j
));
354 /* These are left unchanged. */
363 /* Create a new copy of an rtx. Only copy just one level. */
366 shallow_copy_rtx_stat (const_rtx orig MEM_STAT_DECL
)
368 const unsigned int size
= rtx_size (orig
);
369 rtx
const copy
= ggc_alloc_rtx_def_stat (size PASS_MEM_STAT
);
370 return (rtx
) memcpy (copy
, orig
, size
);
373 /* Nonzero when we are generating CONCATs. */
374 int generating_concat_p
;
376 /* Nonzero when we are expanding trees to RTL. */
377 int currently_expanding_to_rtl
;
381 /* Same as rtx_equal_p, but call CB on each pair of rtx if CB is not NULL.
382 When the callback returns true, we continue with the new pair.
383 Whenever changing this function check if rtx_equal_p below doesn't need
387 rtx_equal_p_cb (const_rtx x
, const_rtx y
, rtx_equal_p_callback_function cb
)
397 if (x
== 0 || y
== 0)
400 /* Invoke the callback first. */
402 && ((*cb
) (&x
, &y
, &nx
, &ny
)))
403 return rtx_equal_p_cb (nx
, ny
, cb
);
406 /* Rtx's of different codes cannot be equal. */
407 if (code
!= GET_CODE (y
))
410 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
411 (REG:SI x) and (REG:HI x) are NOT equivalent. */
413 if (GET_MODE (x
) != GET_MODE (y
))
416 /* MEMs referring to different address space are not equivalent. */
417 if (code
== MEM
&& MEM_ADDR_SPACE (x
) != MEM_ADDR_SPACE (y
))
420 /* Some RTL can be compared nonrecursively. */
424 return (REGNO (x
) == REGNO (y
));
427 return LABEL_REF_LABEL (x
) == LABEL_REF_LABEL (y
);
430 return XSTR (x
, 0) == XSTR (y
, 0);
438 case DEBUG_IMPLICIT_PTR
:
439 return DEBUG_IMPLICIT_PTR_DECL (x
)
440 == DEBUG_IMPLICIT_PTR_DECL (y
);
442 case DEBUG_PARAMETER_REF
:
443 return DEBUG_PARAMETER_REF_DECL (x
)
444 == DEBUG_PARAMETER_REF_DECL (x
);
447 return rtx_equal_p_cb (ENTRY_VALUE_EXP (x
), ENTRY_VALUE_EXP (y
), cb
);
453 /* Compare the elements. If any pair of corresponding elements
454 fail to match, return 0 for the whole thing. */
456 fmt
= GET_RTX_FORMAT (code
);
457 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
462 if (XWINT (x
, i
) != XWINT (y
, i
))
468 if (XINT (x
, i
) != XINT (y
, i
))
470 #ifndef GENERATOR_FILE
471 if (((code
== ASM_OPERANDS
&& i
== 6)
472 || (code
== ASM_INPUT
&& i
== 1))
473 && XINT (x
, i
) == XINT (y
, i
))
482 /* Two vectors must have the same length. */
483 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
486 /* And the corresponding elements must match. */
487 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
488 if (rtx_equal_p_cb (XVECEXP (x
, i
, j
),
489 XVECEXP (y
, i
, j
), cb
) == 0)
494 if (rtx_equal_p_cb (XEXP (x
, i
), XEXP (y
, i
), cb
) == 0)
500 if ((XSTR (x
, i
) || XSTR (y
, i
))
501 && (! XSTR (x
, i
) || ! XSTR (y
, i
)
502 || strcmp (XSTR (x
, i
), XSTR (y
, i
))))
507 /* These are just backpointers, so they don't matter. */
514 /* It is believed that rtx's at this level will never
515 contain anything but integers and other rtx's,
516 except for within LABEL_REFs and SYMBOL_REFs. */
524 /* Return 1 if X and Y are identical-looking rtx's.
525 This is the Lisp function EQUAL for rtx arguments.
526 Whenever changing this function check if rtx_equal_p_cb above doesn't need
530 rtx_equal_p (const_rtx x
, const_rtx y
)
539 if (x
== 0 || y
== 0)
543 /* Rtx's of different codes cannot be equal. */
544 if (code
!= GET_CODE (y
))
547 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
548 (REG:SI x) and (REG:HI x) are NOT equivalent. */
550 if (GET_MODE (x
) != GET_MODE (y
))
553 /* MEMs referring to different address space are not equivalent. */
554 if (code
== MEM
&& MEM_ADDR_SPACE (x
) != MEM_ADDR_SPACE (y
))
557 /* Some RTL can be compared nonrecursively. */
561 return (REGNO (x
) == REGNO (y
));
564 return LABEL_REF_LABEL (x
) == LABEL_REF_LABEL (y
);
567 return XSTR (x
, 0) == XSTR (y
, 0);
575 case DEBUG_IMPLICIT_PTR
:
576 return DEBUG_IMPLICIT_PTR_DECL (x
)
577 == DEBUG_IMPLICIT_PTR_DECL (y
);
579 case DEBUG_PARAMETER_REF
:
580 return DEBUG_PARAMETER_REF_DECL (x
)
581 == DEBUG_PARAMETER_REF_DECL (y
);
584 return rtx_equal_p (ENTRY_VALUE_EXP (x
), ENTRY_VALUE_EXP (y
));
590 /* Compare the elements. If any pair of corresponding elements
591 fail to match, return 0 for the whole thing. */
593 fmt
= GET_RTX_FORMAT (code
);
594 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
599 if (XWINT (x
, i
) != XWINT (y
, i
))
605 if (XINT (x
, i
) != XINT (y
, i
))
607 #ifndef GENERATOR_FILE
608 if (((code
== ASM_OPERANDS
&& i
== 6)
609 || (code
== ASM_INPUT
&& i
== 1))
610 && XINT (x
, i
) == XINT (y
, i
))
619 /* Two vectors must have the same length. */
620 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
623 /* And the corresponding elements must match. */
624 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
625 if (rtx_equal_p (XVECEXP (x
, i
, j
), XVECEXP (y
, i
, j
)) == 0)
630 if (rtx_equal_p (XEXP (x
, i
), XEXP (y
, i
)) == 0)
636 if ((XSTR (x
, i
) || XSTR (y
, i
))
637 && (! XSTR (x
, i
) || ! XSTR (y
, i
)
638 || strcmp (XSTR (x
, i
), XSTR (y
, i
))))
643 /* These are just backpointers, so they don't matter. */
650 /* It is believed that rtx's at this level will never
651 contain anything but integers and other rtx's,
652 except for within LABEL_REFs and SYMBOL_REFs. */
660 /* Return an indication of which type of insn should have X as a body.
661 In generator files, this can be UNKNOWN if the answer is only known
662 at (GCC) runtime. Otherwise the value is CODE_LABEL, INSN, CALL_INSN
666 classify_insn (rtx x
)
670 if (GET_CODE (x
) == CALL
)
672 if (ANY_RETURN_P (x
))
674 if (GET_CODE (x
) == SET
)
676 if (GET_CODE (SET_DEST (x
)) == PC
)
678 else if (GET_CODE (SET_SRC (x
)) == CALL
)
683 if (GET_CODE (x
) == PARALLEL
)
686 for (j
= XVECLEN (x
, 0) - 1; j
>= 0; j
--)
687 if (GET_CODE (XVECEXP (x
, 0, j
)) == CALL
)
689 else if (GET_CODE (XVECEXP (x
, 0, j
)) == SET
690 && GET_CODE (SET_DEST (XVECEXP (x
, 0, j
))) == PC
)
692 else if (GET_CODE (XVECEXP (x
, 0, j
)) == SET
693 && GET_CODE (SET_SRC (XVECEXP (x
, 0, j
))) == CALL
)
696 #ifdef GENERATOR_FILE
697 if (GET_CODE (x
) == MATCH_OPERAND
698 || GET_CODE (x
) == MATCH_OPERATOR
699 || GET_CODE (x
) == MATCH_PARALLEL
700 || GET_CODE (x
) == MATCH_OP_DUP
701 || GET_CODE (x
) == MATCH_DUP
702 || GET_CODE (x
) == PARALLEL
)
709 dump_rtx_statistics (void)
712 int total_counts
= 0;
715 if (! GATHER_STATISTICS
)
717 fprintf (stderr
, "No RTX statistics\n");
721 fprintf (stderr
, "\nRTX Kind Count Bytes\n");
722 fprintf (stderr
, "---------------------------------------\n");
723 for (i
= 0; i
< LAST_AND_UNUSED_RTX_CODE
; i
++)
724 if (rtx_alloc_counts
[i
])
726 fprintf (stderr
, "%-20s %7d %10d\n", GET_RTX_NAME (i
),
727 rtx_alloc_counts
[i
], rtx_alloc_sizes
[i
]);
728 total_counts
+= rtx_alloc_counts
[i
];
729 total_sizes
+= rtx_alloc_sizes
[i
];
731 if (rtvec_alloc_counts
)
733 fprintf (stderr
, "%-20s %7d %10d\n", "rtvec",
734 rtvec_alloc_counts
, rtvec_alloc_sizes
);
735 total_counts
+= rtvec_alloc_counts
;
736 total_sizes
+= rtvec_alloc_sizes
;
738 fprintf (stderr
, "---------------------------------------\n");
739 fprintf (stderr
, "%-20s %7d %10d\n",
740 "Total", total_counts
, total_sizes
);
741 fprintf (stderr
, "---------------------------------------\n");
744 #if defined ENABLE_RTL_CHECKING && (GCC_VERSION >= 2007)
746 rtl_check_failed_bounds (const_rtx r
, int n
, const char *file
, int line
,
750 ("RTL check: access of elt %d of '%s' with last elt %d in %s, at %s:%d",
751 n
, GET_RTX_NAME (GET_CODE (r
)), GET_RTX_LENGTH (GET_CODE (r
)) - 1,
752 func
, trim_filename (file
), line
);
756 rtl_check_failed_type1 (const_rtx r
, int n
, int c1
, const char *file
, int line
,
760 ("RTL check: expected elt %d type '%c', have '%c' (rtx %s) in %s, at %s:%d",
761 n
, c1
, GET_RTX_FORMAT (GET_CODE (r
))[n
], GET_RTX_NAME (GET_CODE (r
)),
762 func
, trim_filename (file
), line
);
766 rtl_check_failed_type2 (const_rtx r
, int n
, int c1
, int c2
, const char *file
,
767 int line
, const char *func
)
770 ("RTL check: expected elt %d type '%c' or '%c', have '%c' (rtx %s) in %s, at %s:%d",
771 n
, c1
, c2
, GET_RTX_FORMAT (GET_CODE (r
))[n
], GET_RTX_NAME (GET_CODE (r
)),
772 func
, trim_filename (file
), line
);
776 rtl_check_failed_code1 (const_rtx r
, enum rtx_code code
, const char *file
,
777 int line
, const char *func
)
779 internal_error ("RTL check: expected code '%s', have '%s' in %s, at %s:%d",
780 GET_RTX_NAME (code
), GET_RTX_NAME (GET_CODE (r
)), func
,
781 trim_filename (file
), line
);
785 rtl_check_failed_code2 (const_rtx r
, enum rtx_code code1
, enum rtx_code code2
,
786 const char *file
, int line
, const char *func
)
789 ("RTL check: expected code '%s' or '%s', have '%s' in %s, at %s:%d",
790 GET_RTX_NAME (code1
), GET_RTX_NAME (code2
), GET_RTX_NAME (GET_CODE (r
)),
791 func
, trim_filename (file
), line
);
795 rtl_check_failed_code_mode (const_rtx r
, enum rtx_code code
, machine_mode mode
,
796 bool not_mode
, const char *file
, int line
,
799 internal_error ((not_mode
800 ? ("RTL check: expected code '%s' and not mode '%s', "
801 "have code '%s' and mode '%s' in %s, at %s:%d")
802 : ("RTL check: expected code '%s' and mode '%s', "
803 "have code '%s' and mode '%s' in %s, at %s:%d")),
804 GET_RTX_NAME (code
), GET_MODE_NAME (mode
),
805 GET_RTX_NAME (GET_CODE (r
)), GET_MODE_NAME (GET_MODE (r
)),
806 func
, trim_filename (file
), line
);
809 /* Report that line LINE of FILE tried to access the block symbol fields
810 of a non-block symbol. FUNC is the function that contains the line. */
813 rtl_check_failed_block_symbol (const char *file
, int line
, const char *func
)
816 ("RTL check: attempt to treat non-block symbol as a block symbol "
817 "in %s, at %s:%d", func
, trim_filename (file
), line
);
820 /* XXX Maybe print the vector? */
822 cwi_check_failed_bounds (const_rtx x
, int n
, const char *file
, int line
,
826 ("RTL check: access of hwi elt %d of vector with last elt %d in %s, at %s:%d",
827 n
, CWI_GET_NUM_ELEM (x
) - 1, func
, trim_filename (file
), line
);
830 /* XXX Maybe print the vector? */
832 rtvec_check_failed_bounds (const_rtvec r
, int n
, const char *file
, int line
,
836 ("RTL check: access of elt %d of vector with last elt %d in %s, at %s:%d",
837 n
, GET_NUM_ELEM (r
) - 1, func
, trim_filename (file
), line
);
839 #endif /* ENABLE_RTL_CHECKING */
841 #if defined ENABLE_RTL_FLAG_CHECKING
843 rtl_check_failed_flag (const char *name
, const_rtx r
, const char *file
,
844 int line
, const char *func
)
847 ("RTL flag check: %s used with unexpected rtx code '%s' in %s, at %s:%d",
848 name
, GET_RTX_NAME (GET_CODE (r
)), func
, trim_filename (file
), line
);
850 #endif /* ENABLE_RTL_FLAG_CHECKING */