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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
|
From c2a0dcc565e0f6274f26644bd389337db8f2940c Mon Sep 17 00:00:00 2001
From: tiancheng-bao <baotiancheng1@huawei.com>
Date: Sat, 30 Mar 2024 11:04:23 +0800
Subject: [PATCH] [struct-reorg] Add Semi Relayout
---
gcc/common.opt | 6 +-
gcc/ipa-struct-reorg/ipa-struct-reorg.cc | 975 +++++++++++++++++-
gcc/ipa-struct-reorg/ipa-struct-reorg.h | 8 +
gcc/params.opt | 5 +
.../gcc.dg/struct/semi_relayout_rewrite.c | 86 ++
gcc/testsuite/gcc.dg/struct/struct-reorg.exp | 4 +
6 files changed, 1040 insertions(+), 44 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/struct/semi_relayout_rewrite.c
diff --git a/gcc/common.opt b/gcc/common.opt
index 38f1e457d..9484df5ad 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -2010,9 +2010,9 @@ Common Var(flag_ipa_struct_reorg) Init(0) Optimization
Perform structure layout optimizations.
fipa-struct-reorg=
-Common RejectNegative Joined UInteger Var(struct_layout_optimize_level) Init(0) IntegerRange(0, 5)
--fipa-struct-reorg=[0,1,2,3,4,5] adding none, struct-reorg, reorder-fields,
-dfe, safe-pointer-compression, unsafe-pointer-compression optimizations.
+Common RejectNegative Joined UInteger Var(struct_layout_optimize_level) Init(0) IntegerRange(0, 6)
+-fipa-struct-reorg=[0,1,2,3,4,5,6] adding none, struct-reorg, reorder-fields,
+dfe, safe-pointer-compression, unsafe-pointer-compression, semi-relayout optimizations.
fipa-vrp
Common Var(flag_ipa_vrp) Optimization
diff --git a/gcc/ipa-struct-reorg/ipa-struct-reorg.cc b/gcc/ipa-struct-reorg/ipa-struct-reorg.cc
index 3922873f3..6a202b4bd 100644
--- a/gcc/ipa-struct-reorg/ipa-struct-reorg.cc
+++ b/gcc/ipa-struct-reorg/ipa-struct-reorg.cc
@@ -294,7 +294,8 @@ enum struct_layout_opt_level
STRUCT_REORDER_FIELDS = 1 << 2,
DEAD_FIELD_ELIMINATION = 1 << 3,
POINTER_COMPRESSION_SAFE = 1 << 4,
- POINTER_COMPRESSION_UNSAFE = 1 << 5
+ POINTER_COMPRESSION_UNSAFE = 1 << 5,
+ SEMI_RELAYOUT = 1 << 6
};
/* Defines the target pointer size of compressed pointer, which should be 8,
@@ -308,6 +309,7 @@ void get_base (tree &base, tree expr);
static unsigned int current_layout_opt_level;
hash_map<tree, tree> replace_type_map;
+hash_map<tree, tree> semi_relayout_map;
/* Return true if one of these types is created by struct-reorg. */
@@ -426,7 +428,9 @@ srtype::srtype (tree type)
visited (false),
pc_candidate (false),
has_legal_alloc_num (false),
- has_alloc_array (0)
+ has_alloc_array (0),
+ semi_relayout (false),
+ bucket_parts (0)
{
for (int i = 0; i < max_split; i++)
newtype[i] = NULL_TREE;
@@ -891,6 +895,66 @@ srfield::create_new_reorder_fields (tree newtype[max_split],
newfield[0] = field;
}
+/* Given a struct s whose fields has already reordered by size, we try to
+ combine fields less than 8 bytes together to 8 bytes. Example:
+ struct s {
+ uint64_t a,
+ uint32_t b,
+ uint32_t c,
+ uint32_t d,
+ uint16_t e,
+ uint8_t f
+ }
+
+ We allocate memory for arrays of struct S, before semi-relayout, their
+ layout in memory is shown as below:
+ [a,b,c,d,e,f,padding;a,b,c,d,e,f,padding;...]
+
+ During semi-relayout, we put a number of structs into a same region called
+ bucket. The number is determined by param realyout-bucket-capacity-level.
+ Using 1024 here as example. After semi-relayout, the layout in a bucket is
+ shown as below:
+ part1 [a;a;a...]
+ part2 [b,c;b,c;b,c;...]
+ part3 [d,e,f,pad;d,e,f,pad;d,e,f,pad;...]
+
+ In the last bucket, if the amount of rest structs is less than the capacity
+ of a bucket, the rest of allcated memory will be wasted as padding. */
+
+unsigned
+srtype::calculate_bucket_size ()
+{
+ unsigned parts = 0;
+ unsigned bit_sum = 0;
+ unsigned relayout_offset = 0;
+ /* Currently, limit each 8 bytes with less than 2 fields. */
+ unsigned curr_part_num = 0;
+ unsigned field_num = 0;
+ for (tree f = TYPE_FIELDS (newtype[0]); f; f = DECL_CHAIN (f))
+ {
+ unsigned size = TYPE_PRECISION (TREE_TYPE (f));
+ bit_sum += size;
+ field_num++;
+ if (++curr_part_num > 2 || bit_sum > 64)
+ {
+ bit_sum = size;
+ parts++;
+ relayout_offset = relayout_part_size * parts;
+ curr_part_num = 1;
+ }
+ else
+ {
+ relayout_offset = relayout_part_size * parts + (bit_sum - size) / 8;
+ }
+ new_field_offsets.put (f, relayout_offset);
+ }
+ /* Donnot relayout a struct with only one field after DFE. */
+ if (field_num == 1)
+ return 0;
+ bucket_parts = ++parts;
+ return parts * relayout_part_size;
+}
+
/* Create the new TYPE corresponding to THIS type. */
bool
@@ -1001,6 +1065,15 @@ srtype::create_new_type (void)
if (pc_candidate && pc_gptr == NULL_TREE)
create_global_ptr_for_pc ();
+ if (semi_relayout)
+ {
+ bucket_size = calculate_bucket_size ();
+ if (bucket_size == 0)
+ return false;
+ if (semi_relayout_map.get (this->newtype[0]) == NULL)
+ semi_relayout_map.put (this->newtype[0], this->type);
+ }
+
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file, "Created %d types:\n", maxclusters);
@@ -1393,7 +1466,7 @@ public:
bool should_create = false, bool can_escape = false);
bool wholeaccess (tree expr, tree base, tree accesstype, srtype *t);
- void check_alloc_num (gimple *stmt, srtype *type);
+ void check_alloc_num (gimple *stmt, srtype *type, bool ptrptr);
void check_definition_assign (srdecl *decl, vec<srdecl *> &worklist);
void check_definition_call (srdecl *decl, vec<srdecl *> &worklist);
void check_definition (srdecl *decl, vec<srdecl *> &);
@@ -1442,6 +1515,33 @@ public:
tree &);
basic_block create_bb_for_compress_nullptr (basic_block, tree &);
basic_block create_bb_for_decompress_nullptr (basic_block, tree, tree &);
+
+ // Semi-relayout methods:
+ bool is_semi_relayout_candidate (tree);
+ srtype *get_semi_relayout_candidate_type (tree);
+ void check_and_prune_struct_for_semi_relayout (void);
+ tree rewrite_pointer_diff (gimple_stmt_iterator *, tree, tree, srtype *);
+ tree rewrite_pointer_plus_integer (gimple *, gimple_stmt_iterator *, tree,
+ tree, srtype *);
+ tree build_div_expr (gimple_stmt_iterator *, tree, tree);
+ tree get_true_pointer_base (gimple_stmt_iterator *, tree, srtype *);
+ tree get_real_allocated_ptr (tree, gimple_stmt_iterator *);
+ tree set_ptr_for_use (tree, gimple_stmt_iterator *);
+ void record_allocated_size (tree, gimple_stmt_iterator *, tree);
+ tree read_allocated_size (tree, gimple_stmt_iterator *);
+ gimple *create_aligned_alloc (gimple_stmt_iterator *, srtype *, tree,
+ tree &);
+ void create_memset_zero (tree, gimple_stmt_iterator *, tree);
+ void create_memcpy (tree, tree, tree, gimple_stmt_iterator *);
+ void create_free (tree, gimple_stmt_iterator *);
+ void copy_to_lhs (tree, tree, gimple_stmt_iterator *);
+ srtype *get_relayout_candidate_type (tree);
+ long unsigned int get_true_field_offset (srfield *, srtype *);
+ tree rewrite_address (tree, srfield *, srtype *, gimple_stmt_iterator *);
+ bool check_sr_copy (gimple *);
+ void relayout_field_copy (gimple_stmt_iterator *, gimple *, tree, tree,
+ tree&, tree &);
+ bool do_semi_relayout (gimple_stmt_iterator *, gimple *, tree &, tree &);
};
struct ipa_struct_relayout
@@ -4355,7 +4455,7 @@ ipa_struct_reorg::check_type_and_push (tree newdecl, srdecl *decl,
}
void
-ipa_struct_reorg::check_alloc_num (gimple *stmt, srtype *type)
+ipa_struct_reorg::check_alloc_num (gimple *stmt, srtype *type, bool ptrptr)
{
if (current_layout_opt_level >= COMPLETE_STRUCT_RELAYOUT
&& handled_allocation_stmt (stmt))
@@ -4363,13 +4463,28 @@ ipa_struct_reorg::check_alloc_num (gimple *stmt, srtype *type)
tree arg0 = gimple_call_arg (stmt, 0);
basic_block bb = gimple_bb (stmt);
cgraph_node *node = current_function->node;
+ if (!ptrptr && current_layout_opt_level >= SEMI_RELAYOUT
+ && gimple_call_builtin_p (stmt, BUILT_IN_MALLOC))
+ {
+ /* Malloc is commonly used for allocations of
+ a single struct and semi-relayout will waste
+ a mess of memory, so we skip it. */
+ type->has_alloc_array = -4;
+ return;
+ }
if (integer_onep (arg0))
/* Actually NOT an array, but may ruin other array. */
type->has_alloc_array = -1;
else if (bb->loop_father != NULL
&& loop_outer (bb->loop_father) != NULL)
- /* The allocation is in a loop. */
- type->has_alloc_array = -2;
+ {
+ /* For semi-relayout, do not escape realloc. */
+ if (current_layout_opt_level & SEMI_RELAYOUT
+ && gimple_call_builtin_p (stmt, BUILT_IN_REALLOC))
+ return;
+ /* The allocation is in a loop. */
+ type->has_alloc_array = -2;
+ }
else if (node->callers != NULL)
type->has_alloc_array = -3;
else
@@ -4448,6 +4563,13 @@ ipa_struct_reorg::check_definition_assign (srdecl *decl,
return;
}
+ if (semi_relayout_map.get (type->type) != NULL)
+ {
+ if (current_layout_opt_level != COMPLETE_STRUCT_RELAYOUT)
+ type->mark_escape (escape_unhandled_rewrite, stmt);
+ return;
+ }
+
/* d) if the name is from a cast/assignment, make sure it is used as
that type or void*
i) If void* then push the ssa_name into worklist. */
@@ -4484,7 +4606,8 @@ ipa_struct_reorg::check_definition_call (srdecl *decl, vec<srdecl *> &worklist)
type->mark_escape (escape_return, stmt);
}
- check_alloc_num (stmt, type);
+ bool ptrptr = isptrptr (decl->orig_type);
+ check_alloc_num (stmt, type, ptrptr);
return;
}
@@ -6038,6 +6161,55 @@ ipa_struct_reorg::pc_candidate_tree_p (tree xhs)
return false;
}
+srtype *
+ipa_struct_reorg::get_semi_relayout_candidate_type (tree xhs)
+{
+ if (xhs == NULL)
+ return NULL;
+ if (TREE_CODE (xhs) == SSA_NAME || TREE_CODE (xhs) == COMPONENT_REF)
+ {
+ srtype *access_type = find_type (inner_type (TREE_TYPE (xhs)));
+ if (access_type != NULL && access_type->semi_relayout)
+ return access_type;
+ }
+ return NULL;
+}
+
+bool
+ipa_struct_reorg::is_semi_relayout_candidate (tree xhs)
+{
+ if (xhs == NULL)
+ return false;
+
+ if (TREE_CODE (xhs) == SSA_NAME)
+ xhs = TREE_TYPE (xhs);
+
+ if (TREE_CODE (xhs) == POINTER_TYPE)
+ {
+ srtype *var_type = find_type (TREE_TYPE (xhs));
+ if (!var_type || var_type->has_escaped ())
+ return false;
+ if (var_type->semi_relayout)
+ return true;
+ }
+
+ if (TREE_CODE (xhs) == COMPONENT_REF)
+ {
+ tree mem = TREE_OPERAND (xhs, 0);
+ if (TREE_CODE (mem) == MEM_REF)
+ {
+ tree type = TREE_TYPE (mem);
+ srtype *old_type = get_relayout_candidate_type (type);
+ if (!old_type)
+ return false;
+ if (types_compatible_p (type, old_type->type)
+ && old_type->semi_relayout)
+ return true;
+ }
+ }
+ return false;
+}
+
/* True if xhs is a component_ref that base has escaped but uses a compression
candidate type. */
@@ -6388,7 +6560,7 @@ ipa_struct_reorg::decompress_candidate_without_check (gimple_stmt_iterator *gsi,
}
}
/* -> _1 = t->s
- _2 = _1->s
+ _2 = _1->s
In this case, _1 might not be nullptr, so decompress it without
check. */
else if (TREE_CODE (next_rhs) == COMPONENT_REF)
@@ -6582,6 +6754,426 @@ ipa_struct_reorg::try_rewrite_with_pointer_compression (gassign *stmt,
}
}
+tree
+ipa_struct_reorg::rewrite_pointer_diff (gimple_stmt_iterator *gsi, tree ptr1,
+ tree ptr2, srtype *type)
+{
+ tree shifts = build_int_cst (long_integer_type_node, semi_relayout_align);
+ tree pointer_type = build_pointer_type (unsigned_char_type_node);
+ // tree pointer_type = build_pointer_type (long_integer_type_node);
+ tree intptr_type = signed_type_for (pointer_type);
+
+ /* addr_high_1 = (intptr_t)ptr1 >> shifts */
+ tree ptr1_cvt = fold_convert (intptr_type, ptr1);
+ tree addr_high_1 = gimplify_build2 (gsi, RSHIFT_EXPR, intptr_type,
+ ptr1_cvt, shifts);
+ /* addr_high_2 = (intptr_t)ptr2 >> shifts */
+ tree ptr2_cvt = fold_convert (intptr_type, ptr2);
+ tree addr_high_2 = gimplify_build2 (gsi, RSHIFT_EXPR, intptr_type,
+ ptr2_cvt, shifts);
+ /* off1 = (intptr_t)ptr1 - (addr_high_1 << shifts) */
+ tree bucket_start_1 = gimplify_build2 (gsi, LSHIFT_EXPR, intptr_type,
+ addr_high_1, shifts);
+ tree off1 = gimplify_build2 (gsi, MINUS_EXPR, intptr_type,
+ ptr1_cvt, bucket_start_1);
+ /* off2 = (intptr_t)ptr2 - (addr_high_2 << shifts) */
+ tree bucket_start_2 = gimplify_build2 (gsi, LSHIFT_EXPR, intptr_type,
+ addr_high_2, shifts);
+ tree off2 = gimplify_build2 (gsi, MINUS_EXPR, intptr_type,
+ ptr2_cvt, bucket_start_2);
+ /* group_diff = (addr_high_1 - addr_high_2) / bucket_parts */
+ tree bucket_sub = gimplify_build2 (gsi, MINUS_EXPR, intptr_type,
+ addr_high_1, addr_high_2);
+ tree bucket_parts = build_int_cst (intptr_type,
+ type->bucket_parts);
+ tree group_diff = gimplify_build2 (gsi, TRUNC_DIV_EXPR,
+ intptr_type,
+ bucket_sub, bucket_parts);
+ /* off_addr_diff = off1 - off2 */
+ tree off_addr_diff = gimplify_build2 (gsi, MINUS_EXPR, intptr_type,
+ off1, off2);
+ /* res = group_diff * bucket_capacity + off_diff / 8 */
+ tree capacity = build_int_cst (long_integer_type_node,
+ relayout_part_size / 8);
+ tree unit_size = build_int_cst (long_integer_type_node, 8);
+ tree bucket_index_diff = gimplify_build2 (gsi, MULT_EXPR,
+ intptr_type,
+ group_diff, capacity);
+ tree off_index = gimplify_build2 (gsi, TRUNC_DIV_EXPR,
+ long_integer_type_node,
+ off_addr_diff, unit_size);
+ tree res = gimplify_build2 (gsi, PLUS_EXPR, intptr_type,
+ bucket_index_diff, off_index);
+ return res;
+}
+
+basic_block
+create_bb_for_group_diff_eq_0 (basic_block last_bb, tree phi, tree new_granule)
+{
+ basic_block new_bb = create_empty_bb (last_bb);
+ if (last_bb->loop_father != NULL)
+ {
+ add_bb_to_loop (new_bb, last_bb->loop_father);
+ loops_state_set (LOOPS_NEED_FIXUP);
+ }
+ /* Emit res = new_granule; */
+ gimple_stmt_iterator gsi = gsi_last_bb (new_bb);
+ gimple *new_stmt = gimple_build_assign (phi, new_granule);
+ gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
+ return new_bb;
+}
+
+basic_block
+create_bb_for_group_diff_ne_0 (basic_block new_bb, tree &phi, tree ptr,
+ tree group_diff, tree off_times_8, srtype *type)
+{
+ tree intptr_type = signed_type_for (long_unsigned_type_node);
+ tree shifts = build_int_cst (intptr_type, semi_relayout_align);
+ gimple_stmt_iterator gsi = gsi_last_bb (new_bb);
+ gsi_insert_after (&gsi, gimple_build_nop (), GSI_NEW_STMT);
+ tree ptr_cvt = fold_convert (intptr_type, ptr);
+ /* curr_group_start = (ptr >> shifts) << shifts; */
+ tree ptr_r_1 = gimplify_build2 (&gsi, RSHIFT_EXPR, intptr_type,
+ ptr_cvt, shifts);
+ tree curr_group_start = gimplify_build2 (&gsi, LSHIFT_EXPR, intptr_type,
+ ptr_r_1, shifts);
+ /* curr_off_from_group = ptr - curr_group_start; */
+ tree curr_off_from_group = gimplify_build2 (&gsi, MINUS_EXPR,
+ intptr_type,
+ ptr_cvt, curr_group_start);
+ /* res = curr_group_start + ((group_diff * parts) << shifts)
+ + ((curr_off_from_group + off_times_8) % shifts); */
+ tree step1 = gimplify_build2 (&gsi, MULT_EXPR, long_integer_type_node,
+ group_diff, build_int_cst (
+ long_integer_type_node, type->bucket_parts));
+ tree step1_cvt = fold_convert (intptr_type, step1);
+ tree step2 = gimplify_build2 (&gsi, LSHIFT_EXPR, intptr_type,
+ step1_cvt, shifts);
+ tree off_times_8_cvt = fold_convert (intptr_type, off_times_8);
+ tree step3 = gimplify_build2 (&gsi, PLUS_EXPR, intptr_type,
+ curr_off_from_group, off_times_8_cvt);
+ tree step4 = gimplify_build2 (&gsi, TRUNC_MOD_EXPR, intptr_type,
+ step3, build_int_cst (intptr_type,
+ relayout_part_size));
+ tree step5 = gimplify_build2 (&gsi, PLUS_EXPR, intptr_type,
+ step2, step4);
+ tree res_phi1 = gimplify_build2 (&gsi, PLUS_EXPR, long_integer_type_node,
+ curr_group_start, step5);
+ /* if (group_diff < 0) */
+ gcond *cond = gimple_build_cond (LT_EXPR, group_diff,
+ build_int_cst (long_integer_type_node, 0),
+ NULL_TREE, NULL_TREE);
+ gsi_insert_before (&gsi, cond, GSI_SAME_STMT);
+ /* remove nop */
+ gsi_remove (&gsi, true);
+ /* res += shifts */
+ basic_block true_bb = create_empty_bb (new_bb);
+ if (new_bb->loop_father != NULL)
+ {
+ add_bb_to_loop (true_bb, new_bb->loop_father);
+ loops_state_set (LOOPS_NEED_FIXUP);
+ }
+ gimple_stmt_iterator true_gsi = gsi_last_bb (true_bb);
+ tree res_phi2 = make_ssa_name (long_integer_type_node);
+ gimple *new_stmt
+ = gimple_build_assign (res_phi2, PLUS_EXPR, res_phi1,
+ build_int_cst (long_integer_type_node,
+ relayout_part_size));
+ gsi_insert_after (&true_gsi, new_stmt, GSI_NEW_STMT);
+ /* create phi bb */
+ basic_block res_bb = create_empty_bb (true_bb);
+ if (new_bb->loop_father != NULL)
+ {
+ add_bb_to_loop (res_bb, new_bb->loop_father);
+ loops_state_set (LOOPS_NEED_FIXUP);
+ }
+ /* rebuild cfg */
+ edge etrue = make_edge (new_bb, true_bb, EDGE_TRUE_VALUE);
+ etrue->probability = profile_probability::unlikely ();
+ true_bb->count = etrue->count ();
+
+ edge efalse = make_edge (new_bb, res_bb, EDGE_FALSE_VALUE);
+ efalse->probability = profile_probability::likely ();
+ res_bb->count = efalse->count ();
+
+ edge efall = make_single_succ_edge (true_bb, res_bb, EDGE_FALLTHRU);
+
+ phi = make_ssa_name (long_integer_type_node);
+ gphi *phi_node = create_phi_node (phi, res_bb);
+ add_phi_arg (phi_node, res_phi2, efall, UNKNOWN_LOCATION);
+ add_phi_arg (phi_node, res_phi1, efalse, UNKNOWN_LOCATION);
+
+ if (dom_info_available_p (CDI_DOMINATORS))
+ {
+ set_immediate_dominator (CDI_DOMINATORS, true_bb, new_bb);
+ set_immediate_dominator (CDI_DOMINATORS, res_bb, new_bb);
+ }
+ return res_bb;
+}
+
+tree
+ipa_struct_reorg::rewrite_pointer_plus_integer (gimple *stmt,
+ gimple_stmt_iterator *gsi,
+ tree ptr, tree offset,
+ srtype *type)
+{
+ gcc_assert (type->semi_relayout);
+ tree off = fold_convert (long_integer_type_node, offset);
+ tree num_8 = build_int_cst (long_integer_type_node, 8);
+ tree shifts = build_int_cst (long_integer_type_node, semi_relayout_align);
+ // tree shifts = build_int_cst (integer_type_node, semi_relayout_align);
+ /* off_times_8 = off * 8; */
+ tree off_times_8 = gimplify_build2 (gsi, MULT_EXPR, long_integer_type_node,
+ off, num_8);
+ /* new_granule = ptr + off * 8; */
+ tree ptr_int = fold_convert (long_integer_type_node, ptr);
+ tree new_granule = gimplify_build2 (gsi, PLUS_EXPR, long_integer_type_node,
+ ptr_int, off_times_8);
+ /* group_diff = (new_granule >> shifts) - (ptr >> shifts); */
+ tree group_diff_rhs_1 = gimplify_build2 (gsi, RSHIFT_EXPR,
+ long_integer_type_node,
+ new_granule, shifts);
+ tree group_diff_rhs_2 = gimplify_build2 (gsi, RSHIFT_EXPR,
+ long_integer_type_node,
+ ptr_int, shifts);
+ tree group_diff = gimplify_build2 (gsi, MINUS_EXPR, long_integer_type_node,
+ group_diff_rhs_1, group_diff_rhs_2);
+ /* if (group_diff == 0) */
+ gcond *cond = gimple_build_cond (EQ_EXPR, group_diff,
+ build_int_cst (long_integer_type_node, 0),
+ NULL_TREE, NULL_TREE);
+ gimple_set_location (cond, UNKNOWN_LOCATION);
+ gsi_insert_before (gsi, cond, GSI_SAME_STMT);
+
+ edge e = split_block (cond->bb, cond);
+ basic_block split_src_bb = e->src;
+ basic_block split_dst_bb = e->dest;
+ remove_edge_raw (e);
+ /* if (group_diff == 0)
+ res = new_granule; */
+ tree res_phi_1 = make_ssa_name (long_integer_type_node);
+ basic_block true_bb = create_bb_for_group_diff_eq_0 (split_src_bb, res_phi_1,
+ new_granule);
+ /* else */
+ tree res_phi_2 = NULL_TREE;
+ basic_block false_bb = create_empty_bb (split_src_bb);
+ if (split_src_bb->loop_father != NULL)
+ {
+ add_bb_to_loop (false_bb, split_src_bb->loop_father);
+ loops_state_set (LOOPS_NEED_FIXUP);
+ }
+
+ edge etrue = make_edge (split_src_bb, true_bb, EDGE_TRUE_VALUE);
+ etrue->probability = profile_probability::very_likely ();
+ true_bb->count = etrue->count ();
+
+ edge efalse = make_edge (split_src_bb, false_bb, EDGE_FALSE_VALUE);
+ efalse->probability = profile_probability::unlikely ();
+ false_bb->count = efalse->count ();
+ basic_block res_bb = create_bb_for_group_diff_ne_0 (false_bb, res_phi_2,
+ ptr_int, group_diff,
+ off_times_8, type);
+ /* rebuild cfg */
+ edge e_true_fall = make_single_succ_edge (true_bb, split_dst_bb,
+ EDGE_FALLTHRU);
+ edge e_false_fall = make_single_succ_edge (res_bb, split_dst_bb,
+ EDGE_FALLTHRU);
+ tree res_int = make_ssa_name (long_integer_type_node);
+ gphi *phi_node = create_phi_node (res_int, split_dst_bb);
+ add_phi_arg (phi_node, res_phi_1, e_true_fall, UNKNOWN_LOCATION);
+ add_phi_arg (phi_node, res_phi_2, e_false_fall, UNKNOWN_LOCATION);
+ if (dom_info_available_p (CDI_DOMINATORS))
+ {
+ set_immediate_dominator (CDI_DOMINATORS, split_dst_bb, split_src_bb);
+ set_immediate_dominator (CDI_DOMINATORS, true_bb, split_src_bb);
+ set_immediate_dominator (CDI_DOMINATORS, false_bb, split_src_bb);
+ }
+ *gsi = gsi_start_bb (split_dst_bb);
+ tree pointer_type = build_pointer_type (unsigned_char_type_node);
+ tree res = gimplify_build1 (gsi, NOP_EXPR, pointer_type, res_int);
+ return res;
+}
+
+tree
+ipa_struct_reorg::build_div_expr (gimple_stmt_iterator *gsi,
+ tree expr, tree orig_size)
+{
+ tree div_expr = build2 (TRUNC_DIV_EXPR, long_unsigned_type_node,
+ expr, orig_size);
+ tree num = make_ssa_name (long_unsigned_type_node);
+ gimple *g = gimple_build_assign (num, div_expr);
+ gsi_insert_before (gsi, g, GSI_SAME_STMT);
+ return num;
+}
+
+srtype *
+ipa_struct_reorg::get_relayout_candidate_type (tree type)
+{
+ if (type == NULL)
+ return NULL;
+ if (TREE_CODE (type) != RECORD_TYPE)
+ return NULL;
+ return find_type (inner_type (type));
+}
+
+long unsigned int
+ipa_struct_reorg::get_true_field_offset (srfield *field, srtype *type)
+{
+ unsigned HOST_WIDE_INT new_offset;
+ new_offset = *(type->new_field_offsets.get (field->newfield[0]));
+ return new_offset;
+}
+
+tree
+ipa_struct_reorg::get_true_pointer_base (gimple_stmt_iterator *gsi,
+ tree mem_ref, srtype *type)
+{
+ tree ptr = TREE_OPERAND (mem_ref, 0);
+ tree off_bytes = TREE_OPERAND (mem_ref, 1);
+ unsigned num = tree_to_shwi (off_bytes);
+ if (num == 0)
+ return ptr;
+ tree orig_size = TYPE_SIZE_UNIT (TREE_TYPE (mem_ref));
+ tree off = build_int_cst (long_integer_type_node,
+ num / tree_to_uhwi (orig_size));
+ gimple *stmt = gsi_stmt (*gsi);
+ tree new_pointer_base = rewrite_pointer_plus_integer (stmt, gsi, ptr,
+ off, type);
+ return new_pointer_base;
+}
+
+tree
+ipa_struct_reorg::rewrite_address (tree pointer_base, srfield *field,
+ srtype *type, gimple_stmt_iterator *gsi)
+{
+ unsigned HOST_WIDE_INT field_offset = get_true_field_offset (field, type);
+
+ tree pointer_ssa = fold_convert (long_unsigned_type_node, pointer_base);
+ tree step1 = gimplify_build1 (gsi, NOP_EXPR, long_unsigned_type_node,
+ pointer_ssa);
+ tree new_offset_ssa = build_int_cst (long_unsigned_type_node, field_offset);
+ tree step2 = gimplify_build2 (gsi, PLUS_EXPR, long_unsigned_type_node, step1,
+ new_offset_ssa);
+ tree field_ssa = fold_convert (
+ build_pointer_type (TREE_TYPE (field->newfield[0])), step2);
+ tree step3 = gimplify_build1 (gsi, NOP_EXPR,
+ TREE_TYPE (field_ssa), field_ssa);
+
+ tree new_mem_ref = fold_build2 (MEM_REF, TREE_TYPE (field->newfield[0]),
+ step3, build_int_cst (
+ TREE_TYPE (field_ssa), 0));
+ return new_mem_ref;
+}
+
+bool
+ipa_struct_reorg::check_sr_copy (gimple *stmt)
+{
+ tree lhs = gimple_assign_lhs (stmt);
+ tree rhs = gimple_assign_rhs1 (stmt);
+
+ if (TREE_CODE (lhs) != MEM_REF || TREE_CODE (rhs) != MEM_REF)
+ return false;
+ srtype *t1 = get_relayout_candidate_type (TREE_TYPE (lhs));
+ srtype *t2 = get_relayout_candidate_type (TREE_TYPE (rhs));
+ if (!t1 || !t2 || !t1->semi_relayout || !t2->semi_relayout || t1 != t2)
+ return false;
+ tree pointer1 = TREE_OPERAND (lhs, 0);
+ tree pointer2 = TREE_OPERAND (rhs, 0);
+ if (TREE_CODE (TREE_TYPE (pointer1)) != POINTER_TYPE
+ || TREE_CODE (TREE_TYPE (pointer2)) != POINTER_TYPE)
+ return false;
+
+ tree type1 = TREE_TYPE (TREE_TYPE (pointer1));
+ tree type2 = TREE_TYPE (TREE_TYPE (pointer2));
+
+ srtype *t3 = get_relayout_candidate_type (type1);
+ srtype *t4 = get_relayout_candidate_type (type2);
+
+ if (t3 != t4 || t3 != t1)
+ return false;
+
+ return true;
+}
+
+void
+ipa_struct_reorg::relayout_field_copy (gimple_stmt_iterator *gsi,
+ gimple *stmt ATTRIBUTE_UNUSED,
+ tree lhs, tree rhs ATTRIBUTE_UNUSED,
+ tree &newlhs, tree &newrhs)
+{
+ srtype *type = get_relayout_candidate_type (TREE_TYPE (lhs));
+ tree lhs_base_pointer = get_true_pointer_base (gsi, newlhs, type);
+ tree rhs_base_pointer = get_true_pointer_base (gsi, newrhs, type);
+ tree new_l_mem_ref = NULL_TREE;
+ tree new_r_mem_ref = NULL_TREE;
+ srfield *field = NULL;
+ unsigned i = 0;
+ FOR_EACH_VEC_ELT (type->fields, i, field)
+ {
+ if (!field->newfield[0])
+ continue;
+ new_l_mem_ref = rewrite_address (lhs_base_pointer, field, type, gsi);
+ new_r_mem_ref = rewrite_address (rhs_base_pointer, field, type, gsi);
+ if (!is_gimple_reg (new_l_mem_ref))
+ {
+ tree tmp_reg = create_tmp_reg (TREE_TYPE(new_l_mem_ref));
+ gimple *copy_stmt = gimple_build_assign (tmp_reg, new_r_mem_ref);
+ gsi_insert_before (gsi, copy_stmt, GSI_SAME_STMT);
+ new_r_mem_ref = tmp_reg;
+ }
+ gimple *new_stmt = gimple_build_assign (new_l_mem_ref, new_r_mem_ref);
+ gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
+ }
+ gcc_assert (new_l_mem_ref != NULL_TREE && new_r_mem_ref != NULL_TREE);
+ newlhs = new_l_mem_ref;
+ newrhs = new_r_mem_ref;
+}
+
+bool
+ipa_struct_reorg::do_semi_relayout (gimple_stmt_iterator *gsi, gimple *stmt,
+ tree &newlhs, tree &newrhs)
+{
+ tree lhs = gimple_assign_lhs (stmt);
+ tree rhs = gimple_assign_rhs1 (stmt);
+
+ bool l = TREE_CODE (lhs) == COMPONENT_REF ? is_semi_relayout_candidate (lhs)
+ : false;
+ bool r = TREE_CODE (rhs) == COMPONENT_REF ? is_semi_relayout_candidate (rhs)
+ : false;
+
+ gcc_assert (!(l && r));
+
+ if (!l && !r)
+ {
+ if (check_sr_copy (stmt))
+ {
+ relayout_field_copy (gsi, stmt, lhs, rhs, newlhs, newrhs);
+ return true;
+ }
+ }
+ else if (l)
+ {
+ srtype *type = get_relayout_candidate_type (
+ TREE_TYPE (TREE_OPERAND (lhs, 0)));
+ srfield *new_field = type->find_field (
+ int_byte_position (TREE_OPERAND (lhs, 1)));
+ tree pointer_base = get_true_pointer_base (
+ gsi, TREE_OPERAND (newlhs, 0), type);
+ newlhs = rewrite_address (pointer_base, new_field, type, gsi);
+ }
+ else if (r)
+ {
+ srtype *type = get_relayout_candidate_type (
+ TREE_TYPE (TREE_OPERAND (rhs, 0)));
+ srfield *new_field = type->find_field (
+ int_byte_position (TREE_OPERAND (rhs, 1)));
+ tree pointer_base = get_true_pointer_base (
+ gsi, TREE_OPERAND (newrhs, 0), type);
+ newrhs = rewrite_address (pointer_base, new_field, type, gsi);
+ }
+ return false;
+}
+
bool
ipa_struct_reorg::rewrite_assign (gassign *stmt, gimple_stmt_iterator *gsi)
{
@@ -6677,7 +7269,8 @@ ipa_struct_reorg::rewrite_assign (gassign *stmt, gimple_stmt_iterator *gsi)
tree size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (lhs)));
tree num;
/* Check if rhs2 is a multiplication of the size of the type. */
- if (!is_result_of_mult (rhs2, &num, size))
+ if (!is_result_of_mult (rhs2, &num, size)
+ && !(current_layout_opt_level & SEMI_RELAYOUT))
internal_error (
"The rhs of pointer is not a multiplicate and it slips through");
@@ -6698,12 +7291,39 @@ ipa_struct_reorg::rewrite_assign (gassign *stmt, gimple_stmt_iterator *gsi)
tree newsize = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (newlhs[i])));
newsize = gimplify_build2 (gsi, MULT_EXPR, sizetype, num,
newsize);
+ if (current_layout_opt_level >= SEMI_RELAYOUT)
+ {
+ if (is_semi_relayout_candidate (lhs))
+ {
+ srtype *type = get_semi_relayout_candidate_type (lhs);
+ newrhs[i] = rewrite_pointer_plus_integer (stmt, gsi,
+ newrhs[i],
+ num, type);
+ newsize = build_int_cst (long_unsigned_type_node, 0);
+ }
+ }
new_stmt = gimple_build_assign (newlhs[i], POINTER_PLUS_EXPR,
newrhs[i], newsize);
}
else
- new_stmt = gimple_build_assign (newlhs[i], POINTER_PLUS_EXPR,
- newrhs[i], rhs2);
+ {
+ /* rhs2 is not a const integer */
+ if (current_layout_opt_level >= SEMI_RELAYOUT)
+ {
+ if (is_semi_relayout_candidate (lhs))
+ {
+ num = build_div_expr (gsi, rhs2,
+ build_int_cst (
+ long_unsigned_type_node, 1));
+ srtype *type = get_semi_relayout_candidate_type (lhs);
+ newrhs[i] = rewrite_pointer_plus_integer (stmt,
+ gsi, newrhs[i], num, type);
+ rhs2 = build_int_cst (long_unsigned_type_node, 0);
+ }
+ }
+ new_stmt = gimple_build_assign (newlhs[i], POINTER_PLUS_EXPR,
+ newrhs[i], rhs2);
+ }
gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
remove = true;
}
@@ -6744,13 +7364,34 @@ ipa_struct_reorg::rewrite_assign (gassign *stmt, gimple_stmt_iterator *gsi)
return false;
/* The two operands always have pointer/reference type. */
- for (unsigned i = 0; i < max_split && newrhs1[i] && newrhs2[i]; i++)
+ if (current_layout_opt_level >= SEMI_RELAYOUT
+ && (is_semi_relayout_candidate (rhs1) || is_semi_relayout_candidate (rhs2)))
{
- gimple_assign_set_rhs1 (stmt, newrhs1[i]);
- gimple_assign_set_rhs2 (stmt, newrhs2[i]);
- update_stmt (stmt);
+ for (unsigned i = 0; i < max_split && newrhs1[i] &&newrhs2[i]; i++)
+ {
+ srtype *type = get_semi_relayout_candidate_type (rhs1);
+ if (!type)
+ {
+ type = get_semi_relayout_candidate_type (rhs2);
+ }
+ gcc_assert (type != NULL);
+ tree res = rewrite_pointer_diff (gsi, newrhs1[i],
+ newrhs2[i], type);
+ gimple *g = gimple_build_assign (gimple_assign_lhs (stmt),
+ res);
+ gsi_insert_before (gsi, g, GSI_SAME_STMT);
+ }
+ remove = true;
+ }
+ else
+ {
+ for (unsigned i = 0; i < max_split && newrhs1[i] && newrhs2[i]; i++)
+ {
+ gimple_assign_set_rhs1 (stmt, newrhs1[i]);
+ gimple_assign_set_rhs2 (stmt, newrhs2[i]);
+ update_stmt (stmt);
+ }
}
- remove = false;
return remove;
}
@@ -6777,18 +7418,24 @@ ipa_struct_reorg::rewrite_assign (gassign *stmt, gimple_stmt_iterator *gsi)
fprintf (dump_file, "\nreplaced with:\n");
for (unsigned i = 0; i < max_split && (newlhs[i] || newrhs[i]); i++)
{
+ bool fields_copied = false;
+ if (current_layout_opt_level & SEMI_RELAYOUT)
+ fields_copied = do_semi_relayout (gsi, stmt, newlhs[i], newrhs[i]);
if (current_layout_opt_level >= POINTER_COMPRESSION_SAFE)
try_rewrite_with_pointer_compression (stmt, gsi, lhs, rhs,
newlhs[i], newrhs[i]);
- gimple *newstmt = gimple_build_assign (newlhs[i] ? newlhs[i] : lhs,
- newrhs[i] ? newrhs[i] : rhs);
+ remove = true;
+ if (fields_copied)
+ continue;
+ tree lhs_expr = newlhs[i] ? newlhs[i] : lhs;
+ tree rhs_expr = newrhs[i] ? newrhs[i] : rhs;
+ gimple *newstmt = gimple_build_assign (lhs_expr, rhs_expr);
if (dump_file && (dump_flags & TDF_DETAILS))
{
print_gimple_stmt (dump_file, newstmt, 0);
fprintf (dump_file, "\n");
}
gsi_insert_before (gsi, newstmt, GSI_SAME_STMT);
- remove = true;
}
return remove;
}
@@ -6796,6 +7443,110 @@ ipa_struct_reorg::rewrite_assign (gassign *stmt, gimple_stmt_iterator *gsi)
return remove;
}
+tree
+ipa_struct_reorg::get_real_allocated_ptr (tree ptr, gimple_stmt_iterator *gsi)
+{
+ tree ptr_to_int = fold_convert (long_unsigned_type_node, ptr);
+ tree align = build_int_cst (long_unsigned_type_node, relayout_part_size);
+ tree real_addr = gimplify_build2 (gsi, MINUS_EXPR, long_unsigned_type_node,
+ ptr_to_int, align);
+ tree res = gimplify_build1 (gsi, NOP_EXPR,
+ build_pointer_type (long_unsigned_type_node),
+ real_addr);
+ return res;
+}
+
+tree
+ipa_struct_reorg::set_ptr_for_use (tree ptr, gimple_stmt_iterator *gsi)
+{
+ tree ptr_to_int = fold_convert (long_unsigned_type_node, ptr);
+ tree align = build_int_cst (long_unsigned_type_node, relayout_part_size);
+ tree ptr_int = gimplify_build2 (gsi, PLUS_EXPR, long_unsigned_type_node,
+ ptr_to_int, align);
+ tree res = gimplify_build1 (gsi, NOP_EXPR,
+ build_pointer_type (long_unsigned_type_node),
+ ptr_int);
+ return res;
+}
+
+void
+ipa_struct_reorg::record_allocated_size (tree ptr, gimple_stmt_iterator *gsi,
+ tree size)
+{
+ tree lhs = fold_build2 (MEM_REF, long_unsigned_type_node, ptr,
+ build_int_cst (build_pointer_type (
+ long_unsigned_type_node), 0));
+ gimple *stmt = gimple_build_assign (lhs, size);
+ gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
+}
+
+tree
+ipa_struct_reorg::read_allocated_size (tree ptr, gimple_stmt_iterator *gsi)
+{
+ tree to_type = build_pointer_type (long_unsigned_type_node);
+ tree off = build_int_cst (to_type, 0);
+ tree size = gimplify_build2 (gsi, MEM_REF, long_unsigned_type_node,
+ ptr, off);
+ return size;
+}
+
+gimple *
+ipa_struct_reorg::create_aligned_alloc (gimple_stmt_iterator *gsi,
+ srtype *type, tree num, tree &size)
+{
+ tree fn = builtin_decl_implicit (BUILT_IN_ALIGNED_ALLOC);
+
+ tree align = build_int_cst (long_unsigned_type_node, relayout_part_size);
+ unsigned bucket_size = type->bucket_size;
+
+ tree nbuckets = gimplify_build2 (gsi, CEIL_DIV_EXPR, long_unsigned_type_node,
+ num, build_int_cst (long_unsigned_type_node,
+ relayout_part_size / 8));
+ tree use_size = gimplify_build2 (gsi, MULT_EXPR, long_unsigned_type_node,
+ nbuckets, build_int_cst (
+ long_unsigned_type_node, bucket_size));
+ size = gimplify_build2 (gsi, PLUS_EXPR, long_unsigned_type_node,
+ use_size, align);
+ gimple *g = gimple_build_call (fn, 2, align, size);
+ gsi_insert_before (gsi, g, GSI_SAME_STMT);
+ return g;
+}
+
+void
+ipa_struct_reorg::create_memset_zero (tree ptr, gimple_stmt_iterator *gsi,
+ tree size)
+{
+ tree fn = builtin_decl_implicit (BUILT_IN_MEMSET);
+ tree val = build_int_cst (long_unsigned_type_node, 0);
+ gimple *g = gimple_build_call (fn, 3, ptr, val, size);
+ gsi_insert_before (gsi, g, GSI_SAME_STMT);
+}
+
+void
+ipa_struct_reorg::create_memcpy (tree src, tree dst, tree size,
+ gimple_stmt_iterator *gsi)
+{
+ tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
+ gimple *g = gimple_build_call (fn, 3, dst, src, size);
+ gsi_insert_before (gsi, g, GSI_SAME_STMT);
+}
+
+void
+ipa_struct_reorg::create_free (tree ptr, gimple_stmt_iterator *gsi)
+{
+ tree fn = builtin_decl_implicit (BUILT_IN_FREE);
+ gimple *g = gimple_build_call (fn, 1, ptr);
+ gsi_insert_before (gsi, g, GSI_SAME_STMT);
+}
+
+void
+ipa_struct_reorg::copy_to_lhs (tree lhs, tree new_lhs,
+ gimple_stmt_iterator *gsi)
+{
+ gimple *g = gimple_build_assign (lhs, new_lhs);
+ gsi_insert_before (gsi, g, GSI_SAME_STMT);
+}
+
/* Rewrite function call statement STMT. Return TRUE if the statement
is to be removed. */
@@ -6837,25 +7588,74 @@ ipa_struct_reorg::rewrite_call (gcall *stmt, gimple_stmt_iterator *gsi)
? TYPE_SIZE_UNIT (decl->orig_type)
: TYPE_SIZE_UNIT (type->newtype[i]);
gimple *g;
- /* Every allocation except for calloc needs
- the size multiplied out. */
- if (!gimple_call_builtin_p (stmt, BUILT_IN_CALLOC))
- newsize = gimplify_build2 (gsi, MULT_EXPR, sizetype, num, newsize);
-
- if (gimple_call_builtin_p (stmt, BUILT_IN_MALLOC)
- || gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA))
- g = gimple_build_call (gimple_call_fndecl (stmt),
- 1, newsize);
- else if (gimple_call_builtin_p (stmt, BUILT_IN_CALLOC))
- g = gimple_build_call (gimple_call_fndecl (stmt),
- 2, num, newsize);
- else if (gimple_call_builtin_p (stmt, BUILT_IN_REALLOC))
- g = gimple_build_call (gimple_call_fndecl (stmt),
- 2, newrhs1[i], newsize);
- else
- gcc_assert (false);
- gimple_call_set_lhs (g, decl->newdecl[i]);
- gsi_insert_before (gsi, g, GSI_SAME_STMT);
+ bool rewrite = false;
+ if (current_layout_opt_level >= SEMI_RELAYOUT
+ && type->semi_relayout)
+ {
+ if (gimple_call_builtin_p (stmt, BUILT_IN_MALLOC))
+ ;
+ else if (gimple_call_builtin_p (stmt, BUILT_IN_CALLOC))
+ {
+ tree rhs2 = gimple_call_arg (stmt, 1);
+ if (tree_to_uhwi (rhs2) == tree_to_uhwi (TYPE_SIZE_UNIT (type->type)))
+ {
+ rewrite = true;
+ tree size = NULL_TREE;
+ g = create_aligned_alloc (gsi, type, num, size);
+ tree real_ptr = make_ssa_name (build_pointer_type (unsigned_char_type_node));
+ gimple_set_lhs (g, real_ptr);
+ create_memset_zero (real_ptr, gsi, size);
+ record_allocated_size (real_ptr, gsi, size);
+ tree lhs_use = set_ptr_for_use (real_ptr, gsi);
+ copy_to_lhs (decl->newdecl[i], lhs_use, gsi);
+ }
+ }
+ else if (gimple_call_builtin_p (stmt, BUILT_IN_REALLOC))
+ {
+ rewrite = true;
+ tree size = NULL_TREE;
+ g = create_aligned_alloc (gsi, type, num, size);
+ tree real_ptr = make_ssa_name (build_pointer_type (unsigned_char_type_node));
+ gimple_set_lhs (g, real_ptr);
+ create_memset_zero (real_ptr, gsi, size);
+ tree src = get_real_allocated_ptr (newrhs1[i], gsi);
+ tree old_size = read_allocated_size (src, gsi);
+ create_memcpy (src, real_ptr, old_size, gsi);
+ record_allocated_size (real_ptr, gsi, size);
+ tree lhs_use = set_ptr_for_use (real_ptr, gsi);
+ create_free (src, gsi);
+ copy_to_lhs (decl->newdecl[i], lhs_use, gsi);
+ }
+ else
+ {
+ gcc_assert (false);
+ internal_error ("supported type for semi-relayout.");
+ }
+ }
+ if (!rewrite
+ && (current_layout_opt_level >= STRUCT_REORDER_FIELDS
+ || current_layout_opt_level == STRUCT_SPLIT))
+ {
+ /* Every allocation except for calloc needs the size multiplied out. */
+ if (!gimple_call_builtin_p (stmt, BUILT_IN_CALLOC))
+ newsize = gimplify_build2 (gsi, MULT_EXPR, sizetype,
+ num, newsize);
+ if (gimple_call_builtin_p (stmt, BUILT_IN_MALLOC)
+ || gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA))
+ g = gimple_build_call (gimple_call_fndecl (stmt), 1, newsize);
+ else if (gimple_call_builtin_p (stmt, BUILT_IN_CALLOC))
+ g = gimple_build_call (gimple_call_fndecl (stmt), 2,
+ num, newsize);
+ else if (gimple_call_builtin_p (stmt, BUILT_IN_REALLOC))
+ g = gimple_build_call (gimple_call_fndecl (stmt), 2,
+ newrhs1[i], newsize);
+ else
+ gcc_assert (false);
+ gimple_call_set_lhs (g, decl->newdecl[i]);
+ gsi_insert_before (gsi, g, GSI_SAME_STMT);
+ }
+
+
if (type->pc_candidate)
{
/* Init global header for pointer compression. */
@@ -6875,11 +7675,14 @@ ipa_struct_reorg::rewrite_call (gcall *stmt, gimple_stmt_iterator *gsi)
if (!rewrite_expr (expr, newexpr))
return false;
+ srtype *t = find_type (TREE_TYPE (TREE_TYPE (expr)));
if (newexpr[1] == NULL)
{
- gimple_call_set_arg (stmt, 0, newexpr[0]);
- update_stmt (stmt);
- return false;
+ if (t && t->semi_relayout)
+ newexpr[0] = get_real_allocated_ptr (newexpr[0], gsi);
+ gimple_call_set_arg (stmt, 0, newexpr[0]);
+ update_stmt (stmt);
+ return false;
}
for (unsigned i = 0; i < max_split && newexpr[i]; i++)
@@ -7571,6 +8374,86 @@ ipa_struct_reorg::check_and_prune_struct_for_pointer_compression (void)
}
}
+void
+ipa_struct_reorg::check_and_prune_struct_for_semi_relayout (void)
+{
+ unsigned relayout_transform = 0;
+ for (unsigned i = 0; i < types.length (); i++)
+ {
+ srtype *type = types[i];
+ if (dump_file)
+ {
+ print_generic_expr (dump_file, type->type);
+ }
+ if (type->has_escaped ())
+ {
+ if (dump_file)
+ {
+ fprintf (dump_file, " has escaped by %s, "
+ "skip relayout.\n", type->escape_reason());
+ }
+ continue;
+ }
+ if (TYPE_FIELDS (type->type) == NULL)
+ {
+ if (dump_file)
+ {
+ fprintf (dump_file, " has zero field, skip relayout.\n");
+ }
+ continue;
+ }
+ if (type->chain_type)
+ {
+ if (dump_file)
+ {
+ fprintf (dump_file, " is chain_type, skip relayout.\n");
+ }
+ continue;
+ }
+ if (type->has_alloc_array == 0 || type->has_alloc_array == 1
+ || type->has_alloc_array == -1 || type->has_alloc_array == -3
+ || type->has_alloc_array == -4)
+ {
+ if (dump_file)
+ {
+ fprintf (dump_file, " has alloc number: %d,"
+ " skip relayout.\n", type->has_alloc_array);
+ }
+ continue;
+ }
+ if (get_type_name (type->type) == NULL)
+ {
+ if (dump_file)
+ {
+ fprintf (dump_file, " has empty struct name,"
+ " skip relayout.\n");
+ }
+ continue;
+ }
+ relayout_transform++;
+ type->semi_relayout = true;
+ if (dump_file)
+ {
+ fprintf (dump_file, " attempts to do semi-relayout.\n");
+ }
+ }
+
+ if (dump_file)
+ {
+ if (relayout_transform)
+ {
+ fprintf (dump_file, "\nNumber of structures to transform in "
+ "semi-relayout is %d\n", relayout_transform);
+ }
+ else
+ {
+ fprintf (dump_file, "\nNo structures to transform in "
+ "semi-relayout.\n");
+ }
+ }
+}
+
+
/* Init pointer size from parameter param_pointer_compression_size. */
static void
@@ -7612,6 +8495,8 @@ ipa_struct_reorg::execute (unsigned int opt)
if (opt >= POINTER_COMPRESSION_SAFE)
check_and_prune_struct_for_pointer_compression ();
+ if (opt >= SEMI_RELAYOUT)
+ check_and_prune_struct_for_semi_relayout ();
ret = rewrite_functions ();
}
else
@@ -7659,6 +8544,8 @@ public:
unsigned int level = 0;
switch (struct_layout_optimize_level)
{
+ case 6: level |= SEMI_RELAYOUT;
+ // FALLTHRU
case 5: level |= POINTER_COMPRESSION_UNSAFE;
// FALLTHRU
case 4: level |= POINTER_COMPRESSION_SAFE;
@@ -7678,6 +8565,12 @@ public:
if (level & POINTER_COMPRESSION_SAFE)
init_pointer_size_for_pointer_compression ();
+ if (level & SEMI_RELAYOUT)
+ {
+ semi_relayout_align = semi_relayout_level;
+ relayout_part_size = 1 << semi_relayout_level;
+ }
+
/* Preserved for backward compatibility, reorder fields needs run before
struct split and complete struct relayout. */
if (flag_ipa_reorder_fields && level < STRUCT_REORDER_FIELDS)
diff --git a/gcc/ipa-struct-reorg/ipa-struct-reorg.h b/gcc/ipa-struct-reorg/ipa-struct-reorg.h
index 6c4469597..e3e6d7afb 100644
--- a/gcc/ipa-struct-reorg/ipa-struct-reorg.h
+++ b/gcc/ipa-struct-reorg/ipa-struct-reorg.h
@@ -25,6 +25,9 @@ namespace struct_reorg {
const int max_split = 2;
+unsigned semi_relayout_align = semi_relayout_level;
+unsigned relayout_part_size = 1 << semi_relayout_level;
+
template <typename type>
struct auto_vec_del : auto_vec<type *>
{
@@ -128,6 +131,10 @@ public:
/* Negative number means it has illegal allocated arrays
that we do not optimize. */
int has_alloc_array;
+ bool semi_relayout;
+ hash_map<tree, unsigned long> new_field_offsets;
+ unsigned bucket_parts;
+ unsigned bucket_size;
// Constructors
srtype (tree type);
@@ -149,6 +156,7 @@ public:
bool has_dead_field (void);
void mark_escape (escape_type, gimple *stmt);
void create_global_ptr_for_pc ();
+ unsigned calculate_bucket_size ();
bool has_escaped (void)
{
return escapes != does_not_escape;
diff --git a/gcc/params.opt b/gcc/params.opt
index bb5d82471..82a3d92c5 100644
--- a/gcc/params.opt
+++ b/gcc/params.opt
@@ -1221,4 +1221,9 @@ Target size of compressed pointer, which should be 8, 16 or 32.
-param=param-ldp-dependency-search-range=
Common Joined UInteger Var(param_ldp_dependency_search_range) Init(16) IntegerRange(1, 32) Param Optimization
Range for depended ldp search in split-ldp-stp path.
+
+-param=semi-relayout-level=
+Common Joined UInteger Var(semi_relayout_level) Init(13) IntegerRange(11, 15) Param Optimization
+Set capacity of each bucket to semi-relayout to (1 << semi-relayout-level) / 8 .
+
; This comment is to ensure we retain the blank line above.
diff --git a/gcc/testsuite/gcc.dg/struct/semi_relayout_rewrite.c b/gcc/testsuite/gcc.dg/struct/semi_relayout_rewrite.c
new file mode 100644
index 000000000..aca8400ca
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/struct/semi_relayout_rewrite.c
@@ -0,0 +1,86 @@
+// Check simplify rewrite chance for semi-relayout
+/* { dg-do compile } */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+typedef struct node node_t;
+typedef struct node *node_p;
+
+typedef struct arc arc_t;
+typedef struct arc *arc_p;
+
+typedef struct network
+{
+ arc_p arcs;
+ arc_p sorted_arcs;
+ int x;
+ node_p nodes;
+ node_p stop_nodes;
+} network_t;
+
+struct node
+{
+ int64_t potential;
+ int orientation;
+ node_p child;
+ node_p pred;
+ node_p sibling;
+ node_p sibling_prev;
+ arc_p basic_arc;
+ arc_p firstout;
+ arc_p firstin;
+ arc_p arc_tmp;
+ int64_t flow;
+ int64_t depth;
+ int number;
+ int time;
+};
+
+struct arc
+{
+ int id;
+ int64_t cost;
+ node_p tail;
+ node_p head;
+ short ident;
+ arc_p nextout;
+ arc_p nextin;
+ int64_t flow;
+ int64_t org_cost;
+ network_t* net_add;
+};
+
+
+const int MAX = 100;
+network_t* net;
+node_p node;
+arc_p arc;
+
+int
+main ()
+{
+ net = (network_t*) calloc (1, sizeof(network_t));
+ net->arcs = (arc_p) calloc (MAX, sizeof (arc_t));
+ net->sorted_arcs = (arc_p) calloc (MAX, sizeof (arc_t));
+ net->nodes = (node_p) calloc (MAX, sizeof (node_t));
+ net->arcs->id = 100;
+
+ node = net->nodes;
+ arc = net->arcs;
+
+ for (unsigned i = 0; i < MAX; i++)
+ {
+ arc->head = node;
+ arc->head->child = node;
+ node->potential = i + 1;
+ arc->cost = arc->head->potential;
+ arc->tail = node->sibling;
+ node = node + 1;
+ arc = arc + 1;
+ }
+
+ return 0;
+}
+
+/* { dg-final { scan-ipa-dump "Number of structures to transform in semi-relayout is 1" "struct_reorg" } } */
\ No newline at end of file
diff --git a/gcc/testsuite/gcc.dg/struct/struct-reorg.exp b/gcc/testsuite/gcc.dg/struct/struct-reorg.exp
index c40474407..c5a955b00 100644
--- a/gcc/testsuite/gcc.dg/struct/struct-reorg.exp
+++ b/gcc/testsuite/gcc.dg/struct/struct-reorg.exp
@@ -55,6 +55,10 @@ gcc-dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/pc*.c]] \
gcc-dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/pc*.c]] \
"" "-fipa-struct-reorg=5 -fdump-ipa-all -flto-partition=one -fwhole-program"
+# -fipa-struct-reorg=6
+gcc-dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/semi_relayout*.c]] \
+ "" "-fipa-struct-reorg=6 -fdump-ipa-all -flto-partition=one -fwhole-program"
+
# All done.
torture-finish
dg-finish
--
2.33.0
|