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
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
|
/* RTL factoring (sequence abstraction).
Copyright (C) 2004, 2005, 2006 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 2, 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 COPYING. If not, write to the Free
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "rtl.h"
#include "obstack.h"
#include "basic-block.h"
#include "resource.h"
#include "flags.h"
#include "ggc.h"
#include "regs.h"
#include "params.h"
#include "expr.h"
#include "tm_p.h"
#include "tree-pass.h"
#include "tree-flow.h"
#include "timevar.h"
#include "output.h"
#include "addresses.h"
/* Sequence abstraction:
It is a size optimization method. The main idea of this technique is to
find identical sequences of code, which can be turned into procedures and
then replace all occurrences with calls to the newly created subroutine.
It is kind of an opposite of function inlining.
There are four major parts of this file:
sequence fingerprint
In order to avoid the comparison of every insn with every other, hash
value will be designed for every insn by COMPUTE_HASH.
These hash values are used for grouping the sequence candidates. So
we only need to compare every insn with every other in same hash group.
FILL_HASH_BUCKET creates all hash values and stores into HASH_BUCKETS.
The result is used by COLLECT_PATTERN_SEQS.
code matching
In code matching the algorithm compares every two possible sequence
candidates which last insns are in the same hash group. If these
sequences are identical they will be stored and do further searches for
finding more sequences which are identical with the first one.
COLLECT_PATTERN_SEQS does the code matching and stores the results into
PATTERN_SEQS.
gain computation
This part computes the gain of abstraction which could be archived when
turning the pattern sequence into a pseudo-function and its matching
sequences into pseudo-calls. After it the most effective sequences will
be marked for abstraction.
RECOMPUTE_GAIN does the gain computation. The sequences with the maximum
gain is on the top of PATTERN_SEQS.
abstract code
This part turns the pattern sequence into a pseudo-function and its
matching sequences into pseudo-calls.
ABSTRACT_BEST_SEQ does the code merging.
C code example:
// Original source // After sequence abstraction
{ {
void *jump_label;
... ...
jump_label = &&exit_0;
entry_0:
I0; I0;
I1; I1;
I2; I2;
I3; I3;
goto *jump_label;
exit_0:
... ...
jump_label = &&exit_1;
goto entry_0;
I0;
I1;
I2;
I3;
exit_1:
... ...
jump_label = &&exit_2;
goto entry_0;
I0;
I1;
I2;
I3;
exit_2:
... ...
jump_label = &&exit_3;
goto entry_0;
I0;
I1;
I2;
I3;
exit_3:
... ...
} }
TODO:
- Use REG_ALLOC_ORDER when choosing link register.
- Handle JUMP_INSNs. Also handle volatile function calls (handle them
similar to unconditional jumps.)
- Test command line option -fpic.
*/
/* Predicate yielding nonzero iff X is an abstractable insn. Non-jump insns are
abstractable. */
#define ABSTRACTABLE_INSN_P(X) (INSN_P (X) && !JUMP_P (X))
/* First parameter of the htab_create function call. */
#define HASH_INIT 1023
/* Multiplier for cost of sequence call to avoid abstracting short
sequences. */
#ifndef SEQ_CALL_COST_MULTIPLIER
#define SEQ_CALL_COST_MULTIPLIER 2
#endif
/* Recomputes the cost of MSEQ pattern/matching sequence. */
#define RECOMPUTE_COST(SEQ) \
{ \
int l; \
rtx x = SEQ->insn; \
SEQ->cost = 0; \
for (l = 0; l < SEQ->abstracted_length; l++) \
{ \
SEQ->cost += compute_rtx_cost (x); \
x = prev_insn_in_block (x); \
} \
}
/* A sequence matching a pattern sequence. */
typedef struct matching_seq_def
{
/* The last insn in the matching sequence. */
rtx insn;
/* Index of INSN instruction. */
unsigned long idx;
/* The number of insns matching in this sequence and the pattern sequence.
*/
int matching_length;
/* The number of insns selected to abstract from this sequence. Less than
or equal to MATCHING_LENGTH. */
int abstracted_length;
/* The cost of the sequence. */
int cost;
/* The next sequence in the chain matching the same pattern. */
struct matching_seq_def *next_matching_seq;
} *matching_seq;
/* A pattern instruction sequence. */
typedef struct pattern_seq_def
{
/* The last insn in the pattern sequence. */
rtx insn;
/* Index of INSN instruction. */
unsigned long idx;
/* The gain of transforming the pattern sequence into a pseudo-function and
the matching sequences into pseudo-calls. */
int gain;
/* The maximum of the ABSTRACTED_LENGTH of the matching sequences. */
int abstracted_length;
/* The cost of the sequence. */
int cost;
/* The register used to hold the return address during the pseudo-call. */
rtx link_reg;
/* The sequences matching this pattern. */
matching_seq matching_seqs;
/* The next pattern sequence in the chain. */
struct pattern_seq_def *next_pattern_seq;
} *pattern_seq;
/* A block of a pattern sequence. */
typedef struct seq_block_def
{
/* The number of insns in the block. */
int length;
/* The code_label of the block. */
rtx label;
/* The sequences entering the pattern sequence at LABEL. */
matching_seq matching_seqs;
/* The next block in the chain. The blocks are sorted by LENGTH in
ascending order. */
struct seq_block_def *next_seq_block;
} *seq_block;
/* Contains same sequence candidates for further searching. */
typedef struct hash_bucket_def
{
/* The hash value of the group. */
unsigned int hash;
/* List of sequence candidates. */
htab_t seq_candidates;
} *p_hash_bucket;
/* Contains the last insn of the sequence, and its index value. */
typedef struct hash_elem_def
{
/* Unique index; ordered by FILL_HASH_BUCKET. */
unsigned long idx;
/* The last insn in the sequence. */
rtx insn;
/* The cached length of the insn. */
int length;
} *p_hash_elem;
/* The list of same sequence candidates. */
static htab_t hash_buckets;
/* The pattern sequences collected from the current functions. */
static pattern_seq pattern_seqs;
/* The blocks of the current pattern sequence. */
static seq_block seq_blocks;
/* Cost of calling sequence. */
static int seq_call_cost;
/* Cost of jump. */
static int seq_jump_cost;
/* Cost of returning. */
static int seq_return_cost;
/* Returns the first insn preceding INSN for which INSN_P is true and belongs to
the same basic block. Returns NULL_RTX if no such insn can be found. */
static rtx
prev_insn_in_block (rtx insn)
{
basic_block bb = BLOCK_FOR_INSN (insn);
if (!bb)
return NULL_RTX;
while (insn != BB_HEAD (bb))
{
insn = PREV_INSN (insn);
if (INSN_P (insn))
return insn;
}
return NULL_RTX;
}
/* Returns the hash value of INSN. */
static unsigned int
compute_hash (rtx insn)
{
unsigned int hash = 0;
rtx prev;
hash = INSN_CODE (insn) * 100;
prev = prev_insn_in_block (insn);
if (prev)
hash += INSN_CODE (prev);
return hash;
}
/* Compute the cost of INSN rtx for abstraction. */
static int
compute_rtx_cost (rtx insn)
{
struct hash_bucket_def tmp_bucket;
p_hash_bucket bucket;
struct hash_elem_def tmp_elem;
p_hash_elem elem = NULL;
int cost = -1;
/* Compute hash value for INSN. */
tmp_bucket.hash = compute_hash (insn);
/* Select the hash group. */
bucket = htab_find (hash_buckets, &tmp_bucket);
if (bucket)
{
tmp_elem.insn = insn;
/* Select the insn. */
elem = htab_find (bucket->seq_candidates, &tmp_elem);
/* If INSN is parsed the cost will be the cached length. */
if (elem)
cost = elem->length;
}
/* If we can't parse the INSN cost will be the instruction length. */
if (cost == -1)
{
cost = get_attr_length (insn);
/* Cache the length. */
if (elem)
elem->length = cost;
}
/* If we can't get an accurate estimate for a complex instruction,
assume that it has the same cost as a single fast instruction. */
return cost != 0 ? cost : COSTS_N_INSNS (1);
}
/* Determines the number of common insns in the sequences ending in INSN1 and
INSN2. Returns with LEN number of common insns and COST cost of sequence.
*/
static void
matching_length (rtx insn1, rtx insn2, int* len, int* cost)
{
rtx x1;
rtx x2;
x1 = insn1;
x2 = insn2;
*len = 0;
*cost = 0;
while (x1 && x2 && (x1 != insn2) && (x2 != insn1)
&& rtx_equal_p (PATTERN (x1), PATTERN (x2)))
{
(*len)++;
(*cost) += compute_rtx_cost (x1);
x1 = prev_insn_in_block (x1);
x2 = prev_insn_in_block (x2);
}
}
/* Adds E0 as a pattern sequence to PATTERN_SEQS with E1 as a matching
sequence. */
static void
match_seqs (p_hash_elem e0, p_hash_elem e1)
{
int len;
int cost;
matching_seq mseq, p_prev, p_next;
/* Determines the cost of the sequence and return without doing anything
if it is too small to produce any gain. */
matching_length (e0->insn, e1->insn, &len, &cost);
if (cost <= seq_call_cost)
return;
/* Prepend a new PATTERN_SEQ to PATTERN_SEQS if the last pattern sequence
does not end in E0->INSN. This assumes that once the E0->INSN changes
the old value will never appear again. */
if (!pattern_seqs || pattern_seqs->insn != e0->insn)
{
pattern_seq pseq =
(pattern_seq) xmalloc (sizeof (struct pattern_seq_def));
pseq->insn = e0->insn;
pseq->idx = e0->idx;
pseq->gain = 0; /* Set to zero to force recomputing. */
pseq->abstracted_length = 0;
pseq->cost = 0;
pseq->link_reg = NULL_RTX;
pseq->matching_seqs = NULL;
pseq->next_pattern_seq = pattern_seqs;
pattern_seqs = pseq;
}
/* Find the position of E1 in the matching sequences list. */
p_prev = NULL;
p_next = pattern_seqs->matching_seqs;
while (p_next && p_next->idx < e1->idx)
{
p_prev = p_next;
p_next = p_next->next_matching_seq;
}
/* Add a new E1 matching sequence to the pattern sequence. We know that
it ends in E0->INSN. */
mseq = (matching_seq) xmalloc (sizeof (struct matching_seq_def));
mseq->insn = e1->insn;
mseq->idx = e1->idx;
mseq->matching_length = len;
mseq->abstracted_length = 0;
mseq->cost = cost;
if (p_prev == NULL)
pattern_seqs->matching_seqs = mseq;
else
p_prev->next_matching_seq = mseq;
mseq->next_matching_seq = p_next;
}
/* Collects all pattern sequences and their matching sequences and puts them
into PATTERN_SEQS. */
static void
collect_pattern_seqs (void)
{
htab_iterator hti0, hti1, hti2;
p_hash_bucket hash_bucket;
p_hash_elem e0, e1;
#ifdef STACK_REGS
basic_block bb;
bitmap_head stack_reg_live;
/* Extra initialization step to ensure that no stack registers (if present)
are live across abnormal edges. Set a flag in STACK_REG_LIVE for an insn
if a stack register is live after the insn. */
bitmap_initialize (&stack_reg_live, NULL);
FOR_EACH_BB (bb)
{
regset_head live;
struct propagate_block_info *pbi;
rtx insn;
/* Initialize liveness propagation. */
INIT_REG_SET (&live);
COPY_REG_SET (&live, bb->il.rtl->global_live_at_end);
pbi = init_propagate_block_info (bb, &live, NULL, NULL, 0);
/* Propagate liveness info and mark insns where a stack reg is live. */
insn = BB_END (bb);
while (1)
{
int reg;
for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; reg++)
{
if (REGNO_REG_SET_P (&live, reg))
{
bitmap_set_bit (&stack_reg_live, INSN_UID (insn));
break;
}
}
if (insn == BB_HEAD (bb))
break;
insn = propagate_one_insn (pbi, insn);
}
/* Free unused data. */
CLEAR_REG_SET (&live);
free_propagate_block_info (pbi);
}
#endif
/* Initialize PATTERN_SEQS to empty. */
pattern_seqs = 0;
/* Try to match every abstractable insn with every other insn in the same
HASH_BUCKET. */
FOR_EACH_HTAB_ELEMENT (hash_buckets, hash_bucket, p_hash_bucket, hti0)
if (htab_elements (hash_bucket->seq_candidates) > 1)
FOR_EACH_HTAB_ELEMENT (hash_bucket->seq_candidates, e0, p_hash_elem, hti1)
FOR_EACH_HTAB_ELEMENT (hash_bucket->seq_candidates, e1, p_hash_elem,
hti2)
if (e0 != e1
#ifdef STACK_REGS
&& !bitmap_bit_p (&stack_reg_live, INSN_UID (e0->insn))
&& !bitmap_bit_p (&stack_reg_live, INSN_UID (e1->insn))
#endif
)
match_seqs (e0, e1);
#ifdef STACK_REGS
/* Free unused data. */
bitmap_clear (&stack_reg_live);
#endif
}
/* Transforms a regset to a HARD_REG_SET. Every hard register in REGS is added
to hregs. Additionally, the hard counterpart of every renumbered pseudo
register is also added. */
static void
renumbered_reg_set_to_hard_reg_set (HARD_REG_SET * hregs, regset regs)
{
int r;
REG_SET_TO_HARD_REG_SET (*hregs, regs);
for (r = FIRST_PSEUDO_REGISTER; r < max_regno; r++)
if (REGNO_REG_SET_P (regs, r) && reg_renumber[r] >= 0)
SET_HARD_REG_BIT (*hregs, reg_renumber[r]);
}
/* Clears the bits in REGS for all registers, which are live in the sequence
give by its last INSN and its LENGTH. */
static void
clear_regs_live_in_seq (HARD_REG_SET * regs, rtx insn, int length)
{
basic_block bb;
regset_head live;
HARD_REG_SET hlive;
struct propagate_block_info *pbi;
rtx x;
int i;
/* Initialize liveness propagation. */
bb = BLOCK_FOR_INSN (insn);
INIT_REG_SET (&live);
COPY_REG_SET (&live, bb->il.rtl->global_live_at_end);
pbi = init_propagate_block_info (bb, &live, NULL, NULL, 0);
/* Propagate until INSN if found. */
for (x = BB_END (bb); x != insn;)
x = propagate_one_insn (pbi, x);
/* Clear registers live after INSN. */
renumbered_reg_set_to_hard_reg_set (&hlive, &live);
AND_COMPL_HARD_REG_SET (*regs, hlive);
/* Clear registers live in and before the sequence. */
for (i = 0; i < length;)
{
rtx prev = propagate_one_insn (pbi, x);
if (INSN_P (x))
{
renumbered_reg_set_to_hard_reg_set (&hlive, &live);
AND_COMPL_HARD_REG_SET (*regs, hlive);
i++;
}
x = prev;
}
/* Free unused data. */
free_propagate_block_info (pbi);
CLEAR_REG_SET (&live);
}
/* Computes the gain of turning PSEQ into a pseudo-function and its matching
sequences into pseudo-calls. Also computes and caches the number of insns to
abstract from the matching sequences. */
static void
recompute_gain_for_pattern_seq (pattern_seq pseq)
{
matching_seq mseq;
rtx x;
int i;
int hascall;
HARD_REG_SET linkregs;
/* Initialize data. */
SET_HARD_REG_SET (linkregs);
pseq->link_reg = NULL_RTX;
pseq->abstracted_length = 0;
pseq->gain = -(seq_call_cost - seq_jump_cost + seq_return_cost);
/* Determine ABSTRACTED_LENGTH and COST for matching sequences of PSEQ.
ABSTRACTED_LENGTH may be less than MATCHING_LENGTH if sequences in the
same block overlap. */
for (mseq = pseq->matching_seqs; mseq; mseq = mseq->next_matching_seq)
{
/* Determine ABSTRACTED_LENGTH. */
if (mseq->next_matching_seq)
mseq->abstracted_length = (int)(mseq->next_matching_seq->idx -
mseq->idx);
else
mseq->abstracted_length = mseq->matching_length;
if (mseq->abstracted_length > mseq->matching_length)
mseq->abstracted_length = mseq->matching_length;
/* Compute the cost of sequence. */
RECOMPUTE_COST (mseq);
/* If COST is big enough registers live in this matching sequence
should not be used as a link register. Also set ABSTRACTED_LENGTH
of PSEQ. */
if (mseq->cost > seq_call_cost)
{
clear_regs_live_in_seq (&linkregs, mseq->insn,
mseq->abstracted_length);
if (mseq->abstracted_length > pseq->abstracted_length)
pseq->abstracted_length = mseq->abstracted_length;
}
}
/* Modify ABSTRACTED_LENGTH of PSEQ if pattern sequence overlaps with one
of the matching sequences. */
for (mseq = pseq->matching_seqs; mseq; mseq = mseq->next_matching_seq)
{
x = pseq->insn;
for (i = 0; (i < pseq->abstracted_length) && (x != mseq->insn); i++)
x = prev_insn_in_block (x);
pseq->abstracted_length = i;
}
/* Compute the cost of pattern sequence. */
RECOMPUTE_COST (pseq);
/* No gain if COST is too small. */
if (pseq->cost <= seq_call_cost)
{
pseq->gain = -1;
return;
}
/* Ensure that no matching sequence is longer than the pattern sequence. */
for (mseq = pseq->matching_seqs; mseq; mseq = mseq->next_matching_seq)
{
if (mseq->abstracted_length > pseq->abstracted_length)
{
mseq->abstracted_length = pseq->abstracted_length;
RECOMPUTE_COST (mseq);
}
/* Once the length is stabilizing the gain can be calculated. */
if (mseq->cost > seq_call_cost)
pseq->gain += mseq->cost - seq_call_cost;
}
/* No need to do further work if there is no gain. */
if (pseq->gain <= 0)
return;
/* Should not use registers live in the pattern sequence as link register.
*/
clear_regs_live_in_seq (&linkregs, pseq->insn, pseq->abstracted_length);
/* Determine whether pattern sequence contains a call_insn. */
hascall = 0;
x = pseq->insn;
for (i = 0; i < pseq->abstracted_length; i++)
{
if (CALL_P (x))
{
hascall = 1;
break;
}
x = prev_insn_in_block (x);
}
/* Should not use a register as a link register if - it is a fixed
register, or - the sequence contains a call insn and the register is a
call used register, or - the register needs to be saved if used in a
function but was not used before (since saving it can invalidate already
computed frame pointer offsets), or - the register cannot be used as a
base register. */
for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
if (fixed_regs[i]
#ifdef REGNO_OK_FOR_INDIRECT_JUMP_P
|| (!REGNO_OK_FOR_INDIRECT_JUMP_P (i, Pmode))
#else
|| (!ok_for_base_p_1 (i, Pmode, MEM, SCRATCH))
|| (!reg_class_subset_p (REGNO_REG_CLASS (i),
base_reg_class (VOIDmode, MEM, SCRATCH)))
#endif
|| (hascall && call_used_regs[i])
|| (!call_used_regs[i] && !regs_ever_live[i]))
CLEAR_HARD_REG_BIT (linkregs, i);
/* Find an appropriate register to be used as the link register. */
for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
if (TEST_HARD_REG_BIT (linkregs, i))
{
pseq->link_reg = gen_rtx_REG (Pmode, i);
break;
}
/* Abstraction is not possible if no link register is available, so set
gain to 0. */
if (!pseq->link_reg)
pseq->gain = 0;
}
/* Deallocates memory occupied by PSEQ and its matching seqs. */
static void
free_pattern_seq (pattern_seq pseq)
{
while (pseq->matching_seqs)
{
matching_seq mseq = pseq->matching_seqs;
pseq->matching_seqs = mseq->next_matching_seq;
free (mseq);
}
free (pseq);
}
/* Computes the gain for pattern sequences. Pattern sequences producing no gain
are deleted. The pattern sequence with the biggest gain is moved to the first
place of PATTERN_SEQS. */
static void
recompute_gain (void)
{
pattern_seq *pseq;
int maxgain;
maxgain = 0;
for (pseq = &pattern_seqs; *pseq;)
{
if ((*pseq)->gain <= 0)
recompute_gain_for_pattern_seq (*pseq);
if ((*pseq)->gain > 0)
{
if ((*pseq)->gain > maxgain)
{
pattern_seq temp = *pseq;
(*pseq) = temp->next_pattern_seq;
temp->next_pattern_seq = pattern_seqs;
pattern_seqs = temp;
maxgain = pattern_seqs->gain;
}
else
{
pseq = &(*pseq)->next_pattern_seq;
}
}
else
{
pattern_seq temp = *pseq;
*pseq = temp->next_pattern_seq;
free_pattern_seq (temp);
}
}
}
/* Updated those pattern sequences and matching sequences, which overlap with
the sequence given by INSN and LEN. Deletes sequences shrinking below a
limit. */
static void
erase_from_pattern_seqs (rtx insn, int len)
{
pattern_seq *pseq;
matching_seq *mseq;
rtx x;
int plen, mlen;
int pcost, mcost;
while (len > 0)
{
for (pseq = &pattern_seqs; *pseq;)
{
plen = 0;
pcost = 0;
for (x = (*pseq)->insn; x && (x != insn);
x = prev_insn_in_block (x))
{
plen++;
pcost += compute_rtx_cost (x);
}
if (pcost <= seq_call_cost)
{
pattern_seq temp = *pseq;
*pseq = temp->next_pattern_seq;
free_pattern_seq (temp);
}
else
{
for (mseq = &(*pseq)->matching_seqs; *mseq;)
{
mlen = 0;
mcost = 0;
for (x = (*mseq)->insn;
x && (x != insn) && (mlen < plen)
&& (mlen < (*mseq)->matching_length);
x = prev_insn_in_block (x))
{
mlen++;
mcost += compute_rtx_cost (x);
}
if (mcost <= seq_call_cost)
{
matching_seq temp = *mseq;
*mseq = temp->next_matching_seq;
free (temp);
/* Set to 0 to force gain recomputation. */
(*pseq)->gain = 0;
}
else
{
if (mlen < (*mseq)->matching_length)
{
(*mseq)->cost = mcost;
(*mseq)->matching_length = mlen;
/* Set to 0 to force gain recomputation. */
(*pseq)->gain = 0;
}
mseq = &(*mseq)->next_matching_seq;
}
}
pseq = &(*pseq)->next_pattern_seq;
}
}
len--;
insn = prev_insn_in_block (insn);
}
}
/* Updates those pattern sequences and matching sequences, which overlap with
the pattern sequence with the biggest gain and its matching sequences. */
static void
update_pattern_seqs (void)
{
pattern_seq bestpseq;
matching_seq mseq;
bestpseq = pattern_seqs;
pattern_seqs = bestpseq->next_pattern_seq;
for (mseq = bestpseq->matching_seqs; mseq; mseq = mseq->next_matching_seq)
if (mseq->cost > seq_call_cost)
erase_from_pattern_seqs (mseq->insn, mseq->abstracted_length);
erase_from_pattern_seqs (bestpseq->insn, bestpseq->abstracted_length);
bestpseq->next_pattern_seq = pattern_seqs;
pattern_seqs = bestpseq;
}
/* Groups together those matching sequences of the best pattern sequence, which
have the same ABSTRACTED_LENGTH and puts these groups in ascending order.
SEQ_BLOCKS contains the result. */
static void
determine_seq_blocks (void)
{
seq_block sb;
matching_seq *mseq;
matching_seq m;
/* Initialize SEQ_BLOCKS to empty. */
seq_blocks = 0;
/* Process all matching sequences. */
for (mseq = &pattern_seqs->matching_seqs; *mseq;)
{
/* Deal only with matching sequences being long enough. */
if ((*mseq)->cost <= seq_call_cost)
{
mseq = &(*mseq)->next_matching_seq;
continue;
}
/* Ensure that SB contains a seq_block with the appropriate length.
Insert a new seq_block if necessary. */
if (!seq_blocks || ((*mseq)->abstracted_length < seq_blocks->length))
{
sb = (seq_block) xmalloc (sizeof (struct seq_block_def));
sb->length = (*mseq)->abstracted_length;
sb->label = NULL_RTX;
sb->matching_seqs = 0;
sb->next_seq_block = seq_blocks;
seq_blocks = sb;
}
else
{
for (sb = seq_blocks; sb; sb = sb->next_seq_block)
{
if ((*mseq)->abstracted_length == sb->length)
break;
if (!sb->next_seq_block
|| ((*mseq)->abstracted_length <
sb->next_seq_block->length))
{
seq_block temp =
(seq_block) xmalloc (sizeof (struct seq_block_def));
temp->length = (*mseq)->abstracted_length;
temp->label = NULL_RTX;
temp->matching_seqs = 0;
temp->next_seq_block = sb->next_seq_block;
sb->next_seq_block = temp;
}
}
}
/* Remove the matching sequence from the linked list of the pattern
sequence and link it to SB. */
m = *mseq;
*mseq = m->next_matching_seq;
m->next_matching_seq = sb->matching_seqs;
sb->matching_seqs = m;
}
}
/* Builds a symbol_ref for LABEL. */
static rtx
gen_symbol_ref_rtx_for_label (rtx label)
{
char name[20];
rtx sym;
ASM_GENERATE_INTERNAL_LABEL (name, "L", CODE_LABEL_NUMBER (label));
sym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_LOCAL;
return sym;
}
/* Ensures that INSN is the last insn in its block and returns the block label
of the next block. */
static rtx
block_label_after (rtx insn)
{
basic_block bb = BLOCK_FOR_INSN (insn);
if ((insn == BB_END (bb)) && (bb->next_bb != EXIT_BLOCK_PTR))
return block_label (bb->next_bb);
else
return block_label (split_block (bb, insn)->dest);
}
/* Ensures that the last insns of the best pattern and its matching sequences
are the last insns in their block. Additionally, extends the live set at the
end of the pattern sequence with the live sets at the end of the matching
sequences. */
static void
split_blocks_after_seqs (void)
{
seq_block sb;
matching_seq mseq;
block_label_after (pattern_seqs->insn);
for (sb = seq_blocks; sb; sb = sb->next_seq_block)
{
for (mseq = sb->matching_seqs; mseq; mseq = mseq->next_matching_seq)
{
block_label_after (mseq->insn);
IOR_REG_SET (BLOCK_FOR_INSN (pattern_seqs->insn)->
il.rtl->global_live_at_end,
BLOCK_FOR_INSN (mseq->insn)->il.rtl->global_live_at_end);
}
}
}
/* Splits the best pattern sequence according to SEQ_BLOCKS. Emits pseudo-call
and -return insns before and after the sequence. */
static void
split_pattern_seq (void)
{
rtx insn;
basic_block bb;
rtx retlabel, retjmp, saveinsn;
int i;
seq_block sb;
insn = pattern_seqs->insn;
bb = BLOCK_FOR_INSN (insn);
/* Get the label after the sequence. This will be the return address. The
label will be referenced using a symbol_ref so protect it from
deleting. */
retlabel = block_label_after (insn);
LABEL_PRESERVE_P (retlabel) = 1;
/* Emit an indirect jump via the link register after the sequence acting
as the return insn. Also emit a barrier and update the basic block. */
retjmp = emit_jump_insn_after (gen_indirect_jump (pattern_seqs->link_reg),
BB_END (bb));
emit_barrier_after (BB_END (bb));
/* Replace all outgoing edges with a new one to the block of RETLABEL. */
while (EDGE_COUNT (bb->succs) != 0)
remove_edge (EDGE_SUCC (bb, 0));
make_edge (bb, BLOCK_FOR_INSN (retlabel), EDGE_ABNORMAL);
/* Split the sequence according to SEQ_BLOCKS and cache the label of the
resulting basic blocks. */
i = 0;
for (sb = seq_blocks; sb; sb = sb->next_seq_block)
{
for (; i < sb->length; i++)
insn = prev_insn_in_block (insn);
sb->label = block_label (split_block (bb, insn)->dest);
}
/* Emit an insn saving the return address to the link register before the
sequence. */
saveinsn = emit_insn_after (gen_move_insn (pattern_seqs->link_reg,
gen_symbol_ref_rtx_for_label
(retlabel)), BB_END (bb));
/* Update liveness info. */
SET_REGNO_REG_SET (bb->il.rtl->global_live_at_end,
REGNO (pattern_seqs->link_reg));
}
/* Deletes the insns of the matching sequences of the best pattern sequence and
replaces them with pseudo-calls to the pattern sequence. */
static void
erase_matching_seqs (void)
{
seq_block sb;
matching_seq mseq;
rtx insn;
basic_block bb;
rtx retlabel, saveinsn, callinsn;
int i;
for (sb = seq_blocks; sb; sb = sb->next_seq_block)
{
for (mseq = sb->matching_seqs; mseq; mseq = mseq->next_matching_seq)
{
insn = mseq->insn;
bb = BLOCK_FOR_INSN (insn);
/* Get the label after the sequence. This will be the return
address. The label will be referenced using a symbol_ref so
protect it from deleting. */
retlabel = block_label_after (insn);
LABEL_PRESERVE_P (retlabel) = 1;
/* Delete the insns of the sequence. */
for (i = 0; i < sb->length; i++)
insn = prev_insn_in_block (insn);
delete_basic_block (split_block (bb, insn)->dest);
/* Emit an insn saving the return address to the link register
before the deleted sequence. */
saveinsn = emit_insn_after (gen_move_insn (pattern_seqs->link_reg,
gen_symbol_ref_rtx_for_label
(retlabel)),
BB_END (bb));
BLOCK_FOR_INSN (saveinsn) = bb;
/* Emit a jump to the appropriate part of the pattern sequence
after the save insn. Also update the basic block. */
callinsn = emit_jump_insn_after (gen_jump (sb->label), saveinsn);
JUMP_LABEL (callinsn) = sb->label;
LABEL_NUSES (sb->label)++;
BLOCK_FOR_INSN (callinsn) = bb;
BB_END (bb) = callinsn;
/* Maintain control flow and liveness information. */
SET_REGNO_REG_SET (bb->il.rtl->global_live_at_end,
REGNO (pattern_seqs->link_reg));
emit_barrier_after (BB_END (bb));
make_single_succ_edge (bb, BLOCK_FOR_INSN (sb->label), 0);
IOR_REG_SET (bb->il.rtl->global_live_at_end,
BLOCK_FOR_INSN (sb->label)->il.rtl->global_live_at_start);
make_edge (BLOCK_FOR_INSN (seq_blocks->label),
BLOCK_FOR_INSN (retlabel), EDGE_ABNORMAL);
}
}
}
/* Deallocates SEQ_BLOCKS and all the matching sequences. */
static void
free_seq_blocks (void)
{
while (seq_blocks)
{
seq_block sb = seq_blocks;
while (sb->matching_seqs)
{
matching_seq mseq = sb->matching_seqs;
sb->matching_seqs = mseq->next_matching_seq;
free (mseq);
}
seq_blocks = sb->next_seq_block;
free (sb);
}
}
/* Transforms the best pattern sequence into a pseudo-function and its matching
sequences to pseudo-calls. Afterwards the best pattern sequence is removed
from PATTERN_SEQS. */
static void
abstract_best_seq (void)
{
pattern_seq bestpseq;
/* Do the abstraction. */
determine_seq_blocks ();
split_blocks_after_seqs ();
split_pattern_seq ();
erase_matching_seqs ();
free_seq_blocks ();
/* Record the usage of the link register. */
regs_ever_live[REGNO (pattern_seqs->link_reg)] = 1;
/* Remove the best pattern sequence. */
bestpseq = pattern_seqs;
pattern_seqs = bestpseq->next_pattern_seq;
free_pattern_seq (bestpseq);
}
/* Prints info on the pattern sequences to the dump file. */
static void
dump_pattern_seqs (void)
{
pattern_seq pseq;
matching_seq mseq;
if (!dump_file)
return;
fprintf (dump_file, ";; Pattern sequences\n");
for (pseq = pattern_seqs; pseq; pseq = pseq->next_pattern_seq)
{
fprintf (dump_file, "Pattern sequence at insn %d matches sequences at",
INSN_UID (pseq->insn));
for (mseq = pseq->matching_seqs; mseq; mseq = mseq->next_matching_seq)
{
fprintf (dump_file, " insn %d (length %d)", INSN_UID (mseq->insn),
mseq->matching_length);
if (mseq->next_matching_seq)
fprintf (dump_file, ",");
}
fprintf (dump_file, ".\n");
}
fprintf (dump_file, "\n");
}
/* Prints info on the best pattern sequence transformed in the ITER-th
iteration to the dump file. */
static void
dump_best_pattern_seq (int iter)
{
matching_seq mseq;
if (!dump_file)
return;
fprintf (dump_file, ";; Iteration %d\n", iter);
fprintf (dump_file,
"Best pattern sequence with %d gain is at insn %d (length %d).\n",
pattern_seqs->gain, INSN_UID (pattern_seqs->insn),
pattern_seqs->abstracted_length);
fprintf (dump_file, "Matching sequences are at");
for (mseq = pattern_seqs->matching_seqs; mseq;
mseq = mseq->next_matching_seq)
{
fprintf (dump_file, " insn %d (length %d)", INSN_UID (mseq->insn),
mseq->abstracted_length);
if (mseq->next_matching_seq)
fprintf (dump_file, ",");
}
fprintf (dump_file, ".\n");
fprintf (dump_file, "Using reg %d as link register.\n\n",
REGNO (pattern_seqs->link_reg));
}
/* Htab hash function for hash_bucket_def structure. */
static unsigned int
htab_hash_bucket (const void *p)
{
p_hash_bucket bucket = (p_hash_bucket) p;
return bucket->hash;
}
/* Htab equal function for hash_bucket_def structure. */
static int
htab_eq_bucket (const void *p0, const void *p1)
{
return htab_hash_bucket (p0) == htab_hash_bucket (p1);
}
/* Htab delete function for hash_bucket_def structure. */
static void
htab_del_bucket (void *p)
{
p_hash_bucket bucket = (p_hash_bucket) p;
if (bucket->seq_candidates)
htab_delete (bucket->seq_candidates);
free (bucket);
}
/* Htab hash function for hash_bucket_def structure. */
static unsigned int
htab_hash_elem (const void *p)
{
p_hash_elem elem = (p_hash_elem) p;
return htab_hash_pointer (elem->insn);
}
/* Htab equal function for hash_bucket_def structure. */
static int
htab_eq_elem (const void *p0, const void *p1)
{
return htab_hash_elem (p0) == htab_hash_elem (p1);
}
/* Htab delete function for hash_bucket_def structure. */
static void
htab_del_elem (void *p)
{
p_hash_elem elem = (p_hash_elem) p;
free (elem);
}
/* Creates a hash value for each sequence candidate and saves them
in HASH_BUCKET. */
static void
fill_hash_bucket (void)
{
basic_block bb;
rtx insn;
void **slot;
p_hash_bucket bucket;
struct hash_bucket_def tmp_bucket;
p_hash_elem elem;
unsigned long insn_idx;
insn_idx = 0;
FOR_EACH_BB (bb)
{
FOR_BB_INSNS_REVERSE (bb, insn)
{
if (!ABSTRACTABLE_INSN_P (insn))
continue;
/* Compute hash value for INSN. */
tmp_bucket.hash = compute_hash (insn);
/* Select the hash group. */
bucket = htab_find (hash_buckets, &tmp_bucket);
if (!bucket)
{
/* Create a new hash group. */
bucket = (p_hash_bucket) xcalloc (1,
sizeof (struct hash_bucket_def));
bucket->hash = tmp_bucket.hash;
bucket->seq_candidates = NULL;
slot = htab_find_slot (hash_buckets, &tmp_bucket, INSERT);
*slot = bucket;
}
/* Create new list for storing sequence candidates. */
if (!bucket->seq_candidates)
bucket->seq_candidates = htab_create (HASH_INIT,
htab_hash_elem,
htab_eq_elem,
htab_del_elem);
elem = (p_hash_elem) xcalloc (1, sizeof (struct hash_elem_def));
elem->insn = insn;
elem->idx = insn_idx;
elem->length = get_attr_length (insn);
/* Insert INSN into BUCKET hash bucket. */
slot = htab_find_slot (bucket->seq_candidates, elem, INSERT);
*slot = elem;
insn_idx++;
}
}
}
/* Computes the cost of calling sequence and the cost of return. */
static void
compute_init_costs (void)
{
rtx rtx_jump, rtx_store, rtx_return, reg, label;
basic_block bb;
FOR_EACH_BB (bb)
if (BB_HEAD (bb))
break;
label = block_label (bb);
reg = gen_rtx_REG (Pmode, 0);
/* Pattern for indirect jump. */
rtx_jump = gen_indirect_jump (reg);
/* Pattern for storing address. */
rtx_store = gen_rtx_SET (VOIDmode, reg, gen_symbol_ref_rtx_for_label (label));
/* Pattern for return insn. */
rtx_return = gen_jump (label);
/* The cost of jump. */
seq_jump_cost = compute_rtx_cost (make_jump_insn_raw (rtx_jump));
/* The cost of calling sequence. */
seq_call_cost = seq_jump_cost + compute_rtx_cost (make_insn_raw (rtx_store));
/* The cost of return. */
seq_return_cost = compute_rtx_cost (make_jump_insn_raw (rtx_return));
/* Simple heuristic for minimal sequence cost. */
seq_call_cost = (int)(seq_call_cost * (double)SEQ_CALL_COST_MULTIPLIER);
}
/* Finds equivalent insn sequences in the current function and retains only one
instance of them which is turned into a pseudo-function. The additional
copies are erased and replaced by pseudo-calls to the retained sequence. */
static void
rtl_seqabstr (void)
{
int iter;
/* Create a hash list for COLLECT_PATTERN_SEQS. */
hash_buckets = htab_create (HASH_INIT, htab_hash_bucket , htab_eq_bucket ,
htab_del_bucket);
fill_hash_bucket ();
/* Compute the common cost of abstraction. */
compute_init_costs ();
/* Build an initial set of pattern sequences from the current function. */
collect_pattern_seqs ();
dump_pattern_seqs ();
/* Iterate until there are no sequences to abstract. */
for (iter = 1;; iter++)
{
/* Recompute gain for sequences if necessary and select sequence with
biggest gain. */
recompute_gain ();
if (!pattern_seqs)
break;
dump_best_pattern_seq (iter);
/* Update the cached info of the other sequences and force gain
recomputation where needed. */
update_pattern_seqs ();
/* Turn best sequences into pseudo-functions and -calls. */
abstract_best_seq ();
}
/* Cleanup hash tables. */
htab_delete (hash_buckets);
if (iter > 1)
{
/* Update notes. */
count_or_remove_death_notes (NULL, 1);
life_analysis (PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
| PROP_KILL_DEAD_CODE);
/* Extra cleanup. */
cleanup_cfg (CLEANUP_EXPENSIVE |
CLEANUP_UPDATE_LIFE |
(flag_crossjumping ? CLEANUP_CROSSJUMP : 0));
}
}
/* The gate function for TREE_OPT_PASS. */
static bool
gate_rtl_seqabstr (void)
{
return flag_rtl_seqabstr;
}
/* The entry point of the sequence abstraction algorithm. */
static unsigned int
rest_of_rtl_seqabstr (void)
{
life_analysis (PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE | PROP_KILL_DEAD_CODE);
cleanup_cfg (CLEANUP_EXPENSIVE |
CLEANUP_UPDATE_LIFE |
(flag_crossjumping ? CLEANUP_CROSSJUMP : 0));
/* Abstract out common insn sequences. */
rtl_seqabstr ();
return 0;
}
struct tree_opt_pass pass_rtl_seqabstr = {
"seqabstr", /* name */
gate_rtl_seqabstr, /* gate */
rest_of_rtl_seqabstr, /* execute */
NULL, /* sub */
NULL, /* next */
0, /* static_pass_number */
TV_SEQABSTR, /* tv_id */
0, /* properties_required */
0, /* properties_provided */
0, /* properties_destroyed */
0, /* todo_flags_start */
TODO_dump_func |
TODO_ggc_collect, /* todo_flags_finish */
'Q' /* letter */
};
|