blob: f5a736399a747d70a9a39ff872152ee3c1f95a77 [file] [log] [blame]
Chris Lattner10c91cc2003-10-28 22:11:31 +00001#!/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] bytecodefile <arguments>
9#
10# OPTIONS may include one or more of the following:
Chris Lattner7069bb22004-02-10 18:01:50 +000011# -block - Enable basicblock-level profiling
12# -function - Enable function-level profiling
13# -o <filename> - Emit profiling information to the specified file, instead
14# of llvmprof.out
Chris Lattner10c91cc2003-10-28 22:11:31 +000015#
Chris Lattner3538c9d2003-10-29 21:51:00 +000016# Any unrecognized options are passed into the invocation of llvm-prof
17#
Chris Lattner10c91cc2003-10-28 22:11:31 +000018
Chris Lattner7069bb22004-02-10 18:01:50 +000019my $ProfilePass = "-insert-block-profiling";
Chris Lattner10c91cc2003-10-28 22:11:31 +000020
Chris Lattner3538c9d2003-10-29 21:51:00 +000021my $LLVMProfOpts = "";
Chris Lattner7069bb22004-02-10 18:01:50 +000022my $ProgramOpts = "";
23my $ProfileFile = "";
Chris Lattner3538c9d2003-10-29 21:51:00 +000024
Chris Lattner10c91cc2003-10-28 22:11:31 +000025# Parse arguments...
26while (scalar(@ARGV) and ($_ = $ARGV[0], /^[-+]/)) {
27 shift;
28 last if /^--$/; # Stop processing arguments on --
29
30 # List command line options here...
Chris Lattner5d8e5522003-11-02 05:17:32 +000031 if (/^-?-block$/) { $ProfilePass = "-insert-block-profiling"; next; }
Chris Lattner7069bb22004-02-10 18:01:50 +000032 if (/^-?-function$/) { $ProfilePass = "-insert-function-profiling"; next; }
33 if (/^-?-o$/) { # Read -o filename...
34 die "-o option requires a filename argument!" if (!scalar(@ARGV));
35 $ProgramOpts .= " -llvmprof-output $ARGV[0]";
36 $ProfileFile = $ARGV[0];
37 shift;
38 next;
39 }
Chris Lattner5d8e5522003-11-02 05:17:32 +000040 if (/^-?-help$/) {
41 print "OVERVIEW: profile.pl - Instrumentation and profile printer.\n\n";
42 print "USAGE: profile.pl [options] program.bc <program args>\n\n";
43 print "OPTIONS:\n";
Chris Lattner7069bb22004-02-10 18:01:50 +000044 print " -block - Enable basicblock-level profiling\n";
45 print " -function - Enable function-level profiling\n";
46 print " -o <file> - Specify an output file other than llvm-prof.out.\n";
47 print " -help - Print this usage information\n";
Chris Lattner5d8e5522003-11-02 05:17:32 +000048 print "\nAll other options are passed into llvm-prof.\n";
49 exit 1;
50 }
Chris Lattner10c91cc2003-10-28 22:11:31 +000051
Chris Lattner3538c9d2003-10-29 21:51:00 +000052 # Otherwise, pass the option on to llvm-prof
53 $LLVMProfOpts .= " " . $_;
Chris Lattner10c91cc2003-10-28 22:11:31 +000054}
55
56die "Must specify LLVM bytecode file as first argument!" if (@ARGV == 0);
57
58my $BytecodeFile = $ARGV[0];
59
60shift @ARGV;
61
62my $LLIPath = `which lli`;
63$LLIPath = `dirname $LLIPath`;
64chomp $LLIPath;
65
66my $LibProfPath = $LLIPath . "/../../lib/Debug/libprofile_rt.so";
67
Chris Lattner5d8e5522003-11-02 05:17:32 +000068system "opt -q $ProfilePass < $BytecodeFile | lli -fake-argv0 '$BytecodeFile'" .
Chris Lattner7069bb22004-02-10 18:01:50 +000069 " -load $LibProfPath -$ProgramOpts " . (join ' ', @ARGV);
Chris Lattner10c91cc2003-10-28 22:11:31 +000070
Chris Lattner7069bb22004-02-10 18:01:50 +000071system "llvm-prof $LLVMProfOpts $BytecodeFile $ProfileFile";