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.
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,
15 # 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
20 # findmisopt bcfile outdir progargs [match]
24 # is the bytecode file input (the unoptimized working case)
26 # is a directory into which intermediate results are placed
28 # is a single argument containing all the arguments the program needs
30 # 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
35 if [ "$#" -lt 3 ] ; then
36 echo "usage: findmisopt bcfile outdir progargs [match]"
40 dir="${0%%/utils/findmisopt}"
41 if [ -x "$dir/Release/bin/bugpoint" ] ; then
42 bugpoint="$dir/Release/bin/bugpoint"
43 elif [ -x "$dir/Debug/bin/bugpoint" ] ; then
44 bugpoint="$dir/Debug/bin/bugpoint"
46 echo "findmisopt: bugpoint not found"
54 name=`basename $bcfile .bc`
55 ll="$outdir/${name}.ll"
57 prog="$outdir/${name}"
58 out="$outdir/${name}.out"
59 optbc="$outdir/${name}.opt.bc"
60 optll="$outdir/${name}.opt.ll"
61 opts="$outdir/${name}.opt.s"
62 optprog="$outdir/${name}.opt"
63 optout="$outdir/${name}.opt.out"
64 ldflags="-lstdc++ -lm -ldl -lc"
66 echo "Test Name: $name"
67 echo "Unoptimized program: $prog"
68 echo " Optimized program: $optprog"
70 # Create output directory if it doesn't exist
71 if [ -f "$outdir" ] ; then
72 echo "$outdir is not a directory"
76 if [ ! -d "$outdir" ] ; then
77 mkdir "$outdir" || exit 1
80 # Generate the disassembly
81 llvm-dis "$bcfile" -o "$ll" -f || exit 1
83 # Generate the non-optimized program
84 llc "$bcfile" -o "$s" -f || exit 1
85 gcc "$s" -o "$prog" $ldflags || exit 1
87 # Define the list of optimizations to run. This comprises the same set of
88 # optimizations that gccas and gccld run, in the same order.
89 all_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"
94 # Current set of switches is empty
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
100 gcc "$opts" -o "$optprog" $ldflags || exit
101 "$prog" $args > "$out" 2>&1
103 "$optprog" $args > "$optout" 2>&1
106 if [ -n "$match" ] ; then
107 if [ "$ex1" -ne "$ex2" ] ; then
108 echo "Return code not the same with these switches:"
110 echo "Unoptimized returned: $ex1"
111 echo "Optimized returned: $ex2"
115 diff "$out" "$optout" > /dev/null
116 if [ $? -ne 0 ] ; then
117 echo "Diff fails with these switches:"
120 diff "$out" "$optout" | head
127 echo "Trying to find optimization that breaks program:"
128 for sw in $all_switches ; do
130 switches="$switches $sw"
131 if tryit "$switches" ; then
136 # Terminate the previous output with a newline
139 # Determine if we're done because none of the optimizations broke the program
140 if [ "$switches" == " $all_switches" ] ; then
141 echo "The program did not miscompile"
146 while [ ! -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 ..."
155 echo "Found required early pass: $trimmed"
156 final="$final $trimmed"
162 if [ "$final" == " $all_switches" ] ; then
163 echo "findmisopt: All optimizations pass. Perhaps this isn't a misopt?"
166 echo "Smallest Optimization list=$final"
168 bpcmd="$bugpoint -run-llc -disable-loop-extraction --output "$out" --input /dev/null $bcfile $final --args $args"
170 echo "Running: $bpcmd"
172 echo "findmisopt finished."