forked from riscvarchive/riscv-gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfgcleanup.c
3366 lines (2836 loc) · 95.4 KB
/
cfgcleanup.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
/* Control flow optimization code for GNU compiler.
Copyright (C) 1987-2020 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
/* This file contains optimizer of the control flow. The main entry point is
cleanup_cfg. Following optimizations are performed:
- Unreachable blocks removal
- Edge forwarding (edge to the forwarder block is forwarded to its
successor. Simplification of the branch instruction is performed by
underlying infrastructure so branch can be converted to simplejump or
eliminated).
- Cross jumping (tail merging)
- Conditional jump-around-simplejump simplification
- Basic block merging. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "target.h"
#include "rtl.h"
#include "tree.h"
#include "cfghooks.h"
#include "df.h"
#include "memmodel.h"
#include "tm_p.h"
#include "insn-config.h"
#include "emit-rtl.h"
#include "cselib.h"
#include "tree-pass.h"
#include "cfgloop.h"
#include "cfgrtl.h"
#include "cfganal.h"
#include "cfgbuild.h"
#include "cfgcleanup.h"
#include "dce.h"
#include "dbgcnt.h"
#include "rtl-iter.h"
#include "regs.h"
#include "function-abi.h"
#define FORWARDER_BLOCK_P(BB) ((BB)->flags & BB_FORWARDER_BLOCK)
/* Set to true when we are running first pass of try_optimize_cfg loop. */
static bool first_pass;
/* Set to true if crossjumps occurred in the latest run of try_optimize_cfg. */
static bool crossjumps_occurred;
/* Set to true if we couldn't run an optimization due to stale liveness
information; we should run df_analyze to enable more opportunities. */
static bool block_was_dirty;
static bool try_crossjump_to_edge (int, edge, edge, enum replace_direction);
static bool try_crossjump_bb (int, basic_block);
static bool outgoing_edges_match (int, basic_block, basic_block);
static enum replace_direction old_insns_match_p (int, rtx_insn *, rtx_insn *);
static void merge_blocks_move_predecessor_nojumps (basic_block, basic_block);
static void merge_blocks_move_successor_nojumps (basic_block, basic_block);
static bool try_optimize_cfg (int);
static bool try_simplify_condjump (basic_block);
static bool try_forward_edges (int, basic_block);
static edge thread_jump (edge, basic_block);
static bool mark_effect (rtx, bitmap);
static void notice_new_block (basic_block);
static void update_forwarder_flag (basic_block);
static void merge_memattrs (rtx, rtx);
/* Set flags for newly created block. */
static void
notice_new_block (basic_block bb)
{
if (!bb)
return;
if (forwarder_block_p (bb))
bb->flags |= BB_FORWARDER_BLOCK;
}
/* Recompute forwarder flag after block has been modified. */
static void
update_forwarder_flag (basic_block bb)
{
if (forwarder_block_p (bb))
bb->flags |= BB_FORWARDER_BLOCK;
else
bb->flags &= ~BB_FORWARDER_BLOCK;
}
/* Simplify a conditional jump around an unconditional jump.
Return true if something changed. */
static bool
try_simplify_condjump (basic_block cbranch_block)
{
basic_block jump_block, jump_dest_block, cbranch_dest_block;
edge cbranch_jump_edge, cbranch_fallthru_edge;
rtx_insn *cbranch_insn;
/* Verify that there are exactly two successors. */
if (EDGE_COUNT (cbranch_block->succs) != 2)
return false;
/* Verify that we've got a normal conditional branch at the end
of the block. */
cbranch_insn = BB_END (cbranch_block);
if (!any_condjump_p (cbranch_insn))
return false;
cbranch_fallthru_edge = FALLTHRU_EDGE (cbranch_block);
cbranch_jump_edge = BRANCH_EDGE (cbranch_block);
/* The next block must not have multiple predecessors, must not
be the last block in the function, and must contain just the
unconditional jump. */
jump_block = cbranch_fallthru_edge->dest;
if (!single_pred_p (jump_block)
|| jump_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
|| !FORWARDER_BLOCK_P (jump_block))
return false;
jump_dest_block = single_succ (jump_block);
/* If we are partitioning hot/cold basic blocks, we don't want to
mess up unconditional or indirect jumps that cross between hot
and cold sections.
Basic block partitioning may result in some jumps that appear to
be optimizable (or blocks that appear to be mergeable), but which really
must be left untouched (they are required to make it safely across
partition boundaries). See the comments at the top of
bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
if (BB_PARTITION (jump_block) != BB_PARTITION (jump_dest_block)
|| (cbranch_jump_edge->flags & EDGE_CROSSING))
return false;
/* The conditional branch must target the block after the
unconditional branch. */
cbranch_dest_block = cbranch_jump_edge->dest;
if (cbranch_dest_block == EXIT_BLOCK_PTR_FOR_FN (cfun)
|| jump_dest_block == EXIT_BLOCK_PTR_FOR_FN (cfun)
|| !can_fallthru (jump_block, cbranch_dest_block))
return false;
/* Invert the conditional branch. */
if (!invert_jump (as_a <rtx_jump_insn *> (cbranch_insn),
block_label (jump_dest_block), 0))
return false;
if (dump_file)
fprintf (dump_file, "Simplifying condjump %i around jump %i\n",
INSN_UID (cbranch_insn), INSN_UID (BB_END (jump_block)));
/* Success. Update the CFG to match. Note that after this point
the edge variable names appear backwards; the redirection is done
this way to preserve edge profile data. */
cbranch_jump_edge = redirect_edge_succ_nodup (cbranch_jump_edge,
cbranch_dest_block);
cbranch_fallthru_edge = redirect_edge_succ_nodup (cbranch_fallthru_edge,
jump_dest_block);
cbranch_jump_edge->flags |= EDGE_FALLTHRU;
cbranch_fallthru_edge->flags &= ~EDGE_FALLTHRU;
update_br_prob_note (cbranch_block);
/* Delete the block with the unconditional jump, and clean up the mess. */
delete_basic_block (jump_block);
tidy_fallthru_edge (cbranch_jump_edge);
update_forwarder_flag (cbranch_block);
return true;
}
/* Attempt to prove that operation is NOOP using CSElib or mark the effect
on register. Used by jump threading. */
static bool
mark_effect (rtx exp, regset nonequal)
{
rtx dest;
switch (GET_CODE (exp))
{
/* In case we do clobber the register, mark it as equal, as we know the
value is dead so it don't have to match. */
case CLOBBER:
dest = XEXP (exp, 0);
if (REG_P (dest))
bitmap_clear_range (nonequal, REGNO (dest), REG_NREGS (dest));
return false;
case SET:
if (rtx_equal_for_cselib_p (SET_DEST (exp), SET_SRC (exp)))
return false;
dest = SET_DEST (exp);
if (dest == pc_rtx)
return false;
if (!REG_P (dest))
return true;
bitmap_set_range (nonequal, REGNO (dest), REG_NREGS (dest));
return false;
default:
return false;
}
}
/* Return true if X contains a register in NONEQUAL. */
static bool
mentions_nonequal_regs (const_rtx x, regset nonequal)
{
subrtx_iterator::array_type array;
FOR_EACH_SUBRTX (iter, array, x, NONCONST)
{
const_rtx x = *iter;
if (REG_P (x))
{
unsigned int end_regno = END_REGNO (x);
for (unsigned int regno = REGNO (x); regno < end_regno; ++regno)
if (REGNO_REG_SET_P (nonequal, regno))
return true;
}
}
return false;
}
/* Attempt to prove that the basic block B will have no side effects and
always continues in the same edge if reached via E. Return the edge
if exist, NULL otherwise. */
static edge
thread_jump (edge e, basic_block b)
{
rtx set1, set2, cond1, cond2;
rtx_insn *insn;
enum rtx_code code1, code2, reversed_code2;
bool reverse1 = false;
unsigned i;
regset nonequal;
bool failed = false;
reg_set_iterator rsi;
/* Jump threading may cause fixup_partitions to introduce new crossing edges,
which is not allowed after reload. */
gcc_checking_assert (!reload_completed || !crtl->has_bb_partition);
if (b->flags & BB_NONTHREADABLE_BLOCK)
return NULL;
/* At the moment, we do handle only conditional jumps, but later we may
want to extend this code to tablejumps and others. */
if (EDGE_COUNT (e->src->succs) != 2)
return NULL;
if (EDGE_COUNT (b->succs) != 2)
{
b->flags |= BB_NONTHREADABLE_BLOCK;
return NULL;
}
/* Second branch must end with onlyjump, as we will eliminate the jump. */
if (!any_condjump_p (BB_END (e->src)))
return NULL;
if (!any_condjump_p (BB_END (b)) || !onlyjump_p (BB_END (b)))
{
b->flags |= BB_NONTHREADABLE_BLOCK;
return NULL;
}
set1 = pc_set (BB_END (e->src));
set2 = pc_set (BB_END (b));
if (((e->flags & EDGE_FALLTHRU) != 0)
!= (XEXP (SET_SRC (set1), 1) == pc_rtx))
reverse1 = true;
cond1 = XEXP (SET_SRC (set1), 0);
cond2 = XEXP (SET_SRC (set2), 0);
if (reverse1)
code1 = reversed_comparison_code (cond1, BB_END (e->src));
else
code1 = GET_CODE (cond1);
code2 = GET_CODE (cond2);
reversed_code2 = reversed_comparison_code (cond2, BB_END (b));
if (!comparison_dominates_p (code1, code2)
&& !comparison_dominates_p (code1, reversed_code2))
return NULL;
/* Ensure that the comparison operators are equivalent.
??? This is far too pessimistic. We should allow swapped operands,
different CCmodes, or for example comparisons for interval, that
dominate even when operands are not equivalent. */
if (!rtx_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
|| !rtx_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
return NULL;
/* Punt if BB_END (e->src) is doloop-like conditional jump that modifies
the registers used in cond1. */
if (modified_in_p (cond1, BB_END (e->src)))
return NULL;
/* Short circuit cases where block B contains some side effects, as we can't
safely bypass it. */
for (insn = NEXT_INSN (BB_HEAD (b)); insn != NEXT_INSN (BB_END (b));
insn = NEXT_INSN (insn))
if (INSN_P (insn) && side_effects_p (PATTERN (insn)))
{
b->flags |= BB_NONTHREADABLE_BLOCK;
return NULL;
}
cselib_init (0);
/* First process all values computed in the source basic block. */
for (insn = NEXT_INSN (BB_HEAD (e->src));
insn != NEXT_INSN (BB_END (e->src));
insn = NEXT_INSN (insn))
if (INSN_P (insn))
cselib_process_insn (insn);
nonequal = BITMAP_ALLOC (NULL);
CLEAR_REG_SET (nonequal);
/* Now assume that we've continued by the edge E to B and continue
processing as if it were same basic block.
Our goal is to prove that whole block is an NOOP. */
for (insn = NEXT_INSN (BB_HEAD (b));
insn != NEXT_INSN (BB_END (b)) && !failed;
insn = NEXT_INSN (insn))
{
/* cond2 must not mention any register that is not equal to the
former block. Check this before processing that instruction,
as BB_END (b) could contain also clobbers. */
if (insn == BB_END (b)
&& mentions_nonequal_regs (cond2, nonequal))
goto failed_exit;
if (INSN_P (insn))
{
rtx pat = PATTERN (insn);
if (GET_CODE (pat) == PARALLEL)
{
for (i = 0; i < (unsigned)XVECLEN (pat, 0); i++)
failed |= mark_effect (XVECEXP (pat, 0, i), nonequal);
}
else
failed |= mark_effect (pat, nonequal);
}
cselib_process_insn (insn);
}
/* Later we should clear nonequal of dead registers. So far we don't
have life information in cfg_cleanup. */
if (failed)
{
b->flags |= BB_NONTHREADABLE_BLOCK;
goto failed_exit;
}
EXECUTE_IF_SET_IN_REG_SET (nonequal, 0, i, rsi)
goto failed_exit;
BITMAP_FREE (nonequal);
cselib_finish ();
if ((comparison_dominates_p (code1, code2) != 0)
!= (XEXP (SET_SRC (set2), 1) == pc_rtx))
return BRANCH_EDGE (b);
else
return FALLTHRU_EDGE (b);
failed_exit:
BITMAP_FREE (nonequal);
cselib_finish ();
return NULL;
}
/* Attempt to forward edges leaving basic block B.
Return true if successful. */
static bool
try_forward_edges (int mode, basic_block b)
{
bool changed = false;
edge_iterator ei;
edge e, *threaded_edges = NULL;
for (ei = ei_start (b->succs); (e = ei_safe_edge (ei)); )
{
basic_block target, first;
location_t goto_locus;
int counter;
bool threaded = false;
int nthreaded_edges = 0;
bool may_thread = first_pass || (b->flags & BB_MODIFIED) != 0;
bool new_target_threaded = false;
/* Skip complex edges because we don't know how to update them.
Still handle fallthru edges, as we can succeed to forward fallthru
edge to the same place as the branch edge of conditional branch
and turn conditional branch to an unconditional branch. */
if (e->flags & EDGE_COMPLEX)
{
ei_next (&ei);
continue;
}
target = first = e->dest;
counter = NUM_FIXED_BLOCKS;
goto_locus = e->goto_locus;
while (counter < n_basic_blocks_for_fn (cfun))
{
basic_block new_target = NULL;
may_thread |= (target->flags & BB_MODIFIED) != 0;
if (FORWARDER_BLOCK_P (target)
&& single_succ (target) != EXIT_BLOCK_PTR_FOR_FN (cfun))
{
/* Bypass trivial infinite loops. */
new_target = single_succ (target);
if (target == new_target)
counter = n_basic_blocks_for_fn (cfun);
else if (!optimize)
{
/* When not optimizing, ensure that edges or forwarder
blocks with different locus are not optimized out. */
location_t new_locus = single_succ_edge (target)->goto_locus;
location_t locus = goto_locus;
if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION
&& LOCATION_LOCUS (locus) != UNKNOWN_LOCATION
&& new_locus != locus)
new_target = NULL;
else
{
if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION)
locus = new_locus;
rtx_insn *last = BB_END (target);
if (DEBUG_INSN_P (last))
last = prev_nondebug_insn (last);
if (last && INSN_P (last))
new_locus = INSN_LOCATION (last);
else
new_locus = UNKNOWN_LOCATION;
if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION
&& LOCATION_LOCUS (locus) != UNKNOWN_LOCATION
&& new_locus != locus)
new_target = NULL;
else
{
if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION)
locus = new_locus;
goto_locus = locus;
}
}
}
}
/* Allow to thread only over one edge at time to simplify updating
of probabilities. */
else if ((mode & CLEANUP_THREADING) && may_thread)
{
edge t = thread_jump (e, target);
if (t)
{
if (!threaded_edges)
threaded_edges = XNEWVEC (edge,
n_basic_blocks_for_fn (cfun));
else
{
int i;
/* Detect an infinite loop across blocks not
including the start block. */
for (i = 0; i < nthreaded_edges; ++i)
if (threaded_edges[i] == t)
break;
if (i < nthreaded_edges)
{
counter = n_basic_blocks_for_fn (cfun);
break;
}
}
/* Detect an infinite loop across the start block. */
if (t->dest == b)
break;
gcc_assert (nthreaded_edges
< (n_basic_blocks_for_fn (cfun)
- NUM_FIXED_BLOCKS));
threaded_edges[nthreaded_edges++] = t;
new_target = t->dest;
new_target_threaded = true;
}
}
if (!new_target)
break;
counter++;
/* Do not turn non-crossing jump to crossing. Depending on target
it may require different instruction pattern. */
if ((e->flags & EDGE_CROSSING)
|| BB_PARTITION (first) == BB_PARTITION (new_target))
{
target = new_target;
threaded |= new_target_threaded;
}
}
if (counter >= n_basic_blocks_for_fn (cfun))
{
if (dump_file)
fprintf (dump_file, "Infinite loop in BB %i.\n",
target->index);
}
else if (target == first)
; /* We didn't do anything. */
else
{
/* Save the values now, as the edge may get removed. */
profile_count edge_count = e->count ();
int n = 0;
e->goto_locus = goto_locus;
/* Don't force if target is exit block. */
if (threaded && target != EXIT_BLOCK_PTR_FOR_FN (cfun))
{
notice_new_block (redirect_edge_and_branch_force (e, target));
if (dump_file)
fprintf (dump_file, "Conditionals threaded.\n");
}
else if (!redirect_edge_and_branch (e, target))
{
if (dump_file)
fprintf (dump_file,
"Forwarding edge %i->%i to %i failed.\n",
b->index, e->dest->index, target->index);
ei_next (&ei);
continue;
}
/* We successfully forwarded the edge. Now update profile
data: for each edge we traversed in the chain, remove
the original edge's execution count. */
do
{
edge t;
if (!single_succ_p (first))
{
gcc_assert (n < nthreaded_edges);
t = threaded_edges [n++];
gcc_assert (t->src == first);
update_bb_profile_for_threading (first, edge_count, t);
update_br_prob_note (first);
}
else
{
first->count -= edge_count;
/* It is possible that as the result of
threading we've removed edge as it is
threaded to the fallthru edge. Avoid
getting out of sync. */
if (n < nthreaded_edges
&& first == threaded_edges [n]->src)
n++;
t = single_succ_edge (first);
}
first = t->dest;
}
while (first != target);
changed = true;
continue;
}
ei_next (&ei);
}
free (threaded_edges);
return changed;
}
/* Blocks A and B are to be merged into a single block. A has no incoming
fallthru edge, so it can be moved before B without adding or modifying
any jumps (aside from the jump from A to B). */
static void
merge_blocks_move_predecessor_nojumps (basic_block a, basic_block b)
{
rtx_insn *barrier;
/* If we are partitioning hot/cold basic blocks, we don't want to
mess up unconditional or indirect jumps that cross between hot
and cold sections.
Basic block partitioning may result in some jumps that appear to
be optimizable (or blocks that appear to be mergeable), but which really
must be left untouched (they are required to make it safely across
partition boundaries). See the comments at the top of
bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
if (BB_PARTITION (a) != BB_PARTITION (b))
return;
barrier = next_nonnote_insn (BB_END (a));
gcc_assert (BARRIER_P (barrier));
delete_insn (barrier);
/* Scramble the insn chain. */
if (BB_END (a) != PREV_INSN (BB_HEAD (b)))
reorder_insns_nobb (BB_HEAD (a), BB_END (a), PREV_INSN (BB_HEAD (b)));
df_set_bb_dirty (a);
if (dump_file)
fprintf (dump_file, "Moved block %d before %d and merged.\n",
a->index, b->index);
/* Swap the records for the two blocks around. */
unlink_block (a);
link_block (a, b->prev_bb);
/* Now blocks A and B are contiguous. Merge them. */
merge_blocks (a, b);
}
/* Blocks A and B are to be merged into a single block. B has no outgoing
fallthru edge, so it can be moved after A without adding or modifying
any jumps (aside from the jump from A to B). */
static void
merge_blocks_move_successor_nojumps (basic_block a, basic_block b)
{
rtx_insn *barrier, *real_b_end;
rtx_insn *label;
rtx_jump_table_data *table;
/* If we are partitioning hot/cold basic blocks, we don't want to
mess up unconditional or indirect jumps that cross between hot
and cold sections.
Basic block partitioning may result in some jumps that appear to
be optimizable (or blocks that appear to be mergeable), but which really
must be left untouched (they are required to make it safely across
partition boundaries). See the comments at the top of
bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
if (BB_PARTITION (a) != BB_PARTITION (b))
return;
real_b_end = BB_END (b);
/* If there is a jump table following block B temporarily add the jump table
to block B so that it will also be moved to the correct location. */
if (tablejump_p (BB_END (b), &label, &table)
&& prev_active_insn (label) == BB_END (b))
{
BB_END (b) = table;
}
/* There had better have been a barrier there. Delete it. */
barrier = NEXT_INSN (BB_END (b));
if (barrier && BARRIER_P (barrier))
delete_insn (barrier);
/* Scramble the insn chain. */
reorder_insns_nobb (BB_HEAD (b), BB_END (b), BB_END (a));
/* Restore the real end of b. */
BB_END (b) = real_b_end;
if (dump_file)
fprintf (dump_file, "Moved block %d after %d and merged.\n",
b->index, a->index);
/* Now blocks A and B are contiguous. Merge them. */
merge_blocks (a, b);
}
/* Attempt to merge basic blocks that are potentially non-adjacent.
Return NULL iff the attempt failed, otherwise return basic block
where cleanup_cfg should continue. Because the merging commonly
moves basic block away or introduces another optimization
possibility, return basic block just before B so cleanup_cfg don't
need to iterate.
It may be good idea to return basic block before C in the case
C has been moved after B and originally appeared earlier in the
insn sequence, but we have no information available about the
relative ordering of these two. Hopefully it is not too common. */
static basic_block
merge_blocks_move (edge e, basic_block b, basic_block c, int mode)
{
basic_block next;
/* If we are partitioning hot/cold basic blocks, we don't want to
mess up unconditional or indirect jumps that cross between hot
and cold sections.
Basic block partitioning may result in some jumps that appear to
be optimizable (or blocks that appear to be mergeable), but which really
must be left untouched (they are required to make it safely across
partition boundaries). See the comments at the top of
bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
if (BB_PARTITION (b) != BB_PARTITION (c))
return NULL;
/* If B has a fallthru edge to C, no need to move anything. */
if (e->flags & EDGE_FALLTHRU)
{
int b_index = b->index, c_index = c->index;
/* Protect the loop latches. */
if (current_loops && c->loop_father->latch == c)
return NULL;
merge_blocks (b, c);
update_forwarder_flag (b);
if (dump_file)
fprintf (dump_file, "Merged %d and %d without moving.\n",
b_index, c_index);
return b->prev_bb == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? b : b->prev_bb;
}
/* Otherwise we will need to move code around. Do that only if expensive
transformations are allowed. */
else if (mode & CLEANUP_EXPENSIVE)
{
edge tmp_edge, b_fallthru_edge;
bool c_has_outgoing_fallthru;
bool b_has_incoming_fallthru;
/* Avoid overactive code motion, as the forwarder blocks should be
eliminated by edge redirection instead. One exception might have
been if B is a forwarder block and C has no fallthru edge, but
that should be cleaned up by bb-reorder instead. */
if (FORWARDER_BLOCK_P (b) || FORWARDER_BLOCK_P (c))
return NULL;
/* We must make sure to not munge nesting of lexical blocks,
and loop notes. This is done by squeezing out all the notes
and leaving them there to lie. Not ideal, but functional. */
tmp_edge = find_fallthru_edge (c->succs);
c_has_outgoing_fallthru = (tmp_edge != NULL);
tmp_edge = find_fallthru_edge (b->preds);
b_has_incoming_fallthru = (tmp_edge != NULL);
b_fallthru_edge = tmp_edge;
next = b->prev_bb;
if (next == c)
next = next->prev_bb;
/* Otherwise, we're going to try to move C after B. If C does
not have an outgoing fallthru, then it can be moved
immediately after B without introducing or modifying jumps. */
if (! c_has_outgoing_fallthru)
{
merge_blocks_move_successor_nojumps (b, c);
return next == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? next->next_bb : next;
}
/* If B does not have an incoming fallthru, then it can be moved
immediately before C without introducing or modifying jumps.
C cannot be the first block, so we do not have to worry about
accessing a non-existent block. */
if (b_has_incoming_fallthru)
{
basic_block bb;
if (b_fallthru_edge->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
return NULL;
bb = force_nonfallthru (b_fallthru_edge);
if (bb)
notice_new_block (bb);
}
merge_blocks_move_predecessor_nojumps (b, c);
return next == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? next->next_bb : next;
}
return NULL;
}
/* Removes the memory attributes of MEM expression
if they are not equal. */
static void
merge_memattrs (rtx x, rtx y)
{
int i;
int j;
enum rtx_code code;
const char *fmt;
if (x == y)
return;
if (x == 0 || y == 0)
return;
code = GET_CODE (x);
if (code != GET_CODE (y))
return;
if (GET_MODE (x) != GET_MODE (y))
return;
if (code == MEM && !mem_attrs_eq_p (MEM_ATTRS (x), MEM_ATTRS (y)))
{
if (! MEM_ATTRS (x))
MEM_ATTRS (y) = 0;
else if (! MEM_ATTRS (y))
MEM_ATTRS (x) = 0;
else
{
if (MEM_ALIAS_SET (x) != MEM_ALIAS_SET (y))
{
set_mem_alias_set (x, 0);
set_mem_alias_set (y, 0);
}
if (! mem_expr_equal_p (MEM_EXPR (x), MEM_EXPR (y)))
{
set_mem_expr (x, 0);
set_mem_expr (y, 0);
clear_mem_offset (x);
clear_mem_offset (y);
}
else if (MEM_OFFSET_KNOWN_P (x) != MEM_OFFSET_KNOWN_P (y)
|| (MEM_OFFSET_KNOWN_P (x)
&& maybe_ne (MEM_OFFSET (x), MEM_OFFSET (y))))
{
clear_mem_offset (x);
clear_mem_offset (y);
}
if (!MEM_SIZE_KNOWN_P (x))
clear_mem_size (y);
else if (!MEM_SIZE_KNOWN_P (y))
clear_mem_size (x);
else if (known_le (MEM_SIZE (x), MEM_SIZE (y)))
set_mem_size (x, MEM_SIZE (y));
else if (known_le (MEM_SIZE (y), MEM_SIZE (x)))
set_mem_size (y, MEM_SIZE (x));
else
{
/* The sizes aren't ordered, so we can't merge them. */
clear_mem_size (x);
clear_mem_size (y);
}
set_mem_align (x, MIN (MEM_ALIGN (x), MEM_ALIGN (y)));
set_mem_align (y, MEM_ALIGN (x));
}
}
if (code == MEM)
{
if (MEM_READONLY_P (x) != MEM_READONLY_P (y))
{
MEM_READONLY_P (x) = 0;
MEM_READONLY_P (y) = 0;
}
if (MEM_NOTRAP_P (x) != MEM_NOTRAP_P (y))
{
MEM_NOTRAP_P (x) = 0;
MEM_NOTRAP_P (y) = 0;
}
if (MEM_VOLATILE_P (x) != MEM_VOLATILE_P (y))
{
MEM_VOLATILE_P (x) = 1;
MEM_VOLATILE_P (y) = 1;
}
}
fmt = GET_RTX_FORMAT (code);
for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
{
switch (fmt[i])
{
case 'E':
/* Two vectors must have the same length. */
if (XVECLEN (x, i) != XVECLEN (y, i))
return;
for (j = 0; j < XVECLEN (x, i); j++)
merge_memattrs (XVECEXP (x, i, j), XVECEXP (y, i, j));
break;
case 'e':
merge_memattrs (XEXP (x, i), XEXP (y, i));
}
}
return;
}
/* Checks if patterns P1 and P2 are equivalent, apart from the possibly
different single sets S1 and S2. */
static bool
equal_different_set_p (rtx p1, rtx s1, rtx p2, rtx s2)
{
int i;
rtx e1, e2;
if (p1 == s1 && p2 == s2)
return true;
if (GET_CODE (p1) != PARALLEL || GET_CODE (p2) != PARALLEL)
return false;
if (XVECLEN (p1, 0) != XVECLEN (p2, 0))
return false;
for (i = 0; i < XVECLEN (p1, 0); i++)
{
e1 = XVECEXP (p1, 0, i);
e2 = XVECEXP (p2, 0, i);
if (e1 == s1 && e2 == s2)
continue;
if (reload_completed
? rtx_renumbered_equal_p (e1, e2) : rtx_equal_p (e1, e2))
continue;
return false;
}
return true;
}
/* NOTE1 is the REG_EQUAL note, if any, attached to an insn
that is a single_set with a SET_SRC of SRC1. Similarly
for NOTE2/SRC2.
So effectively NOTE1/NOTE2 are an alternate form of
SRC1/SRC2 respectively.
Return nonzero if SRC1 or NOTE1 has the same constant
integer value as SRC2 or NOTE2. Else return zero. */
static int
values_equal_p (rtx note1, rtx note2, rtx src1, rtx src2)
{
if (note1
&& note2
&& CONST_INT_P (XEXP (note1, 0))
&& rtx_equal_p (XEXP (note1, 0), XEXP (note2, 0)))
return 1;
if (!note1
&& !note2
&& CONST_INT_P (src1)
&& CONST_INT_P (src2)
&& rtx_equal_p (src1, src2))
return 1;
if (note1
&& CONST_INT_P (src2)
&& rtx_equal_p (XEXP (note1, 0), src2))