1ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner#!/usr/bin/perl -w
2ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner#
3ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner# Program:  profile.pl
4ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner#
5ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner# Synopsis: Insert instrumentation code into a program, run it with the JIT,
6ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner#           then print out a profile report.
7ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner#
818d52f2fb551c295bbce4c7d7357f4050b06e926Duncan Sands# Syntax:   profile.pl [OPTIONS] bitcodefile <arguments>
9ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner#
10ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner# OPTIONS may include one or more of the following:
115cedabafe5decd3c30dea6bf54c5e47ec85c44d9Chris Lattner#     -block    - Enable basicblock profiling
125cedabafe5decd3c30dea6bf54c5e47ec85c44d9Chris Lattner#     -edge     - Enable edge profiling
135cedabafe5decd3c30dea6bf54c5e47ec85c44d9Chris Lattner#     -function - Enable function profiling
14b9f960e39d50a30d86b16d52ef593ff6f444c5ceChris Lattner#     -o <filename> - Emit profiling information to the specified file, instead
15b9f960e39d50a30d86b16d52ef593ff6f444c5ceChris Lattner#                     of llvmprof.out
16ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner#
170faadf2e589b7bbeb740899429a659f576f29683Chris Lattner# Any unrecognized options are passed into the invocation of llvm-prof
180faadf2e589b7bbeb740899429a659f576f29683Chris Lattner#
19ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner
205cedabafe5decd3c30dea6bf54c5e47ec85c44d9Chris Lattnermy $ProfilePass = "-insert-edge-profiling";
21ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner
220faadf2e589b7bbeb740899429a659f576f29683Chris Lattnermy $LLVMProfOpts = "";
23b9f960e39d50a30d86b16d52ef593ff6f444c5ceChris Lattnermy $ProgramOpts = "";
24b9f960e39d50a30d86b16d52ef593ff6f444c5ceChris Lattnermy $ProfileFile = "";
250faadf2e589b7bbeb740899429a659f576f29683Chris Lattner
26ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner# Parse arguments...
27ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattnerwhile (scalar(@ARGV) and ($_ = $ARGV[0], /^[-+]/)) {
28ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner  shift;
29ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner  last if /^--$/;  # Stop processing arguments on --
30ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner
31ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner  # List command line options here...
325cedabafe5decd3c30dea6bf54c5e47ec85c44d9Chris Lattner  if (/^-?-block$/)    { $ProfilePass = "-insert-block-profiling"; next; }
335cedabafe5decd3c30dea6bf54c5e47ec85c44d9Chris Lattner  if (/^-?-edge$/)     { $ProfilePass = "-insert-edge-profiling"; next; }
34b9f960e39d50a30d86b16d52ef593ff6f444c5ceChris Lattner  if (/^-?-function$/) { $ProfilePass = "-insert-function-profiling"; next; }
35b9f960e39d50a30d86b16d52ef593ff6f444c5ceChris Lattner  if (/^-?-o$/) {         # Read -o filename...
36b9f960e39d50a30d86b16d52ef593ff6f444c5ceChris Lattner    die "-o option requires a filename argument!" if (!scalar(@ARGV));
37b9f960e39d50a30d86b16d52ef593ff6f444c5ceChris Lattner    $ProgramOpts .= " -llvmprof-output $ARGV[0]";
38b9f960e39d50a30d86b16d52ef593ff6f444c5ceChris Lattner    $ProfileFile = $ARGV[0];
39b9f960e39d50a30d86b16d52ef593ff6f444c5ceChris Lattner    shift;
40b9f960e39d50a30d86b16d52ef593ff6f444c5ceChris Lattner    next;
41b9f960e39d50a30d86b16d52ef593ff6f444c5ceChris Lattner  }
421ca922110d262161ce922ce314bcdaadf5bbe752Chris Lattner  if (/^-?-help$/) {
431ca922110d262161ce922ce314bcdaadf5bbe752Chris Lattner    print "OVERVIEW: profile.pl - Instrumentation and profile printer.\n\n";
441ca922110d262161ce922ce314bcdaadf5bbe752Chris Lattner    print "USAGE: profile.pl [options] program.bc <program args>\n\n";
451ca922110d262161ce922ce314bcdaadf5bbe752Chris Lattner    print "OPTIONS:\n";
465cedabafe5decd3c30dea6bf54c5e47ec85c44d9Chris Lattner    print "  -block    - Enable basicblock profiling\n";
475cedabafe5decd3c30dea6bf54c5e47ec85c44d9Chris Lattner    print "  -edge     - Enable edge profiling\n";
485cedabafe5decd3c30dea6bf54c5e47ec85c44d9Chris Lattner    print "  -function - Enable function profiling\n";
49b9f960e39d50a30d86b16d52ef593ff6f444c5ceChris Lattner    print "  -o <file> - Specify an output file other than llvm-prof.out.\n";
50b9f960e39d50a30d86b16d52ef593ff6f444c5ceChris Lattner    print "  -help     - Print this usage information\n";
511ca922110d262161ce922ce314bcdaadf5bbe752Chris Lattner    print "\nAll other options are passed into llvm-prof.\n";
521ca922110d262161ce922ce314bcdaadf5bbe752Chris Lattner    exit 1;
531ca922110d262161ce922ce314bcdaadf5bbe752Chris Lattner  }
54ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner
550faadf2e589b7bbeb740899429a659f576f29683Chris Lattner  # Otherwise, pass the option on to llvm-prof
560faadf2e589b7bbeb740899429a659f576f29683Chris Lattner  $LLVMProfOpts .= " " . $_;
57ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner}
58ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner
5918d52f2fb551c295bbce4c7d7357f4050b06e926Duncan Sandsdie "Must specify LLVM bitcode file as first argument!" if (@ARGV == 0);
60ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner
61ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattnermy $BytecodeFile = $ARGV[0];
62ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner
63ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattnershift @ARGV;
64ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner
65111e8ce3a2b6279dcc348878349fd459c4ed883cChris Lattnermy $libdir = `llvm-config --libdir`;
66111e8ce3a2b6279dcc348878349fd459c4ed883cChris Lattnerchomp $libdir;
67ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner
68bcffb1fbb85a88cb99f7c2115a5f78e860d7fc46Nick Lewyckymy $LibProfPath = $libdir . "/libprofile_rt.so";
69ad910ebe70644040701f8f39fd96eed78ca2ed23Chris Lattner
705cedabafe5decd3c30dea6bf54c5e47ec85c44d9Chris Lattnersystem "opt -q -f $ProfilePass $BytecodeFile -o $BytecodeFile.inst";
715cedabafe5decd3c30dea6bf54c5e47ec85c44d9Chris Lattnersystem "lli -fake-argv0 '$BytecodeFile' -load $LibProfPath " .
725cedabafe5decd3c30dea6bf54c5e47ec85c44d9Chris Lattner       "$BytecodeFile.inst $ProgramOpts " . (join ' ', @ARGV);
735cedabafe5decd3c30dea6bf54c5e47ec85c44d9Chris Lattnersystem "rm $BytecodeFile.inst";
74b9f960e39d50a30d86b16d52ef593ff6f444c5ceChris Lattnersystem "llvm-prof $LLVMProfOpts $BytecodeFile $ProfileFile";
75