forked from riscvarchive/riscv-gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfgrtl.c
5235 lines (4448 loc) · 143 KB
/
cfgrtl.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 graph manipulation 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 low level functions to manipulate the CFG and analyze it
that are aware of the RTL intermediate language.
Available functionality:
- Basic CFG/RTL manipulation API documented in cfghooks.h
- CFG-aware instruction chain manipulation
delete_insn, delete_insn_chain
- Edge splitting and committing to edges
insert_insn_on_edge, commit_edge_insertions
- CFG updating after insn simplification
purge_dead_edges, purge_all_dead_edges
- CFG fixing after coarse manipulation
fixup_abnormal_edges
Functions not supposed for generic use:
- Infrastructure to determine quickly basic block for insn
compute_bb_for_insn, update_bb_for_insn, set_block_for_insn,
- Edge redirection with updating and optimizing of insn chain
block_label, tidy_fallthru_edge, force_nonfallthru */
#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 "insn-config.h"
#include "memmodel.h"
#include "emit-rtl.h"
#include "cfgrtl.h"
#include "cfganal.h"
#include "cfgbuild.h"
#include "cfgcleanup.h"
#include "bb-reorder.h"
#include "rtl-error.h"
#include "insn-attr.h"
#include "dojump.h"
#include "expr.h"
#include "cfgloop.h"
#include "tree-pass.h"
#include "print-rtl.h"
/* Disable warnings about missing quoting in GCC diagnostics. */
#if __GNUC__ >= 10
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wformat-diag"
#endif
/* Holds the interesting leading and trailing notes for the function.
Only applicable if the CFG is in cfglayout mode. */
static GTY(()) rtx_insn *cfg_layout_function_footer;
static GTY(()) rtx_insn *cfg_layout_function_header;
static rtx_insn *skip_insns_after_block (basic_block);
static void record_effective_endpoints (void);
static void fixup_reorder_chain (void);
void verify_insn_chain (void);
static void fixup_fallthru_exit_predecessor (void);
static int can_delete_note_p (const rtx_note *);
static int can_delete_label_p (const rtx_code_label *);
static basic_block rtl_split_edge (edge);
static bool rtl_move_block_after (basic_block, basic_block);
static int rtl_verify_flow_info (void);
static basic_block cfg_layout_split_block (basic_block, void *);
static edge cfg_layout_redirect_edge_and_branch (edge, basic_block);
static basic_block cfg_layout_redirect_edge_and_branch_force (edge, basic_block);
static void cfg_layout_delete_block (basic_block);
static void rtl_delete_block (basic_block);
static basic_block rtl_redirect_edge_and_branch_force (edge, basic_block);
static edge rtl_redirect_edge_and_branch (edge, basic_block);
static basic_block rtl_split_block (basic_block, void *);
static void rtl_dump_bb (FILE *, basic_block, int, dump_flags_t);
static int rtl_verify_flow_info_1 (void);
static void rtl_make_forwarder_block (edge);
/* Return true if NOTE is not one of the ones that must be kept paired,
so that we may simply delete it. */
static int
can_delete_note_p (const rtx_note *note)
{
switch (NOTE_KIND (note))
{
case NOTE_INSN_DELETED:
case NOTE_INSN_BASIC_BLOCK:
case NOTE_INSN_EPILOGUE_BEG:
return true;
default:
return false;
}
}
/* True if a given label can be deleted. */
static int
can_delete_label_p (const rtx_code_label *label)
{
return (!LABEL_PRESERVE_P (label)
/* User declared labels must be preserved. */
&& LABEL_NAME (label) == 0
&& !vec_safe_contains<rtx_insn *> (forced_labels,
const_cast<rtx_code_label *> (label)));
}
/* Delete INSN by patching it out. */
void
delete_insn (rtx_insn *insn)
{
rtx note;
bool really_delete = true;
if (LABEL_P (insn))
{
/* Some labels can't be directly removed from the INSN chain, as they
might be references via variables, constant pool etc.
Convert them to the special NOTE_INSN_DELETED_LABEL note. */
if (! can_delete_label_p (as_a <rtx_code_label *> (insn)))
{
const char *name = LABEL_NAME (insn);
basic_block bb = BLOCK_FOR_INSN (insn);
rtx_insn *bb_note = NEXT_INSN (insn);
really_delete = false;
PUT_CODE (insn, NOTE);
NOTE_KIND (insn) = NOTE_INSN_DELETED_LABEL;
NOTE_DELETED_LABEL_NAME (insn) = name;
/* If the note following the label starts a basic block, and the
label is a member of the same basic block, interchange the two. */
if (bb_note != NULL_RTX
&& NOTE_INSN_BASIC_BLOCK_P (bb_note)
&& bb != NULL
&& bb == BLOCK_FOR_INSN (bb_note))
{
reorder_insns_nobb (insn, insn, bb_note);
BB_HEAD (bb) = bb_note;
if (BB_END (bb) == bb_note)
BB_END (bb) = insn;
}
}
remove_node_from_insn_list (insn, &nonlocal_goto_handler_labels);
}
if (really_delete)
{
/* If this insn has already been deleted, something is very wrong. */
gcc_assert (!insn->deleted ());
if (INSN_P (insn))
df_insn_delete (insn);
remove_insn (insn);
insn->set_deleted ();
}
/* If deleting a jump, decrement the use count of the label. Deleting
the label itself should happen in the normal course of block merging. */
if (JUMP_P (insn))
{
if (JUMP_LABEL (insn)
&& LABEL_P (JUMP_LABEL (insn)))
LABEL_NUSES (JUMP_LABEL (insn))--;
/* If there are more targets, remove them too. */
while ((note
= find_reg_note (insn, REG_LABEL_TARGET, NULL_RTX)) != NULL_RTX
&& LABEL_P (XEXP (note, 0)))
{
LABEL_NUSES (XEXP (note, 0))--;
remove_note (insn, note);
}
}
/* Also if deleting any insn that references a label as an operand. */
while ((note = find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX)) != NULL_RTX
&& LABEL_P (XEXP (note, 0)))
{
LABEL_NUSES (XEXP (note, 0))--;
remove_note (insn, note);
}
if (rtx_jump_table_data *table = dyn_cast <rtx_jump_table_data *> (insn))
{
rtvec vec = table->get_labels ();
int len = GET_NUM_ELEM (vec);
int i;
for (i = 0; i < len; i++)
{
rtx label = XEXP (RTVEC_ELT (vec, i), 0);
/* When deleting code in bulk (e.g. removing many unreachable
blocks) we can delete a label that's a target of the vector
before deleting the vector itself. */
if (!NOTE_P (label))
LABEL_NUSES (label)--;
}
}
}
/* Like delete_insn but also purge dead edges from BB.
Return true if any edges are eliminated. */
bool
delete_insn_and_edges (rtx_insn *insn)
{
bool purge = false;
if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
{
basic_block bb = BLOCK_FOR_INSN (insn);
if (BB_END (bb) == insn)
purge = true;
else if (DEBUG_INSN_P (BB_END (bb)))
for (rtx_insn *dinsn = NEXT_INSN (insn);
DEBUG_INSN_P (dinsn); dinsn = NEXT_INSN (dinsn))
if (BB_END (bb) == dinsn)
{
purge = true;
break;
}
}
delete_insn (insn);
if (purge)
return purge_dead_edges (BLOCK_FOR_INSN (insn));
return false;
}
/* Unlink a chain of insns between START and FINISH, leaving notes
that must be paired. If CLEAR_BB is true, we set bb field for
insns that cannot be removed to NULL. */
void
delete_insn_chain (rtx start, rtx_insn *finish, bool clear_bb)
{
/* Unchain the insns one by one. It would be quicker to delete all of these
with a single unchaining, rather than one at a time, but we need to keep
the NOTE's. */
rtx_insn *current = finish;
while (1)
{
rtx_insn *prev = PREV_INSN (current);
if (NOTE_P (current) && !can_delete_note_p (as_a <rtx_note *> (current)))
;
else
delete_insn (current);
if (clear_bb && !current->deleted ())
set_block_for_insn (current, NULL);
if (current == start)
break;
current = prev;
}
}
/* Create a new basic block consisting of the instructions between HEAD and END
inclusive. This function is designed to allow fast BB construction - reuses
the note and basic block struct in BB_NOTE, if any and do not grow
BASIC_BLOCK chain and should be used directly only by CFG construction code.
END can be NULL in to create new empty basic block before HEAD. Both END
and HEAD can be NULL to create basic block at the end of INSN chain.
AFTER is the basic block we should be put after. */
basic_block
create_basic_block_structure (rtx_insn *head, rtx_insn *end, rtx_note *bb_note,
basic_block after)
{
basic_block bb;
if (bb_note
&& (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL
&& bb->aux == NULL)
{
/* If we found an existing note, thread it back onto the chain. */
rtx_insn *after;
if (LABEL_P (head))
after = head;
else
{
after = PREV_INSN (head);
head = bb_note;
}
if (after != bb_note && NEXT_INSN (after) != bb_note)
reorder_insns_nobb (bb_note, bb_note, after);
}
else
{
/* Otherwise we must create a note and a basic block structure. */
bb = alloc_block ();
init_rtl_bb_info (bb);
if (!head && !end)
head = end = bb_note
= emit_note_after (NOTE_INSN_BASIC_BLOCK, get_last_insn ());
else if (LABEL_P (head) && end)
{
bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head);
if (head == end)
end = bb_note;
}
else
{
bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head);
head = bb_note;
if (!end)
end = head;
}
NOTE_BASIC_BLOCK (bb_note) = bb;
}
/* Always include the bb note in the block. */
if (NEXT_INSN (end) == bb_note)
end = bb_note;
BB_HEAD (bb) = head;
BB_END (bb) = end;
bb->index = last_basic_block_for_fn (cfun)++;
bb->flags = BB_NEW | BB_RTL;
link_block (bb, after);
SET_BASIC_BLOCK_FOR_FN (cfun, bb->index, bb);
df_bb_refs_record (bb->index, false);
update_bb_for_insn (bb);
BB_SET_PARTITION (bb, BB_UNPARTITIONED);
/* Tag the block so that we know it has been used when considering
other basic block notes. */
bb->aux = bb;
return bb;
}
/* Create new basic block consisting of instructions in between HEAD and END
and place it to the BB chain after block AFTER. END can be NULL to
create a new empty basic block before HEAD. Both END and HEAD can be
NULL to create basic block at the end of INSN chain. */
static basic_block
rtl_create_basic_block (void *headp, void *endp, basic_block after)
{
rtx_insn *head = (rtx_insn *) headp;
rtx_insn *end = (rtx_insn *) endp;
basic_block bb;
/* Grow the basic block array if needed. */
if ((size_t) last_basic_block_for_fn (cfun)
>= basic_block_info_for_fn (cfun)->length ())
{
size_t new_size =
(last_basic_block_for_fn (cfun)
+ (last_basic_block_for_fn (cfun) + 3) / 4);
vec_safe_grow_cleared (basic_block_info_for_fn (cfun), new_size);
}
n_basic_blocks_for_fn (cfun)++;
bb = create_basic_block_structure (head, end, NULL, after);
bb->aux = NULL;
return bb;
}
static basic_block
cfg_layout_create_basic_block (void *head, void *end, basic_block after)
{
basic_block newbb = rtl_create_basic_block (head, end, after);
return newbb;
}
/* Delete the insns in a (non-live) block. We physically delete every
non-deleted-note insn, and update the flow graph appropriately.
Return nonzero if we deleted an exception handler. */
/* ??? Preserving all such notes strikes me as wrong. It would be nice
to post-process the stream to remove empty blocks, loops, ranges, etc. */
static void
rtl_delete_block (basic_block b)
{
rtx_insn *insn, *end;
/* If the head of this block is a CODE_LABEL, then it might be the
label for an exception handler which can't be reached. We need
to remove the label from the exception_handler_label list. */
insn = BB_HEAD (b);
end = get_last_bb_insn (b);
/* Selectively delete the entire chain. */
BB_HEAD (b) = NULL;
delete_insn_chain (insn, end, true);
if (dump_file)
fprintf (dump_file, "deleting block %d\n", b->index);
df_bb_delete (b->index);
}
/* Records the basic block struct in BLOCK_FOR_INSN for every insn. */
void
compute_bb_for_insn (void)
{
basic_block bb;
FOR_EACH_BB_FN (bb, cfun)
{
rtx_insn *end = BB_END (bb);
rtx_insn *insn;
for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
{
BLOCK_FOR_INSN (insn) = bb;
if (insn == end)
break;
}
}
}
/* Release the basic_block_for_insn array. */
unsigned int
free_bb_for_insn (void)
{
rtx_insn *insn;
for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
if (!BARRIER_P (insn))
BLOCK_FOR_INSN (insn) = NULL;
return 0;
}
namespace {
const pass_data pass_data_free_cfg =
{
RTL_PASS, /* type */
"*free_cfg", /* name */
OPTGROUP_NONE, /* optinfo_flags */
TV_NONE, /* tv_id */
0, /* properties_required */
0, /* properties_provided */
PROP_cfg, /* properties_destroyed */
0, /* todo_flags_start */
0, /* todo_flags_finish */
};
class pass_free_cfg : public rtl_opt_pass
{
public:
pass_free_cfg (gcc::context *ctxt)
: rtl_opt_pass (pass_data_free_cfg, ctxt)
{}
/* opt_pass methods: */
virtual unsigned int execute (function *);
}; // class pass_free_cfg
unsigned int
pass_free_cfg::execute (function *)
{
/* The resource.c machinery uses DF but the CFG isn't guaranteed to be
valid at that point so it would be too late to call df_analyze. */
if (DELAY_SLOTS && optimize > 0 && flag_delayed_branch)
{
df_note_add_problem ();
df_analyze ();
}
if (crtl->has_bb_partition)
insert_section_boundary_note ();
free_bb_for_insn ();
return 0;
}
} // anon namespace
rtl_opt_pass *
make_pass_free_cfg (gcc::context *ctxt)
{
return new pass_free_cfg (ctxt);
}
/* Return RTX to emit after when we want to emit code on the entry of function. */
rtx_insn *
entry_of_function (void)
{
return (n_basic_blocks_for_fn (cfun) > NUM_FIXED_BLOCKS ?
BB_HEAD (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) : get_insns ());
}
/* Emit INSN at the entry point of the function, ensuring that it is only
executed once per function. */
void
emit_insn_at_entry (rtx insn)
{
edge_iterator ei = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
edge e = ei_safe_edge (ei);
gcc_assert (e->flags & EDGE_FALLTHRU);
insert_insn_on_edge (insn, e);
commit_edge_insertions ();
}
/* Update BLOCK_FOR_INSN of insns between BEGIN and END
(or BARRIER if found) and notify df of the bb change.
The insn chain range is inclusive
(i.e. both BEGIN and END will be updated. */
static void
update_bb_for_insn_chain (rtx_insn *begin, rtx_insn *end, basic_block bb)
{
rtx_insn *insn;
end = NEXT_INSN (end);
for (insn = begin; insn != end; insn = NEXT_INSN (insn))
if (!BARRIER_P (insn))
df_insn_change_bb (insn, bb);
}
/* Update BLOCK_FOR_INSN of insns in BB to BB,
and notify df of the change. */
void
update_bb_for_insn (basic_block bb)
{
update_bb_for_insn_chain (BB_HEAD (bb), BB_END (bb), bb);
}
/* Like active_insn_p, except keep the return value use or clobber around
even after reload. */
static bool
flow_active_insn_p (const rtx_insn *insn)
{
if (active_insn_p (insn))
return true;
/* A clobber of the function return value exists for buggy
programs that fail to return a value. Its effect is to
keep the return value from being live across the entire
function. If we allow it to be skipped, we introduce the
possibility for register lifetime confusion.
Similarly, keep a USE of the function return value, otherwise
the USE is dropped and we could fail to thread jump if USE
appears on some paths and not on others, see PR90257. */
if ((GET_CODE (PATTERN (insn)) == CLOBBER
|| GET_CODE (PATTERN (insn)) == USE)
&& REG_P (XEXP (PATTERN (insn), 0))
&& REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))
return true;
return false;
}
/* Return true if the block has no effect and only forwards control flow to
its single destination. */
bool
contains_no_active_insn_p (const_basic_block bb)
{
rtx_insn *insn;
if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
|| bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
|| !single_succ_p (bb)
|| (single_succ_edge (bb)->flags & EDGE_FAKE) != 0)
return false;
for (insn = BB_HEAD (bb); insn != BB_END (bb); insn = NEXT_INSN (insn))
if (INSN_P (insn) && flow_active_insn_p (insn))
return false;
return (!INSN_P (insn)
|| (JUMP_P (insn) && simplejump_p (insn))
|| !flow_active_insn_p (insn));
}
/* Likewise, but protect loop latches, headers and preheaders. */
/* FIXME: Make this a cfg hook. */
bool
forwarder_block_p (const_basic_block bb)
{
if (!contains_no_active_insn_p (bb))
return false;
/* Protect loop latches, headers and preheaders. */
if (current_loops)
{
basic_block dest;
if (bb->loop_father->header == bb)
return false;
dest = EDGE_SUCC (bb, 0)->dest;
if (dest->loop_father->header == dest)
return false;
}
return true;
}
/* Return nonzero if we can reach target from src by falling through. */
/* FIXME: Make this a cfg hook, the result is only valid in cfgrtl mode. */
bool
can_fallthru (basic_block src, basic_block target)
{
rtx_insn *insn = BB_END (src);
rtx_insn *insn2;
edge e;
edge_iterator ei;
if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
return true;
if (src->next_bb != target)
return false;
/* ??? Later we may add code to move jump tables offline. */
if (tablejump_p (insn, NULL, NULL))
return false;
FOR_EACH_EDGE (e, ei, src->succs)
if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
&& e->flags & EDGE_FALLTHRU)
return false;
insn2 = BB_HEAD (target);
if (!active_insn_p (insn2))
insn2 = next_active_insn (insn2);
return next_active_insn (insn) == insn2;
}
/* Return nonzero if we could reach target from src by falling through,
if the target was made adjacent. If we already have a fall-through
edge to the exit block, we can't do that. */
static bool
could_fall_through (basic_block src, basic_block target)
{
edge e;
edge_iterator ei;
if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
return true;
FOR_EACH_EDGE (e, ei, src->succs)
if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
&& e->flags & EDGE_FALLTHRU)
return 0;
return true;
}
/* Return the NOTE_INSN_BASIC_BLOCK of BB. */
rtx_note *
bb_note (basic_block bb)
{
rtx_insn *note;
note = BB_HEAD (bb);
if (LABEL_P (note))
note = NEXT_INSN (note);
gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
return as_a <rtx_note *> (note);
}
/* Return the INSN immediately following the NOTE_INSN_BASIC_BLOCK
note associated with the BLOCK. */
static rtx_insn *
first_insn_after_basic_block_note (basic_block block)
{
rtx_insn *insn;
/* Get the first instruction in the block. */
insn = BB_HEAD (block);
if (insn == NULL_RTX)
return NULL;
if (LABEL_P (insn))
insn = NEXT_INSN (insn);
gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
return NEXT_INSN (insn);
}
/* Creates a new basic block just after basic block BB by splitting
everything after specified instruction INSNP. */
static basic_block
rtl_split_block (basic_block bb, void *insnp)
{
basic_block new_bb;
rtx_insn *insn = (rtx_insn *) insnp;
edge e;
edge_iterator ei;
if (!insn)
{
insn = first_insn_after_basic_block_note (bb);
if (insn)
{
rtx_insn *next = insn;
insn = PREV_INSN (insn);
/* If the block contains only debug insns, insn would have
been NULL in a non-debug compilation, and then we'd end
up emitting a DELETED note. For -fcompare-debug
stability, emit the note too. */
if (insn != BB_END (bb)
&& DEBUG_INSN_P (next)
&& DEBUG_INSN_P (BB_END (bb)))
{
while (next != BB_END (bb) && DEBUG_INSN_P (next))
next = NEXT_INSN (next);
if (next == BB_END (bb))
emit_note_after (NOTE_INSN_DELETED, next);
}
}
else
insn = get_last_insn ();
}
/* We probably should check type of the insn so that we do not create
inconsistent cfg. It is checked in verify_flow_info anyway, so do not
bother. */
if (insn == BB_END (bb))
emit_note_after (NOTE_INSN_DELETED, insn);
/* Create the new basic block. */
new_bb = create_basic_block (NEXT_INSN (insn), BB_END (bb), bb);
BB_COPY_PARTITION (new_bb, bb);
BB_END (bb) = insn;
/* Redirect the outgoing edges. */
new_bb->succs = bb->succs;
bb->succs = NULL;
FOR_EACH_EDGE (e, ei, new_bb->succs)
e->src = new_bb;
/* The new block starts off being dirty. */
df_set_bb_dirty (bb);
return new_bb;
}
/* Return true if the single edge between blocks A and B is the only place
in RTL which holds some unique locus. */
static bool
unique_locus_on_edge_between_p (basic_block a, basic_block b)
{
const location_t goto_locus = EDGE_SUCC (a, 0)->goto_locus;
rtx_insn *insn, *end;
if (LOCATION_LOCUS (goto_locus) == UNKNOWN_LOCATION)
return false;
/* First scan block A backward. */
insn = BB_END (a);
end = PREV_INSN (BB_HEAD (a));
while (insn != end && (!NONDEBUG_INSN_P (insn) || !INSN_HAS_LOCATION (insn)))
insn = PREV_INSN (insn);
if (insn != end && INSN_LOCATION (insn) == goto_locus)
return false;
/* Then scan block B forward. */
insn = BB_HEAD (b);
if (insn)
{
end = NEXT_INSN (BB_END (b));
while (insn != end && !NONDEBUG_INSN_P (insn))
insn = NEXT_INSN (insn);
if (insn != end && INSN_HAS_LOCATION (insn)
&& INSN_LOCATION (insn) == goto_locus)
return false;
}
return true;
}
/* If the single edge between blocks A and B is the only place in RTL which
holds some unique locus, emit a nop with that locus between the blocks. */
static void
emit_nop_for_unique_locus_between (basic_block a, basic_block b)
{
if (!unique_locus_on_edge_between_p (a, b))
return;
BB_END (a) = emit_insn_after_noloc (gen_nop (), BB_END (a), a);
INSN_LOCATION (BB_END (a)) = EDGE_SUCC (a, 0)->goto_locus;
}
/* Blocks A and B are to be merged into a single block A. The insns
are already contiguous. */
static void
rtl_merge_blocks (basic_block a, basic_block b)
{
/* If B is a forwarder block whose outgoing edge has no location, we'll
propagate the locus of the edge between A and B onto it. */
const bool forward_edge_locus
= (b->flags & BB_FORWARDER_BLOCK) != 0
&& LOCATION_LOCUS (EDGE_SUCC (b, 0)->goto_locus) == UNKNOWN_LOCATION;
rtx_insn *b_head = BB_HEAD (b), *b_end = BB_END (b), *a_end = BB_END (a);
rtx_insn *del_first = NULL, *del_last = NULL;
rtx_insn *b_debug_start = b_end, *b_debug_end = b_end;
int b_empty = 0;
if (dump_file)
fprintf (dump_file, "Merging block %d into block %d...\n", b->index,
a->index);
while (DEBUG_INSN_P (b_end))
b_end = PREV_INSN (b_debug_start = b_end);
/* If there was a CODE_LABEL beginning B, delete it. */
if (LABEL_P (b_head))
{
/* Detect basic blocks with nothing but a label. This can happen
in particular at the end of a function. */
if (b_head == b_end)
b_empty = 1;
del_first = del_last = b_head;
b_head = NEXT_INSN (b_head);
}
/* Delete the basic block note and handle blocks containing just that
note. */
if (NOTE_INSN_BASIC_BLOCK_P (b_head))
{
if (b_head == b_end)
b_empty = 1;
if (! del_last)
del_first = b_head;
del_last = b_head;
b_head = NEXT_INSN (b_head);
}
/* If there was a jump out of A, delete it. */
if (JUMP_P (a_end))
{
rtx_insn *prev;
for (prev = PREV_INSN (a_end); ; prev = PREV_INSN (prev))
if (!NOTE_P (prev)
|| NOTE_INSN_BASIC_BLOCK_P (prev)
|| prev == BB_HEAD (a))
break;
del_first = a_end;
/* If this was a conditional jump, we need to also delete
the insn that set cc0. */
if (HAVE_cc0 && only_sets_cc0_p (prev))
{
rtx_insn *tmp = prev;
prev = prev_nonnote_insn (prev);
if (!prev)
prev = BB_HEAD (a);
del_first = tmp;
}
a_end = PREV_INSN (del_first);
}
else if (BARRIER_P (NEXT_INSN (a_end)))
del_first = NEXT_INSN (a_end);
/* Delete everything marked above as well as crap that might be
hanging out between the two blocks. */
BB_END (a) = a_end;
BB_HEAD (b) = b_empty ? NULL : b_head;
delete_insn_chain (del_first, del_last, true);
/* If not optimizing, preserve the locus of the single edge between
blocks A and B if necessary by emitting a nop. */
if (!optimize
&& !forward_edge_locus
&& !DECL_IGNORED_P (current_function_decl))
{
emit_nop_for_unique_locus_between (a, b);
a_end = BB_END (a);
}
/* Reassociate the insns of B with A. */
if (!b_empty)
{
update_bb_for_insn_chain (a_end, b_debug_end, a);
BB_END (a) = b_debug_end;
BB_HEAD (b) = NULL;
}
else if (b_end != b_debug_end)
{
/* Move any deleted labels and other notes between the end of A
and the debug insns that make up B after the debug insns,
bringing the debug insns into A while keeping the notes after
the end of A. */
if (NEXT_INSN (a_end) != b_debug_start)
reorder_insns_nobb (NEXT_INSN (a_end), PREV_INSN (b_debug_start),
b_debug_end);
update_bb_for_insn_chain (b_debug_start, b_debug_end, a);
BB_END (a) = b_debug_end;
}
df_bb_delete (b->index);
if (forward_edge_locus)
EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus;
if (dump_file)
fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index);
}
/* Return true when block A and B can be merged. */
static bool
rtl_can_merge_blocks (basic_block a, basic_block b)
{
/* 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 false;
/* Protect the loop latches. */
if (current_loops && b->loop_father->latch == b)
return false;
/* There must be exactly one edge in between the blocks. */
return (single_succ_p (a)
&& single_succ (a) == b
&& single_pred_p (b)
&& a != b
/* Must be simple edge. */
&& !(single_succ_edge (a)->flags & EDGE_COMPLEX)
&& a->next_bb == b
&& a != ENTRY_BLOCK_PTR_FOR_FN (cfun)
&& b != EXIT_BLOCK_PTR_FOR_FN (cfun)
/* If the jump insn has side effects,
we can't kill the edge. */
&& (!JUMP_P (BB_END (a))
|| (reload_completed
? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
}
/* Return the label in the head of basic block BLOCK. Create one if it doesn't
exist. */
rtx_code_label *
block_label (basic_block block)
{
if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))