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
20my $ProfilePass = "-insert-edge-profiling";
21
22my $LLVMProfOpts = "";
23my $ProgramOpts = "";
24my $ProfileFile = "";
25
26# Parse arguments...
27while (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
59die "Must specify LLVM bitcode file as first argument!" if (@ARGV == 0);
60
61my $BytecodeFile = $ARGV[0];
62
63shift @ARGV;
64
65my $libdir = `llvm-config --libdir`;
66chomp $libdir;
67
68my $LibProfPath = $libdir . "/libprofile_rt.so";
69
70system "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";
74system "llvm-prof $LLVMProfOpts $BytecodeFile $ProfileFile";
75