1 /* RTL utility routines.
2 Copyright (C) 1987-2013 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. */
93 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
94 #include "rtl.def" /* rtl expressions are defined here */
98 /* Indexed by rtx code, gives a character representing the "class" of
99 that rtx code. See rtl.def for documentation on the defined classes. */
101 const enum rtx_class rtx_class
[NUM_RTX_CODE
] = {
102 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) CLASS,
103 #include "rtl.def" /* rtl expressions are defined here */
107 /* Indexed by rtx code, gives the size of the rtx in bytes. */
109 const unsigned char rtx_code_size
[NUM_RTX_CODE
] = {
110 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) \
111 (((ENUM) == CONST_INT || (ENUM) == CONST_DOUBLE \
112 || (ENUM) == CONST_FIXED) \
113 ? RTX_HDR_SIZE + (sizeof FORMAT - 1) * sizeof (HOST_WIDE_INT) \
114 : RTX_HDR_SIZE + (sizeof FORMAT - 1) * sizeof (rtunion)),
120 /* Names for kinds of NOTEs and REG_NOTEs. */
122 const char * const note_insn_name
[NOTE_INSN_MAX
] =
124 #define DEF_INSN_NOTE(NAME) #NAME,
125 #include "insn-notes.def"
129 const char * const reg_note_name
[REG_NOTE_MAX
] =
131 #define DEF_REG_NOTE(NAME) #NAME,
132 #include "reg-notes.def"
136 static int rtx_alloc_counts
[(int) LAST_AND_UNUSED_RTX_CODE
];
137 static int rtx_alloc_sizes
[(int) LAST_AND_UNUSED_RTX_CODE
];
138 static int rtvec_alloc_counts
;
139 static int rtvec_alloc_sizes
;
142 /* Allocate an rtx vector of N elements.
143 Store the length, and initialize all elements to zero. */
150 rt
= ggc_alloc_rtvec_sized (n
);
151 /* Clear out the vector. */
152 memset (&rt
->elem
[0], 0, n
* sizeof (rtx
));
154 PUT_NUM_ELEM (rt
, n
);
156 if (GATHER_STATISTICS
)
158 rtvec_alloc_counts
++;
159 rtvec_alloc_sizes
+= n
* sizeof (rtx
);
165 /* Create a bitwise copy of VEC. */
168 shallow_copy_rtvec (rtvec vec
)
173 n
= GET_NUM_ELEM (vec
);
174 newvec
= rtvec_alloc (n
);
175 memcpy (&newvec
->elem
[0], &vec
->elem
[0], sizeof (rtx
) * n
);
179 /* Return the number of bytes occupied by rtx value X. */
182 rtx_size (const_rtx x
)
184 if (GET_CODE (x
) == SYMBOL_REF
&& SYMBOL_REF_HAS_BLOCK_INFO_P (x
))
185 return RTX_HDR_SIZE
+ sizeof (struct block_symbol
);
186 return RTX_CODE_SIZE (GET_CODE (x
));
189 /* Allocate an rtx of code CODE. The CODE is stored in the rtx;
190 all the rest is initialized to zero. */
193 rtx_alloc_stat (RTX_CODE code MEM_STAT_DECL
)
195 rtx rt
= ggc_alloc_zone_rtx_def_stat (&rtl_zone
, RTX_CODE_SIZE (code
)
198 /* We want to clear everything up to the FLD array. Normally, this
199 is one int, but we don't want to assume that and it isn't very
200 portable anyway; this is. */
202 memset (rt
, 0, RTX_HDR_SIZE
);
205 if (GATHER_STATISTICS
)
207 rtx_alloc_counts
[code
]++;
208 rtx_alloc_sizes
[code
] += RTX_CODE_SIZE (code
);
215 /* Return true if ORIG is a sharable CONST. */
218 shared_const_p (const_rtx orig
)
220 gcc_assert (GET_CODE (orig
) == CONST
);
222 /* CONST can be shared if it contains a SYMBOL_REF. If it contains
223 a LABEL_REF, it isn't sharable. */
224 return (GET_CODE (XEXP (orig
, 0)) == PLUS
225 && GET_CODE (XEXP (XEXP (orig
, 0), 0)) == SYMBOL_REF
226 && CONST_INT_P(XEXP (XEXP (orig
, 0), 1)));
230 /* Create a new copy of an rtx.
231 Recursively copies the operands of the rtx,
232 except for those few rtx codes that are sharable. */
240 const char *format_ptr
;
242 code
= GET_CODE (orig
);
257 /* SCRATCH must be shared because they represent distinct values. */
260 if (REG_P (XEXP (orig
, 0)) && REGNO (XEXP (orig
, 0)) < FIRST_PSEUDO_REGISTER
)
265 if (shared_const_p (orig
))
269 /* A MEM with a constant address is not sharable. The problem is that
270 the constant address may need to be reloaded. If the mem is shared,
271 then reloading one copy of this mem will cause all copies to appear
272 to have been reloaded. */
278 /* Copy the various flags, fields, and other information. We assume
279 that all fields need copying, and then clear the fields that should
280 not be copied. That is the sensible default behavior, and forces
281 us to explicitly document why we are *not* copying a flag. */
282 copy
= shallow_copy_rtx (orig
);
284 /* We do not copy the USED flag, which is used as a mark bit during
285 walks over the RTL. */
286 RTX_FLAG (copy
, used
) = 0;
288 format_ptr
= GET_RTX_FORMAT (GET_CODE (copy
));
290 for (i
= 0; i
< GET_RTX_LENGTH (GET_CODE (copy
)); i
++)
291 switch (*format_ptr
++)
294 if (XEXP (orig
, i
) != NULL
)
295 XEXP (copy
, i
) = copy_rtx (XEXP (orig
, i
));
300 if (XVEC (orig
, i
) != NULL
)
302 XVEC (copy
, i
) = rtvec_alloc (XVECLEN (orig
, i
));
303 for (j
= 0; j
< XVECLEN (copy
, i
); j
++)
304 XVECEXP (copy
, i
, j
) = copy_rtx (XVECEXP (orig
, i
, j
));
317 /* These are left unchanged. */
326 /* Create a new copy of an rtx. Only copy just one level. */
329 shallow_copy_rtx_stat (const_rtx orig MEM_STAT_DECL
)
331 const unsigned int size
= rtx_size (orig
);
332 rtx
const copy
= ggc_alloc_zone_rtx_def_stat (&rtl_zone
, size PASS_MEM_STAT
);
333 return (rtx
) memcpy (copy
, orig
, size
);
336 /* Nonzero when we are generating CONCATs. */
337 int generating_concat_p
;
339 /* Nonzero when we are expanding trees to RTL. */
340 int currently_expanding_to_rtl
;
344 /* Same as rtx_equal_p, but call CB on each pair of rtx if CB is not NULL.
345 When the callback returns true, we continue with the new pair.
346 Whenever changing this function check if rtx_equal_p below doesn't need
350 rtx_equal_p_cb (const_rtx x
, const_rtx y
, rtx_equal_p_callback_function cb
)
360 if (x
== 0 || y
== 0)
363 /* Invoke the callback first. */
365 && ((*cb
) (&x
, &y
, &nx
, &ny
)))
366 return rtx_equal_p_cb (nx
, ny
, cb
);
369 /* Rtx's of different codes cannot be equal. */
370 if (code
!= GET_CODE (y
))
373 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
374 (REG:SI x) and (REG:HI x) are NOT equivalent. */
376 if (GET_MODE (x
) != GET_MODE (y
))
379 /* MEMs referring to different address space are not equivalent. */
380 if (code
== MEM
&& MEM_ADDR_SPACE (x
) != MEM_ADDR_SPACE (y
))
383 /* Some RTL can be compared nonrecursively. */
387 return (REGNO (x
) == REGNO (y
));
390 return XEXP (x
, 0) == XEXP (y
, 0);
393 return XSTR (x
, 0) == XSTR (y
, 0);
401 case DEBUG_IMPLICIT_PTR
:
402 return DEBUG_IMPLICIT_PTR_DECL (x
)
403 == DEBUG_IMPLICIT_PTR_DECL (y
);
405 case DEBUG_PARAMETER_REF
:
406 return DEBUG_PARAMETER_REF_DECL (x
)
407 == DEBUG_PARAMETER_REF_DECL (x
);
410 return rtx_equal_p_cb (ENTRY_VALUE_EXP (x
), ENTRY_VALUE_EXP (y
), cb
);
416 /* Compare the elements. If any pair of corresponding elements
417 fail to match, return 0 for the whole thing. */
419 fmt
= GET_RTX_FORMAT (code
);
420 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
425 if (XWINT (x
, i
) != XWINT (y
, i
))
431 if (XINT (x
, i
) != XINT (y
, i
))
433 #ifndef GENERATOR_FILE
434 if (((code
== ASM_OPERANDS
&& i
== 6)
435 || (code
== ASM_INPUT
&& i
== 1))
436 && XINT (x
, i
) == XINT (y
, i
))
445 /* Two vectors must have the same length. */
446 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
449 /* And the corresponding elements must match. */
450 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
451 if (rtx_equal_p_cb (XVECEXP (x
, i
, j
),
452 XVECEXP (y
, i
, j
), cb
) == 0)
457 if (rtx_equal_p_cb (XEXP (x
, i
), XEXP (y
, i
), cb
) == 0)
463 if ((XSTR (x
, i
) || XSTR (y
, i
))
464 && (! XSTR (x
, i
) || ! XSTR (y
, i
)
465 || strcmp (XSTR (x
, i
), XSTR (y
, i
))))
470 /* These are just backpointers, so they don't matter. */
477 /* It is believed that rtx's at this level will never
478 contain anything but integers and other rtx's,
479 except for within LABEL_REFs and SYMBOL_REFs. */
487 /* Return 1 if X and Y are identical-looking rtx's.
488 This is the Lisp function EQUAL for rtx arguments.
489 Whenever changing this function check if rtx_equal_p_cb above doesn't need
493 rtx_equal_p (const_rtx x
, const_rtx y
)
502 if (x
== 0 || y
== 0)
506 /* Rtx's of different codes cannot be equal. */
507 if (code
!= GET_CODE (y
))
510 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
511 (REG:SI x) and (REG:HI x) are NOT equivalent. */
513 if (GET_MODE (x
) != GET_MODE (y
))
516 /* MEMs referring to different address space are not equivalent. */
517 if (code
== MEM
&& MEM_ADDR_SPACE (x
) != MEM_ADDR_SPACE (y
))
520 /* Some RTL can be compared nonrecursively. */
524 return (REGNO (x
) == REGNO (y
));
527 return XEXP (x
, 0) == XEXP (y
, 0);
530 return XSTR (x
, 0) == XSTR (y
, 0);
538 case DEBUG_IMPLICIT_PTR
:
539 return DEBUG_IMPLICIT_PTR_DECL (x
)
540 == DEBUG_IMPLICIT_PTR_DECL (y
);
542 case DEBUG_PARAMETER_REF
:
543 return DEBUG_PARAMETER_REF_DECL (x
)
544 == DEBUG_PARAMETER_REF_DECL (y
);
547 return rtx_equal_p (ENTRY_VALUE_EXP (x
), ENTRY_VALUE_EXP (y
));
553 /* Compare the elements. If any pair of corresponding elements
554 fail to match, return 0 for the whole thing. */
556 fmt
= GET_RTX_FORMAT (code
);
557 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
562 if (XWINT (x
, i
) != XWINT (y
, i
))
568 if (XINT (x
, i
) != XINT (y
, i
))
570 #ifndef GENERATOR_FILE
571 if (((code
== ASM_OPERANDS
&& i
== 6)
572 || (code
== ASM_INPUT
&& i
== 1))
573 && XINT (x
, i
) == XINT (y
, i
))
582 /* Two vectors must have the same length. */
583 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
586 /* And the corresponding elements must match. */
587 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
588 if (rtx_equal_p (XVECEXP (x
, i
, j
), XVECEXP (y
, i
, j
)) == 0)
593 if (rtx_equal_p (XEXP (x
, i
), XEXP (y
, i
)) == 0)
599 if ((XSTR (x
, i
) || XSTR (y
, i
))
600 && (! XSTR (x
, i
) || ! XSTR (y
, i
)
601 || strcmp (XSTR (x
, i
), XSTR (y
, i
))))
606 /* These are just backpointers, so they don't matter. */
613 /* It is believed that rtx's at this level will never
614 contain anything but integers and other rtx's,
615 except for within LABEL_REFs and SYMBOL_REFs. */
623 /* Iteratively hash rtx X. */
626 iterative_hash_rtx (const_rtx x
, hashval_t hash
)
629 enum machine_mode mode
;
636 hash
= iterative_hash_object (code
, hash
);
638 hash
= iterative_hash_object (mode
, hash
);
643 return iterative_hash_object (i
, hash
);
645 return iterative_hash_object (INTVAL (x
), hash
);
648 return iterative_hash (XSTR (x
, 0), strlen (XSTR (x
, 0)) + 1,
657 case DEBUG_IMPLICIT_PTR
:
658 case DEBUG_PARAMETER_REF
:
664 fmt
= GET_RTX_FORMAT (code
);
665 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
669 hash
= iterative_hash_object (XWINT (x
, i
), hash
);
673 hash
= iterative_hash_object (XINT (x
, i
), hash
);
678 hash
= iterative_hash_object (j
, hash
);
679 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
680 hash
= iterative_hash_rtx (XVECEXP (x
, i
, j
), hash
);
683 hash
= iterative_hash_rtx (XEXP (x
, i
), hash
);
688 hash
= iterative_hash (XSTR (x
, 0), strlen (XSTR (x
, 0)) + 1,
698 dump_rtx_statistics (void)
701 int total_counts
= 0;
704 if (! GATHER_STATISTICS
)
706 fprintf (stderr
, "No RTX statistics\n");
710 fprintf (stderr
, "\nRTX Kind Count Bytes\n");
711 fprintf (stderr
, "---------------------------------------\n");
712 for (i
= 0; i
< LAST_AND_UNUSED_RTX_CODE
; i
++)
713 if (rtx_alloc_counts
[i
])
715 fprintf (stderr
, "%-20s %7d %10d\n", GET_RTX_NAME (i
),
716 rtx_alloc_counts
[i
], rtx_alloc_sizes
[i
]);
717 total_counts
+= rtx_alloc_counts
[i
];
718 total_sizes
+= rtx_alloc_sizes
[i
];
720 if (rtvec_alloc_counts
)
722 fprintf (stderr
, "%-20s %7d %10d\n", "rtvec",
723 rtvec_alloc_counts
, rtvec_alloc_sizes
);
724 total_counts
+= rtvec_alloc_counts
;
725 total_sizes
+= rtvec_alloc_sizes
;
727 fprintf (stderr
, "---------------------------------------\n");
728 fprintf (stderr
, "%-20s %7d %10d\n",
729 "Total", total_counts
, total_sizes
);
730 fprintf (stderr
, "---------------------------------------\n");
733 #if defined ENABLE_RTL_CHECKING && (GCC_VERSION >= 2007)
735 rtl_check_failed_bounds (const_rtx r
, int n
, const char *file
, int line
,
739 ("RTL check: access of elt %d of '%s' with last elt %d in %s, at %s:%d",
740 n
, GET_RTX_NAME (GET_CODE (r
)), GET_RTX_LENGTH (GET_CODE (r
)) - 1,
741 func
, trim_filename (file
), line
);
745 rtl_check_failed_type1 (const_rtx r
, int n
, int c1
, const char *file
, int line
,
749 ("RTL check: expected elt %d type '%c', have '%c' (rtx %s) in %s, at %s:%d",
750 n
, c1
, GET_RTX_FORMAT (GET_CODE (r
))[n
], GET_RTX_NAME (GET_CODE (r
)),
751 func
, trim_filename (file
), line
);
755 rtl_check_failed_type2 (const_rtx r
, int n
, int c1
, int c2
, const char *file
,
756 int line
, const char *func
)
759 ("RTL check: expected elt %d type '%c' or '%c', have '%c' (rtx %s) in %s, at %s:%d",
760 n
, c1
, c2
, GET_RTX_FORMAT (GET_CODE (r
))[n
], GET_RTX_NAME (GET_CODE (r
)),
761 func
, trim_filename (file
), line
);
765 rtl_check_failed_code1 (const_rtx r
, enum rtx_code code
, const char *file
,
766 int line
, const char *func
)
768 internal_error ("RTL check: expected code '%s', have '%s' in %s, at %s:%d",
769 GET_RTX_NAME (code
), GET_RTX_NAME (GET_CODE (r
)), func
,
770 trim_filename (file
), line
);
774 rtl_check_failed_code2 (const_rtx r
, enum rtx_code code1
, enum rtx_code code2
,
775 const char *file
, int line
, const char *func
)
778 ("RTL check: expected code '%s' or '%s', have '%s' in %s, at %s:%d",
779 GET_RTX_NAME (code1
), GET_RTX_NAME (code2
), GET_RTX_NAME (GET_CODE (r
)),
780 func
, trim_filename (file
), line
);
784 rtl_check_failed_code_mode (const_rtx r
, enum rtx_code code
, enum machine_mode mode
,
785 bool not_mode
, const char *file
, int line
,
788 internal_error ((not_mode
789 ? ("RTL check: expected code '%s' and not mode '%s', "
790 "have code '%s' and mode '%s' in %s, at %s:%d")
791 : ("RTL check: expected code '%s' and mode '%s', "
792 "have code '%s' and mode '%s' in %s, at %s:%d")),
793 GET_RTX_NAME (code
), GET_MODE_NAME (mode
),
794 GET_RTX_NAME (GET_CODE (r
)), GET_MODE_NAME (GET_MODE (r
)),
795 func
, trim_filename (file
), line
);
798 /* Report that line LINE of FILE tried to access the block symbol fields
799 of a non-block symbol. FUNC is the function that contains the line. */
802 rtl_check_failed_block_symbol (const char *file
, int line
, const char *func
)
805 ("RTL check: attempt to treat non-block symbol as a block symbol "
806 "in %s, at %s:%d", func
, trim_filename (file
), line
);
809 /* XXX Maybe print the vector? */
811 rtvec_check_failed_bounds (const_rtvec r
, int n
, const char *file
, int line
,
815 ("RTL check: access of elt %d of vector with last elt %d in %s, at %s:%d",
816 n
, GET_NUM_ELEM (r
) - 1, func
, trim_filename (file
), line
);
818 #endif /* ENABLE_RTL_CHECKING */
820 #if defined ENABLE_RTL_FLAG_CHECKING
822 rtl_check_failed_flag (const char *name
, const_rtx r
, const char *file
,
823 int line
, const char *func
)
826 ("RTL flag check: %s used with unexpected rtx code '%s' in %s, at %s:%d",
827 name
, GET_RTX_NAME (GET_CODE (r
)), func
, trim_filename (file
), line
);
829 #endif /* ENABLE_RTL_FLAG_CHECKING */