re PR libfortran/59513 (Fortran runtime error: Sequential READ or WRITE not allowed...
[official-gcc.git] / gcc / stor-layout.c
blobf18f1ac5273df551c4373593d0e576dd528ea4c1
1 /* C-compiler utilities for types and variables storage layout
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
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 "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "stor-layout.h"
37 #include "stringpool.h"
38 #include "varasm.h"
39 #include "print-tree.h"
40 #include "rtl.h"
41 #include "tm_p.h"
42 #include "flags.h"
43 #include "hard-reg-set.h"
44 #include "function.h"
45 #include "hashtab.h"
46 #include "statistics.h"
47 #include "real.h"
48 #include "fixed-value.h"
49 #include "insn-config.h"
50 #include "expmed.h"
51 #include "dojump.h"
52 #include "explow.h"
53 #include "calls.h"
54 #include "emit-rtl.h"
55 #include "stmt.h"
56 #include "expr.h"
57 #include "diagnostic-core.h"
58 #include "target.h"
59 #include "langhooks.h"
60 #include "regs.h"
61 #include "params.h"
62 #include "hash-map.h"
63 #include "is-a.h"
64 #include "plugin-api.h"
65 #include "ipa-ref.h"
66 #include "cgraph.h"
67 #include "tree-inline.h"
68 #include "tree-dump.h"
69 #include "gimplify.h"
71 /* Data type for the expressions representing sizes of data types.
72 It is the first integer type laid out. */
73 tree sizetype_tab[(int) stk_type_kind_last];
75 /* If nonzero, this is an upper limit on alignment of structure fields.
76 The value is measured in bits. */
77 unsigned int maximum_field_alignment = TARGET_DEFAULT_PACK_STRUCT * BITS_PER_UNIT;
79 /* Nonzero if all REFERENCE_TYPEs are internal and hence should be allocated
80 in the address spaces' address_mode, not pointer_mode. Set only by
81 internal_reference_types called only by a front end. */
82 static int reference_types_internal = 0;
84 static tree self_referential_size (tree);
85 static void finalize_record_size (record_layout_info);
86 static void finalize_type_size (tree);
87 static void place_union_field (record_layout_info, tree);
88 #if defined (PCC_BITFIELD_TYPE_MATTERS) || defined (BITFIELD_NBYTES_LIMITED)
89 static int excess_unit_span (HOST_WIDE_INT, HOST_WIDE_INT, HOST_WIDE_INT,
90 HOST_WIDE_INT, tree);
91 #endif
92 extern void debug_rli (record_layout_info);
94 /* Show that REFERENCE_TYPES are internal and should use address_mode.
95 Called only by front end. */
97 void
98 internal_reference_types (void)
100 reference_types_internal = 1;
103 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
104 to serve as the actual size-expression for a type or decl. */
106 tree
107 variable_size (tree size)
109 /* Obviously. */
110 if (TREE_CONSTANT (size))
111 return size;
113 /* If the size is self-referential, we can't make a SAVE_EXPR (see
114 save_expr for the rationale). But we can do something else. */
115 if (CONTAINS_PLACEHOLDER_P (size))
116 return self_referential_size (size);
118 /* If we are in the global binding level, we can't make a SAVE_EXPR
119 since it may end up being shared across functions, so it is up
120 to the front-end to deal with this case. */
121 if (lang_hooks.decls.global_bindings_p ())
122 return size;
124 return save_expr (size);
127 /* An array of functions used for self-referential size computation. */
128 static GTY(()) vec<tree, va_gc> *size_functions;
130 /* Similar to copy_tree_r but do not copy component references involving
131 PLACEHOLDER_EXPRs. These nodes are spotted in find_placeholder_in_expr
132 and substituted in substitute_in_expr. */
134 static tree
135 copy_self_referential_tree_r (tree *tp, int *walk_subtrees, void *data)
137 enum tree_code code = TREE_CODE (*tp);
139 /* Stop at types, decls, constants like copy_tree_r. */
140 if (TREE_CODE_CLASS (code) == tcc_type
141 || TREE_CODE_CLASS (code) == tcc_declaration
142 || TREE_CODE_CLASS (code) == tcc_constant)
144 *walk_subtrees = 0;
145 return NULL_TREE;
148 /* This is the pattern built in ada/make_aligning_type. */
149 else if (code == ADDR_EXPR
150 && TREE_CODE (TREE_OPERAND (*tp, 0)) == PLACEHOLDER_EXPR)
152 *walk_subtrees = 0;
153 return NULL_TREE;
156 /* Default case: the component reference. */
157 else if (code == COMPONENT_REF)
159 tree inner;
160 for (inner = TREE_OPERAND (*tp, 0);
161 REFERENCE_CLASS_P (inner);
162 inner = TREE_OPERAND (inner, 0))
165 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
167 *walk_subtrees = 0;
168 return NULL_TREE;
172 /* We're not supposed to have them in self-referential size trees
173 because we wouldn't properly control when they are evaluated.
174 However, not creating superfluous SAVE_EXPRs requires accurate
175 tracking of readonly-ness all the way down to here, which we
176 cannot always guarantee in practice. So punt in this case. */
177 else if (code == SAVE_EXPR)
178 return error_mark_node;
180 else if (code == STATEMENT_LIST)
181 gcc_unreachable ();
183 return copy_tree_r (tp, walk_subtrees, data);
186 /* Given a SIZE expression that is self-referential, return an equivalent
187 expression to serve as the actual size expression for a type. */
189 static tree
190 self_referential_size (tree size)
192 static unsigned HOST_WIDE_INT fnno = 0;
193 vec<tree> self_refs = vNULL;
194 tree param_type_list = NULL, param_decl_list = NULL;
195 tree t, ref, return_type, fntype, fnname, fndecl;
196 unsigned int i;
197 char buf[128];
198 vec<tree, va_gc> *args = NULL;
200 /* Do not factor out simple operations. */
201 t = skip_simple_constant_arithmetic (size);
202 if (TREE_CODE (t) == CALL_EXPR)
203 return size;
205 /* Collect the list of self-references in the expression. */
206 find_placeholder_in_expr (size, &self_refs);
207 gcc_assert (self_refs.length () > 0);
209 /* Obtain a private copy of the expression. */
210 t = size;
211 if (walk_tree (&t, copy_self_referential_tree_r, NULL, NULL) != NULL_TREE)
212 return size;
213 size = t;
215 /* Build the parameter and argument lists in parallel; also
216 substitute the former for the latter in the expression. */
217 vec_alloc (args, self_refs.length ());
218 FOR_EACH_VEC_ELT (self_refs, i, ref)
220 tree subst, param_name, param_type, param_decl;
222 if (DECL_P (ref))
224 /* We shouldn't have true variables here. */
225 gcc_assert (TREE_READONLY (ref));
226 subst = ref;
228 /* This is the pattern built in ada/make_aligning_type. */
229 else if (TREE_CODE (ref) == ADDR_EXPR)
230 subst = ref;
231 /* Default case: the component reference. */
232 else
233 subst = TREE_OPERAND (ref, 1);
235 sprintf (buf, "p%d", i);
236 param_name = get_identifier (buf);
237 param_type = TREE_TYPE (ref);
238 param_decl
239 = build_decl (input_location, PARM_DECL, param_name, param_type);
240 DECL_ARG_TYPE (param_decl) = param_type;
241 DECL_ARTIFICIAL (param_decl) = 1;
242 TREE_READONLY (param_decl) = 1;
244 size = substitute_in_expr (size, subst, param_decl);
246 param_type_list = tree_cons (NULL_TREE, param_type, param_type_list);
247 param_decl_list = chainon (param_decl, param_decl_list);
248 args->quick_push (ref);
251 self_refs.release ();
253 /* Append 'void' to indicate that the number of parameters is fixed. */
254 param_type_list = tree_cons (NULL_TREE, void_type_node, param_type_list);
256 /* The 3 lists have been created in reverse order. */
257 param_type_list = nreverse (param_type_list);
258 param_decl_list = nreverse (param_decl_list);
260 /* Build the function type. */
261 return_type = TREE_TYPE (size);
262 fntype = build_function_type (return_type, param_type_list);
264 /* Build the function declaration. */
265 sprintf (buf, "SZ"HOST_WIDE_INT_PRINT_UNSIGNED, fnno++);
266 fnname = get_file_function_name (buf);
267 fndecl = build_decl (input_location, FUNCTION_DECL, fnname, fntype);
268 for (t = param_decl_list; t; t = DECL_CHAIN (t))
269 DECL_CONTEXT (t) = fndecl;
270 DECL_ARGUMENTS (fndecl) = param_decl_list;
271 DECL_RESULT (fndecl)
272 = build_decl (input_location, RESULT_DECL, 0, return_type);
273 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
275 /* The function has been created by the compiler and we don't
276 want to emit debug info for it. */
277 DECL_ARTIFICIAL (fndecl) = 1;
278 DECL_IGNORED_P (fndecl) = 1;
280 /* It is supposed to be "const" and never throw. */
281 TREE_READONLY (fndecl) = 1;
282 TREE_NOTHROW (fndecl) = 1;
284 /* We want it to be inlined when this is deemed profitable, as
285 well as discarded if every call has been integrated. */
286 DECL_DECLARED_INLINE_P (fndecl) = 1;
288 /* It is made up of a unique return statement. */
289 DECL_INITIAL (fndecl) = make_node (BLOCK);
290 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
291 t = build2 (MODIFY_EXPR, return_type, DECL_RESULT (fndecl), size);
292 DECL_SAVED_TREE (fndecl) = build1 (RETURN_EXPR, void_type_node, t);
293 TREE_STATIC (fndecl) = 1;
295 /* Put it onto the list of size functions. */
296 vec_safe_push (size_functions, fndecl);
298 /* Replace the original expression with a call to the size function. */
299 return build_call_expr_loc_vec (UNKNOWN_LOCATION, fndecl, args);
302 /* Take, queue and compile all the size functions. It is essential that
303 the size functions be gimplified at the very end of the compilation
304 in order to guarantee transparent handling of self-referential sizes.
305 Otherwise the GENERIC inliner would not be able to inline them back
306 at each of their call sites, thus creating artificial non-constant
307 size expressions which would trigger nasty problems later on. */
309 void
310 finalize_size_functions (void)
312 unsigned int i;
313 tree fndecl;
315 for (i = 0; size_functions && size_functions->iterate (i, &fndecl); i++)
317 allocate_struct_function (fndecl, false);
318 set_cfun (NULL);
319 dump_function (TDI_original, fndecl);
320 gimplify_function_tree (fndecl);
321 dump_function (TDI_generic, fndecl);
322 cgraph_node::finalize_function (fndecl, false);
325 vec_free (size_functions);
328 /* Return the machine mode to use for a nonscalar of SIZE bits. The
329 mode must be in class MCLASS, and have exactly that many value bits;
330 it may have padding as well. If LIMIT is nonzero, modes of wider
331 than MAX_FIXED_MODE_SIZE will not be used. */
333 machine_mode
334 mode_for_size (unsigned int size, enum mode_class mclass, int limit)
336 machine_mode mode;
337 int i;
339 if (limit && size > MAX_FIXED_MODE_SIZE)
340 return BLKmode;
342 /* Get the first mode which has this size, in the specified class. */
343 for (mode = GET_CLASS_NARROWEST_MODE (mclass); mode != VOIDmode;
344 mode = GET_MODE_WIDER_MODE (mode))
345 if (GET_MODE_PRECISION (mode) == size)
346 return mode;
348 if (mclass == MODE_INT || mclass == MODE_PARTIAL_INT)
349 for (i = 0; i < NUM_INT_N_ENTS; i ++)
350 if (int_n_data[i].bitsize == size
351 && int_n_enabled_p[i])
352 return int_n_data[i].m;
354 return BLKmode;
357 /* Similar, except passed a tree node. */
359 machine_mode
360 mode_for_size_tree (const_tree size, enum mode_class mclass, int limit)
362 unsigned HOST_WIDE_INT uhwi;
363 unsigned int ui;
365 if (!tree_fits_uhwi_p (size))
366 return BLKmode;
367 uhwi = tree_to_uhwi (size);
368 ui = uhwi;
369 if (uhwi != ui)
370 return BLKmode;
371 return mode_for_size (ui, mclass, limit);
374 /* Similar, but never return BLKmode; return the narrowest mode that
375 contains at least the requested number of value bits. */
377 machine_mode
378 smallest_mode_for_size (unsigned int size, enum mode_class mclass)
380 machine_mode mode = VOIDmode;
381 int i;
383 /* Get the first mode which has at least this size, in the
384 specified class. */
385 for (mode = GET_CLASS_NARROWEST_MODE (mclass); mode != VOIDmode;
386 mode = GET_MODE_WIDER_MODE (mode))
387 if (GET_MODE_PRECISION (mode) >= size)
388 break;
390 if (mclass == MODE_INT || mclass == MODE_PARTIAL_INT)
391 for (i = 0; i < NUM_INT_N_ENTS; i ++)
392 if (int_n_data[i].bitsize >= size
393 && int_n_data[i].bitsize < GET_MODE_PRECISION (mode)
394 && int_n_enabled_p[i])
395 mode = int_n_data[i].m;
397 if (mode == VOIDmode)
398 gcc_unreachable ();
400 return mode;
403 /* Find an integer mode of the exact same size, or BLKmode on failure. */
405 machine_mode
406 int_mode_for_mode (machine_mode mode)
408 switch (GET_MODE_CLASS (mode))
410 case MODE_INT:
411 case MODE_PARTIAL_INT:
412 break;
414 case MODE_COMPLEX_INT:
415 case MODE_COMPLEX_FLOAT:
416 case MODE_FLOAT:
417 case MODE_DECIMAL_FLOAT:
418 case MODE_VECTOR_INT:
419 case MODE_VECTOR_FLOAT:
420 case MODE_FRACT:
421 case MODE_ACCUM:
422 case MODE_UFRACT:
423 case MODE_UACCUM:
424 case MODE_VECTOR_FRACT:
425 case MODE_VECTOR_ACCUM:
426 case MODE_VECTOR_UFRACT:
427 case MODE_VECTOR_UACCUM:
428 case MODE_POINTER_BOUNDS:
429 mode = mode_for_size (GET_MODE_BITSIZE (mode), MODE_INT, 0);
430 break;
432 case MODE_RANDOM:
433 if (mode == BLKmode)
434 break;
436 /* ... fall through ... */
438 case MODE_CC:
439 default:
440 gcc_unreachable ();
443 return mode;
446 /* Find a mode that can be used for efficient bitwise operations on MODE.
447 Return BLKmode if no such mode exists. */
449 machine_mode
450 bitwise_mode_for_mode (machine_mode mode)
452 /* Quick exit if we already have a suitable mode. */
453 unsigned int bitsize = GET_MODE_BITSIZE (mode);
454 if (SCALAR_INT_MODE_P (mode) && bitsize <= MAX_FIXED_MODE_SIZE)
455 return mode;
457 /* Reuse the sanity checks from int_mode_for_mode. */
458 gcc_checking_assert ((int_mode_for_mode (mode), true));
460 /* Try to replace complex modes with complex modes. In general we
461 expect both components to be processed independently, so we only
462 care whether there is a register for the inner mode. */
463 if (COMPLEX_MODE_P (mode))
465 machine_mode trial = mode;
466 if (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT)
467 trial = mode_for_size (bitsize, MODE_COMPLEX_INT, false);
468 if (trial != BLKmode
469 && have_regs_of_mode[GET_MODE_INNER (trial)])
470 return trial;
473 /* Try to replace vector modes with vector modes. Also try using vector
474 modes if an integer mode would be too big. */
475 if (VECTOR_MODE_P (mode) || bitsize > MAX_FIXED_MODE_SIZE)
477 machine_mode trial = mode;
478 if (GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
479 trial = mode_for_size (bitsize, MODE_VECTOR_INT, 0);
480 if (trial != BLKmode
481 && have_regs_of_mode[trial]
482 && targetm.vector_mode_supported_p (trial))
483 return trial;
486 /* Otherwise fall back on integers while honoring MAX_FIXED_MODE_SIZE. */
487 return mode_for_size (bitsize, MODE_INT, true);
490 /* Find a type that can be used for efficient bitwise operations on MODE.
491 Return null if no such mode exists. */
493 tree
494 bitwise_type_for_mode (machine_mode mode)
496 mode = bitwise_mode_for_mode (mode);
497 if (mode == BLKmode)
498 return NULL_TREE;
500 unsigned int inner_size = GET_MODE_UNIT_BITSIZE (mode);
501 tree inner_type = build_nonstandard_integer_type (inner_size, true);
503 if (VECTOR_MODE_P (mode))
504 return build_vector_type_for_mode (inner_type, mode);
506 if (COMPLEX_MODE_P (mode))
507 return build_complex_type (inner_type);
509 gcc_checking_assert (GET_MODE_INNER (mode) == VOIDmode);
510 return inner_type;
513 /* Find a mode that is suitable for representing a vector with
514 NUNITS elements of mode INNERMODE. Returns BLKmode if there
515 is no suitable mode. */
517 machine_mode
518 mode_for_vector (machine_mode innermode, unsigned nunits)
520 machine_mode mode;
522 /* First, look for a supported vector type. */
523 if (SCALAR_FLOAT_MODE_P (innermode))
524 mode = MIN_MODE_VECTOR_FLOAT;
525 else if (SCALAR_FRACT_MODE_P (innermode))
526 mode = MIN_MODE_VECTOR_FRACT;
527 else if (SCALAR_UFRACT_MODE_P (innermode))
528 mode = MIN_MODE_VECTOR_UFRACT;
529 else if (SCALAR_ACCUM_MODE_P (innermode))
530 mode = MIN_MODE_VECTOR_ACCUM;
531 else if (SCALAR_UACCUM_MODE_P (innermode))
532 mode = MIN_MODE_VECTOR_UACCUM;
533 else
534 mode = MIN_MODE_VECTOR_INT;
536 /* Do not check vector_mode_supported_p here. We'll do that
537 later in vector_type_mode. */
538 for (; mode != VOIDmode ; mode = GET_MODE_WIDER_MODE (mode))
539 if (GET_MODE_NUNITS (mode) == nunits
540 && GET_MODE_INNER (mode) == innermode)
541 break;
543 /* For integers, try mapping it to a same-sized scalar mode. */
544 if (mode == VOIDmode
545 && GET_MODE_CLASS (innermode) == MODE_INT)
546 mode = mode_for_size (nunits * GET_MODE_BITSIZE (innermode),
547 MODE_INT, 0);
549 if (mode == VOIDmode
550 || (GET_MODE_CLASS (mode) == MODE_INT
551 && !have_regs_of_mode[mode]))
552 return BLKmode;
554 return mode;
557 /* Return the alignment of MODE. This will be bounded by 1 and
558 BIGGEST_ALIGNMENT. */
560 unsigned int
561 get_mode_alignment (machine_mode mode)
563 return MIN (BIGGEST_ALIGNMENT, MAX (1, mode_base_align[mode]*BITS_PER_UNIT));
566 /* Return the precision of the mode, or for a complex or vector mode the
567 precision of the mode of its elements. */
569 unsigned int
570 element_precision (machine_mode mode)
572 if (COMPLEX_MODE_P (mode) || VECTOR_MODE_P (mode))
573 mode = GET_MODE_INNER (mode);
575 return GET_MODE_PRECISION (mode);
578 /* Return the natural mode of an array, given that it is SIZE bytes in
579 total and has elements of type ELEM_TYPE. */
581 static machine_mode
582 mode_for_array (tree elem_type, tree size)
584 tree elem_size;
585 unsigned HOST_WIDE_INT int_size, int_elem_size;
586 bool limit_p;
588 /* One-element arrays get the component type's mode. */
589 elem_size = TYPE_SIZE (elem_type);
590 if (simple_cst_equal (size, elem_size))
591 return TYPE_MODE (elem_type);
593 limit_p = true;
594 if (tree_fits_uhwi_p (size) && tree_fits_uhwi_p (elem_size))
596 int_size = tree_to_uhwi (size);
597 int_elem_size = tree_to_uhwi (elem_size);
598 if (int_elem_size > 0
599 && int_size % int_elem_size == 0
600 && targetm.array_mode_supported_p (TYPE_MODE (elem_type),
601 int_size / int_elem_size))
602 limit_p = false;
604 return mode_for_size_tree (size, MODE_INT, limit_p);
607 /* Subroutine of layout_decl: Force alignment required for the data type.
608 But if the decl itself wants greater alignment, don't override that. */
610 static inline void
611 do_type_align (tree type, tree decl)
613 if (TYPE_ALIGN (type) > DECL_ALIGN (decl))
615 DECL_ALIGN (decl) = TYPE_ALIGN (type);
616 if (TREE_CODE (decl) == FIELD_DECL)
617 DECL_USER_ALIGN (decl) = TYPE_USER_ALIGN (type);
621 /* Set the size, mode and alignment of a ..._DECL node.
622 TYPE_DECL does need this for C++.
623 Note that LABEL_DECL and CONST_DECL nodes do not need this,
624 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
625 Don't call layout_decl for them.
627 KNOWN_ALIGN is the amount of alignment we can assume this
628 decl has with no special effort. It is relevant only for FIELD_DECLs
629 and depends on the previous fields.
630 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
631 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
632 the record will be aligned to suit. */
634 void
635 layout_decl (tree decl, unsigned int known_align)
637 tree type = TREE_TYPE (decl);
638 enum tree_code code = TREE_CODE (decl);
639 rtx rtl = NULL_RTX;
640 location_t loc = DECL_SOURCE_LOCATION (decl);
642 if (code == CONST_DECL)
643 return;
645 gcc_assert (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL
646 || code == TYPE_DECL ||code == FIELD_DECL);
648 rtl = DECL_RTL_IF_SET (decl);
650 if (type == error_mark_node)
651 type = void_type_node;
653 /* Usually the size and mode come from the data type without change,
654 however, the front-end may set the explicit width of the field, so its
655 size may not be the same as the size of its type. This happens with
656 bitfields, of course (an `int' bitfield may be only 2 bits, say), but it
657 also happens with other fields. For example, the C++ front-end creates
658 zero-sized fields corresponding to empty base classes, and depends on
659 layout_type setting DECL_FIELD_BITPOS correctly for the field. Set the
660 size in bytes from the size in bits. If we have already set the mode,
661 don't set it again since we can be called twice for FIELD_DECLs. */
663 DECL_UNSIGNED (decl) = TYPE_UNSIGNED (type);
664 if (DECL_MODE (decl) == VOIDmode)
665 DECL_MODE (decl) = TYPE_MODE (type);
667 if (DECL_SIZE (decl) == 0)
669 DECL_SIZE (decl) = TYPE_SIZE (type);
670 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
672 else if (DECL_SIZE_UNIT (decl) == 0)
673 DECL_SIZE_UNIT (decl)
674 = fold_convert_loc (loc, sizetype,
675 size_binop_loc (loc, CEIL_DIV_EXPR, DECL_SIZE (decl),
676 bitsize_unit_node));
678 if (code != FIELD_DECL)
679 /* For non-fields, update the alignment from the type. */
680 do_type_align (type, decl);
681 else
682 /* For fields, it's a bit more complicated... */
684 bool old_user_align = DECL_USER_ALIGN (decl);
685 bool zero_bitfield = false;
686 bool packed_p = DECL_PACKED (decl);
687 unsigned int mfa;
689 if (DECL_BIT_FIELD (decl))
691 DECL_BIT_FIELD_TYPE (decl) = type;
693 /* A zero-length bit-field affects the alignment of the next
694 field. In essence such bit-fields are not influenced by
695 any packing due to #pragma pack or attribute packed. */
696 if (integer_zerop (DECL_SIZE (decl))
697 && ! targetm.ms_bitfield_layout_p (DECL_FIELD_CONTEXT (decl)))
699 zero_bitfield = true;
700 packed_p = false;
701 #ifdef PCC_BITFIELD_TYPE_MATTERS
702 if (PCC_BITFIELD_TYPE_MATTERS)
703 do_type_align (type, decl);
704 else
705 #endif
707 #ifdef EMPTY_FIELD_BOUNDARY
708 if (EMPTY_FIELD_BOUNDARY > DECL_ALIGN (decl))
710 DECL_ALIGN (decl) = EMPTY_FIELD_BOUNDARY;
711 DECL_USER_ALIGN (decl) = 0;
713 #endif
717 /* See if we can use an ordinary integer mode for a bit-field.
718 Conditions are: a fixed size that is correct for another mode,
719 occupying a complete byte or bytes on proper boundary. */
720 if (TYPE_SIZE (type) != 0
721 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
722 && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
724 machine_mode xmode
725 = mode_for_size_tree (DECL_SIZE (decl), MODE_INT, 1);
726 unsigned int xalign = GET_MODE_ALIGNMENT (xmode);
728 if (xmode != BLKmode
729 && !(xalign > BITS_PER_UNIT && DECL_PACKED (decl))
730 && (known_align == 0 || known_align >= xalign))
732 DECL_ALIGN (decl) = MAX (xalign, DECL_ALIGN (decl));
733 DECL_MODE (decl) = xmode;
734 DECL_BIT_FIELD (decl) = 0;
738 /* Turn off DECL_BIT_FIELD if we won't need it set. */
739 if (TYPE_MODE (type) == BLKmode && DECL_MODE (decl) == BLKmode
740 && known_align >= TYPE_ALIGN (type)
741 && DECL_ALIGN (decl) >= TYPE_ALIGN (type))
742 DECL_BIT_FIELD (decl) = 0;
744 else if (packed_p && DECL_USER_ALIGN (decl))
745 /* Don't touch DECL_ALIGN. For other packed fields, go ahead and
746 round up; we'll reduce it again below. We want packing to
747 supersede USER_ALIGN inherited from the type, but defer to
748 alignment explicitly specified on the field decl. */;
749 else
750 do_type_align (type, decl);
752 /* If the field is packed and not explicitly aligned, give it the
753 minimum alignment. Note that do_type_align may set
754 DECL_USER_ALIGN, so we need to check old_user_align instead. */
755 if (packed_p
756 && !old_user_align)
757 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
759 if (! packed_p && ! DECL_USER_ALIGN (decl))
761 /* Some targets (i.e. i386, VMS) limit struct field alignment
762 to a lower boundary than alignment of variables unless
763 it was overridden by attribute aligned. */
764 #ifdef BIGGEST_FIELD_ALIGNMENT
765 DECL_ALIGN (decl)
766 = MIN (DECL_ALIGN (decl), (unsigned) BIGGEST_FIELD_ALIGNMENT);
767 #endif
768 #ifdef ADJUST_FIELD_ALIGN
769 DECL_ALIGN (decl) = ADJUST_FIELD_ALIGN (decl, DECL_ALIGN (decl));
770 #endif
773 if (zero_bitfield)
774 mfa = initial_max_fld_align * BITS_PER_UNIT;
775 else
776 mfa = maximum_field_alignment;
777 /* Should this be controlled by DECL_USER_ALIGN, too? */
778 if (mfa != 0)
779 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), mfa);
782 /* Evaluate nonconstant size only once, either now or as soon as safe. */
783 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
784 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
785 if (DECL_SIZE_UNIT (decl) != 0
786 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST)
787 DECL_SIZE_UNIT (decl) = variable_size (DECL_SIZE_UNIT (decl));
789 /* If requested, warn about definitions of large data objects. */
790 if (warn_larger_than
791 && (code == VAR_DECL || code == PARM_DECL)
792 && ! DECL_EXTERNAL (decl))
794 tree size = DECL_SIZE_UNIT (decl);
796 if (size != 0 && TREE_CODE (size) == INTEGER_CST
797 && compare_tree_int (size, larger_than_size) > 0)
799 int size_as_int = TREE_INT_CST_LOW (size);
801 if (compare_tree_int (size, size_as_int) == 0)
802 warning (OPT_Wlarger_than_, "size of %q+D is %d bytes", decl, size_as_int);
803 else
804 warning (OPT_Wlarger_than_, "size of %q+D is larger than %wd bytes",
805 decl, larger_than_size);
809 /* If the RTL was already set, update its mode and mem attributes. */
810 if (rtl)
812 PUT_MODE (rtl, DECL_MODE (decl));
813 SET_DECL_RTL (decl, 0);
814 set_mem_attributes (rtl, decl, 1);
815 SET_DECL_RTL (decl, rtl);
819 /* Given a VAR_DECL, PARM_DECL or RESULT_DECL, clears the results of
820 a previous call to layout_decl and calls it again. */
822 void
823 relayout_decl (tree decl)
825 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
826 DECL_MODE (decl) = VOIDmode;
827 if (!DECL_USER_ALIGN (decl))
828 DECL_ALIGN (decl) = 0;
829 SET_DECL_RTL (decl, 0);
831 layout_decl (decl, 0);
834 /* Begin laying out type T, which may be a RECORD_TYPE, UNION_TYPE, or
835 QUAL_UNION_TYPE. Return a pointer to a struct record_layout_info which
836 is to be passed to all other layout functions for this record. It is the
837 responsibility of the caller to call `free' for the storage returned.
838 Note that garbage collection is not permitted until we finish laying
839 out the record. */
841 record_layout_info
842 start_record_layout (tree t)
844 record_layout_info rli = XNEW (struct record_layout_info_s);
846 rli->t = t;
848 /* If the type has a minimum specified alignment (via an attribute
849 declaration, for example) use it -- otherwise, start with a
850 one-byte alignment. */
851 rli->record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (t));
852 rli->unpacked_align = rli->record_align;
853 rli->offset_align = MAX (rli->record_align, BIGGEST_ALIGNMENT);
855 #ifdef STRUCTURE_SIZE_BOUNDARY
856 /* Packed structures don't need to have minimum size. */
857 if (! TYPE_PACKED (t))
859 unsigned tmp;
861 /* #pragma pack overrides STRUCTURE_SIZE_BOUNDARY. */
862 tmp = (unsigned) STRUCTURE_SIZE_BOUNDARY;
863 if (maximum_field_alignment != 0)
864 tmp = MIN (tmp, maximum_field_alignment);
865 rli->record_align = MAX (rli->record_align, tmp);
867 #endif
869 rli->offset = size_zero_node;
870 rli->bitpos = bitsize_zero_node;
871 rli->prev_field = 0;
872 rli->pending_statics = 0;
873 rli->packed_maybe_necessary = 0;
874 rli->remaining_in_alignment = 0;
876 return rli;
879 /* Return the combined bit position for the byte offset OFFSET and the
880 bit position BITPOS.
882 These functions operate on byte and bit positions present in FIELD_DECLs
883 and assume that these expressions result in no (intermediate) overflow.
884 This assumption is necessary to fold the expressions as much as possible,
885 so as to avoid creating artificially variable-sized types in languages
886 supporting variable-sized types like Ada. */
888 tree
889 bit_from_pos (tree offset, tree bitpos)
891 if (TREE_CODE (offset) == PLUS_EXPR)
892 offset = size_binop (PLUS_EXPR,
893 fold_convert (bitsizetype, TREE_OPERAND (offset, 0)),
894 fold_convert (bitsizetype, TREE_OPERAND (offset, 1)));
895 else
896 offset = fold_convert (bitsizetype, offset);
897 return size_binop (PLUS_EXPR, bitpos,
898 size_binop (MULT_EXPR, offset, bitsize_unit_node));
901 /* Return the combined truncated byte position for the byte offset OFFSET and
902 the bit position BITPOS. */
904 tree
905 byte_from_pos (tree offset, tree bitpos)
907 tree bytepos;
908 if (TREE_CODE (bitpos) == MULT_EXPR
909 && tree_int_cst_equal (TREE_OPERAND (bitpos, 1), bitsize_unit_node))
910 bytepos = TREE_OPERAND (bitpos, 0);
911 else
912 bytepos = size_binop (TRUNC_DIV_EXPR, bitpos, bitsize_unit_node);
913 return size_binop (PLUS_EXPR, offset, fold_convert (sizetype, bytepos));
916 /* Split the bit position POS into a byte offset *POFFSET and a bit
917 position *PBITPOS with the byte offset aligned to OFF_ALIGN bits. */
919 void
920 pos_from_bit (tree *poffset, tree *pbitpos, unsigned int off_align,
921 tree pos)
923 tree toff_align = bitsize_int (off_align);
924 if (TREE_CODE (pos) == MULT_EXPR
925 && tree_int_cst_equal (TREE_OPERAND (pos, 1), toff_align))
927 *poffset = size_binop (MULT_EXPR,
928 fold_convert (sizetype, TREE_OPERAND (pos, 0)),
929 size_int (off_align / BITS_PER_UNIT));
930 *pbitpos = bitsize_zero_node;
932 else
934 *poffset = size_binop (MULT_EXPR,
935 fold_convert (sizetype,
936 size_binop (FLOOR_DIV_EXPR, pos,
937 toff_align)),
938 size_int (off_align / BITS_PER_UNIT));
939 *pbitpos = size_binop (FLOOR_MOD_EXPR, pos, toff_align);
943 /* Given a pointer to bit and byte offsets and an offset alignment,
944 normalize the offsets so they are within the alignment. */
946 void
947 normalize_offset (tree *poffset, tree *pbitpos, unsigned int off_align)
949 /* If the bit position is now larger than it should be, adjust it
950 downwards. */
951 if (compare_tree_int (*pbitpos, off_align) >= 0)
953 tree offset, bitpos;
954 pos_from_bit (&offset, &bitpos, off_align, *pbitpos);
955 *poffset = size_binop (PLUS_EXPR, *poffset, offset);
956 *pbitpos = bitpos;
960 /* Print debugging information about the information in RLI. */
962 DEBUG_FUNCTION void
963 debug_rli (record_layout_info rli)
965 print_node_brief (stderr, "type", rli->t, 0);
966 print_node_brief (stderr, "\noffset", rli->offset, 0);
967 print_node_brief (stderr, " bitpos", rli->bitpos, 0);
969 fprintf (stderr, "\naligns: rec = %u, unpack = %u, off = %u\n",
970 rli->record_align, rli->unpacked_align,
971 rli->offset_align);
973 /* The ms_struct code is the only that uses this. */
974 if (targetm.ms_bitfield_layout_p (rli->t))
975 fprintf (stderr, "remaining in alignment = %u\n", rli->remaining_in_alignment);
977 if (rli->packed_maybe_necessary)
978 fprintf (stderr, "packed may be necessary\n");
980 if (!vec_safe_is_empty (rli->pending_statics))
982 fprintf (stderr, "pending statics:\n");
983 debug_vec_tree (rli->pending_statics);
987 /* Given an RLI with a possibly-incremented BITPOS, adjust OFFSET and
988 BITPOS if necessary to keep BITPOS below OFFSET_ALIGN. */
990 void
991 normalize_rli (record_layout_info rli)
993 normalize_offset (&rli->offset, &rli->bitpos, rli->offset_align);
996 /* Returns the size in bytes allocated so far. */
998 tree
999 rli_size_unit_so_far (record_layout_info rli)
1001 return byte_from_pos (rli->offset, rli->bitpos);
1004 /* Returns the size in bits allocated so far. */
1006 tree
1007 rli_size_so_far (record_layout_info rli)
1009 return bit_from_pos (rli->offset, rli->bitpos);
1012 /* FIELD is about to be added to RLI->T. The alignment (in bits) of
1013 the next available location within the record is given by KNOWN_ALIGN.
1014 Update the variable alignment fields in RLI, and return the alignment
1015 to give the FIELD. */
1017 unsigned int
1018 update_alignment_for_field (record_layout_info rli, tree field,
1019 unsigned int known_align)
1021 /* The alignment required for FIELD. */
1022 unsigned int desired_align;
1023 /* The type of this field. */
1024 tree type = TREE_TYPE (field);
1025 /* True if the field was explicitly aligned by the user. */
1026 bool user_align;
1027 bool is_bitfield;
1029 /* Do not attempt to align an ERROR_MARK node */
1030 if (TREE_CODE (type) == ERROR_MARK)
1031 return 0;
1033 /* Lay out the field so we know what alignment it needs. */
1034 layout_decl (field, known_align);
1035 desired_align = DECL_ALIGN (field);
1036 user_align = DECL_USER_ALIGN (field);
1038 is_bitfield = (type != error_mark_node
1039 && DECL_BIT_FIELD_TYPE (field)
1040 && ! integer_zerop (TYPE_SIZE (type)));
1042 /* Record must have at least as much alignment as any field.
1043 Otherwise, the alignment of the field within the record is
1044 meaningless. */
1045 if (targetm.ms_bitfield_layout_p (rli->t))
1047 /* Here, the alignment of the underlying type of a bitfield can
1048 affect the alignment of a record; even a zero-sized field
1049 can do this. The alignment should be to the alignment of
1050 the type, except that for zero-size bitfields this only
1051 applies if there was an immediately prior, nonzero-size
1052 bitfield. (That's the way it is, experimentally.) */
1053 if ((!is_bitfield && !DECL_PACKED (field))
1054 || ((DECL_SIZE (field) == NULL_TREE
1055 || !integer_zerop (DECL_SIZE (field)))
1056 ? !DECL_PACKED (field)
1057 : (rli->prev_field
1058 && DECL_BIT_FIELD_TYPE (rli->prev_field)
1059 && ! integer_zerop (DECL_SIZE (rli->prev_field)))))
1061 unsigned int type_align = TYPE_ALIGN (type);
1062 type_align = MAX (type_align, desired_align);
1063 if (maximum_field_alignment != 0)
1064 type_align = MIN (type_align, maximum_field_alignment);
1065 rli->record_align = MAX (rli->record_align, type_align);
1066 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
1069 #ifdef PCC_BITFIELD_TYPE_MATTERS
1070 else if (is_bitfield && PCC_BITFIELD_TYPE_MATTERS)
1072 /* Named bit-fields cause the entire structure to have the
1073 alignment implied by their type. Some targets also apply the same
1074 rules to unnamed bitfields. */
1075 if (DECL_NAME (field) != 0
1076 || targetm.align_anon_bitfield ())
1078 unsigned int type_align = TYPE_ALIGN (type);
1080 #ifdef ADJUST_FIELD_ALIGN
1081 if (! TYPE_USER_ALIGN (type))
1082 type_align = ADJUST_FIELD_ALIGN (field, type_align);
1083 #endif
1085 /* Targets might chose to handle unnamed and hence possibly
1086 zero-width bitfield. Those are not influenced by #pragmas
1087 or packed attributes. */
1088 if (integer_zerop (DECL_SIZE (field)))
1090 if (initial_max_fld_align)
1091 type_align = MIN (type_align,
1092 initial_max_fld_align * BITS_PER_UNIT);
1094 else if (maximum_field_alignment != 0)
1095 type_align = MIN (type_align, maximum_field_alignment);
1096 else if (DECL_PACKED (field))
1097 type_align = MIN (type_align, BITS_PER_UNIT);
1099 /* The alignment of the record is increased to the maximum
1100 of the current alignment, the alignment indicated on the
1101 field (i.e., the alignment specified by an __aligned__
1102 attribute), and the alignment indicated by the type of
1103 the field. */
1104 rli->record_align = MAX (rli->record_align, desired_align);
1105 rli->record_align = MAX (rli->record_align, type_align);
1107 if (warn_packed)
1108 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
1109 user_align |= TYPE_USER_ALIGN (type);
1112 #endif
1113 else
1115 rli->record_align = MAX (rli->record_align, desired_align);
1116 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
1119 TYPE_USER_ALIGN (rli->t) |= user_align;
1121 return desired_align;
1124 /* Called from place_field to handle unions. */
1126 static void
1127 place_union_field (record_layout_info rli, tree field)
1129 update_alignment_for_field (rli, field, /*known_align=*/0);
1131 DECL_FIELD_OFFSET (field) = size_zero_node;
1132 DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
1133 SET_DECL_OFFSET_ALIGN (field, BIGGEST_ALIGNMENT);
1135 /* If this is an ERROR_MARK return *after* having set the
1136 field at the start of the union. This helps when parsing
1137 invalid fields. */
1138 if (TREE_CODE (TREE_TYPE (field)) == ERROR_MARK)
1139 return;
1141 /* We assume the union's size will be a multiple of a byte so we don't
1142 bother with BITPOS. */
1143 if (TREE_CODE (rli->t) == UNION_TYPE)
1144 rli->offset = size_binop (MAX_EXPR, rli->offset, DECL_SIZE_UNIT (field));
1145 else if (TREE_CODE (rli->t) == QUAL_UNION_TYPE)
1146 rli->offset = fold_build3 (COND_EXPR, sizetype, DECL_QUALIFIER (field),
1147 DECL_SIZE_UNIT (field), rli->offset);
1150 #if defined (PCC_BITFIELD_TYPE_MATTERS) || defined (BITFIELD_NBYTES_LIMITED)
1151 /* A bitfield of SIZE with a required access alignment of ALIGN is allocated
1152 at BYTE_OFFSET / BIT_OFFSET. Return nonzero if the field would span more
1153 units of alignment than the underlying TYPE. */
1154 static int
1155 excess_unit_span (HOST_WIDE_INT byte_offset, HOST_WIDE_INT bit_offset,
1156 HOST_WIDE_INT size, HOST_WIDE_INT align, tree type)
1158 /* Note that the calculation of OFFSET might overflow; we calculate it so
1159 that we still get the right result as long as ALIGN is a power of two. */
1160 unsigned HOST_WIDE_INT offset = byte_offset * BITS_PER_UNIT + bit_offset;
1162 offset = offset % align;
1163 return ((offset + size + align - 1) / align
1164 > tree_to_uhwi (TYPE_SIZE (type)) / align);
1166 #endif
1168 /* RLI contains information about the layout of a RECORD_TYPE. FIELD
1169 is a FIELD_DECL to be added after those fields already present in
1170 T. (FIELD is not actually added to the TYPE_FIELDS list here;
1171 callers that desire that behavior must manually perform that step.) */
1173 void
1174 place_field (record_layout_info rli, tree field)
1176 /* The alignment required for FIELD. */
1177 unsigned int desired_align;
1178 /* The alignment FIELD would have if we just dropped it into the
1179 record as it presently stands. */
1180 unsigned int known_align;
1181 unsigned int actual_align;
1182 /* The type of this field. */
1183 tree type = TREE_TYPE (field);
1185 gcc_assert (TREE_CODE (field) != ERROR_MARK);
1187 /* If FIELD is static, then treat it like a separate variable, not
1188 really like a structure field. If it is a FUNCTION_DECL, it's a
1189 method. In both cases, all we do is lay out the decl, and we do
1190 it *after* the record is laid out. */
1191 if (TREE_CODE (field) == VAR_DECL)
1193 vec_safe_push (rli->pending_statics, field);
1194 return;
1197 /* Enumerators and enum types which are local to this class need not
1198 be laid out. Likewise for initialized constant fields. */
1199 else if (TREE_CODE (field) != FIELD_DECL)
1200 return;
1202 /* Unions are laid out very differently than records, so split
1203 that code off to another function. */
1204 else if (TREE_CODE (rli->t) != RECORD_TYPE)
1206 place_union_field (rli, field);
1207 return;
1210 else if (TREE_CODE (type) == ERROR_MARK)
1212 /* Place this field at the current allocation position, so we
1213 maintain monotonicity. */
1214 DECL_FIELD_OFFSET (field) = rli->offset;
1215 DECL_FIELD_BIT_OFFSET (field) = rli->bitpos;
1216 SET_DECL_OFFSET_ALIGN (field, rli->offset_align);
1217 return;
1220 /* Work out the known alignment so far. Note that A & (-A) is the
1221 value of the least-significant bit in A that is one. */
1222 if (! integer_zerop (rli->bitpos))
1223 known_align = (tree_to_uhwi (rli->bitpos)
1224 & - tree_to_uhwi (rli->bitpos));
1225 else if (integer_zerop (rli->offset))
1226 known_align = 0;
1227 else if (tree_fits_uhwi_p (rli->offset))
1228 known_align = (BITS_PER_UNIT
1229 * (tree_to_uhwi (rli->offset)
1230 & - tree_to_uhwi (rli->offset)));
1231 else
1232 known_align = rli->offset_align;
1234 desired_align = update_alignment_for_field (rli, field, known_align);
1235 if (known_align == 0)
1236 known_align = MAX (BIGGEST_ALIGNMENT, rli->record_align);
1238 if (warn_packed && DECL_PACKED (field))
1240 if (known_align >= TYPE_ALIGN (type))
1242 if (TYPE_ALIGN (type) > desired_align)
1244 if (STRICT_ALIGNMENT)
1245 warning (OPT_Wattributes, "packed attribute causes "
1246 "inefficient alignment for %q+D", field);
1247 /* Don't warn if DECL_PACKED was set by the type. */
1248 else if (!TYPE_PACKED (rli->t))
1249 warning (OPT_Wattributes, "packed attribute is "
1250 "unnecessary for %q+D", field);
1253 else
1254 rli->packed_maybe_necessary = 1;
1257 /* Does this field automatically have alignment it needs by virtue
1258 of the fields that precede it and the record's own alignment? */
1259 if (known_align < desired_align)
1261 /* No, we need to skip space before this field.
1262 Bump the cumulative size to multiple of field alignment. */
1264 if (!targetm.ms_bitfield_layout_p (rli->t)
1265 && DECL_SOURCE_LOCATION (field) != BUILTINS_LOCATION)
1266 warning (OPT_Wpadded, "padding struct to align %q+D", field);
1268 /* If the alignment is still within offset_align, just align
1269 the bit position. */
1270 if (desired_align < rli->offset_align)
1271 rli->bitpos = round_up (rli->bitpos, desired_align);
1272 else
1274 /* First adjust OFFSET by the partial bits, then align. */
1275 rli->offset
1276 = size_binop (PLUS_EXPR, rli->offset,
1277 fold_convert (sizetype,
1278 size_binop (CEIL_DIV_EXPR, rli->bitpos,
1279 bitsize_unit_node)));
1280 rli->bitpos = bitsize_zero_node;
1282 rli->offset = round_up (rli->offset, desired_align / BITS_PER_UNIT);
1285 if (! TREE_CONSTANT (rli->offset))
1286 rli->offset_align = desired_align;
1287 if (targetm.ms_bitfield_layout_p (rli->t))
1288 rli->prev_field = NULL;
1291 /* Handle compatibility with PCC. Note that if the record has any
1292 variable-sized fields, we need not worry about compatibility. */
1293 #ifdef PCC_BITFIELD_TYPE_MATTERS
1294 if (PCC_BITFIELD_TYPE_MATTERS
1295 && ! targetm.ms_bitfield_layout_p (rli->t)
1296 && TREE_CODE (field) == FIELD_DECL
1297 && type != error_mark_node
1298 && DECL_BIT_FIELD (field)
1299 && (! DECL_PACKED (field)
1300 /* Enter for these packed fields only to issue a warning. */
1301 || TYPE_ALIGN (type) <= BITS_PER_UNIT)
1302 && maximum_field_alignment == 0
1303 && ! integer_zerop (DECL_SIZE (field))
1304 && tree_fits_uhwi_p (DECL_SIZE (field))
1305 && tree_fits_uhwi_p (rli->offset)
1306 && tree_fits_uhwi_p (TYPE_SIZE (type)))
1308 unsigned int type_align = TYPE_ALIGN (type);
1309 tree dsize = DECL_SIZE (field);
1310 HOST_WIDE_INT field_size = tree_to_uhwi (dsize);
1311 HOST_WIDE_INT offset = tree_to_uhwi (rli->offset);
1312 HOST_WIDE_INT bit_offset = tree_to_shwi (rli->bitpos);
1314 #ifdef ADJUST_FIELD_ALIGN
1315 if (! TYPE_USER_ALIGN (type))
1316 type_align = ADJUST_FIELD_ALIGN (field, type_align);
1317 #endif
1319 /* A bit field may not span more units of alignment of its type
1320 than its type itself. Advance to next boundary if necessary. */
1321 if (excess_unit_span (offset, bit_offset, field_size, type_align, type))
1323 if (DECL_PACKED (field))
1325 if (warn_packed_bitfield_compat == 1)
1326 inform
1327 (input_location,
1328 "offset of packed bit-field %qD has changed in GCC 4.4",
1329 field);
1331 else
1332 rli->bitpos = round_up (rli->bitpos, type_align);
1335 if (! DECL_PACKED (field))
1336 TYPE_USER_ALIGN (rli->t) |= TYPE_USER_ALIGN (type);
1338 #endif
1340 #ifdef BITFIELD_NBYTES_LIMITED
1341 if (BITFIELD_NBYTES_LIMITED
1342 && ! targetm.ms_bitfield_layout_p (rli->t)
1343 && TREE_CODE (field) == FIELD_DECL
1344 && type != error_mark_node
1345 && DECL_BIT_FIELD_TYPE (field)
1346 && ! DECL_PACKED (field)
1347 && ! integer_zerop (DECL_SIZE (field))
1348 && tree_fits_uhwi_p (DECL_SIZE (field))
1349 && tree_fits_uhwi_p (rli->offset)
1350 && tree_fits_uhwi_p (TYPE_SIZE (type)))
1352 unsigned int type_align = TYPE_ALIGN (type);
1353 tree dsize = DECL_SIZE (field);
1354 HOST_WIDE_INT field_size = tree_to_uhwi (dsize);
1355 HOST_WIDE_INT offset = tree_to_uhwi (rli->offset);
1356 HOST_WIDE_INT bit_offset = tree_to_shwi (rli->bitpos);
1358 #ifdef ADJUST_FIELD_ALIGN
1359 if (! TYPE_USER_ALIGN (type))
1360 type_align = ADJUST_FIELD_ALIGN (field, type_align);
1361 #endif
1363 if (maximum_field_alignment != 0)
1364 type_align = MIN (type_align, maximum_field_alignment);
1365 /* ??? This test is opposite the test in the containing if
1366 statement, so this code is unreachable currently. */
1367 else if (DECL_PACKED (field))
1368 type_align = MIN (type_align, BITS_PER_UNIT);
1370 /* A bit field may not span the unit of alignment of its type.
1371 Advance to next boundary if necessary. */
1372 if (excess_unit_span (offset, bit_offset, field_size, type_align, type))
1373 rli->bitpos = round_up (rli->bitpos, type_align);
1375 TYPE_USER_ALIGN (rli->t) |= TYPE_USER_ALIGN (type);
1377 #endif
1379 /* See the docs for TARGET_MS_BITFIELD_LAYOUT_P for details.
1380 A subtlety:
1381 When a bit field is inserted into a packed record, the whole
1382 size of the underlying type is used by one or more same-size
1383 adjacent bitfields. (That is, if its long:3, 32 bits is
1384 used in the record, and any additional adjacent long bitfields are
1385 packed into the same chunk of 32 bits. However, if the size
1386 changes, a new field of that size is allocated.) In an unpacked
1387 record, this is the same as using alignment, but not equivalent
1388 when packing.
1390 Note: for compatibility, we use the type size, not the type alignment
1391 to determine alignment, since that matches the documentation */
1393 if (targetm.ms_bitfield_layout_p (rli->t))
1395 tree prev_saved = rli->prev_field;
1396 tree prev_type = prev_saved ? DECL_BIT_FIELD_TYPE (prev_saved) : NULL;
1398 /* This is a bitfield if it exists. */
1399 if (rli->prev_field)
1401 /* If both are bitfields, nonzero, and the same size, this is
1402 the middle of a run. Zero declared size fields are special
1403 and handled as "end of run". (Note: it's nonzero declared
1404 size, but equal type sizes!) (Since we know that both
1405 the current and previous fields are bitfields by the
1406 time we check it, DECL_SIZE must be present for both.) */
1407 if (DECL_BIT_FIELD_TYPE (field)
1408 && !integer_zerop (DECL_SIZE (field))
1409 && !integer_zerop (DECL_SIZE (rli->prev_field))
1410 && tree_fits_shwi_p (DECL_SIZE (rli->prev_field))
1411 && tree_fits_uhwi_p (TYPE_SIZE (type))
1412 && simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (prev_type)))
1414 /* We're in the middle of a run of equal type size fields; make
1415 sure we realign if we run out of bits. (Not decl size,
1416 type size!) */
1417 HOST_WIDE_INT bitsize = tree_to_uhwi (DECL_SIZE (field));
1419 if (rli->remaining_in_alignment < bitsize)
1421 HOST_WIDE_INT typesize = tree_to_uhwi (TYPE_SIZE (type));
1423 /* out of bits; bump up to next 'word'. */
1424 rli->bitpos
1425 = size_binop (PLUS_EXPR, rli->bitpos,
1426 bitsize_int (rli->remaining_in_alignment));
1427 rli->prev_field = field;
1428 if (typesize < bitsize)
1429 rli->remaining_in_alignment = 0;
1430 else
1431 rli->remaining_in_alignment = typesize - bitsize;
1433 else
1434 rli->remaining_in_alignment -= bitsize;
1436 else
1438 /* End of a run: if leaving a run of bitfields of the same type
1439 size, we have to "use up" the rest of the bits of the type
1440 size.
1442 Compute the new position as the sum of the size for the prior
1443 type and where we first started working on that type.
1444 Note: since the beginning of the field was aligned then
1445 of course the end will be too. No round needed. */
1447 if (!integer_zerop (DECL_SIZE (rli->prev_field)))
1449 rli->bitpos
1450 = size_binop (PLUS_EXPR, rli->bitpos,
1451 bitsize_int (rli->remaining_in_alignment));
1453 else
1454 /* We "use up" size zero fields; the code below should behave
1455 as if the prior field was not a bitfield. */
1456 prev_saved = NULL;
1458 /* Cause a new bitfield to be captured, either this time (if
1459 currently a bitfield) or next time we see one. */
1460 if (!DECL_BIT_FIELD_TYPE (field)
1461 || integer_zerop (DECL_SIZE (field)))
1462 rli->prev_field = NULL;
1465 normalize_rli (rli);
1468 /* If we're starting a new run of same type size bitfields
1469 (or a run of non-bitfields), set up the "first of the run"
1470 fields.
1472 That is, if the current field is not a bitfield, or if there
1473 was a prior bitfield the type sizes differ, or if there wasn't
1474 a prior bitfield the size of the current field is nonzero.
1476 Note: we must be sure to test ONLY the type size if there was
1477 a prior bitfield and ONLY for the current field being zero if
1478 there wasn't. */
1480 if (!DECL_BIT_FIELD_TYPE (field)
1481 || (prev_saved != NULL
1482 ? !simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (prev_type))
1483 : !integer_zerop (DECL_SIZE (field)) ))
1485 /* Never smaller than a byte for compatibility. */
1486 unsigned int type_align = BITS_PER_UNIT;
1488 /* (When not a bitfield), we could be seeing a flex array (with
1489 no DECL_SIZE). Since we won't be using remaining_in_alignment
1490 until we see a bitfield (and come by here again) we just skip
1491 calculating it. */
1492 if (DECL_SIZE (field) != NULL
1493 && tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (field)))
1494 && tree_fits_uhwi_p (DECL_SIZE (field)))
1496 unsigned HOST_WIDE_INT bitsize
1497 = tree_to_uhwi (DECL_SIZE (field));
1498 unsigned HOST_WIDE_INT typesize
1499 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (field)));
1501 if (typesize < bitsize)
1502 rli->remaining_in_alignment = 0;
1503 else
1504 rli->remaining_in_alignment = typesize - bitsize;
1507 /* Now align (conventionally) for the new type. */
1508 type_align = TYPE_ALIGN (TREE_TYPE (field));
1510 if (maximum_field_alignment != 0)
1511 type_align = MIN (type_align, maximum_field_alignment);
1513 rli->bitpos = round_up (rli->bitpos, type_align);
1515 /* If we really aligned, don't allow subsequent bitfields
1516 to undo that. */
1517 rli->prev_field = NULL;
1521 /* Offset so far becomes the position of this field after normalizing. */
1522 normalize_rli (rli);
1523 DECL_FIELD_OFFSET (field) = rli->offset;
1524 DECL_FIELD_BIT_OFFSET (field) = rli->bitpos;
1525 SET_DECL_OFFSET_ALIGN (field, rli->offset_align);
1527 /* Evaluate nonconstant offsets only once, either now or as soon as safe. */
1528 if (TREE_CODE (DECL_FIELD_OFFSET (field)) != INTEGER_CST)
1529 DECL_FIELD_OFFSET (field) = variable_size (DECL_FIELD_OFFSET (field));
1531 /* If this field ended up more aligned than we thought it would be (we
1532 approximate this by seeing if its position changed), lay out the field
1533 again; perhaps we can use an integral mode for it now. */
1534 if (! integer_zerop (DECL_FIELD_BIT_OFFSET (field)))
1535 actual_align = (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
1536 & - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field)));
1537 else if (integer_zerop (DECL_FIELD_OFFSET (field)))
1538 actual_align = MAX (BIGGEST_ALIGNMENT, rli->record_align);
1539 else if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field)))
1540 actual_align = (BITS_PER_UNIT
1541 * (tree_to_uhwi (DECL_FIELD_OFFSET (field))
1542 & - tree_to_uhwi (DECL_FIELD_OFFSET (field))));
1543 else
1544 actual_align = DECL_OFFSET_ALIGN (field);
1545 /* ACTUAL_ALIGN is still the actual alignment *within the record* .
1546 store / extract bit field operations will check the alignment of the
1547 record against the mode of bit fields. */
1549 if (known_align != actual_align)
1550 layout_decl (field, actual_align);
1552 if (rli->prev_field == NULL && DECL_BIT_FIELD_TYPE (field))
1553 rli->prev_field = field;
1555 /* Now add size of this field to the size of the record. If the size is
1556 not constant, treat the field as being a multiple of bytes and just
1557 adjust the offset, resetting the bit position. Otherwise, apportion the
1558 size amongst the bit position and offset. First handle the case of an
1559 unspecified size, which can happen when we have an invalid nested struct
1560 definition, such as struct j { struct j { int i; } }. The error message
1561 is printed in finish_struct. */
1562 if (DECL_SIZE (field) == 0)
1563 /* Do nothing. */;
1564 else if (TREE_CODE (DECL_SIZE (field)) != INTEGER_CST
1565 || TREE_OVERFLOW (DECL_SIZE (field)))
1567 rli->offset
1568 = size_binop (PLUS_EXPR, rli->offset,
1569 fold_convert (sizetype,
1570 size_binop (CEIL_DIV_EXPR, rli->bitpos,
1571 bitsize_unit_node)));
1572 rli->offset
1573 = size_binop (PLUS_EXPR, rli->offset, DECL_SIZE_UNIT (field));
1574 rli->bitpos = bitsize_zero_node;
1575 rli->offset_align = MIN (rli->offset_align, desired_align);
1577 else if (targetm.ms_bitfield_layout_p (rli->t))
1579 rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos, DECL_SIZE (field));
1581 /* If we ended a bitfield before the full length of the type then
1582 pad the struct out to the full length of the last type. */
1583 if ((DECL_CHAIN (field) == NULL
1584 || TREE_CODE (DECL_CHAIN (field)) != FIELD_DECL)
1585 && DECL_BIT_FIELD_TYPE (field)
1586 && !integer_zerop (DECL_SIZE (field)))
1587 rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos,
1588 bitsize_int (rli->remaining_in_alignment));
1590 normalize_rli (rli);
1592 else
1594 rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos, DECL_SIZE (field));
1595 normalize_rli (rli);
1599 /* Assuming that all the fields have been laid out, this function uses
1600 RLI to compute the final TYPE_SIZE, TYPE_ALIGN, etc. for the type
1601 indicated by RLI. */
1603 static void
1604 finalize_record_size (record_layout_info rli)
1606 tree unpadded_size, unpadded_size_unit;
1608 /* Now we want just byte and bit offsets, so set the offset alignment
1609 to be a byte and then normalize. */
1610 rli->offset_align = BITS_PER_UNIT;
1611 normalize_rli (rli);
1613 /* Determine the desired alignment. */
1614 #ifdef ROUND_TYPE_ALIGN
1615 TYPE_ALIGN (rli->t) = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t),
1616 rli->record_align);
1617 #else
1618 TYPE_ALIGN (rli->t) = MAX (TYPE_ALIGN (rli->t), rli->record_align);
1619 #endif
1621 /* Compute the size so far. Be sure to allow for extra bits in the
1622 size in bytes. We have guaranteed above that it will be no more
1623 than a single byte. */
1624 unpadded_size = rli_size_so_far (rli);
1625 unpadded_size_unit = rli_size_unit_so_far (rli);
1626 if (! integer_zerop (rli->bitpos))
1627 unpadded_size_unit
1628 = size_binop (PLUS_EXPR, unpadded_size_unit, size_one_node);
1630 /* Round the size up to be a multiple of the required alignment. */
1631 TYPE_SIZE (rli->t) = round_up (unpadded_size, TYPE_ALIGN (rli->t));
1632 TYPE_SIZE_UNIT (rli->t)
1633 = round_up (unpadded_size_unit, TYPE_ALIGN_UNIT (rli->t));
1635 if (TREE_CONSTANT (unpadded_size)
1636 && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0
1637 && input_location != BUILTINS_LOCATION)
1638 warning (OPT_Wpadded, "padding struct size to alignment boundary");
1640 if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
1641 && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
1642 && TREE_CONSTANT (unpadded_size))
1644 tree unpacked_size;
1646 #ifdef ROUND_TYPE_ALIGN
1647 rli->unpacked_align
1648 = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t), rli->unpacked_align);
1649 #else
1650 rli->unpacked_align = MAX (TYPE_ALIGN (rli->t), rli->unpacked_align);
1651 #endif
1653 unpacked_size = round_up (TYPE_SIZE (rli->t), rli->unpacked_align);
1654 if (simple_cst_equal (unpacked_size, TYPE_SIZE (rli->t)))
1656 if (TYPE_NAME (rli->t))
1658 tree name;
1660 if (TREE_CODE (TYPE_NAME (rli->t)) == IDENTIFIER_NODE)
1661 name = TYPE_NAME (rli->t);
1662 else
1663 name = DECL_NAME (TYPE_NAME (rli->t));
1665 if (STRICT_ALIGNMENT)
1666 warning (OPT_Wpacked, "packed attribute causes inefficient "
1667 "alignment for %qE", name);
1668 else
1669 warning (OPT_Wpacked,
1670 "packed attribute is unnecessary for %qE", name);
1672 else
1674 if (STRICT_ALIGNMENT)
1675 warning (OPT_Wpacked,
1676 "packed attribute causes inefficient alignment");
1677 else
1678 warning (OPT_Wpacked, "packed attribute is unnecessary");
1684 /* Compute the TYPE_MODE for the TYPE (which is a RECORD_TYPE). */
1686 void
1687 compute_record_mode (tree type)
1689 tree field;
1690 machine_mode mode = VOIDmode;
1692 /* Most RECORD_TYPEs have BLKmode, so we start off assuming that.
1693 However, if possible, we use a mode that fits in a register
1694 instead, in order to allow for better optimization down the
1695 line. */
1696 SET_TYPE_MODE (type, BLKmode);
1698 if (! tree_fits_uhwi_p (TYPE_SIZE (type)))
1699 return;
1701 /* A record which has any BLKmode members must itself be
1702 BLKmode; it can't go in a register. Unless the member is
1703 BLKmode only because it isn't aligned. */
1704 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1706 if (TREE_CODE (field) != FIELD_DECL)
1707 continue;
1709 if (TREE_CODE (TREE_TYPE (field)) == ERROR_MARK
1710 || (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1711 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field))
1712 && !(TYPE_SIZE (TREE_TYPE (field)) != 0
1713 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))))
1714 || ! tree_fits_uhwi_p (bit_position (field))
1715 || DECL_SIZE (field) == 0
1716 || ! tree_fits_uhwi_p (DECL_SIZE (field)))
1717 return;
1719 /* If this field is the whole struct, remember its mode so
1720 that, say, we can put a double in a class into a DF
1721 register instead of forcing it to live in the stack. */
1722 if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
1723 mode = DECL_MODE (field);
1725 /* With some targets, it is sub-optimal to access an aligned
1726 BLKmode structure as a scalar. */
1727 if (targetm.member_type_forces_blk (field, mode))
1728 return;
1731 /* If we only have one real field; use its mode if that mode's size
1732 matches the type's size. This only applies to RECORD_TYPE. This
1733 does not apply to unions. */
1734 if (TREE_CODE (type) == RECORD_TYPE && mode != VOIDmode
1735 && tree_fits_uhwi_p (TYPE_SIZE (type))
1736 && GET_MODE_BITSIZE (mode) == tree_to_uhwi (TYPE_SIZE (type)))
1737 SET_TYPE_MODE (type, mode);
1738 else
1739 SET_TYPE_MODE (type, mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1));
1741 /* If structure's known alignment is less than what the scalar
1742 mode would need, and it matters, then stick with BLKmode. */
1743 if (TYPE_MODE (type) != BLKmode
1744 && STRICT_ALIGNMENT
1745 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1746 || TYPE_ALIGN (type) >= GET_MODE_ALIGNMENT (TYPE_MODE (type))))
1748 /* If this is the only reason this type is BLKmode, then
1749 don't force containing types to be BLKmode. */
1750 TYPE_NO_FORCE_BLK (type) = 1;
1751 SET_TYPE_MODE (type, BLKmode);
1755 /* Compute TYPE_SIZE and TYPE_ALIGN for TYPE, once it has been laid
1756 out. */
1758 static void
1759 finalize_type_size (tree type)
1761 /* Normally, use the alignment corresponding to the mode chosen.
1762 However, where strict alignment is not required, avoid
1763 over-aligning structures, since most compilers do not do this
1764 alignment. */
1766 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1767 && (STRICT_ALIGNMENT
1768 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1769 && TREE_CODE (type) != QUAL_UNION_TYPE
1770 && TREE_CODE (type) != ARRAY_TYPE)))
1772 unsigned mode_align = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1774 /* Don't override a larger alignment requirement coming from a user
1775 alignment of one of the fields. */
1776 if (mode_align >= TYPE_ALIGN (type))
1778 TYPE_ALIGN (type) = mode_align;
1779 TYPE_USER_ALIGN (type) = 0;
1783 /* Do machine-dependent extra alignment. */
1784 #ifdef ROUND_TYPE_ALIGN
1785 TYPE_ALIGN (type)
1786 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1787 #endif
1789 /* If we failed to find a simple way to calculate the unit size
1790 of the type, find it by division. */
1791 if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1792 /* TYPE_SIZE (type) is computed in bitsizetype. After the division, the
1793 result will fit in sizetype. We will get more efficient code using
1794 sizetype, so we force a conversion. */
1795 TYPE_SIZE_UNIT (type)
1796 = fold_convert (sizetype,
1797 size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1798 bitsize_unit_node));
1800 if (TYPE_SIZE (type) != 0)
1802 TYPE_SIZE (type) = round_up (TYPE_SIZE (type), TYPE_ALIGN (type));
1803 TYPE_SIZE_UNIT (type)
1804 = round_up (TYPE_SIZE_UNIT (type), TYPE_ALIGN_UNIT (type));
1807 /* Evaluate nonconstant sizes only once, either now or as soon as safe. */
1808 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1809 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1810 if (TYPE_SIZE_UNIT (type) != 0
1811 && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1812 TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1814 /* Also layout any other variants of the type. */
1815 if (TYPE_NEXT_VARIANT (type)
1816 || type != TYPE_MAIN_VARIANT (type))
1818 tree variant;
1819 /* Record layout info of this variant. */
1820 tree size = TYPE_SIZE (type);
1821 tree size_unit = TYPE_SIZE_UNIT (type);
1822 unsigned int align = TYPE_ALIGN (type);
1823 unsigned int precision = TYPE_PRECISION (type);
1824 unsigned int user_align = TYPE_USER_ALIGN (type);
1825 machine_mode mode = TYPE_MODE (type);
1827 /* Copy it into all variants. */
1828 for (variant = TYPE_MAIN_VARIANT (type);
1829 variant != 0;
1830 variant = TYPE_NEXT_VARIANT (variant))
1832 TYPE_SIZE (variant) = size;
1833 TYPE_SIZE_UNIT (variant) = size_unit;
1834 TYPE_ALIGN (variant) = align;
1835 TYPE_PRECISION (variant) = precision;
1836 TYPE_USER_ALIGN (variant) = user_align;
1837 SET_TYPE_MODE (variant, mode);
1842 /* Return a new underlying object for a bitfield started with FIELD. */
1844 static tree
1845 start_bitfield_representative (tree field)
1847 tree repr = make_node (FIELD_DECL);
1848 DECL_FIELD_OFFSET (repr) = DECL_FIELD_OFFSET (field);
1849 /* Force the representative to begin at a BITS_PER_UNIT aligned
1850 boundary - C++ may use tail-padding of a base object to
1851 continue packing bits so the bitfield region does not start
1852 at bit zero (see g++.dg/abi/bitfield5.C for example).
1853 Unallocated bits may happen for other reasons as well,
1854 for example Ada which allows explicit bit-granular structure layout. */
1855 DECL_FIELD_BIT_OFFSET (repr)
1856 = size_binop (BIT_AND_EXPR,
1857 DECL_FIELD_BIT_OFFSET (field),
1858 bitsize_int (~(BITS_PER_UNIT - 1)));
1859 SET_DECL_OFFSET_ALIGN (repr, DECL_OFFSET_ALIGN (field));
1860 DECL_SIZE (repr) = DECL_SIZE (field);
1861 DECL_SIZE_UNIT (repr) = DECL_SIZE_UNIT (field);
1862 DECL_PACKED (repr) = DECL_PACKED (field);
1863 DECL_CONTEXT (repr) = DECL_CONTEXT (field);
1864 return repr;
1867 /* Finish up a bitfield group that was started by creating the underlying
1868 object REPR with the last field in the bitfield group FIELD. */
1870 static void
1871 finish_bitfield_representative (tree repr, tree field)
1873 unsigned HOST_WIDE_INT bitsize, maxbitsize;
1874 machine_mode mode;
1875 tree nextf, size;
1877 size = size_diffop (DECL_FIELD_OFFSET (field),
1878 DECL_FIELD_OFFSET (repr));
1879 while (TREE_CODE (size) == COMPOUND_EXPR)
1880 size = TREE_OPERAND (size, 1);
1881 gcc_assert (tree_fits_uhwi_p (size));
1882 bitsize = (tree_to_uhwi (size) * BITS_PER_UNIT
1883 + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
1884 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr))
1885 + tree_to_uhwi (DECL_SIZE (field)));
1887 /* Round up bitsize to multiples of BITS_PER_UNIT. */
1888 bitsize = (bitsize + BITS_PER_UNIT - 1) & ~(BITS_PER_UNIT - 1);
1890 /* Now nothing tells us how to pad out bitsize ... */
1891 nextf = DECL_CHAIN (field);
1892 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
1893 nextf = DECL_CHAIN (nextf);
1894 if (nextf)
1896 tree maxsize;
1897 /* If there was an error, the field may be not laid out
1898 correctly. Don't bother to do anything. */
1899 if (TREE_TYPE (nextf) == error_mark_node)
1900 return;
1901 maxsize = size_diffop (DECL_FIELD_OFFSET (nextf),
1902 DECL_FIELD_OFFSET (repr));
1903 if (tree_fits_uhwi_p (maxsize))
1905 maxbitsize = (tree_to_uhwi (maxsize) * BITS_PER_UNIT
1906 + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (nextf))
1907 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
1908 /* If the group ends within a bitfield nextf does not need to be
1909 aligned to BITS_PER_UNIT. Thus round up. */
1910 maxbitsize = (maxbitsize + BITS_PER_UNIT - 1) & ~(BITS_PER_UNIT - 1);
1912 else
1913 maxbitsize = bitsize;
1915 else
1917 /* ??? If you consider that tail-padding of this struct might be
1918 re-used when deriving from it we cannot really do the following
1919 and thus need to set maxsize to bitsize? Also we cannot
1920 generally rely on maxsize to fold to an integer constant, so
1921 use bitsize as fallback for this case. */
1922 tree maxsize = size_diffop (TYPE_SIZE_UNIT (DECL_CONTEXT (field)),
1923 DECL_FIELD_OFFSET (repr));
1924 if (tree_fits_uhwi_p (maxsize))
1925 maxbitsize = (tree_to_uhwi (maxsize) * BITS_PER_UNIT
1926 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
1927 else
1928 maxbitsize = bitsize;
1931 /* Only if we don't artificially break up the representative in
1932 the middle of a large bitfield with different possibly
1933 overlapping representatives. And all representatives start
1934 at byte offset. */
1935 gcc_assert (maxbitsize % BITS_PER_UNIT == 0);
1937 /* Find the smallest nice mode to use. */
1938 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1939 mode = GET_MODE_WIDER_MODE (mode))
1940 if (GET_MODE_BITSIZE (mode) >= bitsize)
1941 break;
1942 if (mode != VOIDmode
1943 && (GET_MODE_BITSIZE (mode) > maxbitsize
1944 || GET_MODE_BITSIZE (mode) > MAX_FIXED_MODE_SIZE))
1945 mode = VOIDmode;
1947 if (mode == VOIDmode)
1949 /* We really want a BLKmode representative only as a last resort,
1950 considering the member b in
1951 struct { int a : 7; int b : 17; int c; } __attribute__((packed));
1952 Otherwise we simply want to split the representative up
1953 allowing for overlaps within the bitfield region as required for
1954 struct { int a : 7; int b : 7;
1955 int c : 10; int d; } __attribute__((packed));
1956 [0, 15] HImode for a and b, [8, 23] HImode for c. */
1957 DECL_SIZE (repr) = bitsize_int (bitsize);
1958 DECL_SIZE_UNIT (repr) = size_int (bitsize / BITS_PER_UNIT);
1959 DECL_MODE (repr) = BLKmode;
1960 TREE_TYPE (repr) = build_array_type_nelts (unsigned_char_type_node,
1961 bitsize / BITS_PER_UNIT);
1963 else
1965 unsigned HOST_WIDE_INT modesize = GET_MODE_BITSIZE (mode);
1966 DECL_SIZE (repr) = bitsize_int (modesize);
1967 DECL_SIZE_UNIT (repr) = size_int (modesize / BITS_PER_UNIT);
1968 DECL_MODE (repr) = mode;
1969 TREE_TYPE (repr) = lang_hooks.types.type_for_mode (mode, 1);
1972 /* Remember whether the bitfield group is at the end of the
1973 structure or not. */
1974 DECL_CHAIN (repr) = nextf;
1977 /* Compute and set FIELD_DECLs for the underlying objects we should
1978 use for bitfield access for the structure T. */
1980 void
1981 finish_bitfield_layout (tree t)
1983 tree field, prev;
1984 tree repr = NULL_TREE;
1986 /* Unions would be special, for the ease of type-punning optimizations
1987 we could use the underlying type as hint for the representative
1988 if the bitfield would fit and the representative would not exceed
1989 the union in size. */
1990 if (TREE_CODE (t) != RECORD_TYPE)
1991 return;
1993 for (prev = NULL_TREE, field = TYPE_FIELDS (t);
1994 field; field = DECL_CHAIN (field))
1996 if (TREE_CODE (field) != FIELD_DECL)
1997 continue;
1999 /* In the C++ memory model, consecutive bit fields in a structure are
2000 considered one memory location and updating a memory location
2001 may not store into adjacent memory locations. */
2002 if (!repr
2003 && DECL_BIT_FIELD_TYPE (field))
2005 /* Start new representative. */
2006 repr = start_bitfield_representative (field);
2008 else if (repr
2009 && ! DECL_BIT_FIELD_TYPE (field))
2011 /* Finish off new representative. */
2012 finish_bitfield_representative (repr, prev);
2013 repr = NULL_TREE;
2015 else if (DECL_BIT_FIELD_TYPE (field))
2017 gcc_assert (repr != NULL_TREE);
2019 /* Zero-size bitfields finish off a representative and
2020 do not have a representative themselves. This is
2021 required by the C++ memory model. */
2022 if (integer_zerop (DECL_SIZE (field)))
2024 finish_bitfield_representative (repr, prev);
2025 repr = NULL_TREE;
2028 /* We assume that either DECL_FIELD_OFFSET of the representative
2029 and each bitfield member is a constant or they are equal.
2030 This is because we need to be able to compute the bit-offset
2031 of each field relative to the representative in get_bit_range
2032 during RTL expansion.
2033 If these constraints are not met, simply force a new
2034 representative to be generated. That will at most
2035 generate worse code but still maintain correctness with
2036 respect to the C++ memory model. */
2037 else if (!((tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr))
2038 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (field)))
2039 || operand_equal_p (DECL_FIELD_OFFSET (repr),
2040 DECL_FIELD_OFFSET (field), 0)))
2042 finish_bitfield_representative (repr, prev);
2043 repr = start_bitfield_representative (field);
2046 else
2047 continue;
2049 if (repr)
2050 DECL_BIT_FIELD_REPRESENTATIVE (field) = repr;
2052 prev = field;
2055 if (repr)
2056 finish_bitfield_representative (repr, prev);
2059 /* Do all of the work required to layout the type indicated by RLI,
2060 once the fields have been laid out. This function will call `free'
2061 for RLI, unless FREE_P is false. Passing a value other than false
2062 for FREE_P is bad practice; this option only exists to support the
2063 G++ 3.2 ABI. */
2065 void
2066 finish_record_layout (record_layout_info rli, int free_p)
2068 tree variant;
2070 /* Compute the final size. */
2071 finalize_record_size (rli);
2073 /* Compute the TYPE_MODE for the record. */
2074 compute_record_mode (rli->t);
2076 /* Perform any last tweaks to the TYPE_SIZE, etc. */
2077 finalize_type_size (rli->t);
2079 /* Compute bitfield representatives. */
2080 finish_bitfield_layout (rli->t);
2082 /* Propagate TYPE_PACKED to variants. With C++ templates,
2083 handle_packed_attribute is too early to do this. */
2084 for (variant = TYPE_NEXT_VARIANT (rli->t); variant;
2085 variant = TYPE_NEXT_VARIANT (variant))
2086 TYPE_PACKED (variant) = TYPE_PACKED (rli->t);
2088 /* Lay out any static members. This is done now because their type
2089 may use the record's type. */
2090 while (!vec_safe_is_empty (rli->pending_statics))
2091 layout_decl (rli->pending_statics->pop (), 0);
2093 /* Clean up. */
2094 if (free_p)
2096 vec_free (rli->pending_statics);
2097 free (rli);
2102 /* Finish processing a builtin RECORD_TYPE type TYPE. It's name is
2103 NAME, its fields are chained in reverse on FIELDS.
2105 If ALIGN_TYPE is non-null, it is given the same alignment as
2106 ALIGN_TYPE. */
2108 void
2109 finish_builtin_struct (tree type, const char *name, tree fields,
2110 tree align_type)
2112 tree tail, next;
2114 for (tail = NULL_TREE; fields; tail = fields, fields = next)
2116 DECL_FIELD_CONTEXT (fields) = type;
2117 next = DECL_CHAIN (fields);
2118 DECL_CHAIN (fields) = tail;
2120 TYPE_FIELDS (type) = tail;
2122 if (align_type)
2124 TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
2125 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (align_type);
2128 layout_type (type);
2129 #if 0 /* not yet, should get fixed properly later */
2130 TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
2131 #else
2132 TYPE_NAME (type) = build_decl (BUILTINS_LOCATION,
2133 TYPE_DECL, get_identifier (name), type);
2134 #endif
2135 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2136 layout_decl (TYPE_NAME (type), 0);
2139 /* Calculate the mode, size, and alignment for TYPE.
2140 For an array type, calculate the element separation as well.
2141 Record TYPE on the chain of permanent or temporary types
2142 so that dbxout will find out about it.
2144 TYPE_SIZE of a type is nonzero if the type has been laid out already.
2145 layout_type does nothing on such a type.
2147 If the type is incomplete, its TYPE_SIZE remains zero. */
2149 void
2150 layout_type (tree type)
2152 gcc_assert (type);
2154 if (type == error_mark_node)
2155 return;
2157 /* Do nothing if type has been laid out before. */
2158 if (TYPE_SIZE (type))
2159 return;
2161 switch (TREE_CODE (type))
2163 case LANG_TYPE:
2164 /* This kind of type is the responsibility
2165 of the language-specific code. */
2166 gcc_unreachable ();
2168 case BOOLEAN_TYPE:
2169 case INTEGER_TYPE:
2170 case ENUMERAL_TYPE:
2171 SET_TYPE_MODE (type,
2172 smallest_mode_for_size (TYPE_PRECISION (type), MODE_INT));
2173 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
2174 /* Don't set TYPE_PRECISION here, as it may be set by a bitfield. */
2175 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
2176 break;
2178 case REAL_TYPE:
2179 SET_TYPE_MODE (type,
2180 mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0));
2181 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
2182 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
2183 break;
2185 case FIXED_POINT_TYPE:
2186 /* TYPE_MODE (type) has been set already. */
2187 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
2188 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
2189 break;
2191 case COMPLEX_TYPE:
2192 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TREE_TYPE (type));
2193 SET_TYPE_MODE (type,
2194 mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
2195 (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE
2196 ? MODE_COMPLEX_FLOAT : MODE_COMPLEX_INT),
2197 0));
2198 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
2199 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
2200 break;
2202 case VECTOR_TYPE:
2204 int nunits = TYPE_VECTOR_SUBPARTS (type);
2205 tree innertype = TREE_TYPE (type);
2207 gcc_assert (!(nunits & (nunits - 1)));
2209 /* Find an appropriate mode for the vector type. */
2210 if (TYPE_MODE (type) == VOIDmode)
2211 SET_TYPE_MODE (type,
2212 mode_for_vector (TYPE_MODE (innertype), nunits));
2214 TYPE_SATURATING (type) = TYPE_SATURATING (TREE_TYPE (type));
2215 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TREE_TYPE (type));
2216 TYPE_SIZE_UNIT (type) = int_const_binop (MULT_EXPR,
2217 TYPE_SIZE_UNIT (innertype),
2218 size_int (nunits));
2219 TYPE_SIZE (type) = int_const_binop (MULT_EXPR, TYPE_SIZE (innertype),
2220 bitsize_int (nunits));
2222 /* For vector types, we do not default to the mode's alignment.
2223 Instead, query a target hook, defaulting to natural alignment.
2224 This prevents ABI changes depending on whether or not native
2225 vector modes are supported. */
2226 TYPE_ALIGN (type) = targetm.vector_alignment (type);
2228 /* However, if the underlying mode requires a bigger alignment than
2229 what the target hook provides, we cannot use the mode. For now,
2230 simply reject that case. */
2231 gcc_assert (TYPE_ALIGN (type)
2232 >= GET_MODE_ALIGNMENT (TYPE_MODE (type)));
2233 break;
2236 case VOID_TYPE:
2237 /* This is an incomplete type and so doesn't have a size. */
2238 TYPE_ALIGN (type) = 1;
2239 TYPE_USER_ALIGN (type) = 0;
2240 SET_TYPE_MODE (type, VOIDmode);
2241 break;
2243 case POINTER_BOUNDS_TYPE:
2244 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
2245 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
2246 break;
2248 case OFFSET_TYPE:
2249 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
2250 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE_UNITS);
2251 /* A pointer might be MODE_PARTIAL_INT, but ptrdiff_t must be
2252 integral, which may be an __intN. */
2253 SET_TYPE_MODE (type, mode_for_size (POINTER_SIZE, MODE_INT, 0));
2254 TYPE_PRECISION (type) = POINTER_SIZE;
2255 break;
2257 case FUNCTION_TYPE:
2258 case METHOD_TYPE:
2259 /* It's hard to see what the mode and size of a function ought to
2260 be, but we do know the alignment is FUNCTION_BOUNDARY, so
2261 make it consistent with that. */
2262 SET_TYPE_MODE (type, mode_for_size (FUNCTION_BOUNDARY, MODE_INT, 0));
2263 TYPE_SIZE (type) = bitsize_int (FUNCTION_BOUNDARY);
2264 TYPE_SIZE_UNIT (type) = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
2265 break;
2267 case POINTER_TYPE:
2268 case REFERENCE_TYPE:
2270 machine_mode mode = TYPE_MODE (type);
2271 if (TREE_CODE (type) == REFERENCE_TYPE && reference_types_internal)
2273 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
2274 mode = targetm.addr_space.address_mode (as);
2277 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (mode));
2278 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (mode));
2279 TYPE_UNSIGNED (type) = 1;
2280 TYPE_PRECISION (type) = GET_MODE_PRECISION (mode);
2282 break;
2284 case ARRAY_TYPE:
2286 tree index = TYPE_DOMAIN (type);
2287 tree element = TREE_TYPE (type);
2289 build_pointer_type (element);
2291 /* We need to know both bounds in order to compute the size. */
2292 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
2293 && TYPE_SIZE (element))
2295 tree ub = TYPE_MAX_VALUE (index);
2296 tree lb = TYPE_MIN_VALUE (index);
2297 tree element_size = TYPE_SIZE (element);
2298 tree length;
2300 /* Make sure that an array of zero-sized element is zero-sized
2301 regardless of its extent. */
2302 if (integer_zerop (element_size))
2303 length = size_zero_node;
2305 /* The computation should happen in the original signedness so
2306 that (possible) negative values are handled appropriately
2307 when determining overflow. */
2308 else
2310 /* ??? When it is obvious that the range is signed
2311 represent it using ssizetype. */
2312 if (TREE_CODE (lb) == INTEGER_CST
2313 && TREE_CODE (ub) == INTEGER_CST
2314 && TYPE_UNSIGNED (TREE_TYPE (lb))
2315 && tree_int_cst_lt (ub, lb))
2317 lb = wide_int_to_tree (ssizetype,
2318 offset_int::from (lb, SIGNED));
2319 ub = wide_int_to_tree (ssizetype,
2320 offset_int::from (ub, SIGNED));
2322 length
2323 = fold_convert (sizetype,
2324 size_binop (PLUS_EXPR,
2325 build_int_cst (TREE_TYPE (lb), 1),
2326 size_binop (MINUS_EXPR, ub, lb)));
2329 /* ??? We have no way to distinguish a null-sized array from an
2330 array spanning the whole sizetype range, so we arbitrarily
2331 decide that [0, -1] is the only valid representation. */
2332 if (integer_zerop (length)
2333 && TREE_OVERFLOW (length)
2334 && integer_zerop (lb))
2335 length = size_zero_node;
2337 TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size,
2338 fold_convert (bitsizetype,
2339 length));
2341 /* If we know the size of the element, calculate the total size
2342 directly, rather than do some division thing below. This
2343 optimization helps Fortran assumed-size arrays (where the
2344 size of the array is determined at runtime) substantially. */
2345 if (TYPE_SIZE_UNIT (element))
2346 TYPE_SIZE_UNIT (type)
2347 = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
2350 /* Now round the alignment and size,
2351 using machine-dependent criteria if any. */
2353 #ifdef ROUND_TYPE_ALIGN
2354 TYPE_ALIGN (type)
2355 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
2356 #else
2357 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
2358 #endif
2359 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (element);
2360 SET_TYPE_MODE (type, BLKmode);
2361 if (TYPE_SIZE (type) != 0
2362 && ! targetm.member_type_forces_blk (type, VOIDmode)
2363 /* BLKmode elements force BLKmode aggregate;
2364 else extract/store fields may lose. */
2365 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
2366 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
2368 SET_TYPE_MODE (type, mode_for_array (TREE_TYPE (type),
2369 TYPE_SIZE (type)));
2370 if (TYPE_MODE (type) != BLKmode
2371 && STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
2372 && TYPE_ALIGN (type) < GET_MODE_ALIGNMENT (TYPE_MODE (type)))
2374 TYPE_NO_FORCE_BLK (type) = 1;
2375 SET_TYPE_MODE (type, BLKmode);
2378 /* When the element size is constant, check that it is at least as
2379 large as the element alignment. */
2380 if (TYPE_SIZE_UNIT (element)
2381 && TREE_CODE (TYPE_SIZE_UNIT (element)) == INTEGER_CST
2382 /* If TYPE_SIZE_UNIT overflowed, then it is certainly larger than
2383 TYPE_ALIGN_UNIT. */
2384 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (element))
2385 && !integer_zerop (TYPE_SIZE_UNIT (element))
2386 && compare_tree_int (TYPE_SIZE_UNIT (element),
2387 TYPE_ALIGN_UNIT (element)) < 0)
2388 error ("alignment of array elements is greater than element size");
2389 break;
2392 case RECORD_TYPE:
2393 case UNION_TYPE:
2394 case QUAL_UNION_TYPE:
2396 tree field;
2397 record_layout_info rli;
2399 /* Initialize the layout information. */
2400 rli = start_record_layout (type);
2402 /* If this is a QUAL_UNION_TYPE, we want to process the fields
2403 in the reverse order in building the COND_EXPR that denotes
2404 its size. We reverse them again later. */
2405 if (TREE_CODE (type) == QUAL_UNION_TYPE)
2406 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
2408 /* Place all the fields. */
2409 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2410 place_field (rli, field);
2412 if (TREE_CODE (type) == QUAL_UNION_TYPE)
2413 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
2415 /* Finish laying out the record. */
2416 finish_record_layout (rli, /*free_p=*/true);
2418 break;
2420 default:
2421 gcc_unreachable ();
2424 /* Compute the final TYPE_SIZE, TYPE_ALIGN, etc. for TYPE. For
2425 records and unions, finish_record_layout already called this
2426 function. */
2427 if (TREE_CODE (type) != RECORD_TYPE
2428 && TREE_CODE (type) != UNION_TYPE
2429 && TREE_CODE (type) != QUAL_UNION_TYPE)
2430 finalize_type_size (type);
2432 /* We should never see alias sets on incomplete aggregates. And we
2433 should not call layout_type on not incomplete aggregates. */
2434 if (AGGREGATE_TYPE_P (type))
2435 gcc_assert (!TYPE_ALIAS_SET_KNOWN_P (type));
2438 /* Return the least alignment required for type TYPE. */
2440 unsigned int
2441 min_align_of_type (tree type)
2443 unsigned int align = TYPE_ALIGN (type);
2444 if (!TYPE_USER_ALIGN (type))
2446 align = MIN (align, BIGGEST_ALIGNMENT);
2447 #ifdef BIGGEST_FIELD_ALIGNMENT
2448 align = MIN (align, BIGGEST_FIELD_ALIGNMENT);
2449 #endif
2450 unsigned int field_align = align;
2451 #ifdef ADJUST_FIELD_ALIGN
2452 tree field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, type);
2453 field_align = ADJUST_FIELD_ALIGN (field, field_align);
2454 ggc_free (field);
2455 #endif
2456 align = MIN (align, field_align);
2458 return align / BITS_PER_UNIT;
2461 /* Vector types need to re-check the target flags each time we report
2462 the machine mode. We need to do this because attribute target can
2463 change the result of vector_mode_supported_p and have_regs_of_mode
2464 on a per-function basis. Thus the TYPE_MODE of a VECTOR_TYPE can
2465 change on a per-function basis. */
2466 /* ??? Possibly a better solution is to run through all the types
2467 referenced by a function and re-compute the TYPE_MODE once, rather
2468 than make the TYPE_MODE macro call a function. */
2470 machine_mode
2471 vector_type_mode (const_tree t)
2473 machine_mode mode;
2475 gcc_assert (TREE_CODE (t) == VECTOR_TYPE);
2477 mode = t->type_common.mode;
2478 if (VECTOR_MODE_P (mode)
2479 && (!targetm.vector_mode_supported_p (mode)
2480 || !have_regs_of_mode[mode]))
2482 machine_mode innermode = TREE_TYPE (t)->type_common.mode;
2484 /* For integers, try mapping it to a same-sized scalar mode. */
2485 if (GET_MODE_CLASS (innermode) == MODE_INT)
2487 mode = mode_for_size (TYPE_VECTOR_SUBPARTS (t)
2488 * GET_MODE_BITSIZE (innermode), MODE_INT, 0);
2490 if (mode != VOIDmode && have_regs_of_mode[mode])
2491 return mode;
2494 return BLKmode;
2497 return mode;
2500 /* Create and return a type for signed integers of PRECISION bits. */
2502 tree
2503 make_signed_type (int precision)
2505 tree type = make_node (INTEGER_TYPE);
2507 TYPE_PRECISION (type) = precision;
2509 fixup_signed_type (type);
2510 return type;
2513 /* Create and return a type for unsigned integers of PRECISION bits. */
2515 tree
2516 make_unsigned_type (int precision)
2518 tree type = make_node (INTEGER_TYPE);
2520 TYPE_PRECISION (type) = precision;
2522 fixup_unsigned_type (type);
2523 return type;
2526 /* Create and return a type for fract of PRECISION bits, UNSIGNEDP,
2527 and SATP. */
2529 tree
2530 make_fract_type (int precision, int unsignedp, int satp)
2532 tree type = make_node (FIXED_POINT_TYPE);
2534 TYPE_PRECISION (type) = precision;
2536 if (satp)
2537 TYPE_SATURATING (type) = 1;
2539 /* Lay out the type: set its alignment, size, etc. */
2540 if (unsignedp)
2542 TYPE_UNSIGNED (type) = 1;
2543 SET_TYPE_MODE (type, mode_for_size (precision, MODE_UFRACT, 0));
2545 else
2546 SET_TYPE_MODE (type, mode_for_size (precision, MODE_FRACT, 0));
2547 layout_type (type);
2549 return type;
2552 /* Create and return a type for accum of PRECISION bits, UNSIGNEDP,
2553 and SATP. */
2555 tree
2556 make_accum_type (int precision, int unsignedp, int satp)
2558 tree type = make_node (FIXED_POINT_TYPE);
2560 TYPE_PRECISION (type) = precision;
2562 if (satp)
2563 TYPE_SATURATING (type) = 1;
2565 /* Lay out the type: set its alignment, size, etc. */
2566 if (unsignedp)
2568 TYPE_UNSIGNED (type) = 1;
2569 SET_TYPE_MODE (type, mode_for_size (precision, MODE_UACCUM, 0));
2571 else
2572 SET_TYPE_MODE (type, mode_for_size (precision, MODE_ACCUM, 0));
2573 layout_type (type);
2575 return type;
2578 /* Initialize sizetypes so layout_type can use them. */
2580 void
2581 initialize_sizetypes (void)
2583 int precision, bprecision;
2585 /* Get sizetypes precision from the SIZE_TYPE target macro. */
2586 if (strcmp (SIZETYPE, "unsigned int") == 0)
2587 precision = INT_TYPE_SIZE;
2588 else if (strcmp (SIZETYPE, "long unsigned int") == 0)
2589 precision = LONG_TYPE_SIZE;
2590 else if (strcmp (SIZETYPE, "long long unsigned int") == 0)
2591 precision = LONG_LONG_TYPE_SIZE;
2592 else if (strcmp (SIZETYPE, "short unsigned int") == 0)
2593 precision = SHORT_TYPE_SIZE;
2594 else
2596 int i;
2598 precision = -1;
2599 for (i = 0; i < NUM_INT_N_ENTS; i++)
2600 if (int_n_enabled_p[i])
2602 char name[50];
2603 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
2605 if (strcmp (name, SIZETYPE) == 0)
2607 precision = int_n_data[i].bitsize;
2610 if (precision == -1)
2611 gcc_unreachable ();
2614 bprecision
2615 = MIN (precision + BITS_PER_UNIT_LOG + 1, MAX_FIXED_MODE_SIZE);
2616 bprecision
2617 = GET_MODE_PRECISION (smallest_mode_for_size (bprecision, MODE_INT));
2618 if (bprecision > HOST_BITS_PER_DOUBLE_INT)
2619 bprecision = HOST_BITS_PER_DOUBLE_INT;
2621 /* Create stubs for sizetype and bitsizetype so we can create constants. */
2622 sizetype = make_node (INTEGER_TYPE);
2623 TYPE_NAME (sizetype) = get_identifier ("sizetype");
2624 TYPE_PRECISION (sizetype) = precision;
2625 TYPE_UNSIGNED (sizetype) = 1;
2626 bitsizetype = make_node (INTEGER_TYPE);
2627 TYPE_NAME (bitsizetype) = get_identifier ("bitsizetype");
2628 TYPE_PRECISION (bitsizetype) = bprecision;
2629 TYPE_UNSIGNED (bitsizetype) = 1;
2631 /* Now layout both types manually. */
2632 SET_TYPE_MODE (sizetype, smallest_mode_for_size (precision, MODE_INT));
2633 TYPE_ALIGN (sizetype) = GET_MODE_ALIGNMENT (TYPE_MODE (sizetype));
2634 TYPE_SIZE (sizetype) = bitsize_int (precision);
2635 TYPE_SIZE_UNIT (sizetype) = size_int (GET_MODE_SIZE (TYPE_MODE (sizetype)));
2636 set_min_and_max_values_for_integral_type (sizetype, precision, UNSIGNED);
2638 SET_TYPE_MODE (bitsizetype, smallest_mode_for_size (bprecision, MODE_INT));
2639 TYPE_ALIGN (bitsizetype) = GET_MODE_ALIGNMENT (TYPE_MODE (bitsizetype));
2640 TYPE_SIZE (bitsizetype) = bitsize_int (bprecision);
2641 TYPE_SIZE_UNIT (bitsizetype)
2642 = size_int (GET_MODE_SIZE (TYPE_MODE (bitsizetype)));
2643 set_min_and_max_values_for_integral_type (bitsizetype, bprecision, UNSIGNED);
2645 /* Create the signed variants of *sizetype. */
2646 ssizetype = make_signed_type (TYPE_PRECISION (sizetype));
2647 TYPE_NAME (ssizetype) = get_identifier ("ssizetype");
2648 sbitsizetype = make_signed_type (TYPE_PRECISION (bitsizetype));
2649 TYPE_NAME (sbitsizetype) = get_identifier ("sbitsizetype");
2652 /* TYPE is an integral type, i.e., an INTEGRAL_TYPE, ENUMERAL_TYPE
2653 or BOOLEAN_TYPE. Set TYPE_MIN_VALUE and TYPE_MAX_VALUE
2654 for TYPE, based on the PRECISION and whether or not the TYPE
2655 IS_UNSIGNED. PRECISION need not correspond to a width supported
2656 natively by the hardware; for example, on a machine with 8-bit,
2657 16-bit, and 32-bit register modes, PRECISION might be 7, 23, or
2658 61. */
2660 void
2661 set_min_and_max_values_for_integral_type (tree type,
2662 int precision,
2663 signop sgn)
2665 /* For bitfields with zero width we end up creating integer types
2666 with zero precision. Don't assign any minimum/maximum values
2667 to those types, they don't have any valid value. */
2668 if (precision < 1)
2669 return;
2671 TYPE_MIN_VALUE (type)
2672 = wide_int_to_tree (type, wi::min_value (precision, sgn));
2673 TYPE_MAX_VALUE (type)
2674 = wide_int_to_tree (type, wi::max_value (precision, sgn));
2677 /* Set the extreme values of TYPE based on its precision in bits,
2678 then lay it out. Used when make_signed_type won't do
2679 because the tree code is not INTEGER_TYPE.
2680 E.g. for Pascal, when the -fsigned-char option is given. */
2682 void
2683 fixup_signed_type (tree type)
2685 int precision = TYPE_PRECISION (type);
2687 set_min_and_max_values_for_integral_type (type, precision, SIGNED);
2689 /* Lay out the type: set its alignment, size, etc. */
2690 layout_type (type);
2693 /* Set the extreme values of TYPE based on its precision in bits,
2694 then lay it out. This is used both in `make_unsigned_type'
2695 and for enumeral types. */
2697 void
2698 fixup_unsigned_type (tree type)
2700 int precision = TYPE_PRECISION (type);
2702 TYPE_UNSIGNED (type) = 1;
2704 set_min_and_max_values_for_integral_type (type, precision, UNSIGNED);
2706 /* Lay out the type: set its alignment, size, etc. */
2707 layout_type (type);
2710 /* Construct an iterator for a bitfield that spans BITSIZE bits,
2711 starting at BITPOS.
2713 BITREGION_START is the bit position of the first bit in this
2714 sequence of bit fields. BITREGION_END is the last bit in this
2715 sequence. If these two fields are non-zero, we should restrict the
2716 memory access to that range. Otherwise, we are allowed to touch
2717 any adjacent non bit-fields.
2719 ALIGN is the alignment of the underlying object in bits.
2720 VOLATILEP says whether the bitfield is volatile. */
2722 bit_field_mode_iterator
2723 ::bit_field_mode_iterator (HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
2724 HOST_WIDE_INT bitregion_start,
2725 HOST_WIDE_INT bitregion_end,
2726 unsigned int align, bool volatilep)
2727 : m_mode (GET_CLASS_NARROWEST_MODE (MODE_INT)), m_bitsize (bitsize),
2728 m_bitpos (bitpos), m_bitregion_start (bitregion_start),
2729 m_bitregion_end (bitregion_end), m_align (align),
2730 m_volatilep (volatilep), m_count (0)
2732 if (!m_bitregion_end)
2734 /* We can assume that any aligned chunk of ALIGN bits that overlaps
2735 the bitfield is mapped and won't trap, provided that ALIGN isn't
2736 too large. The cap is the biggest required alignment for data,
2737 or at least the word size. And force one such chunk at least. */
2738 unsigned HOST_WIDE_INT units
2739 = MIN (align, MAX (BIGGEST_ALIGNMENT, BITS_PER_WORD));
2740 if (bitsize <= 0)
2741 bitsize = 1;
2742 m_bitregion_end = bitpos + bitsize + units - 1;
2743 m_bitregion_end -= m_bitregion_end % units + 1;
2747 /* Calls to this function return successively larger modes that can be used
2748 to represent the bitfield. Return true if another bitfield mode is
2749 available, storing it in *OUT_MODE if so. */
2751 bool
2752 bit_field_mode_iterator::next_mode (machine_mode *out_mode)
2754 for (; m_mode != VOIDmode; m_mode = GET_MODE_WIDER_MODE (m_mode))
2756 unsigned int unit = GET_MODE_BITSIZE (m_mode);
2758 /* Skip modes that don't have full precision. */
2759 if (unit != GET_MODE_PRECISION (m_mode))
2760 continue;
2762 /* Stop if the mode is too wide to handle efficiently. */
2763 if (unit > MAX_FIXED_MODE_SIZE)
2764 break;
2766 /* Don't deliver more than one multiword mode; the smallest one
2767 should be used. */
2768 if (m_count > 0 && unit > BITS_PER_WORD)
2769 break;
2771 /* Skip modes that are too small. */
2772 unsigned HOST_WIDE_INT substart = (unsigned HOST_WIDE_INT) m_bitpos % unit;
2773 unsigned HOST_WIDE_INT subend = substart + m_bitsize;
2774 if (subend > unit)
2775 continue;
2777 /* Stop if the mode goes outside the bitregion. */
2778 HOST_WIDE_INT start = m_bitpos - substart;
2779 if (m_bitregion_start && start < m_bitregion_start)
2780 break;
2781 HOST_WIDE_INT end = start + unit;
2782 if (end > m_bitregion_end + 1)
2783 break;
2785 /* Stop if the mode requires too much alignment. */
2786 if (GET_MODE_ALIGNMENT (m_mode) > m_align
2787 && SLOW_UNALIGNED_ACCESS (m_mode, m_align))
2788 break;
2790 *out_mode = m_mode;
2791 m_mode = GET_MODE_WIDER_MODE (m_mode);
2792 m_count++;
2793 return true;
2795 return false;
2798 /* Return true if smaller modes are generally preferred for this kind
2799 of bitfield. */
2801 bool
2802 bit_field_mode_iterator::prefer_smaller_modes ()
2804 return (m_volatilep
2805 ? targetm.narrow_volatile_bitfield ()
2806 : !SLOW_BYTE_ACCESS);
2809 /* Find the best machine mode to use when referencing a bit field of length
2810 BITSIZE bits starting at BITPOS.
2812 BITREGION_START is the bit position of the first bit in this
2813 sequence of bit fields. BITREGION_END is the last bit in this
2814 sequence. If these two fields are non-zero, we should restrict the
2815 memory access to that range. Otherwise, we are allowed to touch
2816 any adjacent non bit-fields.
2818 The underlying object is known to be aligned to a boundary of ALIGN bits.
2819 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
2820 larger than LARGEST_MODE (usually SImode).
2822 If no mode meets all these conditions, we return VOIDmode.
2824 If VOLATILEP is false and SLOW_BYTE_ACCESS is false, we return the
2825 smallest mode meeting these conditions.
2827 If VOLATILEP is false and SLOW_BYTE_ACCESS is true, we return the
2828 largest mode (but a mode no wider than UNITS_PER_WORD) that meets
2829 all the conditions.
2831 If VOLATILEP is true the narrow_volatile_bitfields target hook is used to
2832 decide which of the above modes should be used. */
2834 machine_mode
2835 get_best_mode (int bitsize, int bitpos,
2836 unsigned HOST_WIDE_INT bitregion_start,
2837 unsigned HOST_WIDE_INT bitregion_end,
2838 unsigned int align,
2839 machine_mode largest_mode, bool volatilep)
2841 bit_field_mode_iterator iter (bitsize, bitpos, bitregion_start,
2842 bitregion_end, align, volatilep);
2843 machine_mode widest_mode = VOIDmode;
2844 machine_mode mode;
2845 while (iter.next_mode (&mode)
2846 /* ??? For historical reasons, reject modes that would normally
2847 receive greater alignment, even if unaligned accesses are
2848 acceptable. This has both advantages and disadvantages.
2849 Removing this check means that something like:
2851 struct s { unsigned int x; unsigned int y; };
2852 int f (struct s *s) { return s->x == 0 && s->y == 0; }
2854 can be implemented using a single load and compare on
2855 64-bit machines that have no alignment restrictions.
2856 For example, on powerpc64-linux-gnu, we would generate:
2858 ld 3,0(3)
2859 cntlzd 3,3
2860 srdi 3,3,6
2863 rather than:
2865 lwz 9,0(3)
2866 cmpwi 7,9,0
2867 bne 7,.L3
2868 lwz 3,4(3)
2869 cntlzw 3,3
2870 srwi 3,3,5
2871 extsw 3,3
2873 .p2align 4,,15
2874 .L3:
2875 li 3,0
2878 However, accessing more than one field can make life harder
2879 for the gimple optimizers. For example, gcc.dg/vect/bb-slp-5.c
2880 has a series of unsigned short copies followed by a series of
2881 unsigned short comparisons. With this check, both the copies
2882 and comparisons remain 16-bit accesses and FRE is able
2883 to eliminate the latter. Without the check, the comparisons
2884 can be done using 2 64-bit operations, which FRE isn't able
2885 to handle in the same way.
2887 Either way, it would probably be worth disabling this check
2888 during expand. One particular example where removing the
2889 check would help is the get_best_mode call in store_bit_field.
2890 If we are given a memory bitregion of 128 bits that is aligned
2891 to a 64-bit boundary, and the bitfield we want to modify is
2892 in the second half of the bitregion, this check causes
2893 store_bitfield to turn the memory into a 64-bit reference
2894 to the _first_ half of the region. We later use
2895 adjust_bitfield_address to get a reference to the correct half,
2896 but doing so looks to adjust_bitfield_address as though we are
2897 moving past the end of the original object, so it drops the
2898 associated MEM_EXPR and MEM_OFFSET. Removing the check
2899 causes store_bit_field to keep a 128-bit memory reference,
2900 so that the final bitfield reference still has a MEM_EXPR
2901 and MEM_OFFSET. */
2902 && GET_MODE_ALIGNMENT (mode) <= align
2903 && (largest_mode == VOIDmode
2904 || GET_MODE_SIZE (mode) <= GET_MODE_SIZE (largest_mode)))
2906 widest_mode = mode;
2907 if (iter.prefer_smaller_modes ())
2908 break;
2910 return widest_mode;
2913 /* Gets minimal and maximal values for MODE (signed or unsigned depending on
2914 SIGN). The returned constants are made to be usable in TARGET_MODE. */
2916 void
2917 get_mode_bounds (machine_mode mode, int sign,
2918 machine_mode target_mode,
2919 rtx *mmin, rtx *mmax)
2921 unsigned size = GET_MODE_PRECISION (mode);
2922 unsigned HOST_WIDE_INT min_val, max_val;
2924 gcc_assert (size <= HOST_BITS_PER_WIDE_INT);
2926 /* Special case BImode, which has values 0 and STORE_FLAG_VALUE. */
2927 if (mode == BImode)
2929 if (STORE_FLAG_VALUE < 0)
2931 min_val = STORE_FLAG_VALUE;
2932 max_val = 0;
2934 else
2936 min_val = 0;
2937 max_val = STORE_FLAG_VALUE;
2940 else if (sign)
2942 min_val = -((unsigned HOST_WIDE_INT) 1 << (size - 1));
2943 max_val = ((unsigned HOST_WIDE_INT) 1 << (size - 1)) - 1;
2945 else
2947 min_val = 0;
2948 max_val = ((unsigned HOST_WIDE_INT) 1 << (size - 1) << 1) - 1;
2951 *mmin = gen_int_mode (min_val, target_mode);
2952 *mmax = gen_int_mode (max_val, target_mode);
2955 #include "gt-stor-layout.h"