forked from riscvarchive/riscv-gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.c
4454 lines (3879 loc) · 126 KB
/
predict.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
/* Branch prediction routines for the GNU compiler.
Copyright (C) 2000-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/>. */
/* References:
[1] "Branch Prediction for Free"
Ball and Larus; PLDI '93.
[2] "Static Branch Frequency and Program Profile Analysis"
Wu and Larus; MICRO-27.
[3] "Corpus-based Static Branch Prediction"
Calder, Grunwald, Lindsay, Martin, Mozer, and Zorn; PLDI '95. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "rtl.h"
#include "tree.h"
#include "gimple.h"
#include "cfghooks.h"
#include "tree-pass.h"
#include "ssa.h"
#include "memmodel.h"
#include "emit-rtl.h"
#include "cgraph.h"
#include "coverage.h"
#include "diagnostic-core.h"
#include "gimple-predict.h"
#include "fold-const.h"
#include "calls.h"
#include "cfganal.h"
#include "profile.h"
#include "sreal.h"
#include "cfgloop.h"
#include "gimple-iterator.h"
#include "tree-cfg.h"
#include "tree-ssa-loop-niter.h"
#include "tree-ssa-loop.h"
#include "tree-scalar-evolution.h"
#include "ipa-utils.h"
#include "gimple-pretty-print.h"
#include "selftest.h"
#include "cfgrtl.h"
#include "stringpool.h"
#include "attribs.h"
/* Enum with reasons why a predictor is ignored. */
enum predictor_reason
{
REASON_NONE,
REASON_IGNORED,
REASON_SINGLE_EDGE_DUPLICATE,
REASON_EDGE_PAIR_DUPLICATE
};
/* String messages for the aforementioned enum. */
static const char *reason_messages[] = {"", " (ignored)",
" (single edge duplicate)", " (edge pair duplicate)"};
static void combine_predictions_for_insn (rtx_insn *, basic_block);
static void dump_prediction (FILE *, enum br_predictor, int, basic_block,
enum predictor_reason, edge);
static void predict_paths_leading_to (basic_block, enum br_predictor,
enum prediction,
class loop *in_loop = NULL);
static void predict_paths_leading_to_edge (edge, enum br_predictor,
enum prediction,
class loop *in_loop = NULL);
static bool can_predict_insn_p (const rtx_insn *);
static HOST_WIDE_INT get_predictor_value (br_predictor, HOST_WIDE_INT);
static void determine_unlikely_bbs ();
/* Information we hold about each branch predictor.
Filled using information from predict.def. */
struct predictor_info
{
const char *const name; /* Name used in the debugging dumps. */
const int hitrate; /* Expected hitrate used by
predict_insn_def call. */
const int flags;
};
/* Use given predictor without Dempster-Shaffer theory if it matches
using first_match heuristics. */
#define PRED_FLAG_FIRST_MATCH 1
/* Recompute hitrate in percent to our representation. */
#define HITRATE(VAL) ((int) ((VAL) * REG_BR_PROB_BASE + 50) / 100)
#define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS},
static const struct predictor_info predictor_info[]= {
#include "predict.def"
/* Upper bound on predictors. */
{NULL, 0, 0}
};
#undef DEF_PREDICTOR
static gcov_type min_count = -1;
/* Determine the threshold for hot BB counts. */
gcov_type
get_hot_bb_threshold ()
{
if (min_count == -1)
{
const int hot_frac = param_hot_bb_count_fraction;
const gcov_type min_hot_count
= hot_frac
? profile_info->sum_max / hot_frac
: (gcov_type)profile_count::max_count;
set_hot_bb_threshold (min_hot_count);
if (dump_file)
fprintf (dump_file, "Setting hotness threshold to %" PRId64 ".\n",
min_hot_count);
}
return min_count;
}
/* Set the threshold for hot BB counts. */
void
set_hot_bb_threshold (gcov_type min)
{
min_count = min;
}
/* Return TRUE if COUNT is considered to be hot in function FUN. */
bool
maybe_hot_count_p (struct function *fun, profile_count count)
{
if (!count.initialized_p ())
return true;
if (count.ipa () == profile_count::zero ())
return false;
if (!count.ipa_p ())
{
struct cgraph_node *node = cgraph_node::get (fun->decl);
if (!profile_info || profile_status_for_fn (fun) != PROFILE_READ)
{
if (node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
return false;
if (node->frequency == NODE_FREQUENCY_HOT)
return true;
}
if (profile_status_for_fn (fun) == PROFILE_ABSENT)
return true;
if (node->frequency == NODE_FREQUENCY_EXECUTED_ONCE
&& count < (ENTRY_BLOCK_PTR_FOR_FN (fun)->count.apply_scale (2, 3)))
return false;
if (count.apply_scale (param_hot_bb_frequency_fraction, 1)
< ENTRY_BLOCK_PTR_FOR_FN (fun)->count)
return false;
return true;
}
/* Code executed at most once is not hot. */
if (count <= MAX (profile_info ? profile_info->runs : 1, 1))
return false;
return (count >= get_hot_bb_threshold ());
}
/* Return true if basic block BB of function FUN can be CPU intensive
and should thus be optimized for maximum performance. */
bool
maybe_hot_bb_p (struct function *fun, const_basic_block bb)
{
gcc_checking_assert (fun);
return maybe_hot_count_p (fun, bb->count);
}
/* Return true if edge E can be CPU intensive and should thus be optimized
for maximum performance. */
bool
maybe_hot_edge_p (edge e)
{
return maybe_hot_count_p (cfun, e->count ());
}
/* Return true if COUNT is considered to be never executed in function FUN
or if function FUN is considered so in the static profile. */
static bool
probably_never_executed (struct function *fun, profile_count count)
{
gcc_checking_assert (fun);
if (count.ipa () == profile_count::zero ())
return true;
/* Do not trust adjusted counts. This will make us to drop int cold section
code with low execution count as a result of inlining. These low counts
are not safe even with read profile and may lead us to dropping
code which actually gets executed into cold section of binary that is not
desirable. */
if (count.precise_p () && profile_status_for_fn (fun) == PROFILE_READ)
{
const int unlikely_frac = param_unlikely_bb_count_fraction;
if (count.apply_scale (unlikely_frac, 1) >= profile_info->runs)
return false;
return true;
}
if ((!profile_info || profile_status_for_fn (fun) != PROFILE_READ)
&& (cgraph_node::get (fun->decl)->frequency
== NODE_FREQUENCY_UNLIKELY_EXECUTED))
return true;
return false;
}
/* Return true if basic block BB of function FUN is probably never executed. */
bool
probably_never_executed_bb_p (struct function *fun, const_basic_block bb)
{
return probably_never_executed (fun, bb->count);
}
/* Return true if edge E is unlikely executed for obvious reasons. */
static bool
unlikely_executed_edge_p (edge e)
{
return (e->count () == profile_count::zero ()
|| e->probability == profile_probability::never ())
|| (e->flags & (EDGE_EH | EDGE_FAKE));
}
/* Return true if edge E of function FUN is probably never executed. */
bool
probably_never_executed_edge_p (struct function *fun, edge e)
{
if (unlikely_executed_edge_p (e))
return true;
return probably_never_executed (fun, e->count ());
}
/* Return true if function FUN should always be optimized for size. */
bool
optimize_function_for_size_p (struct function *fun)
{
if (!fun || !fun->decl)
return optimize_size;
cgraph_node *n = cgraph_node::get (fun->decl);
return n && n->optimize_for_size_p ();
}
/* Return true if function FUN should always be optimized for speed. */
bool
optimize_function_for_speed_p (struct function *fun)
{
return !optimize_function_for_size_p (fun);
}
/* Return the optimization type that should be used for function FUN. */
optimization_type
function_optimization_type (struct function *fun)
{
return (optimize_function_for_speed_p (fun)
? OPTIMIZE_FOR_SPEED
: OPTIMIZE_FOR_SIZE);
}
/* Return TRUE if basic block BB should be optimized for size. */
bool
optimize_bb_for_size_p (const_basic_block bb)
{
return (optimize_function_for_size_p (cfun)
|| (bb && !maybe_hot_bb_p (cfun, bb)));
}
/* Return TRUE if basic block BB should be optimized for speed. */
bool
optimize_bb_for_speed_p (const_basic_block bb)
{
return !optimize_bb_for_size_p (bb);
}
/* Return the optimization type that should be used for basic block BB. */
optimization_type
bb_optimization_type (const_basic_block bb)
{
return (optimize_bb_for_speed_p (bb)
? OPTIMIZE_FOR_SPEED
: OPTIMIZE_FOR_SIZE);
}
/* Return TRUE if edge E should be optimized for size. */
bool
optimize_edge_for_size_p (edge e)
{
return optimize_function_for_size_p (cfun) || !maybe_hot_edge_p (e);
}
/* Return TRUE if edge E should be optimized for speed. */
bool
optimize_edge_for_speed_p (edge e)
{
return !optimize_edge_for_size_p (e);
}
/* Return TRUE if the current function is optimized for size. */
bool
optimize_insn_for_size_p (void)
{
return optimize_function_for_size_p (cfun) || !crtl->maybe_hot_insn_p;
}
/* Return TRUE if the current function is optimized for speed. */
bool
optimize_insn_for_speed_p (void)
{
return !optimize_insn_for_size_p ();
}
/* Return TRUE if LOOP should be optimized for size. */
bool
optimize_loop_for_size_p (class loop *loop)
{
return optimize_bb_for_size_p (loop->header);
}
/* Return TRUE if LOOP should be optimized for speed. */
bool
optimize_loop_for_speed_p (class loop *loop)
{
return optimize_bb_for_speed_p (loop->header);
}
/* Return TRUE if nest rooted at LOOP should be optimized for speed. */
bool
optimize_loop_nest_for_speed_p (class loop *loop)
{
class loop *l = loop;
if (optimize_loop_for_speed_p (loop))
return true;
l = loop->inner;
while (l && l != loop)
{
if (optimize_loop_for_speed_p (l))
return true;
if (l->inner)
l = l->inner;
else if (l->next)
l = l->next;
else
{
while (l != loop && !l->next)
l = loop_outer (l);
if (l != loop)
l = l->next;
}
}
return false;
}
/* Return TRUE if nest rooted at LOOP should be optimized for size. */
bool
optimize_loop_nest_for_size_p (class loop *loop)
{
return !optimize_loop_nest_for_speed_p (loop);
}
/* Return true if edge E is likely to be well predictable by branch
predictor. */
bool
predictable_edge_p (edge e)
{
if (!e->probability.initialized_p ())
return false;
if ((e->probability.to_reg_br_prob_base ()
<= param_predictable_branch_outcome * REG_BR_PROB_BASE / 100)
|| (REG_BR_PROB_BASE - e->probability.to_reg_br_prob_base ()
<= param_predictable_branch_outcome * REG_BR_PROB_BASE / 100))
return true;
return false;
}
/* Set RTL expansion for BB profile. */
void
rtl_profile_for_bb (basic_block bb)
{
crtl->maybe_hot_insn_p = maybe_hot_bb_p (cfun, bb);
}
/* Set RTL expansion for edge profile. */
void
rtl_profile_for_edge (edge e)
{
crtl->maybe_hot_insn_p = maybe_hot_edge_p (e);
}
/* Set RTL expansion to default mode (i.e. when profile info is not known). */
void
default_rtl_profile (void)
{
crtl->maybe_hot_insn_p = true;
}
/* Return true if the one of outgoing edges is already predicted by
PREDICTOR. */
bool
rtl_predicted_by_p (const_basic_block bb, enum br_predictor predictor)
{
rtx note;
if (!INSN_P (BB_END (bb)))
return false;
for (note = REG_NOTES (BB_END (bb)); note; note = XEXP (note, 1))
if (REG_NOTE_KIND (note) == REG_BR_PRED
&& INTVAL (XEXP (XEXP (note, 0), 0)) == (int)predictor)
return true;
return false;
}
/* Structure representing predictions in tree level. */
struct edge_prediction {
struct edge_prediction *ep_next;
edge ep_edge;
enum br_predictor ep_predictor;
int ep_probability;
};
/* This map contains for a basic block the list of predictions for the
outgoing edges. */
static hash_map<const_basic_block, edge_prediction *> *bb_predictions;
/* Return true if the one of outgoing edges is already predicted by
PREDICTOR. */
bool
gimple_predicted_by_p (const_basic_block bb, enum br_predictor predictor)
{
struct edge_prediction *i;
edge_prediction **preds = bb_predictions->get (bb);
if (!preds)
return false;
for (i = *preds; i; i = i->ep_next)
if (i->ep_predictor == predictor)
return true;
return false;
}
/* Return true if the one of outgoing edges is already predicted by
PREDICTOR for edge E predicted as TAKEN. */
bool
edge_predicted_by_p (edge e, enum br_predictor predictor, bool taken)
{
struct edge_prediction *i;
basic_block bb = e->src;
edge_prediction **preds = bb_predictions->get (bb);
if (!preds)
return false;
int probability = predictor_info[(int) predictor].hitrate;
if (taken != TAKEN)
probability = REG_BR_PROB_BASE - probability;
for (i = *preds; i; i = i->ep_next)
if (i->ep_predictor == predictor
&& i->ep_edge == e
&& i->ep_probability == probability)
return true;
return false;
}
/* Same predicate as above, working on edges. */
bool
edge_probability_reliable_p (const_edge e)
{
return e->probability.probably_reliable_p ();
}
/* Same predicate as edge_probability_reliable_p, working on notes. */
bool
br_prob_note_reliable_p (const_rtx note)
{
gcc_assert (REG_NOTE_KIND (note) == REG_BR_PROB);
return profile_probability::from_reg_br_prob_note
(XINT (note, 0)).probably_reliable_p ();
}
static void
predict_insn (rtx_insn *insn, enum br_predictor predictor, int probability)
{
gcc_assert (any_condjump_p (insn));
if (!flag_guess_branch_prob)
return;
add_reg_note (insn, REG_BR_PRED,
gen_rtx_CONCAT (VOIDmode,
GEN_INT ((int) predictor),
GEN_INT ((int) probability)));
}
/* Predict insn by given predictor. */
void
predict_insn_def (rtx_insn *insn, enum br_predictor predictor,
enum prediction taken)
{
int probability = predictor_info[(int) predictor].hitrate;
gcc_assert (probability != PROB_UNINITIALIZED);
if (taken != TAKEN)
probability = REG_BR_PROB_BASE - probability;
predict_insn (insn, predictor, probability);
}
/* Predict edge E with given probability if possible. */
void
rtl_predict_edge (edge e, enum br_predictor predictor, int probability)
{
rtx_insn *last_insn;
last_insn = BB_END (e->src);
/* We can store the branch prediction information only about
conditional jumps. */
if (!any_condjump_p (last_insn))
return;
/* We always store probability of branching. */
if (e->flags & EDGE_FALLTHRU)
probability = REG_BR_PROB_BASE - probability;
predict_insn (last_insn, predictor, probability);
}
/* Predict edge E with the given PROBABILITY. */
void
gimple_predict_edge (edge e, enum br_predictor predictor, int probability)
{
if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
&& EDGE_COUNT (e->src->succs) > 1
&& flag_guess_branch_prob
&& optimize)
{
struct edge_prediction *i = XNEW (struct edge_prediction);
edge_prediction *&preds = bb_predictions->get_or_insert (e->src);
i->ep_next = preds;
preds = i;
i->ep_probability = probability;
i->ep_predictor = predictor;
i->ep_edge = e;
}
}
/* Filter edge predictions PREDS by a function FILTER. DATA are passed
to the filter function. */
void
filter_predictions (edge_prediction **preds,
bool (*filter) (edge_prediction *, void *), void *data)
{
if (!bb_predictions)
return;
if (preds)
{
struct edge_prediction **prediction = preds;
struct edge_prediction *next;
while (*prediction)
{
if ((*filter) (*prediction, data))
prediction = &((*prediction)->ep_next);
else
{
next = (*prediction)->ep_next;
free (*prediction);
*prediction = next;
}
}
}
}
/* Filter function predicate that returns true for a edge predicate P
if its edge is equal to DATA. */
bool
equal_edge_p (edge_prediction *p, void *data)
{
return p->ep_edge == (edge)data;
}
/* Remove all predictions on given basic block that are attached
to edge E. */
void
remove_predictions_associated_with_edge (edge e)
{
if (!bb_predictions)
return;
edge_prediction **preds = bb_predictions->get (e->src);
filter_predictions (preds, equal_edge_p, e);
}
/* Clears the list of predictions stored for BB. */
static void
clear_bb_predictions (basic_block bb)
{
edge_prediction **preds = bb_predictions->get (bb);
struct edge_prediction *pred, *next;
if (!preds)
return;
for (pred = *preds; pred; pred = next)
{
next = pred->ep_next;
free (pred);
}
*preds = NULL;
}
/* Return true when we can store prediction on insn INSN.
At the moment we represent predictions only on conditional
jumps, not at computed jump or other complicated cases. */
static bool
can_predict_insn_p (const rtx_insn *insn)
{
return (JUMP_P (insn)
&& any_condjump_p (insn)
&& EDGE_COUNT (BLOCK_FOR_INSN (insn)->succs) >= 2);
}
/* Predict edge E by given predictor if possible. */
void
predict_edge_def (edge e, enum br_predictor predictor,
enum prediction taken)
{
int probability = predictor_info[(int) predictor].hitrate;
if (taken != TAKEN)
probability = REG_BR_PROB_BASE - probability;
predict_edge (e, predictor, probability);
}
/* Invert all branch predictions or probability notes in the INSN. This needs
to be done each time we invert the condition used by the jump. */
void
invert_br_probabilities (rtx insn)
{
rtx note;
for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
if (REG_NOTE_KIND (note) == REG_BR_PROB)
XINT (note, 0) = profile_probability::from_reg_br_prob_note
(XINT (note, 0)).invert ().to_reg_br_prob_note ();
else if (REG_NOTE_KIND (note) == REG_BR_PRED)
XEXP (XEXP (note, 0), 1)
= GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (XEXP (note, 0), 1)));
}
/* Dump information about the branch prediction to the output file. */
static void
dump_prediction (FILE *file, enum br_predictor predictor, int probability,
basic_block bb, enum predictor_reason reason = REASON_NONE,
edge ep_edge = NULL)
{
edge e = ep_edge;
edge_iterator ei;
if (!file)
return;
if (e == NULL)
FOR_EACH_EDGE (e, ei, bb->succs)
if (! (e->flags & EDGE_FALLTHRU))
break;
char edge_info_str[128];
if (ep_edge)
sprintf (edge_info_str, " of edge %d->%d", ep_edge->src->index,
ep_edge->dest->index);
else
edge_info_str[0] = '\0';
fprintf (file, " %s heuristics%s%s: %.2f%%",
predictor_info[predictor].name,
edge_info_str, reason_messages[reason],
probability * 100.0 / REG_BR_PROB_BASE);
if (bb->count.initialized_p ())
{
fprintf (file, " exec ");
bb->count.dump (file);
if (e)
{
fprintf (file, " hit ");
e->count ().dump (file);
fprintf (file, " (%.1f%%)", e->count ().to_gcov_type() * 100.0
/ bb->count.to_gcov_type ());
}
}
fprintf (file, "\n");
/* Print output that be easily read by analyze_brprob.py script. We are
interested only in counts that are read from GCDA files. */
if (dump_file && (dump_flags & TDF_DETAILS)
&& bb->count.precise_p ()
&& reason == REASON_NONE)
{
fprintf (file, ";;heuristics;%s;%" PRId64 ";%" PRId64 ";%.1f;\n",
predictor_info[predictor].name,
bb->count.to_gcov_type (), e->count ().to_gcov_type (),
probability * 100.0 / REG_BR_PROB_BASE);
}
}
/* Return true if STMT is known to be unlikely executed. */
static bool
unlikely_executed_stmt_p (gimple *stmt)
{
if (!is_gimple_call (stmt))
return false;
/* NORETURN attribute alone is not strong enough: exit() may be quite
likely executed once during program run. */
if (gimple_call_fntype (stmt)
&& lookup_attribute ("cold",
TYPE_ATTRIBUTES (gimple_call_fntype (stmt)))
&& !lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl)))
return true;
tree decl = gimple_call_fndecl (stmt);
if (!decl)
return false;
if (lookup_attribute ("cold", DECL_ATTRIBUTES (decl))
&& !lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl)))
return true;
cgraph_node *n = cgraph_node::get (decl);
if (!n)
return false;
availability avail;
n = n->ultimate_alias_target (&avail);
if (avail < AVAIL_AVAILABLE)
return false;
if (!n->analyzed
|| n->decl == current_function_decl)
return false;
return n->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED;
}
/* Return true if BB is unlikely executed. */
static bool
unlikely_executed_bb_p (basic_block bb)
{
if (bb->count == profile_count::zero ())
return true;
if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun) || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
return false;
for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
!gsi_end_p (gsi); gsi_next (&gsi))
{
if (unlikely_executed_stmt_p (gsi_stmt (gsi)))
return true;
if (stmt_can_terminate_bb_p (gsi_stmt (gsi)))
return false;
}
return false;
}
/* We cannot predict the probabilities of outgoing edges of bb. Set them
evenly and hope for the best. If UNLIKELY_EDGES is not null, distribute
even probability for all edges not mentioned in the set. These edges
are given PROB_VERY_UNLIKELY probability. Similarly for LIKELY_EDGES,
if we have exactly one likely edge, make the other edges predicted
as not probable. */
static void
set_even_probabilities (basic_block bb,
hash_set<edge> *unlikely_edges = NULL,
hash_set<edge_prediction *> *likely_edges = NULL)
{
unsigned nedges = 0, unlikely_count = 0;
edge e = NULL;
edge_iterator ei;
profile_probability all = profile_probability::always ();
FOR_EACH_EDGE (e, ei, bb->succs)
if (e->probability.initialized_p ())
all -= e->probability;
else if (!unlikely_executed_edge_p (e))
{
nedges++;
if (unlikely_edges != NULL && unlikely_edges->contains (e))
{
all -= profile_probability::very_unlikely ();
unlikely_count++;
}
}
/* Make the distribution even if all edges are unlikely. */
unsigned likely_count = likely_edges ? likely_edges->elements () : 0;
if (unlikely_count == nedges)
{
unlikely_edges = NULL;
unlikely_count = 0;
}
/* If we have one likely edge, then use its probability and distribute
remaining probabilities as even. */
if (likely_count == 1)
{
FOR_EACH_EDGE (e, ei, bb->succs)
if (e->probability.initialized_p ())
;
else if (!unlikely_executed_edge_p (e))
{
edge_prediction *prediction = *likely_edges->begin ();
int p = prediction->ep_probability;
profile_probability prob
= profile_probability::from_reg_br_prob_base (p);
if (prediction->ep_edge == e)
e->probability = prob;
else if (unlikely_edges != NULL && unlikely_edges->contains (e))
e->probability = profile_probability::very_unlikely ();
else
{
profile_probability remainder = prob.invert ();
remainder -= profile_probability::very_unlikely ()
.apply_scale (unlikely_count, 1);
int count = nedges - unlikely_count - 1;
gcc_assert (count >= 0);
e->probability = remainder.apply_scale (1, count);
}
}
else
e->probability = profile_probability::never ();
}
else
{
/* Make all unlikely edges unlikely and the rest will have even
probability. */
unsigned scale = nedges - unlikely_count;
FOR_EACH_EDGE (e, ei, bb->succs)
if (e->probability.initialized_p ())
;
else if (!unlikely_executed_edge_p (e))
{
if (unlikely_edges != NULL && unlikely_edges->contains (e))
e->probability = profile_probability::very_unlikely ();
else
e->probability = all.apply_scale (1, scale);
}
else
e->probability = profile_probability::never ();
}
}
/* Add REG_BR_PROB note to JUMP with PROB. */
void
add_reg_br_prob_note (rtx_insn *jump, profile_probability prob)
{
gcc_checking_assert (JUMP_P (jump) && !find_reg_note (jump, REG_BR_PROB, 0));
add_int_reg_note (jump, REG_BR_PROB, prob.to_reg_br_prob_note ());
}
/* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
note if not already present. Remove now useless REG_BR_PRED notes. */
static void
combine_predictions_for_insn (rtx_insn *insn, basic_block bb)
{
rtx prob_note;
rtx *pnote;
rtx note;
int best_probability = PROB_EVEN;
enum br_predictor best_predictor = END_PREDICTORS;
int combined_probability = REG_BR_PROB_BASE / 2;
int d;
bool first_match = false;
bool found = false;
if (!can_predict_insn_p (insn))
{
set_even_probabilities (bb);
return;
}
prob_note = find_reg_note (insn, REG_BR_PROB, 0);
pnote = ®_NOTES (insn);
if (dump_file)
fprintf (dump_file, "Predictions for insn %i bb %i\n", INSN_UID (insn),
bb->index);
/* We implement "first match" heuristics and use probability guessed
by predictor with smallest index. */
for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
if (REG_NOTE_KIND (note) == REG_BR_PRED)
{
enum br_predictor predictor = ((enum br_predictor)
INTVAL (XEXP (XEXP (note, 0), 0)));
int probability = INTVAL (XEXP (XEXP (note, 0), 1));
found = true;
if (best_predictor > predictor
&& predictor_info[predictor].flags & PRED_FLAG_FIRST_MATCH)
best_probability = probability, best_predictor = predictor;
d = (combined_probability * probability
+ (REG_BR_PROB_BASE - combined_probability)
* (REG_BR_PROB_BASE - probability));
/* Use FP math to avoid overflows of 32bit integers. */
if (d == 0)
/* If one probability is 0% and one 100%, avoid division by zero. */
combined_probability = REG_BR_PROB_BASE / 2;
else
combined_probability = (((double) combined_probability) * probability
* REG_BR_PROB_BASE / d + 0.5);
}
/* Decide which heuristic to use. In case we didn't match anything,
use no_prediction heuristic, in case we did match, use either
first match or Dempster-Shaffer theory depending on the flags. */
if (best_predictor != END_PREDICTORS)
first_match = true;
if (!found)
dump_prediction (dump_file, PRED_NO_PREDICTION,
combined_probability, bb);
else
{
if (!first_match)
dump_prediction (dump_file, PRED_DS_THEORY, combined_probability,
bb, !first_match ? REASON_NONE : REASON_IGNORED);
else
dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability,
bb, first_match ? REASON_NONE : REASON_IGNORED);
}
if (first_match)
combined_probability = best_probability;
dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb);
while (*pnote)