1 //===- bolt/tools/driver/llvm-bolt.cpp - Feedback-directed optimizer ------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This is a binary optimizer that will take 'perf' output and change
10 // basic block layout for better performance (a.k.a. branch straightening),
11 // plus some other optimizations that are better performed on a binary.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "bolt/Profile/DataAggregator.h"
16 #include "bolt/Rewrite/MachORewriteInstance.h"
17 #include "bolt/Rewrite/RewriteInstance.h"
18 #include "bolt/Utils/CommandLineOpts.h"
19 #include "llvm/MC/TargetRegistry.h"
20 #include "llvm/Object/Binary.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/Path.h"
25 #include "llvm/Support/PrettyStackTrace.h"
26 #include "llvm/Support/Signals.h"
27 #include "llvm/Support/TargetSelect.h"
28 
29 #define DEBUG_TYPE "bolt"
30 
31 using namespace llvm;
32 using namespace object;
33 using namespace bolt;
34 
35 namespace opts {
36 
37 static cl::OptionCategory *BoltCategories[] = {&BoltCategory,
38                                                &BoltOptCategory,
39                                                &BoltRelocCategory,
40                                                &BoltInstrCategory,
41                                                &BoltOutputCategory};
42 
43 static cl::OptionCategory *BoltDiffCategories[] = {&BoltDiffCategory};
44 
45 static cl::OptionCategory *Perf2BoltCategories[] = {&AggregatorCategory,
46                                                     &BoltOutputCategory};
47 
48 static cl::opt<std::string> InputFilename(cl::Positional,
49                                           cl::desc("<executable>"),
50                                           cl::Required, cl::cat(BoltCategory),
51                                           cl::sub(*cl::AllSubCommands));
52 
53 static cl::opt<std::string>
54 InputDataFilename("data",
55   cl::desc("<data file>"),
56   cl::Optional,
57   cl::cat(BoltCategory));
58 
59 static cl::alias
60 BoltProfile("b",
61   cl::desc("alias for -data"),
62   cl::aliasopt(InputDataFilename),
63   cl::cat(BoltCategory));
64 
65 static cl::opt<std::string>
66 InputDataFilename2("data2",
67   cl::desc("<data file>"),
68   cl::Optional,
69   cl::cat(BoltCategory));
70 
71 static cl::opt<std::string>
72 InputFilename2(
73   cl::Positional,
74   cl::desc("<executable>"),
75   cl::Optional,
76   cl::cat(BoltDiffCategory));
77 
78 } // namespace opts
79 
80 static StringRef ToolName;
81 
82 static void report_error(StringRef Message, std::error_code EC) {
83   assert(EC);
84   errs() << ToolName << ": '" << Message << "': " << EC.message() << ".\n";
85   exit(1);
86 }
87 
88 static void report_error(StringRef Message, Error E) {
89   assert(E);
90   errs() << ToolName << ": '" << Message << "': " << toString(std::move(E))
91          << ".\n";
92   exit(1);
93 }
94 
95 static void printBoltRevision(llvm::raw_ostream &OS) {
96   OS << "BOLT revision " << BoltRevision << "\n";
97 }
98 
99 void perf2boltMode(int argc, char **argv) {
100   cl::HideUnrelatedOptions(makeArrayRef(opts::Perf2BoltCategories));
101   cl::AddExtraVersionPrinter(printBoltRevision);
102   cl::ParseCommandLineOptions(
103       argc, argv,
104       "perf2bolt - BOLT data aggregator\n"
105       "\nEXAMPLE: perf2bolt -p=perf.data executable -o data.fdata\n");
106   if (opts::PerfData.empty()) {
107     errs() << ToolName << ": expected -perfdata=<filename> option.\n";
108     exit(1);
109   }
110   if (!opts::InputDataFilename.empty()) {
111     errs() << ToolName << ": unknown -data option.\n";
112     exit(1);
113   }
114   if (!sys::fs::exists(opts::PerfData))
115     report_error(opts::PerfData, errc::no_such_file_or_directory);
116   if (!DataAggregator::checkPerfDataMagic(opts::PerfData)) {
117     errs() << ToolName << ": '" << opts::PerfData
118            << "': expected valid perf.data file.\n";
119     exit(1);
120   }
121   if (opts::OutputFilename.empty()) {
122     errs() << ToolName << ": expected -o=<output file> option.\n";
123     exit(1);
124   }
125   opts::AggregateOnly = true;
126 }
127 
128 void heatmapMode(int argc, char **argv) {
129   // Insert a fake subcommand if invoked via a command alias.
130   std::unique_ptr<char *[]> FakeArgv;
131   if (argc == 1 || strcmp(argv[1], "heatmap")) {
132     ++argc;
133     FakeArgv.reset(new char *[argc + 1]);
134     FakeArgv[0] = argv[0];
135     FakeArgv[1] = const_cast<char *>("heatmap");
136     for (int I = 2; I < argc; ++I)
137       FakeArgv[I] = argv[I - 1];
138     FakeArgv[argc] = nullptr;
139     argv = FakeArgv.get();
140   }
141 
142   cl::ParseCommandLineOptions(argc, argv, "");
143 
144   if (!sys::fs::exists(opts::InputFilename))
145     report_error(opts::InputFilename, errc::no_such_file_or_directory);
146 
147   if (opts::PerfData.empty()) {
148     errs() << ToolName << ": expected -perfdata=<filename> option.\n";
149     exit(1);
150   }
151 
152   opts::HeatmapMode = true;
153   opts::AggregateOnly = true;
154 }
155 
156 void boltDiffMode(int argc, char **argv) {
157   cl::HideUnrelatedOptions(makeArrayRef(opts::BoltDiffCategories));
158   cl::AddExtraVersionPrinter(printBoltRevision);
159   cl::ParseCommandLineOptions(
160       argc, argv,
161       "llvm-boltdiff - BOLT binary diff tool\n"
162       "\nEXAMPLE: llvm-boltdiff -data=a.fdata -data2=b.fdata exec1 exec2\n");
163   if (opts::InputDataFilename2.empty()) {
164     errs() << ToolName << ": expected -data2=<filename> option.\n";
165     exit(1);
166   }
167   if (opts::InputDataFilename.empty()) {
168     errs() << ToolName << ": expected -data=<filename> option.\n";
169     exit(1);
170   }
171   if (opts::InputFilename2.empty()) {
172     errs() << ToolName << ": expected second binary name.\n";
173     exit(1);
174   }
175   if (opts::InputFilename.empty()) {
176     errs() << ToolName << ": expected binary.\n";
177     exit(1);
178   }
179   opts::DiffOnly = true;
180 }
181 
182 void boltMode(int argc, char **argv) {
183   cl::HideUnrelatedOptions(makeArrayRef(opts::BoltCategories));
184   // Register the target printer for --version.
185   cl::AddExtraVersionPrinter(printBoltRevision);
186   cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
187 
188   cl::ParseCommandLineOptions(argc, argv,
189                               "BOLT - Binary Optimization and Layout Tool\n");
190 
191   if (opts::OutputFilename.empty()) {
192     errs() << ToolName << ": expected -o=<output file> option.\n";
193     exit(1);
194   }
195 }
196 
197 std::string GetExecutablePath(const char *Argv0) {
198   SmallString<128> ExecutablePath(Argv0);
199   // Do a PATH lookup if Argv0 isn't a valid path.
200   if (!llvm::sys::fs::exists(ExecutablePath))
201     if (llvm::ErrorOr<std::string> P =
202             llvm::sys::findProgramByName(ExecutablePath))
203       ExecutablePath = *P;
204   return std::string(ExecutablePath.str());
205 }
206 
207 int main(int argc, char **argv) {
208   // Print a stack trace if we signal out.
209   sys::PrintStackTraceOnErrorSignal(argv[0]);
210   PrettyStackTraceProgram X(argc, argv);
211 
212   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
213 
214   std::string ToolPath = GetExecutablePath(argv[0]);
215 
216   // Initialize targets and assembly printers/parsers.
217   llvm::InitializeAllTargetInfos();
218   llvm::InitializeAllTargetMCs();
219   llvm::InitializeAllAsmParsers();
220   llvm::InitializeAllDisassemblers();
221 
222   llvm::InitializeAllTargets();
223   llvm::InitializeAllAsmPrinters();
224 
225   ToolName = argv[0];
226 
227   // Pre-process subcommands.
228   if (argc > 1 && *argv[1] != '-') {
229     if (!strcmp(argv[1], "heatmap"))
230       opts::HeatmapMode = true;
231   }
232 
233   if (llvm::sys::path::filename(ToolName) == "perf2bolt")
234     perf2boltMode(argc, argv);
235   else if (llvm::sys::path::filename(ToolName) == "llvm-boltdiff")
236     boltDiffMode(argc, argv);
237   else if (llvm::sys::path::filename(ToolName) == "llvm-bolt-heatmap" ||
238            opts::HeatmapMode)
239     heatmapMode(argc, argv);
240   else
241     boltMode(argc, argv);
242 
243   if (!sys::fs::exists(opts::InputFilename))
244     report_error(opts::InputFilename, errc::no_such_file_or_directory);
245 
246   // Attempt to open the binary.
247   if (!opts::DiffOnly) {
248     Expected<OwningBinary<Binary>> BinaryOrErr =
249         createBinary(opts::InputFilename);
250     if (Error E = BinaryOrErr.takeError())
251       report_error(opts::InputFilename, std::move(E));
252     Binary &Binary = *BinaryOrErr.get().getBinary();
253 
254     if (auto *e = dyn_cast<ELFObjectFileBase>(&Binary)) {
255       RewriteInstance RI(e, argc, argv, ToolPath);
256       if (!opts::PerfData.empty()) {
257         if (!opts::AggregateOnly) {
258           errs() << ToolName
259                  << ": WARNING: reading perf data directly is unsupported, "
260                     "please use "
261                     "-aggregate-only or perf2bolt.\n!!! Proceed on your own "
262                     "risk. !!!\n";
263         }
264         if (Error E = RI.setProfile(opts::PerfData))
265           report_error(opts::PerfData, std::move(E));
266       }
267       if (!opts::InputDataFilename.empty()) {
268         if (Error E = RI.setProfile(opts::InputDataFilename))
269           report_error(opts::InputDataFilename, std::move(E));
270       }
271       if (opts::AggregateOnly && opts::PerfData.empty()) {
272         errs() << ToolName << ": missing required -perfdata option.\n";
273         exit(1);
274       }
275 
276       RI.run();
277     } else if (auto *O = dyn_cast<MachOObjectFile>(&Binary)) {
278       MachORewriteInstance MachORI(O, ToolPath);
279 
280       if (!opts::InputDataFilename.empty())
281         if (Error E = MachORI.setProfile(opts::InputDataFilename))
282           report_error(opts::InputDataFilename, std::move(E));
283 
284       MachORI.run();
285     } else {
286       report_error(opts::InputFilename, object_error::invalid_file_type);
287     }
288 
289     return EXIT_SUCCESS;
290   }
291 
292   // Bolt-diff
293   Expected<OwningBinary<Binary>> BinaryOrErr1 =
294       createBinary(opts::InputFilename);
295   Expected<OwningBinary<Binary>> BinaryOrErr2 =
296       createBinary(opts::InputFilename2);
297   if (Error E = BinaryOrErr1.takeError())
298     report_error(opts::InputFilename, std::move(E));
299   if (Error E = BinaryOrErr2.takeError())
300     report_error(opts::InputFilename2, std::move(E));
301   Binary &Binary1 = *BinaryOrErr1.get().getBinary();
302   Binary &Binary2 = *BinaryOrErr2.get().getBinary();
303   if (auto *ELFObj1 = dyn_cast<ELFObjectFileBase>(&Binary1)) {
304     if (auto *ELFObj2 = dyn_cast<ELFObjectFileBase>(&Binary2)) {
305       RewriteInstance RI1(ELFObj1, argc, argv, ToolPath);
306       if (Error E = RI1.setProfile(opts::InputDataFilename))
307         report_error(opts::InputDataFilename, std::move(E));
308       RewriteInstance RI2(ELFObj2, argc, argv, ToolPath);
309       if (Error E = RI2.setProfile(opts::InputDataFilename2))
310         report_error(opts::InputDataFilename2, std::move(E));
311       outs() << "BOLT-DIFF: *** Analyzing binary 1: " << opts::InputFilename
312              << "\n";
313       outs() << "BOLT-DIFF: *** Binary 1 fdata:     " << opts::InputDataFilename
314              << "\n";
315       RI1.run();
316       outs() << "BOLT-DIFF: *** Analyzing binary 2: " << opts::InputFilename2
317              << "\n";
318       outs() << "BOLT-DIFF: *** Binary 2 fdata:     "
319              << opts::InputDataFilename2 << "\n";
320       RI2.run();
321       RI1.compare(RI2);
322     } else {
323       report_error(opts::InputFilename2, object_error::invalid_file_type);
324     }
325   } else {
326     report_error(opts::InputFilename, object_error::invalid_file_type);
327   }
328 
329   return EXIT_SUCCESS;
330 }
331