* tree-vect-data-refs.c (compare_tree): Rename and move ...
[official-gcc.git] / contrib / analyze_brprob.py
blobb4dbbc4ac158a5066b16e22347fb028527ff27b9
1 #!/usr/bin/env python3
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
10 # version.
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
15 # for more details.
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.
31 # Usage:
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
63 # coverage.
65 import sys
66 import os
67 import re
68 import argparse
70 from math import *
72 counter_aggregates = set(['combined', 'first match', 'DS theory',
73 'no prediction'])
75 def percentage(a, b):
76 return 100.0 * a / b
78 def average(values):
79 return 1.0 * sum(values) / len(values)
81 def average_cutoff(values, cut):
82 l = len(values)
83 skip = floor(l * cut / 2)
84 if skip > 0:
85 values.sort()
86 values = values[skip:-skip]
87 return average(values)
89 def median(values):
90 values.sort()
91 return values[int(len(values) / 2)]
93 class Summary:
94 def __init__(self, name):
95 self.name = name
96 self.branches = 0
97 self.successfull_branches = 0
98 self.count = 0
99 self.hits = 0
100 self.fits = 0
102 def get_hitrate(self):
103 return 100.0 * self.hits / self.count
105 def get_branch_hitrate(self):
106 return 100.0 * self.successfull_branches / self.branches
108 def count_formatted(self):
109 v = self.count
110 for unit in ['','K','M','G','T','P','E','Z']:
111 if v < 1000:
112 return "%3.2f%s" % (v, unit)
113 v /= 1000.0
114 return "%.1f%s" % (v, 'Y')
116 def print(self, branches_max, count_max):
117 print('%-40s %8i %5.1f%% %11.2f%% %7.2f%% / %6.2f%% %14i %8s %5.1f%%' %
118 (self.name, self.branches,
119 percentage(self.branches, branches_max),
120 self.get_branch_hitrate(),
121 self.get_hitrate(),
122 percentage(self.fits, self.count),
123 self.count, self.count_formatted(),
124 percentage(self.count, count_max)))
126 class Profile:
127 def __init__(self, filename):
128 self.filename = filename
129 self.heuristics = {}
130 self.niter_vector = []
132 def add(self, name, prediction, count, hits):
133 if not name in self.heuristics:
134 self.heuristics[name] = Summary(name)
136 s = self.heuristics[name]
137 s.branches += 1
139 s.count += count
140 if prediction < 50:
141 hits = count - hits
142 remaining = count - hits
143 if hits >= remaining:
144 s.successfull_branches += 1
146 s.hits += hits
147 s.fits += max(hits, remaining)
149 def add_loop_niter(self, niter):
150 if niter > 0:
151 self.niter_vector.append(niter)
153 def branches_max(self):
154 return max([v.branches for k, v in self.heuristics.items()])
156 def count_max(self):
157 return max([v.count for k, v in self.heuristics.items()])
159 def print_group(self, sorting, group_name, heuristics):
160 count_max = self.count_max()
161 branches_max = self.branches_max()
163 sorter = lambda x: x.branches
164 if sorting == 'branch-hitrate':
165 sorter = lambda x: x.get_branch_hitrate()
166 elif sorting == 'hitrate':
167 sorter = lambda x: x.get_hitrate()
168 elif sorting == 'coverage':
169 sorter = lambda x: x.count
170 elif sorting == 'name':
171 sorter = lambda x: x.name.lower()
173 print('%-40s %8s %6s %12s %18s %14s %8s %6s' %
174 ('HEURISTICS', 'BRANCHES', '(REL)',
175 'BR. HITRATE', 'HITRATE', 'COVERAGE', 'COVERAGE', '(REL)'))
176 for h in sorted(heuristics, key = sorter):
177 h.print(branches_max, count_max)
179 def dump(self, sorting):
180 heuristics = self.heuristics.values()
181 if len(heuristics) == 0:
182 print('No heuristics available')
183 return
185 special = list(filter(lambda x: x.name in counter_aggregates,
186 heuristics))
187 normal = list(filter(lambda x: x.name not in counter_aggregates,
188 heuristics))
190 self.print_group(sorting, 'HEURISTICS', normal)
191 print()
192 self.print_group(sorting, 'HEURISTIC AGGREGATES', special)
194 if len(self.niter_vector) > 0:
195 print ('\nLoop count: %d' % len(self.niter_vector)),
196 print(' avg. # of iter: %.2f' % average(self.niter_vector))
197 print(' median # of iter: %.2f' % median(self.niter_vector))
198 for v in [1, 5, 10, 20, 30]:
199 cut = 0.01 * v
200 print(' avg. (%d%% cutoff) # of iter: %.2f'
201 % (v, average_cutoff(self.niter_vector, cut)))
203 parser = argparse.ArgumentParser()
204 parser.add_argument('dump_file', metavar = 'dump_file',
205 help = 'IPA profile dump file')
206 parser.add_argument('-s', '--sorting', dest = 'sorting',
207 choices = ['branches', 'branch-hitrate', 'hitrate', 'coverage', 'name'],
208 default = 'branches')
210 args = parser.parse_args()
212 profile = Profile(sys.argv[1])
213 r = re.compile(' (.*) heuristics( of edge [0-9]*->[0-9]*)?( \\(.*\\))?: (.*)%.*exec ([0-9]*) hit ([0-9]*)')
214 loop_niter_str = ';; profile-based iteration count: '
215 for l in open(args.dump_file).readlines():
216 m = r.match(l)
217 if m != None and m.group(3) == None:
218 name = m.group(1)
219 prediction = float(m.group(4))
220 count = int(m.group(5))
221 hits = int(m.group(6))
223 profile.add(name, prediction, count, hits)
224 elif l.startswith(loop_niter_str):
225 v = int(l[len(loop_niter_str):])
226 profile.add_loop_niter(v)
228 profile.dump(args.sorting)