[InstCombine] Signed saturation patterns
[llvm-core.git] / utils / llvm-locstats / llvm-locstats.py
blob4df525ed1a96bcc988459ecc55f6096686f03f44
1 #!/usr/bin/env python
3 # This is a tool that works like debug location coverage calculator.
4 # It parses the llvm-dwarfdump --statistics output by reporting it
5 # in a more human readable way.
8 from __future__ import print_function
9 import argparse
10 import os
11 import sys
12 from json import loads
13 from math import ceil
14 from subprocess import Popen, PIPE
16 def coverage_buckets():
17 yield '0%'
18 yield '1-9%'
19 for start in range(10, 91, 10):
20 yield '{0}-{1}%'.format(start, start + 9)
21 yield '100%'
23 def locstats_output(
24 variables_total,
25 variables_total_locstats,
26 variables_with_loc,
27 scope_bytes_covered,
28 scope_bytes_from_first_def,
29 variables_coverage_map
32 pc_ranges_covered = int(ceil(scope_bytes_covered * 100.0)
33 / scope_bytes_from_first_def)
34 variables_coverage_per_map = {}
35 for cov_bucket in coverage_buckets():
36 variables_coverage_per_map[cov_bucket] = \
37 int(ceil(variables_coverage_map[cov_bucket] * 100.0) \
38 / variables_total_locstats)
40 print (' =================================================')
41 print (' Debug Location Statistics ')
42 print (' =================================================')
43 print (' cov% samples percentage(~) ')
44 print (' -------------------------------------------------')
45 for cov_bucket in coverage_buckets():
46 print (' {0:6} {1:8d} {2:3d}%'. \
47 format(cov_bucket, variables_coverage_map[cov_bucket], \
48 variables_coverage_per_map[cov_bucket]))
49 print (' =================================================')
50 print (' -the number of debug variables processed: ' \
51 + str(variables_total_locstats))
52 print (' -PC ranges covered: ' + str(pc_ranges_covered) + '%')
54 # Only if we are processing all the variables output the total
55 # availability.
56 if variables_total and variables_with_loc:
57 total_availability = int(ceil(variables_with_loc * 100.0) \
58 / variables_total)
59 print (' -------------------------------------------------')
60 print (' -total availability: ' + str(total_availability) + '%')
61 print (' =================================================')
63 def parse_program_args(parser):
64 parser.add_argument('-only-variables', action='store_true',
65 default=False,
66 help='calculate the location statistics only for '
67 'local variables'
69 parser.add_argument('-only-formal-parameters', action='store_true',
70 default=False,
71 help='calculate the location statistics only for '
72 'formal parameters'
74 parser.add_argument('-ignore-debug-entry-values', action='store_true',
75 default=False,
76 help='ignore the location statistics on locations with '
77 'entry values'
79 parser.add_argument('file_name', type=str, help='file to process')
80 return parser.parse_args()
83 def Main():
84 parser = argparse.ArgumentParser()
85 results = parse_program_args(parser)
87 if len(sys.argv) < 2:
88 print ('error: Too few arguments.')
89 parser.print_help()
90 sys.exit(1)
92 if results.only_variables and results.only_formal_parameters:
93 print ('error: Please use just one only* option.')
94 parser.print_help()
95 sys.exit(1)
97 # These will be different due to different options enabled.
98 variables_total = None
99 variables_total_locstats = None
100 variables_with_loc = None
101 variables_scope_bytes_covered = None
102 variables_scope_bytes_from_first_def = None
103 variables_scope_bytes_entry_values = None
104 variables_coverage_map = {}
105 binary = results.file_name
107 # Get the directory of the LLVM tools.
108 llvm_dwarfdump_cmd = os.path.join(os.path.dirname(__file__), \
109 "llvm-dwarfdump")
110 # The statistics llvm-dwarfdump option.
111 llvm_dwarfdump_stats_opt = "--statistics"
113 subproc = Popen([llvm_dwarfdump_cmd, llvm_dwarfdump_stats_opt, binary], \
114 stdin=PIPE, stdout=PIPE, stderr=PIPE, \
115 universal_newlines = True)
116 cmd_stdout, cmd_stderr = subproc.communicate()
118 # Get the JSON and parse it.
119 json_parsed = None
121 try:
122 json_parsed = loads(cmd_stdout)
123 except:
124 print ('error: No valid llvm-dwarfdump statistics found.')
125 sys.exit(1)
127 if results.only_variables:
128 # Read the JSON only for local variables.
129 variables_total_locstats = \
130 json_parsed['total vars procesed by location statistics']
131 variables_scope_bytes_covered = \
132 json_parsed['vars scope bytes covered']
133 variables_scope_bytes_from_first_def = \
134 json_parsed['vars scope bytes total']
135 if not results.ignore_debug_entry_values:
136 for cov_bucket in coverage_buckets():
137 cov_category = "vars with {} of its scope covered".format(cov_bucket)
138 variables_coverage_map[cov_bucket] = json_parsed[cov_category]
139 else:
140 variables_scope_bytes_entry_values = \
141 json_parsed['vars entry value scope bytes covered']
142 variables_scope_bytes_covered = variables_scope_bytes_covered \
143 - variables_scope_bytes_entry_values
144 for cov_bucket in coverage_buckets():
145 cov_category = \
146 "vars (excluding the debug entry values) " \
147 "with {} of its scope covered".format(cov_bucket)
148 variables_coverage_map[cov_bucket] = json_parsed[cov_category]
149 elif results.only_formal_parameters:
150 # Read the JSON only for formal parameters.
151 variables_total_locstats = \
152 json_parsed['total params procesed by location statistics']
153 variables_scope_bytes_covered = \
154 json_parsed['formal params scope bytes covered']
155 variables_scope_bytes_from_first_def = \
156 json_parsed['formal params scope bytes total']
157 if not results.ignore_debug_entry_values:
158 for cov_bucket in coverage_buckets():
159 cov_category = "params with {} of its scope covered".format(cov_bucket)
160 variables_coverage_map[cov_bucket] = json_parsed[cov_category]
161 else:
162 variables_scope_bytes_entry_values = \
163 json_parsed['formal params entry value scope bytes covered']
164 variables_scope_bytes_covered = variables_scope_bytes_covered \
165 - variables_scope_bytes_entry_values
166 for cov_bucket in coverage_buckets():
167 cov_category = \
168 "params (excluding the debug entry values) " \
169 "with {} of its scope covered".format(cov_bucket)
170 variables_coverage_map[cov_bucket] = json_parsed[cov_category]
171 else:
172 # Read the JSON for both local variables and formal parameters.
173 variables_total = \
174 json_parsed['source variables']
175 variables_with_loc = json_parsed['variables with location']
176 variables_total_locstats = \
177 json_parsed['total variables procesed by location statistics']
178 variables_scope_bytes_covered = \
179 json_parsed['scope bytes covered']
180 variables_scope_bytes_from_first_def = \
181 json_parsed['scope bytes total']
182 if not results.ignore_debug_entry_values:
183 for cov_bucket in coverage_buckets():
184 cov_category = "variables with {} of its scope covered". \
185 format(cov_bucket)
186 variables_coverage_map[cov_bucket] = json_parsed[cov_category]
187 else:
188 variables_scope_bytes_entry_values = \
189 json_parsed['entry value scope bytes covered']
190 variables_scope_bytes_covered = variables_scope_bytes_covered \
191 - variables_scope_bytes_entry_values
192 for cov_bucket in coverage_buckets():
193 cov_category = "variables (excluding the debug entry values) " \
194 "with {} of its scope covered". format(cov_bucket)
195 variables_coverage_map[cov_bucket] = json_parsed[cov_category]
197 # Pretty print collected info.
198 locstats_output(
199 variables_total,
200 variables_total_locstats,
201 variables_with_loc,
202 variables_scope_bytes_covered,
203 variables_scope_bytes_from_first_def,
204 variables_coverage_map
207 if __name__ == '__main__':
208 Main()
209 sys.exit(0)