Merge trunk version 195164 into gupc branch.
[official-gcc.git] / gcc / stor-layout.c
blobb558f5f1f24f394157443d794dc9a2ea001be591
1 /* C-compiler utilities for types and variables storage layout
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "flags.h"
29 #include "function.h"
30 #include "expr.h"
31 #include "diagnostic-core.h"
32 #include "ggc.h"
33 #include "target.h"
34 #include "langhooks.h"
35 #include "regs.h"
36 #include "params.h"
37 #include "cgraph.h"
38 #include "tree-inline.h"
39 #include "tree-dump.h"
40 #include "gimple.h"
42 /* Data type for the expressions representing sizes of data types.
43 It is the first integer type laid out. */
44 tree sizetype_tab[(int) stk_type_kind_last];
46 /* If nonzero, this is an upper limit on alignment of structure fields.
47 The value is measured in bits. */
48 unsigned int maximum_field_alignment = TARGET_DEFAULT_PACK_STRUCT * BITS_PER_UNIT;
50 /* Nonzero if all REFERENCE_TYPEs are internal and hence should be allocated
51 in the address spaces' address_mode, not pointer_mode. Set only by
52 internal_reference_types called only by a front end. */
53 static int reference_types_internal = 0;
55 static tree self_referential_size (tree);
56 static void finalize_record_size (record_layout_info);
57 static void finalize_type_size (tree);
58 static void place_union_field (record_layout_info, tree);
59 #if defined (PCC_BITFIELD_TYPE_MATTERS) || defined (BITFIELD_NBYTES_LIMITED)
60 static int excess_unit_span (HOST_WIDE_INT, HOST_WIDE_INT, HOST_WIDE_INT,
61 HOST_WIDE_INT, tree);
62 #endif
63 extern void debug_rli (record_layout_info);
65 /* Show that REFERENCE_TYPES are internal and should use address_mode.
66 Called only by front end. */
68 void
69 internal_reference_types (void)
71 reference_types_internal = 1;
74 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
75 to serve as the actual size-expression for a type or decl. */
77 tree
78 variable_size (tree size)
80 /* Obviously. */
81 if (TREE_CONSTANT (size))
82 return size;
84 /* If the size is self-referential, we can't make a SAVE_EXPR (see
85 save_expr for the rationale). But we can do something else. */
86 if (CONTAINS_PLACEHOLDER_P (size))
87 return self_referential_size (size);
89 /* If we are in the global binding level, we can't make a SAVE_EXPR
90 since it may end up being shared across functions, so it is up
91 to the front-end to deal with this case. */
92 if (lang_hooks.decls.global_bindings_p ())
93 return size;
95 return save_expr (size);
98 /* An array of functions used for self-referential size computation. */
99 static GTY(()) vec<tree, va_gc> *size_functions;
101 /* Look inside EXPR into simple arithmetic operations involving constants.
102 Return the outermost non-arithmetic or non-constant node. */
104 static tree
105 skip_simple_constant_arithmetic (tree expr)
107 while (true)
109 if (UNARY_CLASS_P (expr))
110 expr = TREE_OPERAND (expr, 0);
111 else if (BINARY_CLASS_P (expr))
113 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
114 expr = TREE_OPERAND (expr, 0);
115 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
116 expr = TREE_OPERAND (expr, 1);
117 else
118 break;
120 else
121 break;
124 return expr;
127 /* Similar to copy_tree_r but do not copy component references involving
128 PLACEHOLDER_EXPRs. These nodes are spotted in find_placeholder_in_expr
129 and substituted in substitute_in_expr. */
131 static tree
132 copy_self_referential_tree_r (tree *tp, int *walk_subtrees, void *data)
134 enum tree_code code = TREE_CODE (*tp);
136 /* Stop at types, decls, constants like copy_tree_r. */
137 if (TREE_CODE_CLASS (code) == tcc_type
138 || TREE_CODE_CLASS (code) == tcc_declaration
139 || TREE_CODE_CLASS (code) == tcc_constant)
141 *walk_subtrees = 0;
142 return NULL_TREE;
145 /* This is the pattern built in ada/make_aligning_type. */
146 else if (code == ADDR_EXPR
147 && TREE_CODE (TREE_OPERAND (*tp, 0)) == PLACEHOLDER_EXPR)
149 *walk_subtrees = 0;
150 return NULL_TREE;
153 /* Default case: the component reference. */
154 else if (code == COMPONENT_REF)
156 tree inner;
157 for (inner = TREE_OPERAND (*tp, 0);
158 REFERENCE_CLASS_P (inner);
159 inner = TREE_OPERAND (inner, 0))
162 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
164 *walk_subtrees = 0;
165 return NULL_TREE;
169 /* We're not supposed to have them in self-referential size trees
170 because we wouldn't properly control when they are evaluated.
171 However, not creating superfluous SAVE_EXPRs requires accurate
172 tracking of readonly-ness all the way down to here, which we
173 cannot always guarantee in practice. So punt in this case. */
174 else if (code == SAVE_EXPR)
175 return error_mark_node;
177 else if (code == STATEMENT_LIST)
178 gcc_unreachable ();
180 return copy_tree_r (tp, walk_subtrees, data);
183 /* Given a SIZE expression that is self-referential, return an equivalent
184 expression to serve as the actual size expression for a type. */
186 static tree
187 self_referential_size (tree size)
189 static unsigned HOST_WIDE_INT fnno = 0;
190 vec<tree> self_refs = vNULL;
191 tree param_type_list = NULL, param_decl_list = NULL;
192 tree t, ref, return_type, fntype, fnname, fndecl;
193 unsigned int i;
194 char buf[128];
195 vec<tree, va_gc> *args = NULL;
197 /* Do not factor out simple operations. */
198 t = skip_simple_constant_arithmetic (size);
199 if (TREE_CODE (t) == CALL_EXPR)
200 return size;
202 /* Collect the list of self-references in the expression. */
203 find_placeholder_in_expr (size, &self_refs);
204 gcc_assert (self_refs.length () > 0);
206 /* Obtain a private copy of the expression. */
207 t = size;
208 if (walk_tree (&t, copy_self_referential_tree_r, NULL, NULL) != NULL_TREE)
209 return size;
210 size = t;
212 /* Build the parameter and argument lists in parallel; also
213 substitute the former for the latter in the expression. */
214 vec_alloc (args, self_refs.length ());
215 FOR_EACH_VEC_ELT (self_refs, i, ref)
217 tree subst, param_name, param_type, param_decl;
219 if (DECL_P (ref))
221 /* We shouldn't have true variables here. */
222 gcc_assert (TREE_READONLY (ref));
223 subst = ref;
225 /* This is the pattern built in ada/make_aligning_type. */
226 else if (TREE_CODE (ref) == ADDR_EXPR)
227 subst = ref;
228 /* Default case: the component reference. */
229 else
230 subst = TREE_OPERAND (ref, 1);
232 sprintf (buf, "p%d", i);
233 param_name = get_identifier (buf);
234 param_type = TREE_TYPE (ref);
235 param_decl
236 = build_decl (input_location, PARM_DECL, param_name, param_type);
237 if (targetm.calls.promote_prototypes (NULL_TREE)
238 && INTEGRAL_TYPE_P (param_type)
239 && TYPE_PRECISION (param_type) < TYPE_PRECISION (integer_type_node))
240 DECL_ARG_TYPE (param_decl) = integer_type_node;
241 else
242 DECL_ARG_TYPE (param_decl) = param_type;
243 DECL_ARTIFICIAL (param_decl) = 1;
244 TREE_READONLY (param_decl) = 1;
246 size = substitute_in_expr (size, subst, param_decl);
248 param_type_list = tree_cons (NULL_TREE, param_type, param_type_list);
249 param_decl_list = chainon (param_decl, param_decl_list);
250 args->quick_push (ref);
253 self_refs.release ();
255 /* Append 'void' to indicate that the number of parameters is fixed. */
256 param_type_list = tree_cons (NULL_TREE, void_type_node, param_type_list);
258 /* The 3 lists have been created in reverse order. */
259 param_type_list = nreverse (param_type_list);
260 param_decl_list = nreverse (param_decl_list);
262 /* Build the function type. */
263 return_type = TREE_TYPE (size);
264 fntype = build_function_type (return_type, param_type_list);
266 /* Build the function declaration. */
267 sprintf (buf, "SZ"HOST_WIDE_INT_PRINT_UNSIGNED, fnno++);
268 fnname = get_file_function_name (buf);
269 fndecl = build_decl (input_location, FUNCTION_DECL, fnname, fntype);
270 for (t = param_decl_list; t; t = DECL_CHAIN (t))
271 DECL_CONTEXT (t) = fndecl;
272 DECL_ARGUMENTS (fndecl) = param_decl_list;
273 DECL_RESULT (fndecl)
274 = build_decl (input_location, RESULT_DECL, 0, return_type);
275 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
277 /* The function has been created by the compiler and we don't
278 want to emit debug info for it. */
279 DECL_ARTIFICIAL (fndecl) = 1;
280 DECL_IGNORED_P (fndecl) = 1;
282 /* It is supposed to be "const" and never throw. */
283 TREE_READONLY (fndecl) = 1;
284 TREE_NOTHROW (fndecl) = 1;
286 /* We want it to be inlined when this is deemed profitable, as
287 well as discarded if every call has been integrated. */
288 DECL_DECLARED_INLINE_P (fndecl) = 1;
290 /* It is made up of a unique return statement. */
291 DECL_INITIAL (fndecl) = make_node (BLOCK);
292 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
293 t = build2 (MODIFY_EXPR, return_type, DECL_RESULT (fndecl), size);
294 DECL_SAVED_TREE (fndecl) = build1 (RETURN_EXPR, void_type_node, t);
295 TREE_STATIC (fndecl) = 1;
297 /* Put it onto the list of size functions. */
298 vec_safe_push (size_functions, fndecl);
300 /* Replace the original expression with a call to the size function. */
301 return build_call_expr_loc_vec (UNKNOWN_LOCATION, fndecl, args);
304 /* Take, queue and compile all the size functions. It is essential that
305 the size functions be gimplified at the very end of the compilation
306 in order to guarantee transparent handling of self-referential sizes.
307 Otherwise the GENERIC inliner would not be able to inline them back
308 at each of their call sites, thus creating artificial non-constant
309 size expressions which would trigger nasty problems later on. */
311 void
312 finalize_size_functions (void)
314 unsigned int i;
315 tree fndecl;
317 for (i = 0; size_functions && size_functions->iterate (i, &fndecl); i++)
319 dump_function (TDI_original, fndecl);
320 gimplify_function_tree (fndecl);
321 dump_function (TDI_generic, fndecl);
322 cgraph_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. */
333 enum machine_mode
334 mode_for_size (unsigned int size, enum mode_class mclass, int limit)
336 enum machine_mode mode;
338 if (limit && size > MAX_FIXED_MODE_SIZE)
339 return BLKmode;
341 /* Get the first mode which has this size, in the specified class. */
342 for (mode = GET_CLASS_NARROWEST_MODE (mclass); mode != VOIDmode;
343 mode = GET_MODE_WIDER_MODE (mode))
344 if (GET_MODE_PRECISION (mode) == size)
345 return mode;
347 return BLKmode;
350 /* Similar, except passed a tree node. */
352 enum machine_mode
353 mode_for_size_tree (const_tree size, enum mode_class mclass, int limit)
355 unsigned HOST_WIDE_INT uhwi;
356 unsigned int ui;
358 if (!host_integerp (size, 1))
359 return BLKmode;
360 uhwi = tree_low_cst (size, 1);
361 ui = uhwi;
362 if (uhwi != ui)
363 return BLKmode;
364 return mode_for_size (ui, mclass, limit);
367 /* Similar, but never return BLKmode; return the narrowest mode that
368 contains at least the requested number of value bits. */
370 enum machine_mode
371 smallest_mode_for_size (unsigned int size, enum mode_class mclass)
373 enum machine_mode mode;
375 /* Get the first mode which has at least this size, in the
376 specified class. */
377 for (mode = GET_CLASS_NARROWEST_MODE (mclass); mode != VOIDmode;
378 mode = GET_MODE_WIDER_MODE (mode))
379 if (GET_MODE_PRECISION (mode) >= size)
380 return mode;
382 gcc_unreachable ();
385 /* Find an integer mode of the exact same size, or BLKmode on failure. */
387 enum machine_mode
388 int_mode_for_mode (enum machine_mode mode)
390 switch (GET_MODE_CLASS (mode))
392 case MODE_INT:
393 case MODE_PARTIAL_INT:
394 break;
396 case MODE_COMPLEX_INT:
397 case MODE_COMPLEX_FLOAT:
398 case MODE_FLOAT:
399 case MODE_DECIMAL_FLOAT:
400 case MODE_VECTOR_INT:
401 case MODE_VECTOR_FLOAT:
402 case MODE_FRACT:
403 case MODE_ACCUM:
404 case MODE_UFRACT:
405 case MODE_UACCUM:
406 case MODE_VECTOR_FRACT:
407 case MODE_VECTOR_ACCUM:
408 case MODE_VECTOR_UFRACT:
409 case MODE_VECTOR_UACCUM:
410 mode = mode_for_size (GET_MODE_BITSIZE (mode), MODE_INT, 0);
411 break;
413 case MODE_RANDOM:
414 if (mode == BLKmode)
415 break;
417 /* ... fall through ... */
419 case MODE_CC:
420 default:
421 gcc_unreachable ();
424 return mode;
427 /* Find a mode that is suitable for representing a vector with
428 NUNITS elements of mode INNERMODE. Returns BLKmode if there
429 is no suitable mode. */
431 enum machine_mode
432 mode_for_vector (enum machine_mode innermode, unsigned nunits)
434 enum machine_mode mode;
436 /* First, look for a supported vector type. */
437 if (SCALAR_FLOAT_MODE_P (innermode))
438 mode = MIN_MODE_VECTOR_FLOAT;
439 else if (SCALAR_FRACT_MODE_P (innermode))
440 mode = MIN_MODE_VECTOR_FRACT;
441 else if (SCALAR_UFRACT_MODE_P (innermode))
442 mode = MIN_MODE_VECTOR_UFRACT;
443 else if (SCALAR_ACCUM_MODE_P (innermode))
444 mode = MIN_MODE_VECTOR_ACCUM;
445 else if (SCALAR_UACCUM_MODE_P (innermode))
446 mode = MIN_MODE_VECTOR_UACCUM;
447 else
448 mode = MIN_MODE_VECTOR_INT;
450 /* Do not check vector_mode_supported_p here. We'll do that
451 later in vector_type_mode. */
452 for (; mode != VOIDmode ; mode = GET_MODE_WIDER_MODE (mode))
453 if (GET_MODE_NUNITS (mode) == nunits
454 && GET_MODE_INNER (mode) == innermode)
455 break;
457 /* For integers, try mapping it to a same-sized scalar mode. */
458 if (mode == VOIDmode
459 && GET_MODE_CLASS (innermode) == MODE_INT)
460 mode = mode_for_size (nunits * GET_MODE_BITSIZE (innermode),
461 MODE_INT, 0);
463 if (mode == VOIDmode
464 || (GET_MODE_CLASS (mode) == MODE_INT
465 && !have_regs_of_mode[mode]))
466 return BLKmode;
468 return mode;
471 /* Return the alignment of MODE. This will be bounded by 1 and
472 BIGGEST_ALIGNMENT. */
474 unsigned int
475 get_mode_alignment (enum machine_mode mode)
477 return MIN (BIGGEST_ALIGNMENT, MAX (1, mode_base_align[mode]*BITS_PER_UNIT));
480 /* Return the natural mode of an array, given that it is SIZE bytes in
481 total and has elements of type ELEM_TYPE. */
483 static enum machine_mode
484 mode_for_array (tree elem_type, tree size)
486 tree elem_size;
487 unsigned HOST_WIDE_INT int_size, int_elem_size;
488 bool limit_p;
490 /* One-element arrays get the component type's mode. */
491 elem_size = TYPE_SIZE (elem_type);
492 if (simple_cst_equal (size, elem_size))
493 return TYPE_MODE (elem_type);
495 limit_p = true;
496 if (host_integerp (size, 1) && host_integerp (elem_size, 1))
498 int_size = tree_low_cst (size, 1);
499 int_elem_size = tree_low_cst (elem_size, 1);
500 if (int_elem_size > 0
501 && int_size % int_elem_size == 0
502 && targetm.array_mode_supported_p (TYPE_MODE (elem_type),
503 int_size / int_elem_size))
504 limit_p = false;
506 return mode_for_size_tree (size, MODE_INT, limit_p);
509 /* Hook for a front-end function that tests to see if a declared
510 object's size needs to be calculated in a language defined way */
512 int (*lang_layout_decl_p)(tree, tree) = 0;
514 void
515 set_lang_layout_decl_p (int (*f)(tree, tree))
517 lang_layout_decl_p = f;
520 /* Hook for a front-end function that can size a declared
521 object, when the size is unknown at the time that
522 `layout_type' is called. */
524 void (*lang_layout_decl) (tree, tree) = 0;
526 void
527 set_lang_layout_decl (void (*f) (tree, tree))
529 lang_layout_decl = f;
532 /* Subroutine of layout_decl: Force alignment required for the data type.
533 But if the decl itself wants greater alignment, don't override that. */
535 static inline void
536 do_type_align (tree type, tree decl)
538 if (TYPE_ALIGN (type) > DECL_ALIGN (decl))
540 DECL_ALIGN (decl) = TYPE_ALIGN (type);
541 if (TREE_CODE (decl) == FIELD_DECL)
542 DECL_USER_ALIGN (decl) = TYPE_USER_ALIGN (type);
546 /* Set the size, mode and alignment of a ..._DECL node.
547 TYPE_DECL does need this for C++.
548 Note that LABEL_DECL and CONST_DECL nodes do not need this,
549 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
550 Don't call layout_decl for them.
552 KNOWN_ALIGN is the amount of alignment we can assume this
553 decl has with no special effort. It is relevant only for FIELD_DECLs
554 and depends on the previous fields.
555 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
556 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
557 the record will be aligned to suit. */
559 void
560 layout_decl (tree decl, unsigned int known_align)
562 tree type = TREE_TYPE (decl);
563 enum tree_code code = TREE_CODE (decl);
564 rtx rtl = NULL_RTX;
565 location_t loc = DECL_SOURCE_LOCATION (decl);
567 if (code == CONST_DECL)
568 return;
570 gcc_assert (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL
571 || code == TYPE_DECL ||code == FIELD_DECL);
573 rtl = DECL_RTL_IF_SET (decl);
575 if (type == error_mark_node)
576 type = void_type_node;
578 /* Usually the size and mode come from the data type without change,
579 however, the front-end may set the explicit width of the field, so its
580 size may not be the same as the size of its type. This happens with
581 bitfields, of course (an `int' bitfield may be only 2 bits, say), but it
582 also happens with other fields. For example, the C++ front-end creates
583 zero-sized fields corresponding to empty base classes, and depends on
584 layout_type setting DECL_FIELD_BITPOS correctly for the field. Set the
585 size in bytes from the size in bits. If we have already set the mode,
586 don't set it again since we can be called twice for FIELD_DECLs. */
588 DECL_UNSIGNED (decl) = TYPE_UNSIGNED (type);
589 if (DECL_MODE (decl) == VOIDmode)
590 DECL_MODE (decl) = TYPE_MODE (type);
592 if (lang_layout_decl_p && (*lang_layout_decl_p) (decl, type))
594 (*lang_layout_decl) (decl, type);
596 else if (DECL_SIZE (decl) == 0)
598 DECL_SIZE (decl) = TYPE_SIZE (type);
599 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
601 else if (DECL_SIZE_UNIT (decl) == 0)
602 DECL_SIZE_UNIT (decl)
603 = fold_convert_loc (loc, sizetype,
604 size_binop_loc (loc, CEIL_DIV_EXPR, DECL_SIZE (decl),
605 bitsize_unit_node));
607 if (code != FIELD_DECL)
608 /* For non-fields, update the alignment from the type. */
609 do_type_align (type, decl);
610 else
611 /* For fields, it's a bit more complicated... */
613 bool old_user_align = DECL_USER_ALIGN (decl);
614 bool zero_bitfield = false;
615 bool packed_p = DECL_PACKED (decl);
616 unsigned int mfa;
618 if (DECL_BIT_FIELD (decl))
620 DECL_BIT_FIELD_TYPE (decl) = type;
622 /* A zero-length bit-field affects the alignment of the next
623 field. In essence such bit-fields are not influenced by
624 any packing due to #pragma pack or attribute packed. */
625 if (integer_zerop (DECL_SIZE (decl))
626 && ! targetm.ms_bitfield_layout_p (DECL_FIELD_CONTEXT (decl)))
628 zero_bitfield = true;
629 packed_p = false;
630 #ifdef PCC_BITFIELD_TYPE_MATTERS
631 if (PCC_BITFIELD_TYPE_MATTERS)
632 do_type_align (type, decl);
633 else
634 #endif
636 #ifdef EMPTY_FIELD_BOUNDARY
637 if (EMPTY_FIELD_BOUNDARY > DECL_ALIGN (decl))
639 DECL_ALIGN (decl) = EMPTY_FIELD_BOUNDARY;
640 DECL_USER_ALIGN (decl) = 0;
642 #endif
646 /* See if we can use an ordinary integer mode for a bit-field.
647 Conditions are: a fixed size that is correct for another mode,
648 occupying a complete byte or bytes on proper boundary,
649 and not -fstrict-volatile-bitfields. If the latter is set,
650 we unfortunately can't check TREE_THIS_VOLATILE, as a cast
651 may make a volatile object later. */
652 if (TYPE_SIZE (type) != 0
653 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
654 && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT
655 && flag_strict_volatile_bitfields <= 0)
657 enum machine_mode xmode
658 = mode_for_size_tree (DECL_SIZE (decl), MODE_INT, 1);
659 unsigned int xalign = GET_MODE_ALIGNMENT (xmode);
661 if (xmode != BLKmode
662 && !(xalign > BITS_PER_UNIT && DECL_PACKED (decl))
663 && (known_align == 0 || known_align >= xalign))
665 DECL_ALIGN (decl) = MAX (xalign, DECL_ALIGN (decl));
666 DECL_MODE (decl) = xmode;
667 DECL_BIT_FIELD (decl) = 0;
671 /* Turn off DECL_BIT_FIELD if we won't need it set. */
672 if (TYPE_MODE (type) == BLKmode && DECL_MODE (decl) == BLKmode
673 && known_align >= TYPE_ALIGN (type)
674 && DECL_ALIGN (decl) >= TYPE_ALIGN (type))
675 DECL_BIT_FIELD (decl) = 0;
677 else if (packed_p && DECL_USER_ALIGN (decl))
678 /* Don't touch DECL_ALIGN. For other packed fields, go ahead and
679 round up; we'll reduce it again below. We want packing to
680 supersede USER_ALIGN inherited from the type, but defer to
681 alignment explicitly specified on the field decl. */;
682 else
683 do_type_align (type, decl);
685 /* If the field is packed and not explicitly aligned, give it the
686 minimum alignment. Note that do_type_align may set
687 DECL_USER_ALIGN, so we need to check old_user_align instead. */
688 if (packed_p
689 && !old_user_align)
690 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
692 if (! packed_p && ! DECL_USER_ALIGN (decl))
694 /* Some targets (i.e. i386, VMS) limit struct field alignment
695 to a lower boundary than alignment of variables unless
696 it was overridden by attribute aligned. */
697 #ifdef BIGGEST_FIELD_ALIGNMENT
698 DECL_ALIGN (decl)
699 = MIN (DECL_ALIGN (decl), (unsigned) BIGGEST_FIELD_ALIGNMENT);
700 #endif
701 #ifdef ADJUST_FIELD_ALIGN
702 DECL_ALIGN (decl) = ADJUST_FIELD_ALIGN (decl, DECL_ALIGN (decl));
703 #endif
706 if (zero_bitfield)
707 mfa = initial_max_fld_align * BITS_PER_UNIT;
708 else
709 mfa = maximum_field_alignment;
710 /* Should this be controlled by DECL_USER_ALIGN, too? */
711 if (mfa != 0)
712 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), mfa);
715 /* Evaluate nonconstant size only once, either now or as soon as safe. */
716 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
717 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
718 if (DECL_SIZE_UNIT (decl) != 0
719 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST)
720 DECL_SIZE_UNIT (decl) = variable_size (DECL_SIZE_UNIT (decl));
722 /* If requested, warn about definitions of large data objects. */
723 if (warn_larger_than
724 && (code == VAR_DECL || code == PARM_DECL)
725 && ! DECL_EXTERNAL (decl))
727 tree size = DECL_SIZE_UNIT (decl);
729 if (size != 0 && TREE_CODE (size) == INTEGER_CST
730 && compare_tree_int (size, larger_than_size) > 0)
732 int size_as_int = TREE_INT_CST_LOW (size);
734 if (compare_tree_int (size, size_as_int) == 0)
735 warning (OPT_Wlarger_than_, "size of %q+D is %d bytes", decl, size_as_int);
736 else
737 warning (OPT_Wlarger_than_, "size of %q+D is larger than %wd bytes",
738 decl, larger_than_size);
742 /* If the RTL was already set, update its mode and mem attributes. */
743 if (rtl)
745 PUT_MODE (rtl, DECL_MODE (decl));
746 SET_DECL_RTL (decl, 0);
747 set_mem_attributes (rtl, decl, 1);
748 SET_DECL_RTL (decl, rtl);
752 /* Given a VAR_DECL, PARM_DECL or RESULT_DECL, clears the results of
753 a previous call to layout_decl and calls it again. */
755 void
756 relayout_decl (tree decl)
758 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
759 DECL_MODE (decl) = VOIDmode;
760 if (!DECL_USER_ALIGN (decl))
761 DECL_ALIGN (decl) = 0;
762 SET_DECL_RTL (decl, 0);
764 layout_decl (decl, 0);
767 /* Begin laying out type T, which may be a RECORD_TYPE, UNION_TYPE, or
768 QUAL_UNION_TYPE. Return a pointer to a struct record_layout_info which
769 is to be passed to all other layout functions for this record. It is the
770 responsibility of the caller to call `free' for the storage returned.
771 Note that garbage collection is not permitted until we finish laying
772 out the record. */
774 record_layout_info
775 start_record_layout (tree t)
777 record_layout_info rli = XNEW (struct record_layout_info_s);
779 rli->t = t;
781 /* If the type has a minimum specified alignment (via an attribute
782 declaration, for example) use it -- otherwise, start with a
783 one-byte alignment. */
784 rli->record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (t));
785 rli->unpacked_align = rli->record_align;
786 rli->offset_align = MAX (rli->record_align, BIGGEST_ALIGNMENT);
788 #ifdef STRUCTURE_SIZE_BOUNDARY
789 /* Packed structures don't need to have minimum size. */
790 if (! TYPE_PACKED (t))
792 unsigned tmp;
794 /* #pragma pack overrides STRUCTURE_SIZE_BOUNDARY. */
795 tmp = (unsigned) STRUCTURE_SIZE_BOUNDARY;
796 if (maximum_field_alignment != 0)
797 tmp = MIN (tmp, maximum_field_alignment);
798 rli->record_align = MAX (rli->record_align, tmp);
800 #endif
802 rli->offset = size_zero_node;
803 rli->bitpos = bitsize_zero_node;
804 rli->prev_field = 0;
805 rli->pending_statics = 0;
806 rli->packed_maybe_necessary = 0;
807 rli->remaining_in_alignment = 0;
809 return rli;
812 /* Return the combined bit position for the byte offset OFFSET and the
813 bit position BITPOS.
815 These functions operate on byte and bit positions present in FIELD_DECLs
816 and assume that these expressions result in no (intermediate) overflow.
817 This assumption is necessary to fold the expressions as much as possible,
818 so as to avoid creating artificially variable-sized types in languages
819 supporting variable-sized types like Ada. */
821 tree
822 bit_from_pos (tree offset, tree bitpos)
824 if (TREE_CODE (offset) == PLUS_EXPR)
825 offset = size_binop (PLUS_EXPR,
826 fold_convert (bitsizetype, TREE_OPERAND (offset, 0)),
827 fold_convert (bitsizetype, TREE_OPERAND (offset, 1)));
828 else
829 offset = fold_convert (bitsizetype, offset);
830 return size_binop (PLUS_EXPR, bitpos,
831 size_binop (MULT_EXPR, offset, bitsize_unit_node));
834 /* Return the combined truncated byte position for the byte offset OFFSET and
835 the bit position BITPOS. */
837 tree
838 byte_from_pos (tree offset, tree bitpos)
840 tree bytepos;
841 if (TREE_CODE (bitpos) == MULT_EXPR
842 && tree_int_cst_equal (TREE_OPERAND (bitpos, 1), bitsize_unit_node))
843 bytepos = TREE_OPERAND (bitpos, 0);
844 else
845 bytepos = size_binop (TRUNC_DIV_EXPR, bitpos, bitsize_unit_node);
846 return size_binop (PLUS_EXPR, offset, fold_convert (sizetype, bytepos));
849 /* Split the bit position POS into a byte offset *POFFSET and a bit
850 position *PBITPOS with the byte offset aligned to OFF_ALIGN bits. */
852 void
853 pos_from_bit (tree *poffset, tree *pbitpos, unsigned int off_align,
854 tree pos)
856 tree toff_align = bitsize_int (off_align);
857 if (TREE_CODE (pos) == MULT_EXPR
858 && tree_int_cst_equal (TREE_OPERAND (pos, 1), toff_align))
860 *poffset = size_binop (MULT_EXPR,
861 fold_convert (sizetype, TREE_OPERAND (pos, 0)),
862 size_int (off_align / BITS_PER_UNIT));
863 *pbitpos = bitsize_zero_node;
865 else
867 *poffset = size_binop (MULT_EXPR,
868 fold_convert (sizetype,
869 size_binop (FLOOR_DIV_EXPR, pos,
870 toff_align)),
871 size_int (off_align / BITS_PER_UNIT));
872 *pbitpos = size_binop (FLOOR_MOD_EXPR, pos, toff_align);
876 /* Given a pointer to bit and byte offsets and an offset alignment,
877 normalize the offsets so they are within the alignment. */
879 void
880 normalize_offset (tree *poffset, tree *pbitpos, unsigned int off_align)
882 /* If the bit position is now larger than it should be, adjust it
883 downwards. */
884 if (compare_tree_int (*pbitpos, off_align) >= 0)
886 tree offset, bitpos;
887 pos_from_bit (&offset, &bitpos, off_align, *pbitpos);
888 *poffset = size_binop (PLUS_EXPR, *poffset, offset);
889 *pbitpos = bitpos;
893 /* Print debugging information about the information in RLI. */
895 DEBUG_FUNCTION void
896 debug_rli (record_layout_info rli)
898 print_node_brief (stderr, "type", rli->t, 0);
899 print_node_brief (stderr, "\noffset", rli->offset, 0);
900 print_node_brief (stderr, " bitpos", rli->bitpos, 0);
902 fprintf (stderr, "\naligns: rec = %u, unpack = %u, off = %u\n",
903 rli->record_align, rli->unpacked_align,
904 rli->offset_align);
906 /* The ms_struct code is the only that uses this. */
907 if (targetm.ms_bitfield_layout_p (rli->t))
908 fprintf (stderr, "remaining in alignment = %u\n", rli->remaining_in_alignment);
910 if (rli->packed_maybe_necessary)
911 fprintf (stderr, "packed may be necessary\n");
913 if (!vec_safe_is_empty (rli->pending_statics))
915 fprintf (stderr, "pending statics:\n");
916 debug_vec_tree (rli->pending_statics);
920 /* Given an RLI with a possibly-incremented BITPOS, adjust OFFSET and
921 BITPOS if necessary to keep BITPOS below OFFSET_ALIGN. */
923 void
924 normalize_rli (record_layout_info rli)
926 normalize_offset (&rli->offset, &rli->bitpos, rli->offset_align);
929 /* Returns the size in bytes allocated so far. */
931 tree
932 rli_size_unit_so_far (record_layout_info rli)
934 return byte_from_pos (rli->offset, rli->bitpos);
937 /* Returns the size in bits allocated so far. */
939 tree
940 rli_size_so_far (record_layout_info rli)
942 return bit_from_pos (rli->offset, rli->bitpos);
945 /* FIELD is about to be added to RLI->T. The alignment (in bits) of
946 the next available location within the record is given by KNOWN_ALIGN.
947 Update the variable alignment fields in RLI, and return the alignment
948 to give the FIELD. */
950 unsigned int
951 update_alignment_for_field (record_layout_info rli, tree field,
952 unsigned int known_align)
954 /* The alignment required for FIELD. */
955 unsigned int desired_align;
956 /* The type of this field. */
957 tree type = TREE_TYPE (field);
958 /* True if the field was explicitly aligned by the user. */
959 bool user_align;
960 bool is_bitfield;
962 /* Do not attempt to align an ERROR_MARK node */
963 if (TREE_CODE (type) == ERROR_MARK)
964 return 0;
966 /* Lay out the field so we know what alignment it needs. */
967 layout_decl (field, known_align);
968 desired_align = DECL_ALIGN (field);
969 user_align = DECL_USER_ALIGN (field);
971 is_bitfield = (type != error_mark_node
972 && DECL_BIT_FIELD_TYPE (field)
973 && ! integer_zerop (TYPE_SIZE (type)));
975 /* Record must have at least as much alignment as any field.
976 Otherwise, the alignment of the field within the record is
977 meaningless. */
978 if (targetm.ms_bitfield_layout_p (rli->t))
980 /* Here, the alignment of the underlying type of a bitfield can
981 affect the alignment of a record; even a zero-sized field
982 can do this. The alignment should be to the alignment of
983 the type, except that for zero-size bitfields this only
984 applies if there was an immediately prior, nonzero-size
985 bitfield. (That's the way it is, experimentally.) */
986 if ((!is_bitfield && !DECL_PACKED (field))
987 || ((DECL_SIZE (field) == NULL_TREE
988 || !integer_zerop (DECL_SIZE (field)))
989 ? !DECL_PACKED (field)
990 : (rli->prev_field
991 && DECL_BIT_FIELD_TYPE (rli->prev_field)
992 && ! integer_zerop (DECL_SIZE (rli->prev_field)))))
994 unsigned int type_align = TYPE_ALIGN (type);
995 type_align = MAX (type_align, desired_align);
996 if (maximum_field_alignment != 0)
997 type_align = MIN (type_align, maximum_field_alignment);
998 rli->record_align = MAX (rli->record_align, type_align);
999 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
1002 #ifdef PCC_BITFIELD_TYPE_MATTERS
1003 else if (is_bitfield && PCC_BITFIELD_TYPE_MATTERS)
1005 /* Named bit-fields cause the entire structure to have the
1006 alignment implied by their type. Some targets also apply the same
1007 rules to unnamed bitfields. */
1008 if (DECL_NAME (field) != 0
1009 || targetm.align_anon_bitfield ())
1011 unsigned int type_align = TYPE_ALIGN (type);
1013 #ifdef ADJUST_FIELD_ALIGN
1014 if (! TYPE_USER_ALIGN (type))
1015 type_align = ADJUST_FIELD_ALIGN (field, type_align);
1016 #endif
1018 /* Targets might chose to handle unnamed and hence possibly
1019 zero-width bitfield. Those are not influenced by #pragmas
1020 or packed attributes. */
1021 if (integer_zerop (DECL_SIZE (field)))
1023 if (initial_max_fld_align)
1024 type_align = MIN (type_align,
1025 initial_max_fld_align * BITS_PER_UNIT);
1027 else if (maximum_field_alignment != 0)
1028 type_align = MIN (type_align, maximum_field_alignment);
1029 else if (DECL_PACKED (field))
1030 type_align = MIN (type_align, BITS_PER_UNIT);
1032 /* The alignment of the record is increased to the maximum
1033 of the current alignment, the alignment indicated on the
1034 field (i.e., the alignment specified by an __aligned__
1035 attribute), and the alignment indicated by the type of
1036 the field. */
1037 rli->record_align = MAX (rli->record_align, desired_align);
1038 rli->record_align = MAX (rli->record_align, type_align);
1040 if (warn_packed)
1041 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
1042 user_align |= TYPE_USER_ALIGN (type);
1045 #endif
1046 else
1048 rli->record_align = MAX (rli->record_align, desired_align);
1049 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
1052 TYPE_USER_ALIGN (rli->t) |= user_align;
1054 return desired_align;
1057 /* Called from place_field to handle unions. */
1059 static void
1060 place_union_field (record_layout_info rli, tree field)
1062 update_alignment_for_field (rli, field, /*known_align=*/0);
1064 DECL_FIELD_OFFSET (field) = size_zero_node;
1065 DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
1066 SET_DECL_OFFSET_ALIGN (field, BIGGEST_ALIGNMENT);
1068 /* If this is an ERROR_MARK return *after* having set the
1069 field at the start of the union. This helps when parsing
1070 invalid fields. */
1071 if (TREE_CODE (TREE_TYPE (field)) == ERROR_MARK)
1072 return;
1074 /* We assume the union's size will be a multiple of a byte so we don't
1075 bother with BITPOS. */
1076 if (TREE_CODE (rli->t) == UNION_TYPE)
1077 rli->offset = size_binop (MAX_EXPR, rli->offset, DECL_SIZE_UNIT (field));
1078 else if (TREE_CODE (rli->t) == QUAL_UNION_TYPE)
1079 rli->offset = fold_build3 (COND_EXPR, sizetype, DECL_QUALIFIER (field),
1080 DECL_SIZE_UNIT (field), rli->offset);
1083 #if defined (PCC_BITFIELD_TYPE_MATTERS) || defined (BITFIELD_NBYTES_LIMITED)
1084 /* A bitfield of SIZE with a required access alignment of ALIGN is allocated
1085 at BYTE_OFFSET / BIT_OFFSET. Return nonzero if the field would span more
1086 units of alignment than the underlying TYPE. */
1087 static int
1088 excess_unit_span (HOST_WIDE_INT byte_offset, HOST_WIDE_INT bit_offset,
1089 HOST_WIDE_INT size, HOST_WIDE_INT align, tree type)
1091 /* Note that the calculation of OFFSET might overflow; we calculate it so
1092 that we still get the right result as long as ALIGN is a power of two. */
1093 unsigned HOST_WIDE_INT offset = byte_offset * BITS_PER_UNIT + bit_offset;
1095 offset = offset % align;
1096 return ((offset + size + align - 1) / align
1097 > ((unsigned HOST_WIDE_INT) tree_low_cst (TYPE_SIZE (type), 1)
1098 / align));
1100 #endif
1102 /* RLI contains information about the layout of a RECORD_TYPE. FIELD
1103 is a FIELD_DECL to be added after those fields already present in
1104 T. (FIELD is not actually added to the TYPE_FIELDS list here;
1105 callers that desire that behavior must manually perform that step.) */
1107 void
1108 place_field (record_layout_info rli, tree field)
1110 /* The alignment required for FIELD. */
1111 unsigned int desired_align;
1112 /* The alignment FIELD would have if we just dropped it into the
1113 record as it presently stands. */
1114 unsigned int known_align;
1115 unsigned int actual_align;
1116 /* The type of this field. */
1117 tree type = TREE_TYPE (field);
1119 gcc_assert (TREE_CODE (field) != ERROR_MARK);
1121 /* If FIELD is static, then treat it like a separate variable, not
1122 really like a structure field. If it is a FUNCTION_DECL, it's a
1123 method. In both cases, all we do is lay out the decl, and we do
1124 it *after* the record is laid out. */
1125 if (TREE_CODE (field) == VAR_DECL)
1127 vec_safe_push (rli->pending_statics, field);
1128 return;
1131 /* Enumerators and enum types which are local to this class need not
1132 be laid out. Likewise for initialized constant fields. */
1133 else if (TREE_CODE (field) != FIELD_DECL)
1134 return;
1136 /* Unions are laid out very differently than records, so split
1137 that code off to another function. */
1138 else if (TREE_CODE (rli->t) != RECORD_TYPE)
1140 place_union_field (rli, field);
1141 return;
1144 else if (TREE_CODE (type) == ERROR_MARK)
1146 /* Place this field at the current allocation position, so we
1147 maintain monotonicity. */
1148 DECL_FIELD_OFFSET (field) = rli->offset;
1149 DECL_FIELD_BIT_OFFSET (field) = rli->bitpos;
1150 SET_DECL_OFFSET_ALIGN (field, rli->offset_align);
1151 return;
1154 /* Work out the known alignment so far. Note that A & (-A) is the
1155 value of the least-significant bit in A that is one. */
1156 if (! integer_zerop (rli->bitpos))
1157 known_align = (tree_low_cst (rli->bitpos, 1)
1158 & - tree_low_cst (rli->bitpos, 1));
1159 else if (integer_zerop (rli->offset))
1160 known_align = 0;
1161 else if (host_integerp (rli->offset, 1))
1162 known_align = (BITS_PER_UNIT
1163 * (tree_low_cst (rli->offset, 1)
1164 & - tree_low_cst (rli->offset, 1)));
1165 else
1166 known_align = rli->offset_align;
1168 desired_align = update_alignment_for_field (rli, field, known_align);
1169 if (known_align == 0)
1170 known_align = MAX (BIGGEST_ALIGNMENT, rli->record_align);
1172 if (warn_packed && DECL_PACKED (field))
1174 if (known_align >= TYPE_ALIGN (type))
1176 if (TYPE_ALIGN (type) > desired_align)
1178 if (STRICT_ALIGNMENT)
1179 warning (OPT_Wattributes, "packed attribute causes "
1180 "inefficient alignment for %q+D", field);
1181 /* Don't warn if DECL_PACKED was set by the type. */
1182 else if (!TYPE_PACKED (rli->t))
1183 warning (OPT_Wattributes, "packed attribute is "
1184 "unnecessary for %q+D", field);
1187 else
1188 rli->packed_maybe_necessary = 1;
1191 /* Does this field automatically have alignment it needs by virtue
1192 of the fields that precede it and the record's own alignment? */
1193 if (known_align < desired_align)
1195 /* No, we need to skip space before this field.
1196 Bump the cumulative size to multiple of field alignment. */
1198 if (!targetm.ms_bitfield_layout_p (rli->t)
1199 && DECL_SOURCE_LOCATION (field) != BUILTINS_LOCATION)
1200 warning (OPT_Wpadded, "padding struct to align %q+D", field);
1202 /* If the alignment is still within offset_align, just align
1203 the bit position. */
1204 if (desired_align < rli->offset_align)
1205 rli->bitpos = round_up (rli->bitpos, desired_align);
1206 else
1208 /* First adjust OFFSET by the partial bits, then align. */
1209 rli->offset
1210 = size_binop (PLUS_EXPR, rli->offset,
1211 fold_convert (sizetype,
1212 size_binop (CEIL_DIV_EXPR, rli->bitpos,
1213 bitsize_unit_node)));
1214 rli->bitpos = bitsize_zero_node;
1216 rli->offset = round_up (rli->offset, desired_align / BITS_PER_UNIT);
1219 if (! TREE_CONSTANT (rli->offset))
1220 rli->offset_align = desired_align;
1221 if (targetm.ms_bitfield_layout_p (rli->t))
1222 rli->prev_field = NULL;
1225 /* Handle compatibility with PCC. Note that if the record has any
1226 variable-sized fields, we need not worry about compatibility. */
1227 #ifdef PCC_BITFIELD_TYPE_MATTERS
1228 if (PCC_BITFIELD_TYPE_MATTERS
1229 && ! targetm.ms_bitfield_layout_p (rli->t)
1230 && TREE_CODE (field) == FIELD_DECL
1231 && type != error_mark_node
1232 && DECL_BIT_FIELD (field)
1233 && (! DECL_PACKED (field)
1234 /* Enter for these packed fields only to issue a warning. */
1235 || TYPE_ALIGN (type) <= BITS_PER_UNIT)
1236 && maximum_field_alignment == 0
1237 && ! integer_zerop (DECL_SIZE (field))
1238 && host_integerp (DECL_SIZE (field), 1)
1239 && host_integerp (rli->offset, 1)
1240 && host_integerp (TYPE_SIZE (type), 1))
1242 unsigned int type_align = TYPE_ALIGN (type);
1243 tree dsize = DECL_SIZE (field);
1244 HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
1245 HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
1246 HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
1248 #ifdef ADJUST_FIELD_ALIGN
1249 if (! TYPE_USER_ALIGN (type))
1250 type_align = ADJUST_FIELD_ALIGN (field, type_align);
1251 #endif
1253 /* A bit field may not span more units of alignment of its type
1254 than its type itself. Advance to next boundary if necessary. */
1255 if (excess_unit_span (offset, bit_offset, field_size, type_align, type))
1257 if (DECL_PACKED (field))
1259 if (warn_packed_bitfield_compat == 1)
1260 inform
1261 (input_location,
1262 "offset of packed bit-field %qD has changed in GCC 4.4",
1263 field);
1265 else
1266 rli->bitpos = round_up (rli->bitpos, type_align);
1269 if (! DECL_PACKED (field))
1270 TYPE_USER_ALIGN (rli->t) |= TYPE_USER_ALIGN (type);
1272 #endif
1274 #ifdef BITFIELD_NBYTES_LIMITED
1275 if (BITFIELD_NBYTES_LIMITED
1276 && ! targetm.ms_bitfield_layout_p (rli->t)
1277 && TREE_CODE (field) == FIELD_DECL
1278 && type != error_mark_node
1279 && DECL_BIT_FIELD_TYPE (field)
1280 && ! DECL_PACKED (field)
1281 && ! integer_zerop (DECL_SIZE (field))
1282 && host_integerp (DECL_SIZE (field), 1)
1283 && host_integerp (rli->offset, 1)
1284 && host_integerp (TYPE_SIZE (type), 1))
1286 unsigned int type_align = TYPE_ALIGN (type);
1287 tree dsize = DECL_SIZE (field);
1288 HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
1289 HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
1290 HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
1292 #ifdef ADJUST_FIELD_ALIGN
1293 if (! TYPE_USER_ALIGN (type))
1294 type_align = ADJUST_FIELD_ALIGN (field, type_align);
1295 #endif
1297 if (maximum_field_alignment != 0)
1298 type_align = MIN (type_align, maximum_field_alignment);
1299 /* ??? This test is opposite the test in the containing if
1300 statement, so this code is unreachable currently. */
1301 else if (DECL_PACKED (field))
1302 type_align = MIN (type_align, BITS_PER_UNIT);
1304 /* A bit field may not span the unit of alignment of its type.
1305 Advance to next boundary if necessary. */
1306 if (excess_unit_span (offset, bit_offset, field_size, type_align, type))
1307 rli->bitpos = round_up (rli->bitpos, type_align);
1309 TYPE_USER_ALIGN (rli->t) |= TYPE_USER_ALIGN (type);
1311 #endif
1313 /* See the docs for TARGET_MS_BITFIELD_LAYOUT_P for details.
1314 A subtlety:
1315 When a bit field is inserted into a packed record, the whole
1316 size of the underlying type is used by one or more same-size
1317 adjacent bitfields. (That is, if its long:3, 32 bits is
1318 used in the record, and any additional adjacent long bitfields are
1319 packed into the same chunk of 32 bits. However, if the size
1320 changes, a new field of that size is allocated.) In an unpacked
1321 record, this is the same as using alignment, but not equivalent
1322 when packing.
1324 Note: for compatibility, we use the type size, not the type alignment
1325 to determine alignment, since that matches the documentation */
1327 if (targetm.ms_bitfield_layout_p (rli->t))
1329 tree prev_saved = rli->prev_field;
1330 tree prev_type = prev_saved ? DECL_BIT_FIELD_TYPE (prev_saved) : NULL;
1332 /* This is a bitfield if it exists. */
1333 if (rli->prev_field)
1335 /* If both are bitfields, nonzero, and the same size, this is
1336 the middle of a run. Zero declared size fields are special
1337 and handled as "end of run". (Note: it's nonzero declared
1338 size, but equal type sizes!) (Since we know that both
1339 the current and previous fields are bitfields by the
1340 time we check it, DECL_SIZE must be present for both.) */
1341 if (DECL_BIT_FIELD_TYPE (field)
1342 && !integer_zerop (DECL_SIZE (field))
1343 && !integer_zerop (DECL_SIZE (rli->prev_field))
1344 && host_integerp (DECL_SIZE (rli->prev_field), 0)
1345 && host_integerp (TYPE_SIZE (type), 0)
1346 && simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (prev_type)))
1348 /* We're in the middle of a run of equal type size fields; make
1349 sure we realign if we run out of bits. (Not decl size,
1350 type size!) */
1351 HOST_WIDE_INT bitsize = tree_low_cst (DECL_SIZE (field), 1);
1353 if (rli->remaining_in_alignment < bitsize)
1355 HOST_WIDE_INT typesize = tree_low_cst (TYPE_SIZE (type), 1);
1357 /* out of bits; bump up to next 'word'. */
1358 rli->bitpos
1359 = size_binop (PLUS_EXPR, rli->bitpos,
1360 bitsize_int (rli->remaining_in_alignment));
1361 rli->prev_field = field;
1362 if (typesize < bitsize)
1363 rli->remaining_in_alignment = 0;
1364 else
1365 rli->remaining_in_alignment = typesize - bitsize;
1367 else
1368 rli->remaining_in_alignment -= bitsize;
1370 else
1372 /* End of a run: if leaving a run of bitfields of the same type
1373 size, we have to "use up" the rest of the bits of the type
1374 size.
1376 Compute the new position as the sum of the size for the prior
1377 type and where we first started working on that type.
1378 Note: since the beginning of the field was aligned then
1379 of course the end will be too. No round needed. */
1381 if (!integer_zerop (DECL_SIZE (rli->prev_field)))
1383 rli->bitpos
1384 = size_binop (PLUS_EXPR, rli->bitpos,
1385 bitsize_int (rli->remaining_in_alignment));
1387 else
1388 /* We "use up" size zero fields; the code below should behave
1389 as if the prior field was not a bitfield. */
1390 prev_saved = NULL;
1392 /* Cause a new bitfield to be captured, either this time (if
1393 currently a bitfield) or next time we see one. */
1394 if (!DECL_BIT_FIELD_TYPE(field)
1395 || integer_zerop (DECL_SIZE (field)))
1396 rli->prev_field = NULL;
1399 normalize_rli (rli);
1402 /* If we're starting a new run of same type size bitfields
1403 (or a run of non-bitfields), set up the "first of the run"
1404 fields.
1406 That is, if the current field is not a bitfield, or if there
1407 was a prior bitfield the type sizes differ, or if there wasn't
1408 a prior bitfield the size of the current field is nonzero.
1410 Note: we must be sure to test ONLY the type size if there was
1411 a prior bitfield and ONLY for the current field being zero if
1412 there wasn't. */
1414 if (!DECL_BIT_FIELD_TYPE (field)
1415 || (prev_saved != NULL
1416 ? !simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (prev_type))
1417 : !integer_zerop (DECL_SIZE (field)) ))
1419 /* Never smaller than a byte for compatibility. */
1420 unsigned int type_align = BITS_PER_UNIT;
1422 /* (When not a bitfield), we could be seeing a flex array (with
1423 no DECL_SIZE). Since we won't be using remaining_in_alignment
1424 until we see a bitfield (and come by here again) we just skip
1425 calculating it. */
1426 if (DECL_SIZE (field) != NULL
1427 && host_integerp (TYPE_SIZE (TREE_TYPE (field)), 1)
1428 && host_integerp (DECL_SIZE (field), 1))
1430 unsigned HOST_WIDE_INT bitsize
1431 = tree_low_cst (DECL_SIZE (field), 1);
1432 unsigned HOST_WIDE_INT typesize
1433 = tree_low_cst (TYPE_SIZE (TREE_TYPE (field)), 1);
1435 if (typesize < bitsize)
1436 rli->remaining_in_alignment = 0;
1437 else
1438 rli->remaining_in_alignment = typesize - bitsize;
1441 /* Now align (conventionally) for the new type. */
1442 type_align = TYPE_ALIGN (TREE_TYPE (field));
1444 if (maximum_field_alignment != 0)
1445 type_align = MIN (type_align, maximum_field_alignment);
1447 rli->bitpos = round_up (rli->bitpos, type_align);
1449 /* If we really aligned, don't allow subsequent bitfields
1450 to undo that. */
1451 rli->prev_field = NULL;
1455 /* Offset so far becomes the position of this field after normalizing. */
1456 normalize_rli (rli);
1457 DECL_FIELD_OFFSET (field) = rli->offset;
1458 DECL_FIELD_BIT_OFFSET (field) = rli->bitpos;
1459 SET_DECL_OFFSET_ALIGN (field, rli->offset_align);
1461 /* If this field ended up more aligned than we thought it would be (we
1462 approximate this by seeing if its position changed), lay out the field
1463 again; perhaps we can use an integral mode for it now. */
1464 if (! integer_zerop (DECL_FIELD_BIT_OFFSET (field)))
1465 actual_align = (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1466 & - tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1));
1467 else if (integer_zerop (DECL_FIELD_OFFSET (field)))
1468 actual_align = MAX (BIGGEST_ALIGNMENT, rli->record_align);
1469 else if (host_integerp (DECL_FIELD_OFFSET (field), 1))
1470 actual_align = (BITS_PER_UNIT
1471 * (tree_low_cst (DECL_FIELD_OFFSET (field), 1)
1472 & - tree_low_cst (DECL_FIELD_OFFSET (field), 1)));
1473 else
1474 actual_align = DECL_OFFSET_ALIGN (field);
1475 /* ACTUAL_ALIGN is still the actual alignment *within the record* .
1476 store / extract bit field operations will check the alignment of the
1477 record against the mode of bit fields. */
1479 if (known_align != actual_align)
1480 layout_decl (field, actual_align);
1482 if (rli->prev_field == NULL && DECL_BIT_FIELD_TYPE (field))
1483 rli->prev_field = field;
1485 /* Now add size of this field to the size of the record. If the size is
1486 not constant, treat the field as being a multiple of bytes and just
1487 adjust the offset, resetting the bit position. Otherwise, apportion the
1488 size amongst the bit position and offset. First handle the case of an
1489 unspecified size, which can happen when we have an invalid nested struct
1490 definition, such as struct j { struct j { int i; } }. The error message
1491 is printed in finish_struct. */
1492 if (DECL_SIZE (field) == 0)
1493 /* Do nothing. */;
1494 else if (TREE_CODE (DECL_SIZE (field)) != INTEGER_CST
1495 || TREE_OVERFLOW (DECL_SIZE (field)))
1497 rli->offset
1498 = size_binop (PLUS_EXPR, rli->offset,
1499 fold_convert (sizetype,
1500 size_binop (CEIL_DIV_EXPR, rli->bitpos,
1501 bitsize_unit_node)));
1502 rli->offset
1503 = size_binop (PLUS_EXPR, rli->offset, DECL_SIZE_UNIT (field));
1504 rli->bitpos = bitsize_zero_node;
1505 rli->offset_align = MIN (rli->offset_align, desired_align);
1507 else if (targetm.ms_bitfield_layout_p (rli->t))
1509 rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos, DECL_SIZE (field));
1511 /* If we ended a bitfield before the full length of the type then
1512 pad the struct out to the full length of the last type. */
1513 if ((DECL_CHAIN (field) == NULL
1514 || TREE_CODE (DECL_CHAIN (field)) != FIELD_DECL)
1515 && DECL_BIT_FIELD_TYPE (field)
1516 && !integer_zerop (DECL_SIZE (field)))
1517 rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos,
1518 bitsize_int (rli->remaining_in_alignment));
1520 normalize_rli (rli);
1522 else
1524 rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos, DECL_SIZE (field));
1525 normalize_rli (rli);
1529 /* Assuming that all the fields have been laid out, this function uses
1530 RLI to compute the final TYPE_SIZE, TYPE_ALIGN, etc. for the type
1531 indicated by RLI. */
1533 static void
1534 finalize_record_size (record_layout_info rli)
1536 tree unpadded_size, unpadded_size_unit;
1538 /* Now we want just byte and bit offsets, so set the offset alignment
1539 to be a byte and then normalize. */
1540 rli->offset_align = BITS_PER_UNIT;
1541 normalize_rli (rli);
1543 /* Determine the desired alignment. */
1544 #ifdef ROUND_TYPE_ALIGN
1545 TYPE_ALIGN (rli->t) = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t),
1546 rli->record_align);
1547 #else
1548 TYPE_ALIGN (rli->t) = MAX (TYPE_ALIGN (rli->t), rli->record_align);
1549 #endif
1551 /* Compute the size so far. Be sure to allow for extra bits in the
1552 size in bytes. We have guaranteed above that it will be no more
1553 than a single byte. */
1554 unpadded_size = rli_size_so_far (rli);
1555 unpadded_size_unit = rli_size_unit_so_far (rli);
1556 if (! integer_zerop (rli->bitpos))
1557 unpadded_size_unit
1558 = size_binop (PLUS_EXPR, unpadded_size_unit, size_one_node);
1560 /* Round the size up to be a multiple of the required alignment. */
1561 TYPE_SIZE (rli->t) = round_up (unpadded_size, TYPE_ALIGN (rli->t));
1562 TYPE_SIZE_UNIT (rli->t)
1563 = round_up (unpadded_size_unit, TYPE_ALIGN_UNIT (rli->t));
1565 if (TREE_CONSTANT (unpadded_size)
1566 && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0
1567 && input_location != BUILTINS_LOCATION)
1568 warning (OPT_Wpadded, "padding struct size to alignment boundary");
1570 if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
1571 && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
1572 && TREE_CONSTANT (unpadded_size))
1574 tree unpacked_size;
1576 #ifdef ROUND_TYPE_ALIGN
1577 rli->unpacked_align
1578 = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t), rli->unpacked_align);
1579 #else
1580 rli->unpacked_align = MAX (TYPE_ALIGN (rli->t), rli->unpacked_align);
1581 #endif
1583 unpacked_size = round_up (TYPE_SIZE (rli->t), rli->unpacked_align);
1584 if (simple_cst_equal (unpacked_size, TYPE_SIZE (rli->t)))
1586 if (TYPE_NAME (rli->t))
1588 tree name;
1590 if (TREE_CODE (TYPE_NAME (rli->t)) == IDENTIFIER_NODE)
1591 name = TYPE_NAME (rli->t);
1592 else
1593 name = DECL_NAME (TYPE_NAME (rli->t));
1595 if (STRICT_ALIGNMENT)
1596 warning (OPT_Wpacked, "packed attribute causes inefficient "
1597 "alignment for %qE", name);
1598 else
1599 warning (OPT_Wpacked,
1600 "packed attribute is unnecessary for %qE", name);
1602 else
1604 if (STRICT_ALIGNMENT)
1605 warning (OPT_Wpacked,
1606 "packed attribute causes inefficient alignment");
1607 else
1608 warning (OPT_Wpacked, "packed attribute is unnecessary");
1614 /* Compute the TYPE_MODE for the TYPE (which is a RECORD_TYPE). */
1616 void
1617 compute_record_mode (tree type)
1619 tree field;
1620 enum machine_mode mode = VOIDmode;
1622 /* Most RECORD_TYPEs have BLKmode, so we start off assuming that.
1623 However, if possible, we use a mode that fits in a register
1624 instead, in order to allow for better optimization down the
1625 line. */
1626 SET_TYPE_MODE (type, BLKmode);
1628 if (! host_integerp (TYPE_SIZE (type), 1))
1629 return;
1631 /* A record which has any BLKmode members must itself be
1632 BLKmode; it can't go in a register. Unless the member is
1633 BLKmode only because it isn't aligned. */
1634 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1636 if (TREE_CODE (field) != FIELD_DECL)
1637 continue;
1639 if (TREE_CODE (TREE_TYPE (field)) == ERROR_MARK
1640 || (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1641 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field))
1642 && !(TYPE_SIZE (TREE_TYPE (field)) != 0
1643 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))))
1644 || ! host_integerp (bit_position (field), 1)
1645 || DECL_SIZE (field) == 0
1646 || ! host_integerp (DECL_SIZE (field), 1))
1647 return;
1649 /* If this field is the whole struct, remember its mode so
1650 that, say, we can put a double in a class into a DF
1651 register instead of forcing it to live in the stack. */
1652 if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
1653 mode = DECL_MODE (field);
1655 /* With some targets, it is sub-optimal to access an aligned
1656 BLKmode structure as a scalar. */
1657 if (targetm.member_type_forces_blk (field, mode))
1658 return;
1661 /* If we only have one real field; use its mode if that mode's size
1662 matches the type's size. This only applies to RECORD_TYPE. This
1663 does not apply to unions. */
1664 if (TREE_CODE (type) == RECORD_TYPE && mode != VOIDmode
1665 && host_integerp (TYPE_SIZE (type), 1)
1666 && GET_MODE_BITSIZE (mode) == TREE_INT_CST_LOW (TYPE_SIZE (type)))
1667 SET_TYPE_MODE (type, mode);
1668 else
1669 SET_TYPE_MODE (type, mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1));
1671 /* If structure's known alignment is less than what the scalar
1672 mode would need, and it matters, then stick with BLKmode. */
1673 if (TYPE_MODE (type) != BLKmode
1674 && STRICT_ALIGNMENT
1675 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1676 || TYPE_ALIGN (type) >= GET_MODE_ALIGNMENT (TYPE_MODE (type))))
1678 /* If this is the only reason this type is BLKmode, then
1679 don't force containing types to be BLKmode. */
1680 TYPE_NO_FORCE_BLK (type) = 1;
1681 SET_TYPE_MODE (type, BLKmode);
1685 /* Compute TYPE_SIZE and TYPE_ALIGN for TYPE, once it has been laid
1686 out. */
1688 static void
1689 finalize_type_size (tree type)
1691 /* Normally, use the alignment corresponding to the mode chosen.
1692 However, where strict alignment is not required, avoid
1693 over-aligning structures, since most compilers do not do this
1694 alignment. */
1696 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1697 && (STRICT_ALIGNMENT
1698 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1699 && TREE_CODE (type) != QUAL_UNION_TYPE
1700 && TREE_CODE (type) != ARRAY_TYPE)))
1702 unsigned mode_align = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1704 /* Don't override a larger alignment requirement coming from a user
1705 alignment of one of the fields. */
1706 if (mode_align >= TYPE_ALIGN (type))
1708 TYPE_ALIGN (type) = mode_align;
1709 TYPE_USER_ALIGN (type) = 0;
1713 /* Do machine-dependent extra alignment. */
1714 #ifdef ROUND_TYPE_ALIGN
1715 TYPE_ALIGN (type)
1716 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1717 #endif
1719 /* If we failed to find a simple way to calculate the unit size
1720 of the type, find it by division. */
1721 if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1722 /* TYPE_SIZE (type) is computed in bitsizetype. After the division, the
1723 result will fit in sizetype. We will get more efficient code using
1724 sizetype, so we force a conversion. */
1725 TYPE_SIZE_UNIT (type)
1726 = fold_convert (sizetype,
1727 size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1728 bitsize_unit_node));
1730 if (TYPE_SIZE (type) != 0)
1732 TYPE_SIZE (type) = round_up (TYPE_SIZE (type), TYPE_ALIGN (type));
1733 TYPE_SIZE_UNIT (type)
1734 = round_up (TYPE_SIZE_UNIT (type), TYPE_ALIGN_UNIT (type));
1737 /* Evaluate nonconstant sizes only once, either now or as soon as safe. */
1738 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1739 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1740 if (TYPE_SIZE_UNIT (type) != 0
1741 && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1742 TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1744 /* Also layout any other variants of the type. */
1745 if (TYPE_NEXT_VARIANT (type)
1746 || type != TYPE_MAIN_VARIANT (type))
1748 tree variant;
1749 /* Record layout info of this variant. */
1750 tree size = TYPE_SIZE (type);
1751 tree size_unit = TYPE_SIZE_UNIT (type);
1752 unsigned int align = TYPE_ALIGN (type);
1753 unsigned int user_align = TYPE_USER_ALIGN (type);
1754 enum machine_mode mode = TYPE_MODE (type);
1756 /* Copy it into all variants. */
1757 for (variant = TYPE_MAIN_VARIANT (type);
1758 variant != 0;
1759 variant = TYPE_NEXT_VARIANT (variant))
1761 TYPE_SIZE (variant) = size;
1762 TYPE_SIZE_UNIT (variant) = size_unit;
1763 TYPE_ALIGN (variant) = align;
1764 TYPE_USER_ALIGN (variant) = user_align;
1765 SET_TYPE_MODE (variant, mode);
1770 /* Return a new underlying object for a bitfield started with FIELD. */
1772 static tree
1773 start_bitfield_representative (tree field)
1775 tree repr = make_node (FIELD_DECL);
1776 DECL_FIELD_OFFSET (repr) = DECL_FIELD_OFFSET (field);
1777 /* Force the representative to begin at a BITS_PER_UNIT aligned
1778 boundary - C++ may use tail-padding of a base object to
1779 continue packing bits so the bitfield region does not start
1780 at bit zero (see g++.dg/abi/bitfield5.C for example).
1781 Unallocated bits may happen for other reasons as well,
1782 for example Ada which allows explicit bit-granular structure layout. */
1783 DECL_FIELD_BIT_OFFSET (repr)
1784 = size_binop (BIT_AND_EXPR,
1785 DECL_FIELD_BIT_OFFSET (field),
1786 bitsize_int (~(BITS_PER_UNIT - 1)));
1787 SET_DECL_OFFSET_ALIGN (repr, DECL_OFFSET_ALIGN (field));
1788 DECL_SIZE (repr) = DECL_SIZE (field);
1789 DECL_SIZE_UNIT (repr) = DECL_SIZE_UNIT (field);
1790 DECL_PACKED (repr) = DECL_PACKED (field);
1791 DECL_CONTEXT (repr) = DECL_CONTEXT (field);
1792 return repr;
1795 /* Finish up a bitfield group that was started by creating the underlying
1796 object REPR with the last field in the bitfield group FIELD. */
1798 static void
1799 finish_bitfield_representative (tree repr, tree field)
1801 unsigned HOST_WIDE_INT bitsize, maxbitsize;
1802 enum machine_mode mode;
1803 tree nextf, size;
1805 size = size_diffop (DECL_FIELD_OFFSET (field),
1806 DECL_FIELD_OFFSET (repr));
1807 gcc_assert (host_integerp (size, 1));
1808 bitsize = (tree_low_cst (size, 1) * BITS_PER_UNIT
1809 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1810 - tree_low_cst (DECL_FIELD_BIT_OFFSET (repr), 1)
1811 + tree_low_cst (DECL_SIZE (field), 1));
1813 /* Round up bitsize to multiples of BITS_PER_UNIT. */
1814 bitsize = (bitsize + BITS_PER_UNIT - 1) & ~(BITS_PER_UNIT - 1);
1816 /* Now nothing tells us how to pad out bitsize ... */
1817 nextf = DECL_CHAIN (field);
1818 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
1819 nextf = DECL_CHAIN (nextf);
1820 if (nextf)
1822 tree maxsize;
1823 /* If there was an error, the field may be not laid out
1824 correctly. Don't bother to do anything. */
1825 if (TREE_TYPE (nextf) == error_mark_node)
1826 return;
1827 maxsize = size_diffop (DECL_FIELD_OFFSET (nextf),
1828 DECL_FIELD_OFFSET (repr));
1829 if (host_integerp (maxsize, 1))
1831 maxbitsize = (tree_low_cst (maxsize, 1) * BITS_PER_UNIT
1832 + tree_low_cst (DECL_FIELD_BIT_OFFSET (nextf), 1)
1833 - tree_low_cst (DECL_FIELD_BIT_OFFSET (repr), 1));
1834 /* If the group ends within a bitfield nextf does not need to be
1835 aligned to BITS_PER_UNIT. Thus round up. */
1836 maxbitsize = (maxbitsize + BITS_PER_UNIT - 1) & ~(BITS_PER_UNIT - 1);
1838 else
1839 maxbitsize = bitsize;
1841 else
1843 /* ??? If you consider that tail-padding of this struct might be
1844 re-used when deriving from it we cannot really do the following
1845 and thus need to set maxsize to bitsize? Also we cannot
1846 generally rely on maxsize to fold to an integer constant, so
1847 use bitsize as fallback for this case. */
1848 tree maxsize = size_diffop (TYPE_SIZE_UNIT (DECL_CONTEXT (field)),
1849 DECL_FIELD_OFFSET (repr));
1850 if (host_integerp (maxsize, 1))
1851 maxbitsize = (tree_low_cst (maxsize, 1) * BITS_PER_UNIT
1852 - tree_low_cst (DECL_FIELD_BIT_OFFSET (repr), 1));
1853 else
1854 maxbitsize = bitsize;
1857 /* Only if we don't artificially break up the representative in
1858 the middle of a large bitfield with different possibly
1859 overlapping representatives. And all representatives start
1860 at byte offset. */
1861 gcc_assert (maxbitsize % BITS_PER_UNIT == 0);
1863 /* Find the smallest nice mode to use. */
1864 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1865 mode = GET_MODE_WIDER_MODE (mode))
1866 if (GET_MODE_BITSIZE (mode) >= bitsize)
1867 break;
1868 if (mode != VOIDmode
1869 && (GET_MODE_BITSIZE (mode) > maxbitsize
1870 || GET_MODE_BITSIZE (mode) > MAX_FIXED_MODE_SIZE))
1871 mode = VOIDmode;
1873 if (mode == VOIDmode)
1875 /* We really want a BLKmode representative only as a last resort,
1876 considering the member b in
1877 struct { int a : 7; int b : 17; int c; } __attribute__((packed));
1878 Otherwise we simply want to split the representative up
1879 allowing for overlaps within the bitfield region as required for
1880 struct { int a : 7; int b : 7;
1881 int c : 10; int d; } __attribute__((packed));
1882 [0, 15] HImode for a and b, [8, 23] HImode for c. */
1883 DECL_SIZE (repr) = bitsize_int (bitsize);
1884 DECL_SIZE_UNIT (repr) = size_int (bitsize / BITS_PER_UNIT);
1885 DECL_MODE (repr) = BLKmode;
1886 TREE_TYPE (repr) = build_array_type_nelts (unsigned_char_type_node,
1887 bitsize / BITS_PER_UNIT);
1889 else
1891 unsigned HOST_WIDE_INT modesize = GET_MODE_BITSIZE (mode);
1892 DECL_SIZE (repr) = bitsize_int (modesize);
1893 DECL_SIZE_UNIT (repr) = size_int (modesize / BITS_PER_UNIT);
1894 DECL_MODE (repr) = mode;
1895 TREE_TYPE (repr) = lang_hooks.types.type_for_mode (mode, 1);
1898 /* Remember whether the bitfield group is at the end of the
1899 structure or not. */
1900 DECL_CHAIN (repr) = nextf;
1903 /* Compute and set FIELD_DECLs for the underlying objects we should
1904 use for bitfield access for the structure laid out with RLI. */
1906 static void
1907 finish_bitfield_layout (record_layout_info rli)
1909 tree field, prev;
1910 tree repr = NULL_TREE;
1912 /* Unions would be special, for the ease of type-punning optimizations
1913 we could use the underlying type as hint for the representative
1914 if the bitfield would fit and the representative would not exceed
1915 the union in size. */
1916 if (TREE_CODE (rli->t) != RECORD_TYPE)
1917 return;
1919 for (prev = NULL_TREE, field = TYPE_FIELDS (rli->t);
1920 field; field = DECL_CHAIN (field))
1922 if (TREE_CODE (field) != FIELD_DECL)
1923 continue;
1925 /* In the C++ memory model, consecutive bit fields in a structure are
1926 considered one memory location and updating a memory location
1927 may not store into adjacent memory locations. */
1928 if (!repr
1929 && DECL_BIT_FIELD_TYPE (field))
1931 /* Start new representative. */
1932 repr = start_bitfield_representative (field);
1934 else if (repr
1935 && ! DECL_BIT_FIELD_TYPE (field))
1937 /* Finish off new representative. */
1938 finish_bitfield_representative (repr, prev);
1939 repr = NULL_TREE;
1941 else if (DECL_BIT_FIELD_TYPE (field))
1943 gcc_assert (repr != NULL_TREE);
1945 /* Zero-size bitfields finish off a representative and
1946 do not have a representative themselves. This is
1947 required by the C++ memory model. */
1948 if (integer_zerop (DECL_SIZE (field)))
1950 finish_bitfield_representative (repr, prev);
1951 repr = NULL_TREE;
1954 /* We assume that either DECL_FIELD_OFFSET of the representative
1955 and each bitfield member is a constant or they are equal.
1956 This is because we need to be able to compute the bit-offset
1957 of each field relative to the representative in get_bit_range
1958 during RTL expansion.
1959 If these constraints are not met, simply force a new
1960 representative to be generated. That will at most
1961 generate worse code but still maintain correctness with
1962 respect to the C++ memory model. */
1963 else if (!((host_integerp (DECL_FIELD_OFFSET (repr), 1)
1964 && host_integerp (DECL_FIELD_OFFSET (field), 1))
1965 || operand_equal_p (DECL_FIELD_OFFSET (repr),
1966 DECL_FIELD_OFFSET (field), 0)))
1968 finish_bitfield_representative (repr, prev);
1969 repr = start_bitfield_representative (field);
1972 else
1973 continue;
1975 if (repr)
1976 DECL_BIT_FIELD_REPRESENTATIVE (field) = repr;
1978 prev = field;
1981 if (repr)
1982 finish_bitfield_representative (repr, prev);
1985 /* Do all of the work required to layout the type indicated by RLI,
1986 once the fields have been laid out. This function will call `free'
1987 for RLI, unless FREE_P is false. Passing a value other than false
1988 for FREE_P is bad practice; this option only exists to support the
1989 G++ 3.2 ABI. */
1991 void
1992 finish_record_layout (record_layout_info rli, int free_p)
1994 tree variant;
1996 /* Compute the final size. */
1997 finalize_record_size (rli);
1999 /* Compute the TYPE_MODE for the record. */
2000 compute_record_mode (rli->t);
2002 /* Perform any last tweaks to the TYPE_SIZE, etc. */
2003 finalize_type_size (rli->t);
2005 /* Compute bitfield representatives. */
2006 finish_bitfield_layout (rli);
2008 /* Propagate TYPE_PACKED to variants. With C++ templates,
2009 handle_packed_attribute is too early to do this. */
2010 for (variant = TYPE_NEXT_VARIANT (rli->t); variant;
2011 variant = TYPE_NEXT_VARIANT (variant))
2012 TYPE_PACKED (variant) = TYPE_PACKED (rli->t);
2014 /* Lay out any static members. This is done now because their type
2015 may use the record's type. */
2016 while (!vec_safe_is_empty (rli->pending_statics))
2017 layout_decl (rli->pending_statics->pop (), 0);
2019 /* Clean up. */
2020 if (free_p)
2022 vec_free (rli->pending_statics);
2023 free (rli);
2028 /* Finish processing a builtin RECORD_TYPE type TYPE. It's name is
2029 NAME, its fields are chained in reverse on FIELDS.
2031 If ALIGN_TYPE is non-null, it is given the same alignment as
2032 ALIGN_TYPE. */
2034 void
2035 finish_builtin_struct (tree type, const char *name, tree fields,
2036 tree align_type)
2038 tree tail, next;
2040 for (tail = NULL_TREE; fields; tail = fields, fields = next)
2042 DECL_FIELD_CONTEXT (fields) = type;
2043 next = DECL_CHAIN (fields);
2044 DECL_CHAIN (fields) = tail;
2046 TYPE_FIELDS (type) = tail;
2048 if (align_type)
2050 TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
2051 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (align_type);
2054 layout_type (type);
2055 #if 0 /* not yet, should get fixed properly later */
2056 TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
2057 #else
2058 TYPE_NAME (type) = build_decl (BUILTINS_LOCATION,
2059 TYPE_DECL, get_identifier (name), type);
2060 #endif
2061 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2062 layout_decl (TYPE_NAME (type), 0);
2065 /* Calculate the mode, size, and alignment for TYPE.
2066 For an array type, calculate the element separation as well.
2067 Record TYPE on the chain of permanent or temporary types
2068 so that dbxout will find out about it.
2070 TYPE_SIZE of a type is nonzero if the type has been laid out already.
2071 layout_type does nothing on such a type.
2073 If the type is incomplete, its TYPE_SIZE remains zero. */
2075 void
2076 layout_type (tree type)
2078 gcc_assert (type);
2080 if (type == error_mark_node)
2081 return;
2083 /* Do nothing if type has been laid out before. */
2084 if (TYPE_SIZE (type))
2085 return;
2087 switch (TREE_CODE (type))
2089 case LANG_TYPE:
2090 /* This kind of type is the responsibility
2091 of the language-specific code. */
2092 gcc_unreachable ();
2094 case BOOLEAN_TYPE: /* Used for Java, Pascal, and Chill. */
2095 if (TYPE_PRECISION (type) == 0)
2096 TYPE_PRECISION (type) = 1; /* default to one byte/boolean. */
2098 /* ... fall through ... */
2100 case INTEGER_TYPE:
2101 case ENUMERAL_TYPE:
2102 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
2103 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
2104 TYPE_UNSIGNED (type) = 1;
2106 SET_TYPE_MODE (type,
2107 smallest_mode_for_size (TYPE_PRECISION (type), MODE_INT));
2108 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
2109 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
2110 break;
2112 case REAL_TYPE:
2113 SET_TYPE_MODE (type,
2114 mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0));
2115 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
2116 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
2117 break;
2119 case FIXED_POINT_TYPE:
2120 /* TYPE_MODE (type) has been set already. */
2121 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
2122 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
2123 break;
2125 case COMPLEX_TYPE:
2126 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TREE_TYPE (type));
2127 SET_TYPE_MODE (type,
2128 mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
2129 (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE
2130 ? MODE_COMPLEX_FLOAT : MODE_COMPLEX_INT),
2131 0));
2132 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
2133 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
2134 break;
2136 case VECTOR_TYPE:
2138 int nunits = TYPE_VECTOR_SUBPARTS (type);
2139 tree innertype = TREE_TYPE (type);
2141 gcc_assert (!(nunits & (nunits - 1)));
2143 /* Find an appropriate mode for the vector type. */
2144 if (TYPE_MODE (type) == VOIDmode)
2145 SET_TYPE_MODE (type,
2146 mode_for_vector (TYPE_MODE (innertype), nunits));
2148 TYPE_SATURATING (type) = TYPE_SATURATING (TREE_TYPE (type));
2149 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TREE_TYPE (type));
2150 TYPE_SIZE_UNIT (type) = int_const_binop (MULT_EXPR,
2151 TYPE_SIZE_UNIT (innertype),
2152 size_int (nunits));
2153 TYPE_SIZE (type) = int_const_binop (MULT_EXPR, TYPE_SIZE (innertype),
2154 bitsize_int (nunits));
2156 /* For vector types, we do not default to the mode's alignment.
2157 Instead, query a target hook, defaulting to natural alignment.
2158 This prevents ABI changes depending on whether or not native
2159 vector modes are supported. */
2160 TYPE_ALIGN (type) = targetm.vector_alignment (type);
2162 /* However, if the underlying mode requires a bigger alignment than
2163 what the target hook provides, we cannot use the mode. For now,
2164 simply reject that case. */
2165 gcc_assert (TYPE_ALIGN (type)
2166 >= GET_MODE_ALIGNMENT (TYPE_MODE (type)));
2167 break;
2170 case VOID_TYPE:
2171 /* This is an incomplete type and so doesn't have a size. */
2172 TYPE_ALIGN (type) = 1;
2173 TYPE_USER_ALIGN (type) = 0;
2174 SET_TYPE_MODE (type, VOIDmode);
2175 break;
2177 case OFFSET_TYPE:
2178 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
2179 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
2180 /* A pointer might be MODE_PARTIAL_INT,
2181 but ptrdiff_t must be integral. */
2182 SET_TYPE_MODE (type, mode_for_size (POINTER_SIZE, MODE_INT, 0));
2183 TYPE_PRECISION (type) = POINTER_SIZE;
2184 break;
2186 case FUNCTION_TYPE:
2187 case METHOD_TYPE:
2188 /* It's hard to see what the mode and size of a function ought to
2189 be, but we do know the alignment is FUNCTION_BOUNDARY, so
2190 make it consistent with that. */
2191 SET_TYPE_MODE (type, mode_for_size (FUNCTION_BOUNDARY, MODE_INT, 0));
2192 TYPE_SIZE (type) = bitsize_int (FUNCTION_BOUNDARY);
2193 TYPE_SIZE_UNIT (type) = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
2194 break;
2196 case POINTER_TYPE:
2197 case REFERENCE_TYPE:
2199 enum machine_mode mode = TYPE_MODE (type);
2200 if (TREE_CODE (type) == REFERENCE_TYPE && reference_types_internal)
2202 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
2203 mode = targetm.addr_space.address_mode (as);
2206 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (mode));
2207 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (mode));
2208 TYPE_UNSIGNED (type) = 1;
2209 TYPE_PRECISION (type) = GET_MODE_BITSIZE (mode);
2211 break;
2213 case ARRAY_TYPE:
2215 tree index = TYPE_DOMAIN (type);
2216 tree element = TREE_TYPE (type);
2218 build_pointer_type (element);
2220 /* We need to know both bounds in order to compute the size. */
2221 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
2222 && TYPE_SIZE (element))
2224 tree ub = TYPE_MAX_VALUE (index);
2225 tree lb = TYPE_MIN_VALUE (index);
2226 tree element_size = TYPE_SIZE (element);
2227 tree length;
2229 /* Make sure that an array of zero-sized element is zero-sized
2230 regardless of its extent. */
2231 if (integer_zerop (element_size))
2232 length = size_zero_node;
2234 /* The computation should happen in the original signedness so
2235 that (possible) negative values are handled appropriately
2236 when determining overflow. */
2237 else
2239 /* ??? When it is obvious that the range is signed
2240 represent it using ssizetype. */
2241 if (TREE_CODE (lb) == INTEGER_CST
2242 && TREE_CODE (ub) == INTEGER_CST
2243 && TYPE_UNSIGNED (TREE_TYPE (lb))
2244 && tree_int_cst_lt (ub, lb))
2246 unsigned prec = TYPE_PRECISION (TREE_TYPE (lb));
2247 lb = double_int_to_tree
2248 (ssizetype,
2249 tree_to_double_int (lb).sext (prec));
2250 ub = double_int_to_tree
2251 (ssizetype,
2252 tree_to_double_int (ub).sext (prec));
2254 length
2255 = fold_convert (sizetype,
2256 size_binop (PLUS_EXPR,
2257 build_int_cst (TREE_TYPE (lb), 1),
2258 size_binop (MINUS_EXPR, ub, lb)));
2261 /* ??? We have no way to distinguish a null-sized array from an
2262 array spanning the whole sizetype range, so we arbitrarily
2263 decide that [0, -1] is the only valid representation. */
2264 if (integer_zerop (length)
2265 && TREE_OVERFLOW (length)
2266 && integer_zerop (lb))
2267 length = size_zero_node;
2269 TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size,
2270 fold_convert (bitsizetype,
2271 length));
2273 /* If we know the size of the element, calculate the total size
2274 directly, rather than do some division thing below. This
2275 optimization helps Fortran assumed-size arrays (where the
2276 size of the array is determined at runtime) substantially. */
2277 if (TYPE_SIZE_UNIT (element))
2278 TYPE_SIZE_UNIT (type)
2279 = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
2282 /* Now round the alignment and size,
2283 using machine-dependent criteria if any. */
2285 #ifdef ROUND_TYPE_ALIGN
2286 TYPE_ALIGN (type)
2287 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
2288 #else
2289 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
2290 #endif
2291 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (element);
2292 SET_TYPE_MODE (type, BLKmode);
2293 if (TYPE_SIZE (type) != 0
2294 && ! targetm.member_type_forces_blk (type, VOIDmode)
2295 /* BLKmode elements force BLKmode aggregate;
2296 else extract/store fields may lose. */
2297 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
2298 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
2300 SET_TYPE_MODE (type, mode_for_array (TREE_TYPE (type),
2301 TYPE_SIZE (type)));
2302 if (TYPE_MODE (type) != BLKmode
2303 && STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
2304 && TYPE_ALIGN (type) < GET_MODE_ALIGNMENT (TYPE_MODE (type)))
2306 TYPE_NO_FORCE_BLK (type) = 1;
2307 SET_TYPE_MODE (type, BLKmode);
2310 /* When the element size is constant, check that it is at least as
2311 large as the element alignment. */
2312 if (TYPE_SIZE_UNIT (element)
2313 && TREE_CODE (TYPE_SIZE_UNIT (element)) == INTEGER_CST
2314 /* If TYPE_SIZE_UNIT overflowed, then it is certainly larger than
2315 TYPE_ALIGN_UNIT. */
2316 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (element))
2317 && !integer_zerop (TYPE_SIZE_UNIT (element))
2318 && compare_tree_int (TYPE_SIZE_UNIT (element),
2319 TYPE_ALIGN_UNIT (element)) < 0)
2320 error ("alignment of array elements is greater than element size");
2321 break;
2324 case RECORD_TYPE:
2325 case UNION_TYPE:
2326 case QUAL_UNION_TYPE:
2328 tree field;
2329 record_layout_info rli;
2331 /* Initialize the layout information. */
2332 rli = start_record_layout (type);
2334 /* If this is a QUAL_UNION_TYPE, we want to process the fields
2335 in the reverse order in building the COND_EXPR that denotes
2336 its size. We reverse them again later. */
2337 if (TREE_CODE (type) == QUAL_UNION_TYPE)
2338 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
2340 /* Place all the fields. */
2341 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2342 place_field (rli, field);
2344 if (TREE_CODE (type) == QUAL_UNION_TYPE)
2345 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
2347 /* Finish laying out the record. */
2348 finish_record_layout (rli, /*free_p=*/true);
2350 break;
2352 default:
2353 gcc_unreachable ();
2356 /* Compute the final TYPE_SIZE, TYPE_ALIGN, etc. for TYPE. For
2357 records and unions, finish_record_layout already called this
2358 function. */
2359 if (TREE_CODE (type) != RECORD_TYPE
2360 && TREE_CODE (type) != UNION_TYPE
2361 && TREE_CODE (type) != QUAL_UNION_TYPE)
2362 finalize_type_size (type);
2364 /* We should never see alias sets on incomplete aggregates. And we
2365 should not call layout_type on not incomplete aggregates. */
2366 if (AGGREGATE_TYPE_P (type))
2367 gcc_assert (!TYPE_ALIAS_SET_KNOWN_P (type));
2370 /* Vector types need to re-check the target flags each time we report
2371 the machine mode. We need to do this because attribute target can
2372 change the result of vector_mode_supported_p and have_regs_of_mode
2373 on a per-function basis. Thus the TYPE_MODE of a VECTOR_TYPE can
2374 change on a per-function basis. */
2375 /* ??? Possibly a better solution is to run through all the types
2376 referenced by a function and re-compute the TYPE_MODE once, rather
2377 than make the TYPE_MODE macro call a function. */
2379 enum machine_mode
2380 vector_type_mode (const_tree t)
2382 enum machine_mode mode;
2384 gcc_assert (TREE_CODE (t) == VECTOR_TYPE);
2386 mode = t->type_common.mode;
2387 if (VECTOR_MODE_P (mode)
2388 && (!targetm.vector_mode_supported_p (mode)
2389 || !have_regs_of_mode[mode]))
2391 enum machine_mode innermode = TREE_TYPE (t)->type_common.mode;
2393 /* For integers, try mapping it to a same-sized scalar mode. */
2394 if (GET_MODE_CLASS (innermode) == MODE_INT)
2396 mode = mode_for_size (TYPE_VECTOR_SUBPARTS (t)
2397 * GET_MODE_BITSIZE (innermode), MODE_INT, 0);
2399 if (mode != VOIDmode && have_regs_of_mode[mode])
2400 return mode;
2403 return BLKmode;
2406 return mode;
2409 /* Create and return a type for signed integers of PRECISION bits. */
2411 tree
2412 make_signed_type (int precision)
2414 tree type = make_node (INTEGER_TYPE);
2416 TYPE_PRECISION (type) = precision;
2418 fixup_signed_type (type);
2419 return type;
2422 /* Create and return a type for unsigned integers of PRECISION bits. */
2424 tree
2425 make_unsigned_type (int precision)
2427 tree type = make_node (INTEGER_TYPE);
2429 TYPE_PRECISION (type) = precision;
2431 fixup_unsigned_type (type);
2432 return type;
2435 /* Create and return a type for fract of PRECISION bits, UNSIGNEDP,
2436 and SATP. */
2438 tree
2439 make_fract_type (int precision, int unsignedp, int satp)
2441 tree type = make_node (FIXED_POINT_TYPE);
2443 TYPE_PRECISION (type) = precision;
2445 if (satp)
2446 TYPE_SATURATING (type) = 1;
2448 /* Lay out the type: set its alignment, size, etc. */
2449 if (unsignedp)
2451 TYPE_UNSIGNED (type) = 1;
2452 SET_TYPE_MODE (type, mode_for_size (precision, MODE_UFRACT, 0));
2454 else
2455 SET_TYPE_MODE (type, mode_for_size (precision, MODE_FRACT, 0));
2456 layout_type (type);
2458 return type;
2461 /* Create and return a type for accum of PRECISION bits, UNSIGNEDP,
2462 and SATP. */
2464 tree
2465 make_accum_type (int precision, int unsignedp, int satp)
2467 tree type = make_node (FIXED_POINT_TYPE);
2469 TYPE_PRECISION (type) = precision;
2471 if (satp)
2472 TYPE_SATURATING (type) = 1;
2474 /* Lay out the type: set its alignment, size, etc. */
2475 if (unsignedp)
2477 TYPE_UNSIGNED (type) = 1;
2478 SET_TYPE_MODE (type, mode_for_size (precision, MODE_UACCUM, 0));
2480 else
2481 SET_TYPE_MODE (type, mode_for_size (precision, MODE_ACCUM, 0));
2482 layout_type (type);
2484 return type;
2487 /* Initialize sizetypes so layout_type can use them. */
2489 void
2490 initialize_sizetypes (void)
2492 int precision, bprecision;
2494 /* Get sizetypes precision from the SIZE_TYPE target macro. */
2495 if (strcmp (SIZETYPE, "unsigned int") == 0)
2496 precision = INT_TYPE_SIZE;
2497 else if (strcmp (SIZETYPE, "long unsigned int") == 0)
2498 precision = LONG_TYPE_SIZE;
2499 else if (strcmp (SIZETYPE, "long long unsigned int") == 0)
2500 precision = LONG_LONG_TYPE_SIZE;
2501 else if (strcmp (SIZETYPE, "short unsigned int") == 0)
2502 precision = SHORT_TYPE_SIZE;
2503 else
2504 gcc_unreachable ();
2506 bprecision
2507 = MIN (precision + BITS_PER_UNIT_LOG + 1, MAX_FIXED_MODE_SIZE);
2508 bprecision
2509 = GET_MODE_PRECISION (smallest_mode_for_size (bprecision, MODE_INT));
2510 if (bprecision > HOST_BITS_PER_DOUBLE_INT)
2511 bprecision = HOST_BITS_PER_DOUBLE_INT;
2513 /* Create stubs for sizetype and bitsizetype so we can create constants. */
2514 sizetype = make_node (INTEGER_TYPE);
2515 TYPE_NAME (sizetype) = get_identifier ("sizetype");
2516 TYPE_PRECISION (sizetype) = precision;
2517 TYPE_UNSIGNED (sizetype) = 1;
2518 bitsizetype = make_node (INTEGER_TYPE);
2519 TYPE_NAME (bitsizetype) = get_identifier ("bitsizetype");
2520 TYPE_PRECISION (bitsizetype) = bprecision;
2521 TYPE_UNSIGNED (bitsizetype) = 1;
2523 /* Now layout both types manually. */
2524 SET_TYPE_MODE (sizetype, smallest_mode_for_size (precision, MODE_INT));
2525 TYPE_ALIGN (sizetype) = GET_MODE_ALIGNMENT (TYPE_MODE (sizetype));
2526 TYPE_SIZE (sizetype) = bitsize_int (precision);
2527 TYPE_SIZE_UNIT (sizetype) = size_int (GET_MODE_SIZE (TYPE_MODE (sizetype)));
2528 set_min_and_max_values_for_integral_type (sizetype, precision,
2529 /*is_unsigned=*/true);
2531 SET_TYPE_MODE (bitsizetype, smallest_mode_for_size (bprecision, MODE_INT));
2532 TYPE_ALIGN (bitsizetype) = GET_MODE_ALIGNMENT (TYPE_MODE (bitsizetype));
2533 TYPE_SIZE (bitsizetype) = bitsize_int (bprecision);
2534 TYPE_SIZE_UNIT (bitsizetype)
2535 = size_int (GET_MODE_SIZE (TYPE_MODE (bitsizetype)));
2536 set_min_and_max_values_for_integral_type (bitsizetype, bprecision,
2537 /*is_unsigned=*/true);
2539 /* Create the signed variants of *sizetype. */
2540 ssizetype = make_signed_type (TYPE_PRECISION (sizetype));
2541 TYPE_NAME (ssizetype) = get_identifier ("ssizetype");
2542 sbitsizetype = make_signed_type (TYPE_PRECISION (bitsizetype));
2543 TYPE_NAME (sbitsizetype) = get_identifier ("sbitsizetype");
2546 /* TYPE is an integral type, i.e., an INTEGRAL_TYPE, ENUMERAL_TYPE
2547 or BOOLEAN_TYPE. Set TYPE_MIN_VALUE and TYPE_MAX_VALUE
2548 for TYPE, based on the PRECISION and whether or not the TYPE
2549 IS_UNSIGNED. PRECISION need not correspond to a width supported
2550 natively by the hardware; for example, on a machine with 8-bit,
2551 16-bit, and 32-bit register modes, PRECISION might be 7, 23, or
2552 61. */
2554 void
2555 set_min_and_max_values_for_integral_type (tree type,
2556 int precision,
2557 bool is_unsigned)
2559 tree min_value;
2560 tree max_value;
2562 if (is_unsigned)
2564 min_value = build_int_cst (type, 0);
2565 max_value
2566 = build_int_cst_wide (type, precision - HOST_BITS_PER_WIDE_INT >= 0
2567 ? -1
2568 : ((HOST_WIDE_INT) 1 << precision) - 1,
2569 precision - HOST_BITS_PER_WIDE_INT > 0
2570 ? ((unsigned HOST_WIDE_INT) ~0
2571 >> (HOST_BITS_PER_WIDE_INT
2572 - (precision - HOST_BITS_PER_WIDE_INT)))
2573 : 0);
2575 else
2577 min_value
2578 = build_int_cst_wide (type,
2579 (precision - HOST_BITS_PER_WIDE_INT > 0
2581 : (HOST_WIDE_INT) (-1) << (precision - 1)),
2582 (((HOST_WIDE_INT) (-1)
2583 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
2584 ? precision - HOST_BITS_PER_WIDE_INT - 1
2585 : 0))));
2586 max_value
2587 = build_int_cst_wide (type,
2588 (precision - HOST_BITS_PER_WIDE_INT > 0
2589 ? -1
2590 : (HOST_WIDE_INT)
2591 (((unsigned HOST_WIDE_INT) 1
2592 << (precision - 1)) - 1)),
2593 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
2594 ? (HOST_WIDE_INT)
2595 ((((unsigned HOST_WIDE_INT) 1
2596 << (precision - HOST_BITS_PER_WIDE_INT
2597 - 1))) - 1)
2598 : 0));
2601 TYPE_MIN_VALUE (type) = min_value;
2602 TYPE_MAX_VALUE (type) = max_value;
2605 /* Set the extreme values of TYPE based on its precision in bits,
2606 then lay it out. Used when make_signed_type won't do
2607 because the tree code is not INTEGER_TYPE.
2608 E.g. for Pascal, when the -fsigned-char option is given. */
2610 void
2611 fixup_signed_type (tree type)
2613 int precision = TYPE_PRECISION (type);
2615 /* We can not represent properly constants greater then
2616 HOST_BITS_PER_DOUBLE_INT, still we need the types
2617 as they are used by i386 vector extensions and friends. */
2618 if (precision > HOST_BITS_PER_DOUBLE_INT)
2619 precision = HOST_BITS_PER_DOUBLE_INT;
2621 set_min_and_max_values_for_integral_type (type, precision,
2622 /*is_unsigned=*/false);
2624 /* Lay out the type: set its alignment, size, etc. */
2625 layout_type (type);
2628 /* Set the extreme values of TYPE based on its precision in bits,
2629 then lay it out. This is used both in `make_unsigned_type'
2630 and for enumeral types. */
2632 void
2633 fixup_unsigned_type (tree type)
2635 int precision = TYPE_PRECISION (type);
2637 /* We can not represent properly constants greater then
2638 HOST_BITS_PER_DOUBLE_INT, still we need the types
2639 as they are used by i386 vector extensions and friends. */
2640 if (precision > HOST_BITS_PER_DOUBLE_INT)
2641 precision = HOST_BITS_PER_DOUBLE_INT;
2643 TYPE_UNSIGNED (type) = 1;
2645 set_min_and_max_values_for_integral_type (type, precision,
2646 /*is_unsigned=*/true);
2648 /* Lay out the type: set its alignment, size, etc. */
2649 layout_type (type);
2652 /* Construct an iterator for a bitfield that spans BITSIZE bits,
2653 starting at BITPOS.
2655 BITREGION_START is the bit position of the first bit in this
2656 sequence of bit fields. BITREGION_END is the last bit in this
2657 sequence. If these two fields are non-zero, we should restrict the
2658 memory access to that range. Otherwise, we are allowed to touch
2659 any adjacent non bit-fields.
2661 ALIGN is the alignment of the underlying object in bits.
2662 VOLATILEP says whether the bitfield is volatile. */
2664 bit_field_mode_iterator
2665 ::bit_field_mode_iterator (HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
2666 HOST_WIDE_INT bitregion_start,
2667 HOST_WIDE_INT bitregion_end,
2668 unsigned int align, bool volatilep)
2669 : mode_ (GET_CLASS_NARROWEST_MODE (MODE_INT)), bitsize_ (bitsize),
2670 bitpos_ (bitpos), bitregion_start_ (bitregion_start),
2671 bitregion_end_ (bitregion_end), align_ (align),
2672 volatilep_ (volatilep), count_ (0)
2674 if (!bitregion_end_)
2676 /* We can assume that any aligned chunk of ALIGN bits that overlaps
2677 the bitfield is mapped and won't trap, provided that ALIGN isn't
2678 too large. The cap is the biggest required alignment for data,
2679 or at least the word size. And force one such chunk at least. */
2680 unsigned HOST_WIDE_INT units
2681 = MIN (align, MAX (BIGGEST_ALIGNMENT, BITS_PER_WORD));
2682 if (bitsize <= 0)
2683 bitsize = 1;
2684 bitregion_end_ = bitpos + bitsize + units - 1;
2685 bitregion_end_ -= bitregion_end_ % units + 1;
2689 /* Calls to this function return successively larger modes that can be used
2690 to represent the bitfield. Return true if another bitfield mode is
2691 available, storing it in *OUT_MODE if so. */
2693 bool
2694 bit_field_mode_iterator::next_mode (enum machine_mode *out_mode)
2696 for (; mode_ != VOIDmode; mode_ = GET_MODE_WIDER_MODE (mode_))
2698 unsigned int unit = GET_MODE_BITSIZE (mode_);
2700 /* Skip modes that don't have full precision. */
2701 if (unit != GET_MODE_PRECISION (mode_))
2702 continue;
2704 /* Stop if the mode is too wide to handle efficiently. */
2705 if (unit > MAX_FIXED_MODE_SIZE)
2706 break;
2708 /* Don't deliver more than one multiword mode; the smallest one
2709 should be used. */
2710 if (count_ > 0 && unit > BITS_PER_WORD)
2711 break;
2713 /* Skip modes that are too small. */
2714 unsigned HOST_WIDE_INT substart = (unsigned HOST_WIDE_INT) bitpos_ % unit;
2715 unsigned HOST_WIDE_INT subend = substart + bitsize_;
2716 if (subend > unit)
2717 continue;
2719 /* Stop if the mode goes outside the bitregion. */
2720 HOST_WIDE_INT start = bitpos_ - substart;
2721 if (bitregion_start_ && start < bitregion_start_)
2722 break;
2723 HOST_WIDE_INT end = start + unit;
2724 if (end > bitregion_end_ + 1)
2725 break;
2727 /* Stop if the mode requires too much alignment. */
2728 if (GET_MODE_ALIGNMENT (mode_) > align_
2729 && SLOW_UNALIGNED_ACCESS (mode_, align_))
2730 break;
2732 *out_mode = mode_;
2733 mode_ = GET_MODE_WIDER_MODE (mode_);
2734 count_++;
2735 return true;
2737 return false;
2740 /* Return true if smaller modes are generally preferred for this kind
2741 of bitfield. */
2743 bool
2744 bit_field_mode_iterator::prefer_smaller_modes ()
2746 return (volatilep_
2747 ? targetm.narrow_volatile_bitfield ()
2748 : !SLOW_BYTE_ACCESS);
2751 /* Find the best machine mode to use when referencing a bit field of length
2752 BITSIZE bits starting at BITPOS.
2754 BITREGION_START is the bit position of the first bit in this
2755 sequence of bit fields. BITREGION_END is the last bit in this
2756 sequence. If these two fields are non-zero, we should restrict the
2757 memory access to that range. Otherwise, we are allowed to touch
2758 any adjacent non bit-fields.
2760 The underlying object is known to be aligned to a boundary of ALIGN bits.
2761 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
2762 larger than LARGEST_MODE (usually SImode).
2764 If no mode meets all these conditions, we return VOIDmode.
2766 If VOLATILEP is false and SLOW_BYTE_ACCESS is false, we return the
2767 smallest mode meeting these conditions.
2769 If VOLATILEP is false and SLOW_BYTE_ACCESS is true, we return the
2770 largest mode (but a mode no wider than UNITS_PER_WORD) that meets
2771 all the conditions.
2773 If VOLATILEP is true the narrow_volatile_bitfields target hook is used to
2774 decide which of the above modes should be used. */
2776 enum machine_mode
2777 get_best_mode (int bitsize, int bitpos,
2778 unsigned HOST_WIDE_INT bitregion_start,
2779 unsigned HOST_WIDE_INT bitregion_end,
2780 unsigned int align,
2781 enum machine_mode largest_mode, bool volatilep)
2783 bit_field_mode_iterator iter (bitsize, bitpos, bitregion_start,
2784 bitregion_end, align, volatilep);
2785 enum machine_mode widest_mode = VOIDmode;
2786 enum machine_mode mode;
2787 while (iter.next_mode (&mode)
2788 /* ??? For historical reasons, reject modes that would normally
2789 receive greater alignment, even if unaligned accesses are
2790 acceptable. This has both advantages and disadvantages.
2791 Removing this check means that something like:
2793 struct s { unsigned int x; unsigned int y; };
2794 int f (struct s *s) { return s->x == 0 && s->y == 0; }
2796 can be implemented using a single load and compare on
2797 64-bit machines that have no alignment restrictions.
2798 For example, on powerpc64-linux-gnu, we would generate:
2800 ld 3,0(3)
2801 cntlzd 3,3
2802 srdi 3,3,6
2805 rather than:
2807 lwz 9,0(3)
2808 cmpwi 7,9,0
2809 bne 7,.L3
2810 lwz 3,4(3)
2811 cntlzw 3,3
2812 srwi 3,3,5
2813 extsw 3,3
2815 .p2align 4,,15
2816 .L3:
2817 li 3,0
2820 However, accessing more than one field can make life harder
2821 for the gimple optimizers. For example, gcc.dg/vect/bb-slp-5.c
2822 has a series of unsigned short copies followed by a series of
2823 unsigned short comparisons. With this check, both the copies
2824 and comparisons remain 16-bit accesses and FRE is able
2825 to eliminate the latter. Without the check, the comparisons
2826 can be done using 2 64-bit operations, which FRE isn't able
2827 to handle in the same way.
2829 Either way, it would probably be worth disabling this check
2830 during expand. One particular example where removing the
2831 check would help is the get_best_mode call in store_bit_field.
2832 If we are given a memory bitregion of 128 bits that is aligned
2833 to a 64-bit boundary, and the bitfield we want to modify is
2834 in the second half of the bitregion, this check causes
2835 store_bitfield to turn the memory into a 64-bit reference
2836 to the _first_ half of the region. We later use
2837 adjust_bitfield_address to get a reference to the correct half,
2838 but doing so looks to adjust_bitfield_address as though we are
2839 moving past the end of the original object, so it drops the
2840 associated MEM_EXPR and MEM_OFFSET. Removing the check
2841 causes store_bit_field to keep a 128-bit memory reference,
2842 so that the final bitfield reference still has a MEM_EXPR
2843 and MEM_OFFSET. */
2844 && GET_MODE_ALIGNMENT (mode) <= align
2845 && (largest_mode == VOIDmode
2846 || GET_MODE_SIZE (mode) <= GET_MODE_SIZE (largest_mode)))
2848 widest_mode = mode;
2849 if (iter.prefer_smaller_modes ())
2850 break;
2852 return widest_mode;
2855 /* Gets minimal and maximal values for MODE (signed or unsigned depending on
2856 SIGN). The returned constants are made to be usable in TARGET_MODE. */
2858 void
2859 get_mode_bounds (enum machine_mode mode, int sign,
2860 enum machine_mode target_mode,
2861 rtx *mmin, rtx *mmax)
2863 unsigned size = GET_MODE_BITSIZE (mode);
2864 unsigned HOST_WIDE_INT min_val, max_val;
2866 gcc_assert (size <= HOST_BITS_PER_WIDE_INT);
2868 if (sign)
2870 min_val = -((unsigned HOST_WIDE_INT) 1 << (size - 1));
2871 max_val = ((unsigned HOST_WIDE_INT) 1 << (size - 1)) - 1;
2873 else
2875 min_val = 0;
2876 max_val = ((unsigned HOST_WIDE_INT) 1 << (size - 1) << 1) - 1;
2879 *mmin = gen_int_mode (min_val, target_mode);
2880 *mmax = gen_int_mode (max_val, target_mode);
2883 #include "gt-stor-layout.h"