Provide a script that can track down which optimization pass causes
[oota-llvm.git] / utils / findoptdiff
1 #!/bin/bash
2 #
3 #  findoptdiff
4 #
5 #      This script helps find the optimization difference between two llvm
6 #      builds. It is useful when you have a build that is known to work and
7 #      one that exhibits an optimization problem. Identifying the difference
8 #      between the two builds can lead to discovery of the source of a
9 #      mis-optimization.
10 #
11 #      The script takes two llvm build paths as arguments. These specify the
12 #      the two llvm builds to compare. It is generally expected that they
13 #      are "close cousins".  That is, they are the same except that the 
14 #      second build contains some experimental optimization features that
15 #      are suspected of producing a misoptimization.
16 #
17 #      The script takes two bytecode files, one from each build. They are
18 #      presumed to be a compilation of the same program or program fragment
19 #      with the only difference being the builds.
20 #
21 #      The script operates by iteratively applying the optimizations that gccas
22 #      and gccld run until there is a difference in the assembly resulting
23 #      from the optimization. The difference is then reported with the set of
24 #      optimization passes that produce the difference. 
25 #
26 #      To work around differences in the assembly language format, the script
27 #      can also take two filter arguments that post-process the assembly 
28 #      so they can be differenced without making false positives for known
29 #      differences in the two builds. These filters are optional.
30 #
31 #   Usage:
32 #      findoptdiff llvm1 llvm2 bc1 bc2 filter1 filter2
33 #
34 #   Where:
35 #      llvm1
36 #          is the path to the first llvm build dir
37 #      llvm2
38 #          is the path to the second llvm build dir
39 #      bc1
40 #          is the bytecode file for the first llvm environment
41 #      bc2
42 #          is the bytecode file for the second llvm environment
43 #      filter1
44 #          is an optional filter for filtering the llvm1 generated assembly
45 #      filter2
46 #          is an optional filter for filtering the llvm2 generated assembly
47 #       
48 llvm1=$1
49 llvm2=$2
50 bc1=$3
51 bc2=$4
52 filt1=$5
53 if [ -z "$filt1" ] ; then
54   filt1="cat"
55 fi
56 filt2=$6
57 if [ -z "$filt2" ] ; then
58   filt2="cat"
59 fi
60 opt1=opt.$bc1
61 opt2=opt.$bc2
62 ll1=${bc1}.ll
63 ll2=${bc2}.ll
64 dir1="/proj/llvm/llvm-test-1/External/SPEC/CINT2000/300.twolf"
65 opt1="/proj/llvm/llvm-1/Debug/bin/opt"
66 dis1="/proj/llvm/llvm-1/Debug/bin/llvm-dis"
67 dir2="/proj/llvm/llvm-test-2/External/SPEC/CINT2000/300.twolf"
68 opt2="/proj/llvm/llvm-2/Debug/bin/opt"
69 dis2="/proj/llvm/llvm-2/Debug/bin/llvm-dis"
70 bcfile="Output/300.twolf.linked.rbc"
71 optll="opt.ll"
72
73 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 -funcresolve -internalize -ipsccp -globalopt -constmerge -deadargelim -inline -prune-eh -globalopt -globaldce -argpromotion -instcombine -predsimplify -scalarrepl -globalsmodref-aa -licm -load-vn -gcse -dse -instcombine -simplifycfg -verify"
74
75 function tryit {
76   switches_to_use="$1"
77   cd $dir1
78   $opt1 $switches_to_use "$bcfile" -o - | $dis1 | $filt1 > "$optll"
79   cd $dir2
80   $opt2 $switches_to_use "$bcfile" -o - | $dis2 | $filt2 > "$optll"
81   diff "$dir1/$optll" "$dir2/$optll" > /dev/null
82   if [ $? -ne 0 ] ; then
83     echo
84     echo "Diff fails with these switches:"
85     echo $switches
86     echo "Differences:"
87     diff "$dir1/$optll" "$dir2/$optll" | head
88     exit 1
89   fi
90   return 1
91 }
92
93 for sw in $all_switches ; do
94   echo -n " $sw"
95   switches="$switches $sw"
96   if tryit "$switches" ; then
97     break;
98   fi
99 done