Chris Lattner | ad910eb | 2003-10-28 22:11:31 +0000 | [diff] [blame] | 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 | # |
Duncan Sands | 18d52f2 | 2010-09-29 20:09:55 +0000 | [diff] [blame] | 8 | # Syntax: profile.pl [OPTIONS] bitcodefile <arguments> |
Chris Lattner | ad910eb | 2003-10-28 22:11:31 +0000 | [diff] [blame] | 9 | # |
| 10 | # OPTIONS may include one or more of the following: |
Chris Lattner | 5cedaba | 2004-03-08 20:04:46 +0000 | [diff] [blame] | 11 | # -block - Enable basicblock profiling |
| 12 | # -edge - Enable edge profiling |
| 13 | # -function - Enable function profiling |
Chris Lattner | b9f960e | 2004-02-10 18:01:50 +0000 | [diff] [blame] | 14 | # -o <filename> - Emit profiling information to the specified file, instead |
| 15 | # of llvmprof.out |
Chris Lattner | ad910eb | 2003-10-28 22:11:31 +0000 | [diff] [blame] | 16 | # |
Chris Lattner | 0faadf2 | 2003-10-29 21:51:00 +0000 | [diff] [blame] | 17 | # Any unrecognized options are passed into the invocation of llvm-prof |
| 18 | # |
Chris Lattner | ad910eb | 2003-10-28 22:11:31 +0000 | [diff] [blame] | 19 | |
Chris Lattner | 5cedaba | 2004-03-08 20:04:46 +0000 | [diff] [blame] | 20 | my $ProfilePass = "-insert-edge-profiling"; |
Chris Lattner | ad910eb | 2003-10-28 22:11:31 +0000 | [diff] [blame] | 21 | |
Chris Lattner | 0faadf2 | 2003-10-29 21:51:00 +0000 | [diff] [blame] | 22 | my $LLVMProfOpts = ""; |
Chris Lattner | b9f960e | 2004-02-10 18:01:50 +0000 | [diff] [blame] | 23 | my $ProgramOpts = ""; |
| 24 | my $ProfileFile = ""; |
Chris Lattner | 0faadf2 | 2003-10-29 21:51:00 +0000 | [diff] [blame] | 25 | |
Chris Lattner | ad910eb | 2003-10-28 22:11:31 +0000 | [diff] [blame] | 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... |
Chris Lattner | 5cedaba | 2004-03-08 20:04:46 +0000 | [diff] [blame] | 32 | if (/^-?-block$/) { $ProfilePass = "-insert-block-profiling"; next; } |
| 33 | if (/^-?-edge$/) { $ProfilePass = "-insert-edge-profiling"; next; } |
Chris Lattner | b9f960e | 2004-02-10 18:01:50 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | 1ca9221 | 2003-11-02 05:17:32 +0000 | [diff] [blame] | 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"; |
Chris Lattner | 5cedaba | 2004-03-08 20:04:46 +0000 | [diff] [blame] | 46 | print " -block - Enable basicblock profiling\n"; |
| 47 | print " -edge - Enable edge profiling\n"; |
| 48 | print " -function - Enable function profiling\n"; |
Chris Lattner | b9f960e | 2004-02-10 18:01:50 +0000 | [diff] [blame] | 49 | print " -o <file> - Specify an output file other than llvm-prof.out.\n"; |
| 50 | print " -help - Print this usage information\n"; |
Chris Lattner | 1ca9221 | 2003-11-02 05:17:32 +0000 | [diff] [blame] | 51 | print "\nAll other options are passed into llvm-prof.\n"; |
| 52 | exit 1; |
| 53 | } |
Chris Lattner | ad910eb | 2003-10-28 22:11:31 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 0faadf2 | 2003-10-29 21:51:00 +0000 | [diff] [blame] | 55 | # Otherwise, pass the option on to llvm-prof |
| 56 | $LLVMProfOpts .= " " . $_; |
Chris Lattner | ad910eb | 2003-10-28 22:11:31 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Duncan Sands | 18d52f2 | 2010-09-29 20:09:55 +0000 | [diff] [blame] | 59 | die "Must specify LLVM bitcode file as first argument!" if (@ARGV == 0); |
Chris Lattner | ad910eb | 2003-10-28 22:11:31 +0000 | [diff] [blame] | 60 | |
| 61 | my $BytecodeFile = $ARGV[0]; |
| 62 | |
| 63 | shift @ARGV; |
| 64 | |
Chris Lattner | 111e8ce | 2007-09-11 17:09:54 +0000 | [diff] [blame] | 65 | my $libdir = `llvm-config --libdir`; |
| 66 | chomp $libdir; |
Chris Lattner | ad910eb | 2003-10-28 22:11:31 +0000 | [diff] [blame] | 67 | |
Nick Lewycky | bcffb1f | 2011-04-29 02:12:06 +0000 | [diff] [blame] | 68 | my $LibProfPath = $libdir . "/libprofile_rt.so"; |
Chris Lattner | ad910eb | 2003-10-28 22:11:31 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 5cedaba | 2004-03-08 20:04:46 +0000 | [diff] [blame] | 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"; |
Chris Lattner | b9f960e | 2004-02-10 18:01:50 +0000 | [diff] [blame] | 74 | system "llvm-prof $LLVMProfOpts $BytecodeFile $ProfileFile"; |