blob: cb390aa7ef17b00662fa3e89cd910b9082117023 [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,
Reid Spencera8c3ff42006-11-16 18:32:47 +000015# 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 Spencer28b7c7f2006-11-09 00:26:17 +000018#
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 Spencera8c3ff42006-11-16 18:32:47 +000029# match
Reid Spencer28b7c7f2006-11-09 00:26:17 +000030# 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 Spencere4d8f332006-11-09 00:50:32 +000035if [ "$#" -lt 3 ] ; then
36 echo "usage: findmisopt bcfile outdir progargs [match]"
37 exit 1
38fi
39
Reid Spencera8c3ff42006-11-16 18:32:47 +000040dir="${0%%/utils/findmisopt}"
41if [ -x "$dir/Release/bin/bugpoint" ] ; then
42 bugpoint="$dir/Release/bin/bugpoint"
43elif [ -x "$dir/Debug/bin/bugpoint" ] ; then
44 bugpoint="$dir/Debug/bin/bugpoint"
45else
46 echo "findmisopt: bugpoint not found"
47 exit 1
48fi
49
Reid Spencer28b7c7f2006-11-09 00:26:17 +000050bcfile="$1"
51outdir="$2"
52args="$3"
53match="$4"
54name=`basename $bcfile .bc`
55ll="$outdir/${name}.ll"
56s="$outdir/${name}.s"
57prog="$outdir/${name}"
58out="$outdir/${name}.out"
59optbc="$outdir/${name}.opt.bc"
60optll="$outdir/${name}.opt.ll"
61opts="$outdir/${name}.opt.s"
62optprog="$outdir/${name}.opt"
63optout="$outdir/${name}.opt.out"
64
65echo "Test Name: $name"
66echo "Unoptimized program: $prog"
67echo " Optimized program: $optprog"
68
Reid Spencer02440992006-11-13 16:08:51 +000069# Create output directory if it doesn't exist
70if [ -f "$outdir" ] ; then
71 echo "$outdir is not a directory"
72 exit 1
73fi
74
75if [ ! -d "$outdir" ] ; then
76 mkdir "$outdir" || exit 1
77fi
Reid Spencer28b7c7f2006-11-09 00:26:17 +000078
79# Generate the disassembly
80llvm-dis "$bcfile" -o "$ll" -f || exit 1
81
82# Generate the non-optimized program
83llc "$bcfile" -o "$s" -f || exit 1
Reid Spencer2fe47fc2006-11-11 10:22:48 +000084gcc "$s" -o "$prog" -lstdc++ -lc -lm || exit 1
Reid Spencer28b7c7f2006-11-09 00:26:17 +000085
Reid Spencera8c3ff42006-11-16 18:32:47 +000086# Define the list of optimizations to run. This comprises the same set of
87# optimizations that gccas and gccld run, in the same order.
88all_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 -simplify-cfg -verify"
Reid Spencer28b7c7f2006-11-09 00:26:17 +000089
90# Current set of switches is empty
91function tryit {
92 switches_to_use="$1"
93 opt $switches_to_use "$bcfile" -o "$optbc" -f || exit
94 llvm-dis "$optbc" -o "$optll" -f || exit
95 llc "$optbc" -o "$opts" -f || exit
Reid Spencer2fe47fc2006-11-11 10:22:48 +000096 gcc "$opts" -o "$optprog" -lstdc++ -lc -lm || exit
Reid Spencer02440992006-11-13 16:08:51 +000097 "$prog" $args > "$out" 2>&1
Reid Spencer28b7c7f2006-11-09 00:26:17 +000098 ex1=$?
Reid Spencer02440992006-11-13 16:08:51 +000099 "$optprog" $args > "$optout" 2>&1
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000100 ex2=$?
101
102 if [ -n "$match" ] ; then
103 if [ "$ex1" -ne "$ex2" ] ; then
104 echo "Return code not the same with these switches:"
105 echo $switches
106 echo "Unoptimized returned: $ex1"
107 echo "Optimized returned: $ex2"
108 return 0
109 fi
110 else
111 diff "$out" "$optout" > /dev/null
112 if [ $? -ne 0 ] ; then
113 echo "Diff fails with these switches:"
114 echo $switches
115 echo "Differences:"
116 diff "$out" "$optout"
117 return 0;
118 fi
119 fi
120 return 1
121}
122
Reid Spencer02440992006-11-13 16:08:51 +0000123echo "Trying to find optimization that breaks program:"
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000124for sw in $all_switches ; do
Reid Spencer02440992006-11-13 16:08:51 +0000125 echo -n " $sw"
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000126 switches="$switches $sw"
127 if tryit "$switches" ; then
128 break;
129 fi
130done
131
132final=""
133while [ ! -z "$switches" ] ; do
134 trimmed=`echo "$switches" | sed -e 's/^ *\(-[^ ]*\).*/\1/'`
135 switches=`echo "$switches" | sed -e 's/^ *-[^ ]* *//'`
136 echo "Trimmed $trimmed from left"
137 tryit "$final $switches"
138 if [ "$?" -eq "0" ] ; then
139 echo "Still Failing .. continuing ..."
140 continue
141 else
142 echo "Found required early pass: $trimmed"
143 final="$final $trimmed"
144 continue
145 fi
146 echo "Next Loop"
147done
148
Reid Spencerdd2b9552006-11-09 01:47:04 +0000149if [ "$final" == " $all_switches" ] ; then
Reid Spencer02440992006-11-13 16:08:51 +0000150 echo "findmisopt: All optimizations pass. Perhaps this isn't a misopt?"
Reid Spencerdd2b9552006-11-09 01:47:04 +0000151 exit 0
152fi
153echo "Smallest Optimization list=$final"
Reid Spencera8c3ff42006-11-16 18:32:47 +0000154
155bpcmd="$bugpoint -run-llc -disable-loop-extraction --output "$out" --input /dev/null $bcfile $final --args $args"
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000156
157echo "Running: $bpcmd"
158$bpcmd
Reid Spencerdd2b9552006-11-09 01:47:04 +0000159echo "findmisopt finished."