3 # Script to analyze results of our branch prediction heuristics
5 # This file is part of GCC.
7 # GCC is free software; you can redistribute it and/or modify it under
8 # the terms of the GNU General Public License as published by the Free
9 # Software Foundation; either version 3, or (at your option) any later
12 # GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 # You should have received a copy of the GNU General Public License
18 # along with GCC; see the file COPYING3. If not see
19 # <http://www.gnu.org/licenses/>. */
23 # This script is used to calculate two basic properties of the branch prediction
24 # heuristics - coverage and hitrate. Coverage is number of executions
25 # of a given branch matched by the heuristics and hitrate is probability
26 # that once branch is predicted as taken it is really taken.
28 # These values are useful to determine the quality of given heuristics.
29 # Hitrate may be directly used in predict.def.
32 # Step 1: Compile and profile your program. You need to use -fprofile-generate
33 # flag to get the profiles.
34 # Step 2: Make a reference run of the intrumented application.
35 # Step 3: Compile the program with collected profile and dump IPA profiles
36 # (-fprofile-use -fdump-ipa-profile-details)
37 # Step 4: Collect all generated dump files:
38 # find . -name '*.profile' | xargs cat > dump_file
39 # Step 5: Run the script:
40 # ./analyze_brprob.py dump_file
41 # and read results. Basically the following table is printed:
43 # HEURISTICS BRANCHES (REL) HITRATE COVERAGE (REL)
44 # early return (on trees) 3 0.2% 35.83% / 93.64% 66360 0.0%
45 # guess loop iv compare 8 0.6% 53.35% / 53.73% 11183344 0.0%
46 # call 18 1.4% 31.95% / 69.95% 51880179 0.2%
47 # loop guard 23 1.8% 84.13% / 84.85% 13749065956 42.2%
48 # opcode values positive (on trees) 42 3.3% 15.71% / 84.81% 6771097902 20.8%
49 # opcode values nonequal (on trees) 226 17.6% 72.48% / 72.84% 844753864 2.6%
50 # loop exit 231 18.0% 86.97% / 86.98% 8952666897 27.5%
51 # loop iterations 239 18.6% 91.10% / 91.10% 3062707264 9.4%
52 # DS theory 281 21.9% 82.08% / 83.39% 7787264075 23.9%
53 # no prediction 293 22.9% 46.92% / 70.70% 2293267840 7.0%
54 # guessed loop iterations 313 24.4% 76.41% / 76.41% 10782750177 33.1%
55 # first match 708 55.2% 82.30% / 82.31% 22489588691 69.0%
56 # combined 1282 100.0% 79.76% / 81.75% 32570120606 100.0%
59 # The heuristics called "first match" is a heuristics used by GCC branch
60 # prediction pass and it predicts 55.2% branches correctly. As you can,
61 # the heuristics has very good covertage (69.05%). On the other hand,
62 # "opcode values nonequal (on trees)" heuristics has good hirate, but poor
73 def __init__(self
, name
):
80 def count_formatted(self
):
82 for unit
in ['','K','M','G','T','P','E','Z']:
84 return "%3.2f%s" % (v
, unit
)
86 return "%.1f%s" % (v
, 'Y')
89 def __init__(self
, filename
):
90 self
.filename
= filename
93 def add(self
, name
, prediction
, count
, hits
):
94 if not name
in self
.heuristics
:
95 self
.heuristics
[name
] = Summary(name
)
97 s
= self
.heuristics
[name
]
103 s
.fits
+= max(hits
, count
- hits
)
105 def branches_max(self
):
106 return max([v
.branches
for k
, v
in self
.heuristics
.items()])
109 return max([v
.count
for k
, v
in self
.heuristics
.items()])
112 print('%-36s %8s %6s %-16s %14s %8s %6s' % ('HEURISTICS', 'BRANCHES', '(REL)',
113 'HITRATE', 'COVERAGE', 'COVERAGE', '(REL)'))
114 for (k
, v
) in sorted(self
.heuristics
.items(), key
= lambda x
: x
[1].branches
):
115 print('%-36s %8i %5.1f%% %6.2f%% / %6.2f%% %14i %8s %5.1f%%' %
116 (k
, v
.branches
, percentage(v
.branches
, self
.branches_max ()),
117 percentage(v
.hits
, v
.count
), percentage(v
.fits
, v
.count
),
118 v
.count
, v
.count_formatted(), percentage(v
.count
, self
.count_max()) ))
120 if len(sys
.argv
) != 2:
121 print('Usage: ./analyze_brprob.py dump_file')
124 profile
= Profile(sys
.argv
[1])
125 r
= re
.compile(' (.*) heuristics: (.*)%.*exec ([0-9]*) hit ([0-9]*)')
126 for l
in open(profile
.filename
).readlines():
130 prediction
= float(m
.group(2))
131 count
= int(m
.group(3))
132 hits
= int(m
.group(4))
134 profile
.add(name
, prediction
, count
, hits
)