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 boltDiffMode(int argc, char **argv) {
129   cl::HideUnrelatedOptions(makeArrayRef(opts::BoltDiffCategories));
130   cl::AddExtraVersionPrinter(printBoltRevision);
131   cl::ParseCommandLineOptions(
132       argc, argv,
133       "llvm-boltdiff - BOLT binary diff tool\n"
134       "\nEXAMPLE: llvm-boltdiff -data=a.fdata -data2=b.fdata exec1 exec2\n");
135   if (opts::InputDataFilename2.empty()) {
136     errs() << ToolName << ": expected -data2=<filename> option.\n";
137     exit(1);
138   }
139   if (opts::InputDataFilename.empty()) {
140     errs() << ToolName << ": expected -data=<filename> option.\n";
141     exit(1);
142   }
143   if (opts::InputFilename2.empty()) {
144     errs() << ToolName << ": expected second binary name.\n";
145     exit(1);
146   }
147   if (opts::InputFilename.empty()) {
148     errs() << ToolName << ": expected binary.\n";
149     exit(1);
150   }
151   opts::DiffOnly = true;
152 }
153 
154 void boltMode(int argc, char **argv) {
155   cl::HideUnrelatedOptions(makeArrayRef(opts::BoltCategories));
156   // Register the target printer for --version.
157   cl::AddExtraVersionPrinter(printBoltRevision);
158   cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
159 
160   cl::ParseCommandLineOptions(argc, argv,
161                               "BOLT - Binary Optimization and Layout Tool\n");
162 
163   if (opts::OutputFilename.empty()) {
164     errs() << ToolName << ": expected -o=<output file> option.\n";
165     exit(1);
166   }
167 }
168 
169 static std::string GetExecutablePath(const char *Argv0) {
170   SmallString<256> ExecutablePath(Argv0);
171   // Do a PATH lookup if Argv0 isn't a valid path.
172   if (!llvm::sys::fs::exists(ExecutablePath))
173     if (llvm::ErrorOr<std::string> P =
174             llvm::sys::findProgramByName(ExecutablePath))
175       ExecutablePath = *P;
176   return std::string(ExecutablePath.str());
177 }
178 
179 int main(int argc, char **argv) {
180   // Print a stack trace if we signal out.
181   sys::PrintStackTraceOnErrorSignal(argv[0]);
182   PrettyStackTraceProgram X(argc, argv);
183 
184   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
185 
186   std::string ToolPath = GetExecutablePath(argv[0]);
187 
188   // Initialize targets and assembly printers/parsers.
189   llvm::InitializeAllTargetInfos();
190   llvm::InitializeAllTargetMCs();
191   llvm::InitializeAllAsmParsers();
192   llvm::InitializeAllDisassemblers();
193 
194   llvm::InitializeAllTargets();
195   llvm::InitializeAllAsmPrinters();
196 
197   ToolName = argv[0];
198 
199   if (llvm::sys::path::filename(ToolName) == "perf2bolt")
200     perf2boltMode(argc, argv);
201   else if (llvm::sys::path::filename(ToolName) == "llvm-boltdiff")
202     boltDiffMode(argc, argv);
203   else
204     boltMode(argc, argv);
205 
206   if (!sys::fs::exists(opts::InputFilename))
207     report_error(opts::InputFilename, errc::no_such_file_or_directory);
208 
209   // Attempt to open the binary.
210   if (!opts::DiffOnly) {
211     Expected<OwningBinary<Binary>> BinaryOrErr =
212         createBinary(opts::InputFilename);
213     if (Error E = BinaryOrErr.takeError())
214       report_error(opts::InputFilename, std::move(E));
215     Binary &Binary = *BinaryOrErr.get().getBinary();
216 
217     if (auto *e = dyn_cast<ELFObjectFileBase>(&Binary)) {
218       RewriteInstance RI(e, argc, argv, ToolPath);
219       if (!opts::PerfData.empty()) {
220         if (!opts::AggregateOnly) {
221           errs() << ToolName
222                  << ": WARNING: reading perf data directly is unsupported, "
223                     "please use "
224                     "-aggregate-only or perf2bolt.\n!!! Proceed on your own "
225                     "risk. !!!\n";
226         }
227         if (Error E = RI.setProfile(opts::PerfData))
228           report_error(opts::PerfData, std::move(E));
229       }
230       if (!opts::InputDataFilename.empty()) {
231         if (Error E = RI.setProfile(opts::InputDataFilename))
232           report_error(opts::InputDataFilename, std::move(E));
233       }
234       if (opts::AggregateOnly && opts::PerfData.empty()) {
235         errs() << ToolName << ": missing required -perfdata option.\n";
236         exit(1);
237       }
238 
239       RI.run();
240     } else if (auto *O = dyn_cast<MachOObjectFile>(&Binary)) {
241       MachORewriteInstance MachORI(O, ToolPath);
242 
243       if (!opts::InputDataFilename.empty())
244         if (Error E = MachORI.setProfile(opts::InputDataFilename))
245           report_error(opts::InputDataFilename, std::move(E));
246 
247       MachORI.run();
248     } else {
249       report_error(opts::InputFilename, object_error::invalid_file_type);
250     }
251 
252     return EXIT_SUCCESS;
253   }
254 
255   // Bolt-diff
256   Expected<OwningBinary<Binary>> BinaryOrErr1 =
257       createBinary(opts::InputFilename);
258   Expected<OwningBinary<Binary>> BinaryOrErr2 =
259       createBinary(opts::InputFilename2);
260   if (Error E = BinaryOrErr1.takeError())
261     report_error(opts::InputFilename, std::move(E));
262   if (Error E = BinaryOrErr2.takeError())
263     report_error(opts::InputFilename2, std::move(E));
264   Binary &Binary1 = *BinaryOrErr1.get().getBinary();
265   Binary &Binary2 = *BinaryOrErr2.get().getBinary();
266   if (auto *ELFObj1 = dyn_cast<ELFObjectFileBase>(&Binary1)) {
267     if (auto *ELFObj2 = dyn_cast<ELFObjectFileBase>(&Binary2)) {
268       RewriteInstance RI1(ELFObj1, argc, argv, ToolPath);
269       if (Error E = RI1.setProfile(opts::InputDataFilename))
270         report_error(opts::InputDataFilename, std::move(E));
271       RewriteInstance RI2(ELFObj2, argc, argv, ToolPath);
272       if (Error E = RI2.setProfile(opts::InputDataFilename2))
273         report_error(opts::InputDataFilename2, std::move(E));
274       outs() << "BOLT-DIFF: *** Analyzing binary 1: " << opts::InputFilename
275              << "\n";
276       outs() << "BOLT-DIFF: *** Binary 1 fdata:     " << opts::InputDataFilename
277              << "\n";
278       RI1.run();
279       outs() << "BOLT-DIFF: *** Analyzing binary 2: " << opts::InputFilename2
280              << "\n";
281       outs() << "BOLT-DIFF: *** Binary 2 fdata:     "
282              << opts::InputDataFilename2 << "\n";
283       RI2.run();
284       RI1.compare(RI2);
285     } else {
286       report_error(opts::InputFilename2, object_error::invalid_file_type);
287     }
288   } else {
289     report_error(opts::InputFilename, object_error::invalid_file_type);
290   }
291 
292   return EXIT_SUCCESS;
293 }
294