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
37 name=`basename $bcfile .bc`
38 ll="$outdir/${name}.ll"
40 prog="$outdir/${name}"
41 out="$outdir/${name}.out"
42 optbc="$outdir/${name}.opt.bc"
43 optll="$outdir/${name}.opt.ll"
44 opts="$outdir/${name}.opt.s"
45 optprog="$outdir/${name}.opt"
46 optout="$outdir/${name}.opt.out"
48 echo "Test Name: $name"
49 echo "Unoptimized program: $prog"
50 echo " Optimized program: $optprog"
53 # Generate the disassembly
54 llvm-dis "$bcfile" -o "$ll" -f || exit 1
56 # Generate the non-optimized program
57 llc "$bcfile" -o "$s" -f || exit 1
58 gcc "$s" -o "$prog" || exit 1
60 # Define the list of optimizations to run
61 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"
63 # Current set of switches is empty
66 opt $switches_to_use "$bcfile" -o "$optbc" -f || exit
67 llvm-dis "$optbc" -o "$optll" -f || exit
68 llc "$optbc" -o "$opts" -f || exit
69 gcc "$opts" -o "$optprog" || exit
70 "$prog" $args > "$out"
72 "$optprog" $args > "$optout"
75 if [ -n "$match" ] ; then
76 if [ "$ex1" -ne "$ex2" ] ; then
77 echo "Return code not the same with these switches:"
79 echo "Unoptimized returned: $ex1"
80 echo "Optimized returned: $ex2"
84 diff "$out" "$optout" > /dev/null
85 if [ $? -ne 0 ] ; then
86 echo "Diff fails with these switches:"
96 for sw in $all_switches ; do
97 switches="$switches $sw"
98 if tryit "$switches" ; then
104 while [ ! -z "$switches" ] ; do
105 trimmed=`echo "$switches" | sed -e 's/^ *\(-[^ ]*\).*/\1/'`
106 switches=`echo "$switches" | sed -e 's/^ *-[^ ]* *//'`
107 echo "Trimmed $trimmed from left"
108 tryit "$final $switches"
109 if [ "$?" -eq "0" ] ; then
110 echo "Still Failing .. continuing ..."
113 echo "Found required early pass: $trimmed"
114 final="$final $trimmed"
120 echo "Smallest Optimization list= $final"
121 bpcmd="bugpoint -run-llc --output "$out" --input /dev/null $bcfile $final --args $args"
123 echo "Running: $bpcmd"