1 //===-- llvm-bcanalyzer.cpp - Byte Code Analyzer --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tool may be invoked in the following manner:
11 // llvm-bcanalyzer [options] - Read LLVM bytecode from stdin
12 // llvm-bcanalyzer [options] x.bc - Read LLVM bytecode from the x.bc file
15 // --help - Output information about command line switches
16 // --nodetails - Don't print out detailed informaton about individual
17 // blocks and functions
18 // --dump - Dump low-level bytecode structure in readable format
20 // This tool provides analytical information about a bytecode file. It is
21 // intended as an aid to developers of bytecode reading and writing software. It
22 // produces on std::out a summary of the bytecode file that shows various
23 // statistics about the contents of the file. By default this information is
24 // detailed and contains information about individual bytecode blocks and the
25 // functions in the module. To avoid this more detailed output, use the
26 // -nodetails option to limit the output to just module level information.
27 // The tool is also able to print a bytecode file in a straight forward text
28 // format that shows the containment and relationships of the information in
29 // the bytecode file (-dump option).
31 //===----------------------------------------------------------------------===//
33 #include "llvm/Analysis/Verifier.h"
34 #include "llvm/Bitcode/BitstreamReader.h"
35 #include "llvm/Bytecode/Analyzer.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/Compressor.h"
38 #include "llvm/Support/ManagedStatic.h"
39 #include "llvm/Support/MemoryBuffer.h"
40 #include "llvm/System/Signals.h"
45 static cl::opt<std::string>
46 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
48 static cl::opt<std::string>
49 OutputFilename("-o", cl::init("-"), cl::desc("<output file>"));
51 static cl::opt<bool> NoDetails("nodetails", cl::desc("Skip detailed output"));
52 static cl::opt<bool> Dump("dump", cl::desc("Dump low level bytecode trace"));
53 static cl::opt<bool> Verify("verify", cl::desc("Progressively verify module"));
54 static cl::opt<bool> Bitcode("bitcode", cl::desc("Read a bitcode file"));
56 /// CurStreamType - If we can sniff the flavor of this stream, we can produce
63 /// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
64 static int AnalyzeBitcode() {
65 // Read the input file.
67 if (InputFilename == "-")
68 Buffer = MemoryBuffer::getSTDIN();
70 Buffer = MemoryBuffer::getFile(&InputFilename[0], InputFilename.size());
73 std::cerr << "Error reading '" << InputFilename << "'.\n";
77 if (Buffer->getBufferSize() & 3) {
78 std::cerr << "Bitcode stream should be a multiple of 4 bytes in length\n";
82 unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart();
83 BitstreamReader Stream(BufPtr, BufPtr+Buffer->getBufferSize());
86 // Read the stream signature.
88 Signature[0] = Stream.Read(8);
89 Signature[1] = Stream.Read(8);
90 Signature[2] = Stream.Read(4);
91 Signature[3] = Stream.Read(4);
92 Signature[4] = Stream.Read(4);
93 Signature[5] = Stream.Read(4);
95 CurStreamType = UnknownBitstream;
96 if (Signature[0] == 'B' && Signature[1] == 'C' &&
97 Signature[2] == 0x0 && Signature[3] == 0xC &&
98 Signature[4] == 0xE && Signature[5] == 0xD)
99 CurStreamType = LLVMIRBitstream;
101 std::cerr << "Summary of " << InputFilename << ":\n";
102 std::cerr << " Stream type: ";
103 switch (CurStreamType) {
104 default: assert(0 && "Unknown bitstream type");
105 case UnknownBitstream: std::cerr << "unknown\n"; break;
106 case LLVMIRBitstream: std::cerr << "LLVM IR\n"; break;
112 int main(int argc, char **argv) {
113 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
114 cl::ParseCommandLineOptions(argc, argv, " llvm-bcanalyzer file analyzer\n");
116 sys::PrintStackTraceOnErrorSignal();
119 return AnalyzeBitcode();
122 std::ostream *Out = &std::cout; // Default to printing to stdout...
123 std::string ErrorMessage;
124 BytecodeAnalysis bca;
126 /// Determine what to generate
127 bca.detailedResults = !NoDetails;
128 bca.progressiveVerify = Verify;
130 /// Analyze the bytecode file
131 Module* M = AnalyzeBytecodeFile(InputFilename, bca,
132 Compressor::decompressToNewBuffer,
133 &ErrorMessage, (Dump?Out:0));
135 // All that bcanalyzer does is write the gathered statistics to the output
136 PrintBytecodeAnalysis(bca,*Out);
139 std::string verificationMsg;
140 if (verifyModule(*M, ReturnStatusAction, &verificationMsg))
141 std::cerr << "Final Verification Message: " << verificationMsg << "\n";
144 if (Out != &std::cout) {
145 ((std::ofstream*)Out)->close();
149 } catch (const std::string& msg) {
150 std::cerr << argv[0] << ": " << msg << "\n";
152 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";