blob: 88f991a634c044c63b71b499b1c860d69cac8d7a [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#
Duncan Sands18d52f22010-09-29 20:09:55 +000010# Given a bitcode file that produces correct output (or return code),
Reid Spencer28b7c7f2006-11-09 00:26:17 +000011# 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
Duncan Sands18d52f22010-09-29 20:09:55 +000024# is the bitcode file input (the unoptimized working case)
Reid Spencer28b7c7f2006-11-09 00:26:17 +000025# 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 Spencer2232a802006-12-09 04:42:33 +000029# proginput
30# is a file name from which stdin should be directed
Reid Spencera8c3ff42006-11-16 18:32:47 +000031# match
Reid Spencer28b7c7f2006-11-09 00:26:17 +000032# if specified to any value causes the result code of the program to
33# be used to determine success/fail. If not specified success/fail is
34# determined by diffing the program's output with the non-optimized
35# output.
36#
Reid Spencere4d8f332006-11-09 00:50:32 +000037if [ "$#" -lt 3 ] ; then
38 echo "usage: findmisopt bcfile outdir progargs [match]"
39 exit 1
40fi
41
Reid Spencera8c3ff42006-11-16 18:32:47 +000042dir="${0%%/utils/findmisopt}"
43if [ -x "$dir/Release/bin/bugpoint" ] ; then
44 bugpoint="$dir/Release/bin/bugpoint"
45elif [ -x "$dir/Debug/bin/bugpoint" ] ; then
46 bugpoint="$dir/Debug/bin/bugpoint"
47else
48 echo "findmisopt: bugpoint not found"
49 exit 1
50fi
51
Reid Spencer28b7c7f2006-11-09 00:26:17 +000052bcfile="$1"
53outdir="$2"
54args="$3"
Reid Spencer2232a802006-12-09 04:42:33 +000055input="$4"
56if [ ! -f "$input" ] ; then
57 input="/dev/null"
58fi
59match="$5"
Reid Spencer28b7c7f2006-11-09 00:26:17 +000060name=`basename $bcfile .bc`
61ll="$outdir/${name}.ll"
62s="$outdir/${name}.s"
63prog="$outdir/${name}"
64out="$outdir/${name}.out"
65optbc="$outdir/${name}.opt.bc"
66optll="$outdir/${name}.opt.ll"
67opts="$outdir/${name}.opt.s"
68optprog="$outdir/${name}.opt"
69optout="$outdir/${name}.opt.out"
Reid Spencerf8463a32006-12-08 18:58:38 +000070ldflags="-lstdc++ -lm -ldl -lc"
Reid Spencer28b7c7f2006-11-09 00:26:17 +000071
72echo "Test Name: $name"
73echo "Unoptimized program: $prog"
74echo " Optimized program: $optprog"
75
Reid Spencerf6101ac2007-08-13 06:19:51 +000076# Define the list of optimizations to run. This comprises the same set of
77# optimizations that opt -std-compile-opts and gccld run, in the same order.
78opt_switches=`llvm-as < /dev/null -o - | opt -std-compile-opts -disable-output -debug-pass=Arguments 2>&1 | sed 's/Pass Arguments: //'`
Michael J. Spencer75338092012-04-19 19:27:54 +000079all_switches="$opt_switches"
Reid Spencerf6101ac2007-08-13 06:19:51 +000080echo "Passes : $all_switches"
81
Reid Spencer02440992006-11-13 16:08:51 +000082# Create output directory if it doesn't exist
83if [ -f "$outdir" ] ; then
84 echo "$outdir is not a directory"
85 exit 1
86fi
87
88if [ ! -d "$outdir" ] ; then
89 mkdir "$outdir" || exit 1
90fi
Reid Spencer28b7c7f2006-11-09 00:26:17 +000091
92# Generate the disassembly
93llvm-dis "$bcfile" -o "$ll" -f || exit 1
94
Reid Spencer2232a802006-12-09 04:42:33 +000095# Generate the non-optimized program and its output
Reid Spencer28b7c7f2006-11-09 00:26:17 +000096llc "$bcfile" -o "$s" -f || exit 1
Reid Spencerf8463a32006-12-08 18:58:38 +000097gcc "$s" -o "$prog" $ldflags || exit 1
Reid Spencer2232a802006-12-09 04:42:33 +000098"$prog" $args > "$out" 2>&1 <$input
99ex1=$?
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000100
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000101# Current set of switches is empty
102function tryit {
103 switches_to_use="$1"
104 opt $switches_to_use "$bcfile" -o "$optbc" -f || exit
105 llvm-dis "$optbc" -o "$optll" -f || exit
106 llc "$optbc" -o "$opts" -f || exit
Reid Spencerf8463a32006-12-08 18:58:38 +0000107 gcc "$opts" -o "$optprog" $ldflags || exit
Reid Spencer2232a802006-12-09 04:42:33 +0000108 "$optprog" $args > "$optout" 2>&1 <"$input"
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000109 ex2=$?
110
111 if [ -n "$match" ] ; then
112 if [ "$ex1" -ne "$ex2" ] ; then
113 echo "Return code not the same with these switches:"
114 echo $switches
115 echo "Unoptimized returned: $ex1"
116 echo "Optimized returned: $ex2"
117 return 0
118 fi
119 else
120 diff "$out" "$optout" > /dev/null
121 if [ $? -ne 0 ] ; then
122 echo "Diff fails with these switches:"
123 echo $switches
124 echo "Differences:"
Reid Spencerba07a692006-11-25 04:07:06 +0000125 diff "$out" "$optout" | head
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000126 return 0;
127 fi
128 fi
129 return 1
130}
131
Reid Spencer02440992006-11-13 16:08:51 +0000132echo "Trying to find optimization that breaks program:"
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000133for sw in $all_switches ; do
Reid Spencer02440992006-11-13 16:08:51 +0000134 echo -n " $sw"
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000135 switches="$switches $sw"
136 if tryit "$switches" ; then
137 break;
138 fi
139done
140
Reid Spencer6d0fbd42006-11-22 03:46:45 +0000141# Terminate the previous output with a newline
142echo ""
143
144# Determine if we're done because none of the optimizations broke the program
145if [ "$switches" == " $all_switches" ] ; then
146 echo "The program did not miscompile"
147 exit 0
148fi
149
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000150final=""
151while [ ! -z "$switches" ] ; do
152 trimmed=`echo "$switches" | sed -e 's/^ *\(-[^ ]*\).*/\1/'`
153 switches=`echo "$switches" | sed -e 's/^ *-[^ ]* *//'`
154 echo "Trimmed $trimmed from left"
155 tryit "$final $switches"
156 if [ "$?" -eq "0" ] ; then
157 echo "Still Failing .. continuing ..."
158 continue
159 else
160 echo "Found required early pass: $trimmed"
161 final="$final $trimmed"
162 continue
163 fi
164 echo "Next Loop"
165done
166
Reid Spencerdd2b9552006-11-09 01:47:04 +0000167if [ "$final" == " $all_switches" ] ; then
Reid Spencer02440992006-11-13 16:08:51 +0000168 echo "findmisopt: All optimizations pass. Perhaps this isn't a misopt?"
Reid Spencerdd2b9552006-11-09 01:47:04 +0000169 exit 0
170fi
171echo "Smallest Optimization list=$final"
Reid Spencera8c3ff42006-11-16 18:32:47 +0000172
173bpcmd="$bugpoint -run-llc -disable-loop-extraction --output "$out" --input /dev/null $bcfile $final --args $args"
Reid Spencer28b7c7f2006-11-09 00:26:17 +0000174
175echo "Running: $bpcmd"
176$bpcmd
Reid Spencerdd2b9552006-11-09 01:47:04 +0000177echo "findmisopt finished."