c-objc-common.c (c_tree_printer): Tidy.
[official-gcc.git] / gcc / testsuite / lib / profopt.exp
blobcdc6b0052523733a6761d303e83028fe4ccf13cf
1 # Copyright (C) 2001-2013 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with GCC; see the file COPYING3. If not see
15 # <http://www.gnu.org/licenses/>.
17 # This script was submitted by Janis Johnson <janis187@us.ibm.com>.
19 # Test the functionality and optionally, performance improvement, of
20 # programs compiled with profile-directed optimizations. Compile and
21 # run a test with profile options, compile it with options using the
22 # profile feedback, and then run the test again. Optionally compile
23 # and run a third time without the profile-directed optimization and
24 # compare timing results of the program with normal optimization and
25 # with the profile-directed optimization. Each test is run using
26 # multiple sets of optimization and/or code generation options in
27 # addition to the profiling and feedback options.
29 # If perf_ext is defined and the performance value for the
30 # profile-directed test run is nonzero then the performance check will
31 # be done.
33 load_lib dg.exp
34 load_lib gcc-dg.exp
36 global PROFOPT_OPTIONS perf_delta
38 # The including .exp file must define these.
39 global tool profile_option feedback_option prof_ext
40 if ![info exists tool] {
41 error "Tools is not specified."
43 if ![info exists prof_ext] {
44 error "No profile data file extensions specified."
47 # The maximum perforance degradation can be defined in the including file.
48 if ![info exists perf_delta] {
49 set perf_delta 4
52 # The default option list can be overridden by
53 # PROFOPT_OPTIONS="{ { list1 } ... { list2 } }"
55 if ![info exists PROFOPT_OPTIONS] {
56 set PROFOPT_OPTIONS [list \
57 { -g } \
58 { -O0 } \
59 { -O1 } \
60 { -O2 } \
61 { -O3 } \
62 { -O3 -g } \
63 { -Os } ]
67 # profopt-cleanup -- remove profiling or performance results files.
69 # TESTCASE is the name of the test
70 # EXT is the extensions of files to remove
72 proc profopt-cleanup { testcase extlist } {
73 set basename [file tail $testcase]
74 set base [file rootname $basename]
75 foreach ext $extlist {
76 set files [glob -nocomplain $base.$ext]
77 if { $files != "" } {
78 eval "remote_file build delete $files"
84 # profopt-perf-value -- get performance value for a test
86 # TESTCASE is the name of the test
87 # PERF_EXT is the extension of the performance result file
88 # OPTSTR is the string of compiler options
90 proc profopt-perf-value { testcase perf_ext optstr } {
91 set basename [file tail $testcase]
92 set base [file rootname $basename]
93 set files [glob -nocomplain $base.$perf_ext]
94 # The file doesn't exist; let the caller decide if that's a problem.
95 if { $files == "" } {
96 return -2
98 remote_upload host $base.$perf_ext $base.$perf_ext
99 set fd [open $base.$perf_ext r]
100 gets $fd line
101 set val -2
102 if [regexp "TIME" $line] {
103 if [regexp "TIME -1" $line] {
104 fail "$testcase perf check: no consistent time available, $optstr"
105 set val -1
106 } elseif ![regexp "(\[0-9\]+)" "$line" val] {
107 set val -2
110 # Report problems with an existing file.
111 if { $val == -2 } {
112 fail "$testcase perf check: file $base.$perf_ext has wrong format, $optstr"
114 close $fd
115 profopt-cleanup $testcase $perf_ext
116 return $val
120 # dg-final-generate -- process code to run after the profile-generate step
122 # ARGS is the line number of the directive followed by the commands.
124 proc dg-final-generate { args } {
125 global generate_final_code
127 if { [llength $args] > 2 } {
128 error "[lindex $args 0]: too many arguments"
129 return
131 append generate_final_code "[lindex $args 1]\n"
135 # dg-final-use -- process code to run after the profile-use step
137 # ARGS is the line number of the directive followed by the commands.
139 proc dg-final-use { args } {
140 global use_final_code
142 if { [llength $args] > 2 } {
143 error "[lindex $args 0]: too many arguments"
144 return
146 append use_final_code "[lindex $args 1]\n"
150 # profopt-final-code -- run final code
152 # WHICH is "generate" or "use".
153 # FINAL_CODE is the TCL code to run.
154 # TESTCASE is the name of the test, for error messages.
156 proc profopt-final-code { which final_code name } {
157 # This is copied from dg-test in dg.exp of DejaGnu.
158 regsub -all "\\\\(\[{}\])" $final_code "\\1" final_code
159 proc profopt-final-proc { args } $final_code
160 if [catch "profopt-final-proc $name" errmsg] {
161 perror "$name: error executing dg-final-${which}: $errmsg"
162 unresolved "$name: Error executing dg-final-${which}: $errmsg"
167 # profopt-get-options -- process test directives
169 # SRC is the full pathname of the testcase.
171 proc profopt-get-options { src } {
172 # dg-options sets a variable called dg-extra-tool-flags.
173 set dg-extra-tool-flags ""
175 # dg-require-* sets dg-do-what.
176 upvar dg-do-what dg-do-what
178 # current_compiler_flags reads tool_flags from the same stack frame
179 # as dg-extra-tool-flags
180 set tool_flags ""
182 set tmp [dg-get-options $src]
183 foreach op $tmp {
184 set cmd [lindex $op 0]
185 if { ![string compare "dg-options" $cmd] \
186 || ![string compare "dg-skip-if" $cmd] \
187 || ![string compare "dg-final-generate" $cmd] \
188 || ![string compare "dg-final-use" $cmd] \
189 || ![string compare "dg-additional-sources" $cmd] \
190 || [string match "dg-require-*" $cmd] } {
191 set status [catch "$op" errmsg]
192 if { $status != 0 } {
193 perror "$src: $errmsg for \"$op\"\n"
194 unresolved "$src: $errmsg for \"$op\""
195 return
197 } else {
198 # Ignore unrecognized dg- commands, but warn about them.
199 warning "profopt.exp does not support $cmd"
203 # Return flags to use for compiling the primary source file and for
204 # linking.
205 return ${dg-extra-tool-flags}
209 # c-prof-execute -- compile for profiling and then feedback, then normal
211 # SRC is the full pathname of the testcase.
213 proc profopt-execute { src } {
214 global srcdir tmpdir
215 global PROFOPT_OPTIONS
216 global tool profile_option feedback_option prof_ext perf_ext perf_delta
217 global generate_final_code use_final_code
218 global verbose
219 global testname_with_flags
221 if ![info exists profile_option] {
222 error "No profile option specified for first compile."
224 if ![info exists feedback_option] {
225 error "No feedback option specified for second compile."
228 # Use the default option list or one defined for a set of tests.
229 if ![info exists PROFOPT_OPTIONS] {
230 error "PROFOPT_OPTIONS is not defined"
232 set prof_option_list $PROFOPT_OPTIONS
234 regsub "(?q)$srcdir/" $src "" testcase
235 # If we couldn't rip $srcdir out of `src' then just do the best we can.
236 # The point is to reduce the unnecessary noise in the logs. Don't strip
237 # out too much because different testcases with the same name can confuse
238 # `test-tool'.
239 if [string match "/*" $testcase] {
240 set testcase "[file tail [file dirname $src]]/[file tail $src]"
243 # Several procedures access the name of the test with torture flags,
244 # normally defined in dg-test. Profile optimization tests don't
245 # use dg-test, so define it here to make it accessible via
246 # testname-for-summary.
247 set testname_with_flags $testcase
249 set executable $tmpdir/[file tail [file rootname $src].x]
250 set basename [file tail $testcase]
251 set base [file rootname $basename]
253 set count 0
254 foreach option $prof_option_list {
255 set execname1 "${executable}${count}1"
256 set execname2 "${executable}${count}2"
257 set execname3 "${executable}${count}3"
258 incr count
260 remote_file build delete $execname1
261 remote_file build delete $execname2
262 remote_file build delete $execname3
263 verbose "Testing $testcase, $option" 1
265 # Remove old profiling and performance data files.
266 foreach ext $prof_ext {
267 remote_file target delete $tmpdir/$base.$ext
269 if [info exists perf_ext] {
270 profopt-cleanup $testcase $perf_ext
273 # Process test directives.
275 set generate_final_code ""
276 set use_final_code ""
277 set dg-do-what [list "run" "" P]
278 set extra_flags [profopt-get-options $src]
279 if { [lindex ${dg-do-what} 1 ] == "N" } {
280 unsupported "$src"
281 unset testname_with_flags
282 verbose "$src not supported on this target, skipping it" 3
283 return
286 set extra_options [dg-additional-files-options "" "$src"]
288 # Tree profiling requires TLS runtime support, which may need
289 # additional flags.
290 if { [string first "-fprofile-generate" $profile_option] >= 0 } {
291 set extra_flags [add_options_for_tls $extra_flags]
294 # Compile for profiling.
296 set options "$extra_options"
297 lappend options "additional_flags=$option $extra_flags $profile_option"
298 set optstr "$option $profile_option"
299 set comp_output [${tool}_target_compile "$src" "$execname1" executable $options]
300 if ![${tool}_check_compile "$testcase compilation" $optstr $execname1 $comp_output] {
301 unresolved "$testcase execution, $optstr"
302 unresolved "$testcase compilation, $option $feedback_option"
303 unresolved "$testcase execution, $option $feedback_option"
304 continue
307 # Run the profiled test.
309 set result [${tool}_load $execname1 "" ""]
310 set status [lindex $result 0]
311 set missing_file 0
312 # Make sure the profile data was generated, and fail if not.
313 if { $status == "pass" } {
314 foreach ext $prof_ext {
315 remote_upload target $tmpdir/$base.$ext
316 set files [glob -nocomplain $base.$ext]
317 if { $files == "" } {
318 set status "fail"
319 set missing_file 1
320 fail "$testcase execution: file $base.$ext does not exist, $option $profile_option"
324 if { $missing_file == 0 } {
325 $status "$testcase execution, $optstr"
328 # If there is dg-final code to execute for the generate step, do it
329 # even if it failed; it might clean up temporary files.
330 if ![string match $generate_final_code ""] {
331 profopt-final-code "generate" $generate_final_code $testcase
334 remote_file build delete $execname1
336 # Quit for this round if it failed
337 if { $status != "pass" } {
338 unresolved "$testcase compilation, $option $feedback_option"
339 unresolved "$testcase execution, $option $feedback_option"
340 continue
343 # Compile with feedback-directed optimizations.
345 set options "$extra_options"
346 lappend options "additional_flags=$option $extra_flags $feedback_option"
347 set optstr "$option $feedback_option"
348 set comp_output [${tool}_target_compile "$src" "$execname2" "executable" $options]
350 # Prune warnings we know are unwanted.
351 set comp_output [prune_warnings $comp_output]
353 if ![${tool}_check_compile "$testcase compilation" $optstr $execname2 $comp_output] {
354 unresolved "$testcase execution, $optstr"
355 continue
358 # Run the profile-directed optimized test.
360 set result [${tool}_load "$execname2" "" ""]
361 set status [lindex $result 0]
362 $status "$testcase execution, $optstr"
364 # If there is dg-final code to execute for the use step, do it.
365 if ![string match $use_final_code ""] {
366 profopt-final-code "use" $use_final_code $testcase
369 # Remove the profiling data files.
370 foreach ext $prof_ext {
371 remote_file target delete $tmpdir/$base.$ext
374 if { $status != "pass" } {
375 continue
378 # If the test is not expected to produce performance data then
379 # we're done now.
380 if ![info exists perf_ext] {
381 remote_file build delete $execname2
382 continue
385 # Get the performance data from the test built with
386 # profile-directed optimization. If the file doesn't exist or if
387 # the value is zero, skip the performance comparison.
388 set val2 [profopt-perf-value $testcase $perf_ext $optstr]
389 if { $val2 <= 0 } {
390 remote_file build delete $execname2
391 continue
394 # Compile with normal optimizations.
396 set options "$extra_options"
397 lappend options "additional_flags=$option"
398 set optstr "$option"
399 set comp_output [${tool}_target_compile "$src" "$execname3" "executable" $options]
400 if ![${tool}_check_compile "$testcase compilation" $optstr $execname3 $comp_output] {
401 unresolved "$testcase execution, $optstr"
402 unresolved "$testcase perf check, $optstr"
403 continue
406 # Run the test with normal optimizations.
408 set result [${tool}_load "$execname3" "" ""]
409 set status [lindex $result 0]
410 $status "$testcase execution, $optstr"
411 if { $status != "pass" } {
412 unresolved "$testcase perf check, $optstr"
413 continue
416 # Get the performance data from the test built with normal
417 # optimization.
418 set val1 [profopt-perf-value $testcase $perf_ext $optstr]
419 if { $val1 < 0 } {
420 if { $val1 == -2 } {
421 # The data file existed with the profile-directed
422 # optimization so this one should, too.
423 fail "$testcase perf check: file $base.$perf_ext does not exist, $optstr"
425 continue
428 # Compare results of the two runs and fail if the time with the
429 # profile-directed optimization is significantly more than the time
430 # without it.
431 set status "pass"
432 if { $val2 > $val1 } {
433 # Check for a performance degration outside of allowable limits.
434 if { [expr $val2 - $val1] > [expr [expr $val1 * $perf_delta] / 100] } {
435 set status "fail"
438 if { $status == "fail" } {
439 fail "$testcase perf check: orig: $val1 new: $val2, $optstr"
440 } else {
441 $status "$testcase perf check, $optstr"
442 verbose "$testcase orig: $val1 new: $val2, $optstr" 2
443 remote_file build delete $execname2
444 remote_file build delete $execname3
447 unset testname_with_flags