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