forked from riscvarchive/riscv-gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree-ssa-tail-merge.c
1824 lines (1481 loc) · 46.6 KB
/
tree-ssa-tail-merge.c
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
/* Tail merging for gimple.
Copyright (C) 2011-2020 Free Software Foundation, Inc.
Contributed by Tom de Vries ([email protected])
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/>. */
/* Pass overview.
MOTIVATIONAL EXAMPLE
gimple representation of gcc/testsuite/gcc.dg/pr43864.c at
hprofStartupp (charD.1 * outputFileNameD.2600, charD.1 * ctxD.2601)
{
struct FILED.1638 * fpD.2605;
charD.1 fileNameD.2604[1000];
intD.0 D.3915;
const charD.1 * restrict outputFileName.0D.3914;
# BLOCK 2 freq:10000
# PRED: ENTRY [100.0%] (fallthru,exec)
# PT = nonlocal { D.3926 } (restr)
outputFileName.0D.3914_3
= (const charD.1 * restrict) outputFileNameD.2600_2(D);
# .MEMD.3923_13 = VDEF <.MEMD.3923_12(D)>
# USE = nonlocal null { fileNameD.2604 D.3926 } (restr)
# CLB = nonlocal null { fileNameD.2604 D.3926 } (restr)
sprintfD.759 (&fileNameD.2604, outputFileName.0D.3914_3);
# .MEMD.3923_14 = VDEF <.MEMD.3923_13>
# USE = nonlocal null { fileNameD.2604 D.3926 } (restr)
# CLB = nonlocal null { fileNameD.2604 D.3926 } (restr)
D.3915_4 = accessD.2606 (&fileNameD.2604, 1);
if (D.3915_4 == 0)
goto <bb 3>;
else
goto <bb 4>;
# SUCC: 3 [10.0%] (true,exec) 4 [90.0%] (false,exec)
# BLOCK 3 freq:1000
# PRED: 2 [10.0%] (true,exec)
# .MEMD.3923_15 = VDEF <.MEMD.3923_14>
# USE = nonlocal null { fileNameD.2604 D.3926 } (restr)
# CLB = nonlocal null { fileNameD.2604 D.3926 } (restr)
freeD.898 (ctxD.2601_5(D));
goto <bb 7>;
# SUCC: 7 [100.0%] (fallthru,exec)
# BLOCK 4 freq:9000
# PRED: 2 [90.0%] (false,exec)
# .MEMD.3923_16 = VDEF <.MEMD.3923_14>
# PT = nonlocal escaped
# USE = nonlocal null { fileNameD.2604 D.3926 } (restr)
# CLB = nonlocal null { fileNameD.2604 D.3926 } (restr)
fpD.2605_8 = fopenD.1805 (&fileNameD.2604[0], 0B);
if (fpD.2605_8 == 0B)
goto <bb 5>;
else
goto <bb 6>;
# SUCC: 5 [1.9%] (true,exec) 6 [98.1%] (false,exec)
# BLOCK 5 freq:173
# PRED: 4 [1.9%] (true,exec)
# .MEMD.3923_17 = VDEF <.MEMD.3923_16>
# USE = nonlocal null { fileNameD.2604 D.3926 } (restr)
# CLB = nonlocal null { fileNameD.2604 D.3926 } (restr)
freeD.898 (ctxD.2601_5(D));
goto <bb 7>;
# SUCC: 7 [100.0%] (fallthru,exec)
# BLOCK 6 freq:8827
# PRED: 4 [98.1%] (false,exec)
# .MEMD.3923_18 = VDEF <.MEMD.3923_16>
# USE = nonlocal null { fileNameD.2604 D.3926 } (restr)
# CLB = nonlocal null { fileNameD.2604 D.3926 } (restr)
fooD.2599 (outputFileNameD.2600_2(D), fpD.2605_8);
# SUCC: 7 [100.0%] (fallthru,exec)
# BLOCK 7 freq:10000
# PRED: 3 [100.0%] (fallthru,exec) 5 [100.0%] (fallthru,exec)
6 [100.0%] (fallthru,exec)
# PT = nonlocal null
# ctxD.2601_1 = PHI <0B(3), 0B(5), ctxD.2601_5(D)(6)>
# .MEMD.3923_11 = PHI <.MEMD.3923_15(3), .MEMD.3923_17(5),
.MEMD.3923_18(6)>
# VUSE <.MEMD.3923_11>
return ctxD.2601_1;
# SUCC: EXIT [100.0%]
}
bb 3 and bb 5 can be merged. The blocks have different predecessors, but the
same successors, and the same operations.
CONTEXT
A technique called tail merging (or cross jumping) can fix the example
above. For a block, we look for common code at the end (the tail) of the
predecessor blocks, and insert jumps from one block to the other.
The example is a special case for tail merging, in that 2 whole blocks
can be merged, rather than just the end parts of it.
We currently only focus on whole block merging, so in that sense
calling this pass tail merge is a bit of a misnomer.
We distinguish 2 kinds of situations in which blocks can be merged:
- same operations, same predecessors. The successor edges coming from one
block are redirected to come from the other block.
- same operations, same successors. The predecessor edges entering one block
are redirected to enter the other block. Note that this operation might
involve introducing phi operations.
For efficient implementation, we would like to value numbers the blocks, and
have a comparison operator that tells us whether the blocks are equal.
Besides being runtime efficient, block value numbering should also abstract
from irrelevant differences in order of operations, much like normal value
numbering abstracts from irrelevant order of operations.
For the first situation (same_operations, same predecessors), normal value
numbering fits well. We can calculate a block value number based on the
value numbers of the defs and vdefs.
For the second situation (same operations, same successors), this approach
doesn't work so well. We can illustrate this using the example. The calls
to free use different vdefs: MEMD.3923_16 and MEMD.3923_14, and these will
remain different in value numbering, since they represent different memory
states. So the resulting vdefs of the frees will be different in value
numbering, so the block value numbers will be different.
The reason why we call the blocks equal is not because they define the same
values, but because uses in the blocks use (possibly different) defs in the
same way. To be able to detect this efficiently, we need to do some kind of
reverse value numbering, meaning number the uses rather than the defs, and
calculate a block value number based on the value number of the uses.
Ideally, a block comparison operator will also indicate which phis are needed
to merge the blocks.
For the moment, we don't do block value numbering, but we do insn-by-insn
matching, using scc value numbers to match operations with results, and
structural comparison otherwise, while ignoring vop mismatches.
IMPLEMENTATION
1. The pass first determines all groups of blocks with the same successor
blocks.
2. Within each group, it tries to determine clusters of equal basic blocks.
3. The clusters are applied.
4. The same successor groups are updated.
5. This process is repeated from 2 onwards, until no more changes.
LIMITATIONS/TODO
- block only
- handles only 'same operations, same successors'.
It handles same predecessors as a special subcase though.
- does not implement the reverse value numbering and block value numbering.
- improve memory allocation: use garbage collected memory, obstacks,
allocpools where appropriate.
- no insertion of gimple_reg phis, We only introduce vop-phis.
- handle blocks with gimple_reg phi_nodes.
PASS PLACEMENT
This 'pass' is not a stand-alone gimple pass, but runs as part of
pass_pre, in order to share the value numbering.
SWITCHES
- ftree-tail-merge. On at -O2. We may have to enable it only at -Os. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "tree.h"
#include "gimple.h"
#include "cfghooks.h"
#include "tree-pass.h"
#include "ssa.h"
#include "fold-const.h"
#include "trans-mem.h"
#include "cfganal.h"
#include "cfgcleanup.h"
#include "gimple-iterator.h"
#include "tree-cfg.h"
#include "tree-into-ssa.h"
#include "tree-ssa-sccvn.h"
#include "cfgloop.h"
#include "tree-eh.h"
#include "tree-cfgcleanup.h"
const int ignore_edge_flags = EDGE_DFS_BACK | EDGE_EXECUTABLE;
/* Describes a group of bbs with the same successors. The successor bbs are
cached in succs, and the successor edge flags are cached in succ_flags.
If a bb has the EDGE_TRUE/FALSE_VALUE flags swapped compared to succ_flags,
it's marked in inverse.
Additionally, the hash value for the struct is cached in hashval, and
in_worklist indicates whether it's currently part of worklist. */
struct same_succ : pointer_hash <same_succ>
{
/* The bbs that have the same successor bbs. */
bitmap bbs;
/* The successor bbs. */
bitmap succs;
/* Indicates whether the EDGE_TRUE/FALSE_VALUEs of succ_flags are swapped for
bb. */
bitmap inverse;
/* The edge flags for each of the successor bbs. */
vec<int> succ_flags;
/* Indicates whether the struct is currently in the worklist. */
bool in_worklist;
/* The hash value of the struct. */
hashval_t hashval;
/* hash_table support. */
static inline hashval_t hash (const same_succ *);
static int equal (const same_succ *, const same_succ *);
static void remove (same_succ *);
};
/* hash routine for hash_table support, returns hashval of E. */
inline hashval_t
same_succ::hash (const same_succ *e)
{
return e->hashval;
}
/* A group of bbs where 1 bb from bbs can replace the other bbs. */
struct bb_cluster
{
/* The bbs in the cluster. */
bitmap bbs;
/* The preds of the bbs in the cluster. */
bitmap preds;
/* Index in all_clusters vector. */
int index;
/* The bb to replace the cluster with. */
basic_block rep_bb;
};
/* Per bb-info. */
struct aux_bb_info
{
/* The number of non-debug statements in the bb. */
int size;
/* The same_succ that this bb is a member of. */
same_succ *bb_same_succ;
/* The cluster that this bb is a member of. */
bb_cluster *cluster;
/* The vop state at the exit of a bb. This is shortlived data, used to
communicate data between update_block_by and update_vuses. */
tree vop_at_exit;
/* The bb that either contains or is dominated by the dependencies of the
bb. */
basic_block dep_bb;
};
/* Macros to access the fields of struct aux_bb_info. */
#define BB_SIZE(bb) (((struct aux_bb_info *)bb->aux)->size)
#define BB_SAME_SUCC(bb) (((struct aux_bb_info *)bb->aux)->bb_same_succ)
#define BB_CLUSTER(bb) (((struct aux_bb_info *)bb->aux)->cluster)
#define BB_VOP_AT_EXIT(bb) (((struct aux_bb_info *)bb->aux)->vop_at_exit)
#define BB_DEP_BB(bb) (((struct aux_bb_info *)bb->aux)->dep_bb)
/* Valueization helper querying the VN lattice. */
static tree
tail_merge_valueize (tree name)
{
if (TREE_CODE (name) == SSA_NAME
&& has_VN_INFO (name))
{
tree tem = VN_INFO (name)->valnum;
if (tem != VN_TOP)
return tem;
}
return name;
}
/* Returns true if the only effect a statement STMT has, is to define locally
used SSA_NAMEs. */
static bool
stmt_local_def (gimple *stmt)
{
basic_block bb, def_bb;
imm_use_iterator iter;
use_operand_p use_p;
tree val;
def_operand_p def_p;
if (gimple_vdef (stmt) != NULL_TREE
|| gimple_has_side_effects (stmt)
|| gimple_could_trap_p_1 (stmt, false, false)
|| gimple_vuse (stmt) != NULL_TREE
/* Copied from tree-ssa-ifcombine.c:bb_no_side_effects_p():
const calls don't match any of the above, yet they could
still have some side-effects - they could contain
gimple_could_trap_p statements, like floating point
exceptions or integer division by zero. See PR70586.
FIXME: perhaps gimple_has_side_effects or gimple_could_trap_p
should handle this. */
|| is_gimple_call (stmt))
return false;
def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
if (def_p == NULL)
return false;
val = DEF_FROM_PTR (def_p);
if (val == NULL_TREE || TREE_CODE (val) != SSA_NAME)
return false;
def_bb = gimple_bb (stmt);
FOR_EACH_IMM_USE_FAST (use_p, iter, val)
{
if (is_gimple_debug (USE_STMT (use_p)))
continue;
bb = gimple_bb (USE_STMT (use_p));
if (bb == def_bb)
continue;
if (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI
&& EDGE_PRED (bb, PHI_ARG_INDEX_FROM_USE (use_p))->src == def_bb)
continue;
return false;
}
return true;
}
/* Let GSI skip forwards over local defs. */
static void
gsi_advance_fw_nondebug_nonlocal (gimple_stmt_iterator *gsi)
{
gimple *stmt;
while (true)
{
if (gsi_end_p (*gsi))
return;
stmt = gsi_stmt (*gsi);
if (!stmt_local_def (stmt))
return;
gsi_next_nondebug (gsi);
}
}
/* VAL1 and VAL2 are either:
- uses in BB1 and BB2, or
- phi alternatives for BB1 and BB2.
Return true if the uses have the same gvn value. */
static bool
gvn_uses_equal (tree val1, tree val2)
{
gcc_checking_assert (val1 != NULL_TREE && val2 != NULL_TREE);
if (val1 == val2)
return true;
if (tail_merge_valueize (val1) != tail_merge_valueize (val2))
return false;
return ((TREE_CODE (val1) == SSA_NAME || CONSTANT_CLASS_P (val1))
&& (TREE_CODE (val2) == SSA_NAME || CONSTANT_CLASS_P (val2)));
}
/* Prints E to FILE. */
static void
same_succ_print (FILE *file, const same_succ *e)
{
unsigned int i;
bitmap_print (file, e->bbs, "bbs:", "\n");
bitmap_print (file, e->succs, "succs:", "\n");
bitmap_print (file, e->inverse, "inverse:", "\n");
fprintf (file, "flags:");
for (i = 0; i < e->succ_flags.length (); ++i)
fprintf (file, " %x", e->succ_flags[i]);
fprintf (file, "\n");
}
/* Prints same_succ VE to VFILE. */
inline int
ssa_same_succ_print_traverse (same_succ **pe, FILE *file)
{
const same_succ *e = *pe;
same_succ_print (file, e);
return 1;
}
/* Update BB_DEP_BB (USE_BB), given a use of VAL in USE_BB. */
static void
update_dep_bb (basic_block use_bb, tree val)
{
basic_block dep_bb;
/* Not a dep. */
if (TREE_CODE (val) != SSA_NAME)
return;
/* Skip use of global def. */
if (SSA_NAME_IS_DEFAULT_DEF (val))
return;
/* Skip use of local def. */
dep_bb = gimple_bb (SSA_NAME_DEF_STMT (val));
if (dep_bb == use_bb)
return;
if (BB_DEP_BB (use_bb) == NULL
|| dominated_by_p (CDI_DOMINATORS, dep_bb, BB_DEP_BB (use_bb)))
BB_DEP_BB (use_bb) = dep_bb;
}
/* Update BB_DEP_BB, given the dependencies in STMT. */
static void
stmt_update_dep_bb (gimple *stmt)
{
ssa_op_iter iter;
use_operand_p use;
FOR_EACH_SSA_USE_OPERAND (use, stmt, iter, SSA_OP_USE)
update_dep_bb (gimple_bb (stmt), USE_FROM_PTR (use));
}
/* Calculates hash value for same_succ VE. */
static hashval_t
same_succ_hash (const same_succ *e)
{
inchash::hash hstate (bitmap_hash (e->succs));
int flags;
unsigned int i;
unsigned int first = bitmap_first_set_bit (e->bbs);
basic_block bb = BASIC_BLOCK_FOR_FN (cfun, first);
int size = 0;
gimple *stmt;
tree arg;
unsigned int s;
bitmap_iterator bs;
for (gimple_stmt_iterator gsi = gsi_start_nondebug_bb (bb);
!gsi_end_p (gsi); gsi_next_nondebug (&gsi))
{
stmt = gsi_stmt (gsi);
stmt_update_dep_bb (stmt);
if (stmt_local_def (stmt))
continue;
size++;
hstate.add_int (gimple_code (stmt));
if (is_gimple_assign (stmt))
hstate.add_int (gimple_assign_rhs_code (stmt));
if (!is_gimple_call (stmt))
continue;
if (gimple_call_internal_p (stmt))
hstate.add_int (gimple_call_internal_fn (stmt));
else
{
inchash::add_expr (gimple_call_fn (stmt), hstate);
if (gimple_call_chain (stmt))
inchash::add_expr (gimple_call_chain (stmt), hstate);
}
for (i = 0; i < gimple_call_num_args (stmt); i++)
{
arg = gimple_call_arg (stmt, i);
arg = tail_merge_valueize (arg);
inchash::add_expr (arg, hstate);
}
}
hstate.add_int (size);
BB_SIZE (bb) = size;
hstate.add_int (bb->loop_father->num);
for (i = 0; i < e->succ_flags.length (); ++i)
{
flags = e->succ_flags[i];
flags = flags & ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
hstate.add_int (flags);
}
EXECUTE_IF_SET_IN_BITMAP (e->succs, 0, s, bs)
{
int n = find_edge (bb, BASIC_BLOCK_FOR_FN (cfun, s))->dest_idx;
for (gphi_iterator gsi = gsi_start_phis (BASIC_BLOCK_FOR_FN (cfun, s));
!gsi_end_p (gsi);
gsi_next (&gsi))
{
gphi *phi = gsi.phi ();
tree lhs = gimple_phi_result (phi);
tree val = gimple_phi_arg_def (phi, n);
if (virtual_operand_p (lhs))
continue;
update_dep_bb (bb, val);
}
}
return hstate.end ();
}
/* Returns true if E1 and E2 have 2 successors, and if the successor flags
are inverse for the EDGE_TRUE_VALUE and EDGE_FALSE_VALUE flags, and equal for
the other edge flags. */
static bool
inverse_flags (const same_succ *e1, const same_succ *e2)
{
int f1a, f1b, f2a, f2b;
int mask = ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
if (e1->succ_flags.length () != 2)
return false;
f1a = e1->succ_flags[0];
f1b = e1->succ_flags[1];
f2a = e2->succ_flags[0];
f2b = e2->succ_flags[1];
if (f1a == f2a && f1b == f2b)
return false;
return (f1a & mask) == (f2a & mask) && (f1b & mask) == (f2b & mask);
}
/* Compares SAME_SUCCs E1 and E2. */
int
same_succ::equal (const same_succ *e1, const same_succ *e2)
{
unsigned int i, first1, first2;
gimple_stmt_iterator gsi1, gsi2;
gimple *s1, *s2;
basic_block bb1, bb2;
if (e1 == e2)
return 1;
if (e1->hashval != e2->hashval)
return 0;
if (e1->succ_flags.length () != e2->succ_flags.length ())
return 0;
if (!bitmap_equal_p (e1->succs, e2->succs))
return 0;
if (!inverse_flags (e1, e2))
{
for (i = 0; i < e1->succ_flags.length (); ++i)
if (e1->succ_flags[i] != e2->succ_flags[i])
return 0;
}
first1 = bitmap_first_set_bit (e1->bbs);
first2 = bitmap_first_set_bit (e2->bbs);
bb1 = BASIC_BLOCK_FOR_FN (cfun, first1);
bb2 = BASIC_BLOCK_FOR_FN (cfun, first2);
if (BB_SIZE (bb1) != BB_SIZE (bb2))
return 0;
if (bb1->loop_father != bb2->loop_father)
return 0;
gsi1 = gsi_start_nondebug_bb (bb1);
gsi2 = gsi_start_nondebug_bb (bb2);
gsi_advance_fw_nondebug_nonlocal (&gsi1);
gsi_advance_fw_nondebug_nonlocal (&gsi2);
while (!(gsi_end_p (gsi1) || gsi_end_p (gsi2)))
{
s1 = gsi_stmt (gsi1);
s2 = gsi_stmt (gsi2);
if (gimple_code (s1) != gimple_code (s2))
return 0;
if (is_gimple_call (s1) && !gimple_call_same_target_p (s1, s2))
return 0;
gsi_next_nondebug (&gsi1);
gsi_next_nondebug (&gsi2);
gsi_advance_fw_nondebug_nonlocal (&gsi1);
gsi_advance_fw_nondebug_nonlocal (&gsi2);
}
return 1;
}
/* Alloc and init a new SAME_SUCC. */
static same_succ *
same_succ_alloc (void)
{
same_succ *same = XNEW (struct same_succ);
same->bbs = BITMAP_ALLOC (NULL);
same->succs = BITMAP_ALLOC (NULL);
same->inverse = BITMAP_ALLOC (NULL);
same->succ_flags.create (10);
same->in_worklist = false;
return same;
}
/* Delete same_succ E. */
void
same_succ::remove (same_succ *e)
{
BITMAP_FREE (e->bbs);
BITMAP_FREE (e->succs);
BITMAP_FREE (e->inverse);
e->succ_flags.release ();
XDELETE (e);
}
/* Reset same_succ SAME. */
static void
same_succ_reset (same_succ *same)
{
bitmap_clear (same->bbs);
bitmap_clear (same->succs);
bitmap_clear (same->inverse);
same->succ_flags.truncate (0);
}
static hash_table<same_succ> *same_succ_htab;
/* Array that is used to store the edge flags for a successor. */
static int *same_succ_edge_flags;
/* Bitmap that is used to mark bbs that are recently deleted. */
static bitmap deleted_bbs;
/* Bitmap that is used to mark predecessors of bbs that are
deleted. */
static bitmap deleted_bb_preds;
/* Prints same_succ_htab to stderr. */
extern void debug_same_succ (void);
DEBUG_FUNCTION void
debug_same_succ ( void)
{
same_succ_htab->traverse <FILE *, ssa_same_succ_print_traverse> (stderr);
}
/* Vector of bbs to process. */
static vec<same_succ *> worklist;
/* Prints worklist to FILE. */
static void
print_worklist (FILE *file)
{
unsigned int i;
for (i = 0; i < worklist.length (); ++i)
same_succ_print (file, worklist[i]);
}
/* Adds SAME to worklist. */
static void
add_to_worklist (same_succ *same)
{
if (same->in_worklist)
return;
if (bitmap_count_bits (same->bbs) < 2)
return;
same->in_worklist = true;
worklist.safe_push (same);
}
/* Add BB to same_succ_htab. */
static void
find_same_succ_bb (basic_block bb, same_succ **same_p)
{
unsigned int j;
bitmap_iterator bj;
same_succ *same = *same_p;
same_succ **slot;
edge_iterator ei;
edge e;
if (bb == NULL)
return;
bitmap_set_bit (same->bbs, bb->index);
FOR_EACH_EDGE (e, ei, bb->succs)
{
int index = e->dest->index;
bitmap_set_bit (same->succs, index);
same_succ_edge_flags[index] = (e->flags & ~ignore_edge_flags);
}
EXECUTE_IF_SET_IN_BITMAP (same->succs, 0, j, bj)
same->succ_flags.safe_push (same_succ_edge_flags[j]);
same->hashval = same_succ_hash (same);
slot = same_succ_htab->find_slot_with_hash (same, same->hashval, INSERT);
if (*slot == NULL)
{
*slot = same;
BB_SAME_SUCC (bb) = same;
add_to_worklist (same);
*same_p = NULL;
}
else
{
bitmap_set_bit ((*slot)->bbs, bb->index);
BB_SAME_SUCC (bb) = *slot;
add_to_worklist (*slot);
if (inverse_flags (same, *slot))
bitmap_set_bit ((*slot)->inverse, bb->index);
same_succ_reset (same);
}
}
/* Find bbs with same successors. */
static void
find_same_succ (void)
{
same_succ *same = same_succ_alloc ();
basic_block bb;
FOR_EACH_BB_FN (bb, cfun)
{
find_same_succ_bb (bb, &same);
if (same == NULL)
same = same_succ_alloc ();
}
same_succ::remove (same);
}
/* Initializes worklist administration. */
static void
init_worklist (void)
{
alloc_aux_for_blocks (sizeof (struct aux_bb_info));
same_succ_htab = new hash_table<same_succ> (n_basic_blocks_for_fn (cfun));
same_succ_edge_flags = XCNEWVEC (int, last_basic_block_for_fn (cfun));
deleted_bbs = BITMAP_ALLOC (NULL);
deleted_bb_preds = BITMAP_ALLOC (NULL);
worklist.create (n_basic_blocks_for_fn (cfun));
find_same_succ ();
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file, "initial worklist:\n");
print_worklist (dump_file);
}
}
/* Deletes worklist administration. */
static void
delete_worklist (void)
{
free_aux_for_blocks ();
delete same_succ_htab;
same_succ_htab = NULL;
XDELETEVEC (same_succ_edge_flags);
same_succ_edge_flags = NULL;
BITMAP_FREE (deleted_bbs);
BITMAP_FREE (deleted_bb_preds);
worklist.release ();
}
/* Mark BB as deleted, and mark its predecessors. */
static void
mark_basic_block_deleted (basic_block bb)
{
edge e;
edge_iterator ei;
bitmap_set_bit (deleted_bbs, bb->index);
FOR_EACH_EDGE (e, ei, bb->preds)
bitmap_set_bit (deleted_bb_preds, e->src->index);
}
/* Removes BB from its corresponding same_succ. */
static void
same_succ_flush_bb (basic_block bb)
{
same_succ *same = BB_SAME_SUCC (bb);
if (! same)
return;
BB_SAME_SUCC (bb) = NULL;
if (bitmap_single_bit_set_p (same->bbs))
same_succ_htab->remove_elt_with_hash (same, same->hashval);
else
bitmap_clear_bit (same->bbs, bb->index);
}
/* Removes all bbs in BBS from their corresponding same_succ. */
static void
same_succ_flush_bbs (bitmap bbs)
{
unsigned int i;
bitmap_iterator bi;
EXECUTE_IF_SET_IN_BITMAP (bbs, 0, i, bi)
same_succ_flush_bb (BASIC_BLOCK_FOR_FN (cfun, i));
}
/* Release the last vdef in BB, either normal or phi result. */
static void
release_last_vdef (basic_block bb)
{
for (gimple_stmt_iterator i = gsi_last_bb (bb); !gsi_end_p (i);
gsi_prev_nondebug (&i))
{
gimple *stmt = gsi_stmt (i);
if (gimple_vdef (stmt) == NULL_TREE)
continue;
mark_virtual_operand_for_renaming (gimple_vdef (stmt));
return;
}
for (gphi_iterator i = gsi_start_phis (bb); !gsi_end_p (i);
gsi_next (&i))
{
gphi *phi = i.phi ();
tree res = gimple_phi_result (phi);
if (!virtual_operand_p (res))
continue;
mark_virtual_phi_result_for_renaming (phi);
return;
}
}
/* For deleted_bb_preds, find bbs with same successors. */
static void
update_worklist (void)
{
unsigned int i;
bitmap_iterator bi;
basic_block bb;
same_succ *same;
bitmap_and_compl_into (deleted_bb_preds, deleted_bbs);
bitmap_clear (deleted_bbs);
bitmap_clear_bit (deleted_bb_preds, ENTRY_BLOCK);
same_succ_flush_bbs (deleted_bb_preds);
same = same_succ_alloc ();
EXECUTE_IF_SET_IN_BITMAP (deleted_bb_preds, 0, i, bi)
{
bb = BASIC_BLOCK_FOR_FN (cfun, i);
gcc_assert (bb != NULL);
find_same_succ_bb (bb, &same);
if (same == NULL)
same = same_succ_alloc ();
}
same_succ::remove (same);
bitmap_clear (deleted_bb_preds);
}
/* Prints cluster C to FILE. */
static void
print_cluster (FILE *file, bb_cluster *c)
{
if (c == NULL)
return;
bitmap_print (file, c->bbs, "bbs:", "\n");
bitmap_print (file, c->preds, "preds:", "\n");
}
/* Prints cluster C to stderr. */
extern void debug_cluster (bb_cluster *);
DEBUG_FUNCTION void
debug_cluster (bb_cluster *c)
{
print_cluster (stderr, c);
}
/* Update C->rep_bb, given that BB is added to the cluster. */
static void
update_rep_bb (bb_cluster *c, basic_block bb)
{
/* Initial. */
if (c->rep_bb == NULL)
{
c->rep_bb = bb;
return;
}
/* Current needs no deps, keep it. */
if (BB_DEP_BB (c->rep_bb) == NULL)
return;
/* Bb needs no deps, change rep_bb. */
if (BB_DEP_BB (bb) == NULL)
{
c->rep_bb = bb;
return;
}
/* Bb needs last deps earlier than current, change rep_bb. A potential
problem with this, is that the first deps might also be earlier, which
would mean we prefer longer lifetimes for the deps. To be able to check
for this, we would have to trace BB_FIRST_DEP_BB as well, besides
BB_DEP_BB, which is really BB_LAST_DEP_BB.
The benefit of choosing the bb with last deps earlier, is that it can
potentially be used as replacement for more bbs. */
if (dominated_by_p (CDI_DOMINATORS, BB_DEP_BB (c->rep_bb), BB_DEP_BB (bb)))
c->rep_bb = bb;
}
/* Add BB to cluster C. Sets BB in C->bbs, and preds of BB in C->preds. */
static void
add_bb_to_cluster (bb_cluster *c, basic_block bb)
{
edge e;
edge_iterator ei;
bitmap_set_bit (c->bbs, bb->index);
FOR_EACH_EDGE (e, ei, bb->preds)
bitmap_set_bit (c->preds, e->src->index);
update_rep_bb (c, bb);
}
/* Allocate and init new cluster. */
static bb_cluster *
new_cluster (void)
{
bb_cluster *c;
c = XCNEW (bb_cluster);
c->bbs = BITMAP_ALLOC (NULL);
c->preds = BITMAP_ALLOC (NULL);
c->rep_bb = NULL;
return c;
}
/* Delete clusters. */
static void
delete_cluster (bb_cluster *c)