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"
58 # Generate the disassembly
59 llvm-dis "$bcfile" -o "$ll" -f || exit 1
61 # Generate the non-optimized program
62 llc "$bcfile" -o "$s" -f || exit 1
63 gcc "$s" -o "$prog" || exit 1
65 # Define the list of optimizations to run
66 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"
68 # Current set of switches is empty
71 opt $switches_to_use "$bcfile" -o "$optbc" -f || exit
72 llvm-dis "$optbc" -o "$optll" -f || exit
73 llc "$optbc" -o "$opts" -f || exit
74 gcc "$opts" -o "$optprog" || exit
75 "$prog" $args > "$out"
77 "$optprog" $args > "$optout"
80 if [ -n "$match" ] ; then
81 if [ "$ex1" -ne "$ex2" ] ; then
82 echo "Return code not the same with these switches:"
84 echo "Unoptimized returned: $ex1"
85 echo "Optimized returned: $ex2"
89 diff "$out" "$optout" > /dev/null
90 if [ $? -ne 0 ] ; then
91 echo "Diff fails with these switches:"
101 for sw in $all_switches ; do
102 switches="$switches $sw"
103 if tryit "$switches" ; then
109 while [ ! -z "$switches" ] ; do
110 trimmed=`echo "$switches" | sed -e 's/^ *\(-[^ ]*\).*/\1/'`
111 switches=`echo "$switches" | sed -e 's/^ *-[^ ]* *//'`
112 echo "Trimmed $trimmed from left"
113 tryit "$final $switches"
114 if [ "$?" -eq "0" ] ; then
115 echo "Still Failing .. continuing ..."
118 echo "Found required early pass: $trimmed"
119 final="$final $trimmed"
125 echo "Smallest Optimization list= $final"
126 bpcmd="bugpoint -run-llc --output "$out" --input /dev/null $bcfile $final --args $args"
128 echo "Running: $bpcmd"