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.
18 # findmisopt bcfile outdir progargs [match]
22 # is the bytecode file input (the unoptimized working case)
24 # is a directory into which intermediate results are placed
26 # is a single argument containing all the arguments the program needs
28 # if specified to any value causes the result code of the program to
29 # be used to determine success/fail. If not specified success/fail is
30 # determined by diffing the program's output with the non-optimized
33 if [ "$#" -lt 3 ] ; then
34 echo "usage: findmisopt bcfile outdir progargs [match]"
42 name=`basename $bcfile .bc`
43 ll="$outdir/${name}.ll"
45 prog="$outdir/${name}"
46 out="$outdir/${name}.out"
47 optbc="$outdir/${name}.opt.bc"
48 optll="$outdir/${name}.opt.ll"
49 opts="$outdir/${name}.opt.s"
50 optprog="$outdir/${name}.opt"
51 optout="$outdir/${name}.opt.out"
53 echo "Test Name: $name"
54 echo "Unoptimized program: $prog"
55 echo " Optimized program: $optprog"
57 # Create output directory if it doesn't exist
58 if [ -f "$outdir" ] ; then
59 echo "$outdir is not a directory"
63 if [ ! -d "$outdir" ] ; then
64 mkdir "$outdir" || exit 1
67 # Generate the disassembly
68 llvm-dis "$bcfile" -o "$ll" -f || exit 1
70 # Generate the non-optimized program
71 llc "$bcfile" -o "$s" -f || exit 1
72 gcc "$s" -o "$prog" -lstdc++ -lc -lm || exit 1
74 # Define the list of optimizations to run
75 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"
77 # Current set of switches is empty
80 opt $switches_to_use "$bcfile" -o "$optbc" -f || exit
81 llvm-dis "$optbc" -o "$optll" -f || exit
82 llc "$optbc" -o "$opts" -f || exit
83 gcc "$opts" -o "$optprog" -lstdc++ -lc -lm || exit
84 "$prog" $args > "$out" 2>&1
86 "$optprog" $args > "$optout" 2>&1
89 if [ -n "$match" ] ; then
90 if [ "$ex1" -ne "$ex2" ] ; then
91 echo "Return code not the same with these switches:"
93 echo "Unoptimized returned: $ex1"
94 echo "Optimized returned: $ex2"
98 diff "$out" "$optout" > /dev/null
99 if [ $? -ne 0 ] ; then
100 echo "Diff fails with these switches:"
103 diff "$out" "$optout"
110 echo "Trying to find optimization that breaks program:"
111 for sw in $all_switches ; do
113 switches="$switches $sw"
114 if tryit "$switches" ; then
120 while [ ! -z "$switches" ] ; do
121 trimmed=`echo "$switches" | sed -e 's/^ *\(-[^ ]*\).*/\1/'`
122 switches=`echo "$switches" | sed -e 's/^ *-[^ ]* *//'`
123 echo "Trimmed $trimmed from left"
124 tryit "$final $switches"
125 if [ "$?" -eq "0" ] ; then
126 echo "Still Failing .. continuing ..."
129 echo "Found required early pass: $trimmed"
130 final="$final $trimmed"
136 if [ "$final" == " $all_switches" ] ; then
137 echo "findmisopt: All optimizations pass. Perhaps this isn't a misopt?"
140 echo "Smallest Optimization list=$final"
141 bpcmd="bugpoint -run-llc --output "$out" --input /dev/null $bcfile $final --args $args"
143 echo "Running: $bpcmd"
145 echo "findmisopt finished."