blob: 15d72097fbe9139e44e211ad9e61028d4c38e719 [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#
33bcfile="$1"
34outdir="$2"
35args="$3"
36match="$4"
37name=`basename $bcfile .bc`
38ll="$outdir/${name}.ll"
39s="$outdir/${name}.s"
40prog="$outdir/${name}"
41out="$outdir/${name}.out"
42optbc="$outdir/${name}.opt.bc"
43optll="$outdir/${name}.opt.ll"
44opts="$outdir/${name}.opt.s"
45optprog="$outdir/${name}.opt"
46optout="$outdir/${name}.opt.out"
47
48echo "Test Name: $name"
49echo "Unoptimized program: $prog"
50echo " Optimized program: $optprog"
51
52
53# Generate the disassembly
54llvm-dis "$bcfile" -o "$ll" -f || exit 1
55
56# Generate the non-optimized program
57llc "$bcfile" -o "$s" -f || exit 1
58gcc "$s" -o "$prog" || exit 1
59
60# Define the list of optimizations to run
61all_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"
62
63# Current set of switches is empty
64function tryit {
65 switches_to_use="$1"
66 opt $switches_to_use "$bcfile" -o "$optbc" -f || exit
67 llvm-dis "$optbc" -o "$optll" -f || exit
68 llc "$optbc" -o "$opts" -f || exit
69 gcc "$opts" -o "$optprog" || exit
70 "$prog" $args > "$out"
71 ex1=$?
72 "$optprog" $args > "$optout"
73 ex2=$?
74
75 if [ -n "$match" ] ; then
76 if [ "$ex1" -ne "$ex2" ] ; then
77 echo "Return code not the same with these switches:"
78 echo $switches
79 echo "Unoptimized returned: $ex1"
80 echo "Optimized returned: $ex2"
81 return 0
82 fi
83 else
84 diff "$out" "$optout" > /dev/null
85 if [ $? -ne 0 ] ; then
86 echo "Diff fails with these switches:"
87 echo $switches
88 echo "Differences:"
89 diff "$out" "$optout"
90 return 0;
91 fi
92 fi
93 return 1
94}
95
96for sw in $all_switches ; do
97 switches="$switches $sw"
98 if tryit "$switches" ; then
99 break;
100 fi
101done
102
103final=""
104while [ ! -z "$switches" ] ; do
105 trimmed=`echo "$switches" | sed -e 's/^ *\(-[^ ]*\).*/\1/'`
106 switches=`echo "$switches" | sed -e 's/^ *-[^ ]* *//'`
107 echo "Trimmed $trimmed from left"
108 tryit "$final $switches"
109 if [ "$?" -eq "0" ] ; then
110 echo "Still Failing .. continuing ..."
111 continue
112 else
113 echo "Found required early pass: $trimmed"
114 final="$final $trimmed"
115 continue
116 fi
117 echo "Next Loop"
118done
119
120echo "Smallest Optimization list= $final"
121bpcmd="bugpoint -run-llc --output "$out" --input /dev/null $bcfile $final --args $args"
122
123echo "Running: $bpcmd"
124$bpcmd
125echo "Finished."