138fd1498Szrj /* C-compiler utilities for types and variables storage layout
238fd1498Szrj Copyright (C) 1987-2018 Free Software Foundation, Inc.
338fd1498Szrj
438fd1498Szrj This file is part of GCC.
538fd1498Szrj
638fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
738fd1498Szrj the terms of the GNU General Public License as published by the Free
838fd1498Szrj Software Foundation; either version 3, or (at your option) any later
938fd1498Szrj version.
1038fd1498Szrj
1138fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
1238fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
1338fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1438fd1498Szrj for more details.
1538fd1498Szrj
1638fd1498Szrj You should have received a copy of the GNU General Public License
1738fd1498Szrj along with GCC; see the file COPYING3. If not see
1838fd1498Szrj <http://www.gnu.org/licenses/>. */
1938fd1498Szrj
2038fd1498Szrj
2138fd1498Szrj #include "config.h"
2238fd1498Szrj #include "system.h"
2338fd1498Szrj #include "coretypes.h"
2438fd1498Szrj #include "target.h"
2538fd1498Szrj #include "function.h"
2638fd1498Szrj #include "rtl.h"
2738fd1498Szrj #include "tree.h"
2838fd1498Szrj #include "memmodel.h"
2938fd1498Szrj #include "tm_p.h"
3038fd1498Szrj #include "stringpool.h"
3138fd1498Szrj #include "regs.h"
3238fd1498Szrj #include "emit-rtl.h"
3338fd1498Szrj #include "cgraph.h"
3438fd1498Szrj #include "diagnostic-core.h"
3538fd1498Szrj #include "fold-const.h"
3638fd1498Szrj #include "stor-layout.h"
3738fd1498Szrj #include "varasm.h"
3838fd1498Szrj #include "print-tree.h"
3938fd1498Szrj #include "langhooks.h"
4038fd1498Szrj #include "tree-inline.h"
4138fd1498Szrj #include "dumpfile.h"
4238fd1498Szrj #include "gimplify.h"
4338fd1498Szrj #include "attribs.h"
4438fd1498Szrj #include "debug.h"
4538fd1498Szrj
4638fd1498Szrj /* Data type for the expressions representing sizes of data types.
4738fd1498Szrj It is the first integer type laid out. */
4838fd1498Szrj tree sizetype_tab[(int) stk_type_kind_last];
4938fd1498Szrj
5038fd1498Szrj /* If nonzero, this is an upper limit on alignment of structure fields.
5138fd1498Szrj The value is measured in bits. */
5238fd1498Szrj unsigned int maximum_field_alignment = TARGET_DEFAULT_PACK_STRUCT * BITS_PER_UNIT;
5338fd1498Szrj
5438fd1498Szrj static tree self_referential_size (tree);
5538fd1498Szrj static void finalize_record_size (record_layout_info);
5638fd1498Szrj static void finalize_type_size (tree);
5738fd1498Szrj static void place_union_field (record_layout_info, tree);
5838fd1498Szrj static int excess_unit_span (HOST_WIDE_INT, HOST_WIDE_INT, HOST_WIDE_INT,
5938fd1498Szrj HOST_WIDE_INT, tree);
6038fd1498Szrj extern void debug_rli (record_layout_info);
6138fd1498Szrj
6238fd1498Szrj /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
6338fd1498Szrj to serve as the actual size-expression for a type or decl. */
6438fd1498Szrj
6538fd1498Szrj tree
variable_size(tree size)6638fd1498Szrj variable_size (tree size)
6738fd1498Szrj {
6838fd1498Szrj /* Obviously. */
6938fd1498Szrj if (TREE_CONSTANT (size))
7038fd1498Szrj return size;
7138fd1498Szrj
7238fd1498Szrj /* If the size is self-referential, we can't make a SAVE_EXPR (see
7338fd1498Szrj save_expr for the rationale). But we can do something else. */
7438fd1498Szrj if (CONTAINS_PLACEHOLDER_P (size))
7538fd1498Szrj return self_referential_size (size);
7638fd1498Szrj
7738fd1498Szrj /* If we are in the global binding level, we can't make a SAVE_EXPR
7838fd1498Szrj since it may end up being shared across functions, so it is up
7938fd1498Szrj to the front-end to deal with this case. */
8038fd1498Szrj if (lang_hooks.decls.global_bindings_p ())
8138fd1498Szrj return size;
8238fd1498Szrj
8338fd1498Szrj return save_expr (size);
8438fd1498Szrj }
8538fd1498Szrj
8638fd1498Szrj /* An array of functions used for self-referential size computation. */
8738fd1498Szrj static GTY(()) vec<tree, va_gc> *size_functions;
8838fd1498Szrj
8938fd1498Szrj /* Return true if T is a self-referential component reference. */
9038fd1498Szrj
9138fd1498Szrj static bool
self_referential_component_ref_p(tree t)9238fd1498Szrj self_referential_component_ref_p (tree t)
9338fd1498Szrj {
9438fd1498Szrj if (TREE_CODE (t) != COMPONENT_REF)
9538fd1498Szrj return false;
9638fd1498Szrj
9738fd1498Szrj while (REFERENCE_CLASS_P (t))
9838fd1498Szrj t = TREE_OPERAND (t, 0);
9938fd1498Szrj
10038fd1498Szrj return (TREE_CODE (t) == PLACEHOLDER_EXPR);
10138fd1498Szrj }
10238fd1498Szrj
10338fd1498Szrj /* Similar to copy_tree_r but do not copy component references involving
10438fd1498Szrj PLACEHOLDER_EXPRs. These nodes are spotted in find_placeholder_in_expr
10538fd1498Szrj and substituted in substitute_in_expr. */
10638fd1498Szrj
10738fd1498Szrj static tree
copy_self_referential_tree_r(tree * tp,int * walk_subtrees,void * data)10838fd1498Szrj copy_self_referential_tree_r (tree *tp, int *walk_subtrees, void *data)
10938fd1498Szrj {
11038fd1498Szrj enum tree_code code = TREE_CODE (*tp);
11138fd1498Szrj
11238fd1498Szrj /* Stop at types, decls, constants like copy_tree_r. */
11338fd1498Szrj if (TREE_CODE_CLASS (code) == tcc_type
11438fd1498Szrj || TREE_CODE_CLASS (code) == tcc_declaration
11538fd1498Szrj || TREE_CODE_CLASS (code) == tcc_constant)
11638fd1498Szrj {
11738fd1498Szrj *walk_subtrees = 0;
11838fd1498Szrj return NULL_TREE;
11938fd1498Szrj }
12038fd1498Szrj
12138fd1498Szrj /* This is the pattern built in ada/make_aligning_type. */
12238fd1498Szrj else if (code == ADDR_EXPR
12338fd1498Szrj && TREE_CODE (TREE_OPERAND (*tp, 0)) == PLACEHOLDER_EXPR)
12438fd1498Szrj {
12538fd1498Szrj *walk_subtrees = 0;
12638fd1498Szrj return NULL_TREE;
12738fd1498Szrj }
12838fd1498Szrj
12938fd1498Szrj /* Default case: the component reference. */
13038fd1498Szrj else if (self_referential_component_ref_p (*tp))
13138fd1498Szrj {
13238fd1498Szrj *walk_subtrees = 0;
13338fd1498Szrj return NULL_TREE;
13438fd1498Szrj }
13538fd1498Szrj
13638fd1498Szrj /* We're not supposed to have them in self-referential size trees
13738fd1498Szrj because we wouldn't properly control when they are evaluated.
13838fd1498Szrj However, not creating superfluous SAVE_EXPRs requires accurate
13938fd1498Szrj tracking of readonly-ness all the way down to here, which we
14038fd1498Szrj cannot always guarantee in practice. So punt in this case. */
14138fd1498Szrj else if (code == SAVE_EXPR)
14238fd1498Szrj return error_mark_node;
14338fd1498Szrj
14438fd1498Szrj else if (code == STATEMENT_LIST)
14538fd1498Szrj gcc_unreachable ();
14638fd1498Szrj
14738fd1498Szrj return copy_tree_r (tp, walk_subtrees, data);
14838fd1498Szrj }
14938fd1498Szrj
15038fd1498Szrj /* Given a SIZE expression that is self-referential, return an equivalent
15138fd1498Szrj expression to serve as the actual size expression for a type. */
15238fd1498Szrj
15338fd1498Szrj static tree
self_referential_size(tree size)15438fd1498Szrj self_referential_size (tree size)
15538fd1498Szrj {
15638fd1498Szrj static unsigned HOST_WIDE_INT fnno = 0;
15738fd1498Szrj vec<tree> self_refs = vNULL;
15838fd1498Szrj tree param_type_list = NULL, param_decl_list = NULL;
15938fd1498Szrj tree t, ref, return_type, fntype, fnname, fndecl;
16038fd1498Szrj unsigned int i;
16138fd1498Szrj char buf[128];
16238fd1498Szrj vec<tree, va_gc> *args = NULL;
16338fd1498Szrj
16438fd1498Szrj /* Do not factor out simple operations. */
16538fd1498Szrj t = skip_simple_constant_arithmetic (size);
16638fd1498Szrj if (TREE_CODE (t) == CALL_EXPR || self_referential_component_ref_p (t))
16738fd1498Szrj return size;
16838fd1498Szrj
16938fd1498Szrj /* Collect the list of self-references in the expression. */
17038fd1498Szrj find_placeholder_in_expr (size, &self_refs);
17138fd1498Szrj gcc_assert (self_refs.length () > 0);
17238fd1498Szrj
17338fd1498Szrj /* Obtain a private copy of the expression. */
17438fd1498Szrj t = size;
17538fd1498Szrj if (walk_tree (&t, copy_self_referential_tree_r, NULL, NULL) != NULL_TREE)
17638fd1498Szrj return size;
17738fd1498Szrj size = t;
17838fd1498Szrj
17938fd1498Szrj /* Build the parameter and argument lists in parallel; also
18038fd1498Szrj substitute the former for the latter in the expression. */
18138fd1498Szrj vec_alloc (args, self_refs.length ());
18238fd1498Szrj FOR_EACH_VEC_ELT (self_refs, i, ref)
18338fd1498Szrj {
18438fd1498Szrj tree subst, param_name, param_type, param_decl;
18538fd1498Szrj
18638fd1498Szrj if (DECL_P (ref))
18738fd1498Szrj {
18838fd1498Szrj /* We shouldn't have true variables here. */
18938fd1498Szrj gcc_assert (TREE_READONLY (ref));
19038fd1498Szrj subst = ref;
19138fd1498Szrj }
19238fd1498Szrj /* This is the pattern built in ada/make_aligning_type. */
19338fd1498Szrj else if (TREE_CODE (ref) == ADDR_EXPR)
19438fd1498Szrj subst = ref;
19538fd1498Szrj /* Default case: the component reference. */
19638fd1498Szrj else
19738fd1498Szrj subst = TREE_OPERAND (ref, 1);
19838fd1498Szrj
19938fd1498Szrj sprintf (buf, "p%d", i);
20038fd1498Szrj param_name = get_identifier (buf);
20138fd1498Szrj param_type = TREE_TYPE (ref);
20238fd1498Szrj param_decl
20338fd1498Szrj = build_decl (input_location, PARM_DECL, param_name, param_type);
20438fd1498Szrj DECL_ARG_TYPE (param_decl) = param_type;
20538fd1498Szrj DECL_ARTIFICIAL (param_decl) = 1;
20638fd1498Szrj TREE_READONLY (param_decl) = 1;
20738fd1498Szrj
20838fd1498Szrj size = substitute_in_expr (size, subst, param_decl);
20938fd1498Szrj
21038fd1498Szrj param_type_list = tree_cons (NULL_TREE, param_type, param_type_list);
21138fd1498Szrj param_decl_list = chainon (param_decl, param_decl_list);
21238fd1498Szrj args->quick_push (ref);
21338fd1498Szrj }
21438fd1498Szrj
21538fd1498Szrj self_refs.release ();
21638fd1498Szrj
21738fd1498Szrj /* Append 'void' to indicate that the number of parameters is fixed. */
21838fd1498Szrj param_type_list = tree_cons (NULL_TREE, void_type_node, param_type_list);
21938fd1498Szrj
22038fd1498Szrj /* The 3 lists have been created in reverse order. */
22138fd1498Szrj param_type_list = nreverse (param_type_list);
22238fd1498Szrj param_decl_list = nreverse (param_decl_list);
22338fd1498Szrj
22438fd1498Szrj /* Build the function type. */
22538fd1498Szrj return_type = TREE_TYPE (size);
22638fd1498Szrj fntype = build_function_type (return_type, param_type_list);
22738fd1498Szrj
22838fd1498Szrj /* Build the function declaration. */
22938fd1498Szrj sprintf (buf, "SZ" HOST_WIDE_INT_PRINT_UNSIGNED, fnno++);
23038fd1498Szrj fnname = get_file_function_name (buf);
23138fd1498Szrj fndecl = build_decl (input_location, FUNCTION_DECL, fnname, fntype);
23238fd1498Szrj for (t = param_decl_list; t; t = DECL_CHAIN (t))
23338fd1498Szrj DECL_CONTEXT (t) = fndecl;
23438fd1498Szrj DECL_ARGUMENTS (fndecl) = param_decl_list;
23538fd1498Szrj DECL_RESULT (fndecl)
23638fd1498Szrj = build_decl (input_location, RESULT_DECL, 0, return_type);
23738fd1498Szrj DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
23838fd1498Szrj
23938fd1498Szrj /* The function has been created by the compiler and we don't
24038fd1498Szrj want to emit debug info for it. */
24138fd1498Szrj DECL_ARTIFICIAL (fndecl) = 1;
24238fd1498Szrj DECL_IGNORED_P (fndecl) = 1;
24338fd1498Szrj
24438fd1498Szrj /* It is supposed to be "const" and never throw. */
24538fd1498Szrj TREE_READONLY (fndecl) = 1;
24638fd1498Szrj TREE_NOTHROW (fndecl) = 1;
24738fd1498Szrj
24838fd1498Szrj /* We want it to be inlined when this is deemed profitable, as
24938fd1498Szrj well as discarded if every call has been integrated. */
25038fd1498Szrj DECL_DECLARED_INLINE_P (fndecl) = 1;
25138fd1498Szrj
25238fd1498Szrj /* It is made up of a unique return statement. */
25338fd1498Szrj DECL_INITIAL (fndecl) = make_node (BLOCK);
25438fd1498Szrj BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
25538fd1498Szrj t = build2 (MODIFY_EXPR, return_type, DECL_RESULT (fndecl), size);
25638fd1498Szrj DECL_SAVED_TREE (fndecl) = build1 (RETURN_EXPR, void_type_node, t);
25738fd1498Szrj TREE_STATIC (fndecl) = 1;
25838fd1498Szrj
25938fd1498Szrj /* Put it onto the list of size functions. */
26038fd1498Szrj vec_safe_push (size_functions, fndecl);
26138fd1498Szrj
26238fd1498Szrj /* Replace the original expression with a call to the size function. */
26338fd1498Szrj return build_call_expr_loc_vec (UNKNOWN_LOCATION, fndecl, args);
26438fd1498Szrj }
26538fd1498Szrj
26638fd1498Szrj /* Take, queue and compile all the size functions. It is essential that
26738fd1498Szrj the size functions be gimplified at the very end of the compilation
26838fd1498Szrj in order to guarantee transparent handling of self-referential sizes.
26938fd1498Szrj Otherwise the GENERIC inliner would not be able to inline them back
27038fd1498Szrj at each of their call sites, thus creating artificial non-constant
27138fd1498Szrj size expressions which would trigger nasty problems later on. */
27238fd1498Szrj
27338fd1498Szrj void
finalize_size_functions(void)27438fd1498Szrj finalize_size_functions (void)
27538fd1498Szrj {
27638fd1498Szrj unsigned int i;
27738fd1498Szrj tree fndecl;
27838fd1498Szrj
27938fd1498Szrj for (i = 0; size_functions && size_functions->iterate (i, &fndecl); i++)
28038fd1498Szrj {
28138fd1498Szrj allocate_struct_function (fndecl, false);
28238fd1498Szrj set_cfun (NULL);
28338fd1498Szrj dump_function (TDI_original, fndecl);
28438fd1498Szrj
28538fd1498Szrj /* As these functions are used to describe the layout of variable-length
28638fd1498Szrj structures, debug info generation needs their implementation. */
28738fd1498Szrj debug_hooks->size_function (fndecl);
28838fd1498Szrj gimplify_function_tree (fndecl);
28938fd1498Szrj cgraph_node::finalize_function (fndecl, false);
29038fd1498Szrj }
29138fd1498Szrj
29238fd1498Szrj vec_free (size_functions);
29338fd1498Szrj }
29438fd1498Szrj
29538fd1498Szrj /* Return a machine mode of class MCLASS with SIZE bits of precision,
29638fd1498Szrj if one exists. The mode may have padding bits as well the SIZE
29738fd1498Szrj value bits. If LIMIT is nonzero, disregard modes wider than
29838fd1498Szrj MAX_FIXED_MODE_SIZE. */
29938fd1498Szrj
30038fd1498Szrj opt_machine_mode
mode_for_size(poly_uint64 size,enum mode_class mclass,int limit)30138fd1498Szrj mode_for_size (poly_uint64 size, enum mode_class mclass, int limit)
30238fd1498Szrj {
30338fd1498Szrj machine_mode mode;
30438fd1498Szrj int i;
30538fd1498Szrj
30638fd1498Szrj if (limit && maybe_gt (size, (unsigned int) MAX_FIXED_MODE_SIZE))
30738fd1498Szrj return opt_machine_mode ();
30838fd1498Szrj
30938fd1498Szrj /* Get the first mode which has this size, in the specified class. */
31038fd1498Szrj FOR_EACH_MODE_IN_CLASS (mode, mclass)
31138fd1498Szrj if (known_eq (GET_MODE_PRECISION (mode), size))
31238fd1498Szrj return mode;
31338fd1498Szrj
31438fd1498Szrj if (mclass == MODE_INT || mclass == MODE_PARTIAL_INT)
31538fd1498Szrj for (i = 0; i < NUM_INT_N_ENTS; i ++)
31638fd1498Szrj if (known_eq (int_n_data[i].bitsize, size)
31738fd1498Szrj && int_n_enabled_p[i])
31838fd1498Szrj return int_n_data[i].m;
31938fd1498Szrj
32038fd1498Szrj return opt_machine_mode ();
32138fd1498Szrj }
32238fd1498Szrj
32338fd1498Szrj /* Similar, except passed a tree node. */
32438fd1498Szrj
32538fd1498Szrj opt_machine_mode
mode_for_size_tree(const_tree size,enum mode_class mclass,int limit)32638fd1498Szrj mode_for_size_tree (const_tree size, enum mode_class mclass, int limit)
32738fd1498Szrj {
32838fd1498Szrj unsigned HOST_WIDE_INT uhwi;
32938fd1498Szrj unsigned int ui;
33038fd1498Szrj
33138fd1498Szrj if (!tree_fits_uhwi_p (size))
33238fd1498Szrj return opt_machine_mode ();
33338fd1498Szrj uhwi = tree_to_uhwi (size);
33438fd1498Szrj ui = uhwi;
33538fd1498Szrj if (uhwi != ui)
33638fd1498Szrj return opt_machine_mode ();
33738fd1498Szrj return mode_for_size (ui, mclass, limit);
33838fd1498Szrj }
33938fd1498Szrj
34038fd1498Szrj /* Return the narrowest mode of class MCLASS that contains at least
34138fd1498Szrj SIZE bits. Abort if no such mode exists. */
34238fd1498Szrj
34338fd1498Szrj machine_mode
smallest_mode_for_size(poly_uint64 size,enum mode_class mclass)34438fd1498Szrj smallest_mode_for_size (poly_uint64 size, enum mode_class mclass)
34538fd1498Szrj {
34638fd1498Szrj machine_mode mode = VOIDmode;
34738fd1498Szrj int i;
34838fd1498Szrj
34938fd1498Szrj /* Get the first mode which has at least this size, in the
35038fd1498Szrj specified class. */
35138fd1498Szrj FOR_EACH_MODE_IN_CLASS (mode, mclass)
35238fd1498Szrj if (known_ge (GET_MODE_PRECISION (mode), size))
35338fd1498Szrj break;
35438fd1498Szrj
35538fd1498Szrj gcc_assert (mode != VOIDmode);
35638fd1498Szrj
35738fd1498Szrj if (mclass == MODE_INT || mclass == MODE_PARTIAL_INT)
35838fd1498Szrj for (i = 0; i < NUM_INT_N_ENTS; i ++)
35938fd1498Szrj if (known_ge (int_n_data[i].bitsize, size)
36038fd1498Szrj && known_lt (int_n_data[i].bitsize, GET_MODE_PRECISION (mode))
36138fd1498Szrj && int_n_enabled_p[i])
36238fd1498Szrj mode = int_n_data[i].m;
36338fd1498Szrj
36438fd1498Szrj return mode;
36538fd1498Szrj }
36638fd1498Szrj
36738fd1498Szrj /* Return an integer mode of exactly the same size as MODE, if one exists. */
36838fd1498Szrj
36938fd1498Szrj opt_scalar_int_mode
int_mode_for_mode(machine_mode mode)37038fd1498Szrj int_mode_for_mode (machine_mode mode)
37138fd1498Szrj {
37238fd1498Szrj switch (GET_MODE_CLASS (mode))
37338fd1498Szrj {
37438fd1498Szrj case MODE_INT:
37538fd1498Szrj case MODE_PARTIAL_INT:
37638fd1498Szrj return as_a <scalar_int_mode> (mode);
37738fd1498Szrj
37838fd1498Szrj case MODE_COMPLEX_INT:
37938fd1498Szrj case MODE_COMPLEX_FLOAT:
38038fd1498Szrj case MODE_FLOAT:
38138fd1498Szrj case MODE_DECIMAL_FLOAT:
38238fd1498Szrj case MODE_FRACT:
38338fd1498Szrj case MODE_ACCUM:
38438fd1498Szrj case MODE_UFRACT:
38538fd1498Szrj case MODE_UACCUM:
38638fd1498Szrj case MODE_VECTOR_BOOL:
38738fd1498Szrj case MODE_VECTOR_INT:
38838fd1498Szrj case MODE_VECTOR_FLOAT:
38938fd1498Szrj case MODE_VECTOR_FRACT:
39038fd1498Szrj case MODE_VECTOR_ACCUM:
39138fd1498Szrj case MODE_VECTOR_UFRACT:
39238fd1498Szrj case MODE_VECTOR_UACCUM:
39338fd1498Szrj case MODE_POINTER_BOUNDS:
39438fd1498Szrj return int_mode_for_size (GET_MODE_BITSIZE (mode), 0);
39538fd1498Szrj
39638fd1498Szrj case MODE_RANDOM:
39738fd1498Szrj if (mode == BLKmode)
39838fd1498Szrj return opt_scalar_int_mode ();
39938fd1498Szrj
40038fd1498Szrj /* fall through */
40138fd1498Szrj
40238fd1498Szrj case MODE_CC:
40338fd1498Szrj default:
40438fd1498Szrj gcc_unreachable ();
40538fd1498Szrj }
40638fd1498Szrj }
40738fd1498Szrj
40838fd1498Szrj /* Find a mode that can be used for efficient bitwise operations on MODE,
40938fd1498Szrj if one exists. */
41038fd1498Szrj
41138fd1498Szrj opt_machine_mode
bitwise_mode_for_mode(machine_mode mode)41238fd1498Szrj bitwise_mode_for_mode (machine_mode mode)
41338fd1498Szrj {
41438fd1498Szrj /* Quick exit if we already have a suitable mode. */
41538fd1498Szrj scalar_int_mode int_mode;
41638fd1498Szrj if (is_a <scalar_int_mode> (mode, &int_mode)
41738fd1498Szrj && GET_MODE_BITSIZE (int_mode) <= MAX_FIXED_MODE_SIZE)
41838fd1498Szrj return int_mode;
41938fd1498Szrj
42038fd1498Szrj /* Reuse the sanity checks from int_mode_for_mode. */
42138fd1498Szrj gcc_checking_assert ((int_mode_for_mode (mode), true));
42238fd1498Szrj
42338fd1498Szrj poly_int64 bitsize = GET_MODE_BITSIZE (mode);
42438fd1498Szrj
42538fd1498Szrj /* Try to replace complex modes with complex modes. In general we
42638fd1498Szrj expect both components to be processed independently, so we only
42738fd1498Szrj care whether there is a register for the inner mode. */
42838fd1498Szrj if (COMPLEX_MODE_P (mode))
42938fd1498Szrj {
43038fd1498Szrj machine_mode trial = mode;
43138fd1498Szrj if ((GET_MODE_CLASS (trial) == MODE_COMPLEX_INT
43238fd1498Szrj || mode_for_size (bitsize, MODE_COMPLEX_INT, false).exists (&trial))
43338fd1498Szrj && have_regs_of_mode[GET_MODE_INNER (trial)])
43438fd1498Szrj return trial;
43538fd1498Szrj }
43638fd1498Szrj
43738fd1498Szrj /* Try to replace vector modes with vector modes. Also try using vector
43838fd1498Szrj modes if an integer mode would be too big. */
43938fd1498Szrj if (VECTOR_MODE_P (mode)
44038fd1498Szrj || maybe_gt (bitsize, MAX_FIXED_MODE_SIZE))
44138fd1498Szrj {
44238fd1498Szrj machine_mode trial = mode;
44338fd1498Szrj if ((GET_MODE_CLASS (trial) == MODE_VECTOR_INT
44438fd1498Szrj || mode_for_size (bitsize, MODE_VECTOR_INT, 0).exists (&trial))
44538fd1498Szrj && have_regs_of_mode[trial]
44638fd1498Szrj && targetm.vector_mode_supported_p (trial))
44738fd1498Szrj return trial;
44838fd1498Szrj }
44938fd1498Szrj
45038fd1498Szrj /* Otherwise fall back on integers while honoring MAX_FIXED_MODE_SIZE. */
45138fd1498Szrj return mode_for_size (bitsize, MODE_INT, true);
45238fd1498Szrj }
45338fd1498Szrj
45438fd1498Szrj /* Find a type that can be used for efficient bitwise operations on MODE.
45538fd1498Szrj Return null if no such mode exists. */
45638fd1498Szrj
45738fd1498Szrj tree
bitwise_type_for_mode(machine_mode mode)45838fd1498Szrj bitwise_type_for_mode (machine_mode mode)
45938fd1498Szrj {
46038fd1498Szrj if (!bitwise_mode_for_mode (mode).exists (&mode))
46138fd1498Szrj return NULL_TREE;
46238fd1498Szrj
46338fd1498Szrj unsigned int inner_size = GET_MODE_UNIT_BITSIZE (mode);
46438fd1498Szrj tree inner_type = build_nonstandard_integer_type (inner_size, true);
46538fd1498Szrj
46638fd1498Szrj if (VECTOR_MODE_P (mode))
46738fd1498Szrj return build_vector_type_for_mode (inner_type, mode);
46838fd1498Szrj
46938fd1498Szrj if (COMPLEX_MODE_P (mode))
47038fd1498Szrj return build_complex_type (inner_type);
47138fd1498Szrj
47238fd1498Szrj gcc_checking_assert (GET_MODE_INNER (mode) == mode);
47338fd1498Szrj return inner_type;
47438fd1498Szrj }
47538fd1498Szrj
47638fd1498Szrj /* Find a mode that is suitable for representing a vector with NUNITS
47738fd1498Szrj elements of mode INNERMODE, if one exists. The returned mode can be
47838fd1498Szrj either an integer mode or a vector mode. */
47938fd1498Szrj
48038fd1498Szrj opt_machine_mode
mode_for_vector(scalar_mode innermode,poly_uint64 nunits)48138fd1498Szrj mode_for_vector (scalar_mode innermode, poly_uint64 nunits)
48238fd1498Szrj {
48338fd1498Szrj machine_mode mode;
48438fd1498Szrj
48538fd1498Szrj /* First, look for a supported vector type. */
48638fd1498Szrj if (SCALAR_FLOAT_MODE_P (innermode))
48738fd1498Szrj mode = MIN_MODE_VECTOR_FLOAT;
48838fd1498Szrj else if (SCALAR_FRACT_MODE_P (innermode))
48938fd1498Szrj mode = MIN_MODE_VECTOR_FRACT;
49038fd1498Szrj else if (SCALAR_UFRACT_MODE_P (innermode))
49138fd1498Szrj mode = MIN_MODE_VECTOR_UFRACT;
49238fd1498Szrj else if (SCALAR_ACCUM_MODE_P (innermode))
49338fd1498Szrj mode = MIN_MODE_VECTOR_ACCUM;
49438fd1498Szrj else if (SCALAR_UACCUM_MODE_P (innermode))
49538fd1498Szrj mode = MIN_MODE_VECTOR_UACCUM;
49638fd1498Szrj else
49738fd1498Szrj mode = MIN_MODE_VECTOR_INT;
49838fd1498Szrj
49938fd1498Szrj /* Do not check vector_mode_supported_p here. We'll do that
50038fd1498Szrj later in vector_type_mode. */
50138fd1498Szrj FOR_EACH_MODE_FROM (mode, mode)
50238fd1498Szrj if (known_eq (GET_MODE_NUNITS (mode), nunits)
50338fd1498Szrj && GET_MODE_INNER (mode) == innermode)
50438fd1498Szrj return mode;
50538fd1498Szrj
50638fd1498Szrj /* For integers, try mapping it to a same-sized scalar mode. */
50738fd1498Szrj if (GET_MODE_CLASS (innermode) == MODE_INT)
50838fd1498Szrj {
50938fd1498Szrj poly_uint64 nbits = nunits * GET_MODE_BITSIZE (innermode);
51038fd1498Szrj if (int_mode_for_size (nbits, 0).exists (&mode)
51138fd1498Szrj && have_regs_of_mode[mode])
51238fd1498Szrj return mode;
51338fd1498Szrj }
51438fd1498Szrj
51538fd1498Szrj return opt_machine_mode ();
51638fd1498Szrj }
51738fd1498Szrj
51838fd1498Szrj /* Return the mode for a vector that has NUNITS integer elements of
51938fd1498Szrj INT_BITS bits each, if such a mode exists. The mode can be either
52038fd1498Szrj an integer mode or a vector mode. */
52138fd1498Szrj
52238fd1498Szrj opt_machine_mode
mode_for_int_vector(unsigned int int_bits,poly_uint64 nunits)52338fd1498Szrj mode_for_int_vector (unsigned int int_bits, poly_uint64 nunits)
52438fd1498Szrj {
52538fd1498Szrj scalar_int_mode int_mode;
52638fd1498Szrj machine_mode vec_mode;
52738fd1498Szrj if (int_mode_for_size (int_bits, 0).exists (&int_mode)
52838fd1498Szrj && mode_for_vector (int_mode, nunits).exists (&vec_mode))
52938fd1498Szrj return vec_mode;
53038fd1498Szrj return opt_machine_mode ();
53138fd1498Szrj }
53238fd1498Szrj
53338fd1498Szrj /* Return the alignment of MODE. This will be bounded by 1 and
53438fd1498Szrj BIGGEST_ALIGNMENT. */
53538fd1498Szrj
53638fd1498Szrj unsigned int
get_mode_alignment(machine_mode mode)53738fd1498Szrj get_mode_alignment (machine_mode mode)
53838fd1498Szrj {
53938fd1498Szrj return MIN (BIGGEST_ALIGNMENT, MAX (1, mode_base_align[mode]*BITS_PER_UNIT));
54038fd1498Szrj }
54138fd1498Szrj
54238fd1498Szrj /* Return the natural mode of an array, given that it is SIZE bytes in
54338fd1498Szrj total and has elements of type ELEM_TYPE. */
54438fd1498Szrj
54538fd1498Szrj static machine_mode
mode_for_array(tree elem_type,tree size)54638fd1498Szrj mode_for_array (tree elem_type, tree size)
54738fd1498Szrj {
54838fd1498Szrj tree elem_size;
54938fd1498Szrj poly_uint64 int_size, int_elem_size;
55038fd1498Szrj unsigned HOST_WIDE_INT num_elems;
55138fd1498Szrj bool limit_p;
55238fd1498Szrj
55338fd1498Szrj /* One-element arrays get the component type's mode. */
55438fd1498Szrj elem_size = TYPE_SIZE (elem_type);
55538fd1498Szrj if (simple_cst_equal (size, elem_size))
55638fd1498Szrj return TYPE_MODE (elem_type);
55738fd1498Szrj
55838fd1498Szrj limit_p = true;
55938fd1498Szrj if (poly_int_tree_p (size, &int_size)
56038fd1498Szrj && poly_int_tree_p (elem_size, &int_elem_size)
56138fd1498Szrj && maybe_ne (int_elem_size, 0U)
56238fd1498Szrj && constant_multiple_p (int_size, int_elem_size, &num_elems))
56338fd1498Szrj {
56438fd1498Szrj machine_mode elem_mode = TYPE_MODE (elem_type);
56538fd1498Szrj machine_mode mode;
56638fd1498Szrj if (targetm.array_mode (elem_mode, num_elems).exists (&mode))
56738fd1498Szrj return mode;
56838fd1498Szrj if (targetm.array_mode_supported_p (elem_mode, num_elems))
56938fd1498Szrj limit_p = false;
57038fd1498Szrj }
57138fd1498Szrj return mode_for_size_tree (size, MODE_INT, limit_p).else_blk ();
57238fd1498Szrj }
57338fd1498Szrj
57438fd1498Szrj /* Subroutine of layout_decl: Force alignment required for the data type.
57538fd1498Szrj But if the decl itself wants greater alignment, don't override that. */
57638fd1498Szrj
57738fd1498Szrj static inline void
do_type_align(tree type,tree decl)57838fd1498Szrj do_type_align (tree type, tree decl)
57938fd1498Szrj {
58038fd1498Szrj if (TYPE_ALIGN (type) > DECL_ALIGN (decl))
58138fd1498Szrj {
58238fd1498Szrj SET_DECL_ALIGN (decl, TYPE_ALIGN (type));
58338fd1498Szrj if (TREE_CODE (decl) == FIELD_DECL)
58438fd1498Szrj DECL_USER_ALIGN (decl) = TYPE_USER_ALIGN (type);
58538fd1498Szrj }
58638fd1498Szrj if (TYPE_WARN_IF_NOT_ALIGN (type) > DECL_WARN_IF_NOT_ALIGN (decl))
58738fd1498Szrj SET_DECL_WARN_IF_NOT_ALIGN (decl, TYPE_WARN_IF_NOT_ALIGN (type));
58838fd1498Szrj }
58938fd1498Szrj
59038fd1498Szrj /* Set the size, mode and alignment of a ..._DECL node.
59138fd1498Szrj TYPE_DECL does need this for C++.
59238fd1498Szrj Note that LABEL_DECL and CONST_DECL nodes do not need this,
59338fd1498Szrj and FUNCTION_DECL nodes have them set up in a special (and simple) way.
59438fd1498Szrj Don't call layout_decl for them.
59538fd1498Szrj
59638fd1498Szrj KNOWN_ALIGN is the amount of alignment we can assume this
59738fd1498Szrj decl has with no special effort. It is relevant only for FIELD_DECLs
59838fd1498Szrj and depends on the previous fields.
59938fd1498Szrj All that matters about KNOWN_ALIGN is which powers of 2 divide it.
60038fd1498Szrj If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
60138fd1498Szrj the record will be aligned to suit. */
60238fd1498Szrj
60338fd1498Szrj void
layout_decl(tree decl,unsigned int known_align)60438fd1498Szrj layout_decl (tree decl, unsigned int known_align)
60538fd1498Szrj {
60638fd1498Szrj tree type = TREE_TYPE (decl);
60738fd1498Szrj enum tree_code code = TREE_CODE (decl);
60838fd1498Szrj rtx rtl = NULL_RTX;
60938fd1498Szrj location_t loc = DECL_SOURCE_LOCATION (decl);
61038fd1498Szrj
61138fd1498Szrj if (code == CONST_DECL)
61238fd1498Szrj return;
61338fd1498Szrj
61438fd1498Szrj gcc_assert (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL
61538fd1498Szrj || code == TYPE_DECL || code == FIELD_DECL);
61638fd1498Szrj
61738fd1498Szrj rtl = DECL_RTL_IF_SET (decl);
61838fd1498Szrj
61938fd1498Szrj if (type == error_mark_node)
62038fd1498Szrj type = void_type_node;
62138fd1498Szrj
62238fd1498Szrj /* Usually the size and mode come from the data type without change,
62338fd1498Szrj however, the front-end may set the explicit width of the field, so its
62438fd1498Szrj size may not be the same as the size of its type. This happens with
62538fd1498Szrj bitfields, of course (an `int' bitfield may be only 2 bits, say), but it
62638fd1498Szrj also happens with other fields. For example, the C++ front-end creates
62738fd1498Szrj zero-sized fields corresponding to empty base classes, and depends on
62838fd1498Szrj layout_type setting DECL_FIELD_BITPOS correctly for the field. Set the
62938fd1498Szrj size in bytes from the size in bits. If we have already set the mode,
63038fd1498Szrj don't set it again since we can be called twice for FIELD_DECLs. */
63138fd1498Szrj
63238fd1498Szrj DECL_UNSIGNED (decl) = TYPE_UNSIGNED (type);
63338fd1498Szrj if (DECL_MODE (decl) == VOIDmode)
63438fd1498Szrj SET_DECL_MODE (decl, TYPE_MODE (type));
63538fd1498Szrj
63638fd1498Szrj if (DECL_SIZE (decl) == 0)
63738fd1498Szrj {
63838fd1498Szrj DECL_SIZE (decl) = TYPE_SIZE (type);
63938fd1498Szrj DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
64038fd1498Szrj }
64138fd1498Szrj else if (DECL_SIZE_UNIT (decl) == 0)
64238fd1498Szrj DECL_SIZE_UNIT (decl)
64338fd1498Szrj = fold_convert_loc (loc, sizetype,
64438fd1498Szrj size_binop_loc (loc, CEIL_DIV_EXPR, DECL_SIZE (decl),
64538fd1498Szrj bitsize_unit_node));
64638fd1498Szrj
64738fd1498Szrj if (code != FIELD_DECL)
64838fd1498Szrj /* For non-fields, update the alignment from the type. */
64938fd1498Szrj do_type_align (type, decl);
65038fd1498Szrj else
65138fd1498Szrj /* For fields, it's a bit more complicated... */
65238fd1498Szrj {
65338fd1498Szrj bool old_user_align = DECL_USER_ALIGN (decl);
65438fd1498Szrj bool zero_bitfield = false;
65538fd1498Szrj bool packed_p = DECL_PACKED (decl);
65638fd1498Szrj unsigned int mfa;
65738fd1498Szrj
65838fd1498Szrj if (DECL_BIT_FIELD (decl))
65938fd1498Szrj {
66038fd1498Szrj DECL_BIT_FIELD_TYPE (decl) = type;
66138fd1498Szrj
66238fd1498Szrj /* A zero-length bit-field affects the alignment of the next
66338fd1498Szrj field. In essence such bit-fields are not influenced by
66438fd1498Szrj any packing due to #pragma pack or attribute packed. */
66538fd1498Szrj if (integer_zerop (DECL_SIZE (decl))
66638fd1498Szrj && ! targetm.ms_bitfield_layout_p (DECL_FIELD_CONTEXT (decl)))
66738fd1498Szrj {
66838fd1498Szrj zero_bitfield = true;
66938fd1498Szrj packed_p = false;
67038fd1498Szrj if (PCC_BITFIELD_TYPE_MATTERS)
67138fd1498Szrj do_type_align (type, decl);
67238fd1498Szrj else
67338fd1498Szrj {
67438fd1498Szrj #ifdef EMPTY_FIELD_BOUNDARY
67538fd1498Szrj if (EMPTY_FIELD_BOUNDARY > DECL_ALIGN (decl))
67638fd1498Szrj {
67738fd1498Szrj SET_DECL_ALIGN (decl, EMPTY_FIELD_BOUNDARY);
67838fd1498Szrj DECL_USER_ALIGN (decl) = 0;
67938fd1498Szrj }
68038fd1498Szrj #endif
68138fd1498Szrj }
68238fd1498Szrj }
68338fd1498Szrj
68438fd1498Szrj /* See if we can use an ordinary integer mode for a bit-field.
68538fd1498Szrj Conditions are: a fixed size that is correct for another mode,
68638fd1498Szrj occupying a complete byte or bytes on proper boundary. */
68738fd1498Szrj if (TYPE_SIZE (type) != 0
68838fd1498Szrj && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
68938fd1498Szrj && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
69038fd1498Szrj {
69138fd1498Szrj machine_mode xmode;
69238fd1498Szrj if (mode_for_size_tree (DECL_SIZE (decl),
69338fd1498Szrj MODE_INT, 1).exists (&xmode))
69438fd1498Szrj {
69538fd1498Szrj unsigned int xalign = GET_MODE_ALIGNMENT (xmode);
69638fd1498Szrj if (!(xalign > BITS_PER_UNIT && DECL_PACKED (decl))
69738fd1498Szrj && (known_align == 0 || known_align >= xalign))
69838fd1498Szrj {
69938fd1498Szrj SET_DECL_ALIGN (decl, MAX (xalign, DECL_ALIGN (decl)));
70038fd1498Szrj SET_DECL_MODE (decl, xmode);
70138fd1498Szrj DECL_BIT_FIELD (decl) = 0;
70238fd1498Szrj }
70338fd1498Szrj }
70438fd1498Szrj }
70538fd1498Szrj
70638fd1498Szrj /* Turn off DECL_BIT_FIELD if we won't need it set. */
70738fd1498Szrj if (TYPE_MODE (type) == BLKmode && DECL_MODE (decl) == BLKmode
70838fd1498Szrj && known_align >= TYPE_ALIGN (type)
70938fd1498Szrj && DECL_ALIGN (decl) >= TYPE_ALIGN (type))
71038fd1498Szrj DECL_BIT_FIELD (decl) = 0;
71138fd1498Szrj }
71238fd1498Szrj else if (packed_p && DECL_USER_ALIGN (decl))
71338fd1498Szrj /* Don't touch DECL_ALIGN. For other packed fields, go ahead and
71438fd1498Szrj round up; we'll reduce it again below. We want packing to
71538fd1498Szrj supersede USER_ALIGN inherited from the type, but defer to
71638fd1498Szrj alignment explicitly specified on the field decl. */;
71738fd1498Szrj else
71838fd1498Szrj do_type_align (type, decl);
71938fd1498Szrj
72038fd1498Szrj /* If the field is packed and not explicitly aligned, give it the
72138fd1498Szrj minimum alignment. Note that do_type_align may set
72238fd1498Szrj DECL_USER_ALIGN, so we need to check old_user_align instead. */
72338fd1498Szrj if (packed_p
72438fd1498Szrj && !old_user_align)
72538fd1498Szrj SET_DECL_ALIGN (decl, MIN (DECL_ALIGN (decl), BITS_PER_UNIT));
72638fd1498Szrj
72738fd1498Szrj if (! packed_p && ! DECL_USER_ALIGN (decl))
72838fd1498Szrj {
72938fd1498Szrj /* Some targets (i.e. i386, VMS) limit struct field alignment
73038fd1498Szrj to a lower boundary than alignment of variables unless
73138fd1498Szrj it was overridden by attribute aligned. */
73238fd1498Szrj #ifdef BIGGEST_FIELD_ALIGNMENT
73338fd1498Szrj SET_DECL_ALIGN (decl, MIN (DECL_ALIGN (decl),
73438fd1498Szrj (unsigned) BIGGEST_FIELD_ALIGNMENT));
73538fd1498Szrj #endif
73638fd1498Szrj #ifdef ADJUST_FIELD_ALIGN
73738fd1498Szrj SET_DECL_ALIGN (decl, ADJUST_FIELD_ALIGN (decl, TREE_TYPE (decl),
73838fd1498Szrj DECL_ALIGN (decl)));
73938fd1498Szrj #endif
74038fd1498Szrj }
74138fd1498Szrj
74238fd1498Szrj if (zero_bitfield)
74338fd1498Szrj mfa = initial_max_fld_align * BITS_PER_UNIT;
74438fd1498Szrj else
74538fd1498Szrj mfa = maximum_field_alignment;
74638fd1498Szrj /* Should this be controlled by DECL_USER_ALIGN, too? */
74738fd1498Szrj if (mfa != 0)
74838fd1498Szrj SET_DECL_ALIGN (decl, MIN (DECL_ALIGN (decl), mfa));
74938fd1498Szrj }
75038fd1498Szrj
75138fd1498Szrj /* Evaluate nonconstant size only once, either now or as soon as safe. */
75238fd1498Szrj if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
75338fd1498Szrj DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
75438fd1498Szrj if (DECL_SIZE_UNIT (decl) != 0
75538fd1498Szrj && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST)
75638fd1498Szrj DECL_SIZE_UNIT (decl) = variable_size (DECL_SIZE_UNIT (decl));
75738fd1498Szrj
75838fd1498Szrj /* If requested, warn about definitions of large data objects. */
75938fd1498Szrj if (warn_larger_than
76038fd1498Szrj && (code == VAR_DECL || code == PARM_DECL)
76138fd1498Szrj && ! DECL_EXTERNAL (decl))
76238fd1498Szrj {
76338fd1498Szrj tree size = DECL_SIZE_UNIT (decl);
76438fd1498Szrj
76538fd1498Szrj if (size != 0 && TREE_CODE (size) == INTEGER_CST
76638fd1498Szrj && compare_tree_int (size, larger_than_size) > 0)
76738fd1498Szrj {
76838fd1498Szrj int size_as_int = TREE_INT_CST_LOW (size);
76938fd1498Szrj
77038fd1498Szrj if (compare_tree_int (size, size_as_int) == 0)
77138fd1498Szrj warning (OPT_Wlarger_than_, "size of %q+D is %d bytes", decl, size_as_int);
77238fd1498Szrj else
77338fd1498Szrj warning (OPT_Wlarger_than_, "size of %q+D is larger than %wd bytes",
77438fd1498Szrj decl, larger_than_size);
77538fd1498Szrj }
77638fd1498Szrj }
77738fd1498Szrj
77838fd1498Szrj /* If the RTL was already set, update its mode and mem attributes. */
77938fd1498Szrj if (rtl)
78038fd1498Szrj {
78138fd1498Szrj PUT_MODE (rtl, DECL_MODE (decl));
78238fd1498Szrj SET_DECL_RTL (decl, 0);
78338fd1498Szrj if (MEM_P (rtl))
78438fd1498Szrj set_mem_attributes (rtl, decl, 1);
78538fd1498Szrj SET_DECL_RTL (decl, rtl);
78638fd1498Szrj }
78738fd1498Szrj }
78838fd1498Szrj
78938fd1498Szrj /* Given a VAR_DECL, PARM_DECL, RESULT_DECL, or FIELD_DECL, clears the
79038fd1498Szrj results of a previous call to layout_decl and calls it again. */
79138fd1498Szrj
79238fd1498Szrj void
relayout_decl(tree decl)79338fd1498Szrj relayout_decl (tree decl)
79438fd1498Szrj {
79538fd1498Szrj DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
79638fd1498Szrj SET_DECL_MODE (decl, VOIDmode);
79738fd1498Szrj if (!DECL_USER_ALIGN (decl))
79838fd1498Szrj SET_DECL_ALIGN (decl, 0);
79938fd1498Szrj if (DECL_RTL_SET_P (decl))
80038fd1498Szrj SET_DECL_RTL (decl, 0);
80138fd1498Szrj
80238fd1498Szrj layout_decl (decl, 0);
80338fd1498Szrj }
80438fd1498Szrj
80538fd1498Szrj /* Begin laying out type T, which may be a RECORD_TYPE, UNION_TYPE, or
80638fd1498Szrj QUAL_UNION_TYPE. Return a pointer to a struct record_layout_info which
80738fd1498Szrj is to be passed to all other layout functions for this record. It is the
80838fd1498Szrj responsibility of the caller to call `free' for the storage returned.
80938fd1498Szrj Note that garbage collection is not permitted until we finish laying
81038fd1498Szrj out the record. */
81138fd1498Szrj
81238fd1498Szrj record_layout_info
start_record_layout(tree t)81338fd1498Szrj start_record_layout (tree t)
81438fd1498Szrj {
81538fd1498Szrj record_layout_info rli = XNEW (struct record_layout_info_s);
81638fd1498Szrj
81738fd1498Szrj rli->t = t;
81838fd1498Szrj
81938fd1498Szrj /* If the type has a minimum specified alignment (via an attribute
82038fd1498Szrj declaration, for example) use it -- otherwise, start with a
82138fd1498Szrj one-byte alignment. */
82238fd1498Szrj rli->record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (t));
82338fd1498Szrj rli->unpacked_align = rli->record_align;
82438fd1498Szrj rli->offset_align = MAX (rli->record_align, BIGGEST_ALIGNMENT);
82538fd1498Szrj
82638fd1498Szrj #ifdef STRUCTURE_SIZE_BOUNDARY
82738fd1498Szrj /* Packed structures don't need to have minimum size. */
82838fd1498Szrj if (! TYPE_PACKED (t))
82938fd1498Szrj {
83038fd1498Szrj unsigned tmp;
83138fd1498Szrj
83238fd1498Szrj /* #pragma pack overrides STRUCTURE_SIZE_BOUNDARY. */
83338fd1498Szrj tmp = (unsigned) STRUCTURE_SIZE_BOUNDARY;
83438fd1498Szrj if (maximum_field_alignment != 0)
83538fd1498Szrj tmp = MIN (tmp, maximum_field_alignment);
83638fd1498Szrj rli->record_align = MAX (rli->record_align, tmp);
83738fd1498Szrj }
83838fd1498Szrj #endif
83938fd1498Szrj
84038fd1498Szrj rli->offset = size_zero_node;
84138fd1498Szrj rli->bitpos = bitsize_zero_node;
84238fd1498Szrj rli->prev_field = 0;
84338fd1498Szrj rli->pending_statics = 0;
84438fd1498Szrj rli->packed_maybe_necessary = 0;
84538fd1498Szrj rli->remaining_in_alignment = 0;
84638fd1498Szrj
84738fd1498Szrj return rli;
84838fd1498Szrj }
84938fd1498Szrj
85038fd1498Szrj /* Fold sizetype value X to bitsizetype, given that X represents a type
85138fd1498Szrj size or offset. */
85238fd1498Szrj
85338fd1498Szrj static tree
bits_from_bytes(tree x)85438fd1498Szrj bits_from_bytes (tree x)
85538fd1498Szrj {
85638fd1498Szrj if (POLY_INT_CST_P (x))
85738fd1498Szrj /* The runtime calculation isn't allowed to overflow sizetype;
85838fd1498Szrj increasing the runtime values must always increase the size
85938fd1498Szrj or offset of the object. This means that the object imposes
86038fd1498Szrj a maximum value on the runtime parameters, but we don't record
86138fd1498Szrj what that is. */
86238fd1498Szrj return build_poly_int_cst
86338fd1498Szrj (bitsizetype,
86438fd1498Szrj poly_wide_int::from (poly_int_cst_value (x),
86538fd1498Szrj TYPE_PRECISION (bitsizetype),
86638fd1498Szrj TYPE_SIGN (TREE_TYPE (x))));
86738fd1498Szrj x = fold_convert (bitsizetype, x);
86838fd1498Szrj gcc_checking_assert (x);
86938fd1498Szrj return x;
87038fd1498Szrj }
87138fd1498Szrj
87238fd1498Szrj /* Return the combined bit position for the byte offset OFFSET and the
87338fd1498Szrj bit position BITPOS.
87438fd1498Szrj
87538fd1498Szrj These functions operate on byte and bit positions present in FIELD_DECLs
87638fd1498Szrj and assume that these expressions result in no (intermediate) overflow.
87738fd1498Szrj This assumption is necessary to fold the expressions as much as possible,
87838fd1498Szrj so as to avoid creating artificially variable-sized types in languages
87938fd1498Szrj supporting variable-sized types like Ada. */
88038fd1498Szrj
88138fd1498Szrj tree
bit_from_pos(tree offset,tree bitpos)88238fd1498Szrj bit_from_pos (tree offset, tree bitpos)
88338fd1498Szrj {
88438fd1498Szrj return size_binop (PLUS_EXPR, bitpos,
88538fd1498Szrj size_binop (MULT_EXPR, bits_from_bytes (offset),
88638fd1498Szrj bitsize_unit_node));
88738fd1498Szrj }
88838fd1498Szrj
88938fd1498Szrj /* Return the combined truncated byte position for the byte offset OFFSET and
89038fd1498Szrj the bit position BITPOS. */
89138fd1498Szrj
89238fd1498Szrj tree
byte_from_pos(tree offset,tree bitpos)89338fd1498Szrj byte_from_pos (tree offset, tree bitpos)
89438fd1498Szrj {
89538fd1498Szrj tree bytepos;
89638fd1498Szrj if (TREE_CODE (bitpos) == MULT_EXPR
89738fd1498Szrj && tree_int_cst_equal (TREE_OPERAND (bitpos, 1), bitsize_unit_node))
89838fd1498Szrj bytepos = TREE_OPERAND (bitpos, 0);
89938fd1498Szrj else
90038fd1498Szrj bytepos = size_binop (TRUNC_DIV_EXPR, bitpos, bitsize_unit_node);
90138fd1498Szrj return size_binop (PLUS_EXPR, offset, fold_convert (sizetype, bytepos));
90238fd1498Szrj }
90338fd1498Szrj
90438fd1498Szrj /* Split the bit position POS into a byte offset *POFFSET and a bit
90538fd1498Szrj position *PBITPOS with the byte offset aligned to OFF_ALIGN bits. */
90638fd1498Szrj
90738fd1498Szrj void
pos_from_bit(tree * poffset,tree * pbitpos,unsigned int off_align,tree pos)90838fd1498Szrj pos_from_bit (tree *poffset, tree *pbitpos, unsigned int off_align,
90938fd1498Szrj tree pos)
91038fd1498Szrj {
91138fd1498Szrj tree toff_align = bitsize_int (off_align);
91238fd1498Szrj if (TREE_CODE (pos) == MULT_EXPR
91338fd1498Szrj && tree_int_cst_equal (TREE_OPERAND (pos, 1), toff_align))
91438fd1498Szrj {
91538fd1498Szrj *poffset = size_binop (MULT_EXPR,
91638fd1498Szrj fold_convert (sizetype, TREE_OPERAND (pos, 0)),
91738fd1498Szrj size_int (off_align / BITS_PER_UNIT));
91838fd1498Szrj *pbitpos = bitsize_zero_node;
91938fd1498Szrj }
92038fd1498Szrj else
92138fd1498Szrj {
92238fd1498Szrj *poffset = size_binop (MULT_EXPR,
92338fd1498Szrj fold_convert (sizetype,
92438fd1498Szrj size_binop (FLOOR_DIV_EXPR, pos,
92538fd1498Szrj toff_align)),
92638fd1498Szrj size_int (off_align / BITS_PER_UNIT));
92738fd1498Szrj *pbitpos = size_binop (FLOOR_MOD_EXPR, pos, toff_align);
92838fd1498Szrj }
92938fd1498Szrj }
93038fd1498Szrj
93138fd1498Szrj /* Given a pointer to bit and byte offsets and an offset alignment,
93238fd1498Szrj normalize the offsets so they are within the alignment. */
93338fd1498Szrj
93438fd1498Szrj void
normalize_offset(tree * poffset,tree * pbitpos,unsigned int off_align)93538fd1498Szrj normalize_offset (tree *poffset, tree *pbitpos, unsigned int off_align)
93638fd1498Szrj {
93738fd1498Szrj /* If the bit position is now larger than it should be, adjust it
93838fd1498Szrj downwards. */
93938fd1498Szrj if (compare_tree_int (*pbitpos, off_align) >= 0)
94038fd1498Szrj {
94138fd1498Szrj tree offset, bitpos;
94238fd1498Szrj pos_from_bit (&offset, &bitpos, off_align, *pbitpos);
94338fd1498Szrj *poffset = size_binop (PLUS_EXPR, *poffset, offset);
94438fd1498Szrj *pbitpos = bitpos;
94538fd1498Szrj }
94638fd1498Szrj }
94738fd1498Szrj
94838fd1498Szrj /* Print debugging information about the information in RLI. */
94938fd1498Szrj
95038fd1498Szrj DEBUG_FUNCTION void
debug_rli(record_layout_info rli)95138fd1498Szrj debug_rli (record_layout_info rli)
95238fd1498Szrj {
95338fd1498Szrj print_node_brief (stderr, "type", rli->t, 0);
95438fd1498Szrj print_node_brief (stderr, "\noffset", rli->offset, 0);
95538fd1498Szrj print_node_brief (stderr, " bitpos", rli->bitpos, 0);
95638fd1498Szrj
95738fd1498Szrj fprintf (stderr, "\naligns: rec = %u, unpack = %u, off = %u\n",
95838fd1498Szrj rli->record_align, rli->unpacked_align,
95938fd1498Szrj rli->offset_align);
96038fd1498Szrj
96138fd1498Szrj /* The ms_struct code is the only that uses this. */
96238fd1498Szrj if (targetm.ms_bitfield_layout_p (rli->t))
96338fd1498Szrj fprintf (stderr, "remaining in alignment = %u\n", rli->remaining_in_alignment);
96438fd1498Szrj
96538fd1498Szrj if (rli->packed_maybe_necessary)
96638fd1498Szrj fprintf (stderr, "packed may be necessary\n");
96738fd1498Szrj
96838fd1498Szrj if (!vec_safe_is_empty (rli->pending_statics))
96938fd1498Szrj {
97038fd1498Szrj fprintf (stderr, "pending statics:\n");
97138fd1498Szrj debug (rli->pending_statics);
97238fd1498Szrj }
97338fd1498Szrj }
97438fd1498Szrj
97538fd1498Szrj /* Given an RLI with a possibly-incremented BITPOS, adjust OFFSET and
97638fd1498Szrj BITPOS if necessary to keep BITPOS below OFFSET_ALIGN. */
97738fd1498Szrj
97838fd1498Szrj void
normalize_rli(record_layout_info rli)97938fd1498Szrj normalize_rli (record_layout_info rli)
98038fd1498Szrj {
98138fd1498Szrj normalize_offset (&rli->offset, &rli->bitpos, rli->offset_align);
98238fd1498Szrj }
98338fd1498Szrj
98438fd1498Szrj /* Returns the size in bytes allocated so far. */
98538fd1498Szrj
98638fd1498Szrj tree
rli_size_unit_so_far(record_layout_info rli)98738fd1498Szrj rli_size_unit_so_far (record_layout_info rli)
98838fd1498Szrj {
98938fd1498Szrj return byte_from_pos (rli->offset, rli->bitpos);
99038fd1498Szrj }
99138fd1498Szrj
99238fd1498Szrj /* Returns the size in bits allocated so far. */
99338fd1498Szrj
99438fd1498Szrj tree
rli_size_so_far(record_layout_info rli)99538fd1498Szrj rli_size_so_far (record_layout_info rli)
99638fd1498Szrj {
99738fd1498Szrj return bit_from_pos (rli->offset, rli->bitpos);
99838fd1498Szrj }
99938fd1498Szrj
100038fd1498Szrj /* FIELD is about to be added to RLI->T. The alignment (in bits) of
100138fd1498Szrj the next available location within the record is given by KNOWN_ALIGN.
100238fd1498Szrj Update the variable alignment fields in RLI, and return the alignment
100338fd1498Szrj to give the FIELD. */
100438fd1498Szrj
100538fd1498Szrj unsigned int
update_alignment_for_field(record_layout_info rli,tree field,unsigned int known_align)100638fd1498Szrj update_alignment_for_field (record_layout_info rli, tree field,
100738fd1498Szrj unsigned int known_align)
100838fd1498Szrj {
100938fd1498Szrj /* The alignment required for FIELD. */
101038fd1498Szrj unsigned int desired_align;
101138fd1498Szrj /* The type of this field. */
101238fd1498Szrj tree type = TREE_TYPE (field);
101338fd1498Szrj /* True if the field was explicitly aligned by the user. */
101438fd1498Szrj bool user_align;
101538fd1498Szrj bool is_bitfield;
101638fd1498Szrj
101738fd1498Szrj /* Do not attempt to align an ERROR_MARK node */
101838fd1498Szrj if (TREE_CODE (type) == ERROR_MARK)
101938fd1498Szrj return 0;
102038fd1498Szrj
102138fd1498Szrj /* Lay out the field so we know what alignment it needs. */
102238fd1498Szrj layout_decl (field, known_align);
102338fd1498Szrj desired_align = DECL_ALIGN (field);
102438fd1498Szrj user_align = DECL_USER_ALIGN (field);
102538fd1498Szrj
102638fd1498Szrj is_bitfield = (type != error_mark_node
102738fd1498Szrj && DECL_BIT_FIELD_TYPE (field)
102838fd1498Szrj && ! integer_zerop (TYPE_SIZE (type)));
102938fd1498Szrj
103038fd1498Szrj /* Record must have at least as much alignment as any field.
103138fd1498Szrj Otherwise, the alignment of the field within the record is
103238fd1498Szrj meaningless. */
103338fd1498Szrj if (targetm.ms_bitfield_layout_p (rli->t))
103438fd1498Szrj {
103538fd1498Szrj /* Here, the alignment of the underlying type of a bitfield can
103638fd1498Szrj affect the alignment of a record; even a zero-sized field
103738fd1498Szrj can do this. The alignment should be to the alignment of
103838fd1498Szrj the type, except that for zero-size bitfields this only
103938fd1498Szrj applies if there was an immediately prior, nonzero-size
104038fd1498Szrj bitfield. (That's the way it is, experimentally.) */
104138fd1498Szrj if (!is_bitfield
104238fd1498Szrj || ((DECL_SIZE (field) == NULL_TREE
104338fd1498Szrj || !integer_zerop (DECL_SIZE (field)))
104438fd1498Szrj ? !DECL_PACKED (field)
104538fd1498Szrj : (rli->prev_field
104638fd1498Szrj && DECL_BIT_FIELD_TYPE (rli->prev_field)
104738fd1498Szrj && ! integer_zerop (DECL_SIZE (rli->prev_field)))))
104838fd1498Szrj {
104938fd1498Szrj unsigned int type_align = TYPE_ALIGN (type);
105038fd1498Szrj if (!is_bitfield && DECL_PACKED (field))
105138fd1498Szrj type_align = desired_align;
105238fd1498Szrj else
105338fd1498Szrj type_align = MAX (type_align, desired_align);
105438fd1498Szrj if (maximum_field_alignment != 0)
105538fd1498Szrj type_align = MIN (type_align, maximum_field_alignment);
105638fd1498Szrj rli->record_align = MAX (rli->record_align, type_align);
105738fd1498Szrj rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
105838fd1498Szrj }
105938fd1498Szrj }
106038fd1498Szrj else if (is_bitfield && PCC_BITFIELD_TYPE_MATTERS)
106138fd1498Szrj {
106238fd1498Szrj /* Named bit-fields cause the entire structure to have the
106338fd1498Szrj alignment implied by their type. Some targets also apply the same
106438fd1498Szrj rules to unnamed bitfields. */
106538fd1498Szrj if (DECL_NAME (field) != 0
106638fd1498Szrj || targetm.align_anon_bitfield ())
106738fd1498Szrj {
106838fd1498Szrj unsigned int type_align = TYPE_ALIGN (type);
106938fd1498Szrj
107038fd1498Szrj #ifdef ADJUST_FIELD_ALIGN
107138fd1498Szrj if (! TYPE_USER_ALIGN (type))
107238fd1498Szrj type_align = ADJUST_FIELD_ALIGN (field, type, type_align);
107338fd1498Szrj #endif
107438fd1498Szrj
107538fd1498Szrj /* Targets might chose to handle unnamed and hence possibly
107638fd1498Szrj zero-width bitfield. Those are not influenced by #pragmas
107738fd1498Szrj or packed attributes. */
107838fd1498Szrj if (integer_zerop (DECL_SIZE (field)))
107938fd1498Szrj {
108038fd1498Szrj if (initial_max_fld_align)
108138fd1498Szrj type_align = MIN (type_align,
108238fd1498Szrj initial_max_fld_align * BITS_PER_UNIT);
108338fd1498Szrj }
108438fd1498Szrj else if (maximum_field_alignment != 0)
108538fd1498Szrj type_align = MIN (type_align, maximum_field_alignment);
108638fd1498Szrj else if (DECL_PACKED (field))
108738fd1498Szrj type_align = MIN (type_align, BITS_PER_UNIT);
108838fd1498Szrj
108938fd1498Szrj /* The alignment of the record is increased to the maximum
109038fd1498Szrj of the current alignment, the alignment indicated on the
109138fd1498Szrj field (i.e., the alignment specified by an __aligned__
109238fd1498Szrj attribute), and the alignment indicated by the type of
109338fd1498Szrj the field. */
109438fd1498Szrj rli->record_align = MAX (rli->record_align, desired_align);
109538fd1498Szrj rli->record_align = MAX (rli->record_align, type_align);
109638fd1498Szrj
109738fd1498Szrj if (warn_packed)
109838fd1498Szrj rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
109938fd1498Szrj user_align |= TYPE_USER_ALIGN (type);
110038fd1498Szrj }
110138fd1498Szrj }
110238fd1498Szrj else
110338fd1498Szrj {
110438fd1498Szrj rli->record_align = MAX (rli->record_align, desired_align);
110538fd1498Szrj rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
110638fd1498Szrj }
110738fd1498Szrj
110838fd1498Szrj TYPE_USER_ALIGN (rli->t) |= user_align;
110938fd1498Szrj
111038fd1498Szrj return desired_align;
111138fd1498Szrj }
111238fd1498Szrj
111338fd1498Szrj /* Issue a warning if the record alignment, RECORD_ALIGN, is less than
111438fd1498Szrj the field alignment of FIELD or FIELD isn't aligned. */
111538fd1498Szrj
111638fd1498Szrj static void
handle_warn_if_not_align(tree field,unsigned int record_align)111738fd1498Szrj handle_warn_if_not_align (tree field, unsigned int record_align)
111838fd1498Szrj {
111938fd1498Szrj tree type = TREE_TYPE (field);
112038fd1498Szrj
112138fd1498Szrj if (type == error_mark_node)
112238fd1498Szrj return;
112338fd1498Szrj
112438fd1498Szrj unsigned int warn_if_not_align = 0;
112538fd1498Szrj
112638fd1498Szrj int opt_w = 0;
112738fd1498Szrj
112838fd1498Szrj if (warn_if_not_aligned)
112938fd1498Szrj {
113038fd1498Szrj warn_if_not_align = DECL_WARN_IF_NOT_ALIGN (field);
113138fd1498Szrj if (!warn_if_not_align)
113238fd1498Szrj warn_if_not_align = TYPE_WARN_IF_NOT_ALIGN (type);
113338fd1498Szrj if (warn_if_not_align)
113438fd1498Szrj opt_w = OPT_Wif_not_aligned;
113538fd1498Szrj }
113638fd1498Szrj
113738fd1498Szrj if (!warn_if_not_align
113838fd1498Szrj && warn_packed_not_aligned
113938fd1498Szrj && lookup_attribute ("aligned", TYPE_ATTRIBUTES (type)))
114038fd1498Szrj {
114138fd1498Szrj warn_if_not_align = TYPE_ALIGN (type);
114238fd1498Szrj opt_w = OPT_Wpacked_not_aligned;
114338fd1498Szrj }
114438fd1498Szrj
114538fd1498Szrj if (!warn_if_not_align)
114638fd1498Szrj return;
114738fd1498Szrj
114838fd1498Szrj tree context = DECL_CONTEXT (field);
114938fd1498Szrj
115038fd1498Szrj warn_if_not_align /= BITS_PER_UNIT;
115138fd1498Szrj record_align /= BITS_PER_UNIT;
115238fd1498Szrj if ((record_align % warn_if_not_align) != 0)
115338fd1498Szrj warning (opt_w, "alignment %u of %qT is less than %u",
115438fd1498Szrj record_align, context, warn_if_not_align);
115538fd1498Szrj
115638fd1498Szrj tree off = byte_position (field);
115738fd1498Szrj if (!multiple_of_p (TREE_TYPE (off), off, size_int (warn_if_not_align)))
115838fd1498Szrj {
115938fd1498Szrj if (TREE_CODE (off) == INTEGER_CST)
116038fd1498Szrj warning (opt_w, "%q+D offset %E in %qT isn%'t aligned to %u",
116138fd1498Szrj field, off, context, warn_if_not_align);
116238fd1498Szrj else
116338fd1498Szrj warning (opt_w, "%q+D offset %E in %qT may not be aligned to %u",
116438fd1498Szrj field, off, context, warn_if_not_align);
116538fd1498Szrj }
116638fd1498Szrj }
116738fd1498Szrj
116838fd1498Szrj /* Called from place_field to handle unions. */
116938fd1498Szrj
117038fd1498Szrj static void
place_union_field(record_layout_info rli,tree field)117138fd1498Szrj place_union_field (record_layout_info rli, tree field)
117238fd1498Szrj {
117338fd1498Szrj update_alignment_for_field (rli, field, /*known_align=*/0);
117438fd1498Szrj
117538fd1498Szrj DECL_FIELD_OFFSET (field) = size_zero_node;
117638fd1498Szrj DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
117738fd1498Szrj SET_DECL_OFFSET_ALIGN (field, BIGGEST_ALIGNMENT);
117838fd1498Szrj handle_warn_if_not_align (field, rli->record_align);
117938fd1498Szrj
118038fd1498Szrj /* If this is an ERROR_MARK return *after* having set the
118138fd1498Szrj field at the start of the union. This helps when parsing
118238fd1498Szrj invalid fields. */
118338fd1498Szrj if (TREE_CODE (TREE_TYPE (field)) == ERROR_MARK)
118438fd1498Szrj return;
118538fd1498Szrj
118638fd1498Szrj if (AGGREGATE_TYPE_P (TREE_TYPE (field))
118738fd1498Szrj && TYPE_TYPELESS_STORAGE (TREE_TYPE (field)))
118838fd1498Szrj TYPE_TYPELESS_STORAGE (rli->t) = 1;
118938fd1498Szrj
119038fd1498Szrj /* We assume the union's size will be a multiple of a byte so we don't
119138fd1498Szrj bother with BITPOS. */
119238fd1498Szrj if (TREE_CODE (rli->t) == UNION_TYPE)
119338fd1498Szrj rli->offset = size_binop (MAX_EXPR, rli->offset, DECL_SIZE_UNIT (field));
119438fd1498Szrj else if (TREE_CODE (rli->t) == QUAL_UNION_TYPE)
119538fd1498Szrj rli->offset = fold_build3 (COND_EXPR, sizetype, DECL_QUALIFIER (field),
119638fd1498Szrj DECL_SIZE_UNIT (field), rli->offset);
119738fd1498Szrj }
119838fd1498Szrj
119938fd1498Szrj /* A bitfield of SIZE with a required access alignment of ALIGN is allocated
120038fd1498Szrj at BYTE_OFFSET / BIT_OFFSET. Return nonzero if the field would span more
120138fd1498Szrj units of alignment than the underlying TYPE. */
120238fd1498Szrj static int
excess_unit_span(HOST_WIDE_INT byte_offset,HOST_WIDE_INT bit_offset,HOST_WIDE_INT size,HOST_WIDE_INT align,tree type)120338fd1498Szrj excess_unit_span (HOST_WIDE_INT byte_offset, HOST_WIDE_INT bit_offset,
120438fd1498Szrj HOST_WIDE_INT size, HOST_WIDE_INT align, tree type)
120538fd1498Szrj {
120638fd1498Szrj /* Note that the calculation of OFFSET might overflow; we calculate it so
120738fd1498Szrj that we still get the right result as long as ALIGN is a power of two. */
120838fd1498Szrj unsigned HOST_WIDE_INT offset = byte_offset * BITS_PER_UNIT + bit_offset;
120938fd1498Szrj
121038fd1498Szrj offset = offset % align;
121138fd1498Szrj return ((offset + size + align - 1) / align
121238fd1498Szrj > tree_to_uhwi (TYPE_SIZE (type)) / align);
121338fd1498Szrj }
121438fd1498Szrj
121538fd1498Szrj /* RLI contains information about the layout of a RECORD_TYPE. FIELD
121638fd1498Szrj is a FIELD_DECL to be added after those fields already present in
121738fd1498Szrj T. (FIELD is not actually added to the TYPE_FIELDS list here;
121838fd1498Szrj callers that desire that behavior must manually perform that step.) */
121938fd1498Szrj
122038fd1498Szrj void
place_field(record_layout_info rli,tree field)122138fd1498Szrj place_field (record_layout_info rli, tree field)
122238fd1498Szrj {
122338fd1498Szrj /* The alignment required for FIELD. */
122438fd1498Szrj unsigned int desired_align;
122538fd1498Szrj /* The alignment FIELD would have if we just dropped it into the
122638fd1498Szrj record as it presently stands. */
122738fd1498Szrj unsigned int known_align;
122838fd1498Szrj unsigned int actual_align;
122938fd1498Szrj /* The type of this field. */
123038fd1498Szrj tree type = TREE_TYPE (field);
123138fd1498Szrj
123238fd1498Szrj gcc_assert (TREE_CODE (field) != ERROR_MARK);
123338fd1498Szrj
123438fd1498Szrj /* If FIELD is static, then treat it like a separate variable, not
123538fd1498Szrj really like a structure field. If it is a FUNCTION_DECL, it's a
123638fd1498Szrj method. In both cases, all we do is lay out the decl, and we do
123738fd1498Szrj it *after* the record is laid out. */
123838fd1498Szrj if (VAR_P (field))
123938fd1498Szrj {
124038fd1498Szrj vec_safe_push (rli->pending_statics, field);
124138fd1498Szrj return;
124238fd1498Szrj }
124338fd1498Szrj
124438fd1498Szrj /* Enumerators and enum types which are local to this class need not
124538fd1498Szrj be laid out. Likewise for initialized constant fields. */
124638fd1498Szrj else if (TREE_CODE (field) != FIELD_DECL)
124738fd1498Szrj return;
124838fd1498Szrj
124938fd1498Szrj /* Unions are laid out very differently than records, so split
125038fd1498Szrj that code off to another function. */
125138fd1498Szrj else if (TREE_CODE (rli->t) != RECORD_TYPE)
125238fd1498Szrj {
125338fd1498Szrj place_union_field (rli, field);
125438fd1498Szrj return;
125538fd1498Szrj }
125638fd1498Szrj
125738fd1498Szrj else if (TREE_CODE (type) == ERROR_MARK)
125838fd1498Szrj {
125938fd1498Szrj /* Place this field at the current allocation position, so we
126038fd1498Szrj maintain monotonicity. */
126138fd1498Szrj DECL_FIELD_OFFSET (field) = rli->offset;
126238fd1498Szrj DECL_FIELD_BIT_OFFSET (field) = rli->bitpos;
126338fd1498Szrj SET_DECL_OFFSET_ALIGN (field, rli->offset_align);
126438fd1498Szrj handle_warn_if_not_align (field, rli->record_align);
126538fd1498Szrj return;
126638fd1498Szrj }
126738fd1498Szrj
126838fd1498Szrj if (AGGREGATE_TYPE_P (type)
126938fd1498Szrj && TYPE_TYPELESS_STORAGE (type))
127038fd1498Szrj TYPE_TYPELESS_STORAGE (rli->t) = 1;
127138fd1498Szrj
127238fd1498Szrj /* Work out the known alignment so far. Note that A & (-A) is the
127338fd1498Szrj value of the least-significant bit in A that is one. */
127438fd1498Szrj if (! integer_zerop (rli->bitpos))
127538fd1498Szrj known_align = least_bit_hwi (tree_to_uhwi (rli->bitpos));
127638fd1498Szrj else if (integer_zerop (rli->offset))
127738fd1498Szrj known_align = 0;
127838fd1498Szrj else if (tree_fits_uhwi_p (rli->offset))
127938fd1498Szrj known_align = (BITS_PER_UNIT
128038fd1498Szrj * least_bit_hwi (tree_to_uhwi (rli->offset)));
128138fd1498Szrj else
128238fd1498Szrj known_align = rli->offset_align;
128338fd1498Szrj
128438fd1498Szrj desired_align = update_alignment_for_field (rli, field, known_align);
128538fd1498Szrj if (known_align == 0)
128638fd1498Szrj known_align = MAX (BIGGEST_ALIGNMENT, rli->record_align);
128738fd1498Szrj
128838fd1498Szrj if (warn_packed && DECL_PACKED (field))
128938fd1498Szrj {
129038fd1498Szrj if (known_align >= TYPE_ALIGN (type))
129138fd1498Szrj {
129238fd1498Szrj if (TYPE_ALIGN (type) > desired_align)
129338fd1498Szrj {
129438fd1498Szrj if (STRICT_ALIGNMENT)
129538fd1498Szrj warning (OPT_Wattributes, "packed attribute causes "
129638fd1498Szrj "inefficient alignment for %q+D", field);
129738fd1498Szrj /* Don't warn if DECL_PACKED was set by the type. */
129838fd1498Szrj else if (!TYPE_PACKED (rli->t))
129938fd1498Szrj warning (OPT_Wattributes, "packed attribute is "
130038fd1498Szrj "unnecessary for %q+D", field);
130138fd1498Szrj }
130238fd1498Szrj }
130338fd1498Szrj else
130438fd1498Szrj rli->packed_maybe_necessary = 1;
130538fd1498Szrj }
130638fd1498Szrj
130738fd1498Szrj /* Does this field automatically have alignment it needs by virtue
130838fd1498Szrj of the fields that precede it and the record's own alignment? */
130938fd1498Szrj if (known_align < desired_align
131038fd1498Szrj && (! targetm.ms_bitfield_layout_p (rli->t)
131138fd1498Szrj || rli->prev_field == NULL))
131238fd1498Szrj {
131338fd1498Szrj /* No, we need to skip space before this field.
131438fd1498Szrj Bump the cumulative size to multiple of field alignment. */
131538fd1498Szrj
131638fd1498Szrj if (!targetm.ms_bitfield_layout_p (rli->t)
131738fd1498Szrj && DECL_SOURCE_LOCATION (field) != BUILTINS_LOCATION)
131838fd1498Szrj warning (OPT_Wpadded, "padding struct to align %q+D", field);
131938fd1498Szrj
132038fd1498Szrj /* If the alignment is still within offset_align, just align
132138fd1498Szrj the bit position. */
132238fd1498Szrj if (desired_align < rli->offset_align)
132338fd1498Szrj rli->bitpos = round_up (rli->bitpos, desired_align);
132438fd1498Szrj else
132538fd1498Szrj {
132638fd1498Szrj /* First adjust OFFSET by the partial bits, then align. */
132738fd1498Szrj rli->offset
132838fd1498Szrj = size_binop (PLUS_EXPR, rli->offset,
132938fd1498Szrj fold_convert (sizetype,
133038fd1498Szrj size_binop (CEIL_DIV_EXPR, rli->bitpos,
133138fd1498Szrj bitsize_unit_node)));
133238fd1498Szrj rli->bitpos = bitsize_zero_node;
133338fd1498Szrj
133438fd1498Szrj rli->offset = round_up (rli->offset, desired_align / BITS_PER_UNIT);
133538fd1498Szrj }
133638fd1498Szrj
133738fd1498Szrj if (! TREE_CONSTANT (rli->offset))
133838fd1498Szrj rli->offset_align = desired_align;
133938fd1498Szrj }
134038fd1498Szrj
134138fd1498Szrj /* Handle compatibility with PCC. Note that if the record has any
134238fd1498Szrj variable-sized fields, we need not worry about compatibility. */
134338fd1498Szrj if (PCC_BITFIELD_TYPE_MATTERS
134438fd1498Szrj && ! targetm.ms_bitfield_layout_p (rli->t)
134538fd1498Szrj && TREE_CODE (field) == FIELD_DECL
134638fd1498Szrj && type != error_mark_node
134738fd1498Szrj && DECL_BIT_FIELD (field)
134838fd1498Szrj && (! DECL_PACKED (field)
134938fd1498Szrj /* Enter for these packed fields only to issue a warning. */
135038fd1498Szrj || TYPE_ALIGN (type) <= BITS_PER_UNIT)
135138fd1498Szrj && maximum_field_alignment == 0
135238fd1498Szrj && ! integer_zerop (DECL_SIZE (field))
135338fd1498Szrj && tree_fits_uhwi_p (DECL_SIZE (field))
135438fd1498Szrj && tree_fits_uhwi_p (rli->offset)
135538fd1498Szrj && tree_fits_uhwi_p (TYPE_SIZE (type)))
135638fd1498Szrj {
135738fd1498Szrj unsigned int type_align = TYPE_ALIGN (type);
135838fd1498Szrj tree dsize = DECL_SIZE (field);
135938fd1498Szrj HOST_WIDE_INT field_size = tree_to_uhwi (dsize);
136038fd1498Szrj HOST_WIDE_INT offset = tree_to_uhwi (rli->offset);
136138fd1498Szrj HOST_WIDE_INT bit_offset = tree_to_shwi (rli->bitpos);
136238fd1498Szrj
136338fd1498Szrj #ifdef ADJUST_FIELD_ALIGN
136438fd1498Szrj if (! TYPE_USER_ALIGN (type))
136538fd1498Szrj type_align = ADJUST_FIELD_ALIGN (field, type, type_align);
136638fd1498Szrj #endif
136738fd1498Szrj
136838fd1498Szrj /* A bit field may not span more units of alignment of its type
136938fd1498Szrj than its type itself. Advance to next boundary if necessary. */
137038fd1498Szrj if (excess_unit_span (offset, bit_offset, field_size, type_align, type))
137138fd1498Szrj {
137238fd1498Szrj if (DECL_PACKED (field))
137338fd1498Szrj {
137438fd1498Szrj if (warn_packed_bitfield_compat == 1)
137538fd1498Szrj inform
137638fd1498Szrj (input_location,
137738fd1498Szrj "offset of packed bit-field %qD has changed in GCC 4.4",
137838fd1498Szrj field);
137938fd1498Szrj }
138038fd1498Szrj else
138138fd1498Szrj rli->bitpos = round_up (rli->bitpos, type_align);
138238fd1498Szrj }
138338fd1498Szrj
138438fd1498Szrj if (! DECL_PACKED (field))
138538fd1498Szrj TYPE_USER_ALIGN (rli->t) |= TYPE_USER_ALIGN (type);
138638fd1498Szrj
138738fd1498Szrj SET_TYPE_WARN_IF_NOT_ALIGN (rli->t,
138838fd1498Szrj TYPE_WARN_IF_NOT_ALIGN (type));
138938fd1498Szrj }
139038fd1498Szrj
139138fd1498Szrj #ifdef BITFIELD_NBYTES_LIMITED
139238fd1498Szrj if (BITFIELD_NBYTES_LIMITED
139338fd1498Szrj && ! targetm.ms_bitfield_layout_p (rli->t)
139438fd1498Szrj && TREE_CODE (field) == FIELD_DECL
139538fd1498Szrj && type != error_mark_node
139638fd1498Szrj && DECL_BIT_FIELD_TYPE (field)
139738fd1498Szrj && ! DECL_PACKED (field)
139838fd1498Szrj && ! integer_zerop (DECL_SIZE (field))
139938fd1498Szrj && tree_fits_uhwi_p (DECL_SIZE (field))
140038fd1498Szrj && tree_fits_uhwi_p (rli->offset)
140138fd1498Szrj && tree_fits_uhwi_p (TYPE_SIZE (type)))
140238fd1498Szrj {
140338fd1498Szrj unsigned int type_align = TYPE_ALIGN (type);
140438fd1498Szrj tree dsize = DECL_SIZE (field);
140538fd1498Szrj HOST_WIDE_INT field_size = tree_to_uhwi (dsize);
140638fd1498Szrj HOST_WIDE_INT offset = tree_to_uhwi (rli->offset);
140738fd1498Szrj HOST_WIDE_INT bit_offset = tree_to_shwi (rli->bitpos);
140838fd1498Szrj
140938fd1498Szrj #ifdef ADJUST_FIELD_ALIGN
141038fd1498Szrj if (! TYPE_USER_ALIGN (type))
141138fd1498Szrj type_align = ADJUST_FIELD_ALIGN (field, type, type_align);
141238fd1498Szrj #endif
141338fd1498Szrj
141438fd1498Szrj if (maximum_field_alignment != 0)
141538fd1498Szrj type_align = MIN (type_align, maximum_field_alignment);
141638fd1498Szrj /* ??? This test is opposite the test in the containing if
141738fd1498Szrj statement, so this code is unreachable currently. */
141838fd1498Szrj else if (DECL_PACKED (field))
141938fd1498Szrj type_align = MIN (type_align, BITS_PER_UNIT);
142038fd1498Szrj
142138fd1498Szrj /* A bit field may not span the unit of alignment of its type.
142238fd1498Szrj Advance to next boundary if necessary. */
142338fd1498Szrj if (excess_unit_span (offset, bit_offset, field_size, type_align, type))
142438fd1498Szrj rli->bitpos = round_up (rli->bitpos, type_align);
142538fd1498Szrj
142638fd1498Szrj TYPE_USER_ALIGN (rli->t) |= TYPE_USER_ALIGN (type);
142738fd1498Szrj SET_TYPE_WARN_IF_NOT_ALIGN (rli->t,
142838fd1498Szrj TYPE_WARN_IF_NOT_ALIGN (type));
142938fd1498Szrj }
143038fd1498Szrj #endif
143138fd1498Szrj
143238fd1498Szrj /* See the docs for TARGET_MS_BITFIELD_LAYOUT_P for details.
143338fd1498Szrj A subtlety:
143438fd1498Szrj When a bit field is inserted into a packed record, the whole
143538fd1498Szrj size of the underlying type is used by one or more same-size
143638fd1498Szrj adjacent bitfields. (That is, if its long:3, 32 bits is
143738fd1498Szrj used in the record, and any additional adjacent long bitfields are
143838fd1498Szrj packed into the same chunk of 32 bits. However, if the size
143938fd1498Szrj changes, a new field of that size is allocated.) In an unpacked
144038fd1498Szrj record, this is the same as using alignment, but not equivalent
144138fd1498Szrj when packing.
144238fd1498Szrj
144338fd1498Szrj Note: for compatibility, we use the type size, not the type alignment
144438fd1498Szrj to determine alignment, since that matches the documentation */
144538fd1498Szrj
144638fd1498Szrj if (targetm.ms_bitfield_layout_p (rli->t))
144738fd1498Szrj {
144838fd1498Szrj tree prev_saved = rli->prev_field;
144938fd1498Szrj tree prev_type = prev_saved ? DECL_BIT_FIELD_TYPE (prev_saved) : NULL;
145038fd1498Szrj
145138fd1498Szrj /* This is a bitfield if it exists. */
145238fd1498Szrj if (rli->prev_field)
145338fd1498Szrj {
145438fd1498Szrj bool realign_p = known_align < desired_align;
145538fd1498Szrj
145638fd1498Szrj /* If both are bitfields, nonzero, and the same size, this is
145738fd1498Szrj the middle of a run. Zero declared size fields are special
145838fd1498Szrj and handled as "end of run". (Note: it's nonzero declared
145938fd1498Szrj size, but equal type sizes!) (Since we know that both
146038fd1498Szrj the current and previous fields are bitfields by the
146138fd1498Szrj time we check it, DECL_SIZE must be present for both.) */
146238fd1498Szrj if (DECL_BIT_FIELD_TYPE (field)
146338fd1498Szrj && !integer_zerop (DECL_SIZE (field))
146438fd1498Szrj && !integer_zerop (DECL_SIZE (rli->prev_field))
146538fd1498Szrj && tree_fits_shwi_p (DECL_SIZE (rli->prev_field))
146638fd1498Szrj && tree_fits_uhwi_p (TYPE_SIZE (type))
146738fd1498Szrj && simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (prev_type)))
146838fd1498Szrj {
146938fd1498Szrj /* We're in the middle of a run of equal type size fields; make
147038fd1498Szrj sure we realign if we run out of bits. (Not decl size,
147138fd1498Szrj type size!) */
147238fd1498Szrj HOST_WIDE_INT bitsize = tree_to_uhwi (DECL_SIZE (field));
147338fd1498Szrj
147438fd1498Szrj if (rli->remaining_in_alignment < bitsize)
147538fd1498Szrj {
147638fd1498Szrj HOST_WIDE_INT typesize = tree_to_uhwi (TYPE_SIZE (type));
147738fd1498Szrj
147838fd1498Szrj /* out of bits; bump up to next 'word'. */
147938fd1498Szrj rli->bitpos
148038fd1498Szrj = size_binop (PLUS_EXPR, rli->bitpos,
148138fd1498Szrj bitsize_int (rli->remaining_in_alignment));
148238fd1498Szrj rli->prev_field = field;
148338fd1498Szrj if (typesize < bitsize)
148438fd1498Szrj rli->remaining_in_alignment = 0;
148538fd1498Szrj else
148638fd1498Szrj rli->remaining_in_alignment = typesize - bitsize;
148738fd1498Szrj }
148838fd1498Szrj else
148938fd1498Szrj {
149038fd1498Szrj rli->remaining_in_alignment -= bitsize;
149138fd1498Szrj realign_p = false;
149238fd1498Szrj }
149338fd1498Szrj }
149438fd1498Szrj else
149538fd1498Szrj {
149638fd1498Szrj /* End of a run: if leaving a run of bitfields of the same type
149738fd1498Szrj size, we have to "use up" the rest of the bits of the type
149838fd1498Szrj size.
149938fd1498Szrj
150038fd1498Szrj Compute the new position as the sum of the size for the prior
150138fd1498Szrj type and where we first started working on that type.
150238fd1498Szrj Note: since the beginning of the field was aligned then
150338fd1498Szrj of course the end will be too. No round needed. */
150438fd1498Szrj
150538fd1498Szrj if (!integer_zerop (DECL_SIZE (rli->prev_field)))
150638fd1498Szrj {
150738fd1498Szrj rli->bitpos
150838fd1498Szrj = size_binop (PLUS_EXPR, rli->bitpos,
150938fd1498Szrj bitsize_int (rli->remaining_in_alignment));
151038fd1498Szrj }
151138fd1498Szrj else
151238fd1498Szrj /* We "use up" size zero fields; the code below should behave
151338fd1498Szrj as if the prior field was not a bitfield. */
151438fd1498Szrj prev_saved = NULL;
151538fd1498Szrj
151638fd1498Szrj /* Cause a new bitfield to be captured, either this time (if
151738fd1498Szrj currently a bitfield) or next time we see one. */
151838fd1498Szrj if (!DECL_BIT_FIELD_TYPE (field)
151938fd1498Szrj || integer_zerop (DECL_SIZE (field)))
152038fd1498Szrj rli->prev_field = NULL;
152138fd1498Szrj }
152238fd1498Szrj
152338fd1498Szrj /* Does this field automatically have alignment it needs by virtue
152438fd1498Szrj of the fields that precede it and the record's own alignment? */
152538fd1498Szrj if (realign_p)
152638fd1498Szrj {
152738fd1498Szrj /* If the alignment is still within offset_align, just align
152838fd1498Szrj the bit position. */
152938fd1498Szrj if (desired_align < rli->offset_align)
153038fd1498Szrj rli->bitpos = round_up (rli->bitpos, desired_align);
153138fd1498Szrj else
153238fd1498Szrj {
153338fd1498Szrj /* First adjust OFFSET by the partial bits, then align. */
153438fd1498Szrj tree d = size_binop (CEIL_DIV_EXPR, rli->bitpos,
153538fd1498Szrj bitsize_unit_node);
153638fd1498Szrj rli->offset = size_binop (PLUS_EXPR, rli->offset,
153738fd1498Szrj fold_convert (sizetype, d));
153838fd1498Szrj rli->bitpos = bitsize_zero_node;
153938fd1498Szrj
154038fd1498Szrj rli->offset = round_up (rli->offset,
154138fd1498Szrj desired_align / BITS_PER_UNIT);
154238fd1498Szrj }
154338fd1498Szrj
154438fd1498Szrj if (! TREE_CONSTANT (rli->offset))
154538fd1498Szrj rli->offset_align = desired_align;
154638fd1498Szrj }
154738fd1498Szrj
154838fd1498Szrj normalize_rli (rli);
154938fd1498Szrj }
155038fd1498Szrj
155138fd1498Szrj /* If we're starting a new run of same type size bitfields
155238fd1498Szrj (or a run of non-bitfields), set up the "first of the run"
155338fd1498Szrj fields.
155438fd1498Szrj
155538fd1498Szrj That is, if the current field is not a bitfield, or if there
155638fd1498Szrj was a prior bitfield the type sizes differ, or if there wasn't
155738fd1498Szrj a prior bitfield the size of the current field is nonzero.
155838fd1498Szrj
155938fd1498Szrj Note: we must be sure to test ONLY the type size if there was
156038fd1498Szrj a prior bitfield and ONLY for the current field being zero if
156138fd1498Szrj there wasn't. */
156238fd1498Szrj
156338fd1498Szrj if (!DECL_BIT_FIELD_TYPE (field)
156438fd1498Szrj || (prev_saved != NULL
156538fd1498Szrj ? !simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (prev_type))
156638fd1498Szrj : !integer_zerop (DECL_SIZE (field))))
156738fd1498Szrj {
156838fd1498Szrj /* Never smaller than a byte for compatibility. */
156938fd1498Szrj unsigned int type_align = BITS_PER_UNIT;
157038fd1498Szrj
157138fd1498Szrj /* (When not a bitfield), we could be seeing a flex array (with
157238fd1498Szrj no DECL_SIZE). Since we won't be using remaining_in_alignment
157338fd1498Szrj until we see a bitfield (and come by here again) we just skip
157438fd1498Szrj calculating it. */
157538fd1498Szrj if (DECL_SIZE (field) != NULL
157638fd1498Szrj && tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (field)))
157738fd1498Szrj && tree_fits_uhwi_p (DECL_SIZE (field)))
157838fd1498Szrj {
157938fd1498Szrj unsigned HOST_WIDE_INT bitsize
158038fd1498Szrj = tree_to_uhwi (DECL_SIZE (field));
158138fd1498Szrj unsigned HOST_WIDE_INT typesize
158238fd1498Szrj = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (field)));
158338fd1498Szrj
158438fd1498Szrj if (typesize < bitsize)
158538fd1498Szrj rli->remaining_in_alignment = 0;
158638fd1498Szrj else
158738fd1498Szrj rli->remaining_in_alignment = typesize - bitsize;
158838fd1498Szrj }
158938fd1498Szrj
159038fd1498Szrj /* Now align (conventionally) for the new type. */
159138fd1498Szrj if (! DECL_PACKED (field))
159238fd1498Szrj type_align = TYPE_ALIGN (TREE_TYPE (field));
159338fd1498Szrj
159438fd1498Szrj if (maximum_field_alignment != 0)
159538fd1498Szrj type_align = MIN (type_align, maximum_field_alignment);
159638fd1498Szrj
159738fd1498Szrj rli->bitpos = round_up (rli->bitpos, type_align);
159838fd1498Szrj
159938fd1498Szrj /* If we really aligned, don't allow subsequent bitfields
160038fd1498Szrj to undo that. */
160138fd1498Szrj rli->prev_field = NULL;
160238fd1498Szrj }
160338fd1498Szrj }
160438fd1498Szrj
160538fd1498Szrj /* Offset so far becomes the position of this field after normalizing. */
160638fd1498Szrj normalize_rli (rli);
160738fd1498Szrj DECL_FIELD_OFFSET (field) = rli->offset;
160838fd1498Szrj DECL_FIELD_BIT_OFFSET (field) = rli->bitpos;
160938fd1498Szrj SET_DECL_OFFSET_ALIGN (field, rli->offset_align);
161038fd1498Szrj handle_warn_if_not_align (field, rli->record_align);
161138fd1498Szrj
161238fd1498Szrj /* Evaluate nonconstant offsets only once, either now or as soon as safe. */
161338fd1498Szrj if (TREE_CODE (DECL_FIELD_OFFSET (field)) != INTEGER_CST)
161438fd1498Szrj DECL_FIELD_OFFSET (field) = variable_size (DECL_FIELD_OFFSET (field));
161538fd1498Szrj
161638fd1498Szrj /* If this field ended up more aligned than we thought it would be (we
161738fd1498Szrj approximate this by seeing if its position changed), lay out the field
161838fd1498Szrj again; perhaps we can use an integral mode for it now. */
161938fd1498Szrj if (! integer_zerop (DECL_FIELD_BIT_OFFSET (field)))
162038fd1498Szrj actual_align = least_bit_hwi (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field)));
162138fd1498Szrj else if (integer_zerop (DECL_FIELD_OFFSET (field)))
162238fd1498Szrj actual_align = MAX (BIGGEST_ALIGNMENT, rli->record_align);
162338fd1498Szrj else if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field)))
162438fd1498Szrj actual_align = (BITS_PER_UNIT
162538fd1498Szrj * least_bit_hwi (tree_to_uhwi (DECL_FIELD_OFFSET (field))));
162638fd1498Szrj else
162738fd1498Szrj actual_align = DECL_OFFSET_ALIGN (field);
162838fd1498Szrj /* ACTUAL_ALIGN is still the actual alignment *within the record* .
162938fd1498Szrj store / extract bit field operations will check the alignment of the
163038fd1498Szrj record against the mode of bit fields. */
163138fd1498Szrj
163238fd1498Szrj if (known_align != actual_align)
163338fd1498Szrj layout_decl (field, actual_align);
163438fd1498Szrj
163538fd1498Szrj if (rli->prev_field == NULL && DECL_BIT_FIELD_TYPE (field))
163638fd1498Szrj rli->prev_field = field;
163738fd1498Szrj
163838fd1498Szrj /* Now add size of this field to the size of the record. If the size is
163938fd1498Szrj not constant, treat the field as being a multiple of bytes and just
164038fd1498Szrj adjust the offset, resetting the bit position. Otherwise, apportion the
164138fd1498Szrj size amongst the bit position and offset. First handle the case of an
164238fd1498Szrj unspecified size, which can happen when we have an invalid nested struct
164338fd1498Szrj definition, such as struct j { struct j { int i; } }. The error message
164438fd1498Szrj is printed in finish_struct. */
164538fd1498Szrj if (DECL_SIZE (field) == 0)
164638fd1498Szrj /* Do nothing. */;
164738fd1498Szrj else if (TREE_CODE (DECL_SIZE (field)) != INTEGER_CST
164838fd1498Szrj || TREE_OVERFLOW (DECL_SIZE (field)))
164938fd1498Szrj {
165038fd1498Szrj rli->offset
165138fd1498Szrj = size_binop (PLUS_EXPR, rli->offset,
165238fd1498Szrj fold_convert (sizetype,
165338fd1498Szrj size_binop (CEIL_DIV_EXPR, rli->bitpos,
165438fd1498Szrj bitsize_unit_node)));
165538fd1498Szrj rli->offset
165638fd1498Szrj = size_binop (PLUS_EXPR, rli->offset, DECL_SIZE_UNIT (field));
165738fd1498Szrj rli->bitpos = bitsize_zero_node;
165838fd1498Szrj rli->offset_align = MIN (rli->offset_align, desired_align);
165938fd1498Szrj
166038fd1498Szrj if (!multiple_of_p (bitsizetype, DECL_SIZE (field),
166138fd1498Szrj bitsize_int (rli->offset_align)))
166238fd1498Szrj {
166338fd1498Szrj tree type = strip_array_types (TREE_TYPE (field));
166438fd1498Szrj /* The above adjusts offset_align just based on the start of the
166538fd1498Szrj field. The field might not have a size that is a multiple of
166638fd1498Szrj that offset_align though. If the field is an array of fixed
166738fd1498Szrj sized elements, assume there can be any multiple of those
166838fd1498Szrj sizes. If it is a variable length aggregate or array of
166938fd1498Szrj variable length aggregates, assume worst that the end is
167038fd1498Szrj just BITS_PER_UNIT aligned. */
167138fd1498Szrj if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
167238fd1498Szrj {
167338fd1498Szrj if (TREE_INT_CST_LOW (TYPE_SIZE (type)))
167438fd1498Szrj {
167538fd1498Szrj unsigned HOST_WIDE_INT sz
167638fd1498Szrj = least_bit_hwi (TREE_INT_CST_LOW (TYPE_SIZE (type)));
167738fd1498Szrj rli->offset_align = MIN (rli->offset_align, sz);
167838fd1498Szrj }
167938fd1498Szrj }
168038fd1498Szrj else
168138fd1498Szrj rli->offset_align = MIN (rli->offset_align, BITS_PER_UNIT);
168238fd1498Szrj }
168338fd1498Szrj }
168438fd1498Szrj else if (targetm.ms_bitfield_layout_p (rli->t))
168538fd1498Szrj {
168638fd1498Szrj rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos, DECL_SIZE (field));
168738fd1498Szrj
1688*58e805e6Szrj /* If FIELD is the last field and doesn't end at the full length
1689*58e805e6Szrj of the type then pad the struct out to the full length of the
1690*58e805e6Szrj last type. */
1691*58e805e6Szrj if (DECL_BIT_FIELD_TYPE (field)
169238fd1498Szrj && !integer_zerop (DECL_SIZE (field)))
1693*58e805e6Szrj {
1694*58e805e6Szrj /* We have to scan, because non-field DECLS are also here. */
1695*58e805e6Szrj tree probe = field;
1696*58e805e6Szrj while ((probe = DECL_CHAIN (probe)))
1697*58e805e6Szrj if (TREE_CODE (probe) == FIELD_DECL)
1698*58e805e6Szrj break;
1699*58e805e6Szrj if (!probe)
170038fd1498Szrj rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos,
170138fd1498Szrj bitsize_int (rli->remaining_in_alignment));
1702*58e805e6Szrj }
170338fd1498Szrj
170438fd1498Szrj normalize_rli (rli);
170538fd1498Szrj }
170638fd1498Szrj else
170738fd1498Szrj {
170838fd1498Szrj rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos, DECL_SIZE (field));
170938fd1498Szrj normalize_rli (rli);
171038fd1498Szrj }
171138fd1498Szrj }
171238fd1498Szrj
171338fd1498Szrj /* Assuming that all the fields have been laid out, this function uses
171438fd1498Szrj RLI to compute the final TYPE_SIZE, TYPE_ALIGN, etc. for the type
171538fd1498Szrj indicated by RLI. */
171638fd1498Szrj
171738fd1498Szrj static void
finalize_record_size(record_layout_info rli)171838fd1498Szrj finalize_record_size (record_layout_info rli)
171938fd1498Szrj {
172038fd1498Szrj tree unpadded_size, unpadded_size_unit;
172138fd1498Szrj
172238fd1498Szrj /* Now we want just byte and bit offsets, so set the offset alignment
172338fd1498Szrj to be a byte and then normalize. */
172438fd1498Szrj rli->offset_align = BITS_PER_UNIT;
172538fd1498Szrj normalize_rli (rli);
172638fd1498Szrj
172738fd1498Szrj /* Determine the desired alignment. */
172838fd1498Szrj #ifdef ROUND_TYPE_ALIGN
172938fd1498Szrj SET_TYPE_ALIGN (rli->t, ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t),
173038fd1498Szrj rli->record_align));
173138fd1498Szrj #else
173238fd1498Szrj SET_TYPE_ALIGN (rli->t, MAX (TYPE_ALIGN (rli->t), rli->record_align));
173338fd1498Szrj #endif
173438fd1498Szrj
173538fd1498Szrj /* Compute the size so far. Be sure to allow for extra bits in the
173638fd1498Szrj size in bytes. We have guaranteed above that it will be no more
173738fd1498Szrj than a single byte. */
173838fd1498Szrj unpadded_size = rli_size_so_far (rli);
173938fd1498Szrj unpadded_size_unit = rli_size_unit_so_far (rli);
174038fd1498Szrj if (! integer_zerop (rli->bitpos))
174138fd1498Szrj unpadded_size_unit
174238fd1498Szrj = size_binop (PLUS_EXPR, unpadded_size_unit, size_one_node);
174338fd1498Szrj
174438fd1498Szrj /* Round the size up to be a multiple of the required alignment. */
174538fd1498Szrj TYPE_SIZE (rli->t) = round_up (unpadded_size, TYPE_ALIGN (rli->t));
174638fd1498Szrj TYPE_SIZE_UNIT (rli->t)
174738fd1498Szrj = round_up (unpadded_size_unit, TYPE_ALIGN_UNIT (rli->t));
174838fd1498Szrj
174938fd1498Szrj if (TREE_CONSTANT (unpadded_size)
175038fd1498Szrj && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0
175138fd1498Szrj && input_location != BUILTINS_LOCATION)
175238fd1498Szrj warning (OPT_Wpadded, "padding struct size to alignment boundary");
175338fd1498Szrj
175438fd1498Szrj if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
175538fd1498Szrj && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
175638fd1498Szrj && TREE_CONSTANT (unpadded_size))
175738fd1498Szrj {
175838fd1498Szrj tree unpacked_size;
175938fd1498Szrj
176038fd1498Szrj #ifdef ROUND_TYPE_ALIGN
176138fd1498Szrj rli->unpacked_align
176238fd1498Szrj = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t), rli->unpacked_align);
176338fd1498Szrj #else
176438fd1498Szrj rli->unpacked_align = MAX (TYPE_ALIGN (rli->t), rli->unpacked_align);
176538fd1498Szrj #endif
176638fd1498Szrj
176738fd1498Szrj unpacked_size = round_up (TYPE_SIZE (rli->t), rli->unpacked_align);
176838fd1498Szrj if (simple_cst_equal (unpacked_size, TYPE_SIZE (rli->t)))
176938fd1498Szrj {
177038fd1498Szrj if (TYPE_NAME (rli->t))
177138fd1498Szrj {
177238fd1498Szrj tree name;
177338fd1498Szrj
177438fd1498Szrj if (TREE_CODE (TYPE_NAME (rli->t)) == IDENTIFIER_NODE)
177538fd1498Szrj name = TYPE_NAME (rli->t);
177638fd1498Szrj else
177738fd1498Szrj name = DECL_NAME (TYPE_NAME (rli->t));
177838fd1498Szrj
177938fd1498Szrj if (STRICT_ALIGNMENT)
178038fd1498Szrj warning (OPT_Wpacked, "packed attribute causes inefficient "
178138fd1498Szrj "alignment for %qE", name);
178238fd1498Szrj else
178338fd1498Szrj warning (OPT_Wpacked,
178438fd1498Szrj "packed attribute is unnecessary for %qE", name);
178538fd1498Szrj }
178638fd1498Szrj else
178738fd1498Szrj {
178838fd1498Szrj if (STRICT_ALIGNMENT)
178938fd1498Szrj warning (OPT_Wpacked,
179038fd1498Szrj "packed attribute causes inefficient alignment");
179138fd1498Szrj else
179238fd1498Szrj warning (OPT_Wpacked, "packed attribute is unnecessary");
179338fd1498Szrj }
179438fd1498Szrj }
179538fd1498Szrj }
179638fd1498Szrj }
179738fd1498Szrj
179838fd1498Szrj /* Compute the TYPE_MODE for the TYPE (which is a RECORD_TYPE). */
179938fd1498Szrj
180038fd1498Szrj void
compute_record_mode(tree type)180138fd1498Szrj compute_record_mode (tree type)
180238fd1498Szrj {
180338fd1498Szrj tree field;
180438fd1498Szrj machine_mode mode = VOIDmode;
180538fd1498Szrj
180638fd1498Szrj /* Most RECORD_TYPEs have BLKmode, so we start off assuming that.
180738fd1498Szrj However, if possible, we use a mode that fits in a register
180838fd1498Szrj instead, in order to allow for better optimization down the
180938fd1498Szrj line. */
181038fd1498Szrj SET_TYPE_MODE (type, BLKmode);
181138fd1498Szrj
181238fd1498Szrj if (! tree_fits_uhwi_p (TYPE_SIZE (type)))
181338fd1498Szrj return;
181438fd1498Szrj
181538fd1498Szrj /* A record which has any BLKmode members must itself be
181638fd1498Szrj BLKmode; it can't go in a register. Unless the member is
181738fd1498Szrj BLKmode only because it isn't aligned. */
181838fd1498Szrj for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
181938fd1498Szrj {
182038fd1498Szrj if (TREE_CODE (field) != FIELD_DECL)
182138fd1498Szrj continue;
182238fd1498Szrj
182338fd1498Szrj if (TREE_CODE (TREE_TYPE (field)) == ERROR_MARK
182438fd1498Szrj || (TYPE_MODE (TREE_TYPE (field)) == BLKmode
182538fd1498Szrj && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field))
182638fd1498Szrj && !(TYPE_SIZE (TREE_TYPE (field)) != 0
182738fd1498Szrj && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))))
182838fd1498Szrj || ! tree_fits_uhwi_p (bit_position (field))
182938fd1498Szrj || DECL_SIZE (field) == 0
183038fd1498Szrj || ! tree_fits_uhwi_p (DECL_SIZE (field)))
183138fd1498Szrj return;
183238fd1498Szrj
183338fd1498Szrj /* If this field is the whole struct, remember its mode so
183438fd1498Szrj that, say, we can put a double in a class into a DF
183538fd1498Szrj register instead of forcing it to live in the stack. */
183638fd1498Szrj if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
183738fd1498Szrj mode = DECL_MODE (field);
183838fd1498Szrj
183938fd1498Szrj /* With some targets, it is sub-optimal to access an aligned
184038fd1498Szrj BLKmode structure as a scalar. */
184138fd1498Szrj if (targetm.member_type_forces_blk (field, mode))
184238fd1498Szrj return;
184338fd1498Szrj }
184438fd1498Szrj
184538fd1498Szrj /* If we only have one real field; use its mode if that mode's size
184638fd1498Szrj matches the type's size. This only applies to RECORD_TYPE. This
184738fd1498Szrj does not apply to unions. */
184838fd1498Szrj if (TREE_CODE (type) == RECORD_TYPE && mode != VOIDmode
184938fd1498Szrj && tree_fits_uhwi_p (TYPE_SIZE (type))
185038fd1498Szrj && known_eq (GET_MODE_BITSIZE (mode), tree_to_uhwi (TYPE_SIZE (type))))
185138fd1498Szrj ;
185238fd1498Szrj else
185338fd1498Szrj mode = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1).else_blk ();
185438fd1498Szrj
185538fd1498Szrj /* If structure's known alignment is less than what the scalar
185638fd1498Szrj mode would need, and it matters, then stick with BLKmode. */
185738fd1498Szrj if (mode != BLKmode
185838fd1498Szrj && STRICT_ALIGNMENT
185938fd1498Szrj && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
186038fd1498Szrj || TYPE_ALIGN (type) >= GET_MODE_ALIGNMENT (mode)))
186138fd1498Szrj {
186238fd1498Szrj /* If this is the only reason this type is BLKmode, then
186338fd1498Szrj don't force containing types to be BLKmode. */
186438fd1498Szrj TYPE_NO_FORCE_BLK (type) = 1;
186538fd1498Szrj mode = BLKmode;
186638fd1498Szrj }
186738fd1498Szrj
186838fd1498Szrj SET_TYPE_MODE (type, mode);
186938fd1498Szrj }
187038fd1498Szrj
187138fd1498Szrj /* Compute TYPE_SIZE and TYPE_ALIGN for TYPE, once it has been laid
187238fd1498Szrj out. */
187338fd1498Szrj
187438fd1498Szrj static void
finalize_type_size(tree type)187538fd1498Szrj finalize_type_size (tree type)
187638fd1498Szrj {
187738fd1498Szrj /* Normally, use the alignment corresponding to the mode chosen.
187838fd1498Szrj However, where strict alignment is not required, avoid
187938fd1498Szrj over-aligning structures, since most compilers do not do this
188038fd1498Szrj alignment. */
188138fd1498Szrj if (TYPE_MODE (type) != BLKmode
188238fd1498Szrj && TYPE_MODE (type) != VOIDmode
188338fd1498Szrj && (STRICT_ALIGNMENT || !AGGREGATE_TYPE_P (type)))
188438fd1498Szrj {
188538fd1498Szrj unsigned mode_align = GET_MODE_ALIGNMENT (TYPE_MODE (type));
188638fd1498Szrj
188738fd1498Szrj /* Don't override a larger alignment requirement coming from a user
188838fd1498Szrj alignment of one of the fields. */
188938fd1498Szrj if (mode_align >= TYPE_ALIGN (type))
189038fd1498Szrj {
189138fd1498Szrj SET_TYPE_ALIGN (type, mode_align);
189238fd1498Szrj TYPE_USER_ALIGN (type) = 0;
189338fd1498Szrj }
189438fd1498Szrj }
189538fd1498Szrj
189638fd1498Szrj /* Do machine-dependent extra alignment. */
189738fd1498Szrj #ifdef ROUND_TYPE_ALIGN
189838fd1498Szrj SET_TYPE_ALIGN (type,
189938fd1498Szrj ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT));
190038fd1498Szrj #endif
190138fd1498Szrj
190238fd1498Szrj /* If we failed to find a simple way to calculate the unit size
190338fd1498Szrj of the type, find it by division. */
190438fd1498Szrj if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
190538fd1498Szrj /* TYPE_SIZE (type) is computed in bitsizetype. After the division, the
190638fd1498Szrj result will fit in sizetype. We will get more efficient code using
190738fd1498Szrj sizetype, so we force a conversion. */
190838fd1498Szrj TYPE_SIZE_UNIT (type)
190938fd1498Szrj = fold_convert (sizetype,
191038fd1498Szrj size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
191138fd1498Szrj bitsize_unit_node));
191238fd1498Szrj
191338fd1498Szrj if (TYPE_SIZE (type) != 0)
191438fd1498Szrj {
191538fd1498Szrj TYPE_SIZE (type) = round_up (TYPE_SIZE (type), TYPE_ALIGN (type));
191638fd1498Szrj TYPE_SIZE_UNIT (type)
191738fd1498Szrj = round_up (TYPE_SIZE_UNIT (type), TYPE_ALIGN_UNIT (type));
191838fd1498Szrj }
191938fd1498Szrj
192038fd1498Szrj /* Evaluate nonconstant sizes only once, either now or as soon as safe. */
192138fd1498Szrj if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
192238fd1498Szrj TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
192338fd1498Szrj if (TYPE_SIZE_UNIT (type) != 0
192438fd1498Szrj && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
192538fd1498Szrj TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
192638fd1498Szrj
192738fd1498Szrj /* Handle empty records as per the x86-64 psABI. */
192838fd1498Szrj TYPE_EMPTY_P (type) = targetm.calls.empty_record_p (type);
192938fd1498Szrj
193038fd1498Szrj /* Also layout any other variants of the type. */
193138fd1498Szrj if (TYPE_NEXT_VARIANT (type)
193238fd1498Szrj || type != TYPE_MAIN_VARIANT (type))
193338fd1498Szrj {
193438fd1498Szrj tree variant;
193538fd1498Szrj /* Record layout info of this variant. */
193638fd1498Szrj tree size = TYPE_SIZE (type);
193738fd1498Szrj tree size_unit = TYPE_SIZE_UNIT (type);
193838fd1498Szrj unsigned int align = TYPE_ALIGN (type);
193938fd1498Szrj unsigned int precision = TYPE_PRECISION (type);
194038fd1498Szrj unsigned int user_align = TYPE_USER_ALIGN (type);
194138fd1498Szrj machine_mode mode = TYPE_MODE (type);
194238fd1498Szrj bool empty_p = TYPE_EMPTY_P (type);
194338fd1498Szrj
194438fd1498Szrj /* Copy it into all variants. */
194538fd1498Szrj for (variant = TYPE_MAIN_VARIANT (type);
194638fd1498Szrj variant != 0;
194738fd1498Szrj variant = TYPE_NEXT_VARIANT (variant))
194838fd1498Szrj {
194938fd1498Szrj TYPE_SIZE (variant) = size;
195038fd1498Szrj TYPE_SIZE_UNIT (variant) = size_unit;
195138fd1498Szrj unsigned valign = align;
195238fd1498Szrj if (TYPE_USER_ALIGN (variant))
195338fd1498Szrj valign = MAX (valign, TYPE_ALIGN (variant));
195438fd1498Szrj else
195538fd1498Szrj TYPE_USER_ALIGN (variant) = user_align;
195638fd1498Szrj SET_TYPE_ALIGN (variant, valign);
195738fd1498Szrj TYPE_PRECISION (variant) = precision;
195838fd1498Szrj SET_TYPE_MODE (variant, mode);
195938fd1498Szrj TYPE_EMPTY_P (variant) = empty_p;
196038fd1498Szrj }
196138fd1498Szrj }
196238fd1498Szrj }
196338fd1498Szrj
196438fd1498Szrj /* Return a new underlying object for a bitfield started with FIELD. */
196538fd1498Szrj
196638fd1498Szrj static tree
start_bitfield_representative(tree field)196738fd1498Szrj start_bitfield_representative (tree field)
196838fd1498Szrj {
196938fd1498Szrj tree repr = make_node (FIELD_DECL);
197038fd1498Szrj DECL_FIELD_OFFSET (repr) = DECL_FIELD_OFFSET (field);
197138fd1498Szrj /* Force the representative to begin at a BITS_PER_UNIT aligned
197238fd1498Szrj boundary - C++ may use tail-padding of a base object to
197338fd1498Szrj continue packing bits so the bitfield region does not start
197438fd1498Szrj at bit zero (see g++.dg/abi/bitfield5.C for example).
197538fd1498Szrj Unallocated bits may happen for other reasons as well,
197638fd1498Szrj for example Ada which allows explicit bit-granular structure layout. */
197738fd1498Szrj DECL_FIELD_BIT_OFFSET (repr)
197838fd1498Szrj = size_binop (BIT_AND_EXPR,
197938fd1498Szrj DECL_FIELD_BIT_OFFSET (field),
198038fd1498Szrj bitsize_int (~(BITS_PER_UNIT - 1)));
198138fd1498Szrj SET_DECL_OFFSET_ALIGN (repr, DECL_OFFSET_ALIGN (field));
198238fd1498Szrj DECL_SIZE (repr) = DECL_SIZE (field);
198338fd1498Szrj DECL_SIZE_UNIT (repr) = DECL_SIZE_UNIT (field);
198438fd1498Szrj DECL_PACKED (repr) = DECL_PACKED (field);
198538fd1498Szrj DECL_CONTEXT (repr) = DECL_CONTEXT (field);
198638fd1498Szrj /* There are no indirect accesses to this field. If we introduce
198738fd1498Szrj some then they have to use the record alias set. This makes
198838fd1498Szrj sure to properly conflict with [indirect] accesses to addressable
198938fd1498Szrj fields of the bitfield group. */
199038fd1498Szrj DECL_NONADDRESSABLE_P (repr) = 1;
199138fd1498Szrj return repr;
199238fd1498Szrj }
199338fd1498Szrj
199438fd1498Szrj /* Finish up a bitfield group that was started by creating the underlying
199538fd1498Szrj object REPR with the last field in the bitfield group FIELD. */
199638fd1498Szrj
199738fd1498Szrj static void
finish_bitfield_representative(tree repr,tree field)199838fd1498Szrj finish_bitfield_representative (tree repr, tree field)
199938fd1498Szrj {
200038fd1498Szrj unsigned HOST_WIDE_INT bitsize, maxbitsize;
200138fd1498Szrj tree nextf, size;
200238fd1498Szrj
200338fd1498Szrj size = size_diffop (DECL_FIELD_OFFSET (field),
200438fd1498Szrj DECL_FIELD_OFFSET (repr));
200538fd1498Szrj while (TREE_CODE (size) == COMPOUND_EXPR)
200638fd1498Szrj size = TREE_OPERAND (size, 1);
200738fd1498Szrj gcc_assert (tree_fits_uhwi_p (size));
200838fd1498Szrj bitsize = (tree_to_uhwi (size) * BITS_PER_UNIT
200938fd1498Szrj + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
201038fd1498Szrj - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr))
201138fd1498Szrj + tree_to_uhwi (DECL_SIZE (field)));
201238fd1498Szrj
201338fd1498Szrj /* Round up bitsize to multiples of BITS_PER_UNIT. */
201438fd1498Szrj bitsize = (bitsize + BITS_PER_UNIT - 1) & ~(BITS_PER_UNIT - 1);
201538fd1498Szrj
201638fd1498Szrj /* Now nothing tells us how to pad out bitsize ... */
201738fd1498Szrj nextf = DECL_CHAIN (field);
201838fd1498Szrj while (nextf && TREE_CODE (nextf) != FIELD_DECL)
201938fd1498Szrj nextf = DECL_CHAIN (nextf);
202038fd1498Szrj if (nextf)
202138fd1498Szrj {
202238fd1498Szrj tree maxsize;
202338fd1498Szrj /* If there was an error, the field may be not laid out
202438fd1498Szrj correctly. Don't bother to do anything. */
202538fd1498Szrj if (TREE_TYPE (nextf) == error_mark_node)
202638fd1498Szrj return;
202738fd1498Szrj maxsize = size_diffop (DECL_FIELD_OFFSET (nextf),
202838fd1498Szrj DECL_FIELD_OFFSET (repr));
202938fd1498Szrj if (tree_fits_uhwi_p (maxsize))
203038fd1498Szrj {
203138fd1498Szrj maxbitsize = (tree_to_uhwi (maxsize) * BITS_PER_UNIT
203238fd1498Szrj + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (nextf))
203338fd1498Szrj - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
203438fd1498Szrj /* If the group ends within a bitfield nextf does not need to be
203538fd1498Szrj aligned to BITS_PER_UNIT. Thus round up. */
203638fd1498Szrj maxbitsize = (maxbitsize + BITS_PER_UNIT - 1) & ~(BITS_PER_UNIT - 1);
203738fd1498Szrj }
203838fd1498Szrj else
203938fd1498Szrj maxbitsize = bitsize;
204038fd1498Szrj }
204138fd1498Szrj else
204238fd1498Szrj {
204338fd1498Szrj /* Note that if the C++ FE sets up tail-padding to be re-used it
204438fd1498Szrj creates a as-base variant of the type with TYPE_SIZE adjusted
204538fd1498Szrj accordingly. So it is safe to include tail-padding here. */
204638fd1498Szrj tree aggsize = lang_hooks.types.unit_size_without_reusable_padding
204738fd1498Szrj (DECL_CONTEXT (field));
204838fd1498Szrj tree maxsize = size_diffop (aggsize, DECL_FIELD_OFFSET (repr));
204938fd1498Szrj /* We cannot generally rely on maxsize to fold to an integer constant,
205038fd1498Szrj so use bitsize as fallback for this case. */
205138fd1498Szrj if (tree_fits_uhwi_p (maxsize))
205238fd1498Szrj maxbitsize = (tree_to_uhwi (maxsize) * BITS_PER_UNIT
205338fd1498Szrj - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
205438fd1498Szrj else
205538fd1498Szrj maxbitsize = bitsize;
205638fd1498Szrj }
205738fd1498Szrj
205838fd1498Szrj /* Only if we don't artificially break up the representative in
205938fd1498Szrj the middle of a large bitfield with different possibly
206038fd1498Szrj overlapping representatives. And all representatives start
206138fd1498Szrj at byte offset. */
206238fd1498Szrj gcc_assert (maxbitsize % BITS_PER_UNIT == 0);
206338fd1498Szrj
206438fd1498Szrj /* Find the smallest nice mode to use. */
206538fd1498Szrj opt_scalar_int_mode mode_iter;
206638fd1498Szrj FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
206738fd1498Szrj if (GET_MODE_BITSIZE (mode_iter.require ()) >= bitsize)
206838fd1498Szrj break;
206938fd1498Szrj
207038fd1498Szrj scalar_int_mode mode;
207138fd1498Szrj if (!mode_iter.exists (&mode)
207238fd1498Szrj || GET_MODE_BITSIZE (mode) > maxbitsize
207338fd1498Szrj || GET_MODE_BITSIZE (mode) > MAX_FIXED_MODE_SIZE)
207438fd1498Szrj {
207538fd1498Szrj /* We really want a BLKmode representative only as a last resort,
207638fd1498Szrj considering the member b in
207738fd1498Szrj struct { int a : 7; int b : 17; int c; } __attribute__((packed));
207838fd1498Szrj Otherwise we simply want to split the representative up
207938fd1498Szrj allowing for overlaps within the bitfield region as required for
208038fd1498Szrj struct { int a : 7; int b : 7;
208138fd1498Szrj int c : 10; int d; } __attribute__((packed));
208238fd1498Szrj [0, 15] HImode for a and b, [8, 23] HImode for c. */
208338fd1498Szrj DECL_SIZE (repr) = bitsize_int (bitsize);
208438fd1498Szrj DECL_SIZE_UNIT (repr) = size_int (bitsize / BITS_PER_UNIT);
208538fd1498Szrj SET_DECL_MODE (repr, BLKmode);
208638fd1498Szrj TREE_TYPE (repr) = build_array_type_nelts (unsigned_char_type_node,
208738fd1498Szrj bitsize / BITS_PER_UNIT);
208838fd1498Szrj }
208938fd1498Szrj else
209038fd1498Szrj {
209138fd1498Szrj unsigned HOST_WIDE_INT modesize = GET_MODE_BITSIZE (mode);
209238fd1498Szrj DECL_SIZE (repr) = bitsize_int (modesize);
209338fd1498Szrj DECL_SIZE_UNIT (repr) = size_int (modesize / BITS_PER_UNIT);
209438fd1498Szrj SET_DECL_MODE (repr, mode);
209538fd1498Szrj TREE_TYPE (repr) = lang_hooks.types.type_for_mode (mode, 1);
209638fd1498Szrj }
209738fd1498Szrj
209838fd1498Szrj /* Remember whether the bitfield group is at the end of the
209938fd1498Szrj structure or not. */
210038fd1498Szrj DECL_CHAIN (repr) = nextf;
210138fd1498Szrj }
210238fd1498Szrj
210338fd1498Szrj /* Compute and set FIELD_DECLs for the underlying objects we should
210438fd1498Szrj use for bitfield access for the structure T. */
210538fd1498Szrj
210638fd1498Szrj void
finish_bitfield_layout(tree t)210738fd1498Szrj finish_bitfield_layout (tree t)
210838fd1498Szrj {
210938fd1498Szrj tree field, prev;
211038fd1498Szrj tree repr = NULL_TREE;
211138fd1498Szrj
211238fd1498Szrj /* Unions would be special, for the ease of type-punning optimizations
211338fd1498Szrj we could use the underlying type as hint for the representative
211438fd1498Szrj if the bitfield would fit and the representative would not exceed
211538fd1498Szrj the union in size. */
211638fd1498Szrj if (TREE_CODE (t) != RECORD_TYPE)
211738fd1498Szrj return;
211838fd1498Szrj
211938fd1498Szrj for (prev = NULL_TREE, field = TYPE_FIELDS (t);
212038fd1498Szrj field; field = DECL_CHAIN (field))
212138fd1498Szrj {
212238fd1498Szrj if (TREE_CODE (field) != FIELD_DECL)
212338fd1498Szrj continue;
212438fd1498Szrj
212538fd1498Szrj /* In the C++ memory model, consecutive bit fields in a structure are
212638fd1498Szrj considered one memory location and updating a memory location
212738fd1498Szrj may not store into adjacent memory locations. */
212838fd1498Szrj if (!repr
212938fd1498Szrj && DECL_BIT_FIELD_TYPE (field))
213038fd1498Szrj {
213138fd1498Szrj /* Start new representative. */
213238fd1498Szrj repr = start_bitfield_representative (field);
213338fd1498Szrj }
213438fd1498Szrj else if (repr
213538fd1498Szrj && ! DECL_BIT_FIELD_TYPE (field))
213638fd1498Szrj {
213738fd1498Szrj /* Finish off new representative. */
213838fd1498Szrj finish_bitfield_representative (repr, prev);
213938fd1498Szrj repr = NULL_TREE;
214038fd1498Szrj }
214138fd1498Szrj else if (DECL_BIT_FIELD_TYPE (field))
214238fd1498Szrj {
214338fd1498Szrj gcc_assert (repr != NULL_TREE);
214438fd1498Szrj
214538fd1498Szrj /* Zero-size bitfields finish off a representative and
214638fd1498Szrj do not have a representative themselves. This is
214738fd1498Szrj required by the C++ memory model. */
214838fd1498Szrj if (integer_zerop (DECL_SIZE (field)))
214938fd1498Szrj {
215038fd1498Szrj finish_bitfield_representative (repr, prev);
215138fd1498Szrj repr = NULL_TREE;
215238fd1498Szrj }
215338fd1498Szrj
215438fd1498Szrj /* We assume that either DECL_FIELD_OFFSET of the representative
215538fd1498Szrj and each bitfield member is a constant or they are equal.
215638fd1498Szrj This is because we need to be able to compute the bit-offset
215738fd1498Szrj of each field relative to the representative in get_bit_range
215838fd1498Szrj during RTL expansion.
215938fd1498Szrj If these constraints are not met, simply force a new
216038fd1498Szrj representative to be generated. That will at most
216138fd1498Szrj generate worse code but still maintain correctness with
216238fd1498Szrj respect to the C++ memory model. */
216338fd1498Szrj else if (!((tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr))
216438fd1498Szrj && tree_fits_uhwi_p (DECL_FIELD_OFFSET (field)))
216538fd1498Szrj || operand_equal_p (DECL_FIELD_OFFSET (repr),
216638fd1498Szrj DECL_FIELD_OFFSET (field), 0)))
216738fd1498Szrj {
216838fd1498Szrj finish_bitfield_representative (repr, prev);
216938fd1498Szrj repr = start_bitfield_representative (field);
217038fd1498Szrj }
217138fd1498Szrj }
217238fd1498Szrj else
217338fd1498Szrj continue;
217438fd1498Szrj
217538fd1498Szrj if (repr)
217638fd1498Szrj DECL_BIT_FIELD_REPRESENTATIVE (field) = repr;
217738fd1498Szrj
217838fd1498Szrj prev = field;
217938fd1498Szrj }
218038fd1498Szrj
218138fd1498Szrj if (repr)
218238fd1498Szrj finish_bitfield_representative (repr, prev);
218338fd1498Szrj }
218438fd1498Szrj
218538fd1498Szrj /* Do all of the work required to layout the type indicated by RLI,
218638fd1498Szrj once the fields have been laid out. This function will call `free'
218738fd1498Szrj for RLI, unless FREE_P is false. Passing a value other than false
218838fd1498Szrj for FREE_P is bad practice; this option only exists to support the
218938fd1498Szrj G++ 3.2 ABI. */
219038fd1498Szrj
219138fd1498Szrj void
finish_record_layout(record_layout_info rli,int free_p)219238fd1498Szrj finish_record_layout (record_layout_info rli, int free_p)
219338fd1498Szrj {
219438fd1498Szrj tree variant;
219538fd1498Szrj
219638fd1498Szrj /* Compute the final size. */
219738fd1498Szrj finalize_record_size (rli);
219838fd1498Szrj
219938fd1498Szrj /* Compute the TYPE_MODE for the record. */
220038fd1498Szrj compute_record_mode (rli->t);
220138fd1498Szrj
220238fd1498Szrj /* Perform any last tweaks to the TYPE_SIZE, etc. */
220338fd1498Szrj finalize_type_size (rli->t);
220438fd1498Szrj
220538fd1498Szrj /* Compute bitfield representatives. */
220638fd1498Szrj finish_bitfield_layout (rli->t);
220738fd1498Szrj
220838fd1498Szrj /* Propagate TYPE_PACKED and TYPE_REVERSE_STORAGE_ORDER to variants.
220938fd1498Szrj With C++ templates, it is too early to do this when the attribute
221038fd1498Szrj is being parsed. */
221138fd1498Szrj for (variant = TYPE_NEXT_VARIANT (rli->t); variant;
221238fd1498Szrj variant = TYPE_NEXT_VARIANT (variant))
221338fd1498Szrj {
221438fd1498Szrj TYPE_PACKED (variant) = TYPE_PACKED (rli->t);
221538fd1498Szrj TYPE_REVERSE_STORAGE_ORDER (variant)
221638fd1498Szrj = TYPE_REVERSE_STORAGE_ORDER (rli->t);
221738fd1498Szrj }
221838fd1498Szrj
221938fd1498Szrj /* Lay out any static members. This is done now because their type
222038fd1498Szrj may use the record's type. */
222138fd1498Szrj while (!vec_safe_is_empty (rli->pending_statics))
222238fd1498Szrj layout_decl (rli->pending_statics->pop (), 0);
222338fd1498Szrj
222438fd1498Szrj /* Clean up. */
222538fd1498Szrj if (free_p)
222638fd1498Szrj {
222738fd1498Szrj vec_free (rli->pending_statics);
222838fd1498Szrj free (rli);
222938fd1498Szrj }
223038fd1498Szrj }
223138fd1498Szrj
223238fd1498Szrj
223338fd1498Szrj /* Finish processing a builtin RECORD_TYPE type TYPE. It's name is
223438fd1498Szrj NAME, its fields are chained in reverse on FIELDS.
223538fd1498Szrj
223638fd1498Szrj If ALIGN_TYPE is non-null, it is given the same alignment as
223738fd1498Szrj ALIGN_TYPE. */
223838fd1498Szrj
223938fd1498Szrj void
finish_builtin_struct(tree type,const char * name,tree fields,tree align_type)224038fd1498Szrj finish_builtin_struct (tree type, const char *name, tree fields,
224138fd1498Szrj tree align_type)
224238fd1498Szrj {
224338fd1498Szrj tree tail, next;
224438fd1498Szrj
224538fd1498Szrj for (tail = NULL_TREE; fields; tail = fields, fields = next)
224638fd1498Szrj {
224738fd1498Szrj DECL_FIELD_CONTEXT (fields) = type;
224838fd1498Szrj next = DECL_CHAIN (fields);
224938fd1498Szrj DECL_CHAIN (fields) = tail;
225038fd1498Szrj }
225138fd1498Szrj TYPE_FIELDS (type) = tail;
225238fd1498Szrj
225338fd1498Szrj if (align_type)
225438fd1498Szrj {
225538fd1498Szrj SET_TYPE_ALIGN (type, TYPE_ALIGN (align_type));
225638fd1498Szrj TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (align_type);
225738fd1498Szrj SET_TYPE_WARN_IF_NOT_ALIGN (type,
225838fd1498Szrj TYPE_WARN_IF_NOT_ALIGN (align_type));
225938fd1498Szrj }
226038fd1498Szrj
226138fd1498Szrj layout_type (type);
226238fd1498Szrj #if 0 /* not yet, should get fixed properly later */
226338fd1498Szrj TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
226438fd1498Szrj #else
226538fd1498Szrj TYPE_NAME (type) = build_decl (BUILTINS_LOCATION,
226638fd1498Szrj TYPE_DECL, get_identifier (name), type);
226738fd1498Szrj #endif
226838fd1498Szrj TYPE_STUB_DECL (type) = TYPE_NAME (type);
226938fd1498Szrj layout_decl (TYPE_NAME (type), 0);
227038fd1498Szrj }
227138fd1498Szrj
227238fd1498Szrj /* Calculate the mode, size, and alignment for TYPE.
227338fd1498Szrj For an array type, calculate the element separation as well.
227438fd1498Szrj Record TYPE on the chain of permanent or temporary types
227538fd1498Szrj so that dbxout will find out about it.
227638fd1498Szrj
227738fd1498Szrj TYPE_SIZE of a type is nonzero if the type has been laid out already.
227838fd1498Szrj layout_type does nothing on such a type.
227938fd1498Szrj
228038fd1498Szrj If the type is incomplete, its TYPE_SIZE remains zero. */
228138fd1498Szrj
228238fd1498Szrj void
layout_type(tree type)228338fd1498Szrj layout_type (tree type)
228438fd1498Szrj {
228538fd1498Szrj gcc_assert (type);
228638fd1498Szrj
228738fd1498Szrj if (type == error_mark_node)
228838fd1498Szrj return;
228938fd1498Szrj
229038fd1498Szrj /* We don't want finalize_type_size to copy an alignment attribute to
229138fd1498Szrj variants that don't have it. */
229238fd1498Szrj type = TYPE_MAIN_VARIANT (type);
229338fd1498Szrj
229438fd1498Szrj /* Do nothing if type has been laid out before. */
229538fd1498Szrj if (TYPE_SIZE (type))
229638fd1498Szrj return;
229738fd1498Szrj
229838fd1498Szrj switch (TREE_CODE (type))
229938fd1498Szrj {
230038fd1498Szrj case LANG_TYPE:
230138fd1498Szrj /* This kind of type is the responsibility
230238fd1498Szrj of the language-specific code. */
230338fd1498Szrj gcc_unreachable ();
230438fd1498Szrj
230538fd1498Szrj case BOOLEAN_TYPE:
230638fd1498Szrj case INTEGER_TYPE:
230738fd1498Szrj case ENUMERAL_TYPE:
230838fd1498Szrj {
230938fd1498Szrj scalar_int_mode mode
231038fd1498Szrj = smallest_int_mode_for_size (TYPE_PRECISION (type));
231138fd1498Szrj SET_TYPE_MODE (type, mode);
231238fd1498Szrj TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (mode));
231338fd1498Szrj /* Don't set TYPE_PRECISION here, as it may be set by a bitfield. */
231438fd1498Szrj TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (mode));
231538fd1498Szrj break;
231638fd1498Szrj }
231738fd1498Szrj
231838fd1498Szrj case REAL_TYPE:
231938fd1498Szrj {
232038fd1498Szrj /* Allow the caller to choose the type mode, which is how decimal
232138fd1498Szrj floats are distinguished from binary ones. */
232238fd1498Szrj if (TYPE_MODE (type) == VOIDmode)
232338fd1498Szrj SET_TYPE_MODE
232438fd1498Szrj (type, float_mode_for_size (TYPE_PRECISION (type)).require ());
232538fd1498Szrj scalar_float_mode mode = as_a <scalar_float_mode> (TYPE_MODE (type));
232638fd1498Szrj TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (mode));
232738fd1498Szrj TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (mode));
232838fd1498Szrj break;
232938fd1498Szrj }
233038fd1498Szrj
233138fd1498Szrj case FIXED_POINT_TYPE:
233238fd1498Szrj {
233338fd1498Szrj /* TYPE_MODE (type) has been set already. */
233438fd1498Szrj scalar_mode mode = SCALAR_TYPE_MODE (type);
233538fd1498Szrj TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (mode));
233638fd1498Szrj TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (mode));
233738fd1498Szrj break;
233838fd1498Szrj }
233938fd1498Szrj
234038fd1498Szrj case COMPLEX_TYPE:
234138fd1498Szrj TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TREE_TYPE (type));
234238fd1498Szrj SET_TYPE_MODE (type,
234338fd1498Szrj GET_MODE_COMPLEX_MODE (TYPE_MODE (TREE_TYPE (type))));
234438fd1498Szrj
234538fd1498Szrj TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
234638fd1498Szrj TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
234738fd1498Szrj break;
234838fd1498Szrj
234938fd1498Szrj case VECTOR_TYPE:
235038fd1498Szrj {
235138fd1498Szrj poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (type);
235238fd1498Szrj tree innertype = TREE_TYPE (type);
235338fd1498Szrj
235438fd1498Szrj /* Find an appropriate mode for the vector type. */
235538fd1498Szrj if (TYPE_MODE (type) == VOIDmode)
235638fd1498Szrj SET_TYPE_MODE (type,
235738fd1498Szrj mode_for_vector (SCALAR_TYPE_MODE (innertype),
235838fd1498Szrj nunits).else_blk ());
235938fd1498Szrj
236038fd1498Szrj TYPE_SATURATING (type) = TYPE_SATURATING (TREE_TYPE (type));
236138fd1498Szrj TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TREE_TYPE (type));
236238fd1498Szrj /* Several boolean vector elements may fit in a single unit. */
236338fd1498Szrj if (VECTOR_BOOLEAN_TYPE_P (type)
236438fd1498Szrj && type->type_common.mode != BLKmode)
236538fd1498Szrj TYPE_SIZE_UNIT (type)
236638fd1498Szrj = size_int (GET_MODE_SIZE (type->type_common.mode));
236738fd1498Szrj else
236838fd1498Szrj TYPE_SIZE_UNIT (type) = int_const_binop (MULT_EXPR,
236938fd1498Szrj TYPE_SIZE_UNIT (innertype),
237038fd1498Szrj size_int (nunits));
237138fd1498Szrj TYPE_SIZE (type) = int_const_binop
237238fd1498Szrj (MULT_EXPR,
237338fd1498Szrj bits_from_bytes (TYPE_SIZE_UNIT (type)),
237438fd1498Szrj bitsize_int (BITS_PER_UNIT));
237538fd1498Szrj
237638fd1498Szrj /* For vector types, we do not default to the mode's alignment.
237738fd1498Szrj Instead, query a target hook, defaulting to natural alignment.
237838fd1498Szrj This prevents ABI changes depending on whether or not native
237938fd1498Szrj vector modes are supported. */
238038fd1498Szrj SET_TYPE_ALIGN (type, targetm.vector_alignment (type));
238138fd1498Szrj
238238fd1498Szrj /* However, if the underlying mode requires a bigger alignment than
238338fd1498Szrj what the target hook provides, we cannot use the mode. For now,
238438fd1498Szrj simply reject that case. */
238538fd1498Szrj gcc_assert (TYPE_ALIGN (type)
238638fd1498Szrj >= GET_MODE_ALIGNMENT (TYPE_MODE (type)));
238738fd1498Szrj break;
238838fd1498Szrj }
238938fd1498Szrj
239038fd1498Szrj case VOID_TYPE:
239138fd1498Szrj /* This is an incomplete type and so doesn't have a size. */
239238fd1498Szrj SET_TYPE_ALIGN (type, 1);
239338fd1498Szrj TYPE_USER_ALIGN (type) = 0;
239438fd1498Szrj SET_TYPE_MODE (type, VOIDmode);
239538fd1498Szrj break;
239638fd1498Szrj
239738fd1498Szrj case POINTER_BOUNDS_TYPE:
239838fd1498Szrj TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
239938fd1498Szrj TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
240038fd1498Szrj break;
240138fd1498Szrj
240238fd1498Szrj case OFFSET_TYPE:
240338fd1498Szrj TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
240438fd1498Szrj TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE_UNITS);
240538fd1498Szrj /* A pointer might be MODE_PARTIAL_INT, but ptrdiff_t must be
240638fd1498Szrj integral, which may be an __intN. */
240738fd1498Szrj SET_TYPE_MODE (type, int_mode_for_size (POINTER_SIZE, 0).require ());
240838fd1498Szrj TYPE_PRECISION (type) = POINTER_SIZE;
240938fd1498Szrj break;
241038fd1498Szrj
241138fd1498Szrj case FUNCTION_TYPE:
241238fd1498Szrj case METHOD_TYPE:
241338fd1498Szrj /* It's hard to see what the mode and size of a function ought to
241438fd1498Szrj be, but we do know the alignment is FUNCTION_BOUNDARY, so
241538fd1498Szrj make it consistent with that. */
241638fd1498Szrj SET_TYPE_MODE (type,
241738fd1498Szrj int_mode_for_size (FUNCTION_BOUNDARY, 0).else_blk ());
241838fd1498Szrj TYPE_SIZE (type) = bitsize_int (FUNCTION_BOUNDARY);
241938fd1498Szrj TYPE_SIZE_UNIT (type) = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
242038fd1498Szrj break;
242138fd1498Szrj
242238fd1498Szrj case POINTER_TYPE:
242338fd1498Szrj case REFERENCE_TYPE:
242438fd1498Szrj {
242538fd1498Szrj scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
242638fd1498Szrj TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (mode));
242738fd1498Szrj TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (mode));
242838fd1498Szrj TYPE_UNSIGNED (type) = 1;
242938fd1498Szrj TYPE_PRECISION (type) = GET_MODE_PRECISION (mode);
243038fd1498Szrj }
243138fd1498Szrj break;
243238fd1498Szrj
243338fd1498Szrj case ARRAY_TYPE:
243438fd1498Szrj {
243538fd1498Szrj tree index = TYPE_DOMAIN (type);
243638fd1498Szrj tree element = TREE_TYPE (type);
243738fd1498Szrj
243838fd1498Szrj /* We need to know both bounds in order to compute the size. */
243938fd1498Szrj if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
244038fd1498Szrj && TYPE_SIZE (element))
244138fd1498Szrj {
244238fd1498Szrj tree ub = TYPE_MAX_VALUE (index);
244338fd1498Szrj tree lb = TYPE_MIN_VALUE (index);
244438fd1498Szrj tree element_size = TYPE_SIZE (element);
244538fd1498Szrj tree length;
244638fd1498Szrj
244738fd1498Szrj /* Make sure that an array of zero-sized element is zero-sized
244838fd1498Szrj regardless of its extent. */
244938fd1498Szrj if (integer_zerop (element_size))
245038fd1498Szrj length = size_zero_node;
245138fd1498Szrj
245238fd1498Szrj /* The computation should happen in the original signedness so
245338fd1498Szrj that (possible) negative values are handled appropriately
245438fd1498Szrj when determining overflow. */
245538fd1498Szrj else
245638fd1498Szrj {
245738fd1498Szrj /* ??? When it is obvious that the range is signed
245838fd1498Szrj represent it using ssizetype. */
245938fd1498Szrj if (TREE_CODE (lb) == INTEGER_CST
246038fd1498Szrj && TREE_CODE (ub) == INTEGER_CST
246138fd1498Szrj && TYPE_UNSIGNED (TREE_TYPE (lb))
246238fd1498Szrj && tree_int_cst_lt (ub, lb))
246338fd1498Szrj {
246438fd1498Szrj lb = wide_int_to_tree (ssizetype,
246538fd1498Szrj offset_int::from (wi::to_wide (lb),
246638fd1498Szrj SIGNED));
246738fd1498Szrj ub = wide_int_to_tree (ssizetype,
246838fd1498Szrj offset_int::from (wi::to_wide (ub),
246938fd1498Szrj SIGNED));
247038fd1498Szrj }
247138fd1498Szrj length
247238fd1498Szrj = fold_convert (sizetype,
247338fd1498Szrj size_binop (PLUS_EXPR,
247438fd1498Szrj build_int_cst (TREE_TYPE (lb), 1),
247538fd1498Szrj size_binop (MINUS_EXPR, ub, lb)));
247638fd1498Szrj }
247738fd1498Szrj
247838fd1498Szrj /* ??? We have no way to distinguish a null-sized array from an
247938fd1498Szrj array spanning the whole sizetype range, so we arbitrarily
248038fd1498Szrj decide that [0, -1] is the only valid representation. */
248138fd1498Szrj if (integer_zerop (length)
248238fd1498Szrj && TREE_OVERFLOW (length)
248338fd1498Szrj && integer_zerop (lb))
248438fd1498Szrj length = size_zero_node;
248538fd1498Szrj
248638fd1498Szrj TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size,
248738fd1498Szrj bits_from_bytes (length));
248838fd1498Szrj
248938fd1498Szrj /* If we know the size of the element, calculate the total size
249038fd1498Szrj directly, rather than do some division thing below. This
249138fd1498Szrj optimization helps Fortran assumed-size arrays (where the
249238fd1498Szrj size of the array is determined at runtime) substantially. */
249338fd1498Szrj if (TYPE_SIZE_UNIT (element))
249438fd1498Szrj TYPE_SIZE_UNIT (type)
249538fd1498Szrj = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
249638fd1498Szrj }
249738fd1498Szrj
249838fd1498Szrj /* Now round the alignment and size,
249938fd1498Szrj using machine-dependent criteria if any. */
250038fd1498Szrj
250138fd1498Szrj unsigned align = TYPE_ALIGN (element);
250238fd1498Szrj if (TYPE_USER_ALIGN (type))
250338fd1498Szrj align = MAX (align, TYPE_ALIGN (type));
250438fd1498Szrj else
250538fd1498Szrj TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (element);
250638fd1498Szrj if (!TYPE_WARN_IF_NOT_ALIGN (type))
250738fd1498Szrj SET_TYPE_WARN_IF_NOT_ALIGN (type,
250838fd1498Szrj TYPE_WARN_IF_NOT_ALIGN (element));
250938fd1498Szrj #ifdef ROUND_TYPE_ALIGN
251038fd1498Szrj align = ROUND_TYPE_ALIGN (type, align, BITS_PER_UNIT);
251138fd1498Szrj #else
251238fd1498Szrj align = MAX (align, BITS_PER_UNIT);
251338fd1498Szrj #endif
251438fd1498Szrj SET_TYPE_ALIGN (type, align);
251538fd1498Szrj SET_TYPE_MODE (type, BLKmode);
251638fd1498Szrj if (TYPE_SIZE (type) != 0
251738fd1498Szrj && ! targetm.member_type_forces_blk (type, VOIDmode)
251838fd1498Szrj /* BLKmode elements force BLKmode aggregate;
251938fd1498Szrj else extract/store fields may lose. */
252038fd1498Szrj && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
252138fd1498Szrj || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
252238fd1498Szrj {
252338fd1498Szrj SET_TYPE_MODE (type, mode_for_array (TREE_TYPE (type),
252438fd1498Szrj TYPE_SIZE (type)));
252538fd1498Szrj if (TYPE_MODE (type) != BLKmode
252638fd1498Szrj && STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
252738fd1498Szrj && TYPE_ALIGN (type) < GET_MODE_ALIGNMENT (TYPE_MODE (type)))
252838fd1498Szrj {
252938fd1498Szrj TYPE_NO_FORCE_BLK (type) = 1;
253038fd1498Szrj SET_TYPE_MODE (type, BLKmode);
253138fd1498Szrj }
253238fd1498Szrj }
253338fd1498Szrj if (AGGREGATE_TYPE_P (element))
253438fd1498Szrj TYPE_TYPELESS_STORAGE (type) = TYPE_TYPELESS_STORAGE (element);
253538fd1498Szrj /* When the element size is constant, check that it is at least as
253638fd1498Szrj large as the element alignment. */
253738fd1498Szrj if (TYPE_SIZE_UNIT (element)
253838fd1498Szrj && TREE_CODE (TYPE_SIZE_UNIT (element)) == INTEGER_CST
253938fd1498Szrj /* If TYPE_SIZE_UNIT overflowed, then it is certainly larger than
254038fd1498Szrj TYPE_ALIGN_UNIT. */
254138fd1498Szrj && !TREE_OVERFLOW (TYPE_SIZE_UNIT (element))
254238fd1498Szrj && !integer_zerop (TYPE_SIZE_UNIT (element))
254338fd1498Szrj && compare_tree_int (TYPE_SIZE_UNIT (element),
254438fd1498Szrj TYPE_ALIGN_UNIT (element)) < 0)
254538fd1498Szrj error ("alignment of array elements is greater than element size");
254638fd1498Szrj break;
254738fd1498Szrj }
254838fd1498Szrj
254938fd1498Szrj case RECORD_TYPE:
255038fd1498Szrj case UNION_TYPE:
255138fd1498Szrj case QUAL_UNION_TYPE:
255238fd1498Szrj {
255338fd1498Szrj tree field;
255438fd1498Szrj record_layout_info rli;
255538fd1498Szrj
255638fd1498Szrj /* Initialize the layout information. */
255738fd1498Szrj rli = start_record_layout (type);
255838fd1498Szrj
255938fd1498Szrj /* If this is a QUAL_UNION_TYPE, we want to process the fields
256038fd1498Szrj in the reverse order in building the COND_EXPR that denotes
256138fd1498Szrj its size. We reverse them again later. */
256238fd1498Szrj if (TREE_CODE (type) == QUAL_UNION_TYPE)
256338fd1498Szrj TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
256438fd1498Szrj
256538fd1498Szrj /* Place all the fields. */
256638fd1498Szrj for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
256738fd1498Szrj place_field (rli, field);
256838fd1498Szrj
256938fd1498Szrj if (TREE_CODE (type) == QUAL_UNION_TYPE)
257038fd1498Szrj TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
257138fd1498Szrj
257238fd1498Szrj /* Finish laying out the record. */
257338fd1498Szrj finish_record_layout (rli, /*free_p=*/true);
257438fd1498Szrj }
257538fd1498Szrj break;
257638fd1498Szrj
257738fd1498Szrj default:
257838fd1498Szrj gcc_unreachable ();
257938fd1498Szrj }
258038fd1498Szrj
258138fd1498Szrj /* Compute the final TYPE_SIZE, TYPE_ALIGN, etc. for TYPE. For
258238fd1498Szrj records and unions, finish_record_layout already called this
258338fd1498Szrj function. */
258438fd1498Szrj if (!RECORD_OR_UNION_TYPE_P (type))
258538fd1498Szrj finalize_type_size (type);
258638fd1498Szrj
258738fd1498Szrj /* We should never see alias sets on incomplete aggregates. And we
258838fd1498Szrj should not call layout_type on not incomplete aggregates. */
258938fd1498Szrj if (AGGREGATE_TYPE_P (type))
259038fd1498Szrj gcc_assert (!TYPE_ALIAS_SET_KNOWN_P (type));
259138fd1498Szrj }
259238fd1498Szrj
259338fd1498Szrj /* Return the least alignment required for type TYPE. */
259438fd1498Szrj
259538fd1498Szrj unsigned int
min_align_of_type(tree type)259638fd1498Szrj min_align_of_type (tree type)
259738fd1498Szrj {
259838fd1498Szrj unsigned int align = TYPE_ALIGN (type);
259938fd1498Szrj if (!TYPE_USER_ALIGN (type))
260038fd1498Szrj {
260138fd1498Szrj align = MIN (align, BIGGEST_ALIGNMENT);
260238fd1498Szrj #ifdef BIGGEST_FIELD_ALIGNMENT
260338fd1498Szrj align = MIN (align, BIGGEST_FIELD_ALIGNMENT);
260438fd1498Szrj #endif
260538fd1498Szrj unsigned int field_align = align;
260638fd1498Szrj #ifdef ADJUST_FIELD_ALIGN
260738fd1498Szrj field_align = ADJUST_FIELD_ALIGN (NULL_TREE, type, field_align);
260838fd1498Szrj #endif
260938fd1498Szrj align = MIN (align, field_align);
261038fd1498Szrj }
261138fd1498Szrj return align / BITS_PER_UNIT;
261238fd1498Szrj }
261338fd1498Szrj
261438fd1498Szrj /* Create and return a type for signed integers of PRECISION bits. */
261538fd1498Szrj
261638fd1498Szrj tree
make_signed_type(int precision)261738fd1498Szrj make_signed_type (int precision)
261838fd1498Szrj {
261938fd1498Szrj tree type = make_node (INTEGER_TYPE);
262038fd1498Szrj
262138fd1498Szrj TYPE_PRECISION (type) = precision;
262238fd1498Szrj
262338fd1498Szrj fixup_signed_type (type);
262438fd1498Szrj return type;
262538fd1498Szrj }
262638fd1498Szrj
262738fd1498Szrj /* Create and return a type for unsigned integers of PRECISION bits. */
262838fd1498Szrj
262938fd1498Szrj tree
make_unsigned_type(int precision)263038fd1498Szrj make_unsigned_type (int precision)
263138fd1498Szrj {
263238fd1498Szrj tree type = make_node (INTEGER_TYPE);
263338fd1498Szrj
263438fd1498Szrj TYPE_PRECISION (type) = precision;
263538fd1498Szrj
263638fd1498Szrj fixup_unsigned_type (type);
263738fd1498Szrj return type;
263838fd1498Szrj }
263938fd1498Szrj
264038fd1498Szrj /* Create and return a type for fract of PRECISION bits, UNSIGNEDP,
264138fd1498Szrj and SATP. */
264238fd1498Szrj
264338fd1498Szrj tree
make_fract_type(int precision,int unsignedp,int satp)264438fd1498Szrj make_fract_type (int precision, int unsignedp, int satp)
264538fd1498Szrj {
264638fd1498Szrj tree type = make_node (FIXED_POINT_TYPE);
264738fd1498Szrj
264838fd1498Szrj TYPE_PRECISION (type) = precision;
264938fd1498Szrj
265038fd1498Szrj if (satp)
265138fd1498Szrj TYPE_SATURATING (type) = 1;
265238fd1498Szrj
265338fd1498Szrj /* Lay out the type: set its alignment, size, etc. */
265438fd1498Szrj TYPE_UNSIGNED (type) = unsignedp;
265538fd1498Szrj enum mode_class mclass = unsignedp ? MODE_UFRACT : MODE_FRACT;
265638fd1498Szrj SET_TYPE_MODE (type, mode_for_size (precision, mclass, 0).require ());
265738fd1498Szrj layout_type (type);
265838fd1498Szrj
265938fd1498Szrj return type;
266038fd1498Szrj }
266138fd1498Szrj
266238fd1498Szrj /* Create and return a type for accum of PRECISION bits, UNSIGNEDP,
266338fd1498Szrj and SATP. */
266438fd1498Szrj
266538fd1498Szrj tree
make_accum_type(int precision,int unsignedp,int satp)266638fd1498Szrj make_accum_type (int precision, int unsignedp, int satp)
266738fd1498Szrj {
266838fd1498Szrj tree type = make_node (FIXED_POINT_TYPE);
266938fd1498Szrj
267038fd1498Szrj TYPE_PRECISION (type) = precision;
267138fd1498Szrj
267238fd1498Szrj if (satp)
267338fd1498Szrj TYPE_SATURATING (type) = 1;
267438fd1498Szrj
267538fd1498Szrj /* Lay out the type: set its alignment, size, etc. */
267638fd1498Szrj TYPE_UNSIGNED (type) = unsignedp;
267738fd1498Szrj enum mode_class mclass = unsignedp ? MODE_UACCUM : MODE_ACCUM;
267838fd1498Szrj SET_TYPE_MODE (type, mode_for_size (precision, mclass, 0).require ());
267938fd1498Szrj layout_type (type);
268038fd1498Szrj
268138fd1498Szrj return type;
268238fd1498Szrj }
268338fd1498Szrj
268438fd1498Szrj /* Initialize sizetypes so layout_type can use them. */
268538fd1498Szrj
268638fd1498Szrj void
initialize_sizetypes(void)268738fd1498Szrj initialize_sizetypes (void)
268838fd1498Szrj {
268938fd1498Szrj int precision, bprecision;
269038fd1498Szrj
269138fd1498Szrj /* Get sizetypes precision from the SIZE_TYPE target macro. */
269238fd1498Szrj if (strcmp (SIZETYPE, "unsigned int") == 0)
269338fd1498Szrj precision = INT_TYPE_SIZE;
269438fd1498Szrj else if (strcmp (SIZETYPE, "long unsigned int") == 0)
269538fd1498Szrj precision = LONG_TYPE_SIZE;
269638fd1498Szrj else if (strcmp (SIZETYPE, "long long unsigned int") == 0)
269738fd1498Szrj precision = LONG_LONG_TYPE_SIZE;
269838fd1498Szrj else if (strcmp (SIZETYPE, "short unsigned int") == 0)
269938fd1498Szrj precision = SHORT_TYPE_SIZE;
270038fd1498Szrj else
270138fd1498Szrj {
270238fd1498Szrj int i;
270338fd1498Szrj
270438fd1498Szrj precision = -1;
270538fd1498Szrj for (i = 0; i < NUM_INT_N_ENTS; i++)
270638fd1498Szrj if (int_n_enabled_p[i])
270738fd1498Szrj {
270838fd1498Szrj char name[50];
270938fd1498Szrj sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
271038fd1498Szrj
271138fd1498Szrj if (strcmp (name, SIZETYPE) == 0)
271238fd1498Szrj {
271338fd1498Szrj precision = int_n_data[i].bitsize;
271438fd1498Szrj }
271538fd1498Szrj }
271638fd1498Szrj if (precision == -1)
271738fd1498Szrj gcc_unreachable ();
271838fd1498Szrj }
271938fd1498Szrj
272038fd1498Szrj bprecision
272138fd1498Szrj = MIN (precision + LOG2_BITS_PER_UNIT + 1, MAX_FIXED_MODE_SIZE);
272238fd1498Szrj bprecision = GET_MODE_PRECISION (smallest_int_mode_for_size (bprecision));
272338fd1498Szrj if (bprecision > HOST_BITS_PER_DOUBLE_INT)
272438fd1498Szrj bprecision = HOST_BITS_PER_DOUBLE_INT;
272538fd1498Szrj
272638fd1498Szrj /* Create stubs for sizetype and bitsizetype so we can create constants. */
272738fd1498Szrj sizetype = make_node (INTEGER_TYPE);
272838fd1498Szrj TYPE_NAME (sizetype) = get_identifier ("sizetype");
272938fd1498Szrj TYPE_PRECISION (sizetype) = precision;
273038fd1498Szrj TYPE_UNSIGNED (sizetype) = 1;
273138fd1498Szrj bitsizetype = make_node (INTEGER_TYPE);
273238fd1498Szrj TYPE_NAME (bitsizetype) = get_identifier ("bitsizetype");
273338fd1498Szrj TYPE_PRECISION (bitsizetype) = bprecision;
273438fd1498Szrj TYPE_UNSIGNED (bitsizetype) = 1;
273538fd1498Szrj
273638fd1498Szrj /* Now layout both types manually. */
273738fd1498Szrj scalar_int_mode mode = smallest_int_mode_for_size (precision);
273838fd1498Szrj SET_TYPE_MODE (sizetype, mode);
273938fd1498Szrj SET_TYPE_ALIGN (sizetype, GET_MODE_ALIGNMENT (TYPE_MODE (sizetype)));
274038fd1498Szrj TYPE_SIZE (sizetype) = bitsize_int (precision);
274138fd1498Szrj TYPE_SIZE_UNIT (sizetype) = size_int (GET_MODE_SIZE (mode));
274238fd1498Szrj set_min_and_max_values_for_integral_type (sizetype, precision, UNSIGNED);
274338fd1498Szrj
274438fd1498Szrj mode = smallest_int_mode_for_size (bprecision);
274538fd1498Szrj SET_TYPE_MODE (bitsizetype, mode);
274638fd1498Szrj SET_TYPE_ALIGN (bitsizetype, GET_MODE_ALIGNMENT (TYPE_MODE (bitsizetype)));
274738fd1498Szrj TYPE_SIZE (bitsizetype) = bitsize_int (bprecision);
274838fd1498Szrj TYPE_SIZE_UNIT (bitsizetype) = size_int (GET_MODE_SIZE (mode));
274938fd1498Szrj set_min_and_max_values_for_integral_type (bitsizetype, bprecision, UNSIGNED);
275038fd1498Szrj
275138fd1498Szrj /* Create the signed variants of *sizetype. */
275238fd1498Szrj ssizetype = make_signed_type (TYPE_PRECISION (sizetype));
275338fd1498Szrj TYPE_NAME (ssizetype) = get_identifier ("ssizetype");
275438fd1498Szrj sbitsizetype = make_signed_type (TYPE_PRECISION (bitsizetype));
275538fd1498Szrj TYPE_NAME (sbitsizetype) = get_identifier ("sbitsizetype");
275638fd1498Szrj }
275738fd1498Szrj
275838fd1498Szrj /* TYPE is an integral type, i.e., an INTEGRAL_TYPE, ENUMERAL_TYPE
275938fd1498Szrj or BOOLEAN_TYPE. Set TYPE_MIN_VALUE and TYPE_MAX_VALUE
276038fd1498Szrj for TYPE, based on the PRECISION and whether or not the TYPE
276138fd1498Szrj IS_UNSIGNED. PRECISION need not correspond to a width supported
276238fd1498Szrj natively by the hardware; for example, on a machine with 8-bit,
276338fd1498Szrj 16-bit, and 32-bit register modes, PRECISION might be 7, 23, or
276438fd1498Szrj 61. */
276538fd1498Szrj
276638fd1498Szrj void
set_min_and_max_values_for_integral_type(tree type,int precision,signop sgn)276738fd1498Szrj set_min_and_max_values_for_integral_type (tree type,
276838fd1498Szrj int precision,
276938fd1498Szrj signop sgn)
277038fd1498Szrj {
277138fd1498Szrj /* For bitfields with zero width we end up creating integer types
277238fd1498Szrj with zero precision. Don't assign any minimum/maximum values
277338fd1498Szrj to those types, they don't have any valid value. */
277438fd1498Szrj if (precision < 1)
277538fd1498Szrj return;
277638fd1498Szrj
277738fd1498Szrj TYPE_MIN_VALUE (type)
277838fd1498Szrj = wide_int_to_tree (type, wi::min_value (precision, sgn));
277938fd1498Szrj TYPE_MAX_VALUE (type)
278038fd1498Szrj = wide_int_to_tree (type, wi::max_value (precision, sgn));
278138fd1498Szrj }
278238fd1498Szrj
278338fd1498Szrj /* Set the extreme values of TYPE based on its precision in bits,
278438fd1498Szrj then lay it out. Used when make_signed_type won't do
278538fd1498Szrj because the tree code is not INTEGER_TYPE. */
278638fd1498Szrj
278738fd1498Szrj void
fixup_signed_type(tree type)278838fd1498Szrj fixup_signed_type (tree type)
278938fd1498Szrj {
279038fd1498Szrj int precision = TYPE_PRECISION (type);
279138fd1498Szrj
279238fd1498Szrj set_min_and_max_values_for_integral_type (type, precision, SIGNED);
279338fd1498Szrj
279438fd1498Szrj /* Lay out the type: set its alignment, size, etc. */
279538fd1498Szrj layout_type (type);
279638fd1498Szrj }
279738fd1498Szrj
279838fd1498Szrj /* Set the extreme values of TYPE based on its precision in bits,
279938fd1498Szrj then lay it out. This is used both in `make_unsigned_type'
280038fd1498Szrj and for enumeral types. */
280138fd1498Szrj
280238fd1498Szrj void
fixup_unsigned_type(tree type)280338fd1498Szrj fixup_unsigned_type (tree type)
280438fd1498Szrj {
280538fd1498Szrj int precision = TYPE_PRECISION (type);
280638fd1498Szrj
280738fd1498Szrj TYPE_UNSIGNED (type) = 1;
280838fd1498Szrj
280938fd1498Szrj set_min_and_max_values_for_integral_type (type, precision, UNSIGNED);
281038fd1498Szrj
281138fd1498Szrj /* Lay out the type: set its alignment, size, etc. */
281238fd1498Szrj layout_type (type);
281338fd1498Szrj }
281438fd1498Szrj
281538fd1498Szrj /* Construct an iterator for a bitfield that spans BITSIZE bits,
281638fd1498Szrj starting at BITPOS.
281738fd1498Szrj
281838fd1498Szrj BITREGION_START is the bit position of the first bit in this
281938fd1498Szrj sequence of bit fields. BITREGION_END is the last bit in this
282038fd1498Szrj sequence. If these two fields are non-zero, we should restrict the
282138fd1498Szrj memory access to that range. Otherwise, we are allowed to touch
282238fd1498Szrj any adjacent non bit-fields.
282338fd1498Szrj
282438fd1498Szrj ALIGN is the alignment of the underlying object in bits.
282538fd1498Szrj VOLATILEP says whether the bitfield is volatile. */
282638fd1498Szrj
282738fd1498Szrj bit_field_mode_iterator
bit_field_mode_iterator(HOST_WIDE_INT bitsize,HOST_WIDE_INT bitpos,poly_int64 bitregion_start,poly_int64 bitregion_end,unsigned int align,bool volatilep)282838fd1498Szrj ::bit_field_mode_iterator (HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
282938fd1498Szrj poly_int64 bitregion_start,
283038fd1498Szrj poly_int64 bitregion_end,
283138fd1498Szrj unsigned int align, bool volatilep)
283238fd1498Szrj : m_mode (NARROWEST_INT_MODE), m_bitsize (bitsize),
283338fd1498Szrj m_bitpos (bitpos), m_bitregion_start (bitregion_start),
283438fd1498Szrj m_bitregion_end (bitregion_end), m_align (align),
283538fd1498Szrj m_volatilep (volatilep), m_count (0)
283638fd1498Szrj {
283738fd1498Szrj if (known_eq (m_bitregion_end, 0))
283838fd1498Szrj {
283938fd1498Szrj /* We can assume that any aligned chunk of ALIGN bits that overlaps
284038fd1498Szrj the bitfield is mapped and won't trap, provided that ALIGN isn't
284138fd1498Szrj too large. The cap is the biggest required alignment for data,
284238fd1498Szrj or at least the word size. And force one such chunk at least. */
284338fd1498Szrj unsigned HOST_WIDE_INT units
284438fd1498Szrj = MIN (align, MAX (BIGGEST_ALIGNMENT, BITS_PER_WORD));
284538fd1498Szrj if (bitsize <= 0)
284638fd1498Szrj bitsize = 1;
284738fd1498Szrj HOST_WIDE_INT end = bitpos + bitsize + units - 1;
284838fd1498Szrj m_bitregion_end = end - end % units - 1;
284938fd1498Szrj }
285038fd1498Szrj }
285138fd1498Szrj
285238fd1498Szrj /* Calls to this function return successively larger modes that can be used
285338fd1498Szrj to represent the bitfield. Return true if another bitfield mode is
285438fd1498Szrj available, storing it in *OUT_MODE if so. */
285538fd1498Szrj
285638fd1498Szrj bool
next_mode(scalar_int_mode * out_mode)285738fd1498Szrj bit_field_mode_iterator::next_mode (scalar_int_mode *out_mode)
285838fd1498Szrj {
285938fd1498Szrj scalar_int_mode mode;
286038fd1498Szrj for (; m_mode.exists (&mode); m_mode = GET_MODE_WIDER_MODE (mode))
286138fd1498Szrj {
286238fd1498Szrj unsigned int unit = GET_MODE_BITSIZE (mode);
286338fd1498Szrj
286438fd1498Szrj /* Skip modes that don't have full precision. */
286538fd1498Szrj if (unit != GET_MODE_PRECISION (mode))
286638fd1498Szrj continue;
286738fd1498Szrj
286838fd1498Szrj /* Stop if the mode is too wide to handle efficiently. */
286938fd1498Szrj if (unit > MAX_FIXED_MODE_SIZE)
287038fd1498Szrj break;
287138fd1498Szrj
287238fd1498Szrj /* Don't deliver more than one multiword mode; the smallest one
287338fd1498Szrj should be used. */
287438fd1498Szrj if (m_count > 0 && unit > BITS_PER_WORD)
287538fd1498Szrj break;
287638fd1498Szrj
287738fd1498Szrj /* Skip modes that are too small. */
287838fd1498Szrj unsigned HOST_WIDE_INT substart = (unsigned HOST_WIDE_INT) m_bitpos % unit;
287938fd1498Szrj unsigned HOST_WIDE_INT subend = substart + m_bitsize;
288038fd1498Szrj if (subend > unit)
288138fd1498Szrj continue;
288238fd1498Szrj
288338fd1498Szrj /* Stop if the mode goes outside the bitregion. */
288438fd1498Szrj HOST_WIDE_INT start = m_bitpos - substart;
288538fd1498Szrj if (maybe_ne (m_bitregion_start, 0)
288638fd1498Szrj && maybe_lt (start, m_bitregion_start))
288738fd1498Szrj break;
288838fd1498Szrj HOST_WIDE_INT end = start + unit;
288938fd1498Szrj if (maybe_gt (end, m_bitregion_end + 1))
289038fd1498Szrj break;
289138fd1498Szrj
289238fd1498Szrj /* Stop if the mode requires too much alignment. */
289338fd1498Szrj if (GET_MODE_ALIGNMENT (mode) > m_align
289438fd1498Szrj && targetm.slow_unaligned_access (mode, m_align))
289538fd1498Szrj break;
289638fd1498Szrj
289738fd1498Szrj *out_mode = mode;
289838fd1498Szrj m_mode = GET_MODE_WIDER_MODE (mode);
289938fd1498Szrj m_count++;
290038fd1498Szrj return true;
290138fd1498Szrj }
290238fd1498Szrj return false;
290338fd1498Szrj }
290438fd1498Szrj
290538fd1498Szrj /* Return true if smaller modes are generally preferred for this kind
290638fd1498Szrj of bitfield. */
290738fd1498Szrj
290838fd1498Szrj bool
prefer_smaller_modes()290938fd1498Szrj bit_field_mode_iterator::prefer_smaller_modes ()
291038fd1498Szrj {
291138fd1498Szrj return (m_volatilep
291238fd1498Szrj ? targetm.narrow_volatile_bitfield ()
291338fd1498Szrj : !SLOW_BYTE_ACCESS);
291438fd1498Szrj }
291538fd1498Szrj
291638fd1498Szrj /* Find the best machine mode to use when referencing a bit field of length
291738fd1498Szrj BITSIZE bits starting at BITPOS.
291838fd1498Szrj
291938fd1498Szrj BITREGION_START is the bit position of the first bit in this
292038fd1498Szrj sequence of bit fields. BITREGION_END is the last bit in this
292138fd1498Szrj sequence. If these two fields are non-zero, we should restrict the
292238fd1498Szrj memory access to that range. Otherwise, we are allowed to touch
292338fd1498Szrj any adjacent non bit-fields.
292438fd1498Szrj
292538fd1498Szrj The chosen mode must have no more than LARGEST_MODE_BITSIZE bits.
292638fd1498Szrj INT_MAX is a suitable value for LARGEST_MODE_BITSIZE if the caller
292738fd1498Szrj doesn't want to apply a specific limit.
292838fd1498Szrj
292938fd1498Szrj If no mode meets all these conditions, we return VOIDmode.
293038fd1498Szrj
293138fd1498Szrj The underlying object is known to be aligned to a boundary of ALIGN bits.
293238fd1498Szrj
293338fd1498Szrj If VOLATILEP is false and SLOW_BYTE_ACCESS is false, we return the
293438fd1498Szrj smallest mode meeting these conditions.
293538fd1498Szrj
293638fd1498Szrj If VOLATILEP is false and SLOW_BYTE_ACCESS is true, we return the
293738fd1498Szrj largest mode (but a mode no wider than UNITS_PER_WORD) that meets
293838fd1498Szrj all the conditions.
293938fd1498Szrj
294038fd1498Szrj If VOLATILEP is true the narrow_volatile_bitfields target hook is used to
294138fd1498Szrj decide which of the above modes should be used. */
294238fd1498Szrj
294338fd1498Szrj bool
get_best_mode(int bitsize,int bitpos,poly_uint64 bitregion_start,poly_uint64 bitregion_end,unsigned int align,unsigned HOST_WIDE_INT largest_mode_bitsize,bool volatilep,scalar_int_mode * best_mode)294438fd1498Szrj get_best_mode (int bitsize, int bitpos,
294538fd1498Szrj poly_uint64 bitregion_start, poly_uint64 bitregion_end,
294638fd1498Szrj unsigned int align,
294738fd1498Szrj unsigned HOST_WIDE_INT largest_mode_bitsize, bool volatilep,
294838fd1498Szrj scalar_int_mode *best_mode)
294938fd1498Szrj {
295038fd1498Szrj bit_field_mode_iterator iter (bitsize, bitpos, bitregion_start,
295138fd1498Szrj bitregion_end, align, volatilep);
295238fd1498Szrj scalar_int_mode mode;
295338fd1498Szrj bool found = false;
295438fd1498Szrj while (iter.next_mode (&mode)
295538fd1498Szrj /* ??? For historical reasons, reject modes that would normally
295638fd1498Szrj receive greater alignment, even if unaligned accesses are
295738fd1498Szrj acceptable. This has both advantages and disadvantages.
295838fd1498Szrj Removing this check means that something like:
295938fd1498Szrj
296038fd1498Szrj struct s { unsigned int x; unsigned int y; };
296138fd1498Szrj int f (struct s *s) { return s->x == 0 && s->y == 0; }
296238fd1498Szrj
296338fd1498Szrj can be implemented using a single load and compare on
296438fd1498Szrj 64-bit machines that have no alignment restrictions.
296538fd1498Szrj For example, on powerpc64-linux-gnu, we would generate:
296638fd1498Szrj
296738fd1498Szrj ld 3,0(3)
296838fd1498Szrj cntlzd 3,3
296938fd1498Szrj srdi 3,3,6
297038fd1498Szrj blr
297138fd1498Szrj
297238fd1498Szrj rather than:
297338fd1498Szrj
297438fd1498Szrj lwz 9,0(3)
297538fd1498Szrj cmpwi 7,9,0
297638fd1498Szrj bne 7,.L3
297738fd1498Szrj lwz 3,4(3)
297838fd1498Szrj cntlzw 3,3
297938fd1498Szrj srwi 3,3,5
298038fd1498Szrj extsw 3,3
298138fd1498Szrj blr
298238fd1498Szrj .p2align 4,,15
298338fd1498Szrj .L3:
298438fd1498Szrj li 3,0
298538fd1498Szrj blr
298638fd1498Szrj
298738fd1498Szrj However, accessing more than one field can make life harder
298838fd1498Szrj for the gimple optimizers. For example, gcc.dg/vect/bb-slp-5.c
298938fd1498Szrj has a series of unsigned short copies followed by a series of
299038fd1498Szrj unsigned short comparisons. With this check, both the copies
299138fd1498Szrj and comparisons remain 16-bit accesses and FRE is able
299238fd1498Szrj to eliminate the latter. Without the check, the comparisons
299338fd1498Szrj can be done using 2 64-bit operations, which FRE isn't able
299438fd1498Szrj to handle in the same way.
299538fd1498Szrj
299638fd1498Szrj Either way, it would probably be worth disabling this check
299738fd1498Szrj during expand. One particular example where removing the
299838fd1498Szrj check would help is the get_best_mode call in store_bit_field.
299938fd1498Szrj If we are given a memory bitregion of 128 bits that is aligned
300038fd1498Szrj to a 64-bit boundary, and the bitfield we want to modify is
300138fd1498Szrj in the second half of the bitregion, this check causes
300238fd1498Szrj store_bitfield to turn the memory into a 64-bit reference
300338fd1498Szrj to the _first_ half of the region. We later use
300438fd1498Szrj adjust_bitfield_address to get a reference to the correct half,
300538fd1498Szrj but doing so looks to adjust_bitfield_address as though we are
300638fd1498Szrj moving past the end of the original object, so it drops the
300738fd1498Szrj associated MEM_EXPR and MEM_OFFSET. Removing the check
300838fd1498Szrj causes store_bit_field to keep a 128-bit memory reference,
300938fd1498Szrj so that the final bitfield reference still has a MEM_EXPR
301038fd1498Szrj and MEM_OFFSET. */
301138fd1498Szrj && GET_MODE_ALIGNMENT (mode) <= align
301238fd1498Szrj && GET_MODE_BITSIZE (mode) <= largest_mode_bitsize)
301338fd1498Szrj {
301438fd1498Szrj *best_mode = mode;
301538fd1498Szrj found = true;
301638fd1498Szrj if (iter.prefer_smaller_modes ())
301738fd1498Szrj break;
301838fd1498Szrj }
301938fd1498Szrj
302038fd1498Szrj return found;
302138fd1498Szrj }
302238fd1498Szrj
302338fd1498Szrj /* Gets minimal and maximal values for MODE (signed or unsigned depending on
302438fd1498Szrj SIGN). The returned constants are made to be usable in TARGET_MODE. */
302538fd1498Szrj
302638fd1498Szrj void
get_mode_bounds(scalar_int_mode mode,int sign,scalar_int_mode target_mode,rtx * mmin,rtx * mmax)302738fd1498Szrj get_mode_bounds (scalar_int_mode mode, int sign,
302838fd1498Szrj scalar_int_mode target_mode,
302938fd1498Szrj rtx *mmin, rtx *mmax)
303038fd1498Szrj {
303138fd1498Szrj unsigned size = GET_MODE_PRECISION (mode);
303238fd1498Szrj unsigned HOST_WIDE_INT min_val, max_val;
303338fd1498Szrj
303438fd1498Szrj gcc_assert (size <= HOST_BITS_PER_WIDE_INT);
303538fd1498Szrj
303638fd1498Szrj /* Special case BImode, which has values 0 and STORE_FLAG_VALUE. */
303738fd1498Szrj if (mode == BImode)
303838fd1498Szrj {
303938fd1498Szrj if (STORE_FLAG_VALUE < 0)
304038fd1498Szrj {
304138fd1498Szrj min_val = STORE_FLAG_VALUE;
304238fd1498Szrj max_val = 0;
304338fd1498Szrj }
304438fd1498Szrj else
304538fd1498Szrj {
304638fd1498Szrj min_val = 0;
304738fd1498Szrj max_val = STORE_FLAG_VALUE;
304838fd1498Szrj }
304938fd1498Szrj }
305038fd1498Szrj else if (sign)
305138fd1498Szrj {
305238fd1498Szrj min_val = -(HOST_WIDE_INT_1U << (size - 1));
305338fd1498Szrj max_val = (HOST_WIDE_INT_1U << (size - 1)) - 1;
305438fd1498Szrj }
305538fd1498Szrj else
305638fd1498Szrj {
305738fd1498Szrj min_val = 0;
305838fd1498Szrj max_val = (HOST_WIDE_INT_1U << (size - 1) << 1) - 1;
305938fd1498Szrj }
306038fd1498Szrj
306138fd1498Szrj *mmin = gen_int_mode (min_val, target_mode);
306238fd1498Szrj *mmax = gen_int_mode (max_val, target_mode);
306338fd1498Szrj }
306438fd1498Szrj
306538fd1498Szrj #include "gt-stor-layout.h"
3066