This repository has been archived by the owner on Nov 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
/
pnacl-bccompress.cpp
135 lines (115 loc) · 4.45 KB
/
pnacl-bccompress.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//===-- pnacl-bccompress.cpp - Bitcode (abbrev) compression ---------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This tool may be invoked in the following manner:
// pnacl-bccompress [options] bcin.pexe -o bcout.pexe
// - Read frozen PNaCl bitcode from the bcin.pexe and introduce
// abbreviations to compress it into bcout.pexe.
//
// Options:
// --help - Output information about command line switches
//
// This tool analyzes the data in bcin.pexe, and determines what
// abbreviations can be added to compress the bitcode file. The result
// is written to bcout.pexe.
//
//===----------------------------------------------------------------------===//
#include "llvm/Bitcode/NaCl/NaClCompress.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/ToolOutputFile.h"
namespace {
using namespace llvm;
static cl::opt<bool>
TraceGeneratedAbbreviations(
"abbreviations",
cl::desc("Trace abbreviations added to compressed file"),
cl::init(false));
static cl::opt<std::string>
InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
static cl::opt<std::string>
OutputFilename("o", cl::desc("Specify output filename"),
cl::value_desc("filename"), cl::init("-"));
static cl::opt<bool>
ShowValueDistributions(
"show-distributions",
cl::desc("Show collected value distributions in bitcode records. "
"Turns off compression."),
cl::init(false));
static cl::opt<bool>
ShowAbbrevLookupTries(
"show-lookup-tries",
cl::desc("Show lookup tries used to minimize search for \n"
"matching abbreviations. Turns off compression."),
cl::init(false));
static cl::opt<bool>
ShowAbbreviationFrequencies(
"show-abbreviation-frequencies",
cl::desc("Show how often each abbreviation is used. "
"Turns off compression."),
cl::init(false));
// Note: When this flag is true, we still generate new abbreviations,
// because we don't want to add the complexity of turning it off.
// Rather, we simply make sure abbreviations are ignored when writing
// out the final copy.
static cl::opt<bool>
RemoveAbbreviations(
"remove-abbreviations",
cl::desc("Remove abbreviations from input bitcode file."),
cl::init(false));
static bool Fatal(const std::string &Err) {
errs() << Err << "\n";
exit(1);
}
// Reads the input file into the given buffer.
static void ReadAndBuffer(std::unique_ptr<MemoryBuffer> &MemBuf) {
ErrorOr<std::unique_ptr<MemoryBuffer>> ErrOrFile =
MemoryBuffer::getFileOrSTDIN(InputFilename);
if (std::error_code EC = ErrOrFile.getError())
Fatal("Error reading '" + InputFilename + "': " + EC.message());
MemBuf.reset(ErrOrFile.get().release());
if (MemBuf->getBufferSize() % 4 != 0)
Fatal("Bitcode stream should be a multiple of 4 bytes in length");
}
} // namespace
int main(int argc, char **argv) {
// Print a stack trace if we signal out.
sys::PrintStackTraceOnErrorSignal();
PrettyStackTraceProgram X(argc, argv);
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
cl::ParseCommandLineOptions(argc, argv, "pnacl-bccompress file analyzer\n");
std::unique_ptr<MemoryBuffer> MemBuf;
ReadAndBuffer(MemBuf);
std::error_code EC;
std::unique_ptr<tool_output_file> OutFile(
new tool_output_file(OutputFilename.c_str(), EC, sys::fs::F_None));
if (EC)
Fatal(EC.message());
NaClBitcodeCompressor Compressor;
Compressor.Flags.TraceGeneratedAbbreviations = TraceGeneratedAbbreviations;
Compressor.Flags.ShowValueDistributions = ShowValueDistributions;
Compressor.Flags.ShowAbbrevLookupTries = ShowAbbrevLookupTries;
Compressor.Flags.ShowAbbreviationFrequencies = ShowAbbreviationFrequencies;
Compressor.Flags.RemoveAbbreviations = RemoveAbbreviations;
if (ShowValueDistributions
|| ShowAbbreviationFrequencies
|| ShowAbbrevLookupTries) {
// Assume we are only interested in analysis.
int ReturnStatus = !Compressor.analyze(MemBuf.get(), OutFile->os());
OutFile->keep();
return ReturnStatus;
}
if (!Compressor.compress(MemBuf.get(), OutFile->os()))
return 1;
OutFile->keep();
return 0;
}