exit(1) instead of abort()'ing on error
[oota-llvm.git] / utils / profile.pl
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] bytecodefile <arguments>
9 #
10 # OPTIONS may include one or more of the following:
11 #     -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
15 #
16 # Any unrecognized options are passed into the invocation of llvm-prof
17 #
18
19 my $ProfilePass = "-insert-block-profiling";
20
21 my $LLVMProfOpts = "";
22 my $ProgramOpts = "";
23 my $ProfileFile = "";
24
25 # Parse arguments...
26 while (scalar(@ARGV) and ($_ = $ARGV[0], /^[-+]/)) {
27   shift;
28   last if /^--$/;  # Stop processing arguments on --
29
30   # List command line options here...
31   if (/^-?-block$/) { $ProfilePass = "-insert-block-profiling"; next; }
32   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   }
40   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";
44     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";
48     print "\nAll other options are passed into llvm-prof.\n";
49     exit 1;
50   }
51
52   # Otherwise, pass the option on to llvm-prof
53   $LLVMProfOpts .= " " . $_;
54 }
55
56 die "Must specify LLVM bytecode file as first argument!" if (@ARGV == 0);
57
58 my $BytecodeFile = $ARGV[0];
59
60 shift @ARGV;
61
62 my $LLIPath = `which lli`;
63 $LLIPath = `dirname $LLIPath`;
64 chomp $LLIPath;
65
66 my $LibProfPath = $LLIPath . "/../../lib/Debug/libprofile_rt.so";
67
68 system "opt -q $ProfilePass < $BytecodeFile | lli -fake-argv0 '$BytecodeFile'" .
69        " -load $LibProfPath -$ProgramOpts " . (join ' ', @ARGV);
70
71 system "llvm-prof $LLVMProfOpts $BytecodeFile $ProfileFile";