blob: 8ee9cd8c100ed2504e6e73787bf4c5ad326b1be1 [file] [log] [blame]
Reid Spencer28b7c7f2006-11-09 00:26:17 +00001#!/bin/bash
2#
3# findmisopt
4#
5# This is a quick and dirty hack to potentially find a misoptimization
6# problem. Mostly its to work around problems in bugpoint that prevent
7# it from finding a problem unless the set of failing optimizations are
8# known and given to it on the command line.
9#
10# Given a bytecode file that produces correct output (or return code),
11# this script will run through all the optimizations passes that gccas
12# uses (in the same order) and will narrow down which optimizations
13# cause the program either generate different output or return a
14# different result code. When the passes have been narrowed down,
Reid Spencera8c3ff42006-11-16 18:32:47 +000015# bugpoint is invoked to further refine the problem to its origin. If a
16# release version of bugpoint is available it will be used, otherwise
17# debug.
Reid Spencer28b7c7f2006-11-09 00:26:17 +000018#
19# Usage:
20# findmisopt bcfile outdir progargs [match]
21#
22# Where:
23# bcfile
24# is the bytecode file input (the unoptimized working case)
25# outdir
26# is a directory into which intermediate results are placed
27# progargs
28# is a single argument containing all the arguments the program needs
Reid Spencera8c3ff42006-11-16 18:32:47 +000029# match
Reid Spencer28b7c7f2006-11-09 00:26:17 +000030# if specified to any value causes the result code of the program to
31# be used to determine success/fail. If not specified success/fail is
32# determined by diffing the program's output with the non-optimized
33# output.
34#
Reid Spencere4d8f332006-11-09 00:50:32 +000035if [ "$#" -lt 3 ] ; then
36 echo "usage: findmisopt bcfile outdir progargs [match]"
37 exit 1
38fi
39
Reid Spencera8c3ff42006-11-16 18:32:47 +000040dir="${0%%/utils/findmisopt}"
41if [ -x "$dir/Release/bin/bugpoint" ] ; then
42 bugpoint="$dir/Release/bin/bugpoint"
43elif [ -x "$dir/Debug/bin/bugpoint" ] ; then
44 bugpoint="$dir/Debug/bin/bugpoint"
45else
46 echo "findmisopt: bugpoint not found"
47 exit 1
48fi
49
Reid Spencer28b7c7f2006-11-09 00:26:17 +000050bcfile="$1"
51outdir="$2"
52args="$3"
53match="$4"
54name=`basename $bcfile .bc`
55ll="$outdir/${name}.ll"
56s="$outdir/${name}.s"
57prog="$outdir/${name}"
58out="$outdir/${name}.out"
59optbc="$outdir/${name}.opt.bc"
60optll="$outdir/${name}.opt.ll"
61opts="$outdir/${name}.opt.s"
62optprog="$outdir/${name}.opt"
63optout="$outdir/${name}.opt.out"
Reid Spencerf8463a32006-12-08 18:58:38 +000064ldflags="-lstdc++ -lm -ldl -lc"
Reid Spencer28b7c7f2006-11-09 00:26:17 +000065
66echo "Test Name: $name"
67echo "Unoptimized program: $prog"
68echo " Optimized program: $optprog"
69
Reid Spencer02440992006-11-13 16:08:51 +000070# Create output directory if it doesn't exist
71if [ -f "$outdir" ] ; then
72 echo "$outdir is not a directory"
73 exit 1
74fi
75
76if [ ! -d "$outdir" ] ; then
77 mkdir "$outdir" || exit 1
78fi
Reid Spencer28b7c7f2006-11-09 00:26:17 +000079
80# Generate the disassembly
81llvm-dis "$bcfile" -o "$ll" -f || exit 1
82
83# Generate the non-optimized program
84llc "$bcfile" -o "$s" -f || exit 1
Reid Spencerf8463a32006-12-08 18:58:38 +000085gcc "$s" -o "$prog" $ldflags || exit 1
Reid Spencer28b7c7f2006-11-09 00:26:17 +000086
Reid Spencera8c3ff42006-11-16 18:32:47 +000087# Define the list of optimizations to run. This comprises the same set of
88# optimizations that gccas and gccld run, in the same order.
Reid Spencer5b31cf02006-11-18 17:14:09 +000089all_switches="-verify -lowersetjmp -funcresolve -raiseallocs -simplifycfg -mem2reg -globalopt -globaldce -ipconstprop -deadargelim -instcombine -simplifycfg -prune-eh -inline -simplify-libcalls -argpromotion -raise -tailduplicate -simplifycfg -scalarrepl -instcombine -predsimplify -condprop -tailcallelim -simplifycfg -reassociate -licm -loop-unswitch -instcombine -indvars -loop-unroll -instcombine -load-vn -gcse -sccp -instcombine -condprop -dse -dce -simplifycfg -deadtypeelim -constmerge -funcresolve -internalize -ipsccp -globalopt -constmerge -deadargelim -inline -prune-eh -globalopt -globaldce -argpromotion -instcombine -predsimplify -scalarrepl -globalsmodref-aa -licm -load-vn -gcse -dse -instcombine -simplifycfg -verify"
90# Here's an alternative list of optimizations comprising just the ones that
91# gccld uses. To use, just comment out the line above, and uncomment this one
92#all_switches="-funcresolve -internalize -ipsccp -globalopt -constmerge -deadargelim -inline -prune-eh -globalopt -globaldce -argpromotion -instcombine -predsimplify -scalarrepl -globalsmodref-aa -licm -load-vn -gcse -dse -instcombine -simplifycfg -verify"
Reid Spencer28b7c7f2006-11-09 00:26:17 +000093
94# Current set of switches is empty
95function tryit {
96 switches_to_use="$1"
97 opt $switches_to_use "$bcfile" -o "$optbc" -f || exit
98 llvm-dis "$optbc" -o "$optll" -f || exit
99 llc "$optbc" -o "$opts" -f || exit
Reid Spencerf8463a32006-12-08 18:58:38 +0000100 gcc "$opts" -o "$optprog" $ldflags || exit
Reid Spencer02440992006-11-13 16:08:51 +0000101 "$prog" $args > "$out" 2>&1
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000102 ex1=$?
Reid Spencer02440992006-11-13 16:08:51 +0000103 "$optprog" $args > "$optout" 2>&1
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000104 ex2=$?
105
106 if [ -n "$match" ] ; then
107 if [ "$ex1" -ne "$ex2" ] ; then
108 echo "Return code not the same with these switches:"
109 echo $switches
110 echo "Unoptimized returned: $ex1"
111 echo "Optimized returned: $ex2"
112 return 0
113 fi
114 else
115 diff "$out" "$optout" > /dev/null
116 if [ $? -ne 0 ] ; then
117 echo "Diff fails with these switches:"
118 echo $switches
119 echo "Differences:"
Reid Spencerba07a692006-11-25 04:07:06 +0000120 diff "$out" "$optout" | head
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000121 return 0;
122 fi
123 fi
124 return 1
125}
126
Reid Spencer02440992006-11-13 16:08:51 +0000127echo "Trying to find optimization that breaks program:"
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000128for sw in $all_switches ; do
Reid Spencer02440992006-11-13 16:08:51 +0000129 echo -n " $sw"
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000130 switches="$switches $sw"
131 if tryit "$switches" ; then
132 break;
133 fi
134done
135
Reid Spencer6d0fbd42006-11-22 03:46:45 +0000136# Terminate the previous output with a newline
137echo ""
138
139# Determine if we're done because none of the optimizations broke the program
140if [ "$switches" == " $all_switches" ] ; then
141 echo "The program did not miscompile"
142 exit 0
143fi
144
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000145final=""
146while [ ! -z "$switches" ] ; do
147 trimmed=`echo "$switches" | sed -e 's/^ *\(-[^ ]*\).*/\1/'`
148 switches=`echo "$switches" | sed -e 's/^ *-[^ ]* *//'`
149 echo "Trimmed $trimmed from left"
150 tryit "$final $switches"
151 if [ "$?" -eq "0" ] ; then
152 echo "Still Failing .. continuing ..."
153 continue
154 else
155 echo "Found required early pass: $trimmed"
156 final="$final $trimmed"
157 continue
158 fi
159 echo "Next Loop"
160done
161
Reid Spencerdd2b9552006-11-09 01:47:04 +0000162if [ "$final" == " $all_switches" ] ; then
Reid Spencer02440992006-11-13 16:08:51 +0000163 echo "findmisopt: All optimizations pass. Perhaps this isn't a misopt?"
Reid Spencerdd2b9552006-11-09 01:47:04 +0000164 exit 0
165fi
166echo "Smallest Optimization list=$final"
Reid Spencera8c3ff42006-11-16 18:32:47 +0000167
168bpcmd="$bugpoint -run-llc -disable-loop-extraction --output "$out" --input /dev/null $bcfile $final --args $args"
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000169
170echo "Running: $bpcmd"
171$bpcmd
Reid Spencerdd2b9552006-11-09 01:47:04 +0000172echo "findmisopt finished."