Reid Spencer | 28b7c7f | 2006-11-09 00:26:17 +0000 | [diff] [blame^] | 1 | #!/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, |
| 15 | # bugpoint is invoked to further refine the problem to its origin. |
| 16 | # |
| 17 | # Usage: |
| 18 | # findmisopt bcfile outdir progargs [match] |
| 19 | # |
| 20 | # Where: |
| 21 | # bcfile |
| 22 | # is the bytecode file input (the unoptimized working case) |
| 23 | # outdir |
| 24 | # is a directory into which intermediate results are placed |
| 25 | # progargs |
| 26 | # is a single argument containing all the arguments the program needs |
| 27 | # match |
| 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 |
| 31 | # output. |
| 32 | # |
| 33 | bcfile="$1" |
| 34 | outdir="$2" |
| 35 | args="$3" |
| 36 | match="$4" |
| 37 | name=`basename $bcfile .bc` |
| 38 | ll="$outdir/${name}.ll" |
| 39 | s="$outdir/${name}.s" |
| 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" |
| 47 | |
| 48 | echo "Test Name: $name" |
| 49 | echo "Unoptimized program: $prog" |
| 50 | echo " Optimized program: $optprog" |
| 51 | |
| 52 | |
| 53 | # Generate the disassembly |
| 54 | llvm-dis "$bcfile" -o "$ll" -f || exit 1 |
| 55 | |
| 56 | # Generate the non-optimized program |
| 57 | llc "$bcfile" -o "$s" -f || exit 1 |
| 58 | gcc "$s" -o "$prog" || exit 1 |
| 59 | |
| 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" |
| 62 | |
| 63 | # Current set of switches is empty |
| 64 | function tryit { |
| 65 | switches_to_use="$1" |
| 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" |
| 71 | ex1=$? |
| 72 | "$optprog" $args > "$optout" |
| 73 | ex2=$? |
| 74 | |
| 75 | if [ -n "$match" ] ; then |
| 76 | if [ "$ex1" -ne "$ex2" ] ; then |
| 77 | echo "Return code not the same with these switches:" |
| 78 | echo $switches |
| 79 | echo "Unoptimized returned: $ex1" |
| 80 | echo "Optimized returned: $ex2" |
| 81 | return 0 |
| 82 | fi |
| 83 | else |
| 84 | diff "$out" "$optout" > /dev/null |
| 85 | if [ $? -ne 0 ] ; then |
| 86 | echo "Diff fails with these switches:" |
| 87 | echo $switches |
| 88 | echo "Differences:" |
| 89 | diff "$out" "$optout" |
| 90 | return 0; |
| 91 | fi |
| 92 | fi |
| 93 | return 1 |
| 94 | } |
| 95 | |
| 96 | for sw in $all_switches ; do |
| 97 | switches="$switches $sw" |
| 98 | if tryit "$switches" ; then |
| 99 | break; |
| 100 | fi |
| 101 | done |
| 102 | |
| 103 | final="" |
| 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 ..." |
| 111 | continue |
| 112 | else |
| 113 | echo "Found required early pass: $trimmed" |
| 114 | final="$final $trimmed" |
| 115 | continue |
| 116 | fi |
| 117 | echo "Next Loop" |
| 118 | done |
| 119 | |
| 120 | echo "Smallest Optimization list= $final" |
| 121 | bpcmd="bugpoint -run-llc --output "$out" --input /dev/null $bcfile $final --args $args" |
| 122 | |
| 123 | echo "Running: $bpcmd" |
| 124 | $bpcmd |
| 125 | echo "Finished." |