xref: /aosp_15_r20/external/llvm/utils/findoptdiff (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker#!/bin/bash
2*9880d681SAndroid Build Coastguard Worker#
3*9880d681SAndroid Build Coastguard Worker#  findoptdiff
4*9880d681SAndroid Build Coastguard Worker#
5*9880d681SAndroid Build Coastguard Worker#      This script helps find the optimization difference between two llvm
6*9880d681SAndroid Build Coastguard Worker#      builds. It is useful when you have a build that is known to work and
7*9880d681SAndroid Build Coastguard Worker#      one that exhibits an optimization problem. Identifying the difference
8*9880d681SAndroid Build Coastguard Worker#      between the two builds can lead to discovery of the source of a
9*9880d681SAndroid Build Coastguard Worker#      mis-optimization.
10*9880d681SAndroid Build Coastguard Worker#
11*9880d681SAndroid Build Coastguard Worker#      The script takes two llvm build paths as arguments. These specify the
12*9880d681SAndroid Build Coastguard Worker#      the two llvm builds to compare. It is generally expected that they
13*9880d681SAndroid Build Coastguard Worker#      are "close cousins".  That is, they are the same except that the
14*9880d681SAndroid Build Coastguard Worker#      second build contains some experimental optimization features that
15*9880d681SAndroid Build Coastguard Worker#      are suspected of producing a misoptimization.
16*9880d681SAndroid Build Coastguard Worker#
17*9880d681SAndroid Build Coastguard Worker#      The script takes two bitcode files, one from each build. They are
18*9880d681SAndroid Build Coastguard Worker#      presumed to be a compilation of the same program or program fragment
19*9880d681SAndroid Build Coastguard Worker#      with the only difference being the builds.
20*9880d681SAndroid Build Coastguard Worker#
21*9880d681SAndroid Build Coastguard Worker#      The script operates by iteratively applying the optimizations that gccas
22*9880d681SAndroid Build Coastguard Worker#      and gccld run until there is a difference in the assembly resulting
23*9880d681SAndroid Build Coastguard Worker#      from the optimization. The difference is then reported with the set of
24*9880d681SAndroid Build Coastguard Worker#      optimization passes that produce the difference.  The processing
25*9880d681SAndroid Build Coastguard Worker#      continues until all optimization passes have been tried. The differences
26*9880d681SAndroid Build Coastguard Worker#      for each pass, if they do differ, are placed in a diffs.# file.
27*9880d681SAndroid Build Coastguard Worker#
28*9880d681SAndroid Build Coastguard Worker#      To work around differences in the assembly language format, the script
29*9880d681SAndroid Build Coastguard Worker#      can also take two filter arguments that post-process the assembly
30*9880d681SAndroid Build Coastguard Worker#      so they can be differenced without making false positives for known
31*9880d681SAndroid Build Coastguard Worker#      differences in the two builds. These filters are optional.
32*9880d681SAndroid Build Coastguard Worker#
33*9880d681SAndroid Build Coastguard Worker#   Usage:
34*9880d681SAndroid Build Coastguard Worker#      findoptdiff llvm1 llvm2 bc1 bc2 filter1 filter2
35*9880d681SAndroid Build Coastguard Worker#
36*9880d681SAndroid Build Coastguard Worker#   Where:
37*9880d681SAndroid Build Coastguard Worker#      llvm1
38*9880d681SAndroid Build Coastguard Worker#          is the path to the first llvm build dir
39*9880d681SAndroid Build Coastguard Worker#      llvm2
40*9880d681SAndroid Build Coastguard Worker#          is the path to the second llvm build dir
41*9880d681SAndroid Build Coastguard Worker#      bc1
42*9880d681SAndroid Build Coastguard Worker#          is the bitcode file for the first llvm environment
43*9880d681SAndroid Build Coastguard Worker#      bc2
44*9880d681SAndroid Build Coastguard Worker#          is the bitcode file for the second llvm environment
45*9880d681SAndroid Build Coastguard Worker#      filter1
46*9880d681SAndroid Build Coastguard Worker#          is an optional filter for filtering the llvm1 generated assembly
47*9880d681SAndroid Build Coastguard Worker#      filter2
48*9880d681SAndroid Build Coastguard Worker#          is an optional filter for filtering the llvm2 generated assembly
49*9880d681SAndroid Build Coastguard Worker#
50*9880d681SAndroid Build Coastguard Workerllvm1=$1
51*9880d681SAndroid Build Coastguard Workerllvm2=$2
52*9880d681SAndroid Build Coastguard Workerbc1=$3
53*9880d681SAndroid Build Coastguard Workerbc2=$4
54*9880d681SAndroid Build Coastguard Workerfilt1=$5
55*9880d681SAndroid Build Coastguard Workerfilt2=$6
56*9880d681SAndroid Build Coastguard Workerif [ -z "$filt1" ] ; then
57*9880d681SAndroid Build Coastguard Worker  filt1="cat"
58*9880d681SAndroid Build Coastguard Workerfi
59*9880d681SAndroid Build Coastguard Workerif [ -z "$filt2" ] ; then
60*9880d681SAndroid Build Coastguard Worker  filt2="cat"
61*9880d681SAndroid Build Coastguard Workerfi
62*9880d681SAndroid Build Coastguard Workeropt1="${bc1}.opt"
63*9880d681SAndroid Build Coastguard Workeropt2="${bc2}.opt"
64*9880d681SAndroid Build Coastguard Workerll1="${bc1}.ll"
65*9880d681SAndroid Build Coastguard Workerll2="${bc2}.ll"
66*9880d681SAndroid Build Coastguard Workeropt1ll="${bc1}.opt.ll"
67*9880d681SAndroid Build Coastguard Workeropt2ll="${bc2}.opt.ll"
68*9880d681SAndroid Build Coastguard Workerdis1="$llvm1/Debug/bin/llvm-dis"
69*9880d681SAndroid Build Coastguard Workerdis2="$llvm2/Debug/bin/llvm-dis"
70*9880d681SAndroid Build Coastguard Workeropt1="$llvm1/Debug/bin/opt"
71*9880d681SAndroid Build Coastguard Workeropt2="$llvm2/Debug/bin/opt"
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard Workerall_switches="-verify -lowersetjmp -simplifycfg -mem2reg -globalopt -globaldce -ipconstprop -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*9880d681SAndroid Build Coastguard Worker
75*9880d681SAndroid Build Coastguard Worker#counter=0
76*9880d681SAndroid Build Coastguard Workerfunction tryit {
77*9880d681SAndroid Build Coastguard Worker  switches_to_use="$1"
78*9880d681SAndroid Build Coastguard Worker  $opt1 $switches_to_use "$bc1" -o - | $dis1 | $filt1 > "$opt1ll"
79*9880d681SAndroid Build Coastguard Worker  $opt2 $switches_to_use "$bc2" -o - | $dis2 | $filt2 > "$opt2ll"
80*9880d681SAndroid Build Coastguard Worker  diffs="diffs."$((counter++))
81*9880d681SAndroid Build Coastguard Worker  diff "$opt1ll" "$opt2ll" > $diffs
82*9880d681SAndroid Build Coastguard Worker  if [ $? -ne 0 ] ; then
83*9880d681SAndroid Build Coastguard Worker    echo
84*9880d681SAndroid Build Coastguard Worker    echo "Diff fails with these switches:"
85*9880d681SAndroid Build Coastguard Worker    echo $switches
86*9880d681SAndroid Build Coastguard Worker    echo "Differences:"
87*9880d681SAndroid Build Coastguard Worker    head $diffs
88*9880d681SAndroid Build Coastguard Worker    echo 'Switches:' $switches_to_use >> $diffs
89*9880d681SAndroid Build Coastguard Worker  else
90*9880d681SAndroid Build Coastguard Worker    rm $diffs
91*9880d681SAndroid Build Coastguard Worker  fi
92*9880d681SAndroid Build Coastguard Worker  return 1
93*9880d681SAndroid Build Coastguard Worker}
94*9880d681SAndroid Build Coastguard Worker
95*9880d681SAndroid Build Coastguard Workerfor sw in $all_switches ; do
96*9880d681SAndroid Build Coastguard Worker  echo -n " $sw"
97*9880d681SAndroid Build Coastguard Worker  switches="$switches $sw"
98*9880d681SAndroid Build Coastguard Worker  if tryit "$switches" ; then
99*9880d681SAndroid Build Coastguard Worker    break;
100*9880d681SAndroid Build Coastguard Worker  fi
101*9880d681SAndroid Build Coastguard Workerdone
102