102 lines · plain
1#!/bin/bash2#3# findoptdiff4#5# This script helps find the optimization difference between two llvm6# builds. It is useful when you have a build that is known to work and7# one that exhibits an optimization problem. Identifying the difference8# between the two builds can lead to discovery of the source of a9# mis-optimization.10#11# The script takes two llvm build paths as arguments. These specify the12# the two llvm builds to compare. It is generally expected that they13# are "close cousins". That is, they are the same except that the 14# second build contains some experimental optimization features that15# are suspected of producing a misoptimization.16#17# The script takes two bitcode files, one from each build. They are18# presumed to be a compilation of the same program or program fragment19# with the only difference being the builds.20#21# The script operates by iteratively applying the optimizations that gccas22# and gccld run until there is a difference in the assembly resulting23# from the optimization. The difference is then reported with the set of24# optimization passes that produce the difference. The processing 25# continues until all optimization passes have been tried. The differences26# for each pass, if they do differ, are placed in a diffs.# file.27#28# To work around differences in the assembly language format, the script29# can also take two filter arguments that post-process the assembly 30# so they can be differenced without making false positives for known31# differences in the two builds. These filters are optional.32#33# Usage:34# findoptdiff llvm1 llvm2 bc1 bc2 filter1 filter235#36# Where:37# llvm138# is the path to the first llvm build dir39# llvm240# is the path to the second llvm build dir41# bc142# is the bitcode file for the first llvm environment43# bc244# is the bitcode file for the second llvm environment45# filter146# is an optional filter for filtering the llvm1 generated assembly47# filter248# is an optional filter for filtering the llvm2 generated assembly49# 50llvm1=$151llvm2=$252bc1=$353bc2=$454filt1=$555filt2=$656if [ -z "$filt1" ] ; then57 filt1="cat"58fi59if [ -z "$filt2" ] ; then60 filt2="cat"61fi62opt1="${bc1}.opt"63opt2="${bc2}.opt" 64ll1="${bc1}.ll"65ll2="${bc2}.ll"66opt1ll="${bc1}.opt.ll"67opt2ll="${bc2}.opt.ll"68dis1="$llvm1/Debug/bin/llvm-dis"69dis2="$llvm2/Debug/bin/llvm-dis"70opt1="$llvm1/Debug/bin/opt"71opt2="$llvm2/Debug/bin/opt"72 73all_switches="-verify -lowersetjmp -simplifycfg -mem2reg -globalopt -globaldce -deadargelim -instcombine -simplifycfg -prune-eh -inline -simplify-libcalls -argpromotion -tailduplicate -simplifycfg -sroa -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 -internalize -ipsccp -globalopt -constmerge -deadargelim -inline -prune-eh -globalopt -globaldce -argpromotion -instcombine -predsimplify -sroa -globalsmodref-aa -licm -load-vn -gcse -dse -instcombine -simplifycfg -verify"74 75#counter=076function tryit {77 switches_to_use="$1"78 $opt1 $switches_to_use "$bc1" -o - | $dis1 | $filt1 > "$opt1ll"79 $opt2 $switches_to_use "$bc2" -o - | $dis2 | $filt2 > "$opt2ll"80 diffs="diffs."$((counter++))81 diff "$opt1ll" "$opt2ll" > $diffs82 if [ $? -ne 0 ] ; then83 echo84 echo "Diff fails with these switches:"85 echo $switches86 echo "Differences:"87 head $diffs88 echo 'Switches:' $switches_to_use >> $diffs89 else90 rm $diffs91 fi92 return 193}94 95for sw in $all_switches ; do96 echo -n " $sw"97 switches="$switches $sw"98 if tryit "$switches" ; then99 break;100 fi101done102