forked from riscvarchive/riscv-gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop-unroll.c
2160 lines (1824 loc) · 62.3 KB
/
loop-unroll.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 unrolling.
Copyright (C) 2002-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/>. */
#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 "memmodel.h"
#include "optabs.h"
#include "emit-rtl.h"
#include "recog.h"
#include "profile.h"
#include "cfgrtl.h"
#include "cfgloop.h"
#include "dojump.h"
#include "expr.h"
#include "dumpfile.h"
/* This pass performs loop unrolling. We only perform this
optimization on innermost loops (with single exception) because
the impact on performance is greatest here, and we want to avoid
unnecessary code size growth. The gain is caused by greater sequentiality
of code, better code to optimize for further passes and in some cases
by fewer testings of exit conditions. The main problem is code growth,
that impacts performance negatively due to effect of caches.
What we do:
-- unrolling of loops that roll constant times; this is almost always
win, as we get rid of exit condition tests.
-- unrolling of loops that roll number of times that we can compute
in runtime; we also get rid of exit condition tests here, but there
is the extra expense for calculating the number of iterations
-- simple unrolling of remaining loops; this is performed only if we
are asked to, as the gain is questionable in this case and often
it may even slow down the code
For more detailed descriptions of each of those, see comments at
appropriate function below.
There is a lot of parameters (defined and described in params.def) that
control how much we unroll.
??? A great problem is that we don't have a good way how to determine
how many times we should unroll the loop; the experiments I have made
showed that this choice may affect performance in order of several %.
*/
/* Information about induction variables to split. */
struct iv_to_split
{
rtx_insn *insn; /* The insn in that the induction variable occurs. */
rtx orig_var; /* The variable (register) for the IV before split. */
rtx base_var; /* The variable on that the values in the further
iterations are based. */
rtx step; /* Step of the induction variable. */
struct iv_to_split *next; /* Next entry in walking order. */
};
/* Information about accumulators to expand. */
struct var_to_expand
{
rtx_insn *insn; /* The insn in that the variable expansion occurs. */
rtx reg; /* The accumulator which is expanded. */
vec<rtx> var_expansions; /* The copies of the accumulator which is expanded. */
struct var_to_expand *next; /* Next entry in walking order. */
enum rtx_code op; /* The type of the accumulation - addition, subtraction
or multiplication. */
int expansion_count; /* Count the number of expansions generated so far. */
int reuse_expansion; /* The expansion we intend to reuse to expand
the accumulator. If REUSE_EXPANSION is 0 reuse
the original accumulator. Else use
var_expansions[REUSE_EXPANSION - 1]. */
};
/* Hashtable helper for iv_to_split. */
struct iv_split_hasher : free_ptr_hash <iv_to_split>
{
static inline hashval_t hash (const iv_to_split *);
static inline bool equal (const iv_to_split *, const iv_to_split *);
};
/* A hash function for information about insns to split. */
inline hashval_t
iv_split_hasher::hash (const iv_to_split *ivts)
{
return (hashval_t) INSN_UID (ivts->insn);
}
/* An equality functions for information about insns to split. */
inline bool
iv_split_hasher::equal (const iv_to_split *i1, const iv_to_split *i2)
{
return i1->insn == i2->insn;
}
/* Hashtable helper for iv_to_split. */
struct var_expand_hasher : free_ptr_hash <var_to_expand>
{
static inline hashval_t hash (const var_to_expand *);
static inline bool equal (const var_to_expand *, const var_to_expand *);
};
/* Return a hash for VES. */
inline hashval_t
var_expand_hasher::hash (const var_to_expand *ves)
{
return (hashval_t) INSN_UID (ves->insn);
}
/* Return true if I1 and I2 refer to the same instruction. */
inline bool
var_expand_hasher::equal (const var_to_expand *i1, const var_to_expand *i2)
{
return i1->insn == i2->insn;
}
/* Information about optimization applied in
the unrolled loop. */
struct opt_info
{
hash_table<iv_split_hasher> *insns_to_split; /* A hashtable of insns to
split. */
struct iv_to_split *iv_to_split_head; /* The first iv to split. */
struct iv_to_split **iv_to_split_tail; /* Pointer to the tail of the list. */
hash_table<var_expand_hasher> *insns_with_var_to_expand; /* A hashtable of
insns with accumulators to expand. */
struct var_to_expand *var_to_expand_head; /* The first var to expand. */
struct var_to_expand **var_to_expand_tail; /* Pointer to the tail of the list. */
unsigned first_new_block; /* The first basic block that was
duplicated. */
basic_block loop_exit; /* The loop exit basic block. */
basic_block loop_preheader; /* The loop preheader basic block. */
};
static void decide_unroll_stupid (class loop *, int);
static void decide_unroll_constant_iterations (class loop *, int);
static void decide_unroll_runtime_iterations (class loop *, int);
static void unroll_loop_stupid (class loop *);
static void decide_unrolling (int);
static void unroll_loop_constant_iterations (class loop *);
static void unroll_loop_runtime_iterations (class loop *);
static struct opt_info *analyze_insns_in_loop (class loop *);
static void opt_info_start_duplication (struct opt_info *);
static void apply_opt_in_copies (struct opt_info *, unsigned, bool, bool);
static void free_opt_info (struct opt_info *);
static struct var_to_expand *analyze_insn_to_expand_var (class loop*, rtx_insn *);
static bool referenced_in_one_insn_in_loop_p (class loop *, rtx, int *);
static struct iv_to_split *analyze_iv_to_split_insn (rtx_insn *);
static void expand_var_during_unrolling (struct var_to_expand *, rtx_insn *);
static void insert_var_expansion_initialization (struct var_to_expand *,
basic_block);
static void combine_var_copies_in_loop_exit (struct var_to_expand *,
basic_block);
static rtx get_expansion (struct var_to_expand *);
/* Emit a message summarizing the unroll that will be
performed for LOOP, along with the loop's location LOCUS, if
appropriate given the dump or -fopt-info settings. */
static void
report_unroll (class loop *loop, dump_location_t locus)
{
dump_flags_t report_flags = MSG_OPTIMIZED_LOCATIONS | TDF_DETAILS;
if (loop->lpt_decision.decision == LPT_NONE)
return;
if (!dump_enabled_p ())
return;
dump_metadata_t metadata (report_flags, locus.get_impl_location ());
dump_printf_loc (metadata, locus.get_user_location (),
"loop unrolled %d times",
loop->lpt_decision.times);
if (profile_info && loop->header->count.initialized_p ())
dump_printf (metadata,
" (header execution count %d)",
(int)loop->header->count.to_gcov_type ());
dump_printf (metadata, "\n");
}
/* Decide whether unroll loops and how much. */
static void
decide_unrolling (int flags)
{
class loop *loop;
/* Scan the loops, inner ones first. */
FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
{
loop->lpt_decision.decision = LPT_NONE;
dump_user_location_t locus = get_loop_location (loop);
if (dump_enabled_p ())
dump_printf_loc (MSG_NOTE, locus,
"considering unrolling loop %d at BB %d\n",
loop->num, loop->header->index);
if (loop->unroll == 1)
{
if (dump_file)
fprintf (dump_file,
";; Not unrolling loop, user didn't want it unrolled\n");
continue;
}
/* Do not peel cold areas. */
if (optimize_loop_for_size_p (loop))
{
if (dump_file)
fprintf (dump_file, ";; Not considering loop, cold area\n");
continue;
}
/* Can the loop be manipulated? */
if (!can_duplicate_loop_p (loop))
{
if (dump_file)
fprintf (dump_file,
";; Not considering loop, cannot duplicate\n");
continue;
}
/* Skip non-innermost loops. */
if (loop->inner)
{
if (dump_file)
fprintf (dump_file, ";; Not considering loop, is not innermost\n");
continue;
}
loop->ninsns = num_loop_insns (loop);
loop->av_ninsns = average_num_loop_insns (loop);
/* Try transformations one by one in decreasing order of priority. */
decide_unroll_constant_iterations (loop, flags);
if (loop->lpt_decision.decision == LPT_NONE)
decide_unroll_runtime_iterations (loop, flags);
if (loop->lpt_decision.decision == LPT_NONE)
decide_unroll_stupid (loop, flags);
report_unroll (loop, locus);
}
}
/* Unroll LOOPS. */
void
unroll_loops (int flags)
{
class loop *loop;
bool changed = false;
/* Now decide rest of unrolling. */
decide_unrolling (flags);
/* Scan the loops, inner ones first. */
FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
{
/* And perform the appropriate transformations. */
switch (loop->lpt_decision.decision)
{
case LPT_UNROLL_CONSTANT:
unroll_loop_constant_iterations (loop);
changed = true;
break;
case LPT_UNROLL_RUNTIME:
unroll_loop_runtime_iterations (loop);
changed = true;
break;
case LPT_UNROLL_STUPID:
unroll_loop_stupid (loop);
changed = true;
break;
case LPT_NONE:
break;
default:
gcc_unreachable ();
}
}
if (changed)
{
calculate_dominance_info (CDI_DOMINATORS);
fix_loop_structure (NULL);
}
iv_analysis_done ();
}
/* Check whether exit of the LOOP is at the end of loop body. */
static bool
loop_exit_at_end_p (class loop *loop)
{
class niter_desc *desc = get_simple_loop_desc (loop);
rtx_insn *insn;
/* We should never have conditional in latch block. */
gcc_assert (desc->in_edge->dest != loop->header);
if (desc->in_edge->dest != loop->latch)
return false;
/* Check that the latch is empty. */
FOR_BB_INSNS (loop->latch, insn)
{
if (INSN_P (insn) && active_insn_p (insn))
return false;
}
return true;
}
/* Decide whether to unroll LOOP iterating constant number of times
and how much. */
static void
decide_unroll_constant_iterations (class loop *loop, int flags)
{
unsigned nunroll, nunroll_by_av, best_copies, best_unroll = 0, n_copies, i;
class niter_desc *desc;
widest_int iterations;
/* If we were not asked to unroll this loop, just return back silently. */
if (!(flags & UAP_UNROLL) && !loop->unroll)
return;
if (dump_enabled_p ())
dump_printf (MSG_NOTE,
"considering unrolling loop with constant "
"number of iterations\n");
/* nunroll = total number of copies of the original loop body in
unrolled loop (i.e. if it is 2, we have to duplicate loop body once). */
nunroll = param_max_unrolled_insns / loop->ninsns;
nunroll_by_av
= param_max_average_unrolled_insns / loop->av_ninsns;
if (nunroll > nunroll_by_av)
nunroll = nunroll_by_av;
if (nunroll > (unsigned) param_max_unroll_times)
nunroll = param_max_unroll_times;
if (targetm.loop_unroll_adjust)
nunroll = targetm.loop_unroll_adjust (nunroll, loop);
/* Skip big loops. */
if (nunroll <= 1)
{
if (dump_file)
fprintf (dump_file, ";; Not considering loop, is too big\n");
return;
}
/* Check for simple loops. */
desc = get_simple_loop_desc (loop);
/* Check number of iterations. */
if (!desc->simple_p || !desc->const_iter || desc->assumptions)
{
if (dump_file)
fprintf (dump_file,
";; Unable to prove that the loop iterates constant times\n");
return;
}
/* Check for an explicit unrolling factor. */
if (loop->unroll > 0 && loop->unroll < USHRT_MAX)
{
/* However we cannot unroll completely at the RTL level a loop with
constant number of iterations; it should have been peeled instead. */
if (desc->niter == 0 || (unsigned) loop->unroll > desc->niter - 1)
{
if (dump_file)
fprintf (dump_file, ";; Loop should have been peeled\n");
}
else
{
loop->lpt_decision.decision = LPT_UNROLL_CONSTANT;
loop->lpt_decision.times = loop->unroll - 1;
}
return;
}
/* Check whether the loop rolls enough to consider.
Consult also loop bounds and profile; in the case the loop has more
than one exit it may well loop less than determined maximal number
of iterations. */
if (desc->niter < 2 * nunroll
|| ((get_estimated_loop_iterations (loop, &iterations)
|| get_likely_max_loop_iterations (loop, &iterations))
&& wi::ltu_p (iterations, 2 * nunroll)))
{
if (dump_file)
fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n");
return;
}
/* Success; now compute number of iterations to unroll. We alter
nunroll so that as few as possible copies of loop body are
necessary, while still not decreasing the number of unrollings
too much (at most by 1). */
best_copies = 2 * nunroll + 10;
i = 2 * nunroll + 2;
if (i > desc->niter - 2)
i = desc->niter - 2;
for (; i >= nunroll - 1; i--)
{
unsigned exit_mod = desc->niter % (i + 1);
if (!loop_exit_at_end_p (loop))
n_copies = exit_mod + i + 1;
else if (exit_mod != (unsigned) i
|| desc->noloop_assumptions != NULL_RTX)
n_copies = exit_mod + i + 2;
else
n_copies = i + 1;
if (n_copies < best_copies)
{
best_copies = n_copies;
best_unroll = i;
}
}
loop->lpt_decision.decision = LPT_UNROLL_CONSTANT;
loop->lpt_decision.times = best_unroll;
}
/* Unroll LOOP with constant number of iterations LOOP->LPT_DECISION.TIMES times.
The transformation does this:
for (i = 0; i < 102; i++)
body;
==> (LOOP->LPT_DECISION.TIMES == 3)
i = 0;
body; i++;
body; i++;
while (i < 102)
{
body; i++;
body; i++;
body; i++;
body; i++;
}
*/
static void
unroll_loop_constant_iterations (class loop *loop)
{
unsigned HOST_WIDE_INT niter;
unsigned exit_mod;
unsigned i;
edge e;
unsigned max_unroll = loop->lpt_decision.times;
class niter_desc *desc = get_simple_loop_desc (loop);
bool exit_at_end = loop_exit_at_end_p (loop);
struct opt_info *opt_info = NULL;
bool ok;
niter = desc->niter;
/* Should not get here (such loop should be peeled instead). */
gcc_assert (niter > max_unroll + 1);
exit_mod = niter % (max_unroll + 1);
auto_sbitmap wont_exit (max_unroll + 2);
bitmap_ones (wont_exit);
auto_vec<edge> remove_edges;
if (flag_split_ivs_in_unroller
|| flag_variable_expansion_in_unroller)
opt_info = analyze_insns_in_loop (loop);
if (!exit_at_end)
{
/* The exit is not at the end of the loop; leave exit test
in the first copy, so that the loops that start with test
of exit condition have continuous body after unrolling. */
if (dump_file)
fprintf (dump_file, ";; Condition at beginning of loop.\n");
/* Peel exit_mod iterations. */
bitmap_clear_bit (wont_exit, 0);
if (desc->noloop_assumptions)
bitmap_clear_bit (wont_exit, 1);
if (exit_mod)
{
opt_info_start_duplication (opt_info);
ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
exit_mod,
wont_exit, desc->out_edge,
&remove_edges,
DLTHE_FLAG_UPDATE_FREQ
| (opt_info && exit_mod > 1
? DLTHE_RECORD_COPY_NUMBER
: 0));
gcc_assert (ok);
if (opt_info && exit_mod > 1)
apply_opt_in_copies (opt_info, exit_mod, false, false);
desc->noloop_assumptions = NULL_RTX;
desc->niter -= exit_mod;
loop->nb_iterations_upper_bound -= exit_mod;
if (loop->any_estimate
&& wi::leu_p (exit_mod, loop->nb_iterations_estimate))
loop->nb_iterations_estimate -= exit_mod;
else
loop->any_estimate = false;
if (loop->any_likely_upper_bound
&& wi::leu_p (exit_mod, loop->nb_iterations_likely_upper_bound))
loop->nb_iterations_likely_upper_bound -= exit_mod;
else
loop->any_likely_upper_bound = false;
}
bitmap_set_bit (wont_exit, 1);
}
else
{
/* Leave exit test in last copy, for the same reason as above if
the loop tests the condition at the end of loop body. */
if (dump_file)
fprintf (dump_file, ";; Condition at end of loop.\n");
/* We know that niter >= max_unroll + 2; so we do not need to care of
case when we would exit before reaching the loop. So just peel
exit_mod + 1 iterations. */
if (exit_mod != max_unroll
|| desc->noloop_assumptions)
{
bitmap_clear_bit (wont_exit, 0);
if (desc->noloop_assumptions)
bitmap_clear_bit (wont_exit, 1);
opt_info_start_duplication (opt_info);
ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
exit_mod + 1,
wont_exit, desc->out_edge,
&remove_edges,
DLTHE_FLAG_UPDATE_FREQ
| (opt_info && exit_mod > 0
? DLTHE_RECORD_COPY_NUMBER
: 0));
gcc_assert (ok);
if (opt_info && exit_mod > 0)
apply_opt_in_copies (opt_info, exit_mod + 1, false, false);
desc->niter -= exit_mod + 1;
loop->nb_iterations_upper_bound -= exit_mod + 1;
if (loop->any_estimate
&& wi::leu_p (exit_mod + 1, loop->nb_iterations_estimate))
loop->nb_iterations_estimate -= exit_mod + 1;
else
loop->any_estimate = false;
if (loop->any_likely_upper_bound
&& wi::leu_p (exit_mod + 1, loop->nb_iterations_likely_upper_bound))
loop->nb_iterations_likely_upper_bound -= exit_mod + 1;
else
loop->any_likely_upper_bound = false;
desc->noloop_assumptions = NULL_RTX;
bitmap_set_bit (wont_exit, 0);
bitmap_set_bit (wont_exit, 1);
}
bitmap_clear_bit (wont_exit, max_unroll);
}
/* Now unroll the loop. */
opt_info_start_duplication (opt_info);
ok = duplicate_loop_to_header_edge (loop, loop_latch_edge (loop),
max_unroll,
wont_exit, desc->out_edge,
&remove_edges,
DLTHE_FLAG_UPDATE_FREQ
| (opt_info
? DLTHE_RECORD_COPY_NUMBER
: 0));
gcc_assert (ok);
if (opt_info)
{
apply_opt_in_copies (opt_info, max_unroll, true, true);
free_opt_info (opt_info);
}
if (exit_at_end)
{
basic_block exit_block = get_bb_copy (desc->in_edge->src);
/* Find a new in and out edge; they are in the last copy we have made. */
if (EDGE_SUCC (exit_block, 0)->dest == desc->out_edge->dest)
{
desc->out_edge = EDGE_SUCC (exit_block, 0);
desc->in_edge = EDGE_SUCC (exit_block, 1);
}
else
{
desc->out_edge = EDGE_SUCC (exit_block, 1);
desc->in_edge = EDGE_SUCC (exit_block, 0);
}
}
desc->niter /= max_unroll + 1;
loop->nb_iterations_upper_bound
= wi::udiv_trunc (loop->nb_iterations_upper_bound, max_unroll + 1);
if (loop->any_estimate)
loop->nb_iterations_estimate
= wi::udiv_trunc (loop->nb_iterations_estimate, max_unroll + 1);
if (loop->any_likely_upper_bound)
loop->nb_iterations_likely_upper_bound
= wi::udiv_trunc (loop->nb_iterations_likely_upper_bound, max_unroll + 1);
desc->niter_expr = gen_int_mode (desc->niter, desc->mode);
/* Remove the edges. */
FOR_EACH_VEC_ELT (remove_edges, i, e)
remove_path (e);
if (dump_file)
fprintf (dump_file,
";; Unrolled loop %d times, constant # of iterations %i insns\n",
max_unroll, num_loop_insns (loop));
}
/* Decide whether to unroll LOOP iterating runtime computable number of times
and how much. */
static void
decide_unroll_runtime_iterations (class loop *loop, int flags)
{
unsigned nunroll, nunroll_by_av, i;
class niter_desc *desc;
widest_int iterations;
/* If we were not asked to unroll this loop, just return back silently. */
if (!(flags & UAP_UNROLL) && !loop->unroll)
return;
if (dump_enabled_p ())
dump_printf (MSG_NOTE,
"considering unrolling loop with runtime-"
"computable number of iterations\n");
/* nunroll = total number of copies of the original loop body in
unrolled loop (i.e. if it is 2, we have to duplicate loop body once. */
nunroll = param_max_unrolled_insns / loop->ninsns;
nunroll_by_av = param_max_average_unrolled_insns / loop->av_ninsns;
if (nunroll > nunroll_by_av)
nunroll = nunroll_by_av;
if (nunroll > (unsigned) param_max_unroll_times)
nunroll = param_max_unroll_times;
if (targetm.loop_unroll_adjust)
nunroll = targetm.loop_unroll_adjust (nunroll, loop);
if (loop->unroll > 0 && loop->unroll < USHRT_MAX)
nunroll = loop->unroll;
/* Skip big loops. */
if (nunroll <= 1)
{
if (dump_file)
fprintf (dump_file, ";; Not considering loop, is too big\n");
return;
}
/* Check for simple loops. */
desc = get_simple_loop_desc (loop);
/* Check simpleness. */
if (!desc->simple_p || desc->assumptions)
{
if (dump_file)
fprintf (dump_file,
";; Unable to prove that the number of iterations "
"can be counted in runtime\n");
return;
}
if (desc->const_iter)
{
if (dump_file)
fprintf (dump_file, ";; Loop iterates constant times\n");
return;
}
/* Check whether the loop rolls. */
if ((get_estimated_loop_iterations (loop, &iterations)
|| get_likely_max_loop_iterations (loop, &iterations))
&& wi::ltu_p (iterations, 2 * nunroll))
{
if (dump_file)
fprintf (dump_file, ";; Not unrolling loop, doesn't roll\n");
return;
}
/* Success; now force nunroll to be power of 2, as code-gen
requires it, we are unable to cope with overflows in
computation of number of iterations. */
for (i = 1; 2 * i <= nunroll; i *= 2)
continue;
loop->lpt_decision.decision = LPT_UNROLL_RUNTIME;
loop->lpt_decision.times = i - 1;
}
/* Splits edge E and inserts the sequence of instructions INSNS on it, and
returns the newly created block. If INSNS is NULL_RTX, nothing is changed
and NULL is returned instead. */
basic_block
split_edge_and_insert (edge e, rtx_insn *insns)
{
basic_block bb;
if (!insns)
return NULL;
bb = split_edge (e);
emit_insn_after (insns, BB_END (bb));
/* ??? We used to assume that INSNS can contain control flow insns, and
that we had to try to find sub basic blocks in BB to maintain a valid
CFG. For this purpose we used to set the BB_SUPERBLOCK flag on BB
and call break_superblocks when going out of cfglayout mode. But it
turns out that this never happens; and that if it does ever happen,
the verify_flow_info at the end of the RTL loop passes would fail.
There are two reasons why we expected we could have control flow insns
in INSNS. The first is when a comparison has to be done in parts, and
the second is when the number of iterations is computed for loops with
the number of iterations known at runtime. In both cases, test cases
to get control flow in INSNS appear to be impossible to construct:
* If do_compare_rtx_and_jump needs several branches to do comparison
in a mode that needs comparison by parts, we cannot analyze the
number of iterations of the loop, and we never get to unrolling it.
* The code in expand_divmod that was suspected to cause creation of
branching code seems to be only accessed for signed division. The
divisions used by # of iterations analysis are always unsigned.
Problems might arise on architectures that emits branching code
for some operations that may appear in the unroller (especially
for division), but we have no such architectures.
Considering all this, it was decided that we should for now assume
that INSNS can in theory contain control flow insns, but in practice
it never does. So we don't handle the theoretical case, and should
a real failure ever show up, we have a pretty good clue for how to
fix it. */
return bb;
}
/* Prepare a sequence comparing OP0 with OP1 using COMP and jumping to LABEL if
true, with probability PROB. If CINSN is not NULL, it is the insn to copy
in order to create a jump. */
static rtx_insn *
compare_and_jump_seq (rtx op0, rtx op1, enum rtx_code comp,
rtx_code_label *label, profile_probability prob,
rtx_insn *cinsn)
{
rtx_insn *seq;
rtx_jump_insn *jump;
rtx cond;
machine_mode mode;
mode = GET_MODE (op0);
if (mode == VOIDmode)
mode = GET_MODE (op1);
start_sequence ();
if (GET_MODE_CLASS (mode) == MODE_CC)
{
/* A hack -- there seems to be no easy generic way how to make a
conditional jump from a ccmode comparison. */
gcc_assert (cinsn);
cond = XEXP (SET_SRC (pc_set (cinsn)), 0);
gcc_assert (GET_CODE (cond) == comp);
gcc_assert (rtx_equal_p (op0, XEXP (cond, 0)));
gcc_assert (rtx_equal_p (op1, XEXP (cond, 1)));
emit_jump_insn (copy_insn (PATTERN (cinsn)));
jump = as_a <rtx_jump_insn *> (get_last_insn ());
JUMP_LABEL (jump) = JUMP_LABEL (cinsn);
LABEL_NUSES (JUMP_LABEL (jump))++;
redirect_jump (jump, label, 0);
}
else
{
gcc_assert (!cinsn);
op0 = force_operand (op0, NULL_RTX);
op1 = force_operand (op1, NULL_RTX);
do_compare_rtx_and_jump (op0, op1, comp, 0,
mode, NULL_RTX, NULL, label,
profile_probability::uninitialized ());
jump = as_a <rtx_jump_insn *> (get_last_insn ());
jump->set_jump_target (label);
LABEL_NUSES (label)++;
}
if (prob.initialized_p ())
add_reg_br_prob_note (jump, prob);
seq = get_insns ();
end_sequence ();
return seq;
}
/* Unroll LOOP for which we are able to count number of iterations in
runtime LOOP->LPT_DECISION.TIMES times. The times value must be a
power of two. The transformation does this (with some extra care
for case n < 0):
for (i = 0; i < n; i++)
body;
==> (LOOP->LPT_DECISION.TIMES == 3)
i = 0;
mod = n % 4;
switch (mod)
{
case 3:
body; i++;
case 2:
body; i++;
case 1:
body; i++;
case 0: ;
}
while (i < n)
{
body; i++;
body; i++;
body; i++;
body; i++;
}
*/
static void
unroll_loop_runtime_iterations (class loop *loop)
{
rtx old_niter, niter, tmp;
rtx_insn *init_code, *branch_code;
unsigned i, j;
profile_probability p;
basic_block preheader, *body, swtch, ezc_swtch = NULL;
int may_exit_copy;
profile_count iter_count, new_count;
unsigned n_peel;
edge e;
bool extra_zero_check, last_may_exit;
unsigned max_unroll = loop->lpt_decision.times;
class niter_desc *desc = get_simple_loop_desc (loop);
bool exit_at_end = loop_exit_at_end_p (loop);
struct opt_info *opt_info = NULL;
bool ok;
if (flag_split_ivs_in_unroller
|| flag_variable_expansion_in_unroller)
opt_info = analyze_insns_in_loop (loop);
/* Remember blocks whose dominators will have to be updated. */
auto_vec<basic_block> dom_bbs;
body = get_loop_body (loop);
for (i = 0; i < loop->num_nodes; i++)
{
vec<basic_block> ldom;
basic_block bb;
ldom = get_dominated_by (CDI_DOMINATORS, body[i]);
FOR_EACH_VEC_ELT (ldom, j, bb)
if (!flow_bb_inside_loop_p (loop, bb))
dom_bbs.safe_push (bb);
ldom.release ();
}
free (body);
if (!exit_at_end)
{
/* Leave exit in first copy (for explanation why see comment in
unroll_loop_constant_iterations). */
may_exit_copy = 0;
n_peel = max_unroll - 1;
extra_zero_check = true;
last_may_exit = false;
}
else
{
/* Leave exit in last copy (for explanation why see comment in
unroll_loop_constant_iterations). */
may_exit_copy = max_unroll;
n_peel = max_unroll;
extra_zero_check = false;
last_may_exit = true;
}
/* Get expression for number of iterations. */
start_sequence ();
old_niter = niter = gen_reg_rtx (desc->mode);
tmp = force_operand (copy_rtx (desc->niter_expr), niter);
if (tmp != niter)
emit_move_insn (niter, tmp);
/* For loops that exit at end and whose number of iterations is reliable,
add one to niter to account for first pass through loop body before
reaching exit test. */
if (exit_at_end && !desc->noloop_assumptions)
{
niter = expand_simple_binop (desc->mode, PLUS,
niter, const1_rtx,
NULL_RTX, 0, OPTAB_LIB_WIDEN);
old_niter = niter;
}
/* Count modulo by ANDing it with max_unroll; we use the fact that
the number of unrollings is a power of two, and thus this is correct
even if there is overflow in the computation. */
niter = expand_simple_binop (desc->mode, AND,
niter, gen_int_mode (max_unroll, desc->mode),
NULL_RTX, 0, OPTAB_LIB_WIDEN);
init_code = get_insns ();
end_sequence ();
unshare_all_rtl_in_chain (init_code);
/* Precondition the loop. */
split_edge_and_insert (loop_preheader_edge (loop), init_code);
auto_vec<edge> remove_edges;
auto_sbitmap wont_exit (max_unroll + 2);
if (extra_zero_check || desc->noloop_assumptions)
{
/* Peel the first copy of loop body. Leave the exit test if the number
of iterations is not reliable. Also record the place of the extra zero
check. */
bitmap_clear (wont_exit);
if (!desc->noloop_assumptions)
bitmap_set_bit (wont_exit, 1);
ezc_swtch = loop_preheader_edge (loop)->src;
ok = duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
1, wont_exit, desc->out_edge,
&remove_edges,
DLTHE_FLAG_UPDATE_FREQ);
gcc_assert (ok);
}
/* Record the place where switch will be built for preconditioning. */
swtch = split_edge (loop_preheader_edge (loop));
/* Compute count increments for each switch block and initialize
innermost switch block. Switch blocks and peeled loop copies are built
from innermost outward. */
iter_count = new_count = swtch->count.apply_scale (1, max_unroll + 1);