Print a usage message if too few arguments to program.
[oota-llvm.git] / utils / findmisopt
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 if [ "$#" -lt 3 ] ; then
34   echo "usage: findmisopt bcfile outdir progargs [match]"
35   exit 1
36 fi
37
38 bcfile="$1"
39 outdir="$2"
40 args="$3"
41 match="$4"
42 name=`basename $bcfile .bc`
43 ll="$outdir/${name}.ll"
44 s="$outdir/${name}.s"
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"
52
53 echo "Test Name: $name"
54 echo "Unoptimized program: $prog"
55 echo "  Optimized program: $optprog"
56
57
58 # Generate the disassembly
59 llvm-dis "$bcfile" -o "$ll" -f || exit 1
60
61 # Generate the non-optimized program
62 llc "$bcfile" -o "$s" -f || exit 1
63 gcc "$s" -o "$prog" || exit 1
64
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"
67
68 # Current set of switches is empty
69 function tryit {
70   switches_to_use="$1"
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"
76   ex1=$?
77   "$optprog" $args > "$optout"
78   ex2=$?
79
80   if [ -n "$match" ] ; then
81     if [ "$ex1" -ne "$ex2" ] ; then
82       echo "Return code not the same with these switches:"
83       echo $switches
84       echo "Unoptimized returned: $ex1"
85       echo "Optimized   returned: $ex2"
86       return 0
87     fi
88   else
89     diff "$out" "$optout" > /dev/null
90     if [ $? -ne 0 ] ; then
91       echo "Diff fails with these switches:"
92       echo $switches
93       echo "Differences:"
94       diff "$out" "$optout" 
95       return 0;
96     fi
97   fi
98   return 1
99 }
100
101 for sw in $all_switches ; do
102   switches="$switches $sw"
103   if tryit "$switches" ; then
104     break;
105   fi
106 done
107
108 final=""
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 ..."
116     continue
117   else
118     echo "Found required early pass: $trimmed"
119     final="$final $trimmed"
120     continue
121   fi
122   echo "Next Loop"
123 done
124
125 echo "Smallest Optimization list= $final"
126 bpcmd="bugpoint -run-llc --output "$out" --input /dev/null $bcfile $final --args $args"
127
128 echo "Running: $bpcmd"
129 $bpcmd
130 echo "Finished."