Home | History | Annotate | Download | only in utils
      1 #!/usr/bin/perl -w
      2 #
      3 # Program:  profile.pl
      4 #
      5 # Synopsis: Insert instrumentation code into a program, run it with the JIT,
      6 #           then print out a profile report.
      7 #
      8 # Syntax:   profile.pl [OPTIONS] bitcodefile <arguments>
      9 #
     10 # OPTIONS may include one or more of the following:
     11 #     -block    - Enable basicblock profiling
     12 #     -edge     - Enable edge profiling
     13 #     -function - Enable function profiling
     14 #     -o <filename> - Emit profiling information to the specified file, instead
     15 #                     of llvmprof.out
     16 #
     17 # Any unrecognized options are passed into the invocation of llvm-prof
     18 #
     19 
     20 my $ProfilePass = "-insert-edge-profiling";
     21 
     22 my $LLVMProfOpts = "";
     23 my $ProgramOpts = "";
     24 my $ProfileFile = "";
     25 
     26 # Parse arguments...
     27 while (scalar(@ARGV) and ($_ = $ARGV[0], /^[-+]/)) {
     28   shift;
     29   last if /^--$/;  # Stop processing arguments on --
     30 
     31   # List command line options here...
     32   if (/^-?-block$/)    { $ProfilePass = "-insert-block-profiling"; next; }
     33   if (/^-?-edge$/)     { $ProfilePass = "-insert-edge-profiling"; next; }
     34   if (/^-?-function$/) { $ProfilePass = "-insert-function-profiling"; next; }
     35   if (/^-?-o$/) {         # Read -o filename...
     36     die "-o option requires a filename argument!" if (!scalar(@ARGV));
     37     $ProgramOpts .= " -llvmprof-output $ARGV[0]";
     38     $ProfileFile = $ARGV[0];
     39     shift;
     40     next;
     41   }
     42   if (/^-?-help$/) {
     43     print "OVERVIEW: profile.pl - Instrumentation and profile printer.\n\n";
     44     print "USAGE: profile.pl [options] program.bc <program args>\n\n";
     45     print "OPTIONS:\n";
     46     print "  -block    - Enable basicblock profiling\n";
     47     print "  -edge     - Enable edge profiling\n";
     48     print "  -function - Enable function profiling\n";
     49     print "  -o <file> - Specify an output file other than llvm-prof.out.\n";
     50     print "  -help     - Print this usage information\n";
     51     print "\nAll other options are passed into llvm-prof.\n";
     52     exit 1;
     53   }
     54 
     55   # Otherwise, pass the option on to llvm-prof
     56   $LLVMProfOpts .= " " . $_;
     57 }
     58 
     59 die "Must specify LLVM bitcode file as first argument!" if (@ARGV == 0);
     60 
     61 my $BytecodeFile = $ARGV[0];
     62 
     63 shift @ARGV;
     64 
     65 my $libdir = `llvm-config --libdir`;
     66 chomp $libdir;
     67 
     68 my $LibProfPath = $libdir . "/libprofile_rt.so";
     69 
     70 system "opt -q -f $ProfilePass $BytecodeFile -o $BytecodeFile.inst";
     71 system "lli -fake-argv0 '$BytecodeFile' -load $LibProfPath " .
     72        "$BytecodeFile.inst $ProgramOpts " . (join ' ', @ARGV);
     73 system "rm $BytecodeFile.inst";
     74 system "llvm-prof $LLVMProfOpts $BytecodeFile $ProfileFile";
     75