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, |
Reid Spencer | a8c3ff4 | 2006-11-16 18:32:47 +0000 | [diff] [blame] | 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 |
| 17 | # debug. |
Reid Spencer | 28b7c7f | 2006-11-09 00:26:17 +0000 | [diff] [blame] | 18 | # |
| 19 | # Usage: |
| 20 | # findmisopt bcfile outdir progargs [match] |
| 21 | # |
| 22 | # Where: |
| 23 | # bcfile |
| 24 | # is the bytecode file input (the unoptimized working case) |
| 25 | # outdir |
| 26 | # is a directory into which intermediate results are placed |
| 27 | # progargs |
| 28 | # is a single argument containing all the arguments the program needs |
Reid Spencer | a8c3ff4 | 2006-11-16 18:32:47 +0000 | [diff] [blame] | 29 | # match |
Reid Spencer | 28b7c7f | 2006-11-09 00:26:17 +0000 | [diff] [blame] | 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 |
| 33 | # output. |
| 34 | # |
Reid Spencer | e4d8f33 | 2006-11-09 00:50:32 +0000 | [diff] [blame] | 35 | if [ "$#" -lt 3 ] ; then |
| 36 | echo "usage: findmisopt bcfile outdir progargs [match]" |
| 37 | exit 1 |
| 38 | fi |
| 39 | |
Reid Spencer | a8c3ff4 | 2006-11-16 18:32:47 +0000 | [diff] [blame] | 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" |
| 45 | else |
| 46 | echo "findmisopt: bugpoint not found" |
| 47 | exit 1 |
| 48 | fi |
| 49 | |
Reid Spencer | 28b7c7f | 2006-11-09 00:26:17 +0000 | [diff] [blame] | 50 | bcfile="$1" |
| 51 | outdir="$2" |
| 52 | args="$3" |
| 53 | match="$4" |
| 54 | name=`basename $bcfile .bc` |
| 55 | ll="$outdir/${name}.ll" |
| 56 | s="$outdir/${name}.s" |
| 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 | |
| 65 | echo "Test Name: $name" |
| 66 | echo "Unoptimized program: $prog" |
| 67 | echo " Optimized program: $optprog" |
| 68 | |
Reid Spencer | 0244099 | 2006-11-13 16:08:51 +0000 | [diff] [blame] | 69 | # Create output directory if it doesn't exist |
| 70 | if [ -f "$outdir" ] ; then |
| 71 | echo "$outdir is not a directory" |
| 72 | exit 1 |
| 73 | fi |
| 74 | |
| 75 | if [ ! -d "$outdir" ] ; then |
| 76 | mkdir "$outdir" || exit 1 |
| 77 | fi |
Reid Spencer | 28b7c7f | 2006-11-09 00:26:17 +0000 | [diff] [blame] | 78 | |
| 79 | # Generate the disassembly |
| 80 | llvm-dis "$bcfile" -o "$ll" -f || exit 1 |
| 81 | |
| 82 | # Generate the non-optimized program |
| 83 | llc "$bcfile" -o "$s" -f || exit 1 |
Reid Spencer | 2fe47fc | 2006-11-11 10:22:48 +0000 | [diff] [blame] | 84 | gcc "$s" -o "$prog" -lstdc++ -lc -lm || exit 1 |
Reid Spencer | 28b7c7f | 2006-11-09 00:26:17 +0000 | [diff] [blame] | 85 | |
Reid Spencer | a8c3ff4 | 2006-11-16 18:32:47 +0000 | [diff] [blame] | 86 | # Define the list of optimizations to run. This comprises the same set of |
| 87 | # optimizations that gccas and gccld run, in the same order. |
Reid Spencer | 5b31cf0 | 2006-11-18 17:14:09 +0000 | [diff] [blame] | 88 | 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" |
| 89 | # Here's an alternative list of optimizations comprising just the ones that |
| 90 | # gccld uses. To use, just comment out the line above, and uncomment this one |
| 91 | #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" |
Reid Spencer | 28b7c7f | 2006-11-09 00:26:17 +0000 | [diff] [blame] | 92 | |
| 93 | # Current set of switches is empty |
| 94 | function tryit { |
| 95 | switches_to_use="$1" |
| 96 | opt $switches_to_use "$bcfile" -o "$optbc" -f || exit |
| 97 | llvm-dis "$optbc" -o "$optll" -f || exit |
| 98 | llc "$optbc" -o "$opts" -f || exit |
Reid Spencer | 2fe47fc | 2006-11-11 10:22:48 +0000 | [diff] [blame] | 99 | gcc "$opts" -o "$optprog" -lstdc++ -lc -lm || exit |
Reid Spencer | 0244099 | 2006-11-13 16:08:51 +0000 | [diff] [blame] | 100 | "$prog" $args > "$out" 2>&1 |
Reid Spencer | 28b7c7f | 2006-11-09 00:26:17 +0000 | [diff] [blame] | 101 | ex1=$? |
Reid Spencer | 0244099 | 2006-11-13 16:08:51 +0000 | [diff] [blame] | 102 | "$optprog" $args > "$optout" 2>&1 |
Reid Spencer | 28b7c7f | 2006-11-09 00:26:17 +0000 | [diff] [blame] | 103 | ex2=$? |
| 104 | |
| 105 | if [ -n "$match" ] ; then |
| 106 | if [ "$ex1" -ne "$ex2" ] ; then |
| 107 | echo "Return code not the same with these switches:" |
| 108 | echo $switches |
| 109 | echo "Unoptimized returned: $ex1" |
| 110 | echo "Optimized returned: $ex2" |
| 111 | return 0 |
| 112 | fi |
| 113 | else |
| 114 | diff "$out" "$optout" > /dev/null |
| 115 | if [ $? -ne 0 ] ; then |
| 116 | echo "Diff fails with these switches:" |
| 117 | echo $switches |
| 118 | echo "Differences:" |
Reid Spencer | ba07a69 | 2006-11-25 04:07:06 +0000 | [diff] [blame^] | 119 | diff "$out" "$optout" | head |
Reid Spencer | 28b7c7f | 2006-11-09 00:26:17 +0000 | [diff] [blame] | 120 | return 0; |
| 121 | fi |
| 122 | fi |
| 123 | return 1 |
| 124 | } |
| 125 | |
Reid Spencer | 0244099 | 2006-11-13 16:08:51 +0000 | [diff] [blame] | 126 | echo "Trying to find optimization that breaks program:" |
Reid Spencer | 28b7c7f | 2006-11-09 00:26:17 +0000 | [diff] [blame] | 127 | for sw in $all_switches ; do |
Reid Spencer | 0244099 | 2006-11-13 16:08:51 +0000 | [diff] [blame] | 128 | echo -n " $sw" |
Reid Spencer | 28b7c7f | 2006-11-09 00:26:17 +0000 | [diff] [blame] | 129 | switches="$switches $sw" |
| 130 | if tryit "$switches" ; then |
| 131 | break; |
| 132 | fi |
| 133 | done |
| 134 | |
Reid Spencer | 6d0fbd4 | 2006-11-22 03:46:45 +0000 | [diff] [blame] | 135 | # Terminate the previous output with a newline |
| 136 | echo "" |
| 137 | |
| 138 | # Determine if we're done because none of the optimizations broke the program |
| 139 | if [ "$switches" == " $all_switches" ] ; then |
| 140 | echo "The program did not miscompile" |
| 141 | exit 0 |
| 142 | fi |
| 143 | |
Reid Spencer | 28b7c7f | 2006-11-09 00:26:17 +0000 | [diff] [blame] | 144 | final="" |
| 145 | while [ ! -z "$switches" ] ; do |
| 146 | trimmed=`echo "$switches" | sed -e 's/^ *\(-[^ ]*\).*/\1/'` |
| 147 | switches=`echo "$switches" | sed -e 's/^ *-[^ ]* *//'` |
| 148 | echo "Trimmed $trimmed from left" |
| 149 | tryit "$final $switches" |
| 150 | if [ "$?" -eq "0" ] ; then |
| 151 | echo "Still Failing .. continuing ..." |
| 152 | continue |
| 153 | else |
| 154 | echo "Found required early pass: $trimmed" |
| 155 | final="$final $trimmed" |
| 156 | continue |
| 157 | fi |
| 158 | echo "Next Loop" |
| 159 | done |
| 160 | |
Reid Spencer | dd2b955 | 2006-11-09 01:47:04 +0000 | [diff] [blame] | 161 | if [ "$final" == " $all_switches" ] ; then |
Reid Spencer | 0244099 | 2006-11-13 16:08:51 +0000 | [diff] [blame] | 162 | echo "findmisopt: All optimizations pass. Perhaps this isn't a misopt?" |
Reid Spencer | dd2b955 | 2006-11-09 01:47:04 +0000 | [diff] [blame] | 163 | exit 0 |
| 164 | fi |
| 165 | echo "Smallest Optimization list=$final" |
Reid Spencer | a8c3ff4 | 2006-11-16 18:32:47 +0000 | [diff] [blame] | 166 | |
| 167 | bpcmd="$bugpoint -run-llc -disable-loop-extraction --output "$out" --input /dev/null $bcfile $final --args $args" |
Reid Spencer | 28b7c7f | 2006-11-09 00:26:17 +0000 | [diff] [blame] | 168 | |
| 169 | echo "Running: $bpcmd" |
| 170 | $bpcmd |
Reid Spencer | dd2b955 | 2006-11-09 01:47:04 +0000 | [diff] [blame] | 171 | echo "findmisopt finished." |