Add some output so the user is informed while they wait.
[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 # Create output directory if it doesn't exist
58 if [ -f "$outdir" ] ; then
59   echo "$outdir is not a directory"
60   exit 1
61 fi
62
63 if [ ! -d "$outdir" ] ; then
64   mkdir "$outdir" || exit 1
65 fi
66
67 # Generate the disassembly
68 llvm-dis "$bcfile" -o "$ll" -f || exit 1
69
70 # Generate the non-optimized program
71 llc "$bcfile" -o "$s" -f || exit 1
72 gcc "$s" -o "$prog" -lstdc++ -lc -lm || exit 1
73
74 # Define the list of optimizations to run
75 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"
76
77 # Current set of switches is empty
78 function tryit {
79   switches_to_use="$1"
80   opt $switches_to_use "$bcfile" -o "$optbc" -f || exit
81   llvm-dis "$optbc" -o "$optll" -f || exit
82   llc "$optbc" -o "$opts" -f || exit
83   gcc "$opts" -o "$optprog" -lstdc++ -lc -lm || exit
84   "$prog" $args > "$out" 2>&1
85   ex1=$?
86   "$optprog" $args > "$optout" 2>&1
87   ex2=$?
88
89   if [ -n "$match" ] ; then
90     if [ "$ex1" -ne "$ex2" ] ; then
91       echo "Return code not the same with these switches:"
92       echo $switches
93       echo "Unoptimized returned: $ex1"
94       echo "Optimized   returned: $ex2"
95       return 0
96     fi
97   else
98     diff "$out" "$optout" > /dev/null
99     if [ $? -ne 0 ] ; then
100       echo "Diff fails with these switches:"
101       echo $switches
102       echo "Differences:"
103       diff "$out" "$optout" 
104       return 0;
105     fi
106   fi
107   return 1
108 }
109
110 echo "Trying to find optimization that breaks program:"
111 for sw in $all_switches ; do
112   echo -n " $sw"
113   switches="$switches $sw"
114   if tryit "$switches" ; then
115     break;
116   fi
117 done
118
119 final=""
120 while [ ! -z "$switches" ] ; do
121   trimmed=`echo "$switches" | sed -e 's/^ *\(-[^ ]*\).*/\1/'`
122   switches=`echo "$switches" | sed -e 's/^ *-[^ ]* *//'`
123   echo "Trimmed $trimmed from left"
124   tryit "$final $switches"
125   if [ "$?" -eq "0" ] ; then
126     echo "Still Failing .. continuing ..."
127     continue
128   else
129     echo "Found required early pass: $trimmed"
130     final="$final $trimmed"
131     continue
132   fi
133   echo "Next Loop"
134 done
135
136 if [ "$final" == " $all_switches" ] ; then
137   echo "findmisopt: All optimizations pass. Perhaps this isn't a misopt?"
138   exit 0
139 fi
140 echo "Smallest Optimization list=$final"
141 bpcmd="bugpoint -run-llc --output "$out" --input /dev/null $bcfile $final --args $args"
142
143 echo "Running: $bpcmd"
144 $bpcmd
145 echo "findmisopt finished."