forked from illumos/gcc
/
cfgloopmanip.c
1734 lines (1457 loc) · 52.3 KB
/
cfgloopmanip.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
/* Loop manipulation code for GNU compiler.
Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
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/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "rtl.h"
#include "hard-reg-set.h"
#include "obstack.h"
#include "basic-block.h"
#include "cfgloop.h"
#include "cfglayout.h"
#include "cfghooks.h"
#include "output.h"
#include "tree-flow.h"
static void copy_loops_to (struct loop **, int,
struct loop *);
static void loop_redirect_edge (edge, basic_block);
static void remove_bbs (basic_block *, int);
static bool rpe_enum_p (const_basic_block, const void *);
static int find_path (edge, basic_block **);
static void fix_loop_placements (struct loop *, bool *);
static bool fix_bb_placement (basic_block);
static void fix_bb_placements (basic_block, bool *);
static void unloop (struct loop *, bool *);
#define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
/* Checks whether basic block BB is dominated by DATA. */
static bool
rpe_enum_p (const_basic_block bb, const void *data)
{
return dominated_by_p (CDI_DOMINATORS, bb, (const_basic_block) data);
}
/* Remove basic blocks BBS. NBBS is the number of the basic blocks. */
static void
remove_bbs (basic_block *bbs, int nbbs)
{
int i;
for (i = 0; i < nbbs; i++)
delete_basic_block (bbs[i]);
}
/* Find path -- i.e. the basic blocks dominated by edge E and put them
into array BBS, that will be allocated large enough to contain them.
E->dest must have exactly one predecessor for this to work (it is
easy to achieve and we do not put it here because we do not want to
alter anything by this function). The number of basic blocks in the
path is returned. */
static int
find_path (edge e, basic_block **bbs)
{
gcc_assert (EDGE_COUNT (e->dest->preds) <= 1);
/* Find bbs in the path. */
*bbs = XCNEWVEC (basic_block, n_basic_blocks);
return dfs_enumerate_from (e->dest, 0, rpe_enum_p, *bbs,
n_basic_blocks, e->dest);
}
/* Fix placement of basic block BB inside loop hierarchy --
Let L be a loop to that BB belongs. Then every successor of BB must either
1) belong to some superloop of loop L, or
2) be a header of loop K such that K->outer is superloop of L
Returns true if we had to move BB into other loop to enforce this condition,
false if the placement of BB was already correct (provided that placements
of its successors are correct). */
static bool
fix_bb_placement (basic_block bb)
{
edge e;
edge_iterator ei;
struct loop *loop = current_loops->tree_root, *act;
FOR_EACH_EDGE (e, ei, bb->succs)
{
if (e->dest == EXIT_BLOCK_PTR)
continue;
act = e->dest->loop_father;
if (act->header == e->dest)
act = loop_outer (act);
if (flow_loop_nested_p (loop, act))
loop = act;
}
if (loop == bb->loop_father)
return false;
remove_bb_from_loops (bb);
add_bb_to_loop (bb, loop);
return true;
}
/* Fix placement of LOOP inside loop tree, i.e. find the innermost superloop
of LOOP to that leads at least one exit edge of LOOP, and set it
as the immediate superloop of LOOP. Return true if the immediate superloop
of LOOP changed. */
static bool
fix_loop_placement (struct loop *loop)
{
unsigned i;
edge e;
VEC (edge, heap) *exits = get_loop_exit_edges (loop);
struct loop *father = current_loops->tree_root, *act;
bool ret = false;
FOR_EACH_VEC_ELT (edge, exits, i, e)
{
act = find_common_loop (loop, e->dest->loop_father);
if (flow_loop_nested_p (father, act))
father = act;
}
if (father != loop_outer (loop))
{
for (act = loop_outer (loop); act != father; act = loop_outer (act))
act->num_nodes -= loop->num_nodes;
flow_loop_tree_node_remove (loop);
flow_loop_tree_node_add (father, loop);
/* The exit edges of LOOP no longer exits its original immediate
superloops; remove them from the appropriate exit lists. */
FOR_EACH_VEC_ELT (edge, exits, i, e)
rescan_loop_exit (e, false, false);
ret = true;
}
VEC_free (edge, heap, exits);
return ret;
}
/* Fix placements of basic blocks inside loop hierarchy stored in loops; i.e.
enforce condition condition stated in description of fix_bb_placement. We
start from basic block FROM that had some of its successors removed, so that
his placement no longer has to be correct, and iteratively fix placement of
its predecessors that may change if placement of FROM changed. Also fix
placement of subloops of FROM->loop_father, that might also be altered due
to this change; the condition for them is similar, except that instead of
successors we consider edges coming out of the loops.
If the changes may invalidate the information about irreducible regions,
IRRED_INVALIDATED is set to true. */
static void
fix_bb_placements (basic_block from,
bool *irred_invalidated)
{
sbitmap in_queue;
basic_block *queue, *qtop, *qbeg, *qend;
struct loop *base_loop, *target_loop;
edge e;
/* We pass through blocks back-reachable from FROM, testing whether some
of their successors moved to outer loop. It may be necessary to
iterate several times, but it is finite, as we stop unless we move
the basic block up the loop structure. The whole story is a bit
more complicated due to presence of subloops, those are moved using
fix_loop_placement. */
base_loop = from->loop_father;
/* If we are already in the outermost loop, the basic blocks cannot be moved
outside of it. If FROM is the header of the base loop, it cannot be moved
outside of it, either. In both cases, we can end now. */
if (base_loop == current_loops->tree_root
|| from == base_loop->header)
return;
in_queue = sbitmap_alloc (last_basic_block);
sbitmap_zero (in_queue);
SET_BIT (in_queue, from->index);
/* Prevent us from going out of the base_loop. */
SET_BIT (in_queue, base_loop->header->index);
queue = XNEWVEC (basic_block, base_loop->num_nodes + 1);
qtop = queue + base_loop->num_nodes + 1;
qbeg = queue;
qend = queue + 1;
*qbeg = from;
while (qbeg != qend)
{
edge_iterator ei;
from = *qbeg;
qbeg++;
if (qbeg == qtop)
qbeg = queue;
RESET_BIT (in_queue, from->index);
if (from->loop_father->header == from)
{
/* Subloop header, maybe move the loop upward. */
if (!fix_loop_placement (from->loop_father))
continue;
target_loop = loop_outer (from->loop_father);
}
else
{
/* Ordinary basic block. */
if (!fix_bb_placement (from))
continue;
target_loop = from->loop_father;
}
FOR_EACH_EDGE (e, ei, from->succs)
{
if (e->flags & EDGE_IRREDUCIBLE_LOOP)
*irred_invalidated = true;
}
/* Something has changed, insert predecessors into queue. */
FOR_EACH_EDGE (e, ei, from->preds)
{
basic_block pred = e->src;
struct loop *nca;
if (e->flags & EDGE_IRREDUCIBLE_LOOP)
*irred_invalidated = true;
if (TEST_BIT (in_queue, pred->index))
continue;
/* If it is subloop, then it either was not moved, or
the path up the loop tree from base_loop do not contain
it. */
nca = find_common_loop (pred->loop_father, base_loop);
if (pred->loop_father != base_loop
&& (nca == base_loop
|| nca != pred->loop_father))
pred = pred->loop_father->header;
else if (!flow_loop_nested_p (target_loop, pred->loop_father))
{
/* If PRED is already higher in the loop hierarchy than the
TARGET_LOOP to that we moved FROM, the change of the position
of FROM does not affect the position of PRED, so there is no
point in processing it. */
continue;
}
if (TEST_BIT (in_queue, pred->index))
continue;
/* Schedule the basic block. */
*qend = pred;
qend++;
if (qend == qtop)
qend = queue;
SET_BIT (in_queue, pred->index);
}
}
free (in_queue);
free (queue);
}
/* Removes path beginning at edge E, i.e. remove basic blocks dominated by E
and update loop structures and dominators. Return true if we were able
to remove the path, false otherwise (and nothing is affected then). */
bool
remove_path (edge e)
{
edge ae;
basic_block *rem_bbs, *bord_bbs, from, bb;
VEC (basic_block, heap) *dom_bbs;
int i, nrem, n_bord_bbs;
sbitmap seen;
bool irred_invalidated = false;
edge_iterator ei;
struct loop *l, *f;
if (!can_remove_branch_p (e))
return false;
/* Keep track of whether we need to update information about irreducible
regions. This is the case if the removed area is a part of the
irreducible region, or if the set of basic blocks that belong to a loop
that is inside an irreducible region is changed, or if such a loop is
removed. */
if (e->flags & EDGE_IRREDUCIBLE_LOOP)
irred_invalidated = true;
/* We need to check whether basic blocks are dominated by the edge
e, but we only have basic block dominators. This is easy to
fix -- when e->dest has exactly one predecessor, this corresponds
to blocks dominated by e->dest, if not, split the edge. */
if (!single_pred_p (e->dest))
e = single_pred_edge (split_edge (e));
/* It may happen that by removing path we remove one or more loops
we belong to. In this case first unloop the loops, then proceed
normally. We may assume that e->dest is not a header of any loop,
as it now has exactly one predecessor. */
for (l = e->src->loop_father; loop_outer (l); l = f)
{
f = loop_outer (l);
if (dominated_by_p (CDI_DOMINATORS, l->latch, e->dest))
unloop (l, &irred_invalidated);
}
/* Identify the path. */
nrem = find_path (e, &rem_bbs);
n_bord_bbs = 0;
bord_bbs = XCNEWVEC (basic_block, n_basic_blocks);
seen = sbitmap_alloc (last_basic_block);
sbitmap_zero (seen);
/* Find "border" hexes -- i.e. those with predecessor in removed path. */
for (i = 0; i < nrem; i++)
SET_BIT (seen, rem_bbs[i]->index);
if (!irred_invalidated)
FOR_EACH_EDGE (ae, ei, e->src->succs)
if (ae != e && ae->dest != EXIT_BLOCK_PTR && !TEST_BIT (seen, ae->dest->index)
&& ae->flags & EDGE_IRREDUCIBLE_LOOP)
irred_invalidated = true;
for (i = 0; i < nrem; i++)
{
bb = rem_bbs[i];
FOR_EACH_EDGE (ae, ei, rem_bbs[i]->succs)
if (ae->dest != EXIT_BLOCK_PTR && !TEST_BIT (seen, ae->dest->index))
{
SET_BIT (seen, ae->dest->index);
bord_bbs[n_bord_bbs++] = ae->dest;
if (ae->flags & EDGE_IRREDUCIBLE_LOOP)
irred_invalidated = true;
}
}
/* Remove the path. */
from = e->src;
remove_branch (e);
dom_bbs = NULL;
/* Cancel loops contained in the path. */
for (i = 0; i < nrem; i++)
if (rem_bbs[i]->loop_father->header == rem_bbs[i])
cancel_loop_tree (rem_bbs[i]->loop_father);
remove_bbs (rem_bbs, nrem);
free (rem_bbs);
/* Find blocks whose dominators may be affected. */
sbitmap_zero (seen);
for (i = 0; i < n_bord_bbs; i++)
{
basic_block ldom;
bb = get_immediate_dominator (CDI_DOMINATORS, bord_bbs[i]);
if (TEST_BIT (seen, bb->index))
continue;
SET_BIT (seen, bb->index);
for (ldom = first_dom_son (CDI_DOMINATORS, bb);
ldom;
ldom = next_dom_son (CDI_DOMINATORS, ldom))
if (!dominated_by_p (CDI_DOMINATORS, from, ldom))
VEC_safe_push (basic_block, heap, dom_bbs, ldom);
}
free (seen);
/* Recount dominators. */
iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, true);
VEC_free (basic_block, heap, dom_bbs);
free (bord_bbs);
/* Fix placements of basic blocks inside loops and the placement of
loops in the loop tree. */
fix_bb_placements (from, &irred_invalidated);
fix_loop_placements (from->loop_father, &irred_invalidated);
if (irred_invalidated
&& loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS))
mark_irreducible_loops ();
return true;
}
/* Creates place for a new LOOP in loops structure. */
static void
place_new_loop (struct loop *loop)
{
loop->num = number_of_loops ();
VEC_safe_push (loop_p, gc, current_loops->larray, loop);
}
/* Given LOOP structure with filled header and latch, find the body of the
corresponding loop and add it to loops tree. Insert the LOOP as a son of
outer. */
void
add_loop (struct loop *loop, struct loop *outer)
{
basic_block *bbs;
int i, n;
struct loop *subloop;
edge e;
edge_iterator ei;
/* Add it to loop structure. */
place_new_loop (loop);
flow_loop_tree_node_add (outer, loop);
/* Find its nodes. */
bbs = XNEWVEC (basic_block, n_basic_blocks);
n = get_loop_body_with_size (loop, bbs, n_basic_blocks);
for (i = 0; i < n; i++)
{
if (bbs[i]->loop_father == outer)
{
remove_bb_from_loops (bbs[i]);
add_bb_to_loop (bbs[i], loop);
continue;
}
loop->num_nodes++;
/* If we find a direct subloop of OUTER, move it to LOOP. */
subloop = bbs[i]->loop_father;
if (loop_outer (subloop) == outer
&& subloop->header == bbs[i])
{
flow_loop_tree_node_remove (subloop);
flow_loop_tree_node_add (loop, subloop);
}
}
/* Update the information about loop exit edges. */
for (i = 0; i < n; i++)
{
FOR_EACH_EDGE (e, ei, bbs[i]->succs)
{
rescan_loop_exit (e, false, false);
}
}
free (bbs);
}
/* Multiply all frequencies in LOOP by NUM/DEN. */
void
scale_loop_frequencies (struct loop *loop, int num, int den)
{
basic_block *bbs;
bbs = get_loop_body (loop);
scale_bbs_frequencies_int (bbs, loop->num_nodes, num, den);
free (bbs);
}
/* Recompute dominance information for basic blocks outside LOOP. */
static void
update_dominators_in_loop (struct loop *loop)
{
VEC (basic_block, heap) *dom_bbs = NULL;
sbitmap seen;
basic_block *body;
unsigned i;
seen = sbitmap_alloc (last_basic_block);
sbitmap_zero (seen);
body = get_loop_body (loop);
for (i = 0; i < loop->num_nodes; i++)
SET_BIT (seen, body[i]->index);
for (i = 0; i < loop->num_nodes; i++)
{
basic_block ldom;
for (ldom = first_dom_son (CDI_DOMINATORS, body[i]);
ldom;
ldom = next_dom_son (CDI_DOMINATORS, ldom))
if (!TEST_BIT (seen, ldom->index))
{
SET_BIT (seen, ldom->index);
VEC_safe_push (basic_block, heap, dom_bbs, ldom);
}
}
iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, false);
free (body);
free (seen);
VEC_free (basic_block, heap, dom_bbs);
}
/* Creates an if region as shown above. CONDITION is used to create
the test for the if.
|
| ------------- -------------
| | pred_bb | | pred_bb |
| ------------- -------------
| | |
| | | ENTRY_EDGE
| | ENTRY_EDGE V
| | ====> -------------
| | | cond_bb |
| | | CONDITION |
| | -------------
| V / \
| ------------- e_false / \ e_true
| | succ_bb | V V
| ------------- ----------- -----------
| | false_bb | | true_bb |
| ----------- -----------
| \ /
| \ /
| V V
| -------------
| | join_bb |
| -------------
| | exit_edge (result)
| V
| -----------
| | succ_bb |
| -----------
|
*/
edge
create_empty_if_region_on_edge (edge entry_edge, tree condition)
{
basic_block cond_bb, true_bb, false_bb, join_bb;
edge e_true, e_false, exit_edge;
gimple cond_stmt;
tree simple_cond;
gimple_stmt_iterator gsi;
cond_bb = split_edge (entry_edge);
/* Insert condition in cond_bb. */
gsi = gsi_last_bb (cond_bb);
simple_cond =
force_gimple_operand_gsi (&gsi, condition, true, NULL,
false, GSI_NEW_STMT);
cond_stmt = gimple_build_cond_from_tree (simple_cond, NULL_TREE, NULL_TREE);
gsi = gsi_last_bb (cond_bb);
gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
join_bb = split_edge (single_succ_edge (cond_bb));
e_true = single_succ_edge (cond_bb);
true_bb = split_edge (e_true);
e_false = make_edge (cond_bb, join_bb, 0);
false_bb = split_edge (e_false);
e_true->flags &= ~EDGE_FALLTHRU;
e_true->flags |= EDGE_TRUE_VALUE;
e_false->flags &= ~EDGE_FALLTHRU;
e_false->flags |= EDGE_FALSE_VALUE;
set_immediate_dominator (CDI_DOMINATORS, cond_bb, entry_edge->src);
set_immediate_dominator (CDI_DOMINATORS, true_bb, cond_bb);
set_immediate_dominator (CDI_DOMINATORS, false_bb, cond_bb);
set_immediate_dominator (CDI_DOMINATORS, join_bb, cond_bb);
exit_edge = single_succ_edge (join_bb);
if (single_pred_p (exit_edge->dest))
set_immediate_dominator (CDI_DOMINATORS, exit_edge->dest, join_bb);
return exit_edge;
}
/* create_empty_loop_on_edge
|
| - pred_bb - ------ pred_bb ------
| | | | iv0 = initial_value |
| -----|----- ---------|-----------
| | ______ | entry_edge
| | entry_edge / | |
| | ====> | -V---V- loop_header -------------
| V | | iv_before = phi (iv0, iv_after) |
| - succ_bb - | ---|-----------------------------
| | | | |
| ----------- | ---V--- loop_body ---------------
| | | iv_after = iv_before + stride |
| | | if (iv_before < upper_bound) |
| | ---|--------------\--------------
| | | \ exit_e
| | V \
| | - loop_latch - V- succ_bb -
| | | | | |
| | /------------- -----------
| \ ___ /
Creates an empty loop as shown above, the IV_BEFORE is the SSA_NAME
that is used before the increment of IV. IV_BEFORE should be used for
adding code to the body that uses the IV. OUTER is the outer loop in
which the new loop should be inserted.
Both INITIAL_VALUE and UPPER_BOUND expressions are gimplified and
inserted on the loop entry edge. This implies that this function
should be used only when the UPPER_BOUND expression is a loop
invariant. */
struct loop *
create_empty_loop_on_edge (edge entry_edge,
tree initial_value,
tree stride, tree upper_bound,
tree iv,
tree *iv_before,
tree *iv_after,
struct loop *outer)
{
basic_block loop_header, loop_latch, succ_bb, pred_bb;
struct loop *loop;
gimple_stmt_iterator gsi;
gimple_seq stmts;
gimple cond_expr;
tree exit_test;
edge exit_e;
int prob;
gcc_assert (entry_edge && initial_value && stride && upper_bound && iv);
/* Create header, latch and wire up the loop. */
pred_bb = entry_edge->src;
loop_header = split_edge (entry_edge);
loop_latch = split_edge (single_succ_edge (loop_header));
succ_bb = single_succ (loop_latch);
make_edge (loop_header, succ_bb, 0);
redirect_edge_succ_nodup (single_succ_edge (loop_latch), loop_header);
/* Set immediate dominator information. */
set_immediate_dominator (CDI_DOMINATORS, loop_header, pred_bb);
set_immediate_dominator (CDI_DOMINATORS, loop_latch, loop_header);
set_immediate_dominator (CDI_DOMINATORS, succ_bb, loop_header);
/* Initialize a loop structure and put it in a loop hierarchy. */
loop = alloc_loop ();
loop->header = loop_header;
loop->latch = loop_latch;
add_loop (loop, outer);
/* TODO: Fix frequencies and counts. */
prob = REG_BR_PROB_BASE / 2;
scale_loop_frequencies (loop, REG_BR_PROB_BASE - prob, REG_BR_PROB_BASE);
/* Update dominators. */
update_dominators_in_loop (loop);
/* Modify edge flags. */
exit_e = single_exit (loop);
exit_e->flags = EDGE_LOOP_EXIT | EDGE_FALSE_VALUE;
single_pred_edge (loop_latch)->flags = EDGE_TRUE_VALUE;
/* Construct IV code in loop. */
initial_value = force_gimple_operand (initial_value, &stmts, true, iv);
if (stmts)
{
gsi_insert_seq_on_edge (loop_preheader_edge (loop), stmts);
gsi_commit_edge_inserts ();
}
upper_bound = force_gimple_operand (upper_bound, &stmts, true, NULL);
if (stmts)
{
gsi_insert_seq_on_edge (loop_preheader_edge (loop), stmts);
gsi_commit_edge_inserts ();
}
gsi = gsi_last_bb (loop_header);
create_iv (initial_value, stride, iv, loop, &gsi, false,
iv_before, iv_after);
/* Insert loop exit condition. */
cond_expr = gimple_build_cond
(LT_EXPR, *iv_before, upper_bound, NULL_TREE, NULL_TREE);
exit_test = gimple_cond_lhs (cond_expr);
exit_test = force_gimple_operand_gsi (&gsi, exit_test, true, NULL,
false, GSI_NEW_STMT);
gimple_cond_set_lhs (cond_expr, exit_test);
gsi = gsi_last_bb (exit_e->src);
gsi_insert_after (&gsi, cond_expr, GSI_NEW_STMT);
split_block_after_labels (loop_header);
return loop;
}
/* Make area between HEADER_EDGE and LATCH_EDGE a loop by connecting
latch to header and update loop tree and dominators
accordingly. Everything between them plus LATCH_EDGE destination must
be dominated by HEADER_EDGE destination, and back-reachable from
LATCH_EDGE source. HEADER_EDGE is redirected to basic block SWITCH_BB,
FALSE_EDGE of SWITCH_BB to original destination of HEADER_EDGE and
TRUE_EDGE of SWITCH_BB to original destination of LATCH_EDGE.
Returns the newly created loop. Frequencies and counts in the new loop
are scaled by FALSE_SCALE and in the old one by TRUE_SCALE. */
struct loop *
loopify (edge latch_edge, edge header_edge,
basic_block switch_bb, edge true_edge, edge false_edge,
bool redirect_all_edges, unsigned true_scale, unsigned false_scale)
{
basic_block succ_bb = latch_edge->dest;
basic_block pred_bb = header_edge->src;
struct loop *loop = alloc_loop ();
struct loop *outer = loop_outer (succ_bb->loop_father);
int freq;
gcov_type cnt;
edge e;
edge_iterator ei;
loop->header = header_edge->dest;
loop->latch = latch_edge->src;
freq = EDGE_FREQUENCY (header_edge);
cnt = header_edge->count;
/* Redirect edges. */
loop_redirect_edge (latch_edge, loop->header);
loop_redirect_edge (true_edge, succ_bb);
/* During loop versioning, one of the switch_bb edge is already properly
set. Do not redirect it again unless redirect_all_edges is true. */
if (redirect_all_edges)
{
loop_redirect_edge (header_edge, switch_bb);
loop_redirect_edge (false_edge, loop->header);
/* Update dominators. */
set_immediate_dominator (CDI_DOMINATORS, switch_bb, pred_bb);
set_immediate_dominator (CDI_DOMINATORS, loop->header, switch_bb);
}
set_immediate_dominator (CDI_DOMINATORS, succ_bb, switch_bb);
/* Compute new loop. */
add_loop (loop, outer);
/* Add switch_bb to appropriate loop. */
if (switch_bb->loop_father)
remove_bb_from_loops (switch_bb);
add_bb_to_loop (switch_bb, outer);
/* Fix frequencies. */
if (redirect_all_edges)
{
switch_bb->frequency = freq;
switch_bb->count = cnt;
FOR_EACH_EDGE (e, ei, switch_bb->succs)
{
e->count = (switch_bb->count * e->probability) / REG_BR_PROB_BASE;
}
}
scale_loop_frequencies (loop, false_scale, REG_BR_PROB_BASE);
scale_loop_frequencies (succ_bb->loop_father, true_scale, REG_BR_PROB_BASE);
update_dominators_in_loop (loop);
return loop;
}
/* Remove the latch edge of a LOOP and update loops to indicate that
the LOOP was removed. After this function, original loop latch will
have no successor, which caller is expected to fix somehow.
If this may cause the information about irreducible regions to become
invalid, IRRED_INVALIDATED is set to true. */
static void
unloop (struct loop *loop, bool *irred_invalidated)
{
basic_block *body;
struct loop *ploop;
unsigned i, n;
basic_block latch = loop->latch;
bool dummy = false;
if (loop_preheader_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP)
*irred_invalidated = true;
/* This is relatively straightforward. The dominators are unchanged, as
loop header dominates loop latch, so the only thing we have to care of
is the placement of loops and basic blocks inside the loop tree. We
move them all to the loop->outer, and then let fix_bb_placements do
its work. */
body = get_loop_body (loop);
n = loop->num_nodes;
for (i = 0; i < n; i++)
if (body[i]->loop_father == loop)
{
remove_bb_from_loops (body[i]);
add_bb_to_loop (body[i], loop_outer (loop));
}
free(body);
while (loop->inner)
{
ploop = loop->inner;
flow_loop_tree_node_remove (ploop);
flow_loop_tree_node_add (loop_outer (loop), ploop);
}
/* Remove the loop and free its data. */
delete_loop (loop);
remove_edge (single_succ_edge (latch));
/* We do not pass IRRED_INVALIDATED to fix_bb_placements here, as even if
there is an irreducible region inside the cancelled loop, the flags will
be still correct. */
fix_bb_placements (latch, &dummy);
}
/* Fix placement of superloops of LOOP inside loop tree, i.e. ensure that
condition stated in description of fix_loop_placement holds for them.
It is used in case when we removed some edges coming out of LOOP, which
may cause the right placement of LOOP inside loop tree to change.
IRRED_INVALIDATED is set to true if a change in the loop structures might
invalidate the information about irreducible regions. */
static void
fix_loop_placements (struct loop *loop, bool *irred_invalidated)
{
struct loop *outer;
while (loop_outer (loop))
{
outer = loop_outer (loop);
if (!fix_loop_placement (loop))
break;
/* Changing the placement of a loop in the loop tree may alter the
validity of condition 2) of the description of fix_bb_placement
for its preheader, because the successor is the header and belongs
to the loop. So call fix_bb_placements to fix up the placement
of the preheader and (possibly) of its predecessors. */
fix_bb_placements (loop_preheader_edge (loop)->src,
irred_invalidated);
loop = outer;
}
}
/* Copies copy of LOOP as subloop of TARGET loop, placing newly
created loop into loops structure. */
struct loop *
duplicate_loop (struct loop *loop, struct loop *target)
{
struct loop *cloop;
cloop = alloc_loop ();
place_new_loop (cloop);
/* Mark the new loop as copy of LOOP. */
set_loop_copy (loop, cloop);
/* Add it to target. */
flow_loop_tree_node_add (target, cloop);
return cloop;
}
/* Copies structure of subloops of LOOP into TARGET loop, placing
newly created loops into loop tree. */
void
duplicate_subloops (struct loop *loop, struct loop *target)
{
struct loop *aloop, *cloop;
for (aloop = loop->inner; aloop; aloop = aloop->next)
{
cloop = duplicate_loop (aloop, target);
duplicate_subloops (aloop, cloop);
}
}
/* Copies structure of subloops of N loops, stored in array COPIED_LOOPS,
into TARGET loop, placing newly created loops into loop tree. */
static void
copy_loops_to (struct loop **copied_loops, int n, struct loop *target)
{
struct loop *aloop;
int i;
for (i = 0; i < n; i++)
{
aloop = duplicate_loop (copied_loops[i], target);
duplicate_subloops (copied_loops[i], aloop);
}
}
/* Redirects edge E to basic block DEST. */
static void
loop_redirect_edge (edge e, basic_block dest)
{
if (e->dest == dest)
return;
redirect_edge_and_branch_force (e, dest);
}
/* Check whether LOOP's body can be duplicated. */
bool
can_duplicate_loop_p (const struct loop *loop)
{
int ret;
basic_block *bbs = get_loop_body (loop);
ret = can_copy_bbs_p (bbs, loop->num_nodes);
free (bbs);
return ret;
}
/* Sets probability and count of edge E to zero. The probability and count
is redistributed evenly to the remaining edges coming from E->src. */
static void
set_zero_probability (edge e)
{
basic_block bb = e->src;
edge_iterator ei;
edge ae, last = NULL;
unsigned n = EDGE_COUNT (bb->succs);
gcov_type cnt = e->count, cnt1;
unsigned prob = e->probability, prob1;
gcc_assert (n > 1);
cnt1 = cnt / (n - 1);
prob1 = prob / (n - 1);
FOR_EACH_EDGE (ae, ei, bb->succs)
{
if (ae == e)
continue;
ae->probability += prob1;
ae->count += cnt1;
last = ae;
}
/* Move the rest to one of the edges. */
last->probability += prob % (n - 1);
last->count += cnt % (n - 1);
e->probability = 0;
e->count = 0;
}
/* Duplicates body of LOOP to given edge E NDUPL times. Takes care of updating
loop structure and dominators. E's destination must be LOOP header for
this to work, i.e. it must be entry or latch edge of this loop; these are
unique, as the loops must have preheaders for this function to work
correctly (in case E is latch, the function unrolls the loop, if E is entry
edge, it peels the loop). Store edges created by copying ORIG edge from
copies corresponding to set bits in WONT_EXIT bitmap (bit 0 corresponds to
original LOOP body, the other copies are numbered in order given by control
flow through them) into TO_REMOVE array. Returns false if duplication is
impossible. */
bool
duplicate_loop_to_header_edge (struct loop *loop, edge e,
unsigned int ndupl, sbitmap wont_exit,
edge orig, VEC (edge, heap) **to_remove,
int flags)
{
struct loop *target, *aloop;
struct loop **orig_loops;
unsigned n_orig_loops;
basic_block header = loop->header, latch = loop->latch;
basic_block *new_bbs, *bbs, *first_active;
basic_block new_bb, bb, first_active_latch = NULL;
edge ae, latch_edge;