blob: 80b8006178e98390b8469459b7d8b018eb7d55a8 [file] [log] [blame]
Reid Spencer28b7c7f2006-11-09 00:26:17 +00001#!/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#
Reid Spencere4d8f332006-11-09 00:50:32 +000033if [ "$#" -lt 3 ] ; then
34 echo "usage: findmisopt bcfile outdir progargs [match]"
35 exit 1
36fi
37
Reid Spencer28b7c7f2006-11-09 00:26:17 +000038bcfile="$1"
39outdir="$2"
40args="$3"
41match="$4"
42name=`basename $bcfile .bc`
43ll="$outdir/${name}.ll"
44s="$outdir/${name}.s"
45prog="$outdir/${name}"
46out="$outdir/${name}.out"
47optbc="$outdir/${name}.opt.bc"
48optll="$outdir/${name}.opt.ll"
49opts="$outdir/${name}.opt.s"
50optprog="$outdir/${name}.opt"
51optout="$outdir/${name}.opt.out"
52
53echo "Test Name: $name"
54echo "Unoptimized program: $prog"
55echo " Optimized program: $optprog"
56
Reid Spencer02440992006-11-13 16:08:51 +000057# Create output directory if it doesn't exist
58if [ -f "$outdir" ] ; then
59 echo "$outdir is not a directory"
60 exit 1
61fi
62
63if [ ! -d "$outdir" ] ; then
64 mkdir "$outdir" || exit 1
65fi
Reid Spencer28b7c7f2006-11-09 00:26:17 +000066
67# Generate the disassembly
68llvm-dis "$bcfile" -o "$ll" -f || exit 1
69
70# Generate the non-optimized program
71llc "$bcfile" -o "$s" -f || exit 1
Reid Spencer2fe47fc2006-11-11 10:22:48 +000072gcc "$s" -o "$prog" -lstdc++ -lc -lm || exit 1
Reid Spencer28b7c7f2006-11-09 00:26:17 +000073
74# Define the list of optimizations to run
75all_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
78function 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
Reid Spencer2fe47fc2006-11-11 10:22:48 +000083 gcc "$opts" -o "$optprog" -lstdc++ -lc -lm || exit
Reid Spencer02440992006-11-13 16:08:51 +000084 "$prog" $args > "$out" 2>&1
Reid Spencer28b7c7f2006-11-09 00:26:17 +000085 ex1=$?
Reid Spencer02440992006-11-13 16:08:51 +000086 "$optprog" $args > "$optout" 2>&1
Reid Spencer28b7c7f2006-11-09 00:26:17 +000087 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
Reid Spencer02440992006-11-13 16:08:51 +0000110echo "Trying to find optimization that breaks program:"
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000111for sw in $all_switches ; do
Reid Spencer02440992006-11-13 16:08:51 +0000112 echo -n " $sw"
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000113 switches="$switches $sw"
114 if tryit "$switches" ; then
115 break;
116 fi
117done
118
119final=""
120while [ ! -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"
134done
135
Reid Spencerdd2b9552006-11-09 01:47:04 +0000136if [ "$final" == " $all_switches" ] ; then
Reid Spencer02440992006-11-13 16:08:51 +0000137 echo "findmisopt: All optimizations pass. Perhaps this isn't a misopt?"
Reid Spencerdd2b9552006-11-09 01:47:04 +0000138 exit 0
139fi
140echo "Smallest Optimization list=$final"
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000141bpcmd="bugpoint -run-llc --output "$out" --input /dev/null $bcfile $final --args $args"
142
143echo "Running: $bpcmd"
144$bpcmd
Reid Spencerdd2b9552006-11-09 01:47:04 +0000145echo "findmisopt finished."