Makefile.in (stmp-docobjdir): New target; ensure $docobjdir exists.
[official-gcc.git] / gcc / testsuite / lib / profopt.exp
blob4d32ff2fdbe77b57c45dddfc24261368d9a50f14
1 # Copyright (C) 2001 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 2 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 this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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 global PROFOPT_OPTIONS perf_delta
35 # The including .exp file must define these.
36 global tool profile_option feedback_option prof_ext
37 if ![info exists tool] {
38 error "Tools is not specified."
40 if ![info exists profile_option] {
41 error "No profile option specified for first compile."
43 if ![info exists feedback_option] {
44 error "No feedback option specified for second compile."
46 if ![info exists prof_ext] {
47 error "No profile data file extension specified."
50 # The maximum perforance degradation can be defined in the including file.
51 if ![info exists perf_delta] {
52 set perf_delta 4
55 # The default option list can be overridden by
56 # PROFOPT_OPTIONS="{ { list1 } ... { list2 } }"
58 if ![info exists PROFOPT_OPTIONS] {
59 set PROFOPT_OPTIONS [list \
60 { -g } \
61 { -O0 } \
62 { -O1 } \
63 { -O2 } \
64 { -O3 } \
65 { -O3 -g } \
66 { -Os } ]
69 set prof_option_list $PROFOPT_OPTIONS
72 # profopt-cleanup -- remove profiling or performance results files.
74 # TESTCASE is the name of the test
75 # EXT is the extension of file to remove
77 proc profopt-cleanup { testcase ext } {
78 set basename [file tail $testcase]
79 set base [file rootname $basename]
80 set files [glob -nocomplain $base.$ext]
81 if { $files != "" } {
82 eval "remote_file build delete $files"
87 # profopt-perf-value -- get performance value for a test
89 # TESTCASE is the name of the test
90 # PERF_EXT is the extension of the performance result file
91 # OPTSTR is the string of compiler options
93 proc profopt-perf-value { testcase perf_ext optstr } {
94 set basename [file tail $testcase]
95 set base [file rootname $basename]
96 set files [glob -nocomplain $base.$perf_ext]
97 # The file doesn't exist; let the caller decide if that's a problem.
98 if { $files == "" } {
99 return -2
101 remote_upload host $base.$perf_ext $base.$perf_ext
102 set fd [open $base.$perf_ext r]
103 gets $fd line
104 set val -2
105 if [regexp "TIME" $line] {
106 if [regexp "TIME -1" $line] {
107 fail "$testcase perf check: no consistent time available, $optstr"
108 set val -1
109 } elseif ![regexp "(\[0-9\]+)" "$line" val] {
110 set val -2
113 # Report problems with an existing file.
114 if { $val == -2 } {
115 fail "$testcase perf check: file $base.$perf_ext has wrong format, $optstr"
117 close $fd
118 profopt-cleanup $testcase $perf_ext
119 return $val
123 # c-prof-execute -- compile for profiling and then feedback, then normal
125 # SRC is the full pathname of the testcase.
127 proc profopt-execute { src } {
128 global srcdir tmpdir
129 global prof_option_list
130 global tool profile_option feedback_option prof_ext perf_ext perf_delta
131 global verbose
133 regsub "^$srcdir/?" $src "" testcase
134 # If we couldn't rip $srcdir out of `src' then just do the best we can.
135 # The point is to reduce the unnecessary noise in the logs. Don't strip
136 # out too much because different testcases with the same name can confuse
137 # `test-tool'.
138 if [string match "/*" $testcase] {
139 set testcase "[file tail [file dirname $src]]/[file tail $src]"
142 set executable $tmpdir/[file tail [file rootname $src].x]
144 set count 0
145 foreach option $prof_option_list {
146 set execname1 "${executable}${count}1"
147 set execname2 "${executable}${count}2"
148 set execname3 "${executable}${count}3"
149 incr count
151 remote_file build delete $execname1
152 remote_file build delete $execname2
153 remote_file build delete $execname3
154 verbose "Testing $testcase, $option" 1
156 # Remove old profiling and performance data files.
157 profopt-cleanup $testcase $prof_ext
158 if [info exists perf_ext] {
159 profopt-cleanup $testcase $perf_ext
162 # Compile for profiling.
164 set options ""
165 lappend options "additional_flags=$option $profile_option"
166 set optstr "$option $profile_option"
167 set comp_output [${tool}_target_compile "$src" "$execname1" executable $options];
168 if ![${tool}_check_compile "$testcase compilation" $optstr $execname1 $comp_output] {
169 unresolved "$testcase execution, $optstr"
170 unresolved "$testcase compilation, $option $feedback_option"
171 unresolved "$testcase execution, $option $feedback_option"
172 continue
175 # Run the profiled test.
177 set result [${tool}_load $execname1 "" ""]
178 set status [lindex $result 0]
179 # Make sure the profile data was generated, and fail if not.
180 if { $status == "pass" } {
181 set basename [file tail $testcase]
182 set base [file rootname $basename]
183 set files [glob -nocomplain $base.$prof_ext]
184 if { $files == "" } {
185 set status "fail"
186 fail "$testcase execution: file $base.$prof_ext does not exist, $option $profile_option"
187 } else {
188 $status "$testcase execution, $optstr"
190 } else {
191 $status "$testcase execution, $optstr"
193 # Quit for this round if it failed
194 if { $status != "pass" } {
195 unresolved "$testcase compilation, $option $feedback_option"
196 unresolved "$testcase execution, $option $feedback_option"
197 continue
199 remote_file build delete $execname1
201 # Compile with feedback-directed optimizations.
203 set options ""
204 lappend options "additional_flags=$option $feedback_option"
205 set optstr "$option $feedback_option"
206 set comp_output [${tool}_target_compile "$src" "$execname2" "executable" $options];
207 if ![${tool}_check_compile "$testcase compilation" $optstr $execname2 $comp_output] {
208 unresolved "$testcase execution, $optstr"
209 continue
212 # Run the profile-directed optimized test.
214 set result [${tool}_load "$execname2" "" ""]
215 set status [lindex $result 0]
216 $status "$testcase execution, $optstr"
217 if { $status != "pass" } {
218 continue
221 # Remove the profiling data files.
222 profopt-cleanup $testcase $prof_ext
224 # If the test is not expected to produce performance data then
225 # we're done now.
226 if ![info exists perf_ext] {
227 remote_file build delete $execname2
228 continue
231 # Get the performance data from the test built with
232 # profile-directed optimization. If the file doesn't exist or if
233 # the value is zero, skip the performance comparison.
234 set val2 [profopt-perf-value $testcase $perf_ext $optstr]
235 if { $val2 <= 0 } {
236 remote_file build delete $execname2
237 continue
240 # Compile with normal optimizations.
242 set options ""
243 lappend options "additional_flags=$option"
244 set optstr "$option"
245 set comp_output [${tool}_target_compile "$src" "$execname3" "executable" $options];
246 if ![${tool}_check_compile "$testcase compilation" $optstr $execname3 $comp_output] {
247 unresolved "$testcase execution, $optstr"
248 unresolved "$testcase perf check, $optstr"
249 continue
252 # Run the test with normal optimizations.
254 set result [${tool}_load "$execname3" "" ""]
255 set status [lindex $result 0]
256 $status "$testcase execution, $optstr"
257 if { $status != "pass" } {
258 unresolved "$testcase perf check, $optstr"
259 continue
262 # Get the performance data from the test built with normal
263 # optimization.
264 set val1 [profopt-perf-value $testcase $perf_ext $optstr]
265 if { $val1 < 0 } {
266 if { $val1 == -2 } {
267 # The data file existed with the profile-directed
268 # optimization so this one should, too.
269 fail "$testcase perf check: file $base.$perf_ext does not exist, $optstr"
271 continue
274 # Compare results of the two runs and fail if the time with the
275 # profile-directed optimization is significantly more than the time
276 # without it.
277 set status "pass"
278 if { $val2 > $val1 } {
279 # Check for a performance degration outside of allowable limits.
280 if { [expr $val2 - $val1] > [expr [expr $val1 * $perf_delta] / 100] } {
281 set status "fail"
284 if { $status == "fail" } {
285 fail "$testcase perf check: orig: $val1 new: $val2, $optstr"
286 } else {
287 $status "$testcase perf check, $optstr"
288 verbose "$testcase orig: $val1 new: $val2, $optstr" 2
289 remote_file build delete $execname2
290 remote_file build delete $execname3