forked from openhwgroup/corev-gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstor-layout.cc
3239 lines (2790 loc) · 106 KB
/
stor-layout.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* C-compiler utilities for types and variables storage layout
Copyright (C) 1987-2023 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "target.h"
#include "function.h"
#include "rtl.h"
#include "tree.h"
#include "memmodel.h"
#include "tm_p.h"
#include "stringpool.h"
#include "regs.h"
#include "emit-rtl.h"
#include "cgraph.h"
#include "diagnostic-core.h"
#include "fold-const.h"
#include "stor-layout.h"
#include "varasm.h"
#include "print-tree.h"
#include "langhooks.h"
#include "tree-inline.h"
#include "dumpfile.h"
#include "gimplify.h"
#include "attribs.h"
#include "debug.h"
#include "calls.h"
/* Data type for the expressions representing sizes of data types.
It is the first integer type laid out. */
tree sizetype_tab[(int) stk_type_kind_last];
/* If nonzero, this is an upper limit on alignment of structure fields.
The value is measured in bits. */
unsigned int maximum_field_alignment = TARGET_DEFAULT_PACK_STRUCT * BITS_PER_UNIT;
static tree self_referential_size (tree);
static void finalize_record_size (record_layout_info);
static void finalize_type_size (tree);
static void place_union_field (record_layout_info, tree);
static int excess_unit_span (HOST_WIDE_INT, HOST_WIDE_INT, HOST_WIDE_INT,
HOST_WIDE_INT, tree);
extern void debug_rli (record_layout_info);
/* Given a size SIZE that may not be a constant, return a SAVE_EXPR
to serve as the actual size-expression for a type or decl. */
tree
variable_size (tree size)
{
/* Obviously. */
if (TREE_CONSTANT (size))
return size;
/* If the size is self-referential, we can't make a SAVE_EXPR (see
save_expr for the rationale). But we can do something else. */
if (CONTAINS_PLACEHOLDER_P (size))
return self_referential_size (size);
/* If we are in the global binding level, we can't make a SAVE_EXPR
since it may end up being shared across functions, so it is up
to the front-end to deal with this case. */
if (lang_hooks.decls.global_bindings_p ())
return size;
return save_expr (size);
}
/* An array of functions used for self-referential size computation. */
static GTY(()) vec<tree, va_gc> *size_functions;
/* Return true if T is a self-referential component reference. */
static bool
self_referential_component_ref_p (tree t)
{
if (TREE_CODE (t) != COMPONENT_REF)
return false;
while (REFERENCE_CLASS_P (t))
t = TREE_OPERAND (t, 0);
return (TREE_CODE (t) == PLACEHOLDER_EXPR);
}
/* Similar to copy_tree_r but do not copy component references involving
PLACEHOLDER_EXPRs. These nodes are spotted in find_placeholder_in_expr
and substituted in substitute_in_expr. */
static tree
copy_self_referential_tree_r (tree *tp, int *walk_subtrees, void *data)
{
enum tree_code code = TREE_CODE (*tp);
/* Stop at types, decls, constants like copy_tree_r. */
if (TREE_CODE_CLASS (code) == tcc_type
|| TREE_CODE_CLASS (code) == tcc_declaration
|| TREE_CODE_CLASS (code) == tcc_constant)
{
*walk_subtrees = 0;
return NULL_TREE;
}
/* This is the pattern built in ada/make_aligning_type. */
else if (code == ADDR_EXPR
&& TREE_CODE (TREE_OPERAND (*tp, 0)) == PLACEHOLDER_EXPR)
{
*walk_subtrees = 0;
return NULL_TREE;
}
/* Default case: the component reference. */
else if (self_referential_component_ref_p (*tp))
{
*walk_subtrees = 0;
return NULL_TREE;
}
/* We're not supposed to have them in self-referential size trees
because we wouldn't properly control when they are evaluated.
However, not creating superfluous SAVE_EXPRs requires accurate
tracking of readonly-ness all the way down to here, which we
cannot always guarantee in practice. So punt in this case. */
else if (code == SAVE_EXPR)
return error_mark_node;
else if (code == STATEMENT_LIST)
gcc_unreachable ();
return copy_tree_r (tp, walk_subtrees, data);
}
/* Given a SIZE expression that is self-referential, return an equivalent
expression to serve as the actual size expression for a type. */
static tree
self_referential_size (tree size)
{
static unsigned HOST_WIDE_INT fnno = 0;
vec<tree> self_refs = vNULL;
tree param_type_list = NULL, param_decl_list = NULL;
tree t, ref, return_type, fntype, fnname, fndecl;
unsigned int i;
char buf[128];
vec<tree, va_gc> *args = NULL;
/* Do not factor out simple operations. */
t = skip_simple_constant_arithmetic (size);
if (TREE_CODE (t) == CALL_EXPR || self_referential_component_ref_p (t))
return size;
/* Collect the list of self-references in the expression. */
find_placeholder_in_expr (size, &self_refs);
gcc_assert (self_refs.length () > 0);
/* Obtain a private copy of the expression. */
t = size;
if (walk_tree (&t, copy_self_referential_tree_r, NULL, NULL) != NULL_TREE)
return size;
size = t;
/* Build the parameter and argument lists in parallel; also
substitute the former for the latter in the expression. */
vec_alloc (args, self_refs.length ());
FOR_EACH_VEC_ELT (self_refs, i, ref)
{
tree subst, param_name, param_type, param_decl;
if (DECL_P (ref))
{
/* We shouldn't have true variables here. */
gcc_assert (TREE_READONLY (ref));
subst = ref;
}
/* This is the pattern built in ada/make_aligning_type. */
else if (TREE_CODE (ref) == ADDR_EXPR)
subst = ref;
/* Default case: the component reference. */
else
subst = TREE_OPERAND (ref, 1);
sprintf (buf, "p%d", i);
param_name = get_identifier (buf);
param_type = TREE_TYPE (ref);
param_decl
= build_decl (input_location, PARM_DECL, param_name, param_type);
DECL_ARG_TYPE (param_decl) = param_type;
DECL_ARTIFICIAL (param_decl) = 1;
TREE_READONLY (param_decl) = 1;
size = substitute_in_expr (size, subst, param_decl);
param_type_list = tree_cons (NULL_TREE, param_type, param_type_list);
param_decl_list = chainon (param_decl, param_decl_list);
args->quick_push (ref);
}
self_refs.release ();
/* Append 'void' to indicate that the number of parameters is fixed. */
param_type_list = tree_cons (NULL_TREE, void_type_node, param_type_list);
/* The 3 lists have been created in reverse order. */
param_type_list = nreverse (param_type_list);
param_decl_list = nreverse (param_decl_list);
/* Build the function type. */
return_type = TREE_TYPE (size);
fntype = build_function_type (return_type, param_type_list);
/* Build the function declaration. */
sprintf (buf, "SZ" HOST_WIDE_INT_PRINT_UNSIGNED, fnno++);
fnname = get_file_function_name (buf);
fndecl = build_decl (input_location, FUNCTION_DECL, fnname, fntype);
for (t = param_decl_list; t; t = DECL_CHAIN (t))
DECL_CONTEXT (t) = fndecl;
DECL_ARGUMENTS (fndecl) = param_decl_list;
DECL_RESULT (fndecl)
= build_decl (input_location, RESULT_DECL, 0, return_type);
DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
/* The function has been created by the compiler and we don't
want to emit debug info for it. */
DECL_ARTIFICIAL (fndecl) = 1;
DECL_IGNORED_P (fndecl) = 1;
/* It is supposed to be "const" and never throw. */
TREE_READONLY (fndecl) = 1;
TREE_NOTHROW (fndecl) = 1;
/* We want it to be inlined when this is deemed profitable, as
well as discarded if every call has been integrated. */
DECL_DECLARED_INLINE_P (fndecl) = 1;
/* It is made up of a unique return statement. */
DECL_INITIAL (fndecl) = make_node (BLOCK);
BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
t = build2 (MODIFY_EXPR, return_type, DECL_RESULT (fndecl), size);
DECL_SAVED_TREE (fndecl) = build1 (RETURN_EXPR, void_type_node, t);
TREE_STATIC (fndecl) = 1;
/* Put it onto the list of size functions. */
vec_safe_push (size_functions, fndecl);
/* Replace the original expression with a call to the size function. */
return build_call_expr_loc_vec (UNKNOWN_LOCATION, fndecl, args);
}
/* Take, queue and compile all the size functions. It is essential that
the size functions be gimplified at the very end of the compilation
in order to guarantee transparent handling of self-referential sizes.
Otherwise the GENERIC inliner would not be able to inline them back
at each of their call sites, thus creating artificial non-constant
size expressions which would trigger nasty problems later on. */
void
finalize_size_functions (void)
{
unsigned int i;
tree fndecl;
for (i = 0; size_functions && size_functions->iterate (i, &fndecl); i++)
{
allocate_struct_function (fndecl, false);
set_cfun (NULL);
dump_function (TDI_original, fndecl);
/* As these functions are used to describe the layout of variable-length
structures, debug info generation needs their implementation. */
debug_hooks->size_function (fndecl);
gimplify_function_tree (fndecl);
cgraph_node::finalize_function (fndecl, false);
}
vec_free (size_functions);
}
/* Return a machine mode of class MCLASS with SIZE bits of precision,
if one exists. The mode may have padding bits as well the SIZE
value bits. If LIMIT is nonzero, disregard modes wider than
MAX_FIXED_MODE_SIZE. */
opt_machine_mode
mode_for_size (poly_uint64 size, enum mode_class mclass, int limit)
{
machine_mode mode;
int i;
if (limit && maybe_gt (size, (unsigned int) MAX_FIXED_MODE_SIZE))
return opt_machine_mode ();
/* Get the first mode which has this size, in the specified class. */
FOR_EACH_MODE_IN_CLASS (mode, mclass)
if (known_eq (GET_MODE_PRECISION (mode), size))
return mode;
if (mclass == MODE_INT || mclass == MODE_PARTIAL_INT)
for (i = 0; i < NUM_INT_N_ENTS; i ++)
if (known_eq (int_n_data[i].bitsize, size)
&& int_n_enabled_p[i])
return int_n_data[i].m;
return opt_machine_mode ();
}
/* Similar, except passed a tree node. */
opt_machine_mode
mode_for_size_tree (const_tree size, enum mode_class mclass, int limit)
{
unsigned HOST_WIDE_INT uhwi;
unsigned int ui;
if (!tree_fits_uhwi_p (size))
return opt_machine_mode ();
uhwi = tree_to_uhwi (size);
ui = uhwi;
if (uhwi != ui)
return opt_machine_mode ();
return mode_for_size (ui, mclass, limit);
}
/* Return the narrowest mode of class MCLASS that contains at least
SIZE bits. Abort if no such mode exists. */
machine_mode
smallest_mode_for_size (poly_uint64 size, enum mode_class mclass)
{
machine_mode mode = VOIDmode;
int i;
/* Get the first mode which has at least this size, in the
specified class. */
FOR_EACH_MODE_IN_CLASS (mode, mclass)
if (known_ge (GET_MODE_PRECISION (mode), size))
break;
gcc_assert (mode != VOIDmode);
if (mclass == MODE_INT || mclass == MODE_PARTIAL_INT)
for (i = 0; i < NUM_INT_N_ENTS; i ++)
if (known_ge (int_n_data[i].bitsize, size)
&& known_lt (int_n_data[i].bitsize, GET_MODE_PRECISION (mode))
&& int_n_enabled_p[i])
mode = int_n_data[i].m;
return mode;
}
/* Return an integer mode of exactly the same size as MODE, if one exists. */
opt_scalar_int_mode
int_mode_for_mode (machine_mode mode)
{
switch (GET_MODE_CLASS (mode))
{
case MODE_INT:
case MODE_PARTIAL_INT:
return as_a <scalar_int_mode> (mode);
case MODE_COMPLEX_INT:
case MODE_COMPLEX_FLOAT:
case MODE_FLOAT:
case MODE_DECIMAL_FLOAT:
case MODE_FRACT:
case MODE_ACCUM:
case MODE_UFRACT:
case MODE_UACCUM:
case MODE_VECTOR_BOOL:
case MODE_VECTOR_INT:
case MODE_VECTOR_FLOAT:
case MODE_VECTOR_FRACT:
case MODE_VECTOR_ACCUM:
case MODE_VECTOR_UFRACT:
case MODE_VECTOR_UACCUM:
return int_mode_for_size (GET_MODE_BITSIZE (mode), 0);
case MODE_OPAQUE:
return opt_scalar_int_mode ();
case MODE_RANDOM:
if (mode == BLKmode)
return opt_scalar_int_mode ();
/* fall through */
case MODE_CC:
default:
gcc_unreachable ();
}
}
/* Find a mode that can be used for efficient bitwise operations on MODE,
if one exists. */
opt_machine_mode
bitwise_mode_for_mode (machine_mode mode)
{
/* Quick exit if we already have a suitable mode. */
scalar_int_mode int_mode;
if (is_a <scalar_int_mode> (mode, &int_mode)
&& GET_MODE_BITSIZE (int_mode) <= MAX_FIXED_MODE_SIZE)
return int_mode;
/* Reuse the sanity checks from int_mode_for_mode. */
gcc_checking_assert ((int_mode_for_mode (mode), true));
poly_int64 bitsize = GET_MODE_BITSIZE (mode);
/* Try to replace complex modes with complex modes. In general we
expect both components to be processed independently, so we only
care whether there is a register for the inner mode. */
if (COMPLEX_MODE_P (mode))
{
machine_mode trial = mode;
if ((GET_MODE_CLASS (trial) == MODE_COMPLEX_INT
|| mode_for_size (bitsize, MODE_COMPLEX_INT, false).exists (&trial))
&& have_regs_of_mode[GET_MODE_INNER (trial)])
return trial;
}
/* Try to replace vector modes with vector modes. Also try using vector
modes if an integer mode would be too big. */
if (VECTOR_MODE_P (mode)
|| maybe_gt (bitsize, MAX_FIXED_MODE_SIZE))
{
machine_mode trial = mode;
if ((GET_MODE_CLASS (trial) == MODE_VECTOR_INT
|| mode_for_size (bitsize, MODE_VECTOR_INT, 0).exists (&trial))
&& have_regs_of_mode[trial]
&& targetm.vector_mode_supported_p (trial))
return trial;
}
/* Otherwise fall back on integers while honoring MAX_FIXED_MODE_SIZE. */
return mode_for_size (bitsize, MODE_INT, true);
}
/* Find a type that can be used for efficient bitwise operations on MODE.
Return null if no such mode exists. */
tree
bitwise_type_for_mode (machine_mode mode)
{
if (!bitwise_mode_for_mode (mode).exists (&mode))
return NULL_TREE;
unsigned int inner_size = GET_MODE_UNIT_BITSIZE (mode);
tree inner_type = build_nonstandard_integer_type (inner_size, true);
if (VECTOR_MODE_P (mode))
return build_vector_type_for_mode (inner_type, mode);
if (COMPLEX_MODE_P (mode))
return build_complex_type (inner_type);
gcc_checking_assert (GET_MODE_INNER (mode) == mode);
return inner_type;
}
/* Find a mode that is suitable for representing a vector with NUNITS
elements of mode INNERMODE, if one exists. The returned mode can be
either an integer mode or a vector mode. */
opt_machine_mode
mode_for_vector (scalar_mode innermode, poly_uint64 nunits)
{
machine_mode mode;
/* First, look for a supported vector type. */
if (SCALAR_FLOAT_MODE_P (innermode))
mode = MIN_MODE_VECTOR_FLOAT;
else if (SCALAR_FRACT_MODE_P (innermode))
mode = MIN_MODE_VECTOR_FRACT;
else if (SCALAR_UFRACT_MODE_P (innermode))
mode = MIN_MODE_VECTOR_UFRACT;
else if (SCALAR_ACCUM_MODE_P (innermode))
mode = MIN_MODE_VECTOR_ACCUM;
else if (SCALAR_UACCUM_MODE_P (innermode))
mode = MIN_MODE_VECTOR_UACCUM;
else
mode = MIN_MODE_VECTOR_INT;
/* Only check the broader vector_mode_supported_any_target_p here.
We'll filter through target-specific availability and
vector_mode_supported_p later in vector_type_mode. */
FOR_EACH_MODE_FROM (mode, mode)
if (known_eq (GET_MODE_NUNITS (mode), nunits)
&& GET_MODE_INNER (mode) == innermode
&& targetm.vector_mode_supported_any_target_p (mode))
return mode;
/* For integers, try mapping it to a same-sized scalar mode. */
if (GET_MODE_CLASS (innermode) == MODE_INT)
{
poly_uint64 nbits = nunits * GET_MODE_BITSIZE (innermode);
if (int_mode_for_size (nbits, 0).exists (&mode)
&& have_regs_of_mode[mode])
return mode;
}
return opt_machine_mode ();
}
/* If a piece of code is using vector mode VECTOR_MODE and also wants
to operate on elements of mode ELEMENT_MODE, return the vector mode
it should use for those elements. If NUNITS is nonzero, ensure that
the mode has exactly NUNITS elements, otherwise pick whichever vector
size pairs the most naturally with VECTOR_MODE; this may mean choosing
a mode with a different size and/or number of elements, depending on
what the target prefers. Return an empty opt_machine_mode if there
is no supported vector mode with the required properties.
Unlike mode_for_vector. any returned mode is guaranteed to satisfy
both VECTOR_MODE_P and targetm.vector_mode_supported_p. */
opt_machine_mode
related_vector_mode (machine_mode vector_mode, scalar_mode element_mode,
poly_uint64 nunits)
{
gcc_assert (VECTOR_MODE_P (vector_mode));
return targetm.vectorize.related_mode (vector_mode, element_mode, nunits);
}
/* If a piece of code is using vector mode VECTOR_MODE and also wants
to operate on integer vectors with the same element size and number
of elements, return the vector mode it should use. Return an empty
opt_machine_mode if there is no supported vector mode with the
required properties.
Unlike mode_for_vector. any returned mode is guaranteed to satisfy
both VECTOR_MODE_P and targetm.vector_mode_supported_p. */
opt_machine_mode
related_int_vector_mode (machine_mode vector_mode)
{
gcc_assert (VECTOR_MODE_P (vector_mode));
scalar_int_mode int_mode;
if (int_mode_for_mode (GET_MODE_INNER (vector_mode)).exists (&int_mode))
return related_vector_mode (vector_mode, int_mode,
GET_MODE_NUNITS (vector_mode));
return opt_machine_mode ();
}
/* Return the alignment of MODE. This will be bounded by 1 and
BIGGEST_ALIGNMENT. */
unsigned int
get_mode_alignment (machine_mode mode)
{
return MIN (BIGGEST_ALIGNMENT, MAX (1, mode_base_align[mode]*BITS_PER_UNIT));
}
/* Return the natural mode of an array, given that it is SIZE bytes in
total and has elements of type ELEM_TYPE. */
static machine_mode
mode_for_array (tree elem_type, tree size)
{
tree elem_size;
poly_uint64 int_size, int_elem_size;
unsigned HOST_WIDE_INT num_elems;
bool limit_p;
/* One-element arrays get the component type's mode. */
elem_size = TYPE_SIZE (elem_type);
if (simple_cst_equal (size, elem_size))
return TYPE_MODE (elem_type);
limit_p = true;
if (poly_int_tree_p (size, &int_size)
&& poly_int_tree_p (elem_size, &int_elem_size)
&& maybe_ne (int_elem_size, 0U)
&& constant_multiple_p (int_size, int_elem_size, &num_elems))
{
machine_mode elem_mode = TYPE_MODE (elem_type);
machine_mode mode;
if (targetm.array_mode (elem_mode, num_elems).exists (&mode))
return mode;
if (targetm.array_mode_supported_p (elem_mode, num_elems))
limit_p = false;
}
return mode_for_size_tree (size, MODE_INT, limit_p).else_blk ();
}
/* Subroutine of layout_decl: Force alignment required for the data type.
But if the decl itself wants greater alignment, don't override that. */
static inline void
do_type_align (tree type, tree decl)
{
if (TYPE_ALIGN (type) > DECL_ALIGN (decl))
{
SET_DECL_ALIGN (decl, TYPE_ALIGN (type));
if (TREE_CODE (decl) == FIELD_DECL)
DECL_USER_ALIGN (decl) = TYPE_USER_ALIGN (type);
}
if (TYPE_WARN_IF_NOT_ALIGN (type) > DECL_WARN_IF_NOT_ALIGN (decl))
SET_DECL_WARN_IF_NOT_ALIGN (decl, TYPE_WARN_IF_NOT_ALIGN (type));
}
/* Set the size, mode and alignment of a ..._DECL node.
TYPE_DECL does need this for C++.
Note that LABEL_DECL and CONST_DECL nodes do not need this,
and FUNCTION_DECL nodes have them set up in a special (and simple) way.
Don't call layout_decl for them.
KNOWN_ALIGN is the amount of alignment we can assume this
decl has with no special effort. It is relevant only for FIELD_DECLs
and depends on the previous fields.
All that matters about KNOWN_ALIGN is which powers of 2 divide it.
If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
the record will be aligned to suit. */
void
layout_decl (tree decl, unsigned int known_align)
{
tree type = TREE_TYPE (decl);
enum tree_code code = TREE_CODE (decl);
rtx rtl = NULL_RTX;
location_t loc = DECL_SOURCE_LOCATION (decl);
if (code == CONST_DECL)
return;
gcc_assert (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL
|| code == TYPE_DECL || code == FIELD_DECL);
rtl = DECL_RTL_IF_SET (decl);
if (type == error_mark_node)
type = void_type_node;
/* Usually the size and mode come from the data type without change,
however, the front-end may set the explicit width of the field, so its
size may not be the same as the size of its type. This happens with
bitfields, of course (an `int' bitfield may be only 2 bits, say), but it
also happens with other fields. For example, the C++ front-end creates
zero-sized fields corresponding to empty base classes, and depends on
layout_type setting DECL_FIELD_BITPOS correctly for the field. Set the
size in bytes from the size in bits. If we have already set the mode,
don't set it again since we can be called twice for FIELD_DECLs. */
DECL_UNSIGNED (decl) = TYPE_UNSIGNED (type);
if (DECL_MODE (decl) == VOIDmode)
SET_DECL_MODE (decl, TYPE_MODE (type));
if (DECL_SIZE (decl) == 0)
{
DECL_SIZE (decl) = TYPE_SIZE (type);
DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
}
else if (DECL_SIZE_UNIT (decl) == 0)
DECL_SIZE_UNIT (decl)
= fold_convert_loc (loc, sizetype,
size_binop_loc (loc, CEIL_DIV_EXPR, DECL_SIZE (decl),
bitsize_unit_node));
if (code != FIELD_DECL)
/* For non-fields, update the alignment from the type. */
do_type_align (type, decl);
else
/* For fields, it's a bit more complicated... */
{
bool old_user_align = DECL_USER_ALIGN (decl);
bool zero_bitfield = false;
bool packed_p = DECL_PACKED (decl);
unsigned int mfa;
if (DECL_BIT_FIELD (decl))
{
DECL_BIT_FIELD_TYPE (decl) = type;
/* A zero-length bit-field affects the alignment of the next
field. In essence such bit-fields are not influenced by
any packing due to #pragma pack or attribute packed. */
if (integer_zerop (DECL_SIZE (decl))
&& ! targetm.ms_bitfield_layout_p (DECL_FIELD_CONTEXT (decl)))
{
zero_bitfield = true;
packed_p = false;
if (PCC_BITFIELD_TYPE_MATTERS)
do_type_align (type, decl);
else
{
#ifdef EMPTY_FIELD_BOUNDARY
if (EMPTY_FIELD_BOUNDARY > DECL_ALIGN (decl))
{
SET_DECL_ALIGN (decl, EMPTY_FIELD_BOUNDARY);
DECL_USER_ALIGN (decl) = 0;
}
#endif
}
}
/* See if we can use an ordinary integer mode for a bit-field.
Conditions are: a fixed size that is correct for another mode,
occupying a complete byte or bytes on proper boundary. */
if (TYPE_SIZE (type) != 0
&& TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
&& GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
{
machine_mode xmode;
if (mode_for_size_tree (DECL_SIZE (decl),
MODE_INT, 1).exists (&xmode))
{
unsigned int xalign = GET_MODE_ALIGNMENT (xmode);
if (!(xalign > BITS_PER_UNIT && DECL_PACKED (decl))
&& (known_align == 0 || known_align >= xalign))
{
SET_DECL_ALIGN (decl, MAX (xalign, DECL_ALIGN (decl)));
SET_DECL_MODE (decl, xmode);
DECL_BIT_FIELD (decl) = 0;
}
}
}
/* Turn off DECL_BIT_FIELD if we won't need it set. */
if (TYPE_MODE (type) == BLKmode && DECL_MODE (decl) == BLKmode
&& known_align >= TYPE_ALIGN (type)
&& DECL_ALIGN (decl) >= TYPE_ALIGN (type))
DECL_BIT_FIELD (decl) = 0;
}
else if (packed_p && DECL_USER_ALIGN (decl))
/* Don't touch DECL_ALIGN. For other packed fields, go ahead and
round up; we'll reduce it again below. We want packing to
supersede USER_ALIGN inherited from the type, but defer to
alignment explicitly specified on the field decl. */;
else
do_type_align (type, decl);
/* If the field is packed and not explicitly aligned, give it the
minimum alignment. Note that do_type_align may set
DECL_USER_ALIGN, so we need to check old_user_align instead. */
if (packed_p
&& !old_user_align)
SET_DECL_ALIGN (decl, MIN (DECL_ALIGN (decl), BITS_PER_UNIT));
if (! packed_p && ! DECL_USER_ALIGN (decl))
{
/* Some targets (i.e. i386, VMS) limit struct field alignment
to a lower boundary than alignment of variables unless
it was overridden by attribute aligned. */
#ifdef BIGGEST_FIELD_ALIGNMENT
SET_DECL_ALIGN (decl, MIN (DECL_ALIGN (decl),
(unsigned) BIGGEST_FIELD_ALIGNMENT));
#endif
#ifdef ADJUST_FIELD_ALIGN
SET_DECL_ALIGN (decl, ADJUST_FIELD_ALIGN (decl, TREE_TYPE (decl),
DECL_ALIGN (decl)));
#endif
}
if (zero_bitfield)
mfa = initial_max_fld_align * BITS_PER_UNIT;
else
mfa = maximum_field_alignment;
/* Should this be controlled by DECL_USER_ALIGN, too? */
if (mfa != 0)
SET_DECL_ALIGN (decl, MIN (DECL_ALIGN (decl), mfa));
}
/* Evaluate nonconstant size only once, either now or as soon as safe. */
if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
if (DECL_SIZE_UNIT (decl) != 0
&& TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST)
DECL_SIZE_UNIT (decl) = variable_size (DECL_SIZE_UNIT (decl));
/* If requested, warn about definitions of large data objects. */
if ((code == PARM_DECL || (code == VAR_DECL && !DECL_NONLOCAL_FRAME (decl)))
&& !DECL_EXTERNAL (decl))
{
tree size = DECL_SIZE_UNIT (decl);
if (size != 0 && TREE_CODE (size) == INTEGER_CST)
{
/* -Wlarger-than= argument of HOST_WIDE_INT_MAX is treated
as if PTRDIFF_MAX had been specified, with the value
being that on the target rather than the host. */
unsigned HOST_WIDE_INT max_size = warn_larger_than_size;
if (max_size == HOST_WIDE_INT_MAX)
max_size = tree_to_shwi (TYPE_MAX_VALUE (ptrdiff_type_node));
if (compare_tree_int (size, max_size) > 0)
warning (OPT_Wlarger_than_, "size of %q+D %E bytes exceeds "
"maximum object size %wu",
decl, size, max_size);
}
}
/* If the RTL was already set, update its mode and mem attributes. */
if (rtl)
{
PUT_MODE (rtl, DECL_MODE (decl));
SET_DECL_RTL (decl, 0);
if (MEM_P (rtl))
set_mem_attributes (rtl, decl, 1);
SET_DECL_RTL (decl, rtl);
}
}
/* Given a VAR_DECL, PARM_DECL, RESULT_DECL, or FIELD_DECL, clears the
results of a previous call to layout_decl and calls it again. */
void
relayout_decl (tree decl)
{
DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
SET_DECL_MODE (decl, VOIDmode);
if (!DECL_USER_ALIGN (decl))
SET_DECL_ALIGN (decl, 0);
if (DECL_RTL_SET_P (decl))
SET_DECL_RTL (decl, 0);
layout_decl (decl, 0);
}
/* Begin laying out type T, which may be a RECORD_TYPE, UNION_TYPE, or
QUAL_UNION_TYPE. Return a pointer to a struct record_layout_info which
is to be passed to all other layout functions for this record. It is the
responsibility of the caller to call `free' for the storage returned.
Note that garbage collection is not permitted until we finish laying
out the record. */
record_layout_info
start_record_layout (tree t)
{
record_layout_info rli = XNEW (struct record_layout_info_s);
rli->t = t;
/* If the type has a minimum specified alignment (via an attribute
declaration, for example) use it -- otherwise, start with a
one-byte alignment. */
rli->record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (t));
rli->unpacked_align = rli->record_align;
rli->offset_align = MAX (rli->record_align, BIGGEST_ALIGNMENT);
#ifdef STRUCTURE_SIZE_BOUNDARY
/* Packed structures don't need to have minimum size. */
if (! TYPE_PACKED (t))
{
unsigned tmp;
/* #pragma pack overrides STRUCTURE_SIZE_BOUNDARY. */
tmp = (unsigned) STRUCTURE_SIZE_BOUNDARY;
if (maximum_field_alignment != 0)
tmp = MIN (tmp, maximum_field_alignment);
rli->record_align = MAX (rli->record_align, tmp);
}
#endif
rli->offset = size_zero_node;
rli->bitpos = bitsize_zero_node;
rli->prev_field = 0;
rli->pending_statics = 0;
rli->packed_maybe_necessary = 0;
rli->remaining_in_alignment = 0;
return rli;
}
/* Fold sizetype value X to bitsizetype, given that X represents a type
size or offset. */
static tree
bits_from_bytes (tree x)
{
if (POLY_INT_CST_P (x))
/* The runtime calculation isn't allowed to overflow sizetype;
increasing the runtime values must always increase the size
or offset of the object. This means that the object imposes
a maximum value on the runtime parameters, but we don't record
what that is. */
return build_poly_int_cst
(bitsizetype,
poly_wide_int::from (poly_int_cst_value (x),
TYPE_PRECISION (bitsizetype),
TYPE_SIGN (TREE_TYPE (x))));
x = fold_convert (bitsizetype, x);
gcc_checking_assert (x);
return x;
}
/* Return the combined bit position for the byte offset OFFSET and the
bit position BITPOS.
These functions operate on byte and bit positions present in FIELD_DECLs
and assume that these expressions result in no (intermediate) overflow.
This assumption is necessary to fold the expressions as much as possible,
so as to avoid creating artificially variable-sized types in languages
supporting variable-sized types like Ada. */
tree
bit_from_pos (tree offset, tree bitpos)
{
return size_binop (PLUS_EXPR, bitpos,
size_binop (MULT_EXPR, bits_from_bytes (offset),
bitsize_unit_node));
}
/* Return the combined truncated byte position for the byte offset OFFSET and
the bit position BITPOS. */
tree
byte_from_pos (tree offset, tree bitpos)
{
tree bytepos;
if (TREE_CODE (bitpos) == MULT_EXPR
&& tree_int_cst_equal (TREE_OPERAND (bitpos, 1), bitsize_unit_node))
bytepos = TREE_OPERAND (bitpos, 0);
else
bytepos = size_binop (TRUNC_DIV_EXPR, bitpos, bitsize_unit_node);
return size_binop (PLUS_EXPR, offset, fold_convert (sizetype, bytepos));
}
/* Split the bit position POS into a byte offset *POFFSET and a bit
position *PBITPOS with the byte offset aligned to OFF_ALIGN bits. */
void
pos_from_bit (tree *poffset, tree *pbitpos, unsigned int off_align,
tree pos)
{
tree toff_align = bitsize_int (off_align);
if (TREE_CODE (pos) == MULT_EXPR
&& tree_int_cst_equal (TREE_OPERAND (pos, 1), toff_align))
{
*poffset = size_binop (MULT_EXPR,
fold_convert (sizetype, TREE_OPERAND (pos, 0)),
size_int (off_align / BITS_PER_UNIT));
*pbitpos = bitsize_zero_node;
}
else
{
*poffset = size_binop (MULT_EXPR,
fold_convert (sizetype,
size_binop (FLOOR_DIV_EXPR, pos,
toff_align)),
size_int (off_align / BITS_PER_UNIT));
*pbitpos = size_binop (FLOOR_MOD_EXPR, pos, toff_align);
}
}
/* Given a pointer to bit and byte offsets and an offset alignment,
normalize the offsets so they are within the alignment. */
void
normalize_offset (tree *poffset, tree *pbitpos, unsigned int off_align)
{
/* If the bit position is now larger than it should be, adjust it
downwards. */
if (compare_tree_int (*pbitpos, off_align) >= 0)
{
tree offset, bitpos;
pos_from_bit (&offset, &bitpos, off_align, *pbitpos);
*poffset = size_binop (PLUS_EXPR, *poffset, offset);
*pbitpos = bitpos;
}
}
/* Print debugging information about the information in RLI. */
DEBUG_FUNCTION void
debug_rli (record_layout_info rli)
{
print_node_brief (stderr, "type", rli->t, 0);
print_node_brief (stderr, "\noffset", rli->offset, 0);
print_node_brief (stderr, " bitpos", rli->bitpos, 0);
fprintf (stderr, "\naligns: rec = %u, unpack = %u, off = %u\n",
rli->record_align, rli->unpacked_align,
rli->offset_align);
/* The ms_struct code is the only that uses this. */
if (targetm.ms_bitfield_layout_p (rli->t))
fprintf (stderr, "remaining in alignment = %u\n", rli->remaining_in_alignment);
if (rli->packed_maybe_necessary)
fprintf (stderr, "packed may be necessary\n");
if (!vec_safe_is_empty (rli->pending_statics))