1 /* C-compiler utilities for types and variables storage layout
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/>. */
23 #include "coretypes.h"
28 #include "double-int.h"
35 #include "fold-const.h"
36 #include "stor-layout.h"
37 #include "stringpool.h"
39 #include "print-tree.h"
43 #include "hard-reg-set.h"
46 #include "statistics.h"
48 #include "fixed-value.h"
49 #include "insn-config.h"
57 #include "diagnostic-core.h"
59 #include "langhooks.h"
64 #include "plugin-api.h"
67 #include "tree-inline.h"
68 #include "tree-dump.h"
71 /* Data type for the expressions representing sizes of data types.
72 It is the first integer type laid out. */
73 tree sizetype_tab
[(int) stk_type_kind_last
];
75 /* If nonzero, this is an upper limit on alignment of structure fields.
76 The value is measured in bits. */
77 unsigned int maximum_field_alignment
= TARGET_DEFAULT_PACK_STRUCT
* BITS_PER_UNIT
;
79 /* Nonzero if all REFERENCE_TYPEs are internal and hence should be allocated
80 in the address spaces' address_mode, not pointer_mode. Set only by
81 internal_reference_types called only by a front end. */
82 static int reference_types_internal
= 0;
84 static tree
self_referential_size (tree
);
85 static void finalize_record_size (record_layout_info
);
86 static void finalize_type_size (tree
);
87 static void place_union_field (record_layout_info
, tree
);
88 #if defined (PCC_BITFIELD_TYPE_MATTERS) || defined (BITFIELD_NBYTES_LIMITED)
89 static int excess_unit_span (HOST_WIDE_INT
, HOST_WIDE_INT
, HOST_WIDE_INT
,
92 extern void debug_rli (record_layout_info
);
94 /* Show that REFERENCE_TYPES are internal and should use address_mode.
95 Called only by front end. */
98 internal_reference_types (void)
100 reference_types_internal
= 1;
103 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
104 to serve as the actual size-expression for a type or decl. */
107 variable_size (tree size
)
110 if (TREE_CONSTANT (size
))
113 /* If the size is self-referential, we can't make a SAVE_EXPR (see
114 save_expr for the rationale). But we can do something else. */
115 if (CONTAINS_PLACEHOLDER_P (size
))
116 return self_referential_size (size
);
118 /* If we are in the global binding level, we can't make a SAVE_EXPR
119 since it may end up being shared across functions, so it is up
120 to the front-end to deal with this case. */
121 if (lang_hooks
.decls
.global_bindings_p ())
124 return save_expr (size
);
127 /* An array of functions used for self-referential size computation. */
128 static GTY(()) vec
<tree
, va_gc
> *size_functions
;
130 /* Similar to copy_tree_r but do not copy component references involving
131 PLACEHOLDER_EXPRs. These nodes are spotted in find_placeholder_in_expr
132 and substituted in substitute_in_expr. */
135 copy_self_referential_tree_r (tree
*tp
, int *walk_subtrees
, void *data
)
137 enum tree_code code
= TREE_CODE (*tp
);
139 /* Stop at types, decls, constants like copy_tree_r. */
140 if (TREE_CODE_CLASS (code
) == tcc_type
141 || TREE_CODE_CLASS (code
) == tcc_declaration
142 || TREE_CODE_CLASS (code
) == tcc_constant
)
148 /* This is the pattern built in ada/make_aligning_type. */
149 else if (code
== ADDR_EXPR
150 && TREE_CODE (TREE_OPERAND (*tp
, 0)) == PLACEHOLDER_EXPR
)
156 /* Default case: the component reference. */
157 else if (code
== COMPONENT_REF
)
160 for (inner
= TREE_OPERAND (*tp
, 0);
161 REFERENCE_CLASS_P (inner
);
162 inner
= TREE_OPERAND (inner
, 0))
165 if (TREE_CODE (inner
) == PLACEHOLDER_EXPR
)
172 /* We're not supposed to have them in self-referential size trees
173 because we wouldn't properly control when they are evaluated.
174 However, not creating superfluous SAVE_EXPRs requires accurate
175 tracking of readonly-ness all the way down to here, which we
176 cannot always guarantee in practice. So punt in this case. */
177 else if (code
== SAVE_EXPR
)
178 return error_mark_node
;
180 else if (code
== STATEMENT_LIST
)
183 return copy_tree_r (tp
, walk_subtrees
, data
);
186 /* Given a SIZE expression that is self-referential, return an equivalent
187 expression to serve as the actual size expression for a type. */
190 self_referential_size (tree size
)
192 static unsigned HOST_WIDE_INT fnno
= 0;
193 vec
<tree
> self_refs
= vNULL
;
194 tree param_type_list
= NULL
, param_decl_list
= NULL
;
195 tree t
, ref
, return_type
, fntype
, fnname
, fndecl
;
198 vec
<tree
, va_gc
> *args
= NULL
;
200 /* Do not factor out simple operations. */
201 t
= skip_simple_constant_arithmetic (size
);
202 if (TREE_CODE (t
) == CALL_EXPR
)
205 /* Collect the list of self-references in the expression. */
206 find_placeholder_in_expr (size
, &self_refs
);
207 gcc_assert (self_refs
.length () > 0);
209 /* Obtain a private copy of the expression. */
211 if (walk_tree (&t
, copy_self_referential_tree_r
, NULL
, NULL
) != NULL_TREE
)
215 /* Build the parameter and argument lists in parallel; also
216 substitute the former for the latter in the expression. */
217 vec_alloc (args
, self_refs
.length ());
218 FOR_EACH_VEC_ELT (self_refs
, i
, ref
)
220 tree subst
, param_name
, param_type
, param_decl
;
224 /* We shouldn't have true variables here. */
225 gcc_assert (TREE_READONLY (ref
));
228 /* This is the pattern built in ada/make_aligning_type. */
229 else if (TREE_CODE (ref
) == ADDR_EXPR
)
231 /* Default case: the component reference. */
233 subst
= TREE_OPERAND (ref
, 1);
235 sprintf (buf
, "p%d", i
);
236 param_name
= get_identifier (buf
);
237 param_type
= TREE_TYPE (ref
);
239 = build_decl (input_location
, PARM_DECL
, param_name
, param_type
);
240 DECL_ARG_TYPE (param_decl
) = param_type
;
241 DECL_ARTIFICIAL (param_decl
) = 1;
242 TREE_READONLY (param_decl
) = 1;
244 size
= substitute_in_expr (size
, subst
, param_decl
);
246 param_type_list
= tree_cons (NULL_TREE
, param_type
, param_type_list
);
247 param_decl_list
= chainon (param_decl
, param_decl_list
);
248 args
->quick_push (ref
);
251 self_refs
.release ();
253 /* Append 'void' to indicate that the number of parameters is fixed. */
254 param_type_list
= tree_cons (NULL_TREE
, void_type_node
, param_type_list
);
256 /* The 3 lists have been created in reverse order. */
257 param_type_list
= nreverse (param_type_list
);
258 param_decl_list
= nreverse (param_decl_list
);
260 /* Build the function type. */
261 return_type
= TREE_TYPE (size
);
262 fntype
= build_function_type (return_type
, param_type_list
);
264 /* Build the function declaration. */
265 sprintf (buf
, "SZ"HOST_WIDE_INT_PRINT_UNSIGNED
, fnno
++);
266 fnname
= get_file_function_name (buf
);
267 fndecl
= build_decl (input_location
, FUNCTION_DECL
, fnname
, fntype
);
268 for (t
= param_decl_list
; t
; t
= DECL_CHAIN (t
))
269 DECL_CONTEXT (t
) = fndecl
;
270 DECL_ARGUMENTS (fndecl
) = param_decl_list
;
272 = build_decl (input_location
, RESULT_DECL
, 0, return_type
);
273 DECL_CONTEXT (DECL_RESULT (fndecl
)) = fndecl
;
275 /* The function has been created by the compiler and we don't
276 want to emit debug info for it. */
277 DECL_ARTIFICIAL (fndecl
) = 1;
278 DECL_IGNORED_P (fndecl
) = 1;
280 /* It is supposed to be "const" and never throw. */
281 TREE_READONLY (fndecl
) = 1;
282 TREE_NOTHROW (fndecl
) = 1;
284 /* We want it to be inlined when this is deemed profitable, as
285 well as discarded if every call has been integrated. */
286 DECL_DECLARED_INLINE_P (fndecl
) = 1;
288 /* It is made up of a unique return statement. */
289 DECL_INITIAL (fndecl
) = make_node (BLOCK
);
290 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
291 t
= build2 (MODIFY_EXPR
, return_type
, DECL_RESULT (fndecl
), size
);
292 DECL_SAVED_TREE (fndecl
) = build1 (RETURN_EXPR
, void_type_node
, t
);
293 TREE_STATIC (fndecl
) = 1;
295 /* Put it onto the list of size functions. */
296 vec_safe_push (size_functions
, fndecl
);
298 /* Replace the original expression with a call to the size function. */
299 return build_call_expr_loc_vec (UNKNOWN_LOCATION
, fndecl
, args
);
302 /* Take, queue and compile all the size functions. It is essential that
303 the size functions be gimplified at the very end of the compilation
304 in order to guarantee transparent handling of self-referential sizes.
305 Otherwise the GENERIC inliner would not be able to inline them back
306 at each of their call sites, thus creating artificial non-constant
307 size expressions which would trigger nasty problems later on. */
310 finalize_size_functions (void)
315 for (i
= 0; size_functions
&& size_functions
->iterate (i
, &fndecl
); i
++)
317 allocate_struct_function (fndecl
, false);
319 dump_function (TDI_original
, fndecl
);
320 gimplify_function_tree (fndecl
);
321 dump_function (TDI_generic
, fndecl
);
322 cgraph_node::finalize_function (fndecl
, false);
325 vec_free (size_functions
);
328 /* Return the machine mode to use for a nonscalar of SIZE bits. The
329 mode must be in class MCLASS, and have exactly that many value bits;
330 it may have padding as well. If LIMIT is nonzero, modes of wider
331 than MAX_FIXED_MODE_SIZE will not be used. */
334 mode_for_size (unsigned int size
, enum mode_class mclass
, int limit
)
339 if (limit
&& size
> MAX_FIXED_MODE_SIZE
)
342 /* Get the first mode which has this size, in the specified class. */
343 for (mode
= GET_CLASS_NARROWEST_MODE (mclass
); mode
!= VOIDmode
;
344 mode
= GET_MODE_WIDER_MODE (mode
))
345 if (GET_MODE_PRECISION (mode
) == size
)
348 if (mclass
== MODE_INT
|| mclass
== MODE_PARTIAL_INT
)
349 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
350 if (int_n_data
[i
].bitsize
== size
351 && int_n_enabled_p
[i
])
352 return int_n_data
[i
].m
;
357 /* Similar, except passed a tree node. */
360 mode_for_size_tree (const_tree size
, enum mode_class mclass
, int limit
)
362 unsigned HOST_WIDE_INT uhwi
;
365 if (!tree_fits_uhwi_p (size
))
367 uhwi
= tree_to_uhwi (size
);
371 return mode_for_size (ui
, mclass
, limit
);
374 /* Similar, but never return BLKmode; return the narrowest mode that
375 contains at least the requested number of value bits. */
378 smallest_mode_for_size (unsigned int size
, enum mode_class mclass
)
380 machine_mode mode
= VOIDmode
;
383 /* Get the first mode which has at least this size, in the
385 for (mode
= GET_CLASS_NARROWEST_MODE (mclass
); mode
!= VOIDmode
;
386 mode
= GET_MODE_WIDER_MODE (mode
))
387 if (GET_MODE_PRECISION (mode
) >= size
)
390 if (mclass
== MODE_INT
|| mclass
== MODE_PARTIAL_INT
)
391 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
392 if (int_n_data
[i
].bitsize
>= size
393 && int_n_data
[i
].bitsize
< GET_MODE_PRECISION (mode
)
394 && int_n_enabled_p
[i
])
395 mode
= int_n_data
[i
].m
;
397 if (mode
== VOIDmode
)
403 /* Find an integer mode of the exact same size, or BLKmode on failure. */
406 int_mode_for_mode (machine_mode mode
)
408 switch (GET_MODE_CLASS (mode
))
411 case MODE_PARTIAL_INT
:
414 case MODE_COMPLEX_INT
:
415 case MODE_COMPLEX_FLOAT
:
417 case MODE_DECIMAL_FLOAT
:
418 case MODE_VECTOR_INT
:
419 case MODE_VECTOR_FLOAT
:
424 case MODE_VECTOR_FRACT
:
425 case MODE_VECTOR_ACCUM
:
426 case MODE_VECTOR_UFRACT
:
427 case MODE_VECTOR_UACCUM
:
428 case MODE_POINTER_BOUNDS
:
429 mode
= mode_for_size (GET_MODE_BITSIZE (mode
), MODE_INT
, 0);
436 /* ... fall through ... */
446 /* Find a mode that can be used for efficient bitwise operations on MODE.
447 Return BLKmode if no such mode exists. */
450 bitwise_mode_for_mode (machine_mode mode
)
452 /* Quick exit if we already have a suitable mode. */
453 unsigned int bitsize
= GET_MODE_BITSIZE (mode
);
454 if (SCALAR_INT_MODE_P (mode
) && bitsize
<= MAX_FIXED_MODE_SIZE
)
457 /* Reuse the sanity checks from int_mode_for_mode. */
458 gcc_checking_assert ((int_mode_for_mode (mode
), true));
460 /* Try to replace complex modes with complex modes. In general we
461 expect both components to be processed independently, so we only
462 care whether there is a register for the inner mode. */
463 if (COMPLEX_MODE_P (mode
))
465 machine_mode trial
= mode
;
466 if (GET_MODE_CLASS (mode
) != MODE_COMPLEX_INT
)
467 trial
= mode_for_size (bitsize
, MODE_COMPLEX_INT
, false);
469 && have_regs_of_mode
[GET_MODE_INNER (trial
)])
473 /* Try to replace vector modes with vector modes. Also try using vector
474 modes if an integer mode would be too big. */
475 if (VECTOR_MODE_P (mode
) || bitsize
> MAX_FIXED_MODE_SIZE
)
477 machine_mode trial
= mode
;
478 if (GET_MODE_CLASS (mode
) != MODE_VECTOR_INT
)
479 trial
= mode_for_size (bitsize
, MODE_VECTOR_INT
, 0);
481 && have_regs_of_mode
[trial
]
482 && targetm
.vector_mode_supported_p (trial
))
486 /* Otherwise fall back on integers while honoring MAX_FIXED_MODE_SIZE. */
487 return mode_for_size (bitsize
, MODE_INT
, true);
490 /* Find a type that can be used for efficient bitwise operations on MODE.
491 Return null if no such mode exists. */
494 bitwise_type_for_mode (machine_mode mode
)
496 mode
= bitwise_mode_for_mode (mode
);
500 unsigned int inner_size
= GET_MODE_UNIT_BITSIZE (mode
);
501 tree inner_type
= build_nonstandard_integer_type (inner_size
, true);
503 if (VECTOR_MODE_P (mode
))
504 return build_vector_type_for_mode (inner_type
, mode
);
506 if (COMPLEX_MODE_P (mode
))
507 return build_complex_type (inner_type
);
509 gcc_checking_assert (GET_MODE_INNER (mode
) == VOIDmode
);
513 /* Find a mode that is suitable for representing a vector with
514 NUNITS elements of mode INNERMODE. Returns BLKmode if there
515 is no suitable mode. */
518 mode_for_vector (machine_mode innermode
, unsigned nunits
)
522 /* First, look for a supported vector type. */
523 if (SCALAR_FLOAT_MODE_P (innermode
))
524 mode
= MIN_MODE_VECTOR_FLOAT
;
525 else if (SCALAR_FRACT_MODE_P (innermode
))
526 mode
= MIN_MODE_VECTOR_FRACT
;
527 else if (SCALAR_UFRACT_MODE_P (innermode
))
528 mode
= MIN_MODE_VECTOR_UFRACT
;
529 else if (SCALAR_ACCUM_MODE_P (innermode
))
530 mode
= MIN_MODE_VECTOR_ACCUM
;
531 else if (SCALAR_UACCUM_MODE_P (innermode
))
532 mode
= MIN_MODE_VECTOR_UACCUM
;
534 mode
= MIN_MODE_VECTOR_INT
;
536 /* Do not check vector_mode_supported_p here. We'll do that
537 later in vector_type_mode. */
538 for (; mode
!= VOIDmode
; mode
= GET_MODE_WIDER_MODE (mode
))
539 if (GET_MODE_NUNITS (mode
) == nunits
540 && GET_MODE_INNER (mode
) == innermode
)
543 /* For integers, try mapping it to a same-sized scalar mode. */
545 && GET_MODE_CLASS (innermode
) == MODE_INT
)
546 mode
= mode_for_size (nunits
* GET_MODE_BITSIZE (innermode
),
550 || (GET_MODE_CLASS (mode
) == MODE_INT
551 && !have_regs_of_mode
[mode
]))
557 /* Return the alignment of MODE. This will be bounded by 1 and
558 BIGGEST_ALIGNMENT. */
561 get_mode_alignment (machine_mode mode
)
563 return MIN (BIGGEST_ALIGNMENT
, MAX (1, mode_base_align
[mode
]*BITS_PER_UNIT
));
566 /* Return the precision of the mode, or for a complex or vector mode the
567 precision of the mode of its elements. */
570 element_precision (machine_mode mode
)
572 if (COMPLEX_MODE_P (mode
) || VECTOR_MODE_P (mode
))
573 mode
= GET_MODE_INNER (mode
);
575 return GET_MODE_PRECISION (mode
);
578 /* Return the natural mode of an array, given that it is SIZE bytes in
579 total and has elements of type ELEM_TYPE. */
582 mode_for_array (tree elem_type
, tree size
)
585 unsigned HOST_WIDE_INT int_size
, int_elem_size
;
588 /* One-element arrays get the component type's mode. */
589 elem_size
= TYPE_SIZE (elem_type
);
590 if (simple_cst_equal (size
, elem_size
))
591 return TYPE_MODE (elem_type
);
594 if (tree_fits_uhwi_p (size
) && tree_fits_uhwi_p (elem_size
))
596 int_size
= tree_to_uhwi (size
);
597 int_elem_size
= tree_to_uhwi (elem_size
);
598 if (int_elem_size
> 0
599 && int_size
% int_elem_size
== 0
600 && targetm
.array_mode_supported_p (TYPE_MODE (elem_type
),
601 int_size
/ int_elem_size
))
604 return mode_for_size_tree (size
, MODE_INT
, limit_p
);
607 /* Subroutine of layout_decl: Force alignment required for the data type.
608 But if the decl itself wants greater alignment, don't override that. */
611 do_type_align (tree type
, tree decl
)
613 if (TYPE_ALIGN (type
) > DECL_ALIGN (decl
))
615 DECL_ALIGN (decl
) = TYPE_ALIGN (type
);
616 if (TREE_CODE (decl
) == FIELD_DECL
)
617 DECL_USER_ALIGN (decl
) = TYPE_USER_ALIGN (type
);
621 /* Set the size, mode and alignment of a ..._DECL node.
622 TYPE_DECL does need this for C++.
623 Note that LABEL_DECL and CONST_DECL nodes do not need this,
624 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
625 Don't call layout_decl for them.
627 KNOWN_ALIGN is the amount of alignment we can assume this
628 decl has with no special effort. It is relevant only for FIELD_DECLs
629 and depends on the previous fields.
630 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
631 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
632 the record will be aligned to suit. */
635 layout_decl (tree decl
, unsigned int known_align
)
637 tree type
= TREE_TYPE (decl
);
638 enum tree_code code
= TREE_CODE (decl
);
640 location_t loc
= DECL_SOURCE_LOCATION (decl
);
642 if (code
== CONST_DECL
)
645 gcc_assert (code
== VAR_DECL
|| code
== PARM_DECL
|| code
== RESULT_DECL
646 || code
== TYPE_DECL
||code
== FIELD_DECL
);
648 rtl
= DECL_RTL_IF_SET (decl
);
650 if (type
== error_mark_node
)
651 type
= void_type_node
;
653 /* Usually the size and mode come from the data type without change,
654 however, the front-end may set the explicit width of the field, so its
655 size may not be the same as the size of its type. This happens with
656 bitfields, of course (an `int' bitfield may be only 2 bits, say), but it
657 also happens with other fields. For example, the C++ front-end creates
658 zero-sized fields corresponding to empty base classes, and depends on
659 layout_type setting DECL_FIELD_BITPOS correctly for the field. Set the
660 size in bytes from the size in bits. If we have already set the mode,
661 don't set it again since we can be called twice for FIELD_DECLs. */
663 DECL_UNSIGNED (decl
) = TYPE_UNSIGNED (type
);
664 if (DECL_MODE (decl
) == VOIDmode
)
665 DECL_MODE (decl
) = TYPE_MODE (type
);
667 if (DECL_SIZE (decl
) == 0)
669 DECL_SIZE (decl
) = TYPE_SIZE (type
);
670 DECL_SIZE_UNIT (decl
) = TYPE_SIZE_UNIT (type
);
672 else if (DECL_SIZE_UNIT (decl
) == 0)
673 DECL_SIZE_UNIT (decl
)
674 = fold_convert_loc (loc
, sizetype
,
675 size_binop_loc (loc
, CEIL_DIV_EXPR
, DECL_SIZE (decl
),
678 if (code
!= FIELD_DECL
)
679 /* For non-fields, update the alignment from the type. */
680 do_type_align (type
, decl
);
682 /* For fields, it's a bit more complicated... */
684 bool old_user_align
= DECL_USER_ALIGN (decl
);
685 bool zero_bitfield
= false;
686 bool packed_p
= DECL_PACKED (decl
);
689 if (DECL_BIT_FIELD (decl
))
691 DECL_BIT_FIELD_TYPE (decl
) = type
;
693 /* A zero-length bit-field affects the alignment of the next
694 field. In essence such bit-fields are not influenced by
695 any packing due to #pragma pack or attribute packed. */
696 if (integer_zerop (DECL_SIZE (decl
))
697 && ! targetm
.ms_bitfield_layout_p (DECL_FIELD_CONTEXT (decl
)))
699 zero_bitfield
= true;
701 #ifdef PCC_BITFIELD_TYPE_MATTERS
702 if (PCC_BITFIELD_TYPE_MATTERS
)
703 do_type_align (type
, decl
);
707 #ifdef EMPTY_FIELD_BOUNDARY
708 if (EMPTY_FIELD_BOUNDARY
> DECL_ALIGN (decl
))
710 DECL_ALIGN (decl
) = EMPTY_FIELD_BOUNDARY
;
711 DECL_USER_ALIGN (decl
) = 0;
717 /* See if we can use an ordinary integer mode for a bit-field.
718 Conditions are: a fixed size that is correct for another mode,
719 occupying a complete byte or bytes on proper boundary. */
720 if (TYPE_SIZE (type
) != 0
721 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
722 && GET_MODE_CLASS (TYPE_MODE (type
)) == MODE_INT
)
725 = mode_for_size_tree (DECL_SIZE (decl
), MODE_INT
, 1);
726 unsigned int xalign
= GET_MODE_ALIGNMENT (xmode
);
729 && !(xalign
> BITS_PER_UNIT
&& DECL_PACKED (decl
))
730 && (known_align
== 0 || known_align
>= xalign
))
732 DECL_ALIGN (decl
) = MAX (xalign
, DECL_ALIGN (decl
));
733 DECL_MODE (decl
) = xmode
;
734 DECL_BIT_FIELD (decl
) = 0;
738 /* Turn off DECL_BIT_FIELD if we won't need it set. */
739 if (TYPE_MODE (type
) == BLKmode
&& DECL_MODE (decl
) == BLKmode
740 && known_align
>= TYPE_ALIGN (type
)
741 && DECL_ALIGN (decl
) >= TYPE_ALIGN (type
))
742 DECL_BIT_FIELD (decl
) = 0;
744 else if (packed_p
&& DECL_USER_ALIGN (decl
))
745 /* Don't touch DECL_ALIGN. For other packed fields, go ahead and
746 round up; we'll reduce it again below. We want packing to
747 supersede USER_ALIGN inherited from the type, but defer to
748 alignment explicitly specified on the field decl. */;
750 do_type_align (type
, decl
);
752 /* If the field is packed and not explicitly aligned, give it the
753 minimum alignment. Note that do_type_align may set
754 DECL_USER_ALIGN, so we need to check old_user_align instead. */
757 DECL_ALIGN (decl
) = MIN (DECL_ALIGN (decl
), BITS_PER_UNIT
);
759 if (! packed_p
&& ! DECL_USER_ALIGN (decl
))
761 /* Some targets (i.e. i386, VMS) limit struct field alignment
762 to a lower boundary than alignment of variables unless
763 it was overridden by attribute aligned. */
764 #ifdef BIGGEST_FIELD_ALIGNMENT
766 = MIN (DECL_ALIGN (decl
), (unsigned) BIGGEST_FIELD_ALIGNMENT
);
768 #ifdef ADJUST_FIELD_ALIGN
769 DECL_ALIGN (decl
) = ADJUST_FIELD_ALIGN (decl
, DECL_ALIGN (decl
));
774 mfa
= initial_max_fld_align
* BITS_PER_UNIT
;
776 mfa
= maximum_field_alignment
;
777 /* Should this be controlled by DECL_USER_ALIGN, too? */
779 DECL_ALIGN (decl
) = MIN (DECL_ALIGN (decl
), mfa
);
782 /* Evaluate nonconstant size only once, either now or as soon as safe. */
783 if (DECL_SIZE (decl
) != 0 && TREE_CODE (DECL_SIZE (decl
)) != INTEGER_CST
)
784 DECL_SIZE (decl
) = variable_size (DECL_SIZE (decl
));
785 if (DECL_SIZE_UNIT (decl
) != 0
786 && TREE_CODE (DECL_SIZE_UNIT (decl
)) != INTEGER_CST
)
787 DECL_SIZE_UNIT (decl
) = variable_size (DECL_SIZE_UNIT (decl
));
789 /* If requested, warn about definitions of large data objects. */
791 && (code
== VAR_DECL
|| code
== PARM_DECL
)
792 && ! DECL_EXTERNAL (decl
))
794 tree size
= DECL_SIZE_UNIT (decl
);
796 if (size
!= 0 && TREE_CODE (size
) == INTEGER_CST
797 && compare_tree_int (size
, larger_than_size
) > 0)
799 int size_as_int
= TREE_INT_CST_LOW (size
);
801 if (compare_tree_int (size
, size_as_int
) == 0)
802 warning (OPT_Wlarger_than_
, "size of %q+D is %d bytes", decl
, size_as_int
);
804 warning (OPT_Wlarger_than_
, "size of %q+D is larger than %wd bytes",
805 decl
, larger_than_size
);
809 /* If the RTL was already set, update its mode and mem attributes. */
812 PUT_MODE (rtl
, DECL_MODE (decl
));
813 SET_DECL_RTL (decl
, 0);
814 set_mem_attributes (rtl
, decl
, 1);
815 SET_DECL_RTL (decl
, rtl
);
819 /* Given a VAR_DECL, PARM_DECL or RESULT_DECL, clears the results of
820 a previous call to layout_decl and calls it again. */
823 relayout_decl (tree decl
)
825 DECL_SIZE (decl
) = DECL_SIZE_UNIT (decl
) = 0;
826 DECL_MODE (decl
) = VOIDmode
;
827 if (!DECL_USER_ALIGN (decl
))
828 DECL_ALIGN (decl
) = 0;
829 SET_DECL_RTL (decl
, 0);
831 layout_decl (decl
, 0);
834 /* Begin laying out type T, which may be a RECORD_TYPE, UNION_TYPE, or
835 QUAL_UNION_TYPE. Return a pointer to a struct record_layout_info which
836 is to be passed to all other layout functions for this record. It is the
837 responsibility of the caller to call `free' for the storage returned.
838 Note that garbage collection is not permitted until we finish laying
842 start_record_layout (tree t
)
844 record_layout_info rli
= XNEW (struct record_layout_info_s
);
848 /* If the type has a minimum specified alignment (via an attribute
849 declaration, for example) use it -- otherwise, start with a
850 one-byte alignment. */
851 rli
->record_align
= MAX (BITS_PER_UNIT
, TYPE_ALIGN (t
));
852 rli
->unpacked_align
= rli
->record_align
;
853 rli
->offset_align
= MAX (rli
->record_align
, BIGGEST_ALIGNMENT
);
855 #ifdef STRUCTURE_SIZE_BOUNDARY
856 /* Packed structures don't need to have minimum size. */
857 if (! TYPE_PACKED (t
))
861 /* #pragma pack overrides STRUCTURE_SIZE_BOUNDARY. */
862 tmp
= (unsigned) STRUCTURE_SIZE_BOUNDARY
;
863 if (maximum_field_alignment
!= 0)
864 tmp
= MIN (tmp
, maximum_field_alignment
);
865 rli
->record_align
= MAX (rli
->record_align
, tmp
);
869 rli
->offset
= size_zero_node
;
870 rli
->bitpos
= bitsize_zero_node
;
872 rli
->pending_statics
= 0;
873 rli
->packed_maybe_necessary
= 0;
874 rli
->remaining_in_alignment
= 0;
879 /* Return the combined bit position for the byte offset OFFSET and the
882 These functions operate on byte and bit positions present in FIELD_DECLs
883 and assume that these expressions result in no (intermediate) overflow.
884 This assumption is necessary to fold the expressions as much as possible,
885 so as to avoid creating artificially variable-sized types in languages
886 supporting variable-sized types like Ada. */
889 bit_from_pos (tree offset
, tree bitpos
)
891 if (TREE_CODE (offset
) == PLUS_EXPR
)
892 offset
= size_binop (PLUS_EXPR
,
893 fold_convert (bitsizetype
, TREE_OPERAND (offset
, 0)),
894 fold_convert (bitsizetype
, TREE_OPERAND (offset
, 1)));
896 offset
= fold_convert (bitsizetype
, offset
);
897 return size_binop (PLUS_EXPR
, bitpos
,
898 size_binop (MULT_EXPR
, offset
, bitsize_unit_node
));
901 /* Return the combined truncated byte position for the byte offset OFFSET and
902 the bit position BITPOS. */
905 byte_from_pos (tree offset
, tree bitpos
)
908 if (TREE_CODE (bitpos
) == MULT_EXPR
909 && tree_int_cst_equal (TREE_OPERAND (bitpos
, 1), bitsize_unit_node
))
910 bytepos
= TREE_OPERAND (bitpos
, 0);
912 bytepos
= size_binop (TRUNC_DIV_EXPR
, bitpos
, bitsize_unit_node
);
913 return size_binop (PLUS_EXPR
, offset
, fold_convert (sizetype
, bytepos
));
916 /* Split the bit position POS into a byte offset *POFFSET and a bit
917 position *PBITPOS with the byte offset aligned to OFF_ALIGN bits. */
920 pos_from_bit (tree
*poffset
, tree
*pbitpos
, unsigned int off_align
,
923 tree toff_align
= bitsize_int (off_align
);
924 if (TREE_CODE (pos
) == MULT_EXPR
925 && tree_int_cst_equal (TREE_OPERAND (pos
, 1), toff_align
))
927 *poffset
= size_binop (MULT_EXPR
,
928 fold_convert (sizetype
, TREE_OPERAND (pos
, 0)),
929 size_int (off_align
/ BITS_PER_UNIT
));
930 *pbitpos
= bitsize_zero_node
;
934 *poffset
= size_binop (MULT_EXPR
,
935 fold_convert (sizetype
,
936 size_binop (FLOOR_DIV_EXPR
, pos
,
938 size_int (off_align
/ BITS_PER_UNIT
));
939 *pbitpos
= size_binop (FLOOR_MOD_EXPR
, pos
, toff_align
);
943 /* Given a pointer to bit and byte offsets and an offset alignment,
944 normalize the offsets so they are within the alignment. */
947 normalize_offset (tree
*poffset
, tree
*pbitpos
, unsigned int off_align
)
949 /* If the bit position is now larger than it should be, adjust it
951 if (compare_tree_int (*pbitpos
, off_align
) >= 0)
954 pos_from_bit (&offset
, &bitpos
, off_align
, *pbitpos
);
955 *poffset
= size_binop (PLUS_EXPR
, *poffset
, offset
);
960 /* Print debugging information about the information in RLI. */
963 debug_rli (record_layout_info rli
)
965 print_node_brief (stderr
, "type", rli
->t
, 0);
966 print_node_brief (stderr
, "\noffset", rli
->offset
, 0);
967 print_node_brief (stderr
, " bitpos", rli
->bitpos
, 0);
969 fprintf (stderr
, "\naligns: rec = %u, unpack = %u, off = %u\n",
970 rli
->record_align
, rli
->unpacked_align
,
973 /* The ms_struct code is the only that uses this. */
974 if (targetm
.ms_bitfield_layout_p (rli
->t
))
975 fprintf (stderr
, "remaining in alignment = %u\n", rli
->remaining_in_alignment
);
977 if (rli
->packed_maybe_necessary
)
978 fprintf (stderr
, "packed may be necessary\n");
980 if (!vec_safe_is_empty (rli
->pending_statics
))
982 fprintf (stderr
, "pending statics:\n");
983 debug_vec_tree (rli
->pending_statics
);
987 /* Given an RLI with a possibly-incremented BITPOS, adjust OFFSET and
988 BITPOS if necessary to keep BITPOS below OFFSET_ALIGN. */
991 normalize_rli (record_layout_info rli
)
993 normalize_offset (&rli
->offset
, &rli
->bitpos
, rli
->offset_align
);
996 /* Returns the size in bytes allocated so far. */
999 rli_size_unit_so_far (record_layout_info rli
)
1001 return byte_from_pos (rli
->offset
, rli
->bitpos
);
1004 /* Returns the size in bits allocated so far. */
1007 rli_size_so_far (record_layout_info rli
)
1009 return bit_from_pos (rli
->offset
, rli
->bitpos
);
1012 /* FIELD is about to be added to RLI->T. The alignment (in bits) of
1013 the next available location within the record is given by KNOWN_ALIGN.
1014 Update the variable alignment fields in RLI, and return the alignment
1015 to give the FIELD. */
1018 update_alignment_for_field (record_layout_info rli
, tree field
,
1019 unsigned int known_align
)
1021 /* The alignment required for FIELD. */
1022 unsigned int desired_align
;
1023 /* The type of this field. */
1024 tree type
= TREE_TYPE (field
);
1025 /* True if the field was explicitly aligned by the user. */
1029 /* Do not attempt to align an ERROR_MARK node */
1030 if (TREE_CODE (type
) == ERROR_MARK
)
1033 /* Lay out the field so we know what alignment it needs. */
1034 layout_decl (field
, known_align
);
1035 desired_align
= DECL_ALIGN (field
);
1036 user_align
= DECL_USER_ALIGN (field
);
1038 is_bitfield
= (type
!= error_mark_node
1039 && DECL_BIT_FIELD_TYPE (field
)
1040 && ! integer_zerop (TYPE_SIZE (type
)));
1042 /* Record must have at least as much alignment as any field.
1043 Otherwise, the alignment of the field within the record is
1045 if (targetm
.ms_bitfield_layout_p (rli
->t
))
1047 /* Here, the alignment of the underlying type of a bitfield can
1048 affect the alignment of a record; even a zero-sized field
1049 can do this. The alignment should be to the alignment of
1050 the type, except that for zero-size bitfields this only
1051 applies if there was an immediately prior, nonzero-size
1052 bitfield. (That's the way it is, experimentally.) */
1053 if ((!is_bitfield
&& !DECL_PACKED (field
))
1054 || ((DECL_SIZE (field
) == NULL_TREE
1055 || !integer_zerop (DECL_SIZE (field
)))
1056 ? !DECL_PACKED (field
)
1058 && DECL_BIT_FIELD_TYPE (rli
->prev_field
)
1059 && ! integer_zerop (DECL_SIZE (rli
->prev_field
)))))
1061 unsigned int type_align
= TYPE_ALIGN (type
);
1062 type_align
= MAX (type_align
, desired_align
);
1063 if (maximum_field_alignment
!= 0)
1064 type_align
= MIN (type_align
, maximum_field_alignment
);
1065 rli
->record_align
= MAX (rli
->record_align
, type_align
);
1066 rli
->unpacked_align
= MAX (rli
->unpacked_align
, TYPE_ALIGN (type
));
1069 #ifdef PCC_BITFIELD_TYPE_MATTERS
1070 else if (is_bitfield
&& PCC_BITFIELD_TYPE_MATTERS
)
1072 /* Named bit-fields cause the entire structure to have the
1073 alignment implied by their type. Some targets also apply the same
1074 rules to unnamed bitfields. */
1075 if (DECL_NAME (field
) != 0
1076 || targetm
.align_anon_bitfield ())
1078 unsigned int type_align
= TYPE_ALIGN (type
);
1080 #ifdef ADJUST_FIELD_ALIGN
1081 if (! TYPE_USER_ALIGN (type
))
1082 type_align
= ADJUST_FIELD_ALIGN (field
, type_align
);
1085 /* Targets might chose to handle unnamed and hence possibly
1086 zero-width bitfield. Those are not influenced by #pragmas
1087 or packed attributes. */
1088 if (integer_zerop (DECL_SIZE (field
)))
1090 if (initial_max_fld_align
)
1091 type_align
= MIN (type_align
,
1092 initial_max_fld_align
* BITS_PER_UNIT
);
1094 else if (maximum_field_alignment
!= 0)
1095 type_align
= MIN (type_align
, maximum_field_alignment
);
1096 else if (DECL_PACKED (field
))
1097 type_align
= MIN (type_align
, BITS_PER_UNIT
);
1099 /* The alignment of the record is increased to the maximum
1100 of the current alignment, the alignment indicated on the
1101 field (i.e., the alignment specified by an __aligned__
1102 attribute), and the alignment indicated by the type of
1104 rli
->record_align
= MAX (rli
->record_align
, desired_align
);
1105 rli
->record_align
= MAX (rli
->record_align
, type_align
);
1108 rli
->unpacked_align
= MAX (rli
->unpacked_align
, TYPE_ALIGN (type
));
1109 user_align
|= TYPE_USER_ALIGN (type
);
1115 rli
->record_align
= MAX (rli
->record_align
, desired_align
);
1116 rli
->unpacked_align
= MAX (rli
->unpacked_align
, TYPE_ALIGN (type
));
1119 TYPE_USER_ALIGN (rli
->t
) |= user_align
;
1121 return desired_align
;
1124 /* Called from place_field to handle unions. */
1127 place_union_field (record_layout_info rli
, tree field
)
1129 update_alignment_for_field (rli
, field
, /*known_align=*/0);
1131 DECL_FIELD_OFFSET (field
) = size_zero_node
;
1132 DECL_FIELD_BIT_OFFSET (field
) = bitsize_zero_node
;
1133 SET_DECL_OFFSET_ALIGN (field
, BIGGEST_ALIGNMENT
);
1135 /* If this is an ERROR_MARK return *after* having set the
1136 field at the start of the union. This helps when parsing
1138 if (TREE_CODE (TREE_TYPE (field
)) == ERROR_MARK
)
1141 /* We assume the union's size will be a multiple of a byte so we don't
1142 bother with BITPOS. */
1143 if (TREE_CODE (rli
->t
) == UNION_TYPE
)
1144 rli
->offset
= size_binop (MAX_EXPR
, rli
->offset
, DECL_SIZE_UNIT (field
));
1145 else if (TREE_CODE (rli
->t
) == QUAL_UNION_TYPE
)
1146 rli
->offset
= fold_build3 (COND_EXPR
, sizetype
, DECL_QUALIFIER (field
),
1147 DECL_SIZE_UNIT (field
), rli
->offset
);
1150 #if defined (PCC_BITFIELD_TYPE_MATTERS) || defined (BITFIELD_NBYTES_LIMITED)
1151 /* A bitfield of SIZE with a required access alignment of ALIGN is allocated
1152 at BYTE_OFFSET / BIT_OFFSET. Return nonzero if the field would span more
1153 units of alignment than the underlying TYPE. */
1155 excess_unit_span (HOST_WIDE_INT byte_offset
, HOST_WIDE_INT bit_offset
,
1156 HOST_WIDE_INT size
, HOST_WIDE_INT align
, tree type
)
1158 /* Note that the calculation of OFFSET might overflow; we calculate it so
1159 that we still get the right result as long as ALIGN is a power of two. */
1160 unsigned HOST_WIDE_INT offset
= byte_offset
* BITS_PER_UNIT
+ bit_offset
;
1162 offset
= offset
% align
;
1163 return ((offset
+ size
+ align
- 1) / align
1164 > tree_to_uhwi (TYPE_SIZE (type
)) / align
);
1168 /* RLI contains information about the layout of a RECORD_TYPE. FIELD
1169 is a FIELD_DECL to be added after those fields already present in
1170 T. (FIELD is not actually added to the TYPE_FIELDS list here;
1171 callers that desire that behavior must manually perform that step.) */
1174 place_field (record_layout_info rli
, tree field
)
1176 /* The alignment required for FIELD. */
1177 unsigned int desired_align
;
1178 /* The alignment FIELD would have if we just dropped it into the
1179 record as it presently stands. */
1180 unsigned int known_align
;
1181 unsigned int actual_align
;
1182 /* The type of this field. */
1183 tree type
= TREE_TYPE (field
);
1185 gcc_assert (TREE_CODE (field
) != ERROR_MARK
);
1187 /* If FIELD is static, then treat it like a separate variable, not
1188 really like a structure field. If it is a FUNCTION_DECL, it's a
1189 method. In both cases, all we do is lay out the decl, and we do
1190 it *after* the record is laid out. */
1191 if (TREE_CODE (field
) == VAR_DECL
)
1193 vec_safe_push (rli
->pending_statics
, field
);
1197 /* Enumerators and enum types which are local to this class need not
1198 be laid out. Likewise for initialized constant fields. */
1199 else if (TREE_CODE (field
) != FIELD_DECL
)
1202 /* Unions are laid out very differently than records, so split
1203 that code off to another function. */
1204 else if (TREE_CODE (rli
->t
) != RECORD_TYPE
)
1206 place_union_field (rli
, field
);
1210 else if (TREE_CODE (type
) == ERROR_MARK
)
1212 /* Place this field at the current allocation position, so we
1213 maintain monotonicity. */
1214 DECL_FIELD_OFFSET (field
) = rli
->offset
;
1215 DECL_FIELD_BIT_OFFSET (field
) = rli
->bitpos
;
1216 SET_DECL_OFFSET_ALIGN (field
, rli
->offset_align
);
1220 /* Work out the known alignment so far. Note that A & (-A) is the
1221 value of the least-significant bit in A that is one. */
1222 if (! integer_zerop (rli
->bitpos
))
1223 known_align
= (tree_to_uhwi (rli
->bitpos
)
1224 & - tree_to_uhwi (rli
->bitpos
));
1225 else if (integer_zerop (rli
->offset
))
1227 else if (tree_fits_uhwi_p (rli
->offset
))
1228 known_align
= (BITS_PER_UNIT
1229 * (tree_to_uhwi (rli
->offset
)
1230 & - tree_to_uhwi (rli
->offset
)));
1232 known_align
= rli
->offset_align
;
1234 desired_align
= update_alignment_for_field (rli
, field
, known_align
);
1235 if (known_align
== 0)
1236 known_align
= MAX (BIGGEST_ALIGNMENT
, rli
->record_align
);
1238 if (warn_packed
&& DECL_PACKED (field
))
1240 if (known_align
>= TYPE_ALIGN (type
))
1242 if (TYPE_ALIGN (type
) > desired_align
)
1244 if (STRICT_ALIGNMENT
)
1245 warning (OPT_Wattributes
, "packed attribute causes "
1246 "inefficient alignment for %q+D", field
);
1247 /* Don't warn if DECL_PACKED was set by the type. */
1248 else if (!TYPE_PACKED (rli
->t
))
1249 warning (OPT_Wattributes
, "packed attribute is "
1250 "unnecessary for %q+D", field
);
1254 rli
->packed_maybe_necessary
= 1;
1257 /* Does this field automatically have alignment it needs by virtue
1258 of the fields that precede it and the record's own alignment? */
1259 if (known_align
< desired_align
)
1261 /* No, we need to skip space before this field.
1262 Bump the cumulative size to multiple of field alignment. */
1264 if (!targetm
.ms_bitfield_layout_p (rli
->t
)
1265 && DECL_SOURCE_LOCATION (field
) != BUILTINS_LOCATION
)
1266 warning (OPT_Wpadded
, "padding struct to align %q+D", field
);
1268 /* If the alignment is still within offset_align, just align
1269 the bit position. */
1270 if (desired_align
< rli
->offset_align
)
1271 rli
->bitpos
= round_up (rli
->bitpos
, desired_align
);
1274 /* First adjust OFFSET by the partial bits, then align. */
1276 = size_binop (PLUS_EXPR
, rli
->offset
,
1277 fold_convert (sizetype
,
1278 size_binop (CEIL_DIV_EXPR
, rli
->bitpos
,
1279 bitsize_unit_node
)));
1280 rli
->bitpos
= bitsize_zero_node
;
1282 rli
->offset
= round_up (rli
->offset
, desired_align
/ BITS_PER_UNIT
);
1285 if (! TREE_CONSTANT (rli
->offset
))
1286 rli
->offset_align
= desired_align
;
1287 if (targetm
.ms_bitfield_layout_p (rli
->t
))
1288 rli
->prev_field
= NULL
;
1291 /* Handle compatibility with PCC. Note that if the record has any
1292 variable-sized fields, we need not worry about compatibility. */
1293 #ifdef PCC_BITFIELD_TYPE_MATTERS
1294 if (PCC_BITFIELD_TYPE_MATTERS
1295 && ! targetm
.ms_bitfield_layout_p (rli
->t
)
1296 && TREE_CODE (field
) == FIELD_DECL
1297 && type
!= error_mark_node
1298 && DECL_BIT_FIELD (field
)
1299 && (! DECL_PACKED (field
)
1300 /* Enter for these packed fields only to issue a warning. */
1301 || TYPE_ALIGN (type
) <= BITS_PER_UNIT
)
1302 && maximum_field_alignment
== 0
1303 && ! integer_zerop (DECL_SIZE (field
))
1304 && tree_fits_uhwi_p (DECL_SIZE (field
))
1305 && tree_fits_uhwi_p (rli
->offset
)
1306 && tree_fits_uhwi_p (TYPE_SIZE (type
)))
1308 unsigned int type_align
= TYPE_ALIGN (type
);
1309 tree dsize
= DECL_SIZE (field
);
1310 HOST_WIDE_INT field_size
= tree_to_uhwi (dsize
);
1311 HOST_WIDE_INT offset
= tree_to_uhwi (rli
->offset
);
1312 HOST_WIDE_INT bit_offset
= tree_to_shwi (rli
->bitpos
);
1314 #ifdef ADJUST_FIELD_ALIGN
1315 if (! TYPE_USER_ALIGN (type
))
1316 type_align
= ADJUST_FIELD_ALIGN (field
, type_align
);
1319 /* A bit field may not span more units of alignment of its type
1320 than its type itself. Advance to next boundary if necessary. */
1321 if (excess_unit_span (offset
, bit_offset
, field_size
, type_align
, type
))
1323 if (DECL_PACKED (field
))
1325 if (warn_packed_bitfield_compat
== 1)
1328 "offset of packed bit-field %qD has changed in GCC 4.4",
1332 rli
->bitpos
= round_up (rli
->bitpos
, type_align
);
1335 if (! DECL_PACKED (field
))
1336 TYPE_USER_ALIGN (rli
->t
) |= TYPE_USER_ALIGN (type
);
1340 #ifdef BITFIELD_NBYTES_LIMITED
1341 if (BITFIELD_NBYTES_LIMITED
1342 && ! targetm
.ms_bitfield_layout_p (rli
->t
)
1343 && TREE_CODE (field
) == FIELD_DECL
1344 && type
!= error_mark_node
1345 && DECL_BIT_FIELD_TYPE (field
)
1346 && ! DECL_PACKED (field
)
1347 && ! integer_zerop (DECL_SIZE (field
))
1348 && tree_fits_uhwi_p (DECL_SIZE (field
))
1349 && tree_fits_uhwi_p (rli
->offset
)
1350 && tree_fits_uhwi_p (TYPE_SIZE (type
)))
1352 unsigned int type_align
= TYPE_ALIGN (type
);
1353 tree dsize
= DECL_SIZE (field
);
1354 HOST_WIDE_INT field_size
= tree_to_uhwi (dsize
);
1355 HOST_WIDE_INT offset
= tree_to_uhwi (rli
->offset
);
1356 HOST_WIDE_INT bit_offset
= tree_to_shwi (rli
->bitpos
);
1358 #ifdef ADJUST_FIELD_ALIGN
1359 if (! TYPE_USER_ALIGN (type
))
1360 type_align
= ADJUST_FIELD_ALIGN (field
, type_align
);
1363 if (maximum_field_alignment
!= 0)
1364 type_align
= MIN (type_align
, maximum_field_alignment
);
1365 /* ??? This test is opposite the test in the containing if
1366 statement, so this code is unreachable currently. */
1367 else if (DECL_PACKED (field
))
1368 type_align
= MIN (type_align
, BITS_PER_UNIT
);
1370 /* A bit field may not span the unit of alignment of its type.
1371 Advance to next boundary if necessary. */
1372 if (excess_unit_span (offset
, bit_offset
, field_size
, type_align
, type
))
1373 rli
->bitpos
= round_up (rli
->bitpos
, type_align
);
1375 TYPE_USER_ALIGN (rli
->t
) |= TYPE_USER_ALIGN (type
);
1379 /* See the docs for TARGET_MS_BITFIELD_LAYOUT_P for details.
1381 When a bit field is inserted into a packed record, the whole
1382 size of the underlying type is used by one or more same-size
1383 adjacent bitfields. (That is, if its long:3, 32 bits is
1384 used in the record, and any additional adjacent long bitfields are
1385 packed into the same chunk of 32 bits. However, if the size
1386 changes, a new field of that size is allocated.) In an unpacked
1387 record, this is the same as using alignment, but not equivalent
1390 Note: for compatibility, we use the type size, not the type alignment
1391 to determine alignment, since that matches the documentation */
1393 if (targetm
.ms_bitfield_layout_p (rli
->t
))
1395 tree prev_saved
= rli
->prev_field
;
1396 tree prev_type
= prev_saved
? DECL_BIT_FIELD_TYPE (prev_saved
) : NULL
;
1398 /* This is a bitfield if it exists. */
1399 if (rli
->prev_field
)
1401 /* If both are bitfields, nonzero, and the same size, this is
1402 the middle of a run. Zero declared size fields are special
1403 and handled as "end of run". (Note: it's nonzero declared
1404 size, but equal type sizes!) (Since we know that both
1405 the current and previous fields are bitfields by the
1406 time we check it, DECL_SIZE must be present for both.) */
1407 if (DECL_BIT_FIELD_TYPE (field
)
1408 && !integer_zerop (DECL_SIZE (field
))
1409 && !integer_zerop (DECL_SIZE (rli
->prev_field
))
1410 && tree_fits_shwi_p (DECL_SIZE (rli
->prev_field
))
1411 && tree_fits_uhwi_p (TYPE_SIZE (type
))
1412 && simple_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (prev_type
)))
1414 /* We're in the middle of a run of equal type size fields; make
1415 sure we realign if we run out of bits. (Not decl size,
1417 HOST_WIDE_INT bitsize
= tree_to_uhwi (DECL_SIZE (field
));
1419 if (rli
->remaining_in_alignment
< bitsize
)
1421 HOST_WIDE_INT typesize
= tree_to_uhwi (TYPE_SIZE (type
));
1423 /* out of bits; bump up to next 'word'. */
1425 = size_binop (PLUS_EXPR
, rli
->bitpos
,
1426 bitsize_int (rli
->remaining_in_alignment
));
1427 rli
->prev_field
= field
;
1428 if (typesize
< bitsize
)
1429 rli
->remaining_in_alignment
= 0;
1431 rli
->remaining_in_alignment
= typesize
- bitsize
;
1434 rli
->remaining_in_alignment
-= bitsize
;
1438 /* End of a run: if leaving a run of bitfields of the same type
1439 size, we have to "use up" the rest of the bits of the type
1442 Compute the new position as the sum of the size for the prior
1443 type and where we first started working on that type.
1444 Note: since the beginning of the field was aligned then
1445 of course the end will be too. No round needed. */
1447 if (!integer_zerop (DECL_SIZE (rli
->prev_field
)))
1450 = size_binop (PLUS_EXPR
, rli
->bitpos
,
1451 bitsize_int (rli
->remaining_in_alignment
));
1454 /* We "use up" size zero fields; the code below should behave
1455 as if the prior field was not a bitfield. */
1458 /* Cause a new bitfield to be captured, either this time (if
1459 currently a bitfield) or next time we see one. */
1460 if (!DECL_BIT_FIELD_TYPE (field
)
1461 || integer_zerop (DECL_SIZE (field
)))
1462 rli
->prev_field
= NULL
;
1465 normalize_rli (rli
);
1468 /* If we're starting a new run of same type size bitfields
1469 (or a run of non-bitfields), set up the "first of the run"
1472 That is, if the current field is not a bitfield, or if there
1473 was a prior bitfield the type sizes differ, or if there wasn't
1474 a prior bitfield the size of the current field is nonzero.
1476 Note: we must be sure to test ONLY the type size if there was
1477 a prior bitfield and ONLY for the current field being zero if
1480 if (!DECL_BIT_FIELD_TYPE (field
)
1481 || (prev_saved
!= NULL
1482 ? !simple_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (prev_type
))
1483 : !integer_zerop (DECL_SIZE (field
)) ))
1485 /* Never smaller than a byte for compatibility. */
1486 unsigned int type_align
= BITS_PER_UNIT
;
1488 /* (When not a bitfield), we could be seeing a flex array (with
1489 no DECL_SIZE). Since we won't be using remaining_in_alignment
1490 until we see a bitfield (and come by here again) we just skip
1492 if (DECL_SIZE (field
) != NULL
1493 && tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (field
)))
1494 && tree_fits_uhwi_p (DECL_SIZE (field
)))
1496 unsigned HOST_WIDE_INT bitsize
1497 = tree_to_uhwi (DECL_SIZE (field
));
1498 unsigned HOST_WIDE_INT typesize
1499 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (field
)));
1501 if (typesize
< bitsize
)
1502 rli
->remaining_in_alignment
= 0;
1504 rli
->remaining_in_alignment
= typesize
- bitsize
;
1507 /* Now align (conventionally) for the new type. */
1508 type_align
= TYPE_ALIGN (TREE_TYPE (field
));
1510 if (maximum_field_alignment
!= 0)
1511 type_align
= MIN (type_align
, maximum_field_alignment
);
1513 rli
->bitpos
= round_up (rli
->bitpos
, type_align
);
1515 /* If we really aligned, don't allow subsequent bitfields
1517 rli
->prev_field
= NULL
;
1521 /* Offset so far becomes the position of this field after normalizing. */
1522 normalize_rli (rli
);
1523 DECL_FIELD_OFFSET (field
) = rli
->offset
;
1524 DECL_FIELD_BIT_OFFSET (field
) = rli
->bitpos
;
1525 SET_DECL_OFFSET_ALIGN (field
, rli
->offset_align
);
1527 /* Evaluate nonconstant offsets only once, either now or as soon as safe. */
1528 if (TREE_CODE (DECL_FIELD_OFFSET (field
)) != INTEGER_CST
)
1529 DECL_FIELD_OFFSET (field
) = variable_size (DECL_FIELD_OFFSET (field
));
1531 /* If this field ended up more aligned than we thought it would be (we
1532 approximate this by seeing if its position changed), lay out the field
1533 again; perhaps we can use an integral mode for it now. */
1534 if (! integer_zerop (DECL_FIELD_BIT_OFFSET (field
)))
1535 actual_align
= (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field
))
1536 & - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field
)));
1537 else if (integer_zerop (DECL_FIELD_OFFSET (field
)))
1538 actual_align
= MAX (BIGGEST_ALIGNMENT
, rli
->record_align
);
1539 else if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field
)))
1540 actual_align
= (BITS_PER_UNIT
1541 * (tree_to_uhwi (DECL_FIELD_OFFSET (field
))
1542 & - tree_to_uhwi (DECL_FIELD_OFFSET (field
))));
1544 actual_align
= DECL_OFFSET_ALIGN (field
);
1545 /* ACTUAL_ALIGN is still the actual alignment *within the record* .
1546 store / extract bit field operations will check the alignment of the
1547 record against the mode of bit fields. */
1549 if (known_align
!= actual_align
)
1550 layout_decl (field
, actual_align
);
1552 if (rli
->prev_field
== NULL
&& DECL_BIT_FIELD_TYPE (field
))
1553 rli
->prev_field
= field
;
1555 /* Now add size of this field to the size of the record. If the size is
1556 not constant, treat the field as being a multiple of bytes and just
1557 adjust the offset, resetting the bit position. Otherwise, apportion the
1558 size amongst the bit position and offset. First handle the case of an
1559 unspecified size, which can happen when we have an invalid nested struct
1560 definition, such as struct j { struct j { int i; } }. The error message
1561 is printed in finish_struct. */
1562 if (DECL_SIZE (field
) == 0)
1564 else if (TREE_CODE (DECL_SIZE (field
)) != INTEGER_CST
1565 || TREE_OVERFLOW (DECL_SIZE (field
)))
1568 = size_binop (PLUS_EXPR
, rli
->offset
,
1569 fold_convert (sizetype
,
1570 size_binop (CEIL_DIV_EXPR
, rli
->bitpos
,
1571 bitsize_unit_node
)));
1573 = size_binop (PLUS_EXPR
, rli
->offset
, DECL_SIZE_UNIT (field
));
1574 rli
->bitpos
= bitsize_zero_node
;
1575 rli
->offset_align
= MIN (rli
->offset_align
, desired_align
);
1577 else if (targetm
.ms_bitfield_layout_p (rli
->t
))
1579 rli
->bitpos
= size_binop (PLUS_EXPR
, rli
->bitpos
, DECL_SIZE (field
));
1581 /* If we ended a bitfield before the full length of the type then
1582 pad the struct out to the full length of the last type. */
1583 if ((DECL_CHAIN (field
) == NULL
1584 || TREE_CODE (DECL_CHAIN (field
)) != FIELD_DECL
)
1585 && DECL_BIT_FIELD_TYPE (field
)
1586 && !integer_zerop (DECL_SIZE (field
)))
1587 rli
->bitpos
= size_binop (PLUS_EXPR
, rli
->bitpos
,
1588 bitsize_int (rli
->remaining_in_alignment
));
1590 normalize_rli (rli
);
1594 rli
->bitpos
= size_binop (PLUS_EXPR
, rli
->bitpos
, DECL_SIZE (field
));
1595 normalize_rli (rli
);
1599 /* Assuming that all the fields have been laid out, this function uses
1600 RLI to compute the final TYPE_SIZE, TYPE_ALIGN, etc. for the type
1601 indicated by RLI. */
1604 finalize_record_size (record_layout_info rli
)
1606 tree unpadded_size
, unpadded_size_unit
;
1608 /* Now we want just byte and bit offsets, so set the offset alignment
1609 to be a byte and then normalize. */
1610 rli
->offset_align
= BITS_PER_UNIT
;
1611 normalize_rli (rli
);
1613 /* Determine the desired alignment. */
1614 #ifdef ROUND_TYPE_ALIGN
1615 TYPE_ALIGN (rli
->t
) = ROUND_TYPE_ALIGN (rli
->t
, TYPE_ALIGN (rli
->t
),
1618 TYPE_ALIGN (rli
->t
) = MAX (TYPE_ALIGN (rli
->t
), rli
->record_align
);
1621 /* Compute the size so far. Be sure to allow for extra bits in the
1622 size in bytes. We have guaranteed above that it will be no more
1623 than a single byte. */
1624 unpadded_size
= rli_size_so_far (rli
);
1625 unpadded_size_unit
= rli_size_unit_so_far (rli
);
1626 if (! integer_zerop (rli
->bitpos
))
1628 = size_binop (PLUS_EXPR
, unpadded_size_unit
, size_one_node
);
1630 if (TREE_CODE (unpadded_size_unit
) == INTEGER_CST
1631 && !TREE_OVERFLOW (unpadded_size_unit
)
1632 && !valid_constant_size_p (unpadded_size_unit
))
1633 error ("type %qT is too large", rli
->t
);
1635 /* Round the size up to be a multiple of the required alignment. */
1636 TYPE_SIZE (rli
->t
) = round_up (unpadded_size
, TYPE_ALIGN (rli
->t
));
1637 TYPE_SIZE_UNIT (rli
->t
)
1638 = round_up (unpadded_size_unit
, TYPE_ALIGN_UNIT (rli
->t
));
1640 if (TREE_CONSTANT (unpadded_size
)
1641 && simple_cst_equal (unpadded_size
, TYPE_SIZE (rli
->t
)) == 0
1642 && input_location
!= BUILTINS_LOCATION
)
1643 warning (OPT_Wpadded
, "padding struct size to alignment boundary");
1645 if (warn_packed
&& TREE_CODE (rli
->t
) == RECORD_TYPE
1646 && TYPE_PACKED (rli
->t
) && ! rli
->packed_maybe_necessary
1647 && TREE_CONSTANT (unpadded_size
))
1651 #ifdef ROUND_TYPE_ALIGN
1653 = ROUND_TYPE_ALIGN (rli
->t
, TYPE_ALIGN (rli
->t
), rli
->unpacked_align
);
1655 rli
->unpacked_align
= MAX (TYPE_ALIGN (rli
->t
), rli
->unpacked_align
);
1658 unpacked_size
= round_up (TYPE_SIZE (rli
->t
), rli
->unpacked_align
);
1659 if (simple_cst_equal (unpacked_size
, TYPE_SIZE (rli
->t
)))
1661 if (TYPE_NAME (rli
->t
))
1665 if (TREE_CODE (TYPE_NAME (rli
->t
)) == IDENTIFIER_NODE
)
1666 name
= TYPE_NAME (rli
->t
);
1668 name
= DECL_NAME (TYPE_NAME (rli
->t
));
1670 if (STRICT_ALIGNMENT
)
1671 warning (OPT_Wpacked
, "packed attribute causes inefficient "
1672 "alignment for %qE", name
);
1674 warning (OPT_Wpacked
,
1675 "packed attribute is unnecessary for %qE", name
);
1679 if (STRICT_ALIGNMENT
)
1680 warning (OPT_Wpacked
,
1681 "packed attribute causes inefficient alignment");
1683 warning (OPT_Wpacked
, "packed attribute is unnecessary");
1689 /* Compute the TYPE_MODE for the TYPE (which is a RECORD_TYPE). */
1692 compute_record_mode (tree type
)
1695 machine_mode mode
= VOIDmode
;
1697 /* Most RECORD_TYPEs have BLKmode, so we start off assuming that.
1698 However, if possible, we use a mode that fits in a register
1699 instead, in order to allow for better optimization down the
1701 SET_TYPE_MODE (type
, BLKmode
);
1703 if (! tree_fits_uhwi_p (TYPE_SIZE (type
)))
1706 /* A record which has any BLKmode members must itself be
1707 BLKmode; it can't go in a register. Unless the member is
1708 BLKmode only because it isn't aligned. */
1709 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
1711 if (TREE_CODE (field
) != FIELD_DECL
)
1714 if (TREE_CODE (TREE_TYPE (field
)) == ERROR_MARK
1715 || (TYPE_MODE (TREE_TYPE (field
)) == BLKmode
1716 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field
))
1717 && !(TYPE_SIZE (TREE_TYPE (field
)) != 0
1718 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))))
1719 || ! tree_fits_uhwi_p (bit_position (field
))
1720 || DECL_SIZE (field
) == 0
1721 || ! tree_fits_uhwi_p (DECL_SIZE (field
)))
1724 /* If this field is the whole struct, remember its mode so
1725 that, say, we can put a double in a class into a DF
1726 register instead of forcing it to live in the stack. */
1727 if (simple_cst_equal (TYPE_SIZE (type
), DECL_SIZE (field
)))
1728 mode
= DECL_MODE (field
);
1730 /* With some targets, it is sub-optimal to access an aligned
1731 BLKmode structure as a scalar. */
1732 if (targetm
.member_type_forces_blk (field
, mode
))
1736 /* If we only have one real field; use its mode if that mode's size
1737 matches the type's size. This only applies to RECORD_TYPE. This
1738 does not apply to unions. */
1739 if (TREE_CODE (type
) == RECORD_TYPE
&& mode
!= VOIDmode
1740 && tree_fits_uhwi_p (TYPE_SIZE (type
))
1741 && GET_MODE_BITSIZE (mode
) == tree_to_uhwi (TYPE_SIZE (type
)))
1742 SET_TYPE_MODE (type
, mode
);
1744 SET_TYPE_MODE (type
, mode_for_size_tree (TYPE_SIZE (type
), MODE_INT
, 1));
1746 /* If structure's known alignment is less than what the scalar
1747 mode would need, and it matters, then stick with BLKmode. */
1748 if (TYPE_MODE (type
) != BLKmode
1750 && ! (TYPE_ALIGN (type
) >= BIGGEST_ALIGNMENT
1751 || TYPE_ALIGN (type
) >= GET_MODE_ALIGNMENT (TYPE_MODE (type
))))
1753 /* If this is the only reason this type is BLKmode, then
1754 don't force containing types to be BLKmode. */
1755 TYPE_NO_FORCE_BLK (type
) = 1;
1756 SET_TYPE_MODE (type
, BLKmode
);
1760 /* Compute TYPE_SIZE and TYPE_ALIGN for TYPE, once it has been laid
1764 finalize_type_size (tree type
)
1766 /* Normally, use the alignment corresponding to the mode chosen.
1767 However, where strict alignment is not required, avoid
1768 over-aligning structures, since most compilers do not do this
1771 if (TYPE_MODE (type
) != BLKmode
&& TYPE_MODE (type
) != VOIDmode
1772 && (STRICT_ALIGNMENT
1773 || (TREE_CODE (type
) != RECORD_TYPE
&& TREE_CODE (type
) != UNION_TYPE
1774 && TREE_CODE (type
) != QUAL_UNION_TYPE
1775 && TREE_CODE (type
) != ARRAY_TYPE
)))
1777 unsigned mode_align
= GET_MODE_ALIGNMENT (TYPE_MODE (type
));
1779 /* Don't override a larger alignment requirement coming from a user
1780 alignment of one of the fields. */
1781 if (mode_align
>= TYPE_ALIGN (type
))
1783 TYPE_ALIGN (type
) = mode_align
;
1784 TYPE_USER_ALIGN (type
) = 0;
1788 /* Do machine-dependent extra alignment. */
1789 #ifdef ROUND_TYPE_ALIGN
1791 = ROUND_TYPE_ALIGN (type
, TYPE_ALIGN (type
), BITS_PER_UNIT
);
1794 /* If we failed to find a simple way to calculate the unit size
1795 of the type, find it by division. */
1796 if (TYPE_SIZE_UNIT (type
) == 0 && TYPE_SIZE (type
) != 0)
1797 /* TYPE_SIZE (type) is computed in bitsizetype. After the division, the
1798 result will fit in sizetype. We will get more efficient code using
1799 sizetype, so we force a conversion. */
1800 TYPE_SIZE_UNIT (type
)
1801 = fold_convert (sizetype
,
1802 size_binop (FLOOR_DIV_EXPR
, TYPE_SIZE (type
),
1803 bitsize_unit_node
));
1805 if (TYPE_SIZE (type
) != 0)
1807 TYPE_SIZE (type
) = round_up (TYPE_SIZE (type
), TYPE_ALIGN (type
));
1808 TYPE_SIZE_UNIT (type
)
1809 = round_up (TYPE_SIZE_UNIT (type
), TYPE_ALIGN_UNIT (type
));
1812 /* Evaluate nonconstant sizes only once, either now or as soon as safe. */
1813 if (TYPE_SIZE (type
) != 0 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
1814 TYPE_SIZE (type
) = variable_size (TYPE_SIZE (type
));
1815 if (TYPE_SIZE_UNIT (type
) != 0
1816 && TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
)
1817 TYPE_SIZE_UNIT (type
) = variable_size (TYPE_SIZE_UNIT (type
));
1819 /* Also layout any other variants of the type. */
1820 if (TYPE_NEXT_VARIANT (type
)
1821 || type
!= TYPE_MAIN_VARIANT (type
))
1824 /* Record layout info of this variant. */
1825 tree size
= TYPE_SIZE (type
);
1826 tree size_unit
= TYPE_SIZE_UNIT (type
);
1827 unsigned int align
= TYPE_ALIGN (type
);
1828 unsigned int precision
= TYPE_PRECISION (type
);
1829 unsigned int user_align
= TYPE_USER_ALIGN (type
);
1830 machine_mode mode
= TYPE_MODE (type
);
1832 /* Copy it into all variants. */
1833 for (variant
= TYPE_MAIN_VARIANT (type
);
1835 variant
= TYPE_NEXT_VARIANT (variant
))
1837 TYPE_SIZE (variant
) = size
;
1838 TYPE_SIZE_UNIT (variant
) = size_unit
;
1839 TYPE_ALIGN (variant
) = align
;
1840 TYPE_PRECISION (variant
) = precision
;
1841 TYPE_USER_ALIGN (variant
) = user_align
;
1842 SET_TYPE_MODE (variant
, mode
);
1847 /* Return a new underlying object for a bitfield started with FIELD. */
1850 start_bitfield_representative (tree field
)
1852 tree repr
= make_node (FIELD_DECL
);
1853 DECL_FIELD_OFFSET (repr
) = DECL_FIELD_OFFSET (field
);
1854 /* Force the representative to begin at a BITS_PER_UNIT aligned
1855 boundary - C++ may use tail-padding of a base object to
1856 continue packing bits so the bitfield region does not start
1857 at bit zero (see g++.dg/abi/bitfield5.C for example).
1858 Unallocated bits may happen for other reasons as well,
1859 for example Ada which allows explicit bit-granular structure layout. */
1860 DECL_FIELD_BIT_OFFSET (repr
)
1861 = size_binop (BIT_AND_EXPR
,
1862 DECL_FIELD_BIT_OFFSET (field
),
1863 bitsize_int (~(BITS_PER_UNIT
- 1)));
1864 SET_DECL_OFFSET_ALIGN (repr
, DECL_OFFSET_ALIGN (field
));
1865 DECL_SIZE (repr
) = DECL_SIZE (field
);
1866 DECL_SIZE_UNIT (repr
) = DECL_SIZE_UNIT (field
);
1867 DECL_PACKED (repr
) = DECL_PACKED (field
);
1868 DECL_CONTEXT (repr
) = DECL_CONTEXT (field
);
1872 /* Finish up a bitfield group that was started by creating the underlying
1873 object REPR with the last field in the bitfield group FIELD. */
1876 finish_bitfield_representative (tree repr
, tree field
)
1878 unsigned HOST_WIDE_INT bitsize
, maxbitsize
;
1882 size
= size_diffop (DECL_FIELD_OFFSET (field
),
1883 DECL_FIELD_OFFSET (repr
));
1884 while (TREE_CODE (size
) == COMPOUND_EXPR
)
1885 size
= TREE_OPERAND (size
, 1);
1886 gcc_assert (tree_fits_uhwi_p (size
));
1887 bitsize
= (tree_to_uhwi (size
) * BITS_PER_UNIT
1888 + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field
))
1889 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr
))
1890 + tree_to_uhwi (DECL_SIZE (field
)));
1892 /* Round up bitsize to multiples of BITS_PER_UNIT. */
1893 bitsize
= (bitsize
+ BITS_PER_UNIT
- 1) & ~(BITS_PER_UNIT
- 1);
1895 /* Now nothing tells us how to pad out bitsize ... */
1896 nextf
= DECL_CHAIN (field
);
1897 while (nextf
&& TREE_CODE (nextf
) != FIELD_DECL
)
1898 nextf
= DECL_CHAIN (nextf
);
1902 /* If there was an error, the field may be not laid out
1903 correctly. Don't bother to do anything. */
1904 if (TREE_TYPE (nextf
) == error_mark_node
)
1906 maxsize
= size_diffop (DECL_FIELD_OFFSET (nextf
),
1907 DECL_FIELD_OFFSET (repr
));
1908 if (tree_fits_uhwi_p (maxsize
))
1910 maxbitsize
= (tree_to_uhwi (maxsize
) * BITS_PER_UNIT
1911 + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (nextf
))
1912 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr
)));
1913 /* If the group ends within a bitfield nextf does not need to be
1914 aligned to BITS_PER_UNIT. Thus round up. */
1915 maxbitsize
= (maxbitsize
+ BITS_PER_UNIT
- 1) & ~(BITS_PER_UNIT
- 1);
1918 maxbitsize
= bitsize
;
1922 /* ??? If you consider that tail-padding of this struct might be
1923 re-used when deriving from it we cannot really do the following
1924 and thus need to set maxsize to bitsize? Also we cannot
1925 generally rely on maxsize to fold to an integer constant, so
1926 use bitsize as fallback for this case. */
1927 tree maxsize
= size_diffop (TYPE_SIZE_UNIT (DECL_CONTEXT (field
)),
1928 DECL_FIELD_OFFSET (repr
));
1929 if (tree_fits_uhwi_p (maxsize
))
1930 maxbitsize
= (tree_to_uhwi (maxsize
) * BITS_PER_UNIT
1931 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr
)));
1933 maxbitsize
= bitsize
;
1936 /* Only if we don't artificially break up the representative in
1937 the middle of a large bitfield with different possibly
1938 overlapping representatives. And all representatives start
1940 gcc_assert (maxbitsize
% BITS_PER_UNIT
== 0);
1942 /* Find the smallest nice mode to use. */
1943 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
); mode
!= VOIDmode
;
1944 mode
= GET_MODE_WIDER_MODE (mode
))
1945 if (GET_MODE_BITSIZE (mode
) >= bitsize
)
1947 if (mode
!= VOIDmode
1948 && (GET_MODE_BITSIZE (mode
) > maxbitsize
1949 || GET_MODE_BITSIZE (mode
) > MAX_FIXED_MODE_SIZE
))
1952 if (mode
== VOIDmode
)
1954 /* We really want a BLKmode representative only as a last resort,
1955 considering the member b in
1956 struct { int a : 7; int b : 17; int c; } __attribute__((packed));
1957 Otherwise we simply want to split the representative up
1958 allowing for overlaps within the bitfield region as required for
1959 struct { int a : 7; int b : 7;
1960 int c : 10; int d; } __attribute__((packed));
1961 [0, 15] HImode for a and b, [8, 23] HImode for c. */
1962 DECL_SIZE (repr
) = bitsize_int (bitsize
);
1963 DECL_SIZE_UNIT (repr
) = size_int (bitsize
/ BITS_PER_UNIT
);
1964 DECL_MODE (repr
) = BLKmode
;
1965 TREE_TYPE (repr
) = build_array_type_nelts (unsigned_char_type_node
,
1966 bitsize
/ BITS_PER_UNIT
);
1970 unsigned HOST_WIDE_INT modesize
= GET_MODE_BITSIZE (mode
);
1971 DECL_SIZE (repr
) = bitsize_int (modesize
);
1972 DECL_SIZE_UNIT (repr
) = size_int (modesize
/ BITS_PER_UNIT
);
1973 DECL_MODE (repr
) = mode
;
1974 TREE_TYPE (repr
) = lang_hooks
.types
.type_for_mode (mode
, 1);
1977 /* Remember whether the bitfield group is at the end of the
1978 structure or not. */
1979 DECL_CHAIN (repr
) = nextf
;
1982 /* Compute and set FIELD_DECLs for the underlying objects we should
1983 use for bitfield access for the structure T. */
1986 finish_bitfield_layout (tree t
)
1989 tree repr
= NULL_TREE
;
1991 /* Unions would be special, for the ease of type-punning optimizations
1992 we could use the underlying type as hint for the representative
1993 if the bitfield would fit and the representative would not exceed
1994 the union in size. */
1995 if (TREE_CODE (t
) != RECORD_TYPE
)
1998 for (prev
= NULL_TREE
, field
= TYPE_FIELDS (t
);
1999 field
; field
= DECL_CHAIN (field
))
2001 if (TREE_CODE (field
) != FIELD_DECL
)
2004 /* In the C++ memory model, consecutive bit fields in a structure are
2005 considered one memory location and updating a memory location
2006 may not store into adjacent memory locations. */
2008 && DECL_BIT_FIELD_TYPE (field
))
2010 /* Start new representative. */
2011 repr
= start_bitfield_representative (field
);
2014 && ! DECL_BIT_FIELD_TYPE (field
))
2016 /* Finish off new representative. */
2017 finish_bitfield_representative (repr
, prev
);
2020 else if (DECL_BIT_FIELD_TYPE (field
))
2022 gcc_assert (repr
!= NULL_TREE
);
2024 /* Zero-size bitfields finish off a representative and
2025 do not have a representative themselves. This is
2026 required by the C++ memory model. */
2027 if (integer_zerop (DECL_SIZE (field
)))
2029 finish_bitfield_representative (repr
, prev
);
2033 /* We assume that either DECL_FIELD_OFFSET of the representative
2034 and each bitfield member is a constant or they are equal.
2035 This is because we need to be able to compute the bit-offset
2036 of each field relative to the representative in get_bit_range
2037 during RTL expansion.
2038 If these constraints are not met, simply force a new
2039 representative to be generated. That will at most
2040 generate worse code but still maintain correctness with
2041 respect to the C++ memory model. */
2042 else if (!((tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr
))
2043 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (field
)))
2044 || operand_equal_p (DECL_FIELD_OFFSET (repr
),
2045 DECL_FIELD_OFFSET (field
), 0)))
2047 finish_bitfield_representative (repr
, prev
);
2048 repr
= start_bitfield_representative (field
);
2055 DECL_BIT_FIELD_REPRESENTATIVE (field
) = repr
;
2061 finish_bitfield_representative (repr
, prev
);
2064 /* Do all of the work required to layout the type indicated by RLI,
2065 once the fields have been laid out. This function will call `free'
2066 for RLI, unless FREE_P is false. Passing a value other than false
2067 for FREE_P is bad practice; this option only exists to support the
2071 finish_record_layout (record_layout_info rli
, int free_p
)
2075 /* Compute the final size. */
2076 finalize_record_size (rli
);
2078 /* Compute the TYPE_MODE for the record. */
2079 compute_record_mode (rli
->t
);
2081 /* Perform any last tweaks to the TYPE_SIZE, etc. */
2082 finalize_type_size (rli
->t
);
2084 /* Compute bitfield representatives. */
2085 finish_bitfield_layout (rli
->t
);
2087 /* Propagate TYPE_PACKED to variants. With C++ templates,
2088 handle_packed_attribute is too early to do this. */
2089 for (variant
= TYPE_NEXT_VARIANT (rli
->t
); variant
;
2090 variant
= TYPE_NEXT_VARIANT (variant
))
2091 TYPE_PACKED (variant
) = TYPE_PACKED (rli
->t
);
2093 /* Lay out any static members. This is done now because their type
2094 may use the record's type. */
2095 while (!vec_safe_is_empty (rli
->pending_statics
))
2096 layout_decl (rli
->pending_statics
->pop (), 0);
2101 vec_free (rli
->pending_statics
);
2107 /* Finish processing a builtin RECORD_TYPE type TYPE. It's name is
2108 NAME, its fields are chained in reverse on FIELDS.
2110 If ALIGN_TYPE is non-null, it is given the same alignment as
2114 finish_builtin_struct (tree type
, const char *name
, tree fields
,
2119 for (tail
= NULL_TREE
; fields
; tail
= fields
, fields
= next
)
2121 DECL_FIELD_CONTEXT (fields
) = type
;
2122 next
= DECL_CHAIN (fields
);
2123 DECL_CHAIN (fields
) = tail
;
2125 TYPE_FIELDS (type
) = tail
;
2129 TYPE_ALIGN (type
) = TYPE_ALIGN (align_type
);
2130 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (align_type
);
2134 #if 0 /* not yet, should get fixed properly later */
2135 TYPE_NAME (type
) = make_type_decl (get_identifier (name
), type
);
2137 TYPE_NAME (type
) = build_decl (BUILTINS_LOCATION
,
2138 TYPE_DECL
, get_identifier (name
), type
);
2140 TYPE_STUB_DECL (type
) = TYPE_NAME (type
);
2141 layout_decl (TYPE_NAME (type
), 0);
2144 /* Calculate the mode, size, and alignment for TYPE.
2145 For an array type, calculate the element separation as well.
2146 Record TYPE on the chain of permanent or temporary types
2147 so that dbxout will find out about it.
2149 TYPE_SIZE of a type is nonzero if the type has been laid out already.
2150 layout_type does nothing on such a type.
2152 If the type is incomplete, its TYPE_SIZE remains zero. */
2155 layout_type (tree type
)
2159 if (type
== error_mark_node
)
2162 /* Do nothing if type has been laid out before. */
2163 if (TYPE_SIZE (type
))
2166 switch (TREE_CODE (type
))
2169 /* This kind of type is the responsibility
2170 of the language-specific code. */
2176 SET_TYPE_MODE (type
,
2177 smallest_mode_for_size (TYPE_PRECISION (type
), MODE_INT
));
2178 TYPE_SIZE (type
) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
2179 /* Don't set TYPE_PRECISION here, as it may be set by a bitfield. */
2180 TYPE_SIZE_UNIT (type
) = size_int (GET_MODE_SIZE (TYPE_MODE (type
)));
2184 SET_TYPE_MODE (type
,
2185 mode_for_size (TYPE_PRECISION (type
), MODE_FLOAT
, 0));
2186 TYPE_SIZE (type
) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
2187 TYPE_SIZE_UNIT (type
) = size_int (GET_MODE_SIZE (TYPE_MODE (type
)));
2190 case FIXED_POINT_TYPE
:
2191 /* TYPE_MODE (type) has been set already. */
2192 TYPE_SIZE (type
) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
2193 TYPE_SIZE_UNIT (type
) = size_int (GET_MODE_SIZE (TYPE_MODE (type
)));
2197 TYPE_UNSIGNED (type
) = TYPE_UNSIGNED (TREE_TYPE (type
));
2198 SET_TYPE_MODE (type
,
2199 mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type
)),
2200 (TREE_CODE (TREE_TYPE (type
)) == REAL_TYPE
2201 ? MODE_COMPLEX_FLOAT
: MODE_COMPLEX_INT
),
2203 TYPE_SIZE (type
) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
2204 TYPE_SIZE_UNIT (type
) = size_int (GET_MODE_SIZE (TYPE_MODE (type
)));
2209 int nunits
= TYPE_VECTOR_SUBPARTS (type
);
2210 tree innertype
= TREE_TYPE (type
);
2212 gcc_assert (!(nunits
& (nunits
- 1)));
2214 /* Find an appropriate mode for the vector type. */
2215 if (TYPE_MODE (type
) == VOIDmode
)
2216 SET_TYPE_MODE (type
,
2217 mode_for_vector (TYPE_MODE (innertype
), nunits
));
2219 TYPE_SATURATING (type
) = TYPE_SATURATING (TREE_TYPE (type
));
2220 TYPE_UNSIGNED (type
) = TYPE_UNSIGNED (TREE_TYPE (type
));
2221 TYPE_SIZE_UNIT (type
) = int_const_binop (MULT_EXPR
,
2222 TYPE_SIZE_UNIT (innertype
),
2224 TYPE_SIZE (type
) = int_const_binop (MULT_EXPR
, TYPE_SIZE (innertype
),
2225 bitsize_int (nunits
));
2227 /* For vector types, we do not default to the mode's alignment.
2228 Instead, query a target hook, defaulting to natural alignment.
2229 This prevents ABI changes depending on whether or not native
2230 vector modes are supported. */
2231 TYPE_ALIGN (type
) = targetm
.vector_alignment (type
);
2233 /* However, if the underlying mode requires a bigger alignment than
2234 what the target hook provides, we cannot use the mode. For now,
2235 simply reject that case. */
2236 gcc_assert (TYPE_ALIGN (type
)
2237 >= GET_MODE_ALIGNMENT (TYPE_MODE (type
)));
2242 /* This is an incomplete type and so doesn't have a size. */
2243 TYPE_ALIGN (type
) = 1;
2244 TYPE_USER_ALIGN (type
) = 0;
2245 SET_TYPE_MODE (type
, VOIDmode
);
2248 case POINTER_BOUNDS_TYPE
:
2249 TYPE_SIZE (type
) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
2250 TYPE_SIZE_UNIT (type
) = size_int (GET_MODE_SIZE (TYPE_MODE (type
)));
2254 TYPE_SIZE (type
) = bitsize_int (POINTER_SIZE
);
2255 TYPE_SIZE_UNIT (type
) = size_int (POINTER_SIZE_UNITS
);
2256 /* A pointer might be MODE_PARTIAL_INT, but ptrdiff_t must be
2257 integral, which may be an __intN. */
2258 SET_TYPE_MODE (type
, mode_for_size (POINTER_SIZE
, MODE_INT
, 0));
2259 TYPE_PRECISION (type
) = POINTER_SIZE
;
2264 /* It's hard to see what the mode and size of a function ought to
2265 be, but we do know the alignment is FUNCTION_BOUNDARY, so
2266 make it consistent with that. */
2267 SET_TYPE_MODE (type
, mode_for_size (FUNCTION_BOUNDARY
, MODE_INT
, 0));
2268 TYPE_SIZE (type
) = bitsize_int (FUNCTION_BOUNDARY
);
2269 TYPE_SIZE_UNIT (type
) = size_int (FUNCTION_BOUNDARY
/ BITS_PER_UNIT
);
2273 case REFERENCE_TYPE
:
2275 machine_mode mode
= TYPE_MODE (type
);
2276 if (TREE_CODE (type
) == REFERENCE_TYPE
&& reference_types_internal
)
2278 addr_space_t as
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
2279 mode
= targetm
.addr_space
.address_mode (as
);
2282 TYPE_SIZE (type
) = bitsize_int (GET_MODE_BITSIZE (mode
));
2283 TYPE_SIZE_UNIT (type
) = size_int (GET_MODE_SIZE (mode
));
2284 TYPE_UNSIGNED (type
) = 1;
2285 TYPE_PRECISION (type
) = GET_MODE_PRECISION (mode
);
2291 tree index
= TYPE_DOMAIN (type
);
2292 tree element
= TREE_TYPE (type
);
2294 build_pointer_type (element
);
2296 /* We need to know both bounds in order to compute the size. */
2297 if (index
&& TYPE_MAX_VALUE (index
) && TYPE_MIN_VALUE (index
)
2298 && TYPE_SIZE (element
))
2300 tree ub
= TYPE_MAX_VALUE (index
);
2301 tree lb
= TYPE_MIN_VALUE (index
);
2302 tree element_size
= TYPE_SIZE (element
);
2305 /* Make sure that an array of zero-sized element is zero-sized
2306 regardless of its extent. */
2307 if (integer_zerop (element_size
))
2308 length
= size_zero_node
;
2310 /* The computation should happen in the original signedness so
2311 that (possible) negative values are handled appropriately
2312 when determining overflow. */
2315 /* ??? When it is obvious that the range is signed
2316 represent it using ssizetype. */
2317 if (TREE_CODE (lb
) == INTEGER_CST
2318 && TREE_CODE (ub
) == INTEGER_CST
2319 && TYPE_UNSIGNED (TREE_TYPE (lb
))
2320 && tree_int_cst_lt (ub
, lb
))
2322 lb
= wide_int_to_tree (ssizetype
,
2323 offset_int::from (lb
, SIGNED
));
2324 ub
= wide_int_to_tree (ssizetype
,
2325 offset_int::from (ub
, SIGNED
));
2328 = fold_convert (sizetype
,
2329 size_binop (PLUS_EXPR
,
2330 build_int_cst (TREE_TYPE (lb
), 1),
2331 size_binop (MINUS_EXPR
, ub
, lb
)));
2334 /* ??? We have no way to distinguish a null-sized array from an
2335 array spanning the whole sizetype range, so we arbitrarily
2336 decide that [0, -1] is the only valid representation. */
2337 if (integer_zerop (length
)
2338 && TREE_OVERFLOW (length
)
2339 && integer_zerop (lb
))
2340 length
= size_zero_node
;
2342 TYPE_SIZE (type
) = size_binop (MULT_EXPR
, element_size
,
2343 fold_convert (bitsizetype
,
2346 /* If we know the size of the element, calculate the total size
2347 directly, rather than do some division thing below. This
2348 optimization helps Fortran assumed-size arrays (where the
2349 size of the array is determined at runtime) substantially. */
2350 if (TYPE_SIZE_UNIT (element
))
2351 TYPE_SIZE_UNIT (type
)
2352 = size_binop (MULT_EXPR
, TYPE_SIZE_UNIT (element
), length
);
2355 /* Now round the alignment and size,
2356 using machine-dependent criteria if any. */
2358 #ifdef ROUND_TYPE_ALIGN
2360 = ROUND_TYPE_ALIGN (type
, TYPE_ALIGN (element
), BITS_PER_UNIT
);
2362 TYPE_ALIGN (type
) = MAX (TYPE_ALIGN (element
), BITS_PER_UNIT
);
2364 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (element
);
2365 SET_TYPE_MODE (type
, BLKmode
);
2366 if (TYPE_SIZE (type
) != 0
2367 && ! targetm
.member_type_forces_blk (type
, VOIDmode
)
2368 /* BLKmode elements force BLKmode aggregate;
2369 else extract/store fields may lose. */
2370 && (TYPE_MODE (TREE_TYPE (type
)) != BLKmode
2371 || TYPE_NO_FORCE_BLK (TREE_TYPE (type
))))
2373 SET_TYPE_MODE (type
, mode_for_array (TREE_TYPE (type
),
2375 if (TYPE_MODE (type
) != BLKmode
2376 && STRICT_ALIGNMENT
&& TYPE_ALIGN (type
) < BIGGEST_ALIGNMENT
2377 && TYPE_ALIGN (type
) < GET_MODE_ALIGNMENT (TYPE_MODE (type
)))
2379 TYPE_NO_FORCE_BLK (type
) = 1;
2380 SET_TYPE_MODE (type
, BLKmode
);
2383 /* When the element size is constant, check that it is at least as
2384 large as the element alignment. */
2385 if (TYPE_SIZE_UNIT (element
)
2386 && TREE_CODE (TYPE_SIZE_UNIT (element
)) == INTEGER_CST
2387 /* If TYPE_SIZE_UNIT overflowed, then it is certainly larger than
2389 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (element
))
2390 && !integer_zerop (TYPE_SIZE_UNIT (element
))
2391 && compare_tree_int (TYPE_SIZE_UNIT (element
),
2392 TYPE_ALIGN_UNIT (element
)) < 0)
2393 error ("alignment of array elements is greater than element size");
2399 case QUAL_UNION_TYPE
:
2402 record_layout_info rli
;
2404 /* Initialize the layout information. */
2405 rli
= start_record_layout (type
);
2407 /* If this is a QUAL_UNION_TYPE, we want to process the fields
2408 in the reverse order in building the COND_EXPR that denotes
2409 its size. We reverse them again later. */
2410 if (TREE_CODE (type
) == QUAL_UNION_TYPE
)
2411 TYPE_FIELDS (type
) = nreverse (TYPE_FIELDS (type
));
2413 /* Place all the fields. */
2414 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2415 place_field (rli
, field
);
2417 if (TREE_CODE (type
) == QUAL_UNION_TYPE
)
2418 TYPE_FIELDS (type
) = nreverse (TYPE_FIELDS (type
));
2420 /* Finish laying out the record. */
2421 finish_record_layout (rli
, /*free_p=*/true);
2429 /* Compute the final TYPE_SIZE, TYPE_ALIGN, etc. for TYPE. For
2430 records and unions, finish_record_layout already called this
2432 if (TREE_CODE (type
) != RECORD_TYPE
2433 && TREE_CODE (type
) != UNION_TYPE
2434 && TREE_CODE (type
) != QUAL_UNION_TYPE
)
2435 finalize_type_size (type
);
2437 /* We should never see alias sets on incomplete aggregates. And we
2438 should not call layout_type on not incomplete aggregates. */
2439 if (AGGREGATE_TYPE_P (type
))
2440 gcc_assert (!TYPE_ALIAS_SET_KNOWN_P (type
));
2443 /* Return the least alignment required for type TYPE. */
2446 min_align_of_type (tree type
)
2448 unsigned int align
= TYPE_ALIGN (type
);
2449 if (!TYPE_USER_ALIGN (type
))
2451 align
= MIN (align
, BIGGEST_ALIGNMENT
);
2452 #ifdef BIGGEST_FIELD_ALIGNMENT
2453 align
= MIN (align
, BIGGEST_FIELD_ALIGNMENT
);
2455 unsigned int field_align
= align
;
2456 #ifdef ADJUST_FIELD_ALIGN
2457 tree field
= build_decl (UNKNOWN_LOCATION
, FIELD_DECL
, NULL_TREE
, type
);
2458 field_align
= ADJUST_FIELD_ALIGN (field
, field_align
);
2461 align
= MIN (align
, field_align
);
2463 return align
/ BITS_PER_UNIT
;
2466 /* Vector types need to re-check the target flags each time we report
2467 the machine mode. We need to do this because attribute target can
2468 change the result of vector_mode_supported_p and have_regs_of_mode
2469 on a per-function basis. Thus the TYPE_MODE of a VECTOR_TYPE can
2470 change on a per-function basis. */
2471 /* ??? Possibly a better solution is to run through all the types
2472 referenced by a function and re-compute the TYPE_MODE once, rather
2473 than make the TYPE_MODE macro call a function. */
2476 vector_type_mode (const_tree t
)
2480 gcc_assert (TREE_CODE (t
) == VECTOR_TYPE
);
2482 mode
= t
->type_common
.mode
;
2483 if (VECTOR_MODE_P (mode
)
2484 && (!targetm
.vector_mode_supported_p (mode
)
2485 || !have_regs_of_mode
[mode
]))
2487 machine_mode innermode
= TREE_TYPE (t
)->type_common
.mode
;
2489 /* For integers, try mapping it to a same-sized scalar mode. */
2490 if (GET_MODE_CLASS (innermode
) == MODE_INT
)
2492 mode
= mode_for_size (TYPE_VECTOR_SUBPARTS (t
)
2493 * GET_MODE_BITSIZE (innermode
), MODE_INT
, 0);
2495 if (mode
!= VOIDmode
&& have_regs_of_mode
[mode
])
2505 /* Create and return a type for signed integers of PRECISION bits. */
2508 make_signed_type (int precision
)
2510 tree type
= make_node (INTEGER_TYPE
);
2512 TYPE_PRECISION (type
) = precision
;
2514 fixup_signed_type (type
);
2518 /* Create and return a type for unsigned integers of PRECISION bits. */
2521 make_unsigned_type (int precision
)
2523 tree type
= make_node (INTEGER_TYPE
);
2525 TYPE_PRECISION (type
) = precision
;
2527 fixup_unsigned_type (type
);
2531 /* Create and return a type for fract of PRECISION bits, UNSIGNEDP,
2535 make_fract_type (int precision
, int unsignedp
, int satp
)
2537 tree type
= make_node (FIXED_POINT_TYPE
);
2539 TYPE_PRECISION (type
) = precision
;
2542 TYPE_SATURATING (type
) = 1;
2544 /* Lay out the type: set its alignment, size, etc. */
2547 TYPE_UNSIGNED (type
) = 1;
2548 SET_TYPE_MODE (type
, mode_for_size (precision
, MODE_UFRACT
, 0));
2551 SET_TYPE_MODE (type
, mode_for_size (precision
, MODE_FRACT
, 0));
2557 /* Create and return a type for accum of PRECISION bits, UNSIGNEDP,
2561 make_accum_type (int precision
, int unsignedp
, int satp
)
2563 tree type
= make_node (FIXED_POINT_TYPE
);
2565 TYPE_PRECISION (type
) = precision
;
2568 TYPE_SATURATING (type
) = 1;
2570 /* Lay out the type: set its alignment, size, etc. */
2573 TYPE_UNSIGNED (type
) = 1;
2574 SET_TYPE_MODE (type
, mode_for_size (precision
, MODE_UACCUM
, 0));
2577 SET_TYPE_MODE (type
, mode_for_size (precision
, MODE_ACCUM
, 0));
2583 /* Initialize sizetypes so layout_type can use them. */
2586 initialize_sizetypes (void)
2588 int precision
, bprecision
;
2590 /* Get sizetypes precision from the SIZE_TYPE target macro. */
2591 if (strcmp (SIZETYPE
, "unsigned int") == 0)
2592 precision
= INT_TYPE_SIZE
;
2593 else if (strcmp (SIZETYPE
, "long unsigned int") == 0)
2594 precision
= LONG_TYPE_SIZE
;
2595 else if (strcmp (SIZETYPE
, "long long unsigned int") == 0)
2596 precision
= LONG_LONG_TYPE_SIZE
;
2597 else if (strcmp (SIZETYPE
, "short unsigned int") == 0)
2598 precision
= SHORT_TYPE_SIZE
;
2604 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
2605 if (int_n_enabled_p
[i
])
2608 sprintf (name
, "__int%d unsigned", int_n_data
[i
].bitsize
);
2610 if (strcmp (name
, SIZETYPE
) == 0)
2612 precision
= int_n_data
[i
].bitsize
;
2615 if (precision
== -1)
2620 = MIN (precision
+ BITS_PER_UNIT_LOG
+ 1, MAX_FIXED_MODE_SIZE
);
2622 = GET_MODE_PRECISION (smallest_mode_for_size (bprecision
, MODE_INT
));
2623 if (bprecision
> HOST_BITS_PER_DOUBLE_INT
)
2624 bprecision
= HOST_BITS_PER_DOUBLE_INT
;
2626 /* Create stubs for sizetype and bitsizetype so we can create constants. */
2627 sizetype
= make_node (INTEGER_TYPE
);
2628 TYPE_NAME (sizetype
) = get_identifier ("sizetype");
2629 TYPE_PRECISION (sizetype
) = precision
;
2630 TYPE_UNSIGNED (sizetype
) = 1;
2631 bitsizetype
= make_node (INTEGER_TYPE
);
2632 TYPE_NAME (bitsizetype
) = get_identifier ("bitsizetype");
2633 TYPE_PRECISION (bitsizetype
) = bprecision
;
2634 TYPE_UNSIGNED (bitsizetype
) = 1;
2636 /* Now layout both types manually. */
2637 SET_TYPE_MODE (sizetype
, smallest_mode_for_size (precision
, MODE_INT
));
2638 TYPE_ALIGN (sizetype
) = GET_MODE_ALIGNMENT (TYPE_MODE (sizetype
));
2639 TYPE_SIZE (sizetype
) = bitsize_int (precision
);
2640 TYPE_SIZE_UNIT (sizetype
) = size_int (GET_MODE_SIZE (TYPE_MODE (sizetype
)));
2641 set_min_and_max_values_for_integral_type (sizetype
, precision
, UNSIGNED
);
2643 SET_TYPE_MODE (bitsizetype
, smallest_mode_for_size (bprecision
, MODE_INT
));
2644 TYPE_ALIGN (bitsizetype
) = GET_MODE_ALIGNMENT (TYPE_MODE (bitsizetype
));
2645 TYPE_SIZE (bitsizetype
) = bitsize_int (bprecision
);
2646 TYPE_SIZE_UNIT (bitsizetype
)
2647 = size_int (GET_MODE_SIZE (TYPE_MODE (bitsizetype
)));
2648 set_min_and_max_values_for_integral_type (bitsizetype
, bprecision
, UNSIGNED
);
2650 /* Create the signed variants of *sizetype. */
2651 ssizetype
= make_signed_type (TYPE_PRECISION (sizetype
));
2652 TYPE_NAME (ssizetype
) = get_identifier ("ssizetype");
2653 sbitsizetype
= make_signed_type (TYPE_PRECISION (bitsizetype
));
2654 TYPE_NAME (sbitsizetype
) = get_identifier ("sbitsizetype");
2657 /* TYPE is an integral type, i.e., an INTEGRAL_TYPE, ENUMERAL_TYPE
2658 or BOOLEAN_TYPE. Set TYPE_MIN_VALUE and TYPE_MAX_VALUE
2659 for TYPE, based on the PRECISION and whether or not the TYPE
2660 IS_UNSIGNED. PRECISION need not correspond to a width supported
2661 natively by the hardware; for example, on a machine with 8-bit,
2662 16-bit, and 32-bit register modes, PRECISION might be 7, 23, or
2666 set_min_and_max_values_for_integral_type (tree type
,
2670 /* For bitfields with zero width we end up creating integer types
2671 with zero precision. Don't assign any minimum/maximum values
2672 to those types, they don't have any valid value. */
2676 TYPE_MIN_VALUE (type
)
2677 = wide_int_to_tree (type
, wi::min_value (precision
, sgn
));
2678 TYPE_MAX_VALUE (type
)
2679 = wide_int_to_tree (type
, wi::max_value (precision
, sgn
));
2682 /* Set the extreme values of TYPE based on its precision in bits,
2683 then lay it out. Used when make_signed_type won't do
2684 because the tree code is not INTEGER_TYPE.
2685 E.g. for Pascal, when the -fsigned-char option is given. */
2688 fixup_signed_type (tree type
)
2690 int precision
= TYPE_PRECISION (type
);
2692 set_min_and_max_values_for_integral_type (type
, precision
, SIGNED
);
2694 /* Lay out the type: set its alignment, size, etc. */
2698 /* Set the extreme values of TYPE based on its precision in bits,
2699 then lay it out. This is used both in `make_unsigned_type'
2700 and for enumeral types. */
2703 fixup_unsigned_type (tree type
)
2705 int precision
= TYPE_PRECISION (type
);
2707 TYPE_UNSIGNED (type
) = 1;
2709 set_min_and_max_values_for_integral_type (type
, precision
, UNSIGNED
);
2711 /* Lay out the type: set its alignment, size, etc. */
2715 /* Construct an iterator for a bitfield that spans BITSIZE bits,
2718 BITREGION_START is the bit position of the first bit in this
2719 sequence of bit fields. BITREGION_END is the last bit in this
2720 sequence. If these two fields are non-zero, we should restrict the
2721 memory access to that range. Otherwise, we are allowed to touch
2722 any adjacent non bit-fields.
2724 ALIGN is the alignment of the underlying object in bits.
2725 VOLATILEP says whether the bitfield is volatile. */
2727 bit_field_mode_iterator
2728 ::bit_field_mode_iterator (HOST_WIDE_INT bitsize
, HOST_WIDE_INT bitpos
,
2729 HOST_WIDE_INT bitregion_start
,
2730 HOST_WIDE_INT bitregion_end
,
2731 unsigned int align
, bool volatilep
)
2732 : m_mode (GET_CLASS_NARROWEST_MODE (MODE_INT
)), m_bitsize (bitsize
),
2733 m_bitpos (bitpos
), m_bitregion_start (bitregion_start
),
2734 m_bitregion_end (bitregion_end
), m_align (align
),
2735 m_volatilep (volatilep
), m_count (0)
2737 if (!m_bitregion_end
)
2739 /* We can assume that any aligned chunk of ALIGN bits that overlaps
2740 the bitfield is mapped and won't trap, provided that ALIGN isn't
2741 too large. The cap is the biggest required alignment for data,
2742 or at least the word size. And force one such chunk at least. */
2743 unsigned HOST_WIDE_INT units
2744 = MIN (align
, MAX (BIGGEST_ALIGNMENT
, BITS_PER_WORD
));
2747 m_bitregion_end
= bitpos
+ bitsize
+ units
- 1;
2748 m_bitregion_end
-= m_bitregion_end
% units
+ 1;
2752 /* Calls to this function return successively larger modes that can be used
2753 to represent the bitfield. Return true if another bitfield mode is
2754 available, storing it in *OUT_MODE if so. */
2757 bit_field_mode_iterator::next_mode (machine_mode
*out_mode
)
2759 for (; m_mode
!= VOIDmode
; m_mode
= GET_MODE_WIDER_MODE (m_mode
))
2761 unsigned int unit
= GET_MODE_BITSIZE (m_mode
);
2763 /* Skip modes that don't have full precision. */
2764 if (unit
!= GET_MODE_PRECISION (m_mode
))
2767 /* Stop if the mode is too wide to handle efficiently. */
2768 if (unit
> MAX_FIXED_MODE_SIZE
)
2771 /* Don't deliver more than one multiword mode; the smallest one
2773 if (m_count
> 0 && unit
> BITS_PER_WORD
)
2776 /* Skip modes that are too small. */
2777 unsigned HOST_WIDE_INT substart
= (unsigned HOST_WIDE_INT
) m_bitpos
% unit
;
2778 unsigned HOST_WIDE_INT subend
= substart
+ m_bitsize
;
2782 /* Stop if the mode goes outside the bitregion. */
2783 HOST_WIDE_INT start
= m_bitpos
- substart
;
2784 if (m_bitregion_start
&& start
< m_bitregion_start
)
2786 HOST_WIDE_INT end
= start
+ unit
;
2787 if (end
> m_bitregion_end
+ 1)
2790 /* Stop if the mode requires too much alignment. */
2791 if (GET_MODE_ALIGNMENT (m_mode
) > m_align
2792 && SLOW_UNALIGNED_ACCESS (m_mode
, m_align
))
2796 m_mode
= GET_MODE_WIDER_MODE (m_mode
);
2803 /* Return true if smaller modes are generally preferred for this kind
2807 bit_field_mode_iterator::prefer_smaller_modes ()
2810 ? targetm
.narrow_volatile_bitfield ()
2811 : !SLOW_BYTE_ACCESS
);
2814 /* Find the best machine mode to use when referencing a bit field of length
2815 BITSIZE bits starting at BITPOS.
2817 BITREGION_START is the bit position of the first bit in this
2818 sequence of bit fields. BITREGION_END is the last bit in this
2819 sequence. If these two fields are non-zero, we should restrict the
2820 memory access to that range. Otherwise, we are allowed to touch
2821 any adjacent non bit-fields.
2823 The underlying object is known to be aligned to a boundary of ALIGN bits.
2824 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
2825 larger than LARGEST_MODE (usually SImode).
2827 If no mode meets all these conditions, we return VOIDmode.
2829 If VOLATILEP is false and SLOW_BYTE_ACCESS is false, we return the
2830 smallest mode meeting these conditions.
2832 If VOLATILEP is false and SLOW_BYTE_ACCESS is true, we return the
2833 largest mode (but a mode no wider than UNITS_PER_WORD) that meets
2836 If VOLATILEP is true the narrow_volatile_bitfields target hook is used to
2837 decide which of the above modes should be used. */
2840 get_best_mode (int bitsize
, int bitpos
,
2841 unsigned HOST_WIDE_INT bitregion_start
,
2842 unsigned HOST_WIDE_INT bitregion_end
,
2844 machine_mode largest_mode
, bool volatilep
)
2846 bit_field_mode_iterator
iter (bitsize
, bitpos
, bitregion_start
,
2847 bitregion_end
, align
, volatilep
);
2848 machine_mode widest_mode
= VOIDmode
;
2850 while (iter
.next_mode (&mode
)
2851 /* ??? For historical reasons, reject modes that would normally
2852 receive greater alignment, even if unaligned accesses are
2853 acceptable. This has both advantages and disadvantages.
2854 Removing this check means that something like:
2856 struct s { unsigned int x; unsigned int y; };
2857 int f (struct s *s) { return s->x == 0 && s->y == 0; }
2859 can be implemented using a single load and compare on
2860 64-bit machines that have no alignment restrictions.
2861 For example, on powerpc64-linux-gnu, we would generate:
2883 However, accessing more than one field can make life harder
2884 for the gimple optimizers. For example, gcc.dg/vect/bb-slp-5.c
2885 has a series of unsigned short copies followed by a series of
2886 unsigned short comparisons. With this check, both the copies
2887 and comparisons remain 16-bit accesses and FRE is able
2888 to eliminate the latter. Without the check, the comparisons
2889 can be done using 2 64-bit operations, which FRE isn't able
2890 to handle in the same way.
2892 Either way, it would probably be worth disabling this check
2893 during expand. One particular example where removing the
2894 check would help is the get_best_mode call in store_bit_field.
2895 If we are given a memory bitregion of 128 bits that is aligned
2896 to a 64-bit boundary, and the bitfield we want to modify is
2897 in the second half of the bitregion, this check causes
2898 store_bitfield to turn the memory into a 64-bit reference
2899 to the _first_ half of the region. We later use
2900 adjust_bitfield_address to get a reference to the correct half,
2901 but doing so looks to adjust_bitfield_address as though we are
2902 moving past the end of the original object, so it drops the
2903 associated MEM_EXPR and MEM_OFFSET. Removing the check
2904 causes store_bit_field to keep a 128-bit memory reference,
2905 so that the final bitfield reference still has a MEM_EXPR
2907 && GET_MODE_ALIGNMENT (mode
) <= align
2908 && (largest_mode
== VOIDmode
2909 || GET_MODE_SIZE (mode
) <= GET_MODE_SIZE (largest_mode
)))
2912 if (iter
.prefer_smaller_modes ())
2918 /* Gets minimal and maximal values for MODE (signed or unsigned depending on
2919 SIGN). The returned constants are made to be usable in TARGET_MODE. */
2922 get_mode_bounds (machine_mode mode
, int sign
,
2923 machine_mode target_mode
,
2924 rtx
*mmin
, rtx
*mmax
)
2926 unsigned size
= GET_MODE_PRECISION (mode
);
2927 unsigned HOST_WIDE_INT min_val
, max_val
;
2929 gcc_assert (size
<= HOST_BITS_PER_WIDE_INT
);
2931 /* Special case BImode, which has values 0 and STORE_FLAG_VALUE. */
2934 if (STORE_FLAG_VALUE
< 0)
2936 min_val
= STORE_FLAG_VALUE
;
2942 max_val
= STORE_FLAG_VALUE
;
2947 min_val
= -((unsigned HOST_WIDE_INT
) 1 << (size
- 1));
2948 max_val
= ((unsigned HOST_WIDE_INT
) 1 << (size
- 1)) - 1;
2953 max_val
= ((unsigned HOST_WIDE_INT
) 1 << (size
- 1) << 1) - 1;
2956 *mmin
= gen_int_mode (min_val
, target_mode
);
2957 *mmax
= gen_int_mode (max_val
, target_mode
);
2960 #include "gt-stor-layout.h"