blob: 782e5dc24d468e686dbada7cd30827adb3fedd7a [file] [log] [blame]
Chris Lattnerad910eb2003-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#
Duncan Sands18d52f22010-09-29 20:09:55 +00008# Syntax: profile.pl [OPTIONS] bitcodefile <arguments>
Chris Lattnerad910eb2003-10-28 22:11:31 +00009#
10# OPTIONS may include one or more of the following:
Chris Lattner5cedaba2004-03-08 20:04:46 +000011# -block - Enable basicblock profiling
12# -edge - Enable edge profiling
13# -function - Enable function profiling
Chris Lattnerb9f960e2004-02-10 18:01:50 +000014# -o <filename> - Emit profiling information to the specified file, instead
15# of llvmprof.out
Chris Lattnerad910eb2003-10-28 22:11:31 +000016#
Chris Lattner0faadf22003-10-29 21:51:00 +000017# Any unrecognized options are passed into the invocation of llvm-prof
18#
Chris Lattnerad910eb2003-10-28 22:11:31 +000019
Chris Lattner5cedaba2004-03-08 20:04:46 +000020my $ProfilePass = "-insert-edge-profiling";
Chris Lattnerad910eb2003-10-28 22:11:31 +000021
Chris Lattner0faadf22003-10-29 21:51:00 +000022my $LLVMProfOpts = "";
Chris Lattnerb9f960e2004-02-10 18:01:50 +000023my $ProgramOpts = "";
24my $ProfileFile = "";
Chris Lattner0faadf22003-10-29 21:51:00 +000025
Chris Lattnerad910eb2003-10-28 22:11:31 +000026# Parse arguments...
27while (scalar(@ARGV) and ($_ = $ARGV[0], /^[-+]/)) {
28 shift;
29 last if /^--$/; # Stop processing arguments on --
30
31 # List command line options here...
Chris Lattner5cedaba2004-03-08 20:04:46 +000032 if (/^-?-block$/) { $ProfilePass = "-insert-block-profiling"; next; }
33 if (/^-?-edge$/) { $ProfilePass = "-insert-edge-profiling"; next; }
Chris Lattnerb9f960e2004-02-10 18:01:50 +000034 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 Lattner1ca92212003-11-02 05:17:32 +000042 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 Lattner5cedaba2004-03-08 20:04:46 +000046 print " -block - Enable basicblock profiling\n";
47 print " -edge - Enable edge profiling\n";
48 print " -function - Enable function profiling\n";
Chris Lattnerb9f960e2004-02-10 18:01:50 +000049 print " -o <file> - Specify an output file other than llvm-prof.out.\n";
50 print " -help - Print this usage information\n";
Chris Lattner1ca92212003-11-02 05:17:32 +000051 print "\nAll other options are passed into llvm-prof.\n";
52 exit 1;
53 }
Chris Lattnerad910eb2003-10-28 22:11:31 +000054
Chris Lattner0faadf22003-10-29 21:51:00 +000055 # Otherwise, pass the option on to llvm-prof
56 $LLVMProfOpts .= " " . $_;
Chris Lattnerad910eb2003-10-28 22:11:31 +000057}
58
Duncan Sands18d52f22010-09-29 20:09:55 +000059die "Must specify LLVM bitcode file as first argument!" if (@ARGV == 0);
Chris Lattnerad910eb2003-10-28 22:11:31 +000060
61my $BytecodeFile = $ARGV[0];
62
63shift @ARGV;
64
Chris Lattner111e8ce2007-09-11 17:09:54 +000065my $libdir = `llvm-config --libdir`;
66chomp $libdir;
Chris Lattnerad910eb2003-10-28 22:11:31 +000067
Nick Lewyckybcffb1f2011-04-29 02:12:06 +000068my $LibProfPath = $libdir . "/libprofile_rt.so";
Chris Lattnerad910eb2003-10-28 22:11:31 +000069
Chris Lattner5cedaba2004-03-08 20:04:46 +000070system "opt -q -f $ProfilePass $BytecodeFile -o $BytecodeFile.inst";
71system "lli -fake-argv0 '$BytecodeFile' -load $LibProfPath " .
72 "$BytecodeFile.inst $ProgramOpts " . (join ' ', @ARGV);
73system "rm $BytecodeFile.inst";
Chris Lattnerb9f960e2004-02-10 18:01:50 +000074system "llvm-prof $LLVMProfOpts $BytecodeFile $ProfileFile";