Bug Summary

File:build/source/bolt/lib/Core/BinaryFunction.cpp
Warning:line 4172, column 33
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name BinaryFunction.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/source/build-llvm/tools/clang/stage2-bins -resource-dir /usr/lib/llvm-17/lib/clang/17 -I tools/bolt/lib/Core -I /build/source/bolt/lib/Core -I include -I /build/source/llvm/include -I /build/source/bolt/include -I tools/bolt/include -D _DEBUG -D _GLIBCXX_ASSERTIONS -D _GNU_SOURCE -D _LIBCPP_ENABLE_ASSERTIONS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-17/lib/clang/17/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fmacro-prefix-map=/build/source/= -fcoverage-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fcoverage-prefix-map=/build/source/= -source-date-epoch 1677842174 -O2 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -Wno-misleading-indentation -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/build/source/build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/source/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2023-03-03-140516-16496-1 -x c++ /build/source/bolt/lib/Core/BinaryFunction.cpp
1//===- bolt/Core/BinaryFunction.cpp - Low-level function ------------------===//
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 file implements the BinaryFunction class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "bolt/Core/BinaryFunction.h"
14#include "bolt/Core/BinaryBasicBlock.h"
15#include "bolt/Core/BinaryDomTree.h"
16#include "bolt/Core/DynoStats.h"
17#include "bolt/Core/MCPlusBuilder.h"
18#include "bolt/Utils/NameResolver.h"
19#include "bolt/Utils/NameShortener.h"
20#include "bolt/Utils/Utils.h"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/SmallSet.h"
23#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/Demangle/Demangle.h"
26#include "llvm/MC/MCAsmInfo.h"
27#include "llvm/MC/MCAsmLayout.h"
28#include "llvm/MC/MCContext.h"
29#include "llvm/MC/MCDisassembler/MCDisassembler.h"
30#include "llvm/MC/MCExpr.h"
31#include "llvm/MC/MCInst.h"
32#include "llvm/MC/MCInstPrinter.h"
33#include "llvm/MC/MCRegisterInfo.h"
34#include "llvm/MC/MCSymbol.h"
35#include "llvm/Object/ObjectFile.h"
36#include "llvm/Support/CommandLine.h"
37#include "llvm/Support/Debug.h"
38#include "llvm/Support/GraphWriter.h"
39#include "llvm/Support/LEB128.h"
40#include "llvm/Support/Regex.h"
41#include "llvm/Support/Timer.h"
42#include "llvm/Support/raw_ostream.h"
43#include <functional>
44#include <limits>
45#include <numeric>
46#include <string>
47
48#define DEBUG_TYPE"bolt" "bolt"
49
50using namespace llvm;
51using namespace bolt;
52
53namespace opts {
54
55extern cl::OptionCategory BoltCategory;
56extern cl::OptionCategory BoltOptCategory;
57extern cl::OptionCategory BoltRelocCategory;
58
59extern cl::opt<bool> EnableBAT;
60extern cl::opt<bool> Instrument;
61extern cl::opt<bool> StrictMode;
62extern cl::opt<bool> UpdateDebugSections;
63extern cl::opt<unsigned> Verbosity;
64
65extern bool processAllFunctions();
66
67cl::opt<bool> CheckEncoding(
68 "check-encoding",
69 cl::desc("perform verification of LLVM instruction encoding/decoding. "
70 "Every instruction in the input is decoded and re-encoded. "
71 "If the resulting bytes do not match the input, a warning message "
72 "is printed."),
73 cl::Hidden, cl::cat(BoltCategory));
74
75static cl::opt<bool> DotToolTipCode(
76 "dot-tooltip-code",
77 cl::desc("add basic block instructions as tool tips on nodes"), cl::Hidden,
78 cl::cat(BoltCategory));
79
80cl::opt<JumpTableSupportLevel>
81JumpTables("jump-tables",
82 cl::desc("jump tables support (default=basic)"),
83 cl::init(JTS_BASIC),
84 cl::values(
85 clEnumValN(JTS_NONE, "none",llvm::cl::OptionEnumValue { "none", int(JTS_NONE), "do not optimize functions with jump tables"
}
86 "do not optimize functions with jump tables")llvm::cl::OptionEnumValue { "none", int(JTS_NONE), "do not optimize functions with jump tables"
}
,
87 clEnumValN(JTS_BASIC, "basic",llvm::cl::OptionEnumValue { "basic", int(JTS_BASIC), "optimize functions with jump tables"
}
88 "optimize functions with jump tables")llvm::cl::OptionEnumValue { "basic", int(JTS_BASIC), "optimize functions with jump tables"
}
,
89 clEnumValN(JTS_MOVE, "move",llvm::cl::OptionEnumValue { "move", int(JTS_MOVE), "move jump tables to a separate section"
}
90 "move jump tables to a separate section")llvm::cl::OptionEnumValue { "move", int(JTS_MOVE), "move jump tables to a separate section"
}
,
91 clEnumValN(JTS_SPLIT, "split",llvm::cl::OptionEnumValue { "split", int(JTS_SPLIT), "split jump tables section into hot and cold based on "
"function execution frequency" }
92 "split jump tables section into hot and cold based on "llvm::cl::OptionEnumValue { "split", int(JTS_SPLIT), "split jump tables section into hot and cold based on "
"function execution frequency" }
93 "function execution frequency")llvm::cl::OptionEnumValue { "split", int(JTS_SPLIT), "split jump tables section into hot and cold based on "
"function execution frequency" }
,
94 clEnumValN(JTS_AGGRESSIVE, "aggressive",llvm::cl::OptionEnumValue { "aggressive", int(JTS_AGGRESSIVE)
, "aggressively split jump tables section based on usage " "of the tables"
}
95 "aggressively split jump tables section based on usage "llvm::cl::OptionEnumValue { "aggressive", int(JTS_AGGRESSIVE)
, "aggressively split jump tables section based on usage " "of the tables"
}
96 "of the tables")llvm::cl::OptionEnumValue { "aggressive", int(JTS_AGGRESSIVE)
, "aggressively split jump tables section based on usage " "of the tables"
}
),
97 cl::ZeroOrMore,
98 cl::cat(BoltOptCategory));
99
100static cl::opt<bool> NoScan(
101 "no-scan",
102 cl::desc(
103 "do not scan cold functions for external references (may result in "
104 "slower binary)"),
105 cl::Hidden, cl::cat(BoltOptCategory));
106
107cl::opt<bool>
108 PreserveBlocksAlignment("preserve-blocks-alignment",
109 cl::desc("try to preserve basic block alignment"),
110 cl::cat(BoltOptCategory));
111
112cl::opt<bool>
113PrintDynoStats("dyno-stats",
114 cl::desc("print execution info based on profile"),
115 cl::cat(BoltCategory));
116
117static cl::opt<bool>
118PrintDynoStatsOnly("print-dyno-stats-only",
119 cl::desc("while printing functions output dyno-stats and skip instructions"),
120 cl::init(false),
121 cl::Hidden,
122 cl::cat(BoltCategory));
123
124static cl::list<std::string>
125PrintOnly("print-only",
126 cl::CommaSeparated,
127 cl::desc("list of functions to print"),
128 cl::value_desc("func1,func2,func3,..."),
129 cl::Hidden,
130 cl::cat(BoltCategory));
131
132cl::opt<bool>
133 TimeBuild("time-build",
134 cl::desc("print time spent constructing binary functions"),
135 cl::Hidden, cl::cat(BoltCategory));
136
137cl::opt<bool>
138TrapOnAVX512("trap-avx512",
139 cl::desc("in relocation mode trap upon entry to any function that uses "
140 "AVX-512 instructions"),
141 cl::init(false),
142 cl::ZeroOrMore,
143 cl::Hidden,
144 cl::cat(BoltCategory));
145
146bool shouldPrint(const BinaryFunction &Function) {
147 if (Function.isIgnored())
148 return false;
149
150 if (PrintOnly.empty())
151 return true;
152
153 for (std::string &Name : opts::PrintOnly) {
154 if (Function.hasNameRegex(Name)) {
155 return true;
156 }
157 }
158
159 return false;
160}
161
162} // namespace opts
163
164namespace llvm {
165namespace bolt {
166
167constexpr unsigned BinaryFunction::MinAlign;
168
169template <typename R> static bool emptyRange(const R &Range) {
170 return Range.begin() == Range.end();
171}
172
173/// Gets debug line information for the instruction located at the given
174/// address in the original binary. The SMLoc's pointer is used
175/// to point to this information, which is represented by a
176/// DebugLineTableRowRef. The returned pointer is null if no debug line
177/// information for this instruction was found.
178static SMLoc findDebugLineInformationForInstructionAt(
179 uint64_t Address, DWARFUnit *Unit,
180 const DWARFDebugLine::LineTable *LineTable) {
181 // We use the pointer in SMLoc to store an instance of DebugLineTableRowRef,
182 // which occupies 64 bits. Thus, we can only proceed if the struct fits into
183 // the pointer itself.
184 assert(sizeof(decltype(SMLoc().getPointer())) >=(static_cast <bool> (sizeof(decltype(SMLoc().getPointer
())) >= sizeof(DebugLineTableRowRef) && "Cannot fit instruction debug line information into SMLoc's pointer"
) ? void (0) : __assert_fail ("sizeof(decltype(SMLoc().getPointer())) >= sizeof(DebugLineTableRowRef) && \"Cannot fit instruction debug line information into SMLoc's pointer\""
, "bolt/lib/Core/BinaryFunction.cpp", 186, __extension__ __PRETTY_FUNCTION__
))
185 sizeof(DebugLineTableRowRef) &&(static_cast <bool> (sizeof(decltype(SMLoc().getPointer
())) >= sizeof(DebugLineTableRowRef) && "Cannot fit instruction debug line information into SMLoc's pointer"
) ? void (0) : __assert_fail ("sizeof(decltype(SMLoc().getPointer())) >= sizeof(DebugLineTableRowRef) && \"Cannot fit instruction debug line information into SMLoc's pointer\""
, "bolt/lib/Core/BinaryFunction.cpp", 186, __extension__ __PRETTY_FUNCTION__
))
186 "Cannot fit instruction debug line information into SMLoc's pointer")(static_cast <bool> (sizeof(decltype(SMLoc().getPointer
())) >= sizeof(DebugLineTableRowRef) && "Cannot fit instruction debug line information into SMLoc's pointer"
) ? void (0) : __assert_fail ("sizeof(decltype(SMLoc().getPointer())) >= sizeof(DebugLineTableRowRef) && \"Cannot fit instruction debug line information into SMLoc's pointer\""
, "bolt/lib/Core/BinaryFunction.cpp", 186, __extension__ __PRETTY_FUNCTION__
))
;
187
188 SMLoc NullResult = DebugLineTableRowRef::NULL_ROW.toSMLoc();
189 uint32_t RowIndex = LineTable->lookupAddress(
190 {Address, object::SectionedAddress::UndefSection});
191 if (RowIndex == LineTable->UnknownRowIndex)
192 return NullResult;
193
194 assert(RowIndex < LineTable->Rows.size() &&(static_cast <bool> (RowIndex < LineTable->Rows.size
() && "Line Table lookup returned invalid index.") ? void
(0) : __assert_fail ("RowIndex < LineTable->Rows.size() && \"Line Table lookup returned invalid index.\""
, "bolt/lib/Core/BinaryFunction.cpp", 195, __extension__ __PRETTY_FUNCTION__
))
195 "Line Table lookup returned invalid index.")(static_cast <bool> (RowIndex < LineTable->Rows.size
() && "Line Table lookup returned invalid index.") ? void
(0) : __assert_fail ("RowIndex < LineTable->Rows.size() && \"Line Table lookup returned invalid index.\""
, "bolt/lib/Core/BinaryFunction.cpp", 195, __extension__ __PRETTY_FUNCTION__
))
;
196
197 decltype(SMLoc().getPointer()) Ptr;
198 DebugLineTableRowRef *InstructionLocation =
199 reinterpret_cast<DebugLineTableRowRef *>(&Ptr);
200
201 InstructionLocation->DwCompileUnitIndex = Unit->getOffset();
202 InstructionLocation->RowIndex = RowIndex + 1;
203
204 return SMLoc::getFromPointer(Ptr);
205}
206
207static std::string buildSectionName(StringRef Prefix, StringRef Name,
208 const BinaryContext &BC) {
209 if (BC.isELF())
210 return (Prefix + Name).str();
211 static NameShortener NS;
212 return (Prefix + Twine(NS.getID(Name))).str();
213}
214
215static raw_ostream &operator<<(raw_ostream &OS,
216 const BinaryFunction::State State) {
217 switch (State) {
218 case BinaryFunction::State::Empty: OS << "empty"; break;
219 case BinaryFunction::State::Disassembled: OS << "disassembled"; break;
220 case BinaryFunction::State::CFG: OS << "CFG constructed"; break;
221 case BinaryFunction::State::CFG_Finalized: OS << "CFG finalized"; break;
222 case BinaryFunction::State::EmittedCFG: OS << "emitted with CFG"; break;
223 case BinaryFunction::State::Emitted: OS << "emitted"; break;
224 }
225
226 return OS;
227}
228
229std::string BinaryFunction::buildCodeSectionName(StringRef Name,
230 const BinaryContext &BC) {
231 return buildSectionName(BC.isELF() ? ".local.text." : ".l.text.", Name, BC);
232}
233
234std::string BinaryFunction::buildColdCodeSectionName(StringRef Name,
235 const BinaryContext &BC) {
236 return buildSectionName(BC.isELF() ? ".local.cold.text." : ".l.c.text.", Name,
237 BC);
238}
239
240uint64_t BinaryFunction::Count = 0;
241
242std::optional<StringRef>
243BinaryFunction::hasNameRegex(const StringRef Name) const {
244 const std::string RegexName = (Twine("^") + StringRef(Name) + "$").str();
245 Regex MatchName(RegexName);
246 return forEachName(
247 [&MatchName](StringRef Name) { return MatchName.match(Name); });
248}
249
250std::optional<StringRef>
251BinaryFunction::hasRestoredNameRegex(const StringRef Name) const {
252 const std::string RegexName = (Twine("^") + StringRef(Name) + "$").str();
253 Regex MatchName(RegexName);
254 return forEachName([&MatchName](StringRef Name) {
255 return MatchName.match(NameResolver::restore(Name));
256 });
257}
258
259std::string BinaryFunction::getDemangledName() const {
260 StringRef MangledName = NameResolver::restore(getOneName());
261 return demangle(MangledName.str());
262}
263
264BinaryBasicBlock *
265BinaryFunction::getBasicBlockContainingOffset(uint64_t Offset) {
266 if (Offset > Size)
267 return nullptr;
268
269 if (BasicBlockOffsets.empty())
270 return nullptr;
271
272 /*
273 * This is commented out because it makes BOLT too slow.
274 * assert(std::is_sorted(BasicBlockOffsets.begin(),
275 * BasicBlockOffsets.end(),
276 * CompareBasicBlockOffsets())));
277 */
278 auto I =
279 llvm::upper_bound(BasicBlockOffsets, BasicBlockOffset(Offset, nullptr),
280 CompareBasicBlockOffsets());
281 assert(I != BasicBlockOffsets.begin() && "first basic block not at offset 0")(static_cast <bool> (I != BasicBlockOffsets.begin() &&
"first basic block not at offset 0") ? void (0) : __assert_fail
("I != BasicBlockOffsets.begin() && \"first basic block not at offset 0\""
, "bolt/lib/Core/BinaryFunction.cpp", 281, __extension__ __PRETTY_FUNCTION__
))
;
282 --I;
283 BinaryBasicBlock *BB = I->second;
284 return (Offset < BB->getOffset() + BB->getOriginalSize()) ? BB : nullptr;
285}
286
287void BinaryFunction::markUnreachableBlocks() {
288 std::stack<BinaryBasicBlock *> Stack;
289
290 for (BinaryBasicBlock &BB : blocks())
291 BB.markValid(false);
292
293 // Add all entries and landing pads as roots.
294 for (BinaryBasicBlock *BB : BasicBlocks) {
295 if (isEntryPoint(*BB) || BB->isLandingPad()) {
296 Stack.push(BB);
297 BB->markValid(true);
298 continue;
299 }
300 // FIXME:
301 // Also mark BBs with indirect jumps as reachable, since we do not
302 // support removing unused jump tables yet (GH-issue20).
303 for (const MCInst &Inst : *BB) {
304 if (BC.MIB->getJumpTable(Inst)) {
305 Stack.push(BB);
306 BB->markValid(true);
307 break;
308 }
309 }
310 }
311
312 // Determine reachable BBs from the entry point
313 while (!Stack.empty()) {
314 BinaryBasicBlock *BB = Stack.top();
315 Stack.pop();
316 for (BinaryBasicBlock *Succ : BB->successors()) {
317 if (Succ->isValid())
318 continue;
319 Succ->markValid(true);
320 Stack.push(Succ);
321 }
322 }
323}
324
325// Any unnecessary fallthrough jumps revealed after calling eraseInvalidBBs
326// will be cleaned up by fixBranches().
327std::pair<unsigned, uint64_t> BinaryFunction::eraseInvalidBBs() {
328 DenseSet<const BinaryBasicBlock *> InvalidBBs;
329 unsigned Count = 0;
330 uint64_t Bytes = 0;
331 for (BinaryBasicBlock *const BB : BasicBlocks) {
332 if (!BB->isValid()) {
333 assert(!isEntryPoint(*BB) && "all entry blocks must be valid")(static_cast <bool> (!isEntryPoint(*BB) && "all entry blocks must be valid"
) ? void (0) : __assert_fail ("!isEntryPoint(*BB) && \"all entry blocks must be valid\""
, "bolt/lib/Core/BinaryFunction.cpp", 333, __extension__ __PRETTY_FUNCTION__
))
;
334 InvalidBBs.insert(BB);
335 ++Count;
336 Bytes += BC.computeCodeSize(BB->begin(), BB->end());
337 }
338 }
339
340 Layout.eraseBasicBlocks(InvalidBBs);
341
342 BasicBlockListType NewBasicBlocks;
343 for (auto I = BasicBlocks.begin(), E = BasicBlocks.end(); I != E; ++I) {
344 BinaryBasicBlock *BB = *I;
345 if (InvalidBBs.contains(BB)) {
346 // Make sure the block is removed from the list of predecessors.
347 BB->removeAllSuccessors();
348 DeletedBasicBlocks.push_back(BB);
349 } else {
350 NewBasicBlocks.push_back(BB);
351 }
352 }
353 BasicBlocks = std::move(NewBasicBlocks);
354
355 assert(BasicBlocks.size() == Layout.block_size())(static_cast <bool> (BasicBlocks.size() == Layout.block_size
()) ? void (0) : __assert_fail ("BasicBlocks.size() == Layout.block_size()"
, "bolt/lib/Core/BinaryFunction.cpp", 355, __extension__ __PRETTY_FUNCTION__
))
;
356
357 // Update CFG state if needed
358 if (Count > 0)
359 recomputeLandingPads();
360
361 return std::make_pair(Count, Bytes);
362}
363
364bool BinaryFunction::isForwardCall(const MCSymbol *CalleeSymbol) const {
365 // This function should work properly before and after function reordering.
366 // In order to accomplish this, we use the function index (if it is valid).
367 // If the function indices are not valid, we fall back to the original
368 // addresses. This should be ok because the functions without valid indices
369 // should have been ordered with a stable sort.
370 const BinaryFunction *CalleeBF = BC.getFunctionForSymbol(CalleeSymbol);
371 if (CalleeBF) {
372 if (CalleeBF->isInjected())
373 return true;
374
375 if (hasValidIndex() && CalleeBF->hasValidIndex()) {
376 return getIndex() < CalleeBF->getIndex();
377 } else if (hasValidIndex() && !CalleeBF->hasValidIndex()) {
378 return true;
379 } else if (!hasValidIndex() && CalleeBF->hasValidIndex()) {
380 return false;
381 } else {
382 return getAddress() < CalleeBF->getAddress();
383 }
384 } else {
385 // Absolute symbol.
386 ErrorOr<uint64_t> CalleeAddressOrError = BC.getSymbolValue(*CalleeSymbol);
387 assert(CalleeAddressOrError && "unregistered symbol found")(static_cast <bool> (CalleeAddressOrError && "unregistered symbol found"
) ? void (0) : __assert_fail ("CalleeAddressOrError && \"unregistered symbol found\""
, "bolt/lib/Core/BinaryFunction.cpp", 387, __extension__ __PRETTY_FUNCTION__
))
;
388 return *CalleeAddressOrError > getAddress();
389 }
390}
391
392void BinaryFunction::dump() const {
393 // getDynoStats calls FunctionLayout::updateLayoutIndices and
394 // BasicBlock::analyzeBranch. The former cannot be const, but should be
395 // removed, the latter should be made const, but seems to require refactoring.
396 // Forcing all callers to have a non-const reference to BinaryFunction to call
397 // dump non-const however is not ideal either. Adding this const_cast is right
398 // now the best solution. It is safe, because BinaryFunction itself is not
399 // modified. Only BinaryBasicBlocks are actually modified (if it all) and we
400 // have mutable pointers to those regardless whether this function is
401 // const-qualified or not.
402 const_cast<BinaryFunction &>(*this).print(dbgs(), "");
403}
404
405void BinaryFunction::print(raw_ostream &OS, std::string Annotation) {
406 if (!opts::shouldPrint(*this))
407 return;
408
409 StringRef SectionName =
410 OriginSection ? OriginSection->getName() : "<no origin section>";
411 OS << "Binary Function \"" << *this << "\" " << Annotation << " {";
412 std::vector<StringRef> AllNames = getNames();
413 if (AllNames.size() > 1) {
414 OS << "\n All names : ";
415 const char *Sep = "";
416 for (const StringRef &Name : AllNames) {
417 OS << Sep << Name;
418 Sep = "\n ";
419 }
420 }
421 OS << "\n Number : " << FunctionNumber;
422 OS << "\n State : " << CurrentState;
423 OS << "\n Address : 0x" << Twine::utohexstr(Address);
424 OS << "\n Size : 0x" << Twine::utohexstr(Size);
425 OS << "\n MaxSize : 0x" << Twine::utohexstr(MaxSize);
426 OS << "\n Offset : 0x" << Twine::utohexstr(getFileOffset());
427 OS << "\n Section : " << SectionName;
428 OS << "\n Orc Section : " << getCodeSectionName();
429 OS << "\n LSDA : 0x" << Twine::utohexstr(getLSDAAddress());
430 OS << "\n IsSimple : " << IsSimple;
431 OS << "\n IsMultiEntry: " << isMultiEntry();
432 OS << "\n IsSplit : " << isSplit();
433 OS << "\n BB Count : " << size();
434
435 if (HasFixedIndirectBranch)
436 OS << "\n HasFixedIndirectBranch : true";
437 if (HasUnknownControlFlow)
438 OS << "\n Unknown CF : true";
439 if (getPersonalityFunction())
440 OS << "\n Personality : " << getPersonalityFunction()->getName();
441 if (IsFragment)
442 OS << "\n IsFragment : true";
443 if (isFolded())
444 OS << "\n FoldedInto : " << *getFoldedIntoFunction();
445 for (BinaryFunction *ParentFragment : ParentFragments)
446 OS << "\n Parent : " << *ParentFragment;
447 if (!Fragments.empty()) {
448 OS << "\n Fragments : ";
449 ListSeparator LS;
450 for (BinaryFunction *Frag : Fragments)
451 OS << LS << *Frag;
452 }
453 if (hasCFG())
454 OS << "\n Hash : " << Twine::utohexstr(computeHash());
455 if (isMultiEntry()) {
456 OS << "\n Secondary Entry Points : ";
457 ListSeparator LS;
458 for (const auto &KV : SecondaryEntryPoints)
459 OS << LS << KV.second->getName();
460 }
461 if (FrameInstructions.size())
462 OS << "\n CFI Instrs : " << FrameInstructions.size();
463 if (!Layout.block_empty()) {
464 OS << "\n BB Layout : ";
465 ListSeparator LS;
466 for (const BinaryBasicBlock *BB : Layout.blocks())
467 OS << LS << BB->getName();
468 }
469 if (getImageAddress())
470 OS << "\n Image : 0x" << Twine::utohexstr(getImageAddress());
471 if (ExecutionCount != COUNT_NO_PROFILE) {
472 OS << "\n Exec Count : " << ExecutionCount;
473 OS << "\n Profile Acc : " << format("%.1f%%", ProfileMatchRatio * 100.0f);
474 }
475
476 if (opts::PrintDynoStats && !getLayout().block_empty()) {
477 OS << '\n';
478 DynoStats dynoStats = getDynoStats(*this);
479 OS << dynoStats;
480 }
481
482 OS << "\n}\n";
483
484 if (opts::PrintDynoStatsOnly || !BC.InstPrinter)
485 return;
486
487 // Offset of the instruction in function.
488 uint64_t Offset = 0;
489
490 if (BasicBlocks.empty() && !Instructions.empty()) {
491 // Print before CFG was built.
492 for (const std::pair<const uint32_t, MCInst> &II : Instructions) {
493 Offset = II.first;
494
495 // Print label if exists at this offset.
496 auto LI = Labels.find(Offset);
497 if (LI != Labels.end()) {
498 if (const MCSymbol *EntrySymbol =
499 getSecondaryEntryPointSymbol(LI->second))
500 OS << EntrySymbol->getName() << " (Entry Point):\n";
501 OS << LI->second->getName() << ":\n";
502 }
503
504 BC.printInstruction(OS, II.second, Offset, this);
505 }
506 }
507
508 StringRef SplitPointMsg = "";
509 for (const FunctionFragment &FF : Layout.fragments()) {
510 OS << SplitPointMsg;
511 SplitPointMsg = "------- HOT-COLD SPLIT POINT -------\n\n";
512 for (const BinaryBasicBlock *BB : FF) {
513 OS << BB->getName() << " (" << BB->size()
514 << " instructions, align : " << BB->getAlignment() << ")\n";
515
516 if (isEntryPoint(*BB)) {
517 if (MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(*BB))
518 OS << " Secondary Entry Point: " << EntrySymbol->getName() << '\n';
519 else
520 OS << " Entry Point\n";
521 }
522
523 if (BB->isLandingPad())
524 OS << " Landing Pad\n";
525
526 uint64_t BBExecCount = BB->getExecutionCount();
527 if (hasValidProfile()) {
528 OS << " Exec Count : ";
529 if (BB->getExecutionCount() != BinaryBasicBlock::COUNT_NO_PROFILE)
530 OS << BBExecCount << '\n';
531 else
532 OS << "<unknown>\n";
533 }
534 if (BB->getCFIState() >= 0)
535 OS << " CFI State : " << BB->getCFIState() << '\n';
536 if (opts::EnableBAT) {
537 OS << " Input offset: " << Twine::utohexstr(BB->getInputOffset())
538 << "\n";
539 }
540 if (!BB->pred_empty()) {
541 OS << " Predecessors: ";
542 ListSeparator LS;
543 for (BinaryBasicBlock *Pred : BB->predecessors())
544 OS << LS << Pred->getName();
545 OS << '\n';
546 }
547 if (!BB->throw_empty()) {
548 OS << " Throwers: ";
549 ListSeparator LS;
550 for (BinaryBasicBlock *Throw : BB->throwers())
551 OS << LS << Throw->getName();
552 OS << '\n';
553 }
554
555 Offset = alignTo(Offset, BB->getAlignment());
556
557 // Note: offsets are imprecise since this is happening prior to
558 // relaxation.
559 Offset = BC.printInstructions(OS, BB->begin(), BB->end(), Offset, this);
560
561 if (!BB->succ_empty()) {
562 OS << " Successors: ";
563 // For more than 2 successors, sort them based on frequency.
564 std::vector<uint64_t> Indices(BB->succ_size());
565 std::iota(Indices.begin(), Indices.end(), 0);
566 if (BB->succ_size() > 2 && BB->getKnownExecutionCount()) {
567 llvm::stable_sort(Indices, [&](const uint64_t A, const uint64_t B) {
568 return BB->BranchInfo[B] < BB->BranchInfo[A];
569 });
570 }
571 ListSeparator LS;
572 for (unsigned I = 0; I < Indices.size(); ++I) {
573 BinaryBasicBlock *Succ = BB->Successors[Indices[I]];
574 const BinaryBasicBlock::BinaryBranchInfo &BI =
575 BB->BranchInfo[Indices[I]];
576 OS << LS << Succ->getName();
577 if (ExecutionCount != COUNT_NO_PROFILE &&
578 BI.MispredictedCount != BinaryBasicBlock::COUNT_INFERRED) {
579 OS << " (mispreds: " << BI.MispredictedCount
580 << ", count: " << BI.Count << ")";
581 } else if (ExecutionCount != COUNT_NO_PROFILE &&
582 BI.Count != BinaryBasicBlock::COUNT_NO_PROFILE) {
583 OS << " (inferred count: " << BI.Count << ")";
584 }
585 }
586 OS << '\n';
587 }
588
589 if (!BB->lp_empty()) {
590 OS << " Landing Pads: ";
591 ListSeparator LS;
592 for (BinaryBasicBlock *LP : BB->landing_pads()) {
593 OS << LS << LP->getName();
594 if (ExecutionCount != COUNT_NO_PROFILE) {
595 OS << " (count: " << LP->getExecutionCount() << ")";
596 }
597 }
598 OS << '\n';
599 }
600
601 // In CFG_Finalized state we can miscalculate CFI state at exit.
602 if (CurrentState == State::CFG) {
603 const int32_t CFIStateAtExit = BB->getCFIStateAtExit();
604 if (CFIStateAtExit >= 0)
605 OS << " CFI State: " << CFIStateAtExit << '\n';
606 }
607
608 OS << '\n';
609 }
610 }
611
612 // Dump new exception ranges for the function.
613 if (!CallSites.empty()) {
614 OS << "EH table:\n";
615 for (const FunctionFragment &FF : getLayout().fragments()) {
616 for (const auto &FCSI : getCallSites(FF.getFragmentNum())) {
617 const CallSite &CSI = FCSI.second;
618 OS << " [" << *CSI.Start << ", " << *CSI.End << ") landing pad : ";
619 if (CSI.LP)
620 OS << *CSI.LP;
621 else
622 OS << "0";
623 OS << ", action : " << CSI.Action << '\n';
624 }
625 }
626 OS << '\n';
627 }
628
629 // Print all jump tables.
630 for (const std::pair<const uint64_t, JumpTable *> &JTI : JumpTables)
631 JTI.second->print(OS);
632
633 OS << "DWARF CFI Instructions:\n";
634 if (OffsetToCFI.size()) {
635 // Pre-buildCFG information
636 for (const std::pair<const uint32_t, uint32_t> &Elmt : OffsetToCFI) {
637 OS << format(" %08x:\t", Elmt.first);
638 assert(Elmt.second < FrameInstructions.size() && "Incorrect CFI offset")(static_cast <bool> (Elmt.second < FrameInstructions
.size() && "Incorrect CFI offset") ? void (0) : __assert_fail
("Elmt.second < FrameInstructions.size() && \"Incorrect CFI offset\""
, "bolt/lib/Core/BinaryFunction.cpp", 638, __extension__ __PRETTY_FUNCTION__
))
;
639 BinaryContext::printCFI(OS, FrameInstructions[Elmt.second]);
640 OS << "\n";
641 }
642 } else {
643 // Post-buildCFG information
644 for (uint32_t I = 0, E = FrameInstructions.size(); I != E; ++I) {
645 const MCCFIInstruction &CFI = FrameInstructions[I];
646 OS << format(" %d:\t", I);
647 BinaryContext::printCFI(OS, CFI);
648 OS << "\n";
649 }
650 }
651 if (FrameInstructions.empty())
652 OS << " <empty>\n";
653
654 OS << "End of Function \"" << *this << "\"\n\n";
655}
656
657void BinaryFunction::printRelocations(raw_ostream &OS, uint64_t Offset,
658 uint64_t Size) const {
659 const char *Sep = " # Relocs: ";
660
661 auto RI = Relocations.lower_bound(Offset);
662 while (RI != Relocations.end() && RI->first < Offset + Size) {
663 OS << Sep << "(R: " << RI->second << ")";
664 Sep = ", ";
665 ++RI;
666 }
667}
668
669static std::string mutateDWARFExpressionTargetReg(const MCCFIInstruction &Instr,
670 MCPhysReg NewReg) {
671 StringRef ExprBytes = Instr.getValues();
672 assert(ExprBytes.size() > 1 && "DWARF expression CFI is too short")(static_cast <bool> (ExprBytes.size() > 1 &&
"DWARF expression CFI is too short") ? void (0) : __assert_fail
("ExprBytes.size() > 1 && \"DWARF expression CFI is too short\""
, "bolt/lib/Core/BinaryFunction.cpp", 672, __extension__ __PRETTY_FUNCTION__
))
;
673 uint8_t Opcode = ExprBytes[0];
674 assert((Opcode == dwarf::DW_CFA_expression ||(static_cast <bool> ((Opcode == dwarf::DW_CFA_expression
|| Opcode == dwarf::DW_CFA_val_expression) && "invalid DWARF expression CFI"
) ? void (0) : __assert_fail ("(Opcode == dwarf::DW_CFA_expression || Opcode == dwarf::DW_CFA_val_expression) && \"invalid DWARF expression CFI\""
, "bolt/lib/Core/BinaryFunction.cpp", 676, __extension__ __PRETTY_FUNCTION__
))
675 Opcode == dwarf::DW_CFA_val_expression) &&(static_cast <bool> ((Opcode == dwarf::DW_CFA_expression
|| Opcode == dwarf::DW_CFA_val_expression) && "invalid DWARF expression CFI"
) ? void (0) : __assert_fail ("(Opcode == dwarf::DW_CFA_expression || Opcode == dwarf::DW_CFA_val_expression) && \"invalid DWARF expression CFI\""
, "bolt/lib/Core/BinaryFunction.cpp", 676, __extension__ __PRETTY_FUNCTION__
))
676 "invalid DWARF expression CFI")(static_cast <bool> ((Opcode == dwarf::DW_CFA_expression
|| Opcode == dwarf::DW_CFA_val_expression) && "invalid DWARF expression CFI"
) ? void (0) : __assert_fail ("(Opcode == dwarf::DW_CFA_expression || Opcode == dwarf::DW_CFA_val_expression) && \"invalid DWARF expression CFI\""
, "bolt/lib/Core/BinaryFunction.cpp", 676, __extension__ __PRETTY_FUNCTION__
))
;
677 (void)Opcode;
678 const uint8_t *const Start =
679 reinterpret_cast<const uint8_t *>(ExprBytes.drop_front(1).data());
680 const uint8_t *const End =
681 reinterpret_cast<const uint8_t *>(Start + ExprBytes.size() - 1);
682 unsigned Size = 0;
683 decodeULEB128(Start, &Size, End);
684 assert(Size > 0 && "Invalid reg encoding for DWARF expression CFI")(static_cast <bool> (Size > 0 && "Invalid reg encoding for DWARF expression CFI"
) ? void (0) : __assert_fail ("Size > 0 && \"Invalid reg encoding for DWARF expression CFI\""
, "bolt/lib/Core/BinaryFunction.cpp", 684, __extension__ __PRETTY_FUNCTION__
))
;
685 SmallString<8> Tmp;
686 raw_svector_ostream OSE(Tmp);
687 encodeULEB128(NewReg, OSE);
688 return Twine(ExprBytes.slice(0, 1))
689 .concat(OSE.str())
690 .concat(ExprBytes.drop_front(1 + Size))
691 .str();
692}
693
694void BinaryFunction::mutateCFIRegisterFor(const MCInst &Instr,
695 MCPhysReg NewReg) {
696 const MCCFIInstruction *OldCFI = getCFIFor(Instr);
697 assert(OldCFI && "invalid CFI instr")(static_cast <bool> (OldCFI && "invalid CFI instr"
) ? void (0) : __assert_fail ("OldCFI && \"invalid CFI instr\""
, "bolt/lib/Core/BinaryFunction.cpp", 697, __extension__ __PRETTY_FUNCTION__
))
;
698 switch (OldCFI->getOperation()) {
699 default:
700 llvm_unreachable("Unexpected instruction")::llvm::llvm_unreachable_internal("Unexpected instruction", "bolt/lib/Core/BinaryFunction.cpp"
, 700)
;
701 case MCCFIInstruction::OpDefCfa:
702 setCFIFor(Instr, MCCFIInstruction::cfiDefCfa(nullptr, NewReg,
703 OldCFI->getOffset()));
704 break;
705 case MCCFIInstruction::OpDefCfaRegister:
706 setCFIFor(Instr, MCCFIInstruction::createDefCfaRegister(nullptr, NewReg));
707 break;
708 case MCCFIInstruction::OpOffset:
709 setCFIFor(Instr, MCCFIInstruction::createOffset(nullptr, NewReg,
710 OldCFI->getOffset()));
711 break;
712 case MCCFIInstruction::OpRegister:
713 setCFIFor(Instr, MCCFIInstruction::createRegister(nullptr, NewReg,
714 OldCFI->getRegister2()));
715 break;
716 case MCCFIInstruction::OpSameValue:
717 setCFIFor(Instr, MCCFIInstruction::createSameValue(nullptr, NewReg));
718 break;
719 case MCCFIInstruction::OpEscape:
720 setCFIFor(Instr,
721 MCCFIInstruction::createEscape(
722 nullptr,
723 StringRef(mutateDWARFExpressionTargetReg(*OldCFI, NewReg))));
724 break;
725 case MCCFIInstruction::OpRestore:
726 setCFIFor(Instr, MCCFIInstruction::createRestore(nullptr, NewReg));
727 break;
728 case MCCFIInstruction::OpUndefined:
729 setCFIFor(Instr, MCCFIInstruction::createUndefined(nullptr, NewReg));
730 break;
731 }
732}
733
734const MCCFIInstruction *BinaryFunction::mutateCFIOffsetFor(const MCInst &Instr,
735 int64_t NewOffset) {
736 const MCCFIInstruction *OldCFI = getCFIFor(Instr);
737 assert(OldCFI && "invalid CFI instr")(static_cast <bool> (OldCFI && "invalid CFI instr"
) ? void (0) : __assert_fail ("OldCFI && \"invalid CFI instr\""
, "bolt/lib/Core/BinaryFunction.cpp", 737, __extension__ __PRETTY_FUNCTION__
))
;
738 switch (OldCFI->getOperation()) {
739 default:
740 llvm_unreachable("Unexpected instruction")::llvm::llvm_unreachable_internal("Unexpected instruction", "bolt/lib/Core/BinaryFunction.cpp"
, 740)
;
741 case MCCFIInstruction::OpDefCfaOffset:
742 setCFIFor(Instr, MCCFIInstruction::cfiDefCfaOffset(nullptr, NewOffset));
743 break;
744 case MCCFIInstruction::OpAdjustCfaOffset:
745 setCFIFor(Instr,
746 MCCFIInstruction::createAdjustCfaOffset(nullptr, NewOffset));
747 break;
748 case MCCFIInstruction::OpDefCfa:
749 setCFIFor(Instr, MCCFIInstruction::cfiDefCfa(nullptr, OldCFI->getRegister(),
750 NewOffset));
751 break;
752 case MCCFIInstruction::OpOffset:
753 setCFIFor(Instr, MCCFIInstruction::createOffset(
754 nullptr, OldCFI->getRegister(), NewOffset));
755 break;
756 }
757 return getCFIFor(Instr);
758}
759
760IndirectBranchType
761BinaryFunction::processIndirectBranch(MCInst &Instruction, unsigned Size,
762 uint64_t Offset,
763 uint64_t &TargetAddress) {
764 const unsigned PtrSize = BC.AsmInfo->getCodePointerSize();
765
766 // The instruction referencing memory used by the branch instruction.
767 // It could be the branch instruction itself or one of the instructions
768 // setting the value of the register used by the branch.
769 MCInst *MemLocInstr;
770
771 // Address of the table referenced by MemLocInstr. Could be either an
772 // array of function pointers, or a jump table.
773 uint64_t ArrayStart = 0;
774
775 unsigned BaseRegNum, IndexRegNum;
776 int64_t DispValue;
777 const MCExpr *DispExpr;
778
779 // In AArch, identify the instruction adding the PC-relative offset to
780 // jump table entries to correctly decode it.
781 MCInst *PCRelBaseInstr;
782 uint64_t PCRelAddr = 0;
783
784 auto Begin = Instructions.begin();
785 if (BC.isAArch64()) {
786 PreserveNops = BC.HasRelocations;
787 // Start at the last label as an approximation of the current basic block.
788 // This is a heuristic, since the full set of labels have yet to be
789 // determined
790 for (const uint32_t Offset :
791 llvm::make_first_range(llvm::reverse(Labels))) {
792 auto II = Instructions.find(Offset);
793 if (II != Instructions.end()) {
794 Begin = II;
795 break;
796 }
797 }
798 }
799
800 IndirectBranchType BranchType = BC.MIB->analyzeIndirectBranch(
801 Instruction, Begin, Instructions.end(), PtrSize, MemLocInstr, BaseRegNum,
802 IndexRegNum, DispValue, DispExpr, PCRelBaseInstr);
803
804 if (BranchType == IndirectBranchType::UNKNOWN && !MemLocInstr)
805 return BranchType;
806
807 if (MemLocInstr != &Instruction)
808 IndexRegNum = BC.MIB->getNoRegister();
809
810 if (BC.isAArch64()) {
811 const MCSymbol *Sym = BC.MIB->getTargetSymbol(*PCRelBaseInstr, 1);
812 assert(Sym && "Symbol extraction failed")(static_cast <bool> (Sym && "Symbol extraction failed"
) ? void (0) : __assert_fail ("Sym && \"Symbol extraction failed\""
, "bolt/lib/Core/BinaryFunction.cpp", 812, __extension__ __PRETTY_FUNCTION__
))
;
813 ErrorOr<uint64_t> SymValueOrError = BC.getSymbolValue(*Sym);
814 if (SymValueOrError) {
815 PCRelAddr = *SymValueOrError;
816 } else {
817 for (std::pair<const uint32_t, MCSymbol *> &Elmt : Labels) {
818 if (Elmt.second == Sym) {
819 PCRelAddr = Elmt.first + getAddress();
820 break;
821 }
822 }
823 }
824 uint64_t InstrAddr = 0;
825 for (auto II = Instructions.rbegin(); II != Instructions.rend(); ++II) {
826 if (&II->second == PCRelBaseInstr) {
827 InstrAddr = II->first + getAddress();
828 break;
829 }
830 }
831 assert(InstrAddr != 0 && "instruction not found")(static_cast <bool> (InstrAddr != 0 && "instruction not found"
) ? void (0) : __assert_fail ("InstrAddr != 0 && \"instruction not found\""
, "bolt/lib/Core/BinaryFunction.cpp", 831, __extension__ __PRETTY_FUNCTION__
))
;
832 // We do this to avoid spurious references to code locations outside this
833 // function (for example, if the indirect jump lives in the last basic
834 // block of the function, it will create a reference to the next function).
835 // This replaces a symbol reference with an immediate.
836 BC.MIB->replaceMemOperandDisp(*PCRelBaseInstr,
837 MCOperand::createImm(PCRelAddr - InstrAddr));
838 // FIXME: Disable full jump table processing for AArch64 until we have a
839 // proper way of determining the jump table limits.
840 return IndirectBranchType::UNKNOWN;
841 }
842
843 // RIP-relative addressing should be converted to symbol form by now
844 // in processed instructions (but not in jump).
845 if (DispExpr) {
846 const MCSymbol *TargetSym;
847 uint64_t TargetOffset;
848 std::tie(TargetSym, TargetOffset) = BC.MIB->getTargetSymbolInfo(DispExpr);
849 ErrorOr<uint64_t> SymValueOrError = BC.getSymbolValue(*TargetSym);
850 assert(SymValueOrError && "global symbol needs a value")(static_cast <bool> (SymValueOrError && "global symbol needs a value"
) ? void (0) : __assert_fail ("SymValueOrError && \"global symbol needs a value\""
, "bolt/lib/Core/BinaryFunction.cpp", 850, __extension__ __PRETTY_FUNCTION__
))
;
851 ArrayStart = *SymValueOrError + TargetOffset;
852 BaseRegNum = BC.MIB->getNoRegister();
853 if (BC.isAArch64()) {
854 ArrayStart &= ~0xFFFULL;
855 ArrayStart += DispValue & 0xFFFULL;
856 }
857 } else {
858 ArrayStart = static_cast<uint64_t>(DispValue);
859 }
860
861 if (BaseRegNum == BC.MRI->getProgramCounter())
862 ArrayStart += getAddress() + Offset + Size;
863
864 LLVM_DEBUG(dbgs() << "BOLT-DEBUG: addressed memory is 0x"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: addressed memory is 0x"
<< Twine::utohexstr(ArrayStart) << '\n'; } } while
(false)
865 << Twine::utohexstr(ArrayStart) << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: addressed memory is 0x"
<< Twine::utohexstr(ArrayStart) << '\n'; } } while
(false)
;
866
867 ErrorOr<BinarySection &> Section = BC.getSectionForAddress(ArrayStart);
868 if (!Section) {
869 // No section - possibly an absolute address. Since we don't allow
870 // internal function addresses to escape the function scope - we
871 // consider it a tail call.
872 if (opts::Verbosity >= 1) {
873 errs() << "BOLT-WARNING: no section for address 0x"
874 << Twine::utohexstr(ArrayStart) << " referenced from function "
875 << *this << '\n';
876 }
877 return IndirectBranchType::POSSIBLE_TAIL_CALL;
878 }
879 if (Section->isVirtual()) {
880 // The contents are filled at runtime.
881 return IndirectBranchType::POSSIBLE_TAIL_CALL;
882 }
883
884 if (BranchType == IndirectBranchType::POSSIBLE_FIXED_BRANCH) {
885 ErrorOr<uint64_t> Value = BC.getPointerAtAddress(ArrayStart);
886 if (!Value)
887 return IndirectBranchType::UNKNOWN;
888
889 if (BC.getSectionForAddress(ArrayStart)->isWritable())
890 return IndirectBranchType::UNKNOWN;
891
892 outs() << "BOLT-INFO: fixed indirect branch detected in " << *this
893 << " at 0x" << Twine::utohexstr(getAddress() + Offset)
894 << " referencing data at 0x" << Twine::utohexstr(ArrayStart)
895 << " the destination value is 0x" << Twine::utohexstr(*Value)
896 << '\n';
897
898 TargetAddress = *Value;
899 return BranchType;
900 }
901
902 // Check if there's already a jump table registered at this address.
903 MemoryContentsType MemType;
904 if (JumpTable *JT = BC.getJumpTableContainingAddress(ArrayStart)) {
905 switch (JT->Type) {
906 case JumpTable::JTT_NORMAL:
907 MemType = MemoryContentsType::POSSIBLE_JUMP_TABLE;
908 break;
909 case JumpTable::JTT_PIC:
910 MemType = MemoryContentsType::POSSIBLE_PIC_JUMP_TABLE;
911 break;
912 }
913 } else {
914 MemType = BC.analyzeMemoryAt(ArrayStart, *this);
915 }
916
917 // Check that jump table type in instruction pattern matches memory contents.
918 JumpTable::JumpTableType JTType;
919 if (BranchType == IndirectBranchType::POSSIBLE_PIC_JUMP_TABLE) {
920 if (MemType != MemoryContentsType::POSSIBLE_PIC_JUMP_TABLE)
921 return IndirectBranchType::UNKNOWN;
922 JTType = JumpTable::JTT_PIC;
923 } else {
924 if (MemType == MemoryContentsType::POSSIBLE_PIC_JUMP_TABLE)
925 return IndirectBranchType::UNKNOWN;
926
927 if (MemType == MemoryContentsType::UNKNOWN)
928 return IndirectBranchType::POSSIBLE_TAIL_CALL;
929
930 BranchType = IndirectBranchType::POSSIBLE_JUMP_TABLE;
931 JTType = JumpTable::JTT_NORMAL;
932 }
933
934 // Convert the instruction into jump table branch.
935 const MCSymbol *JTLabel = BC.getOrCreateJumpTable(*this, ArrayStart, JTType);
936 BC.MIB->replaceMemOperandDisp(*MemLocInstr, JTLabel, BC.Ctx.get());
937 BC.MIB->setJumpTable(Instruction, ArrayStart, IndexRegNum);
938
939 JTSites.emplace_back(Offset, ArrayStart);
940
941 return BranchType;
942}
943
944MCSymbol *BinaryFunction::getOrCreateLocalLabel(uint64_t Address,
945 bool CreatePastEnd) {
946 const uint64_t Offset = Address - getAddress();
947
948 if ((Offset == getSize()) && CreatePastEnd)
949 return getFunctionEndLabel();
950
951 auto LI = Labels.find(Offset);
952 if (LI != Labels.end())
953 return LI->second;
954
955 // For AArch64, check if this address is part of a constant island.
956 if (BC.isAArch64()) {
957 if (MCSymbol *IslandSym = getOrCreateIslandAccess(Address))
958 return IslandSym;
959 }
960
961 MCSymbol *Label = BC.Ctx->createNamedTempSymbol();
962 Labels[Offset] = Label;
963
964 return Label;
965}
966
967ErrorOr<ArrayRef<uint8_t>> BinaryFunction::getData() const {
968 BinarySection &Section = *getOriginSection();
969 assert(Section.containsRange(getAddress(), getMaxSize()) &&(static_cast <bool> (Section.containsRange(getAddress()
, getMaxSize()) && "wrong section for function") ? void
(0) : __assert_fail ("Section.containsRange(getAddress(), getMaxSize()) && \"wrong section for function\""
, "bolt/lib/Core/BinaryFunction.cpp", 970, __extension__ __PRETTY_FUNCTION__
))
970 "wrong section for function")(static_cast <bool> (Section.containsRange(getAddress()
, getMaxSize()) && "wrong section for function") ? void
(0) : __assert_fail ("Section.containsRange(getAddress(), getMaxSize()) && \"wrong section for function\""
, "bolt/lib/Core/BinaryFunction.cpp", 970, __extension__ __PRETTY_FUNCTION__
))
;
971
972 if (!Section.isText() || Section.isVirtual() || !Section.getSize())
973 return std::make_error_code(std::errc::bad_address);
974
975 StringRef SectionContents = Section.getContents();
976
977 assert(SectionContents.size() == Section.getSize() &&(static_cast <bool> (SectionContents.size() == Section.
getSize() && "section size mismatch") ? void (0) : __assert_fail
("SectionContents.size() == Section.getSize() && \"section size mismatch\""
, "bolt/lib/Core/BinaryFunction.cpp", 978, __extension__ __PRETTY_FUNCTION__
))
978 "section size mismatch")(static_cast <bool> (SectionContents.size() == Section.
getSize() && "section size mismatch") ? void (0) : __assert_fail
("SectionContents.size() == Section.getSize() && \"section size mismatch\""
, "bolt/lib/Core/BinaryFunction.cpp", 978, __extension__ __PRETTY_FUNCTION__
))
;
979
980 // Function offset from the section start.
981 uint64_t Offset = getAddress() - Section.getAddress();
982 auto *Bytes = reinterpret_cast<const uint8_t *>(SectionContents.data());
983 return ArrayRef<uint8_t>(Bytes + Offset, getMaxSize());
984}
985
986size_t BinaryFunction::getSizeOfDataInCodeAt(uint64_t Offset) const {
987 if (!Islands)
988 return 0;
989
990 if (Islands->DataOffsets.find(Offset) == Islands->DataOffsets.end())
991 return 0;
992
993 auto Iter = Islands->CodeOffsets.upper_bound(Offset);
994 if (Iter != Islands->CodeOffsets.end())
995 return *Iter - Offset;
996 return getSize() - Offset;
997}
998
999bool BinaryFunction::isZeroPaddingAt(uint64_t Offset) const {
1000 ArrayRef<uint8_t> FunctionData = *getData();
1001 uint64_t EndOfCode = getSize();
1002 if (Islands) {
1003 auto Iter = Islands->DataOffsets.upper_bound(Offset);
1004 if (Iter != Islands->DataOffsets.end())
1005 EndOfCode = *Iter;
1006 }
1007 for (uint64_t I = Offset; I < EndOfCode; ++I)
1008 if (FunctionData[I] != 0)
1009 return false;
1010
1011 return true;
1012}
1013
1014void BinaryFunction::handlePCRelOperand(MCInst &Instruction, uint64_t Address,
1015 uint64_t Size) {
1016 auto &MIB = BC.MIB;
1017 uint64_t TargetAddress = 0;
1018 if (!MIB->evaluateMemOperandTarget(Instruction, TargetAddress, Address,
1019 Size)) {
1020 errs() << "BOLT-ERROR: PC-relative operand can't be evaluated:\n";
1021 BC.InstPrinter->printInst(&Instruction, 0, "", *BC.STI, errs());
1022 errs() << '\n';
1023 Instruction.dump_pretty(errs(), BC.InstPrinter.get());
1024 errs() << '\n';
1025 errs() << "BOLT-ERROR: cannot handle PC-relative operand at 0x"
1026 << Twine::utohexstr(Address) << ". Skipping function " << *this
1027 << ".\n";
1028 if (BC.HasRelocations)
1029 exit(1);
1030 IsSimple = false;
1031 return;
1032 }
1033 if (TargetAddress == 0 && opts::Verbosity >= 1) {
1034 outs() << "BOLT-INFO: PC-relative operand is zero in function " << *this
1035 << '\n';
1036 }
1037
1038 const MCSymbol *TargetSymbol;
1039 uint64_t TargetOffset;
1040 std::tie(TargetSymbol, TargetOffset) =
1041 BC.handleAddressRef(TargetAddress, *this, /*IsPCRel*/ true);
1042
1043 bool ReplaceSuccess = MIB->replaceMemOperandDisp(
1044 Instruction, TargetSymbol, static_cast<int64_t>(TargetOffset), &*BC.Ctx);
1045 (void)ReplaceSuccess;
1046 assert(ReplaceSuccess && "Failed to replace mem operand with symbol+off.")(static_cast <bool> (ReplaceSuccess && "Failed to replace mem operand with symbol+off."
) ? void (0) : __assert_fail ("ReplaceSuccess && \"Failed to replace mem operand with symbol+off.\""
, "bolt/lib/Core/BinaryFunction.cpp", 1046, __extension__ __PRETTY_FUNCTION__
))
;
1047}
1048
1049MCSymbol *BinaryFunction::handleExternalReference(MCInst &Instruction,
1050 uint64_t Size,
1051 uint64_t Offset,
1052 uint64_t TargetAddress,
1053 bool &IsCall) {
1054 auto &MIB = BC.MIB;
1055
1056 const uint64_t AbsoluteInstrAddr = getAddress() + Offset;
1057 BC.addInterproceduralReference(this, TargetAddress);
1058 if (opts::Verbosity >= 2 && !IsCall && Size == 2 && !BC.HasRelocations) {
1059 errs() << "BOLT-WARNING: relaxed tail call detected at 0x"
1060 << Twine::utohexstr(AbsoluteInstrAddr) << " in function " << *this
1061 << ". Code size will be increased.\n";
1062 }
1063
1064 assert(!MIB->isTailCall(Instruction) &&(static_cast <bool> (!MIB->isTailCall(Instruction) &&
"synthetic tail call instruction found") ? void (0) : __assert_fail
("!MIB->isTailCall(Instruction) && \"synthetic tail call instruction found\""
, "bolt/lib/Core/BinaryFunction.cpp", 1065, __extension__ __PRETTY_FUNCTION__
))
1065 "synthetic tail call instruction found")(static_cast <bool> (!MIB->isTailCall(Instruction) &&
"synthetic tail call instruction found") ? void (0) : __assert_fail
("!MIB->isTailCall(Instruction) && \"synthetic tail call instruction found\""
, "bolt/lib/Core/BinaryFunction.cpp", 1065, __extension__ __PRETTY_FUNCTION__
))
;
1066
1067 // This is a call regardless of the opcode.
1068 // Assign proper opcode for tail calls, so that they could be
1069 // treated as calls.
1070 if (!IsCall) {
1071 if (!MIB->convertJmpToTailCall(Instruction)) {
1072 assert(MIB->isConditionalBranch(Instruction) &&(static_cast <bool> (MIB->isConditionalBranch(Instruction
) && "unknown tail call instruction") ? void (0) : __assert_fail
("MIB->isConditionalBranch(Instruction) && \"unknown tail call instruction\""
, "bolt/lib/Core/BinaryFunction.cpp", 1073, __extension__ __PRETTY_FUNCTION__
))
1073 "unknown tail call instruction")(static_cast <bool> (MIB->isConditionalBranch(Instruction
) && "unknown tail call instruction") ? void (0) : __assert_fail
("MIB->isConditionalBranch(Instruction) && \"unknown tail call instruction\""
, "bolt/lib/Core/BinaryFunction.cpp", 1073, __extension__ __PRETTY_FUNCTION__
))
;
1074 if (opts::Verbosity >= 2) {
1075 errs() << "BOLT-WARNING: conditional tail call detected in "
1076 << "function " << *this << " at 0x"
1077 << Twine::utohexstr(AbsoluteInstrAddr) << ".\n";
1078 }
1079 }
1080 IsCall = true;
1081 }
1082
1083 if (opts::Verbosity >= 2 && TargetAddress == 0) {
1084 // We actually see calls to address 0 in presence of weak
1085 // symbols originating from libraries. This code is never meant
1086 // to be executed.
1087 outs() << "BOLT-INFO: Function " << *this
1088 << " has a call to address zero.\n";
1089 }
1090
1091 return BC.getOrCreateGlobalSymbol(TargetAddress, "FUNCat");
1092}
1093
1094void BinaryFunction::handleIndirectBranch(MCInst &Instruction, uint64_t Size,
1095 uint64_t Offset) {
1096 auto &MIB = BC.MIB;
1097 uint64_t IndirectTarget = 0;
1098 IndirectBranchType Result =
1099 processIndirectBranch(Instruction, Size, Offset, IndirectTarget);
1100 switch (Result) {
1101 default:
1102 llvm_unreachable("unexpected result")::llvm::llvm_unreachable_internal("unexpected result", "bolt/lib/Core/BinaryFunction.cpp"
, 1102)
;
1103 case IndirectBranchType::POSSIBLE_TAIL_CALL: {
1104 bool Result = MIB->convertJmpToTailCall(Instruction);
1105 (void)Result;
1106 assert(Result)(static_cast <bool> (Result) ? void (0) : __assert_fail
("Result", "bolt/lib/Core/BinaryFunction.cpp", 1106, __extension__
__PRETTY_FUNCTION__))
;
1107 break;
1108 }
1109 case IndirectBranchType::POSSIBLE_JUMP_TABLE:
1110 case IndirectBranchType::POSSIBLE_PIC_JUMP_TABLE:
1111 if (opts::JumpTables == JTS_NONE)
1112 IsSimple = false;
1113 break;
1114 case IndirectBranchType::POSSIBLE_FIXED_BRANCH: {
1115 if (containsAddress(IndirectTarget)) {
1116 const MCSymbol *TargetSymbol = getOrCreateLocalLabel(IndirectTarget);
1117 Instruction.clear();
1118 MIB->createUncondBranch(Instruction, TargetSymbol, BC.Ctx.get());
1119 TakenBranches.emplace_back(Offset, IndirectTarget - getAddress());
1120 HasFixedIndirectBranch = true;
1121 } else {
1122 MIB->convertJmpToTailCall(Instruction);
1123 BC.addInterproceduralReference(this, IndirectTarget);
1124 }
1125 break;
1126 }
1127 case IndirectBranchType::UNKNOWN:
1128 // Keep processing. We'll do more checks and fixes in
1129 // postProcessIndirectBranches().
1130 UnknownIndirectBranchOffsets.emplace(Offset);
1131 break;
1132 }
1133}
1134
1135void BinaryFunction::handleAArch64IndirectCall(MCInst &Instruction,
1136 const uint64_t Offset) {
1137 auto &MIB = BC.MIB;
1138 const uint64_t AbsoluteInstrAddr = getAddress() + Offset;
1139 MCInst *TargetHiBits, *TargetLowBits;
1140 uint64_t TargetAddress, Count;
1141 Count = MIB->matchLinkerVeneer(Instructions.begin(), Instructions.end(),
1142 AbsoluteInstrAddr, Instruction, TargetHiBits,
1143 TargetLowBits, TargetAddress);
1144 if (Count) {
1145 MIB->addAnnotation(Instruction, "AArch64Veneer", true);
1146 --Count;
1147 for (auto It = std::prev(Instructions.end()); Count != 0;
1148 It = std::prev(It), --Count) {
1149 MIB->addAnnotation(It->second, "AArch64Veneer", true);
1150 }
1151
1152 BC.addAdrpAddRelocAArch64(*this, *TargetLowBits, *TargetHiBits,
1153 TargetAddress);
1154 }
1155}
1156
1157bool BinaryFunction::disassemble() {
1158 NamedRegionTimer T("disassemble", "Disassemble function", "buildfuncs",
1159 "Build Binary Functions", opts::TimeBuild);
1160 ErrorOr<ArrayRef<uint8_t>> ErrorOrFunctionData = getData();
1161 assert(ErrorOrFunctionData && "function data is not available")(static_cast <bool> (ErrorOrFunctionData && "function data is not available"
) ? void (0) : __assert_fail ("ErrorOrFunctionData && \"function data is not available\""
, "bolt/lib/Core/BinaryFunction.cpp", 1161, __extension__ __PRETTY_FUNCTION__
))
;
1162 ArrayRef<uint8_t> FunctionData = *ErrorOrFunctionData;
1163 assert(FunctionData.size() == getMaxSize() &&(static_cast <bool> (FunctionData.size() == getMaxSize(
) && "function size does not match raw data size") ? void
(0) : __assert_fail ("FunctionData.size() == getMaxSize() && \"function size does not match raw data size\""
, "bolt/lib/Core/BinaryFunction.cpp", 1164, __extension__ __PRETTY_FUNCTION__
))
1164 "function size does not match raw data size")(static_cast <bool> (FunctionData.size() == getMaxSize(
) && "function size does not match raw data size") ? void
(0) : __assert_fail ("FunctionData.size() == getMaxSize() && \"function size does not match raw data size\""
, "bolt/lib/Core/BinaryFunction.cpp", 1164, __extension__ __PRETTY_FUNCTION__
))
;
1165
1166 auto &Ctx = BC.Ctx;
1167 auto &MIB = BC.MIB;
1168
1169 BC.SymbolicDisAsm->setSymbolizer(MIB->createTargetSymbolizer(*this));
1170
1171 // Insert a label at the beginning of the function. This will be our first
1172 // basic block.
1173 Labels[0] = Ctx->createNamedTempSymbol("BB0");
1174
1175 uint64_t Size = 0; // instruction size
1176 for (uint64_t Offset = 0; Offset < getSize(); Offset += Size) {
1177 MCInst Instruction;
1178 const uint64_t AbsoluteInstrAddr = getAddress() + Offset;
1179
1180 // Check for data inside code and ignore it
1181 if (const size_t DataInCodeSize = getSizeOfDataInCodeAt(Offset)) {
1182 Size = DataInCodeSize;
1183 continue;
1184 }
1185
1186 if (!BC.SymbolicDisAsm->getInstruction(Instruction, Size,
1187 FunctionData.slice(Offset),
1188 AbsoluteInstrAddr, nulls())) {
1189 // Functions with "soft" boundaries, e.g. coming from assembly source,
1190 // can have 0-byte padding at the end.
1191 if (isZeroPaddingAt(Offset))
1192 break;
1193
1194 errs() << "BOLT-WARNING: unable to disassemble instruction at offset 0x"
1195 << Twine::utohexstr(Offset) << " (address 0x"
1196 << Twine::utohexstr(AbsoluteInstrAddr) << ") in function " << *this
1197 << '\n';
1198 // Some AVX-512 instructions could not be disassembled at all.
1199 if (BC.HasRelocations && opts::TrapOnAVX512 && BC.isX86()) {
1200 setTrapOnEntry();
1201 BC.TrappedFunctions.push_back(this);
1202 } else {
1203 setIgnored();
1204 }
1205
1206 break;
1207 }
1208
1209 // Check integrity of LLVM assembler/disassembler.
1210 if (opts::CheckEncoding && !BC.MIB->isBranch(Instruction) &&
1211 !BC.MIB->isCall(Instruction) && !BC.MIB->isNoop(Instruction)) {
1212 if (!BC.validateInstructionEncoding(FunctionData.slice(Offset, Size))) {
1213 errs() << "BOLT-WARNING: mismatching LLVM encoding detected in "
1214 << "function " << *this << " for instruction :\n";
1215 BC.printInstruction(errs(), Instruction, AbsoluteInstrAddr);
1216 errs() << '\n';
1217 }
1218 }
1219
1220 // Special handling for AVX-512 instructions.
1221 if (MIB->hasEVEXEncoding(Instruction)) {
1222 if (BC.HasRelocations && opts::TrapOnAVX512) {
1223 setTrapOnEntry();
1224 BC.TrappedFunctions.push_back(this);
1225 break;
1226 }
1227
1228 if (!BC.validateInstructionEncoding(FunctionData.slice(Offset, Size))) {
1229 errs() << "BOLT-WARNING: internal assembler/disassembler error "
1230 "detected for AVX512 instruction:\n";
1231 BC.printInstruction(errs(), Instruction, AbsoluteInstrAddr);
1232 errs() << " in function " << *this << '\n';
1233 setIgnored();
1234 break;
1235 }
1236 }
1237
1238 if (MIB->isBranch(Instruction) || MIB->isCall(Instruction)) {
1239 uint64_t TargetAddress = 0;
1240 if (MIB->evaluateBranch(Instruction, AbsoluteInstrAddr, Size,
1241 TargetAddress)) {
1242 // Check if the target is within the same function. Otherwise it's
1243 // a call, possibly a tail call.
1244 //
1245 // If the target *is* the function address it could be either a branch
1246 // or a recursive call.
1247 bool IsCall = MIB->isCall(Instruction);
1248 const bool IsCondBranch = MIB->isConditionalBranch(Instruction);
1249 MCSymbol *TargetSymbol = nullptr;
1250
1251 if (BC.MIB->isUnsupportedBranch(Instruction.getOpcode())) {
1252 setIgnored();
1253 if (BinaryFunction *TargetFunc =
1254 BC.getBinaryFunctionContainingAddress(TargetAddress))
1255 TargetFunc->setIgnored();
1256 }
1257
1258 if (IsCall && containsAddress(TargetAddress)) {
1259 if (TargetAddress == getAddress()) {
1260 // Recursive call.
1261 TargetSymbol = getSymbol();
1262 } else {
1263 if (BC.isX86()) {
1264 // Dangerous old-style x86 PIC code. We may need to freeze this
1265 // function, so preserve the function as is for now.
1266 PreserveNops = true;
1267 } else {
1268 errs() << "BOLT-WARNING: internal call detected at 0x"
1269 << Twine::utohexstr(AbsoluteInstrAddr) << " in function "
1270 << *this << ". Skipping.\n";
1271 IsSimple = false;
1272 }
1273 }
1274 }
1275
1276 if (!TargetSymbol) {
1277 // Create either local label or external symbol.
1278 if (containsAddress(TargetAddress)) {
1279 TargetSymbol = getOrCreateLocalLabel(TargetAddress);
1280 } else {
1281 if (TargetAddress == getAddress() + getSize() &&
1282 TargetAddress < getAddress() + getMaxSize() &&
1283 !(BC.isAArch64() &&
1284 BC.handleAArch64Veneer(TargetAddress, /*MatchOnly*/ true))) {
1285 // Result of __builtin_unreachable().
1286 LLVM_DEBUG(dbgs() << "BOLT-DEBUG: jump past end detected at 0x"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: jump past end detected at 0x"
<< Twine::utohexstr(AbsoluteInstrAddr) << " in function "
<< *this << " : replacing with nop.\n"; } } while
(false)
1287 << Twine::utohexstr(AbsoluteInstrAddr)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: jump past end detected at 0x"
<< Twine::utohexstr(AbsoluteInstrAddr) << " in function "
<< *this << " : replacing with nop.\n"; } } while
(false)
1288 << " in function " << *thisdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: jump past end detected at 0x"
<< Twine::utohexstr(AbsoluteInstrAddr) << " in function "
<< *this << " : replacing with nop.\n"; } } while
(false)
1289 << " : replacing with nop.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: jump past end detected at 0x"
<< Twine::utohexstr(AbsoluteInstrAddr) << " in function "
<< *this << " : replacing with nop.\n"; } } while
(false)
;
1290 BC.MIB->createNoop(Instruction);
1291 if (IsCondBranch) {
1292 // Register branch offset for profile validation.
1293 IgnoredBranches.emplace_back(Offset, Offset + Size);
1294 }
1295 goto add_instruction;
1296 }
1297 // May update Instruction and IsCall
1298 TargetSymbol = handleExternalReference(Instruction, Size, Offset,
1299 TargetAddress, IsCall);
1300 }
1301 }
1302
1303 if (!IsCall) {
1304 // Add taken branch info.
1305 TakenBranches.emplace_back(Offset, TargetAddress - getAddress());
1306 }
1307 BC.MIB->replaceBranchTarget(Instruction, TargetSymbol, &*Ctx);
1308
1309 // Mark CTC.
1310 if (IsCondBranch && IsCall)
1311 MIB->setConditionalTailCall(Instruction, TargetAddress);
1312 } else {
1313 // Could not evaluate branch. Should be an indirect call or an
1314 // indirect branch. Bail out on the latter case.
1315 if (MIB->isIndirectBranch(Instruction))
1316 handleIndirectBranch(Instruction, Size, Offset);
1317 // Indirect call. We only need to fix it if the operand is RIP-relative.
1318 if (IsSimple && MIB->hasPCRelOperand(Instruction))
1319 handlePCRelOperand(Instruction, AbsoluteInstrAddr, Size);
1320
1321 if (BC.isAArch64())
1322 handleAArch64IndirectCall(Instruction, Offset);
1323 }
1324 } else if (BC.isAArch64()) {
1325 // Check if there's a relocation associated with this instruction.
1326 bool UsedReloc = false;
1327 for (auto Itr = Relocations.lower_bound(Offset),
1328 ItrE = Relocations.lower_bound(Offset + Size);
1329 Itr != ItrE; ++Itr) {
1330 const Relocation &Relocation = Itr->second;
1331 int64_t Value = Relocation.Value;
1332 const bool Result = BC.MIB->replaceImmWithSymbolRef(
1333 Instruction, Relocation.Symbol, Relocation.Addend, Ctx.get(), Value,
1334 Relocation.Type);
1335 (void)Result;
1336 assert(Result && "cannot replace immediate with relocation")(static_cast <bool> (Result && "cannot replace immediate with relocation"
) ? void (0) : __assert_fail ("Result && \"cannot replace immediate with relocation\""
, "bolt/lib/Core/BinaryFunction.cpp", 1336, __extension__ __PRETTY_FUNCTION__
))
;
1337
1338 // For aarch64, if we replaced an immediate with a symbol from a
1339 // relocation, we mark it so we do not try to further process a
1340 // pc-relative operand. All we need is the symbol.
1341 UsedReloc = true;
1342 }
1343
1344 if (MIB->hasPCRelOperand(Instruction) && !UsedReloc)
1345 handlePCRelOperand(Instruction, AbsoluteInstrAddr, Size);
1346 }
1347
1348add_instruction:
1349 if (getDWARFLineTable()) {
1350 Instruction.setLoc(findDebugLineInformationForInstructionAt(
1351 AbsoluteInstrAddr, getDWARFUnit(), getDWARFLineTable()));
1352 }
1353
1354 // Record offset of the instruction for profile matching.
1355 if (BC.keepOffsetForInstruction(Instruction))
1356 MIB->setOffset(Instruction, static_cast<uint32_t>(Offset));
1357
1358 if (BC.MIB->isNoop(Instruction)) {
1359 // NOTE: disassembly loses the correct size information for noops.
1360 // E.g. nopw 0x0(%rax,%rax,1) is 9 bytes, but re-encoded it's only
1361 // 5 bytes. Preserve the size info using annotations.
1362 MIB->addAnnotation(Instruction, "Size", static_cast<uint32_t>(Size));
1363 }
1364
1365 addInstruction(Offset, std::move(Instruction));
1366 }
1367
1368 // Reset symbolizer for the disassembler.
1369 BC.SymbolicDisAsm->setSymbolizer(nullptr);
1370
1371 if (uint64_t Offset = getFirstInstructionOffset())
1372 Labels[Offset] = BC.Ctx->createNamedTempSymbol();
1373
1374 clearList(Relocations);
1375
1376 if (!IsSimple) {
1377 clearList(Instructions);
1378 return false;
1379 }
1380
1381 updateState(State::Disassembled);
1382
1383 return true;
1384}
1385
1386bool BinaryFunction::scanExternalRefs() {
1387 bool Success = true;
1388 bool DisassemblyFailed = false;
1389
1390 // Ignore pseudo functions.
1391 if (isPseudo())
1392 return Success;
1393
1394 if (opts::NoScan) {
1395 clearList(Relocations);
1396 clearList(ExternallyReferencedOffsets);
1397
1398 return false;
1399 }
1400
1401 // List of external references for this function.
1402 std::vector<Relocation> FunctionRelocations;
1403
1404 static BinaryContext::IndependentCodeEmitter Emitter =
1405 BC.createIndependentMCCodeEmitter();
1406
1407 ErrorOr<ArrayRef<uint8_t>> ErrorOrFunctionData = getData();
1408 assert(ErrorOrFunctionData && "function data is not available")(static_cast <bool> (ErrorOrFunctionData && "function data is not available"
) ? void (0) : __assert_fail ("ErrorOrFunctionData && \"function data is not available\""
, "bolt/lib/Core/BinaryFunction.cpp", 1408, __extension__ __PRETTY_FUNCTION__
))
;
1409 ArrayRef<uint8_t> FunctionData = *ErrorOrFunctionData;
1410 assert(FunctionData.size() == getMaxSize() &&(static_cast <bool> (FunctionData.size() == getMaxSize(
) && "function size does not match raw data size") ? void
(0) : __assert_fail ("FunctionData.size() == getMaxSize() && \"function size does not match raw data size\""
, "bolt/lib/Core/BinaryFunction.cpp", 1411, __extension__ __PRETTY_FUNCTION__
))
1411 "function size does not match raw data size")(static_cast <bool> (FunctionData.size() == getMaxSize(
) && "function size does not match raw data size") ? void
(0) : __assert_fail ("FunctionData.size() == getMaxSize() && \"function size does not match raw data size\""
, "bolt/lib/Core/BinaryFunction.cpp", 1411, __extension__ __PRETTY_FUNCTION__
))
;
1412
1413 uint64_t Size = 0; // instruction size
1414 for (uint64_t Offset = 0; Offset < getSize(); Offset += Size) {
1415 // Check for data inside code and ignore it
1416 if (const size_t DataInCodeSize = getSizeOfDataInCodeAt(Offset)) {
1417 Size = DataInCodeSize;
1418 continue;
1419 }
1420
1421 const uint64_t AbsoluteInstrAddr = getAddress() + Offset;
1422 MCInst Instruction;
1423 if (!BC.DisAsm->getInstruction(Instruction, Size,
1424 FunctionData.slice(Offset),
1425 AbsoluteInstrAddr, nulls())) {
1426 if (opts::Verbosity >= 1 && !isZeroPaddingAt(Offset)) {
1427 errs() << "BOLT-WARNING: unable to disassemble instruction at offset 0x"
1428 << Twine::utohexstr(Offset) << " (address 0x"
1429 << Twine::utohexstr(AbsoluteInstrAddr) << ") in function "
1430 << *this << '\n';
1431 }
1432 Success = false;
1433 DisassemblyFailed = true;
1434 break;
1435 }
1436
1437 // Return true if we can skip handling the Target function reference.
1438 auto ignoreFunctionRef = [&](const BinaryFunction &Target) {
1439 if (&Target == this)
1440 return true;
1441
1442 // Note that later we may decide not to emit Target function. In that
1443 // case, we conservatively create references that will be ignored or
1444 // resolved to the same function.
1445 if (!BC.shouldEmit(Target))
1446 return true;
1447
1448 return false;
1449 };
1450
1451 // Return true if we can ignore reference to the symbol.
1452 auto ignoreReference = [&](const MCSymbol *TargetSymbol) {
1453 if (!TargetSymbol)
1454 return true;
1455
1456 if (BC.forceSymbolRelocations(TargetSymbol->getName()))
1457 return false;
1458
1459 BinaryFunction *TargetFunction = BC.getFunctionForSymbol(TargetSymbol);
1460 if (!TargetFunction)
1461 return true;
1462
1463 return ignoreFunctionRef(*TargetFunction);
1464 };
1465
1466 // Detect if the instruction references an address.
1467 // Without relocations, we can only trust PC-relative address modes.
1468 uint64_t TargetAddress = 0;
1469 bool IsPCRel = false;
1470 bool IsBranch = false;
1471 if (BC.MIB->hasPCRelOperand(Instruction)) {
1472 IsPCRel = BC.MIB->evaluateMemOperandTarget(Instruction, TargetAddress,
1473 AbsoluteInstrAddr, Size);
1474 } else if (BC.MIB->isCall(Instruction) || BC.MIB->isBranch(Instruction)) {
1475 IsBranch = BC.MIB->evaluateBranch(Instruction, AbsoluteInstrAddr, Size,
1476 TargetAddress);
1477 }
1478
1479 MCSymbol *TargetSymbol = nullptr;
1480
1481 // Create an entry point at reference address if needed.
1482 BinaryFunction *TargetFunction =
1483 BC.getBinaryFunctionContainingAddress(TargetAddress);
1484 if (TargetFunction && !ignoreFunctionRef(*TargetFunction)) {
1485 const uint64_t FunctionOffset =
1486 TargetAddress - TargetFunction->getAddress();
1487 TargetSymbol = FunctionOffset
1488 ? TargetFunction->addEntryPointAtOffset(FunctionOffset)
1489 : TargetFunction->getSymbol();
1490 }
1491
1492 // Can't find more references and not creating relocations.
1493 if (!BC.HasRelocations)
1494 continue;
1495
1496 // Create a relocation against the TargetSymbol as the symbol might get
1497 // moved.
1498 if (TargetSymbol) {
1499 if (IsBranch) {
1500 BC.MIB->replaceBranchTarget(Instruction, TargetSymbol,
1501 Emitter.LocalCtx.get());
1502 } else if (IsPCRel) {
1503 const MCExpr *Expr = MCSymbolRefExpr::create(
1504 TargetSymbol, MCSymbolRefExpr::VK_None, *Emitter.LocalCtx.get());
1505 BC.MIB->replaceMemOperandDisp(
1506 Instruction, MCOperand::createExpr(BC.MIB->getTargetExprFor(
1507 Instruction, Expr, *Emitter.LocalCtx.get(), 0)));
1508 }
1509 }
1510
1511 // Create more relocations based on input file relocations.
1512 bool HasRel = false;
1513 for (auto Itr = Relocations.lower_bound(Offset),
1514 ItrE = Relocations.lower_bound(Offset + Size);
1515 Itr != ItrE; ++Itr) {
1516 Relocation &Relocation = Itr->second;
1517 if (Relocation.isPCRelative() && BC.isX86())
1518 continue;
1519 if (ignoreReference(Relocation.Symbol))
1520 continue;
1521
1522 int64_t Value = Relocation.Value;
1523 const bool Result = BC.MIB->replaceImmWithSymbolRef(
1524 Instruction, Relocation.Symbol, Relocation.Addend,
1525 Emitter.LocalCtx.get(), Value, Relocation.Type);
1526 (void)Result;
1527 assert(Result && "cannot replace immediate with relocation")(static_cast <bool> (Result && "cannot replace immediate with relocation"
) ? void (0) : __assert_fail ("Result && \"cannot replace immediate with relocation\""
, "bolt/lib/Core/BinaryFunction.cpp", 1527, __extension__ __PRETTY_FUNCTION__
))
;
1528
1529 HasRel = true;
1530 }
1531
1532 if (!TargetSymbol && !HasRel)
1533 continue;
1534
1535 // Emit the instruction using temp emitter and generate relocations.
1536 SmallString<256> Code;
1537 SmallVector<MCFixup, 4> Fixups;
1538 raw_svector_ostream VecOS(Code);
1539 Emitter.MCE->encodeInstruction(Instruction, VecOS, Fixups, *BC.STI);
1540
1541 // Create relocation for every fixup.
1542 for (const MCFixup &Fixup : Fixups) {
1543 std::optional<Relocation> Rel = BC.MIB->createRelocation(Fixup, *BC.MAB);
1544 if (!Rel) {
1545 Success = false;
1546 continue;
1547 }
1548
1549 if (Relocation::getSizeForType(Rel->Type) < 4) {
1550 // If the instruction uses a short form, then we might not be able
1551 // to handle the rewrite without relaxation, and hence cannot reliably
1552 // create an external reference relocation.
1553 Success = false;
1554 continue;
1555 }
1556 Rel->Offset += getAddress() - getOriginSection()->getAddress() + Offset;
1557 FunctionRelocations.push_back(*Rel);
1558 }
1559
1560 if (!Success)
1561 break;
1562 }
1563
1564 // Add relocations unless disassembly failed for this function.
1565 if (!DisassemblyFailed)
1566 for (Relocation &Rel : FunctionRelocations)
1567 getOriginSection()->addPendingRelocation(Rel);
1568
1569 // Inform BinaryContext that this function symbols will not be defined and
1570 // relocations should not be created against them.
1571 if (BC.HasRelocations) {
1572 for (std::pair<const uint32_t, MCSymbol *> &LI : Labels)
1573 BC.UndefinedSymbols.insert(LI.second);
1574 for (MCSymbol *const EndLabel : FunctionEndLabels)
1575 if (EndLabel)
1576 BC.UndefinedSymbols.insert(EndLabel);
1577 }
1578
1579 clearList(Relocations);
1580 clearList(ExternallyReferencedOffsets);
1581
1582 if (Success && BC.HasRelocations)
1583 HasExternalRefRelocations = true;
1584
1585 if (opts::Verbosity >= 1 && !Success)
1586 outs() << "BOLT-INFO: failed to scan refs for " << *this << '\n';
1587
1588 return Success;
1589}
1590
1591void BinaryFunction::postProcessEntryPoints() {
1592 if (!isSimple())
1593 return;
1594
1595 for (auto &KV : Labels) {
1596 MCSymbol *Label = KV.second;
1597 if (!getSecondaryEntryPointSymbol(Label))
1598 continue;
1599
1600 // In non-relocation mode there's potentially an external undetectable
1601 // reference to the entry point and hence we cannot move this entry
1602 // point. Optimizing without moving could be difficult.
1603 if (!BC.HasRelocations)
1604 setSimple(false);
1605
1606 const uint32_t Offset = KV.first;
1607
1608 // If we are at Offset 0 and there is no instruction associated with it,
1609 // this means this is an empty function. Just ignore. If we find an
1610 // instruction at this offset, this entry point is valid.
1611 if (!Offset || getInstructionAtOffset(Offset))
1612 continue;
1613
1614 // On AArch64 there are legitimate reasons to have references past the
1615 // end of the function, e.g. jump tables.
1616 if (BC.isAArch64() && Offset == getSize())
1617 continue;
1618
1619 errs() << "BOLT-WARNING: reference in the middle of instruction "
1620 "detected in function "
1621 << *this << " at offset 0x" << Twine::utohexstr(Offset) << '\n';
1622 if (BC.HasRelocations)
1623 setIgnored();
1624 setSimple(false);
1625 return;
1626 }
1627}
1628
1629void BinaryFunction::postProcessJumpTables() {
1630 // Create labels for all entries.
1631 for (auto &JTI : JumpTables) {
1632 JumpTable &JT = *JTI.second;
1633 if (JT.Type == JumpTable::JTT_PIC && opts::JumpTables == JTS_BASIC) {
1634 opts::JumpTables = JTS_MOVE;
1635 outs() << "BOLT-INFO: forcing -jump-tables=move as PIC jump table was "
1636 "detected in function "
1637 << *this << '\n';
1638 }
1639 if (JT.Entries.empty()) {
1640 bool HasOneParent = (JT.Parents.size() == 1);
1641 for (unsigned I = 0; I < JT.EntriesAsAddress.size(); ++I) {
1642 uint64_t EntryAddress = JT.EntriesAsAddress[I];
1643 // builtin_unreachable does not belong to any function
1644 // Need to handle separately
1645 bool IsBuiltIn = false;
1646 for (BinaryFunction *Parent : JT.Parents) {
1647 if (EntryAddress == Parent->getAddress() + Parent->getSize()) {
1648 IsBuiltIn = true;
1649 // Specify second parameter as true to accept builtin_unreachable
1650 MCSymbol *Label = getOrCreateLocalLabel(EntryAddress, true);
1651 JT.Entries.push_back(Label);
1652 break;
1653 }
1654 }
1655 if (IsBuiltIn)
1656 continue;
1657 // Create local label for targets cannot be reached by other fragments
1658 // Otherwise, secondary entry point to target function
1659 BinaryFunction *TargetBF =
1660 BC.getBinaryFunctionContainingAddress(EntryAddress);
1661 if (TargetBF->getAddress() != EntryAddress) {
1662 MCSymbol *Label =
1663 (HasOneParent && TargetBF == this)
1664 ? getOrCreateLocalLabel(JT.EntriesAsAddress[I], true)
1665 : TargetBF->addEntryPointAtOffset(EntryAddress -
1666 TargetBF->getAddress());
1667 JT.Entries.push_back(Label);
1668 }
1669 }
1670 }
1671
1672 const uint64_t BDSize =
1673 BC.getBinaryDataAtAddress(JT.getAddress())->getSize();
1674 if (!BDSize) {
1675 BC.setBinaryDataSize(JT.getAddress(), JT.getSize());
1676 } else {
1677 assert(BDSize >= JT.getSize() &&(static_cast <bool> (BDSize >= JT.getSize() &&
"jump table cannot be larger than the containing object") ? void
(0) : __assert_fail ("BDSize >= JT.getSize() && \"jump table cannot be larger than the containing object\""
, "bolt/lib/Core/BinaryFunction.cpp", 1678, __extension__ __PRETTY_FUNCTION__
))
1678 "jump table cannot be larger than the containing object")(static_cast <bool> (BDSize >= JT.getSize() &&
"jump table cannot be larger than the containing object") ? void
(0) : __assert_fail ("BDSize >= JT.getSize() && \"jump table cannot be larger than the containing object\""
, "bolt/lib/Core/BinaryFunction.cpp", 1678, __extension__ __PRETTY_FUNCTION__
))
;
1679 }
1680 }
1681
1682 // Add TakenBranches from JumpTables.
1683 //
1684 // We want to do it after initial processing since we don't know jump tables'
1685 // boundaries until we process them all.
1686 for (auto &JTSite : JTSites) {
1687 const uint64_t JTSiteOffset = JTSite.first;
1688 const uint64_t JTAddress = JTSite.second;
1689 const JumpTable *JT = getJumpTableContainingAddress(JTAddress);
1690 assert(JT && "cannot find jump table for address")(static_cast <bool> (JT && "cannot find jump table for address"
) ? void (0) : __assert_fail ("JT && \"cannot find jump table for address\""
, "bolt/lib/Core/BinaryFunction.cpp", 1690, __extension__ __PRETTY_FUNCTION__
))
;
1691
1692 uint64_t EntryOffset = JTAddress - JT->getAddress();
1693 while (EntryOffset < JT->getSize()) {
1694 uint64_t EntryAddress = JT->EntriesAsAddress[EntryOffset / JT->EntrySize];
1695 uint64_t TargetOffset = EntryAddress - getAddress();
1696 if (TargetOffset < getSize()) {
1697 TakenBranches.emplace_back(JTSiteOffset, TargetOffset);
1698
1699 if (opts::StrictMode)
1700 registerReferencedOffset(TargetOffset);
1701 }
1702
1703 EntryOffset += JT->EntrySize;
1704
1705 // A label at the next entry means the end of this jump table.
1706 if (JT->Labels.count(EntryOffset))
1707 break;
1708 }
1709 }
1710 clearList(JTSites);
1711
1712 // Conservatively populate all possible destinations for unknown indirect
1713 // branches.
1714 if (opts::StrictMode && hasInternalReference()) {
1715 for (uint64_t Offset : UnknownIndirectBranchOffsets) {
1716 for (uint64_t PossibleDestination : ExternallyReferencedOffsets) {
1717 // Ignore __builtin_unreachable().
1718 if (PossibleDestination == getSize())
1719 continue;
1720 TakenBranches.emplace_back(Offset, PossibleDestination);
1721 }
1722 }
1723 }
1724
1725 // Remove duplicates branches. We can get a bunch of them from jump tables.
1726 // Without doing jump table value profiling we don't have use for extra
1727 // (duplicate) branches.
1728 llvm::sort(TakenBranches);
1729 auto NewEnd = std::unique(TakenBranches.begin(), TakenBranches.end());
1730 TakenBranches.erase(NewEnd, TakenBranches.end());
1731}
1732
1733bool BinaryFunction::validateExternallyReferencedOffsets() {
1734 SmallPtrSet<MCSymbol *, 4> JTTargets;
1735 for (const JumpTable *JT : llvm::make_second_range(JumpTables))
1736 JTTargets.insert(JT->Entries.begin(), JT->Entries.end());
1737
1738 bool HasUnclaimedReference = false;
1739 for (uint64_t Destination : ExternallyReferencedOffsets) {
1740 // Ignore __builtin_unreachable().
1741 if (Destination == getSize())
1742 continue;
1743 // Ignore constant islands
1744 if (isInConstantIsland(Destination + getAddress()))
1745 continue;
1746
1747 if (BinaryBasicBlock *BB = getBasicBlockAtOffset(Destination)) {
1748 // Check if the externally referenced offset is a recognized jump table
1749 // target.
1750 if (JTTargets.contains(BB->getLabel()))
1751 continue;
1752
1753 if (opts::Verbosity >= 1) {
1754 errs() << "BOLT-WARNING: unclaimed data to code reference (possibly "
1755 << "an unrecognized jump table entry) to " << BB->getName()
1756 << " in " << *this << "\n";
1757 }
1758 auto L = BC.scopeLock();
1759 addEntryPoint(*BB);
1760 } else {
1761 errs() << "BOLT-WARNING: unknown data to code reference to offset "
1762 << Twine::utohexstr(Destination) << " in " << *this << "\n";
1763 setIgnored();
1764 }
1765 HasUnclaimedReference = true;
1766 }
1767 return !HasUnclaimedReference;
1768}
1769
1770bool BinaryFunction::postProcessIndirectBranches(
1771 MCPlusBuilder::AllocatorIdTy AllocId) {
1772 auto addUnknownControlFlow = [&](BinaryBasicBlock &BB) {
1773 LLVM_DEBUG(dbgs() << "BOLT-DEBUG: adding unknown control flow in " << *thisdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: adding unknown control flow in "
<< *this << " for " << BB.getName() <<
"\n"; } } while (false)
1774 << " for " << BB.getName() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: adding unknown control flow in "
<< *this << " for " << BB.getName() <<
"\n"; } } while (false)
;
1775 HasUnknownControlFlow = true;
1776 BB.removeAllSuccessors();
1777 for (uint64_t PossibleDestination : ExternallyReferencedOffsets)
1778 if (BinaryBasicBlock *SuccBB = getBasicBlockAtOffset(PossibleDestination))
1779 BB.addSuccessor(SuccBB);
1780 };
1781
1782 uint64_t NumIndirectJumps = 0;
1783 MCInst *LastIndirectJump = nullptr;
1784 BinaryBasicBlock *LastIndirectJumpBB = nullptr;
1785 uint64_t LastJT = 0;
1786 uint16_t LastJTIndexReg = BC.MIB->getNoRegister();
1787 for (BinaryBasicBlock &BB : blocks()) {
1788 for (MCInst &Instr : BB) {
1789 if (!BC.MIB->isIndirectBranch(Instr))
1790 continue;
1791
1792 // If there's an indirect branch in a single-block function -
1793 // it must be a tail call.
1794 if (BasicBlocks.size() == 1) {
1795 BC.MIB->convertJmpToTailCall(Instr);
1796 return true;
1797 }
1798
1799 ++NumIndirectJumps;
1800
1801 if (opts::StrictMode && !hasInternalReference()) {
1802 BC.MIB->convertJmpToTailCall(Instr);
1803 break;
1804 }
1805
1806 // Validate the tail call or jump table assumptions now that we know
1807 // basic block boundaries.
1808 if (BC.MIB->isTailCall(Instr) || BC.MIB->getJumpTable(Instr)) {
1809 const unsigned PtrSize = BC.AsmInfo->getCodePointerSize();
1810 MCInst *MemLocInstr;
1811 unsigned BaseRegNum, IndexRegNum;
1812 int64_t DispValue;
1813 const MCExpr *DispExpr;
1814 MCInst *PCRelBaseInstr;
1815 IndirectBranchType Type = BC.MIB->analyzeIndirectBranch(
1816 Instr, BB.begin(), BB.end(), PtrSize, MemLocInstr, BaseRegNum,
1817 IndexRegNum, DispValue, DispExpr, PCRelBaseInstr);
1818 if (Type != IndirectBranchType::UNKNOWN || MemLocInstr != nullptr)
1819 continue;
1820
1821 if (!opts::StrictMode)
1822 return false;
1823
1824 if (BC.MIB->isTailCall(Instr)) {
1825 BC.MIB->convertTailCallToJmp(Instr);
1826 } else {
1827 LastIndirectJump = &Instr;
1828 LastIndirectJumpBB = &BB;
1829 LastJT = BC.MIB->getJumpTable(Instr);
1830 LastJTIndexReg = BC.MIB->getJumpTableIndexReg(Instr);
1831 BC.MIB->unsetJumpTable(Instr);
1832
1833 JumpTable *JT = BC.getJumpTableContainingAddress(LastJT);
1834 if (JT->Type == JumpTable::JTT_NORMAL) {
1835 // Invalidating the jump table may also invalidate other jump table
1836 // boundaries. Until we have/need a support for this, mark the
1837 // function as non-simple.
1838 LLVM_DEBUG(dbgs() << "BOLT-DEBUG: rejected jump table reference"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: rejected jump table reference"
<< JT->getName() << " in " << *this <<
'\n'; } } while (false)
1839 << JT->getName() << " in " << *this << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: rejected jump table reference"
<< JT->getName() << " in " << *this <<
'\n'; } } while (false)
;
1840 return false;
1841 }
1842 }
1843
1844 addUnknownControlFlow(BB);
1845 continue;
1846 }
1847
1848 // If this block contains an epilogue code and has an indirect branch,
1849 // then most likely it's a tail call. Otherwise, we cannot tell for sure
1850 // what it is and conservatively reject the function's CFG.
1851 bool IsEpilogue = llvm::any_of(BB, [&](const MCInst &Instr) {
1852 return BC.MIB->isLeave(Instr) || BC.MIB->isPop(Instr);
1853 });
1854 if (IsEpilogue) {
1855 BC.MIB->convertJmpToTailCall(Instr);
1856 BB.removeAllSuccessors();
1857 continue;
1858 }
1859
1860 if (opts::Verbosity >= 2) {
1861 outs() << "BOLT-INFO: rejected potential indirect tail call in "
1862 << "function " << *this << " in basic block " << BB.getName()
1863 << ".\n";
1864 LLVM_DEBUG(BC.printInstructions(dbgs(), BB.begin(), BB.end(),do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { BC.printInstructions(dbgs(), BB.begin(), BB.end()
, BB.getOffset(), this, true); } } while (false)
1865 BB.getOffset(), this, true))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { BC.printInstructions(dbgs(), BB.begin(), BB.end()
, BB.getOffset(), this, true); } } while (false)
;
1866 }
1867
1868 if (!opts::StrictMode)
1869 return false;
1870
1871 addUnknownControlFlow(BB);
1872 }
1873 }
1874
1875 if (HasInternalLabelReference)
1876 return false;
1877
1878 // If there's only one jump table, and one indirect jump, and no other
1879 // references, then we should be able to derive the jump table even if we
1880 // fail to match the pattern.
1881 if (HasUnknownControlFlow && NumIndirectJumps == 1 &&
1882 JumpTables.size() == 1 && LastIndirectJump &&
1883 !BC.getJumpTableContainingAddress(LastJT)->IsSplit) {
1884 LLVM_DEBUG(dbgs() << "BOLT-DEBUG: unsetting unknown control flow in "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: unsetting unknown control flow in "
<< *this << '\n'; } } while (false)
1885 << *this << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: unsetting unknown control flow in "
<< *this << '\n'; } } while (false)
;
1886 BC.MIB->setJumpTable(*LastIndirectJump, LastJT, LastJTIndexReg, AllocId);
1887 HasUnknownControlFlow = false;
1888
1889 LastIndirectJumpBB->updateJumpTableSuccessors();
1890 }
1891
1892 if (HasFixedIndirectBranch)
1893 return false;
1894
1895 // Validate that all data references to function offsets are claimed by
1896 // recognized jump tables. Register externally referenced blocks as entry
1897 // points.
1898 if (!opts::StrictMode && hasInternalReference()) {
1899 if (!validateExternallyReferencedOffsets())
1900 return false;
1901 }
1902
1903 if (HasUnknownControlFlow && !BC.HasRelocations)
1904 return false;
1905
1906 return true;
1907}
1908
1909void BinaryFunction::recomputeLandingPads() {
1910 updateBBIndices(0);
1911
1912 for (BinaryBasicBlock *BB : BasicBlocks) {
1913 BB->LandingPads.clear();
1914 BB->Throwers.clear();
1915 }
1916
1917 for (BinaryBasicBlock *BB : BasicBlocks) {
1918 std::unordered_set<const BinaryBasicBlock *> BBLandingPads;
1919 for (MCInst &Instr : *BB) {
1920 if (!BC.MIB->isInvoke(Instr))
1921 continue;
1922
1923 const std::optional<MCPlus::MCLandingPad> EHInfo =
1924 BC.MIB->getEHInfo(Instr);
1925 if (!EHInfo || !EHInfo->first)
1926 continue;
1927
1928 BinaryBasicBlock *LPBlock = getBasicBlockForLabel(EHInfo->first);
1929 if (!BBLandingPads.count(LPBlock)) {
1930 BBLandingPads.insert(LPBlock);
1931 BB->LandingPads.emplace_back(LPBlock);
1932 LPBlock->Throwers.emplace_back(BB);
1933 }
1934 }
1935 }
1936}
1937
1938bool BinaryFunction::buildCFG(MCPlusBuilder::AllocatorIdTy AllocatorId) {
1939 auto &MIB = BC.MIB;
1940
1941 if (!isSimple()) {
1942 assert(!BC.HasRelocations &&(static_cast <bool> (!BC.HasRelocations && "cannot process file with non-simple function in relocs mode"
) ? void (0) : __assert_fail ("!BC.HasRelocations && \"cannot process file with non-simple function in relocs mode\""
, "bolt/lib/Core/BinaryFunction.cpp", 1943, __extension__ __PRETTY_FUNCTION__
))
1943 "cannot process file with non-simple function in relocs mode")(static_cast <bool> (!BC.HasRelocations && "cannot process file with non-simple function in relocs mode"
) ? void (0) : __assert_fail ("!BC.HasRelocations && \"cannot process file with non-simple function in relocs mode\""
, "bolt/lib/Core/BinaryFunction.cpp", 1943, __extension__ __PRETTY_FUNCTION__
))
;
1944 return false;
1945 }
1946
1947 if (CurrentState != State::Disassembled)
1948 return false;
1949
1950 assert(BasicBlocks.empty() && "basic block list should be empty")(static_cast <bool> (BasicBlocks.empty() && "basic block list should be empty"
) ? void (0) : __assert_fail ("BasicBlocks.empty() && \"basic block list should be empty\""
, "bolt/lib/Core/BinaryFunction.cpp", 1950, __extension__ __PRETTY_FUNCTION__
))
;
1951 assert((Labels.find(getFirstInstructionOffset()) != Labels.end()) &&(static_cast <bool> ((Labels.find(getFirstInstructionOffset
()) != Labels.end()) && "first instruction should always have a label"
) ? void (0) : __assert_fail ("(Labels.find(getFirstInstructionOffset()) != Labels.end()) && \"first instruction should always have a label\""
, "bolt/lib/Core/BinaryFunction.cpp", 1952, __extension__ __PRETTY_FUNCTION__
))
1952 "first instruction should always have a label")(static_cast <bool> ((Labels.find(getFirstInstructionOffset
()) != Labels.end()) && "first instruction should always have a label"
) ? void (0) : __assert_fail ("(Labels.find(getFirstInstructionOffset()) != Labels.end()) && \"first instruction should always have a label\""
, "bolt/lib/Core/BinaryFunction.cpp", 1952, __extension__ __PRETTY_FUNCTION__
))
;
1953
1954 // Create basic blocks in the original layout order:
1955 //
1956 // * Every instruction with associated label marks
1957 // the beginning of a basic block.
1958 // * Conditional instruction marks the end of a basic block,
1959 // except when the following instruction is an
1960 // unconditional branch, and the unconditional branch is not
1961 // a destination of another branch. In the latter case, the
1962 // basic block will consist of a single unconditional branch
1963 // (missed "double-jump" optimization).
1964 //
1965 // Created basic blocks are sorted in layout order since they are
1966 // created in the same order as instructions, and instructions are
1967 // sorted by offsets.
1968 BinaryBasicBlock *InsertBB = nullptr;
1969 BinaryBasicBlock *PrevBB = nullptr;
1970 bool IsLastInstrNop = false;
1971 // Offset of the last non-nop instruction.
1972 uint64_t LastInstrOffset = 0;
1973
1974 auto addCFIPlaceholders = [this](uint64_t CFIOffset,
1975 BinaryBasicBlock *InsertBB) {
1976 for (auto FI = OffsetToCFI.lower_bound(CFIOffset),
1977 FE = OffsetToCFI.upper_bound(CFIOffset);
1978 FI != FE; ++FI) {
1979 addCFIPseudo(InsertBB, InsertBB->end(), FI->second);
1980 }
1981 };
1982
1983 // For profiling purposes we need to save the offset of the last instruction
1984 // in the basic block.
1985 // NOTE: nops always have an Offset annotation. Annotate the last non-nop as
1986 // older profiles ignored nops.
1987 auto updateOffset = [&](uint64_t Offset) {
1988 assert(PrevBB && PrevBB != InsertBB && "invalid previous block")(static_cast <bool> (PrevBB && PrevBB != InsertBB
&& "invalid previous block") ? void (0) : __assert_fail
("PrevBB && PrevBB != InsertBB && \"invalid previous block\""
, "bolt/lib/Core/BinaryFunction.cpp", 1988, __extension__ __PRETTY_FUNCTION__
))
;
1989 MCInst *LastNonNop = nullptr;
1990 for (BinaryBasicBlock::reverse_iterator RII = PrevBB->getLastNonPseudo(),
1991 E = PrevBB->rend();
1992 RII != E; ++RII) {
1993 if (!BC.MIB->isPseudo(*RII) && !BC.MIB->isNoop(*RII)) {
1994 LastNonNop = &*RII;
1995 break;
1996 }
1997 }
1998 if (LastNonNop && !MIB->getOffset(*LastNonNop))
1999 MIB->setOffset(*LastNonNop, static_cast<uint32_t>(Offset), AllocatorId);
2000 };
2001
2002 for (auto I = Instructions.begin(), E = Instructions.end(); I != E; ++I) {
2003 const uint32_t Offset = I->first;
2004 MCInst &Instr = I->second;
2005
2006 auto LI = Labels.find(Offset);
2007 if (LI != Labels.end()) {
2008 // Always create new BB at branch destination.
2009 PrevBB = InsertBB ? InsertBB : PrevBB;
2010 InsertBB = addBasicBlockAt(LI->first, LI->second);
2011 if (opts::PreserveBlocksAlignment && IsLastInstrNop)
2012 InsertBB->setDerivedAlignment();
2013
2014 if (PrevBB)
2015 updateOffset(LastInstrOffset);
2016 }
2017
2018 const uint64_t InstrInputAddr = I->first + Address;
2019 bool IsSDTMarker =
2020 MIB->isNoop(Instr) && BC.SDTMarkers.count(InstrInputAddr);
2021 bool IsLKMarker = BC.LKMarkers.count(InstrInputAddr);
2022 // Mark all nops with Offset for profile tracking purposes.
2023 if (MIB->isNoop(Instr) || IsLKMarker) {
2024 if (!MIB->getOffset(Instr))
2025 MIB->setOffset(Instr, static_cast<uint32_t>(Offset), AllocatorId);
2026 if (IsSDTMarker || IsLKMarker)
2027 HasSDTMarker = true;
2028 else
2029 // Annotate ordinary nops, so we can safely delete them if required.
2030 MIB->addAnnotation(Instr, "NOP", static_cast<uint32_t>(1), AllocatorId);
2031 }
2032
2033 if (!InsertBB) {
2034 // It must be a fallthrough or unreachable code. Create a new block unless
2035 // we see an unconditional branch following a conditional one. The latter
2036 // should not be a conditional tail call.
2037 assert(PrevBB && "no previous basic block for a fall through")(static_cast <bool> (PrevBB && "no previous basic block for a fall through"
) ? void (0) : __assert_fail ("PrevBB && \"no previous basic block for a fall through\""
, "bolt/lib/Core/BinaryFunction.cpp", 2037, __extension__ __PRETTY_FUNCTION__
))
;
2038 MCInst *PrevInstr = PrevBB->getLastNonPseudoInstr();
2039 assert(PrevInstr && "no previous instruction for a fall through")(static_cast <bool> (PrevInstr && "no previous instruction for a fall through"
) ? void (0) : __assert_fail ("PrevInstr && \"no previous instruction for a fall through\""
, "bolt/lib/Core/BinaryFunction.cpp", 2039, __extension__ __PRETTY_FUNCTION__
))
;
2040 if (MIB->isUnconditionalBranch(Instr) &&
2041 !MIB->isUnconditionalBranch(*PrevInstr) &&
2042 !MIB->getConditionalTailCall(*PrevInstr) &&
2043 !MIB->isReturn(*PrevInstr)) {
2044 // Temporarily restore inserter basic block.
2045 InsertBB = PrevBB;
2046 } else {
2047 MCSymbol *Label;
2048 {
2049 auto L = BC.scopeLock();
2050 Label = BC.Ctx->createNamedTempSymbol("FT");
2051 }
2052 InsertBB = addBasicBlockAt(Offset, Label);
2053 if (opts::PreserveBlocksAlignment && IsLastInstrNop)
2054 InsertBB->setDerivedAlignment();
2055 updateOffset(LastInstrOffset);
2056 }
2057 }
2058 if (Offset == getFirstInstructionOffset()) {
2059 // Add associated CFI pseudos in the first offset
2060 addCFIPlaceholders(Offset, InsertBB);
2061 }
2062
2063 const bool IsBlockEnd = MIB->isTerminator(Instr);
2064 IsLastInstrNop = MIB->isNoop(Instr);
2065 if (!IsLastInstrNop)
2066 LastInstrOffset = Offset;
2067 InsertBB->addInstruction(std::move(Instr));
2068
2069 // Add associated CFI instrs. We always add the CFI instruction that is
2070 // located immediately after this instruction, since the next CFI
2071 // instruction reflects the change in state caused by this instruction.
2072 auto NextInstr = std::next(I);
2073 uint64_t CFIOffset;
2074 if (NextInstr != E)
2075 CFIOffset = NextInstr->first;
2076 else
2077 CFIOffset = getSize();
2078
2079 // Note: this potentially invalidates instruction pointers/iterators.
2080 addCFIPlaceholders(CFIOffset, InsertBB);
2081
2082 if (IsBlockEnd) {
2083 PrevBB = InsertBB;
2084 InsertBB = nullptr;
2085 }
2086 }
2087
2088 if (BasicBlocks.empty()) {
2089 setSimple(false);
2090 return false;
2091 }
2092
2093 // Intermediate dump.
2094 LLVM_DEBUG(print(dbgs(), "after creating basic blocks"))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { print(dbgs(), "after creating basic blocks"); } }
while (false)
;
2095
2096 // TODO: handle properly calls to no-return functions,
2097 // e.g. exit(3), etc. Otherwise we'll see a false fall-through
2098 // blocks.
2099
2100 for (std::pair<uint32_t, uint32_t> &Branch : TakenBranches) {
2101 LLVM_DEBUG(dbgs() << "registering branch [0x"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "registering branch [0x" <<
Twine::utohexstr(Branch.first) << "] -> [0x" <<
Twine::utohexstr(Branch.second) << "]\n"; } } while (false
)
2102 << Twine::utohexstr(Branch.first) << "] -> [0x"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "registering branch [0x" <<
Twine::utohexstr(Branch.first) << "] -> [0x" <<
Twine::utohexstr(Branch.second) << "]\n"; } } while (false
)
2103 << Twine::utohexstr(Branch.second) << "]\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "registering branch [0x" <<
Twine::utohexstr(Branch.first) << "] -> [0x" <<
Twine::utohexstr(Branch.second) << "]\n"; } } while (false
)
;
2104 BinaryBasicBlock *FromBB = getBasicBlockContainingOffset(Branch.first);
2105 BinaryBasicBlock *ToBB = getBasicBlockAtOffset(Branch.second);
2106 if (!FromBB || !ToBB) {
2107 if (!FromBB)
2108 errs() << "BOLT-ERROR: cannot find BB containing the branch.\n";
2109 if (!ToBB)
2110 errs() << "BOLT-ERROR: cannot find BB containing branch destination.\n";
2111 BC.exitWithBugReport("disassembly failed - inconsistent branch found.",
2112 *this);
2113 }
2114
2115 FromBB->addSuccessor(ToBB);
2116 }
2117
2118 // Add fall-through branches.
2119 PrevBB = nullptr;
2120 bool IsPrevFT = false; // Is previous block a fall-through.
2121 for (BinaryBasicBlock *BB : BasicBlocks) {
2122 if (IsPrevFT)
2123 PrevBB->addSuccessor(BB);
2124
2125 if (BB->empty()) {
2126 IsPrevFT = true;
2127 PrevBB = BB;
2128 continue;
2129 }
2130
2131 MCInst *LastInstr = BB->getLastNonPseudoInstr();
2132 assert(LastInstr &&(static_cast <bool> (LastInstr && "should have non-pseudo instruction in non-empty block"
) ? void (0) : __assert_fail ("LastInstr && \"should have non-pseudo instruction in non-empty block\""
, "bolt/lib/Core/BinaryFunction.cpp", 2133, __extension__ __PRETTY_FUNCTION__
))
2133 "should have non-pseudo instruction in non-empty block")(static_cast <bool> (LastInstr && "should have non-pseudo instruction in non-empty block"
) ? void (0) : __assert_fail ("LastInstr && \"should have non-pseudo instruction in non-empty block\""
, "bolt/lib/Core/BinaryFunction.cpp", 2133, __extension__ __PRETTY_FUNCTION__
))
;
2134
2135 if (BB->succ_size() == 0) {
2136 // Since there's no existing successors, we know the last instruction is
2137 // not a conditional branch. Thus if it's a terminator, it shouldn't be a
2138 // fall-through.
2139 //
2140 // Conditional tail call is a special case since we don't add a taken
2141 // branch successor for it.
2142 IsPrevFT = !MIB->isTerminator(*LastInstr) ||
2143 MIB->getConditionalTailCall(*LastInstr);
2144 } else if (BB->succ_size() == 1) {
2145 IsPrevFT = MIB->isConditionalBranch(*LastInstr);
2146 } else {
2147 IsPrevFT = false;
2148 }
2149
2150 PrevBB = BB;
2151 }
2152
2153 // Assign landing pads and throwers info.
2154 recomputeLandingPads();
2155
2156 // Assign CFI information to each BB entry.
2157 annotateCFIState();
2158
2159 // Annotate invoke instructions with GNU_args_size data.
2160 propagateGnuArgsSizeInfo(AllocatorId);
2161
2162 // Set the basic block layout to the original order and set end offsets.
2163 PrevBB = nullptr;
2164 for (BinaryBasicBlock *BB : BasicBlocks) {
2165 Layout.addBasicBlock(BB);
2166 if (PrevBB)
2167 PrevBB->setEndOffset(BB->getOffset());
2168 PrevBB = BB;
2169 }
2170 PrevBB->setEndOffset(getSize());
2171
2172 Layout.updateLayoutIndices();
2173
2174 normalizeCFIState();
2175
2176 // Clean-up memory taken by intermediate structures.
2177 //
2178 // NB: don't clear Labels list as we may need them if we mark the function
2179 // as non-simple later in the process of discovering extra entry points.
2180 clearList(Instructions);
2181 clearList(OffsetToCFI);
2182 clearList(TakenBranches);
2183
2184 // Update the state.
2185 CurrentState = State::CFG;
2186
2187 // Make any necessary adjustments for indirect branches.
2188 if (!postProcessIndirectBranches(AllocatorId)) {
2189 if (opts::Verbosity) {
2190 errs() << "BOLT-WARNING: failed to post-process indirect branches for "
2191 << *this << '\n';
2192 }
2193 // In relocation mode we want to keep processing the function but avoid
2194 // optimizing it.
2195 setSimple(false);
2196 }
2197
2198 clearList(ExternallyReferencedOffsets);
2199 clearList(UnknownIndirectBranchOffsets);
2200
2201 return true;
2202}
2203
2204void BinaryFunction::postProcessCFG() {
2205 if (isSimple() && !BasicBlocks.empty()) {
2206 // Convert conditional tail call branches to conditional branches that jump
2207 // to a tail call.
2208 removeConditionalTailCalls();
2209
2210 postProcessProfile();
2211
2212 // Eliminate inconsistencies between branch instructions and CFG.
2213 postProcessBranches();
2214 }
2215
2216 calculateMacroOpFusionStats();
2217
2218 // The final cleanup of intermediate structures.
2219 clearList(IgnoredBranches);
2220
2221 // Remove "Offset" annotations, unless we need an address-translation table
2222 // later. This has no cost, since annotations are allocated by a bumpptr
2223 // allocator and won't be released anyway until late in the pipeline.
2224 if (!requiresAddressTranslation() && !opts::Instrument) {
2225 for (BinaryBasicBlock &BB : blocks())
2226 for (MCInst &Inst : BB)
2227 BC.MIB->clearOffset(Inst);
2228 }
2229
2230 assert((!isSimple() || validateCFG()) &&(static_cast <bool> ((!isSimple() || validateCFG()) &&
"invalid CFG detected after post-processing") ? void (0) : __assert_fail
("(!isSimple() || validateCFG()) && \"invalid CFG detected after post-processing\""
, "bolt/lib/Core/BinaryFunction.cpp", 2231, __extension__ __PRETTY_FUNCTION__
))
2231 "invalid CFG detected after post-processing")(static_cast <bool> ((!isSimple() || validateCFG()) &&
"invalid CFG detected after post-processing") ? void (0) : __assert_fail
("(!isSimple() || validateCFG()) && \"invalid CFG detected after post-processing\""
, "bolt/lib/Core/BinaryFunction.cpp", 2231, __extension__ __PRETTY_FUNCTION__
))
;
2232}
2233
2234void BinaryFunction::calculateMacroOpFusionStats() {
2235 if (!getBinaryContext().isX86())
2236 return;
2237 for (const BinaryBasicBlock &BB : blocks()) {
2238 auto II = BB.getMacroOpFusionPair();
2239 if (II == BB.end())
2240 continue;
2241
2242 // Check offset of the second instruction.
2243 // FIXME: arch-specific.
2244 const uint32_t Offset = BC.MIB->getOffsetWithDefault(*std::next(II), 0);
2245 if (!Offset || (getAddress() + Offset) % 64)
2246 continue;
2247
2248 LLVM_DEBUG(dbgs() << "\nmissed macro-op fusion at address 0x"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "\nmissed macro-op fusion at address 0x"
<< Twine::utohexstr(getAddress() + Offset) << " in function "
<< *this << "; executed " << BB.getKnownExecutionCount
() << " times.\n"; } } while (false)
2249 << Twine::utohexstr(getAddress() + Offset)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "\nmissed macro-op fusion at address 0x"
<< Twine::utohexstr(getAddress() + Offset) << " in function "
<< *this << "; executed " << BB.getKnownExecutionCount
() << " times.\n"; } } while (false)
2250 << " in function " << *this << "; executed "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "\nmissed macro-op fusion at address 0x"
<< Twine::utohexstr(getAddress() + Offset) << " in function "
<< *this << "; executed " << BB.getKnownExecutionCount
() << " times.\n"; } } while (false)
2251 << BB.getKnownExecutionCount() << " times.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "\nmissed macro-op fusion at address 0x"
<< Twine::utohexstr(getAddress() + Offset) << " in function "
<< *this << "; executed " << BB.getKnownExecutionCount
() << " times.\n"; } } while (false)
;
2252 ++BC.MissedMacroFusionPairs;
2253 BC.MissedMacroFusionExecCount += BB.getKnownExecutionCount();
2254 }
2255}
2256
2257void BinaryFunction::removeTagsFromProfile() {
2258 for (BinaryBasicBlock *BB : BasicBlocks) {
2259 if (BB->ExecutionCount == BinaryBasicBlock::COUNT_NO_PROFILE)
2260 BB->ExecutionCount = 0;
2261 for (BinaryBasicBlock::BinaryBranchInfo &BI : BB->branch_info()) {
2262 if (BI.Count != BinaryBasicBlock::COUNT_NO_PROFILE &&
2263 BI.MispredictedCount != BinaryBasicBlock::COUNT_NO_PROFILE)
2264 continue;
2265 BI.Count = 0;
2266 BI.MispredictedCount = 0;
2267 }
2268 }
2269}
2270
2271void BinaryFunction::removeConditionalTailCalls() {
2272 // Blocks to be appended at the end.
2273 std::vector<std::unique_ptr<BinaryBasicBlock>> NewBlocks;
2274
2275 for (auto BBI = begin(); BBI != end(); ++BBI) {
2276 BinaryBasicBlock &BB = *BBI;
2277 MCInst *CTCInstr = BB.getLastNonPseudoInstr();
2278 if (!CTCInstr)
2279 continue;
2280
2281 std::optional<uint64_t> TargetAddressOrNone =
2282 BC.MIB->getConditionalTailCall(*CTCInstr);
2283 if (!TargetAddressOrNone)
2284 continue;
2285
2286 // Gather all necessary information about CTC instruction before
2287 // annotations are destroyed.
2288 const int32_t CFIStateBeforeCTC = BB.getCFIStateAtInstr(CTCInstr);
2289 uint64_t CTCTakenCount = BinaryBasicBlock::COUNT_NO_PROFILE;
2290 uint64_t CTCMispredCount = BinaryBasicBlock::COUNT_NO_PROFILE;
2291 if (hasValidProfile()) {
2292 CTCTakenCount = BC.MIB->getAnnotationWithDefault<uint64_t>(
2293 *CTCInstr, "CTCTakenCount");
2294 CTCMispredCount = BC.MIB->getAnnotationWithDefault<uint64_t>(
2295 *CTCInstr, "CTCMispredCount");
2296 }
2297
2298 // Assert that the tail call does not throw.
2299 assert(!BC.MIB->getEHInfo(*CTCInstr) &&(static_cast <bool> (!BC.MIB->getEHInfo(*CTCInstr) &&
"found tail call with associated landing pad") ? void (0) : __assert_fail
("!BC.MIB->getEHInfo(*CTCInstr) && \"found tail call with associated landing pad\""
, "bolt/lib/Core/BinaryFunction.cpp", 2300, __extension__ __PRETTY_FUNCTION__
))
2300 "found tail call with associated landing pad")(static_cast <bool> (!BC.MIB->getEHInfo(*CTCInstr) &&
"found tail call with associated landing pad") ? void (0) : __assert_fail
("!BC.MIB->getEHInfo(*CTCInstr) && \"found tail call with associated landing pad\""
, "bolt/lib/Core/BinaryFunction.cpp", 2300, __extension__ __PRETTY_FUNCTION__
))
;
2301
2302 // Create a basic block with an unconditional tail call instruction using
2303 // the same destination.
2304 const MCSymbol *CTCTargetLabel = BC.MIB->getTargetSymbol(*CTCInstr);
2305 assert(CTCTargetLabel && "symbol expected for conditional tail call")(static_cast <bool> (CTCTargetLabel && "symbol expected for conditional tail call"
) ? void (0) : __assert_fail ("CTCTargetLabel && \"symbol expected for conditional tail call\""
, "bolt/lib/Core/BinaryFunction.cpp", 2305, __extension__ __PRETTY_FUNCTION__
))
;
2306 MCInst TailCallInstr;
2307 BC.MIB->createTailCall(TailCallInstr, CTCTargetLabel, BC.Ctx.get());
2308 // Link new BBs to the original input offset of the BB where the CTC
2309 // is, so we can map samples recorded in new BBs back to the original BB
2310 // seem in the input binary (if using BAT)
2311 std::unique_ptr<BinaryBasicBlock> TailCallBB =
2312 createBasicBlock(BC.Ctx->createNamedTempSymbol("TC"));
2313 TailCallBB->setOffset(BB.getInputOffset());
2314 TailCallBB->addInstruction(TailCallInstr);
2315 TailCallBB->setCFIState(CFIStateBeforeCTC);
2316
2317 // Add CFG edge with profile info from BB to TailCallBB.
2318 BB.addSuccessor(TailCallBB.get(), CTCTakenCount, CTCMispredCount);
2319
2320 // Add execution count for the block.
2321 TailCallBB->setExecutionCount(CTCTakenCount);
2322
2323 BC.MIB->convertTailCallToJmp(*CTCInstr);
2324
2325 BC.MIB->replaceBranchTarget(*CTCInstr, TailCallBB->getLabel(),
2326 BC.Ctx.get());
2327
2328 // Add basic block to the list that will be added to the end.
2329 NewBlocks.emplace_back(std::move(TailCallBB));
2330
2331 // Swap edges as the TailCallBB corresponds to the taken branch.
2332 BB.swapConditionalSuccessors();
2333
2334 // This branch is no longer a conditional tail call.
2335 BC.MIB->unsetConditionalTailCall(*CTCInstr);
2336 }
2337
2338 insertBasicBlocks(std::prev(end()), std::move(NewBlocks),
2339 /* UpdateLayout */ true,
2340 /* UpdateCFIState */ false);
2341}
2342
2343uint64_t BinaryFunction::getFunctionScore() const {
2344 if (FunctionScore != -1)
2345 return FunctionScore;
2346
2347 if (!isSimple() || !hasValidProfile()) {
2348 FunctionScore = 0;
2349 return FunctionScore;
2350 }
2351
2352 uint64_t TotalScore = 0ULL;
2353 for (const BinaryBasicBlock &BB : blocks()) {
2354 uint64_t BBExecCount = BB.getExecutionCount();
2355 if (BBExecCount == BinaryBasicBlock::COUNT_NO_PROFILE)
2356 continue;
2357 TotalScore += BBExecCount * BB.getNumNonPseudos();
2358 }
2359 FunctionScore = TotalScore;
2360 return FunctionScore;
2361}
2362
2363void BinaryFunction::annotateCFIState() {
2364 assert(CurrentState == State::Disassembled && "unexpected function state")(static_cast <bool> (CurrentState == State::Disassembled
&& "unexpected function state") ? void (0) : __assert_fail
("CurrentState == State::Disassembled && \"unexpected function state\""
, "bolt/lib/Core/BinaryFunction.cpp", 2364, __extension__ __PRETTY_FUNCTION__
))
;
2365 assert(!BasicBlocks.empty() && "basic block list should not be empty")(static_cast <bool> (!BasicBlocks.empty() && "basic block list should not be empty"
) ? void (0) : __assert_fail ("!BasicBlocks.empty() && \"basic block list should not be empty\""
, "bolt/lib/Core/BinaryFunction.cpp", 2365, __extension__ __PRETTY_FUNCTION__
))
;
2366
2367 // This is an index of the last processed CFI in FDE CFI program.
2368 uint32_t State = 0;
2369
2370 // This is an index of RememberState CFI reflecting effective state right
2371 // after execution of RestoreState CFI.
2372 //
2373 // It differs from State iff the CFI at (State-1)
2374 // was RestoreState (modulo GNU_args_size CFIs, which are ignored).
2375 //
2376 // This allows us to generate shorter replay sequences when producing new
2377 // CFI programs.
2378 uint32_t EffectiveState = 0;
2379
2380 // For tracking RememberState/RestoreState sequences.
2381 std::stack<uint32_t> StateStack;
2382
2383 for (BinaryBasicBlock *BB : BasicBlocks) {
2384 BB->setCFIState(EffectiveState);
2385
2386 for (const MCInst &Instr : *BB) {
2387 const MCCFIInstruction *CFI = getCFIFor(Instr);
2388 if (!CFI)
2389 continue;
2390
2391 ++State;
2392
2393 switch (CFI->getOperation()) {
2394 case MCCFIInstruction::OpRememberState:
2395 StateStack.push(EffectiveState);
2396 EffectiveState = State;
2397 break;
2398 case MCCFIInstruction::OpRestoreState:
2399 assert(!StateStack.empty() && "corrupt CFI stack")(static_cast <bool> (!StateStack.empty() && "corrupt CFI stack"
) ? void (0) : __assert_fail ("!StateStack.empty() && \"corrupt CFI stack\""
, "bolt/lib/Core/BinaryFunction.cpp", 2399, __extension__ __PRETTY_FUNCTION__
))
;
2400 EffectiveState = StateStack.top();
2401 StateStack.pop();
2402 break;
2403 case MCCFIInstruction::OpGnuArgsSize:
2404 // OpGnuArgsSize CFIs do not affect the CFI state.
2405 break;
2406 default:
2407 // Any other CFI updates the state.
2408 EffectiveState = State;
2409 break;
2410 }
2411 }
2412 }
2413
2414 assert(StateStack.empty() && "corrupt CFI stack")(static_cast <bool> (StateStack.empty() && "corrupt CFI stack"
) ? void (0) : __assert_fail ("StateStack.empty() && \"corrupt CFI stack\""
, "bolt/lib/Core/BinaryFunction.cpp", 2414, __extension__ __PRETTY_FUNCTION__
))
;
2415}
2416
2417namespace {
2418
2419/// Our full interpretation of a DWARF CFI machine state at a given point
2420struct CFISnapshot {
2421 /// CFA register number and offset defining the canonical frame at this
2422 /// point, or the number of a rule (CFI state) that computes it with a
2423 /// DWARF expression. This number will be negative if it refers to a CFI
2424 /// located in the CIE instead of the FDE.
2425 uint32_t CFAReg;
2426 int32_t CFAOffset;
2427 int32_t CFARule;
2428 /// Mapping of rules (CFI states) that define the location of each
2429 /// register. If absent, no rule defining the location of such register
2430 /// was ever read. This number will be negative if it refers to a CFI
2431 /// located in the CIE instead of the FDE.
2432 DenseMap<int32_t, int32_t> RegRule;
2433
2434 /// References to CIE, FDE and expanded instructions after a restore state
2435 const BinaryFunction::CFIInstrMapType &CIE;
2436 const BinaryFunction::CFIInstrMapType &FDE;
2437 const DenseMap<int32_t, SmallVector<int32_t, 4>> &FrameRestoreEquivalents;
2438
2439 /// Current FDE CFI number representing the state where the snapshot is at
2440 int32_t CurState;
2441
2442 /// Used when we don't have information about which state/rule to apply
2443 /// to recover the location of either the CFA or a specific register
2444 constexpr static int32_t UNKNOWN = std::numeric_limits<int32_t>::min();
2445
2446private:
2447 /// Update our snapshot by executing a single CFI
2448 void update(const MCCFIInstruction &Instr, int32_t RuleNumber) {
2449 switch (Instr.getOperation()) {
2450 case MCCFIInstruction::OpSameValue:
2451 case MCCFIInstruction::OpRelOffset:
2452 case MCCFIInstruction::OpOffset:
2453 case MCCFIInstruction::OpRestore:
2454 case MCCFIInstruction::OpUndefined:
2455 case MCCFIInstruction::OpRegister:
2456 RegRule[Instr.getRegister()] = RuleNumber;
2457 break;
2458 case MCCFIInstruction::OpDefCfaRegister:
2459 CFAReg = Instr.getRegister();
2460 CFARule = UNKNOWN;
2461 break;
2462 case MCCFIInstruction::OpDefCfaOffset:
2463 CFAOffset = Instr.getOffset();
2464 CFARule = UNKNOWN;
2465 break;
2466 case MCCFIInstruction::OpDefCfa:
2467 CFAReg = Instr.getRegister();
2468 CFAOffset = Instr.getOffset();
2469 CFARule = UNKNOWN;
2470 break;
2471 case MCCFIInstruction::OpEscape: {
2472 std::optional<uint8_t> Reg =
2473 readDWARFExpressionTargetReg(Instr.getValues());
2474 // Handle DW_CFA_def_cfa_expression
2475 if (!Reg) {
2476 CFARule = RuleNumber;
2477 break;
2478 }
2479 RegRule[*Reg] = RuleNumber;
2480 break;
2481 }
2482 case MCCFIInstruction::OpAdjustCfaOffset:
2483 case MCCFIInstruction::OpWindowSave:
2484 case MCCFIInstruction::OpNegateRAState:
2485 case MCCFIInstruction::OpLLVMDefAspaceCfa:
2486 llvm_unreachable("unsupported CFI opcode")::llvm::llvm_unreachable_internal("unsupported CFI opcode", "bolt/lib/Core/BinaryFunction.cpp"
, 2486)
;
2487 break;
2488 case MCCFIInstruction::OpRememberState:
2489 case MCCFIInstruction::OpRestoreState:
2490 case MCCFIInstruction::OpGnuArgsSize:
2491 // do not affect CFI state
2492 break;
2493 }
2494 }
2495
2496public:
2497 /// Advance state reading FDE CFI instructions up to State number
2498 void advanceTo(int32_t State) {
2499 for (int32_t I = CurState, E = State; I != E; ++I) {
2500 const MCCFIInstruction &Instr = FDE[I];
2501 if (Instr.getOperation() != MCCFIInstruction::OpRestoreState) {
2502 update(Instr, I);
2503 continue;
2504 }
2505 // If restore state instruction, fetch the equivalent CFIs that have
2506 // the same effect of this restore. This is used to ensure remember-
2507 // restore pairs are completely removed.
2508 auto Iter = FrameRestoreEquivalents.find(I);
2509 if (Iter == FrameRestoreEquivalents.end())
2510 continue;
2511 for (int32_t RuleNumber : Iter->second)
2512 update(FDE[RuleNumber], RuleNumber);
2513 }
2514
2515 assert(((CFAReg != (uint32_t)UNKNOWN && CFAOffset != UNKNOWN) ||(static_cast <bool> (((CFAReg != (uint32_t)UNKNOWN &&
CFAOffset != UNKNOWN) || CFARule != UNKNOWN) && "CIE did not define default CFA?"
) ? void (0) : __assert_fail ("((CFAReg != (uint32_t)UNKNOWN && CFAOffset != UNKNOWN) || CFARule != UNKNOWN) && \"CIE did not define default CFA?\""
, "bolt/lib/Core/BinaryFunction.cpp", 2517, __extension__ __PRETTY_FUNCTION__
))
2516 CFARule != UNKNOWN) &&(static_cast <bool> (((CFAReg != (uint32_t)UNKNOWN &&
CFAOffset != UNKNOWN) || CFARule != UNKNOWN) && "CIE did not define default CFA?"
) ? void (0) : __assert_fail ("((CFAReg != (uint32_t)UNKNOWN && CFAOffset != UNKNOWN) || CFARule != UNKNOWN) && \"CIE did not define default CFA?\""
, "bolt/lib/Core/BinaryFunction.cpp", 2517, __extension__ __PRETTY_FUNCTION__
))
2517 "CIE did not define default CFA?")(static_cast <bool> (((CFAReg != (uint32_t)UNKNOWN &&
CFAOffset != UNKNOWN) || CFARule != UNKNOWN) && "CIE did not define default CFA?"
) ? void (0) : __assert_fail ("((CFAReg != (uint32_t)UNKNOWN && CFAOffset != UNKNOWN) || CFARule != UNKNOWN) && \"CIE did not define default CFA?\""
, "bolt/lib/Core/BinaryFunction.cpp", 2517, __extension__ __PRETTY_FUNCTION__
))
;
2518
2519 CurState = State;
2520 }
2521
2522 /// Interpret all CIE and FDE instructions up until CFI State number and
2523 /// populate this snapshot
2524 CFISnapshot(
2525 const BinaryFunction::CFIInstrMapType &CIE,
2526 const BinaryFunction::CFIInstrMapType &FDE,
2527 const DenseMap<int32_t, SmallVector<int32_t, 4>> &FrameRestoreEquivalents,
2528 int32_t State)
2529 : CIE(CIE), FDE(FDE), FrameRestoreEquivalents(FrameRestoreEquivalents) {
2530 CFAReg = UNKNOWN;
2531 CFAOffset = UNKNOWN;
2532 CFARule = UNKNOWN;
2533 CurState = 0;
2534
2535 for (int32_t I = 0, E = CIE.size(); I != E; ++I) {
2536 const MCCFIInstruction &Instr = CIE[I];
2537 update(Instr, -I);
2538 }
2539
2540 advanceTo(State);
2541 }
2542};
2543
2544/// A CFI snapshot with the capability of checking if incremental additions to
2545/// it are redundant. This is used to ensure we do not emit two CFI instructions
2546/// back-to-back that are doing the same state change, or to avoid emitting a
2547/// CFI at all when the state at that point would not be modified after that CFI
2548struct CFISnapshotDiff : public CFISnapshot {
2549 bool RestoredCFAReg{false};
2550 bool RestoredCFAOffset{false};
2551 DenseMap<int32_t, bool> RestoredRegs;
2552
2553 CFISnapshotDiff(const CFISnapshot &S) : CFISnapshot(S) {}
2554
2555 CFISnapshotDiff(
2556 const BinaryFunction::CFIInstrMapType &CIE,
2557 const BinaryFunction::CFIInstrMapType &FDE,
2558 const DenseMap<int32_t, SmallVector<int32_t, 4>> &FrameRestoreEquivalents,
2559 int32_t State)
2560 : CFISnapshot(CIE, FDE, FrameRestoreEquivalents, State) {}
2561
2562 /// Return true if applying Instr to this state is redundant and can be
2563 /// dismissed.
2564 bool isRedundant(const MCCFIInstruction &Instr) {
2565 switch (Instr.getOperation()) {
2566 case MCCFIInstruction::OpSameValue:
2567 case MCCFIInstruction::OpRelOffset:
2568 case MCCFIInstruction::OpOffset:
2569 case MCCFIInstruction::OpRestore:
2570 case MCCFIInstruction::OpUndefined:
2571 case MCCFIInstruction::OpRegister:
2572 case MCCFIInstruction::OpEscape: {
2573 uint32_t Reg;
2574 if (Instr.getOperation() != MCCFIInstruction::OpEscape) {
2575 Reg = Instr.getRegister();
2576 } else {
2577 std::optional<uint8_t> R =
2578 readDWARFExpressionTargetReg(Instr.getValues());
2579 // Handle DW_CFA_def_cfa_expression
2580 if (!R) {
2581 if (RestoredCFAReg && RestoredCFAOffset)
2582 return true;
2583 RestoredCFAReg = true;
2584 RestoredCFAOffset = true;
2585 return false;
2586 }
2587 Reg = *R;
2588 }
2589 if (RestoredRegs[Reg])
2590 return true;
2591 RestoredRegs[Reg] = true;
2592 const int32_t CurRegRule =
2593 RegRule.find(Reg) != RegRule.end() ? RegRule[Reg] : UNKNOWN;
2594 if (CurRegRule == UNKNOWN) {
2595 if (Instr.getOperation() == MCCFIInstruction::OpRestore ||
2596 Instr.getOperation() == MCCFIInstruction::OpSameValue)
2597 return true;
2598 return false;
2599 }
2600 const MCCFIInstruction &LastDef =
2601 CurRegRule < 0 ? CIE[-CurRegRule] : FDE[CurRegRule];
2602 return LastDef == Instr;
2603 }
2604 case MCCFIInstruction::OpDefCfaRegister:
2605 if (RestoredCFAReg)
2606 return true;
2607 RestoredCFAReg = true;
2608 return CFAReg == Instr.getRegister();
2609 case MCCFIInstruction::OpDefCfaOffset:
2610 if (RestoredCFAOffset)
2611 return true;
2612 RestoredCFAOffset = true;
2613 return CFAOffset == Instr.getOffset();
2614 case MCCFIInstruction::OpDefCfa:
2615 if (RestoredCFAReg && RestoredCFAOffset)
2616 return true;
2617 RestoredCFAReg = true;
2618 RestoredCFAOffset = true;
2619 return CFAReg == Instr.getRegister() && CFAOffset == Instr.getOffset();
2620 case MCCFIInstruction::OpAdjustCfaOffset:
2621 case MCCFIInstruction::OpWindowSave:
2622 case MCCFIInstruction::OpNegateRAState:
2623 case MCCFIInstruction::OpLLVMDefAspaceCfa:
2624 llvm_unreachable("unsupported CFI opcode")::llvm::llvm_unreachable_internal("unsupported CFI opcode", "bolt/lib/Core/BinaryFunction.cpp"
, 2624)
;
2625 return false;
2626 case MCCFIInstruction::OpRememberState:
2627 case MCCFIInstruction::OpRestoreState:
2628 case MCCFIInstruction::OpGnuArgsSize:
2629 // do not affect CFI state
2630 return true;
2631 }
2632 return false;
2633 }
2634};
2635
2636} // end anonymous namespace
2637
2638bool BinaryFunction::replayCFIInstrs(int32_t FromState, int32_t ToState,
2639 BinaryBasicBlock *InBB,
2640 BinaryBasicBlock::iterator InsertIt) {
2641 if (FromState == ToState)
2642 return true;
2643 assert(FromState < ToState && "can only replay CFIs forward")(static_cast <bool> (FromState < ToState && "can only replay CFIs forward"
) ? void (0) : __assert_fail ("FromState < ToState && \"can only replay CFIs forward\""
, "bolt/lib/Core/BinaryFunction.cpp", 2643, __extension__ __PRETTY_FUNCTION__
))
;
2644
2645 CFISnapshotDiff CFIDiff(CIEFrameInstructions, FrameInstructions,
2646 FrameRestoreEquivalents, FromState);
2647
2648 std::vector<uint32_t> NewCFIs;
2649 for (int32_t CurState = FromState; CurState < ToState; ++CurState) {
2650 MCCFIInstruction *Instr = &FrameInstructions[CurState];
2651 if (Instr->getOperation() == MCCFIInstruction::OpRestoreState) {
2652 auto Iter = FrameRestoreEquivalents.find(CurState);
2653 assert(Iter != FrameRestoreEquivalents.end())(static_cast <bool> (Iter != FrameRestoreEquivalents.end
()) ? void (0) : __assert_fail ("Iter != FrameRestoreEquivalents.end()"
, "bolt/lib/Core/BinaryFunction.cpp", 2653, __extension__ __PRETTY_FUNCTION__
))
;
2654 NewCFIs.insert(NewCFIs.end(), Iter->second.begin(), Iter->second.end());
2655 // RestoreState / Remember will be filtered out later by CFISnapshotDiff,
2656 // so we might as well fall-through here.
2657 }
2658 NewCFIs.push_back(CurState);
2659 }
2660
2661 // Replay instructions while avoiding duplicates
2662 for (int32_t State : llvm::reverse(NewCFIs)) {
2663 if (CFIDiff.isRedundant(FrameInstructions[State]))
2664 continue;
2665 InsertIt = addCFIPseudo(InBB, InsertIt, State);
2666 }
2667
2668 return true;
2669}
2670
2671SmallVector<int32_t, 4>
2672BinaryFunction::unwindCFIState(int32_t FromState, int32_t ToState,
2673 BinaryBasicBlock *InBB,
2674 BinaryBasicBlock::iterator &InsertIt) {
2675 SmallVector<int32_t, 4> NewStates;
2676
2677 CFISnapshot ToCFITable(CIEFrameInstructions, FrameInstructions,
2678 FrameRestoreEquivalents, ToState);
2679 CFISnapshotDiff FromCFITable(ToCFITable);
2680 FromCFITable.advanceTo(FromState);
2681
2682 auto undoStateDefCfa = [&]() {
2683 if (ToCFITable.CFARule == CFISnapshot::UNKNOWN) {
2684 FrameInstructions.emplace_back(MCCFIInstruction::cfiDefCfa(
2685 nullptr, ToCFITable.CFAReg, ToCFITable.CFAOffset));
2686 if (FromCFITable.isRedundant(FrameInstructions.back())) {
2687 FrameInstructions.pop_back();
2688 return;
2689 }
2690 NewStates.push_back(FrameInstructions.size() - 1);
2691 InsertIt = addCFIPseudo(InBB, InsertIt, FrameInstructions.size() - 1);
2692 ++InsertIt;
2693 } else if (ToCFITable.CFARule < 0) {
2694 if (FromCFITable.isRedundant(CIEFrameInstructions[-ToCFITable.CFARule]))
2695 return;
2696 NewStates.push_back(FrameInstructions.size());
2697 InsertIt = addCFIPseudo(InBB, InsertIt, FrameInstructions.size());
2698 ++InsertIt;
2699 FrameInstructions.emplace_back(CIEFrameInstructions[-ToCFITable.CFARule]);
2700 } else if (!FromCFITable.isRedundant(
2701 FrameInstructions[ToCFITable.CFARule])) {
2702 NewStates.push_back(ToCFITable.CFARule);
2703 InsertIt = addCFIPseudo(InBB, InsertIt, ToCFITable.CFARule);
2704 ++InsertIt;
2705 }
2706 };
2707
2708 auto undoState = [&](const MCCFIInstruction &Instr) {
2709 switch (Instr.getOperation()) {
2710 case MCCFIInstruction::OpRememberState:
2711 case MCCFIInstruction::OpRestoreState:
2712 break;
2713 case MCCFIInstruction::OpSameValue:
2714 case MCCFIInstruction::OpRelOffset:
2715 case MCCFIInstruction::OpOffset:
2716 case MCCFIInstruction::OpRestore:
2717 case MCCFIInstruction::OpUndefined:
2718 case MCCFIInstruction::OpEscape:
2719 case MCCFIInstruction::OpRegister: {
2720 uint32_t Reg;
2721 if (Instr.getOperation() != MCCFIInstruction::OpEscape) {
2722 Reg = Instr.getRegister();
2723 } else {
2724 std::optional<uint8_t> R =
2725 readDWARFExpressionTargetReg(Instr.getValues());
2726 // Handle DW_CFA_def_cfa_expression
2727 if (!R) {
2728 undoStateDefCfa();
2729 return;
2730 }
2731 Reg = *R;
2732 }
2733
2734 if (ToCFITable.RegRule.find(Reg) == ToCFITable.RegRule.end()) {
2735 FrameInstructions.emplace_back(
2736 MCCFIInstruction::createRestore(nullptr, Reg));
2737 if (FromCFITable.isRedundant(FrameInstructions.back())) {
2738 FrameInstructions.pop_back();
2739 break;
2740 }
2741 NewStates.push_back(FrameInstructions.size() - 1);
2742 InsertIt = addCFIPseudo(InBB, InsertIt, FrameInstructions.size() - 1);
2743 ++InsertIt;
2744 break;
2745 }
2746 const int32_t Rule = ToCFITable.RegRule[Reg];
2747 if (Rule < 0) {
2748 if (FromCFITable.isRedundant(CIEFrameInstructions[-Rule]))
2749 break;
2750 NewStates.push_back(FrameInstructions.size());
2751 InsertIt = addCFIPseudo(InBB, InsertIt, FrameInstructions.size());
2752 ++InsertIt;
2753 FrameInstructions.emplace_back(CIEFrameInstructions[-Rule]);
2754 break;
2755 }
2756 if (FromCFITable.isRedundant(FrameInstructions[Rule]))
2757 break;
2758 NewStates.push_back(Rule);
2759 InsertIt = addCFIPseudo(InBB, InsertIt, Rule);
2760 ++InsertIt;
2761 break;
2762 }
2763 case MCCFIInstruction::OpDefCfaRegister:
2764 case MCCFIInstruction::OpDefCfaOffset:
2765 case MCCFIInstruction::OpDefCfa:
2766 undoStateDefCfa();
2767 break;
2768 case MCCFIInstruction::OpAdjustCfaOffset:
2769 case MCCFIInstruction::OpWindowSave:
2770 case MCCFIInstruction::OpNegateRAState:
2771 case MCCFIInstruction::OpLLVMDefAspaceCfa:
2772 llvm_unreachable("unsupported CFI opcode")::llvm::llvm_unreachable_internal("unsupported CFI opcode", "bolt/lib/Core/BinaryFunction.cpp"
, 2772)
;
2773 break;
2774 case MCCFIInstruction::OpGnuArgsSize:
2775 // do not affect CFI state
2776 break;
2777 }
2778 };
2779
2780 // Undo all modifications from ToState to FromState
2781 for (int32_t I = ToState, E = FromState; I != E; ++I) {
2782 const MCCFIInstruction &Instr = FrameInstructions[I];
2783 if (Instr.getOperation() != MCCFIInstruction::OpRestoreState) {
2784 undoState(Instr);
2785 continue;
2786 }
2787 auto Iter = FrameRestoreEquivalents.find(I);
2788 if (Iter == FrameRestoreEquivalents.end())
2789 continue;
2790 for (int32_t State : Iter->second)
2791 undoState(FrameInstructions[State]);
2792 }
2793
2794 return NewStates;
2795}
2796
2797void BinaryFunction::normalizeCFIState() {
2798 // Reordering blocks with remember-restore state instructions can be specially
2799 // tricky. When rewriting the CFI, we omit remember-restore state instructions
2800 // entirely. For restore state, we build a map expanding each restore to the
2801 // equivalent unwindCFIState sequence required at that point to achieve the
2802 // same effect of the restore. All remember state are then just ignored.
2803 std::stack<int32_t> Stack;
2804 for (BinaryBasicBlock *CurBB : Layout.blocks()) {
2805 for (auto II = CurBB->begin(); II != CurBB->end(); ++II) {
2806 if (const MCCFIInstruction *CFI = getCFIFor(*II)) {
2807 if (CFI->getOperation() == MCCFIInstruction::OpRememberState) {
2808 Stack.push(II->getOperand(0).getImm());
2809 continue;
2810 }
2811 if (CFI->getOperation() == MCCFIInstruction::OpRestoreState) {
2812 const int32_t RememberState = Stack.top();
2813 const int32_t CurState = II->getOperand(0).getImm();
2814 FrameRestoreEquivalents[CurState] =
2815 unwindCFIState(CurState, RememberState, CurBB, II);
2816 Stack.pop();
2817 }
2818 }
2819 }
2820 }
2821}
2822
2823bool BinaryFunction::finalizeCFIState() {
2824 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "Trying to fix CFI states for each BB after reordering.\n"
; } } while (false)
2825 dbgs() << "Trying to fix CFI states for each BB after reordering.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "Trying to fix CFI states for each BB after reordering.\n"
; } } while (false)
;
2826 LLVM_DEBUG(dbgs() << "This is the list of CFI states for each BB of " << *thisdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "This is the list of CFI states for each BB of "
<< *this << ": "; } } while (false)
2827 << ": ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "This is the list of CFI states for each BB of "
<< *this << ": "; } } while (false)
;
2828
2829 const char *Sep = "";
2830 (void)Sep;
2831 for (FunctionFragment &FF : Layout.fragments()) {
2832 // Hot-cold border: at start of each region (with a different FDE) we need
2833 // to reset the CFI state.
2834 int32_t State = 0;
2835
2836 for (BinaryBasicBlock *BB : FF) {
2837 const int32_t CFIStateAtExit = BB->getCFIStateAtExit();
2838
2839 // We need to recover the correct state if it doesn't match expected
2840 // state at BB entry point.
2841 if (BB->getCFIState() < State) {
2842 // In this case, State is currently higher than what this BB expect it
2843 // to be. To solve this, we need to insert CFI instructions to undo
2844 // the effect of all CFI from BB's state to current State.
2845 auto InsertIt = BB->begin();
2846 unwindCFIState(State, BB->getCFIState(), BB, InsertIt);
2847 } else if (BB->getCFIState() > State) {
2848 // If BB's CFI state is greater than State, it means we are behind in
2849 // the state. Just emit all instructions to reach this state at the
2850 // beginning of this BB. If this sequence of instructions involve
2851 // remember state or restore state, bail out.
2852 if (!replayCFIInstrs(State, BB->getCFIState(), BB, BB->begin()))
2853 return false;
2854 }
2855
2856 State = CFIStateAtExit;
2857 LLVM_DEBUG(dbgs() << Sep << State; Sep = ", ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << Sep << State; Sep = ", "; }
} while (false)
;
2858 }
2859 }
2860 LLVM_DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "\n"; } } while (false)
;
2861
2862 for (BinaryBasicBlock &BB : blocks()) {
2863 for (auto II = BB.begin(); II != BB.end();) {
2864 const MCCFIInstruction *CFI = getCFIFor(*II);
2865 if (CFI && (CFI->getOperation() == MCCFIInstruction::OpRememberState ||
2866 CFI->getOperation() == MCCFIInstruction::OpRestoreState)) {
2867 II = BB.eraseInstruction(II);
2868 } else {
2869 ++II;
2870 }
2871 }
2872 }
2873
2874 return true;
2875}
2876
2877bool BinaryFunction::requiresAddressTranslation() const {
2878 return opts::EnableBAT || hasSDTMarker() || hasPseudoProbe();
2879}
2880
2881uint64_t BinaryFunction::getInstructionCount() const {
2882 uint64_t Count = 0;
2883 for (const BinaryBasicBlock &BB : blocks())
2884 Count += BB.getNumNonPseudos();
2885 return Count;
2886}
2887
2888void BinaryFunction::clearDisasmState() {
2889 clearList(Instructions);
2890 clearList(IgnoredBranches);
2891 clearList(TakenBranches);
2892
2893 if (BC.HasRelocations) {
2894 for (std::pair<const uint32_t, MCSymbol *> &LI : Labels)
2895 BC.UndefinedSymbols.insert(LI.second);
2896 for (MCSymbol *const EndLabel : FunctionEndLabels)
2897 if (EndLabel)
2898 BC.UndefinedSymbols.insert(EndLabel);
2899 }
2900}
2901
2902void BinaryFunction::setTrapOnEntry() {
2903 clearDisasmState();
2904
2905 forEachEntryPoint([&](uint64_t Offset, const MCSymbol *Label) -> bool {
2906 MCInst TrapInstr;
2907 BC.MIB->createTrap(TrapInstr);
2908 addInstruction(Offset, std::move(TrapInstr));
2909 return true;
2910 });
2911
2912 TrapsOnEntry = true;
2913}
2914
2915void BinaryFunction::setIgnored() {
2916 if (opts::processAllFunctions()) {
2917 // We can accept ignored functions before they've been disassembled.
2918 // In that case, they would still get disassembled and emited, but not
2919 // optimized.
2920 assert(CurrentState == State::Empty &&(static_cast <bool> (CurrentState == State::Empty &&
"cannot ignore non-empty functions in current mode") ? void (
0) : __assert_fail ("CurrentState == State::Empty && \"cannot ignore non-empty functions in current mode\""
, "bolt/lib/Core/BinaryFunction.cpp", 2921, __extension__ __PRETTY_FUNCTION__
))
2921 "cannot ignore non-empty functions in current mode")(static_cast <bool> (CurrentState == State::Empty &&
"cannot ignore non-empty functions in current mode") ? void (
0) : __assert_fail ("CurrentState == State::Empty && \"cannot ignore non-empty functions in current mode\""
, "bolt/lib/Core/BinaryFunction.cpp", 2921, __extension__ __PRETTY_FUNCTION__
))
;
2922 IsIgnored = true;
2923 return;
2924 }
2925
2926 clearDisasmState();
2927
2928 // Clear CFG state too.
2929 if (hasCFG()) {
2930 releaseCFG();
2931
2932 for (BinaryBasicBlock *BB : BasicBlocks)
2933 delete BB;
2934 clearList(BasicBlocks);
2935
2936 for (BinaryBasicBlock *BB : DeletedBasicBlocks)
2937 delete BB;
2938 clearList(DeletedBasicBlocks);
2939
2940 Layout.clear();
2941 }
2942
2943 CurrentState = State::Empty;
2944
2945 IsIgnored = true;
2946 IsSimple = false;
2947 LLVM_DEBUG(dbgs() << "Ignoring " << getPrintName() << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "Ignoring " << getPrintName
() << '\n'; } } while (false)
;
2948}
2949
2950void BinaryFunction::duplicateConstantIslands() {
2951 assert(Islands && "function expected to have constant islands")(static_cast <bool> (Islands && "function expected to have constant islands"
) ? void (0) : __assert_fail ("Islands && \"function expected to have constant islands\""
, "bolt/lib/Core/BinaryFunction.cpp", 2951, __extension__ __PRETTY_FUNCTION__
))
;
2952
2953 for (BinaryBasicBlock *BB : getLayout().blocks()) {
2954 if (!BB->isCold())
2955 continue;
2956
2957 for (MCInst &Inst : *BB) {
2958 int OpNum = 0;
2959 for (MCOperand &Operand : Inst) {
2960 if (!Operand.isExpr()) {
2961 ++OpNum;
2962 continue;
2963 }
2964 const MCSymbol *Symbol = BC.MIB->getTargetSymbol(Inst, OpNum);
2965 // Check if this is an island symbol
2966 if (!Islands->Symbols.count(Symbol) &&
2967 !Islands->ProxySymbols.count(Symbol))
2968 continue;
2969
2970 // Create cold symbol, if missing
2971 auto ISym = Islands->ColdSymbols.find(Symbol);
2972 MCSymbol *ColdSymbol;
2973 if (ISym != Islands->ColdSymbols.end()) {
2974 ColdSymbol = ISym->second;
2975 } else {
2976 ColdSymbol = BC.Ctx->getOrCreateSymbol(Symbol->getName() + ".cold");
2977 Islands->ColdSymbols[Symbol] = ColdSymbol;
2978 // Check if this is a proxy island symbol and update owner proxy map
2979 if (Islands->ProxySymbols.count(Symbol)) {
2980 BinaryFunction *Owner = Islands->ProxySymbols[Symbol];
2981 auto IProxiedSym = Owner->Islands->Proxies[this].find(Symbol);
2982 Owner->Islands->ColdProxies[this][IProxiedSym->second] = ColdSymbol;
2983 }
2984 }
2985
2986 // Update instruction reference
2987 Operand = MCOperand::createExpr(BC.MIB->getTargetExprFor(
2988 Inst,
2989 MCSymbolRefExpr::create(ColdSymbol, MCSymbolRefExpr::VK_None,
2990 *BC.Ctx),
2991 *BC.Ctx, 0));
2992 ++OpNum;
2993 }
2994 }
2995 }
2996}
2997
2998#ifndef MAX_PATH255
2999#define MAX_PATH255 255
3000#endif
3001
3002static std::string constructFilename(std::string Filename,
3003 std::string Annotation,
3004 std::string Suffix) {
3005 std::replace(Filename.begin(), Filename.end(), '/', '-');
3006 if (!Annotation.empty())
3007 Annotation.insert(0, "-");
3008 if (Filename.size() + Annotation.size() + Suffix.size() > MAX_PATH255) {
3009 assert(Suffix.size() + Annotation.size() <= MAX_PATH)(static_cast <bool> (Suffix.size() + Annotation.size() <=
255) ? void (0) : __assert_fail ("Suffix.size() + Annotation.size() <= MAX_PATH"
, "bolt/lib/Core/BinaryFunction.cpp", 3009, __extension__ __PRETTY_FUNCTION__
))
;
3010 if (opts::Verbosity >= 1) {
3011 errs() << "BOLT-WARNING: Filename \"" << Filename << Annotation << Suffix
3012 << "\" exceeds the " << MAX_PATH255 << " size limit, truncating.\n";
3013 }
3014 Filename.resize(MAX_PATH255 - (Suffix.size() + Annotation.size()));
3015 }
3016 Filename += Annotation;
3017 Filename += Suffix;
3018 return Filename;
3019}
3020
3021static std::string formatEscapes(const std::string &Str) {
3022 std::string Result;
3023 for (unsigned I = 0; I < Str.size(); ++I) {
3024 char C = Str[I];
3025 switch (C) {
3026 case '\n':
3027 Result += "&#13;";
3028 break;
3029 case '"':
3030 break;
3031 default:
3032 Result += C;
3033 break;
3034 }
3035 }
3036 return Result;
3037}
3038
3039void BinaryFunction::dumpGraph(raw_ostream &OS) const {
3040 OS << "digraph \"" << getPrintName() << "\" {\n"
3041 << "node [fontname=courier, shape=box, style=filled, colorscheme=brbg9]\n";
3042 uint64_t Offset = Address;
3043 for (BinaryBasicBlock *BB : BasicBlocks) {
3044 auto LayoutPos = find(Layout.blocks(), BB);
3045 unsigned LayoutIndex = LayoutPos - Layout.block_begin();
3046 const char *ColdStr = BB->isCold() ? " (cold)" : "";
3047 std::vector<std::string> Attrs;
3048 // Bold box for entry points
3049 if (isEntryPoint(*BB))
3050 Attrs.push_back("penwidth=2");
3051 if (BLI && BLI->getLoopFor(BB)) {
3052 // Distinguish innermost loops
3053 const BinaryLoop *Loop = BLI->getLoopFor(BB);
3054 if (Loop->isInnermost())
3055 Attrs.push_back("fillcolor=6");
3056 else // some outer loop
3057 Attrs.push_back("fillcolor=4");
3058 } else { // non-loopy code
3059 Attrs.push_back("fillcolor=5");
3060 }
3061 ListSeparator LS;
3062 OS << "\"" << BB->getName() << "\" [";
3063 for (StringRef Attr : Attrs)
3064 OS << LS << Attr;
3065 OS << "]\n";
3066 OS << format("\"%s\" [label=\"%s%s\\n(C:%lu,O:%lu,I:%u,L:%u,CFI:%u)\\n",
3067 BB->getName().data(), BB->getName().data(), ColdStr,
3068 BB->getKnownExecutionCount(), BB->getOffset(), getIndex(BB),
3069 LayoutIndex, BB->getCFIState());
3070
3071 if (opts::DotToolTipCode) {
3072 std::string Str;
3073 raw_string_ostream CS(Str);
3074 Offset = BC.printInstructions(CS, BB->begin(), BB->end(), Offset, this,
3075 /* PrintMCInst = */ false,
3076 /* PrintMemData = */ false,
3077 /* PrintRelocations = */ false,
3078 /* Endl = */ R"(\\l)");
3079 OS << formatEscapes(CS.str()) << '\n';
3080 }
3081 OS << "\"]\n";
3082
3083 // analyzeBranch is just used to get the names of the branch
3084 // opcodes.
3085 const MCSymbol *TBB = nullptr;
3086 const MCSymbol *FBB = nullptr;
3087 MCInst *CondBranch = nullptr;
3088 MCInst *UncondBranch = nullptr;
3089 const bool Success = BB->analyzeBranch(TBB, FBB, CondBranch, UncondBranch);
3090
3091 const MCInst *LastInstr = BB->getLastNonPseudoInstr();
3092 const bool IsJumpTable = LastInstr && BC.MIB->getJumpTable(*LastInstr);
3093
3094 auto BI = BB->branch_info_begin();
3095 for (BinaryBasicBlock *Succ : BB->successors()) {
3096 std::string Branch;
3097 if (Success) {
3098 if (Succ == BB->getConditionalSuccessor(true)) {
3099 Branch = CondBranch ? std::string(BC.InstPrinter->getOpcodeName(
3100 CondBranch->getOpcode()))
3101 : "TB";
3102 } else if (Succ == BB->getConditionalSuccessor(false)) {
3103 Branch = UncondBranch ? std::string(BC.InstPrinter->getOpcodeName(
3104 UncondBranch->getOpcode()))
3105 : "FB";
3106 } else {
3107 Branch = "FT";
3108 }
3109 }
3110 if (IsJumpTable)
3111 Branch = "JT";
3112 OS << format("\"%s\" -> \"%s\" [label=\"%s", BB->getName().data(),
3113 Succ->getName().data(), Branch.c_str());
3114
3115 if (BB->getExecutionCount() != COUNT_NO_PROFILE &&
3116 BI->MispredictedCount != BinaryBasicBlock::COUNT_INFERRED) {
3117 OS << "\\n(C:" << BI->Count << ",M:" << BI->MispredictedCount << ")";
3118 } else if (ExecutionCount != COUNT_NO_PROFILE &&
3119 BI->Count != BinaryBasicBlock::COUNT_NO_PROFILE) {
3120 OS << "\\n(IC:" << BI->Count << ")";
3121 }
3122 OS << "\"]\n";
3123
3124 ++BI;
3125 }
3126 for (BinaryBasicBlock *LP : BB->landing_pads()) {
3127 OS << format("\"%s\" -> \"%s\" [constraint=false style=dashed]\n",
3128 BB->getName().data(), LP->getName().data());
3129 }
3130 }
3131 OS << "}\n";
3132}
3133
3134void BinaryFunction::viewGraph() const {
3135 SmallString<MAX_PATH255> Filename;
3136 if (std::error_code EC =
3137 sys::fs::createTemporaryFile("bolt-cfg", "dot", Filename)) {
3138 errs() << "BOLT-ERROR: " << EC.message() << ", unable to create "
3139 << " bolt-cfg-XXXXX.dot temporary file.\n";
3140 return;
3141 }
3142 dumpGraphToFile(std::string(Filename));
3143 if (DisplayGraph(Filename))
3144 errs() << "BOLT-ERROR: Can't display " << Filename << " with graphviz.\n";
3145 if (std::error_code EC = sys::fs::remove(Filename)) {
3146 errs() << "BOLT-WARNING: " << EC.message() << ", failed to remove "
3147 << Filename << "\n";
3148 }
3149}
3150
3151void BinaryFunction::dumpGraphForPass(std::string Annotation) const {
3152 if (!opts::shouldPrint(*this))
3153 return;
3154
3155 std::string Filename = constructFilename(getPrintName(), Annotation, ".dot");
3156 if (opts::Verbosity >= 1)
3157 outs() << "BOLT-INFO: dumping CFG to " << Filename << "\n";
3158 dumpGraphToFile(Filename);
3159}
3160
3161void BinaryFunction::dumpGraphToFile(std::string Filename) const {
3162 std::error_code EC;
3163 raw_fd_ostream of(Filename, EC, sys::fs::OF_None);
3164 if (EC) {
3165 if (opts::Verbosity >= 1) {
3166 errs() << "BOLT-WARNING: " << EC.message() << ", unable to open "
3167 << Filename << " for output.\n";
3168 }
3169 return;
3170 }
3171 dumpGraph(of);
3172}
3173
3174bool BinaryFunction::validateCFG() const {
3175 bool Valid = true;
3176 for (BinaryBasicBlock *BB : BasicBlocks)
3177 Valid &= BB->validateSuccessorInvariants();
3178
3179 if (!Valid)
3180 return Valid;
3181
3182 // Make sure all blocks in CFG are valid.
3183 auto validateBlock = [this](const BinaryBasicBlock *BB, StringRef Desc) {
3184 if (!BB->isValid()) {
3185 errs() << "BOLT-ERROR: deleted " << Desc << " " << BB->getName()
3186 << " detected in:\n";
3187 this->dump();
3188 return false;
3189 }
3190 return true;
3191 };
3192 for (const BinaryBasicBlock *BB : BasicBlocks) {
3193 if (!validateBlock(BB, "block"))
3194 return false;
3195 for (const BinaryBasicBlock *PredBB : BB->predecessors())
3196 if (!validateBlock(PredBB, "predecessor"))
3197 return false;
3198 for (const BinaryBasicBlock *SuccBB : BB->successors())
3199 if (!validateBlock(SuccBB, "successor"))
3200 return false;
3201 for (const BinaryBasicBlock *LP : BB->landing_pads())
3202 if (!validateBlock(LP, "landing pad"))
3203 return false;
3204 for (const BinaryBasicBlock *Thrower : BB->throwers())
3205 if (!validateBlock(Thrower, "thrower"))
3206 return false;
3207 }
3208
3209 for (const BinaryBasicBlock *BB : BasicBlocks) {
3210 std::unordered_set<const BinaryBasicBlock *> BBLandingPads;
3211 for (const BinaryBasicBlock *LP : BB->landing_pads()) {
3212 if (BBLandingPads.count(LP)) {
3213 errs() << "BOLT-ERROR: duplicate landing pad detected in"
3214 << BB->getName() << " in function " << *this << '\n';
3215 return false;
3216 }
3217 BBLandingPads.insert(LP);
3218 }
3219
3220 std::unordered_set<const BinaryBasicBlock *> BBThrowers;
3221 for (const BinaryBasicBlock *Thrower : BB->throwers()) {
3222 if (BBThrowers.count(Thrower)) {
3223 errs() << "BOLT-ERROR: duplicate thrower detected in" << BB->getName()
3224 << " in function " << *this << '\n';
3225 return false;
3226 }
3227 BBThrowers.insert(Thrower);
3228 }
3229
3230 for (const BinaryBasicBlock *LPBlock : BB->landing_pads()) {
3231 if (!llvm::is_contained(LPBlock->throwers(), BB)) {
3232 errs() << "BOLT-ERROR: inconsistent landing pad detected in " << *this
3233 << ": " << BB->getName() << " is in LandingPads but not in "
3234 << LPBlock->getName() << " Throwers\n";
3235 return false;
3236 }
3237 }
3238 for (const BinaryBasicBlock *Thrower : BB->throwers()) {
3239 if (!llvm::is_contained(Thrower->landing_pads(), BB)) {
3240 errs() << "BOLT-ERROR: inconsistent thrower detected in " << *this
3241 << ": " << BB->getName() << " is in Throwers list but not in "
3242 << Thrower->getName() << " LandingPads\n";
3243 return false;
3244 }
3245 }
3246 }
3247
3248 return Valid;
3249}
3250
3251void BinaryFunction::fixBranches() {
3252 auto &MIB = BC.MIB;
3253 MCContext *Ctx = BC.Ctx.get();
3254
3255 for (BinaryBasicBlock *BB : BasicBlocks) {
3256 const MCSymbol *TBB = nullptr;
3257 const MCSymbol *FBB = nullptr;
3258 MCInst *CondBranch = nullptr;
3259 MCInst *UncondBranch = nullptr;
3260 if (!BB->analyzeBranch(TBB, FBB, CondBranch, UncondBranch))
3261 continue;
3262
3263 // We will create unconditional branch with correct destination if needed.
3264 if (UncondBranch)
3265 BB->eraseInstruction(BB->findInstruction(UncondBranch));
3266
3267 // Basic block that follows the current one in the final layout.
3268 const BinaryBasicBlock *NextBB =
3269 Layout.getBasicBlockAfter(BB, /*IgnoreSplits=*/false);
3270
3271 if (BB->succ_size() == 1) {
3272 // __builtin_unreachable() could create a conditional branch that
3273 // falls-through into the next function - hence the block will have only
3274 // one valid successor. Since behaviour is undefined - we replace
3275 // the conditional branch with an unconditional if required.
3276 if (CondBranch)
3277 BB->eraseInstruction(BB->findInstruction(CondBranch));
3278 if (BB->getSuccessor() == NextBB)
3279 continue;
3280 BB->addBranchInstruction(BB->getSuccessor());
3281 } else if (BB->succ_size() == 2) {
3282 assert(CondBranch && "conditional branch expected")(static_cast <bool> (CondBranch && "conditional branch expected"
) ? void (0) : __assert_fail ("CondBranch && \"conditional branch expected\""
, "bolt/lib/Core/BinaryFunction.cpp", 3282, __extension__ __PRETTY_FUNCTION__
))
;
3283 const BinaryBasicBlock *TSuccessor = BB->getConditionalSuccessor(true);
3284 const BinaryBasicBlock *FSuccessor = BB->getConditionalSuccessor(false);
3285 // Check whether we support reversing this branch direction
3286 const bool IsSupported =
3287 !MIB->isUnsupportedBranch(CondBranch->getOpcode());
3288 if (NextBB && NextBB == TSuccessor && IsSupported) {
3289 std::swap(TSuccessor, FSuccessor);
3290 {
3291 auto L = BC.scopeLock();
3292 MIB->reverseBranchCondition(*CondBranch, TSuccessor->getLabel(), Ctx);
3293 }
3294 BB->swapConditionalSuccessors();
3295 } else {
3296 auto L = BC.scopeLock();
3297 MIB->replaceBranchTarget(*CondBranch, TSuccessor->getLabel(), Ctx);
3298 }
3299 if (TSuccessor == FSuccessor)
3300 BB->removeDuplicateConditionalSuccessor(CondBranch);
3301 if (!NextBB ||
3302 ((NextBB != TSuccessor || !IsSupported) && NextBB != FSuccessor)) {
3303 // If one of the branches is guaranteed to be "long" while the other
3304 // could be "short", then prioritize short for "taken". This will
3305 // generate a sequence 1 byte shorter on x86.
3306 if (IsSupported && BC.isX86() &&
3307 TSuccessor->getFragmentNum() != FSuccessor->getFragmentNum() &&
3308 BB->getFragmentNum() != TSuccessor->getFragmentNum()) {
3309 std::swap(TSuccessor, FSuccessor);
3310 {
3311 auto L = BC.scopeLock();
3312 MIB->reverseBranchCondition(*CondBranch, TSuccessor->getLabel(),
3313 Ctx);
3314 }
3315 BB->swapConditionalSuccessors();
3316 }
3317 BB->addBranchInstruction(FSuccessor);
3318 }
3319 }
3320 // Cases where the number of successors is 0 (block ends with a
3321 // terminator) or more than 2 (switch table) don't require branch
3322 // instruction adjustments.
3323 }
3324 assert((!isSimple() || validateCFG()) &&(static_cast <bool> ((!isSimple() || validateCFG()) &&
"Invalid CFG detected after fixing branches") ? void (0) : __assert_fail
("(!isSimple() || validateCFG()) && \"Invalid CFG detected after fixing branches\""
, "bolt/lib/Core/BinaryFunction.cpp", 3325, __extension__ __PRETTY_FUNCTION__
))
3325 "Invalid CFG detected after fixing branches")(static_cast <bool> ((!isSimple() || validateCFG()) &&
"Invalid CFG detected after fixing branches") ? void (0) : __assert_fail
("(!isSimple() || validateCFG()) && \"Invalid CFG detected after fixing branches\""
, "bolt/lib/Core/BinaryFunction.cpp", 3325, __extension__ __PRETTY_FUNCTION__
))
;
3326}
3327
3328void BinaryFunction::propagateGnuArgsSizeInfo(
3329 MCPlusBuilder::AllocatorIdTy AllocId) {
3330 assert(CurrentState == State::Disassembled && "unexpected function state")(static_cast <bool> (CurrentState == State::Disassembled
&& "unexpected function state") ? void (0) : __assert_fail
("CurrentState == State::Disassembled && \"unexpected function state\""
, "bolt/lib/Core/BinaryFunction.cpp", 3330, __extension__ __PRETTY_FUNCTION__
))
;
3331
3332 if (!hasEHRanges() || !usesGnuArgsSize())
3333 return;
3334
3335 // The current value of DW_CFA_GNU_args_size affects all following
3336 // invoke instructions until the next CFI overrides it.
3337 // It is important to iterate basic blocks in the original order when
3338 // assigning the value.
3339 uint64_t CurrentGnuArgsSize = 0;
3340 for (BinaryBasicBlock *BB : BasicBlocks) {
3341 for (auto II = BB->begin(); II != BB->end();) {
3342 MCInst &Instr = *II;
3343 if (BC.MIB->isCFI(Instr)) {
3344 const MCCFIInstruction *CFI = getCFIFor(Instr);
3345 if (CFI->getOperation() == MCCFIInstruction::OpGnuArgsSize) {
3346 CurrentGnuArgsSize = CFI->getOffset();
3347 // Delete DW_CFA_GNU_args_size instructions and only regenerate
3348 // during the final code emission. The information is embedded
3349 // inside call instructions.
3350 II = BB->erasePseudoInstruction(II);
3351 continue;
3352 }
3353 } else if (BC.MIB->isInvoke(Instr)) {
3354 // Add the value of GNU_args_size as an extra operand to invokes.
3355 BC.MIB->addGnuArgsSize(Instr, CurrentGnuArgsSize, AllocId);
3356 }
3357 ++II;
3358 }
3359 }
3360}
3361
3362void BinaryFunction::postProcessBranches() {
3363 if (!isSimple())
3364 return;
3365 for (BinaryBasicBlock &BB : blocks()) {
3366 auto LastInstrRI = BB.getLastNonPseudo();
3367 if (BB.succ_size() == 1) {
3368 if (LastInstrRI != BB.rend() &&
3369 BC.MIB->isConditionalBranch(*LastInstrRI)) {
3370 // __builtin_unreachable() could create a conditional branch that
3371 // falls-through into the next function - hence the block will have only
3372 // one valid successor. Such behaviour is undefined and thus we remove
3373 // the conditional branch while leaving a valid successor.
3374 BB.eraseInstruction(std::prev(LastInstrRI.base()));
3375 LLVM_DEBUG(dbgs() << "BOLT-DEBUG: erasing conditional branch in "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: erasing conditional branch in "
<< BB.getName() << " in function " << *this
<< '\n'; } } while (false)
3376 << BB.getName() << " in function " << *this << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: erasing conditional branch in "
<< BB.getName() << " in function " << *this
<< '\n'; } } while (false)
;
3377 }
3378 } else if (BB.succ_size() == 0) {
3379 // Ignore unreachable basic blocks.
3380 if (BB.pred_size() == 0 || BB.isLandingPad())
3381 continue;
3382
3383 // If it's the basic block that does not end up with a terminator - we
3384 // insert a return instruction unless it's a call instruction.
3385 if (LastInstrRI == BB.rend()) {
3386 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: at least one instruction expected in BB "
<< BB.getName() << " in function " << *this
<< '\n'; } } while (false)
3387 dbgs() << "BOLT-DEBUG: at least one instruction expected in BB "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: at least one instruction expected in BB "
<< BB.getName() << " in function " << *this
<< '\n'; } } while (false)
3388 << BB.getName() << " in function " << *this << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: at least one instruction expected in BB "
<< BB.getName() << " in function " << *this
<< '\n'; } } while (false)
;
3389 continue;
3390 }
3391 if (!BC.MIB->isTerminator(*LastInstrRI) &&
3392 !BC.MIB->isCall(*LastInstrRI)) {
3393 LLVM_DEBUG(dbgs() << "BOLT-DEBUG: adding return to basic block "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: adding return to basic block "
<< BB.getName() << " in function " << *this
<< '\n'; } } while (false)
3394 << BB.getName() << " in function " << *this << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: adding return to basic block "
<< BB.getName() << " in function " << *this
<< '\n'; } } while (false)
;
3395 MCInst ReturnInstr;
3396 BC.MIB->createReturn(ReturnInstr);
3397 BB.addInstruction(ReturnInstr);
3398 }
3399 }
3400 }
3401 assert(validateCFG() && "invalid CFG")(static_cast <bool> (validateCFG() && "invalid CFG"
) ? void (0) : __assert_fail ("validateCFG() && \"invalid CFG\""
, "bolt/lib/Core/BinaryFunction.cpp", 3401, __extension__ __PRETTY_FUNCTION__
))
;
3402}
3403
3404MCSymbol *BinaryFunction::addEntryPointAtOffset(uint64_t Offset) {
3405 assert(Offset && "cannot add primary entry point")(static_cast <bool> (Offset && "cannot add primary entry point"
) ? void (0) : __assert_fail ("Offset && \"cannot add primary entry point\""
, "bolt/lib/Core/BinaryFunction.cpp", 3405, __extension__ __PRETTY_FUNCTION__
))
;
3406 assert(CurrentState == State::Empty || CurrentState == State::Disassembled)(static_cast <bool> (CurrentState == State::Empty || CurrentState
== State::Disassembled) ? void (0) : __assert_fail ("CurrentState == State::Empty || CurrentState == State::Disassembled"
, "bolt/lib/Core/BinaryFunction.cpp", 3406, __extension__ __PRETTY_FUNCTION__
))
;
3407
3408 const uint64_t EntryPointAddress = getAddress() + Offset;
3409 MCSymbol *LocalSymbol = getOrCreateLocalLabel(EntryPointAddress);
3410
3411 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(LocalSymbol);
3412 if (EntrySymbol)
3413 return EntrySymbol;
3414
3415 if (BinaryData *EntryBD = BC.getBinaryDataAtAddress(EntryPointAddress)) {
3416 EntrySymbol = EntryBD->getSymbol();
3417 } else {
3418 EntrySymbol = BC.getOrCreateGlobalSymbol(
3419 EntryPointAddress, Twine("__ENTRY_") + getOneName() + "@");
3420 }
3421 SecondaryEntryPoints[LocalSymbol] = EntrySymbol;
3422
3423 BC.setSymbolToFunctionMap(EntrySymbol, this);
3424
3425 return EntrySymbol;
3426}
3427
3428MCSymbol *BinaryFunction::addEntryPoint(const BinaryBasicBlock &BB) {
3429 assert(CurrentState == State::CFG &&(static_cast <bool> (CurrentState == State::CFG &&
"basic block can be added as an entry only in a function with CFG"
) ? void (0) : __assert_fail ("CurrentState == State::CFG && \"basic block can be added as an entry only in a function with CFG\""
, "bolt/lib/Core/BinaryFunction.cpp", 3430, __extension__ __PRETTY_FUNCTION__
))
3430 "basic block can be added as an entry only in a function with CFG")(static_cast <bool> (CurrentState == State::CFG &&
"basic block can be added as an entry only in a function with CFG"
) ? void (0) : __assert_fail ("CurrentState == State::CFG && \"basic block can be added as an entry only in a function with CFG\""
, "bolt/lib/Core/BinaryFunction.cpp", 3430, __extension__ __PRETTY_FUNCTION__
))
;
3431
3432 if (&BB == BasicBlocks.front())
3433 return getSymbol();
3434
3435 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(BB);
3436 if (EntrySymbol)
3437 return EntrySymbol;
3438
3439 EntrySymbol =
3440 BC.Ctx->getOrCreateSymbol("__ENTRY_" + BB.getLabel()->getName());
3441
3442 SecondaryEntryPoints[BB.getLabel()] = EntrySymbol;
3443
3444 BC.setSymbolToFunctionMap(EntrySymbol, this);
3445
3446 return EntrySymbol;
3447}
3448
3449MCSymbol *BinaryFunction::getSymbolForEntryID(uint64_t EntryID) {
3450 if (EntryID == 0)
3451 return getSymbol();
3452
3453 if (!isMultiEntry())
3454 return nullptr;
3455
3456 uint64_t NumEntries = 0;
3457 if (hasCFG()) {
3458 for (BinaryBasicBlock *BB : BasicBlocks) {
3459 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(*BB);
3460 if (!EntrySymbol)
3461 continue;
3462 if (NumEntries == EntryID)
3463 return EntrySymbol;
3464 ++NumEntries;
3465 }
3466 } else {
3467 for (std::pair<const uint32_t, MCSymbol *> &KV : Labels) {
3468 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(KV.second);
3469 if (!EntrySymbol)
3470 continue;
3471 if (NumEntries == EntryID)
3472 return EntrySymbol;
3473 ++NumEntries;
3474 }
3475 }
3476
3477 return nullptr;
3478}
3479
3480uint64_t BinaryFunction::getEntryIDForSymbol(const MCSymbol *Symbol) const {
3481 if (!isMultiEntry())
3482 return 0;
3483
3484 for (const MCSymbol *FunctionSymbol : getSymbols())
3485 if (FunctionSymbol == Symbol)
3486 return 0;
3487
3488 // Check all secondary entries available as either basic blocks or lables.
3489 uint64_t NumEntries = 0;
3490 for (const BinaryBasicBlock *BB : BasicBlocks) {
3491 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(*BB);
3492 if (!EntrySymbol)
3493 continue;
3494 if (EntrySymbol == Symbol)
3495 return NumEntries;
3496 ++NumEntries;
3497 }
3498 NumEntries = 0;
3499 for (const std::pair<const uint32_t, MCSymbol *> &KV : Labels) {
3500 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(KV.second);
3501 if (!EntrySymbol)
3502 continue;
3503 if (EntrySymbol == Symbol)
3504 return NumEntries;
3505 ++NumEntries;
3506 }
3507
3508 llvm_unreachable("symbol not found")::llvm::llvm_unreachable_internal("symbol not found", "bolt/lib/Core/BinaryFunction.cpp"
, 3508)
;
3509}
3510
3511bool BinaryFunction::forEachEntryPoint(EntryPointCallbackTy Callback) const {
3512 bool Status = Callback(0, getSymbol());
3513 if (!isMultiEntry())
3514 return Status;
3515
3516 for (const std::pair<const uint32_t, MCSymbol *> &KV : Labels) {
3517 if (!Status)
3518 break;
3519
3520 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(KV.second);
3521 if (!EntrySymbol)
3522 continue;
3523
3524 Status = Callback(KV.first, EntrySymbol);
3525 }
3526
3527 return Status;
3528}
3529
3530BinaryFunction::BasicBlockListType BinaryFunction::dfs() const {
3531 BasicBlockListType DFS;
3532 unsigned Index = 0;
3533 std::stack<BinaryBasicBlock *> Stack;
3534
3535 // Push entry points to the stack in reverse order.
3536 //
3537 // NB: we rely on the original order of entries to match.
3538 SmallVector<BinaryBasicBlock *> EntryPoints;
3539 llvm::copy_if(BasicBlocks, std::back_inserter(EntryPoints),
3540 [&](const BinaryBasicBlock *const BB) { return isEntryPoint(*BB); });
3541 // Sort entry points by their offset to make sure we got them in the right
3542 // order.
3543 llvm::stable_sort(EntryPoints, [](const BinaryBasicBlock *const A,
3544 const BinaryBasicBlock *const B) {
3545 return A->getOffset() < B->getOffset();
3546 });
3547 for (BinaryBasicBlock *const BB : reverse(EntryPoints))
3548 Stack.push(BB);
3549
3550 for (BinaryBasicBlock &BB : blocks())
3551 BB.setLayoutIndex(BinaryBasicBlock::InvalidIndex);
3552
3553 while (!Stack.empty()) {
3554 BinaryBasicBlock *BB = Stack.top();
3555 Stack.pop();
3556
3557 if (BB->getLayoutIndex() != BinaryBasicBlock::InvalidIndex)
3558 continue;
3559
3560 BB->setLayoutIndex(Index++);
3561 DFS.push_back(BB);
3562
3563 for (BinaryBasicBlock *SuccBB : BB->landing_pads()) {
3564 Stack.push(SuccBB);
3565 }
3566
3567 const MCSymbol *TBB = nullptr;
3568 const MCSymbol *FBB = nullptr;
3569 MCInst *CondBranch = nullptr;
3570 MCInst *UncondBranch = nullptr;
3571 if (BB->analyzeBranch(TBB, FBB, CondBranch, UncondBranch) && CondBranch &&
3572 BB->succ_size() == 2) {
3573 if (BC.MIB->getCanonicalBranchCondCode(BC.MIB->getCondCode(
3574 *CondBranch)) == BC.MIB->getCondCode(*CondBranch)) {
3575 Stack.push(BB->getConditionalSuccessor(true));
3576 Stack.push(BB->getConditionalSuccessor(false));
3577 } else {
3578 Stack.push(BB->getConditionalSuccessor(false));
3579 Stack.push(BB->getConditionalSuccessor(true));
3580 }
3581 } else {
3582 for (BinaryBasicBlock *SuccBB : BB->successors()) {
3583 Stack.push(SuccBB);
3584 }
3585 }
3586 }
3587
3588 return DFS;
3589}
3590
3591size_t BinaryFunction::computeHash(bool UseDFS,
3592 OperandHashFuncTy OperandHashFunc) const {
3593 if (size() == 0)
3594 return 0;
3595
3596 assert(hasCFG() && "function is expected to have CFG")(static_cast <bool> (hasCFG() && "function is expected to have CFG"
) ? void (0) : __assert_fail ("hasCFG() && \"function is expected to have CFG\""
, "bolt/lib/Core/BinaryFunction.cpp", 3596, __extension__ __PRETTY_FUNCTION__
))
;
3597
3598 SmallVector<const BinaryBasicBlock *, 0> Order;
3599 if (UseDFS)
3600 llvm::copy(dfs(), std::back_inserter(Order));
3601 else
3602 llvm::copy(Layout.blocks(), std::back_inserter(Order));
3603
3604 // The hash is computed by creating a string of all instruction opcodes and
3605 // possibly their operands and then hashing that string with std::hash.
3606 std::string HashString;
3607 for (const BinaryBasicBlock *BB : Order) {
3608 for (const MCInst &Inst : *BB) {
3609 unsigned Opcode = Inst.getOpcode();
3610
3611 if (BC.MIB->isPseudo(Inst))
3612 continue;
3613
3614 // Ignore unconditional jumps since we check CFG consistency by processing
3615 // basic blocks in order and do not rely on branches to be in-sync with
3616 // CFG. Note that we still use condition code of conditional jumps.
3617 if (BC.MIB->isUnconditionalBranch(Inst))
3618 continue;
3619
3620 if (Opcode == 0)
3621 HashString.push_back(0);
3622
3623 while (Opcode) {
3624 uint8_t LSB = Opcode & 0xff;
3625 HashString.push_back(LSB);
3626 Opcode = Opcode >> 8;
3627 }
3628
3629 for (const MCOperand &Op : MCPlus::primeOperands(Inst))
3630 HashString.append(OperandHashFunc(Op));
3631 }
3632 }
3633
3634 return Hash = std::hash<std::string>{}(HashString);
3635}
3636
3637void BinaryFunction::insertBasicBlocks(
3638 BinaryBasicBlock *Start,
3639 std::vector<std::unique_ptr<BinaryBasicBlock>> &&NewBBs,
3640 const bool UpdateLayout, const bool UpdateCFIState,
3641 const bool RecomputeLandingPads) {
3642 const int64_t StartIndex = Start ? getIndex(Start) : -1LL;
3643 const size_t NumNewBlocks = NewBBs.size();
3644
3645 BasicBlocks.insert(BasicBlocks.begin() + (StartIndex + 1), NumNewBlocks,
3646 nullptr);
3647
3648 int64_t I = StartIndex + 1;
3649 for (std::unique_ptr<BinaryBasicBlock> &BB : NewBBs) {
3650 assert(!BasicBlocks[I])(static_cast <bool> (!BasicBlocks[I]) ? void (0) : __assert_fail
("!BasicBlocks[I]", "bolt/lib/Core/BinaryFunction.cpp", 3650
, __extension__ __PRETTY_FUNCTION__))
;
3651 BasicBlocks[I++] = BB.release();
3652 }
3653
3654 if (RecomputeLandingPads)
3655 recomputeLandingPads();
3656 else
3657 updateBBIndices(0);
3658
3659 if (UpdateLayout)
3660 updateLayout(Start, NumNewBlocks);
3661
3662 if (UpdateCFIState)
3663 updateCFIState(Start, NumNewBlocks);
3664}
3665
3666BinaryFunction::iterator BinaryFunction::insertBasicBlocks(
3667 BinaryFunction::iterator StartBB,
3668 std::vector<std::unique_ptr<BinaryBasicBlock>> &&NewBBs,
3669 const bool UpdateLayout, const bool UpdateCFIState,
3670 const bool RecomputeLandingPads) {
3671 const unsigned StartIndex = getIndex(&*StartBB);
3672 const size_t NumNewBlocks = NewBBs.size();
3673
3674 BasicBlocks.insert(BasicBlocks.begin() + StartIndex + 1, NumNewBlocks,
3675 nullptr);
3676 auto RetIter = BasicBlocks.begin() + StartIndex + 1;
3677
3678 unsigned I = StartIndex + 1;
3679 for (std::unique_ptr<BinaryBasicBlock> &BB : NewBBs) {
3680 assert(!BasicBlocks[I])(static_cast <bool> (!BasicBlocks[I]) ? void (0) : __assert_fail
("!BasicBlocks[I]", "bolt/lib/Core/BinaryFunction.cpp", 3680
, __extension__ __PRETTY_FUNCTION__))
;
3681 BasicBlocks[I++] = BB.release();
3682 }
3683
3684 if (RecomputeLandingPads)
3685 recomputeLandingPads();
3686 else
3687 updateBBIndices(0);
3688
3689 if (UpdateLayout)
3690 updateLayout(*std::prev(RetIter), NumNewBlocks);
3691
3692 if (UpdateCFIState)
3693 updateCFIState(*std::prev(RetIter), NumNewBlocks);
3694
3695 return RetIter;
3696}
3697
3698void BinaryFunction::updateBBIndices(const unsigned StartIndex) {
3699 for (unsigned I = StartIndex; I < BasicBlocks.size(); ++I)
3700 BasicBlocks[I]->Index = I;
3701}
3702
3703void BinaryFunction::updateCFIState(BinaryBasicBlock *Start,
3704 const unsigned NumNewBlocks) {
3705 const int32_t CFIState = Start->getCFIStateAtExit();
3706 const unsigned StartIndex = getIndex(Start) + 1;
3707 for (unsigned I = 0; I < NumNewBlocks; ++I)
3708 BasicBlocks[StartIndex + I]->setCFIState(CFIState);
3709}
3710
3711void BinaryFunction::updateLayout(BinaryBasicBlock *Start,
3712 const unsigned NumNewBlocks) {
3713 BasicBlockListType::iterator Begin;
3714 BasicBlockListType::iterator End;
3715
3716 // If start not provided copy new blocks from the beginning of BasicBlocks
3717 if (!Start) {
3718 Begin = BasicBlocks.begin();
3719 End = BasicBlocks.begin() + NumNewBlocks;
3720 } else {
3721 unsigned StartIndex = getIndex(Start);
3722 Begin = std::next(BasicBlocks.begin(), StartIndex + 1);
3723 End = std::next(BasicBlocks.begin(), StartIndex + NumNewBlocks + 1);
3724 }
3725
3726 // Insert new blocks in the layout immediately after Start.
3727 Layout.insertBasicBlocks(Start, {Begin, End});
3728 Layout.updateLayoutIndices();
3729}
3730
3731bool BinaryFunction::checkForAmbiguousJumpTables() {
3732 SmallSet<uint64_t, 4> JumpTables;
3733 for (BinaryBasicBlock *&BB : BasicBlocks) {
3734 for (MCInst &Inst : *BB) {
3735 if (!BC.MIB->isIndirectBranch(Inst))
3736 continue;
3737 uint64_t JTAddress = BC.MIB->getJumpTable(Inst);
3738 if (!JTAddress)
3739 continue;
3740 // This address can be inside another jump table, but we only consider
3741 // it ambiguous when the same start address is used, not the same JT
3742 // object.
3743 if (!JumpTables.count(JTAddress)) {
3744 JumpTables.insert(JTAddress);
3745 continue;
3746 }
3747 return true;
3748 }
3749 }
3750 return false;
3751}
3752
3753void BinaryFunction::disambiguateJumpTables(
3754 MCPlusBuilder::AllocatorIdTy AllocId) {
3755 assert((opts::JumpTables != JTS_BASIC && isSimple()) || !BC.HasRelocations)(static_cast <bool> ((opts::JumpTables != JTS_BASIC &&
isSimple()) || !BC.HasRelocations) ? void (0) : __assert_fail
("(opts::JumpTables != JTS_BASIC && isSimple()) || !BC.HasRelocations"
, "bolt/lib/Core/BinaryFunction.cpp", 3755, __extension__ __PRETTY_FUNCTION__
))
;
3756 SmallPtrSet<JumpTable *, 4> JumpTables;
3757 for (BinaryBasicBlock *&BB : BasicBlocks) {
3758 for (MCInst &Inst : *BB) {
3759 if (!BC.MIB->isIndirectBranch(Inst))
3760 continue;
3761 JumpTable *JT = getJumpTable(Inst);
3762 if (!JT)
3763 continue;
3764 auto Iter = JumpTables.find(JT);
3765 if (Iter == JumpTables.end()) {
3766 JumpTables.insert(JT);
3767 continue;
3768 }
3769 // This instruction is an indirect jump using a jump table, but it is
3770 // using the same jump table of another jump. Try all our tricks to
3771 // extract the jump table symbol and make it point to a new, duplicated JT
3772 MCPhysReg BaseReg1;
3773 uint64_t Scale;
3774 const MCSymbol *Target;
3775 // In case we match if our first matcher, first instruction is the one to
3776 // patch
3777 MCInst *JTLoadInst = &Inst;
3778 // Try a standard indirect jump matcher, scale 8
3779 std::unique_ptr<MCPlusBuilder::MCInstMatcher> IndJmpMatcher =
3780 BC.MIB->matchIndJmp(BC.MIB->matchReg(BaseReg1),
3781 BC.MIB->matchImm(Scale), BC.MIB->matchReg(),
3782 /*Offset=*/BC.MIB->matchSymbol(Target));
3783 if (!IndJmpMatcher->match(
3784 *BC.MRI, *BC.MIB,
3785 MutableArrayRef<MCInst>(&*BB->begin(), &Inst + 1), -1) ||
3786 BaseReg1 != BC.MIB->getNoRegister() || Scale != 8) {
3787 MCPhysReg BaseReg2;
3788 uint64_t Offset;
3789 // Standard JT matching failed. Trying now:
3790 // movq "jt.2397/1"(,%rax,8), %rax
3791 // jmpq *%rax
3792 std::unique_ptr<MCPlusBuilder::MCInstMatcher> LoadMatcherOwner =
3793 BC.MIB->matchLoad(BC.MIB->matchReg(BaseReg1),
3794 BC.MIB->matchImm(Scale), BC.MIB->matchReg(),
3795 /*Offset=*/BC.MIB->matchSymbol(Target));
3796 MCPlusBuilder::MCInstMatcher *LoadMatcher = LoadMatcherOwner.get();
3797 std::unique_ptr<MCPlusBuilder::MCInstMatcher> IndJmpMatcher2 =
3798 BC.MIB->matchIndJmp(std::move(LoadMatcherOwner));
3799 if (!IndJmpMatcher2->match(
3800 *BC.MRI, *BC.MIB,
3801 MutableArrayRef<MCInst>(&*BB->begin(), &Inst + 1), -1) ||
3802 BaseReg1 != BC.MIB->getNoRegister() || Scale != 8) {
3803 // JT matching failed. Trying now:
3804 // PIC-style matcher, scale 4
3805 // addq %rdx, %rsi
3806 // addq %rdx, %rdi
3807 // leaq DATAat0x402450(%rip), %r11
3808 // movslq (%r11,%rdx,4), %rcx
3809 // addq %r11, %rcx
3810 // jmpq *%rcx # JUMPTABLE @0x402450
3811 std::unique_ptr<MCPlusBuilder::MCInstMatcher> PICIndJmpMatcher =
3812 BC.MIB->matchIndJmp(BC.MIB->matchAdd(
3813 BC.MIB->matchReg(BaseReg1),
3814 BC.MIB->matchLoad(BC.MIB->matchReg(BaseReg2),
3815 BC.MIB->matchImm(Scale), BC.MIB->matchReg(),
3816 BC.MIB->matchImm(Offset))));
3817 std::unique_ptr<MCPlusBuilder::MCInstMatcher> LEAMatcherOwner =
3818 BC.MIB->matchLoadAddr(BC.MIB->matchSymbol(Target));
3819 MCPlusBuilder::MCInstMatcher *LEAMatcher = LEAMatcherOwner.get();
3820 std::unique_ptr<MCPlusBuilder::MCInstMatcher> PICBaseAddrMatcher =
3821 BC.MIB->matchIndJmp(BC.MIB->matchAdd(std::move(LEAMatcherOwner),
3822 BC.MIB->matchAnyOperand()));
3823 if (!PICIndJmpMatcher->match(
3824 *BC.MRI, *BC.MIB,
3825 MutableArrayRef<MCInst>(&*BB->begin(), &Inst + 1), -1) ||
3826 Scale != 4 || BaseReg1 != BaseReg2 || Offset != 0 ||
3827 !PICBaseAddrMatcher->match(
3828 *BC.MRI, *BC.MIB,
3829 MutableArrayRef<MCInst>(&*BB->begin(), &Inst + 1), -1)) {
3830 llvm_unreachable("Failed to extract jump table base")::llvm::llvm_unreachable_internal("Failed to extract jump table base"
, "bolt/lib/Core/BinaryFunction.cpp", 3830)
;
3831 continue;
3832 }
3833 // Matched PIC, identify the instruction with the reference to the JT
3834 JTLoadInst = LEAMatcher->CurInst;
3835 } else {
3836 // Matched non-PIC
3837 JTLoadInst = LoadMatcher->CurInst;
3838 }
3839 }
3840
3841 uint64_t NewJumpTableID = 0;
3842 const MCSymbol *NewJTLabel;
3843 std::tie(NewJumpTableID, NewJTLabel) =
3844 BC.duplicateJumpTable(*this, JT, Target);
3845 {
3846 auto L = BC.scopeLock();
3847 BC.MIB->replaceMemOperandDisp(*JTLoadInst, NewJTLabel, BC.Ctx.get());
3848 }
3849 // We use a unique ID with the high bit set as address for this "injected"
3850 // jump table (not originally in the input binary).
3851 BC.MIB->setJumpTable(Inst, NewJumpTableID, 0, AllocId);
3852 }
3853 }
3854}
3855
3856bool BinaryFunction::replaceJumpTableEntryIn(BinaryBasicBlock *BB,
3857 BinaryBasicBlock *OldDest,
3858 BinaryBasicBlock *NewDest) {
3859 MCInst *Instr = BB->getLastNonPseudoInstr();
3860 if (!Instr || !BC.MIB->isIndirectBranch(*Instr))
3861 return false;
3862 uint64_t JTAddress = BC.MIB->getJumpTable(*Instr);
3863 assert(JTAddress && "Invalid jump table address")(static_cast <bool> (JTAddress && "Invalid jump table address"
) ? void (0) : __assert_fail ("JTAddress && \"Invalid jump table address\""
, "bolt/lib/Core/BinaryFunction.cpp", 3863, __extension__ __PRETTY_FUNCTION__
))
;
3864 JumpTable *JT = getJumpTableContainingAddress(JTAddress);
3865 assert(JT && "No jump table structure for this indirect branch")(static_cast <bool> (JT && "No jump table structure for this indirect branch"
) ? void (0) : __assert_fail ("JT && \"No jump table structure for this indirect branch\""
, "bolt/lib/Core/BinaryFunction.cpp", 3865, __extension__ __PRETTY_FUNCTION__
))
;
3866 bool Patched = JT->replaceDestination(JTAddress, OldDest->getLabel(),
3867 NewDest->getLabel());
3868 (void)Patched;
3869 assert(Patched && "Invalid entry to be replaced in jump table")(static_cast <bool> (Patched && "Invalid entry to be replaced in jump table"
) ? void (0) : __assert_fail ("Patched && \"Invalid entry to be replaced in jump table\""
, "bolt/lib/Core/BinaryFunction.cpp", 3869, __extension__ __PRETTY_FUNCTION__
))
;
3870 return true;
3871}
3872
3873BinaryBasicBlock *BinaryFunction::splitEdge(BinaryBasicBlock *From,
3874 BinaryBasicBlock *To) {
3875 // Create intermediate BB
3876 MCSymbol *Tmp;
3877 {
3878 auto L = BC.scopeLock();
3879 Tmp = BC.Ctx->createNamedTempSymbol("SplitEdge");
3880 }
3881 // Link new BBs to the original input offset of the From BB, so we can map
3882 // samples recorded in new BBs back to the original BB seem in the input
3883 // binary (if using BAT)
3884 std::unique_ptr<BinaryBasicBlock> NewBB = createBasicBlock(Tmp);
3885 NewBB->setOffset(From->getInputOffset());
3886 BinaryBasicBlock *NewBBPtr = NewBB.get();
3887
3888 // Update "From" BB
3889 auto I = From->succ_begin();
3890 auto BI = From->branch_info_begin();
3891 for (; I != From->succ_end(); ++I) {
3892 if (*I == To)
3893 break;
3894 ++BI;
3895 }
3896 assert(I != From->succ_end() && "Invalid CFG edge in splitEdge!")(static_cast <bool> (I != From->succ_end() &&
"Invalid CFG edge in splitEdge!") ? void (0) : __assert_fail
("I != From->succ_end() && \"Invalid CFG edge in splitEdge!\""
, "bolt/lib/Core/BinaryFunction.cpp", 3896, __extension__ __PRETTY_FUNCTION__
))
;
3897 uint64_t OrigCount = BI->Count;
3898 uint64_t OrigMispreds = BI->MispredictedCount;
3899 replaceJumpTableEntryIn(From, To, NewBBPtr);
3900 From->replaceSuccessor(To, NewBBPtr, OrigCount, OrigMispreds);
3901
3902 NewBB->addSuccessor(To, OrigCount, OrigMispreds);
3903 NewBB->setExecutionCount(OrigCount);
3904 NewBB->setIsCold(From->isCold());
3905
3906 // Update CFI and BB layout with new intermediate BB
3907 std::vector<std::unique_ptr<BinaryBasicBlock>> NewBBs;
3908 NewBBs.emplace_back(std::move(NewBB));
3909 insertBasicBlocks(From, std::move(NewBBs), true, true,
3910 /*RecomputeLandingPads=*/false);
3911 return NewBBPtr;
3912}
3913
3914void BinaryFunction::deleteConservativeEdges() {
3915 // Our goal is to aggressively remove edges from the CFG that we believe are
3916 // wrong. This is used for instrumentation, where it is safe to remove
3917 // fallthrough edges because we won't reorder blocks.
3918 for (auto I = BasicBlocks.begin(), E = BasicBlocks.end(); I != E; ++I) {
3919 BinaryBasicBlock *BB = *I;
3920 if (BB->succ_size() != 1 || BB->size() == 0)
3921 continue;
3922
3923 auto NextBB = std::next(I);
3924 MCInst *Last = BB->getLastNonPseudoInstr();
3925 // Fallthrough is a landing pad? Delete this edge (as long as we don't
3926 // have a direct jump to it)
3927 if ((*BB->succ_begin())->isLandingPad() && NextBB != E &&
3928 *BB->succ_begin() == *NextBB && Last && !BC.MIB->isBranch(*Last)) {
3929 BB->removeAllSuccessors();
3930 continue;
3931 }
3932
3933 // Look for suspicious calls at the end of BB where gcc may optimize it and
3934 // remove the jump to the epilogue when it knows the call won't return.
3935 if (!Last || !BC.MIB->isCall(*Last))
3936 continue;
3937
3938 const MCSymbol *CalleeSymbol = BC.MIB->getTargetSymbol(*Last);
3939 if (!CalleeSymbol)
3940 continue;
3941
3942 StringRef CalleeName = CalleeSymbol->getName();
3943 if (CalleeName != "__cxa_throw@PLT" && CalleeName != "_Unwind_Resume@PLT" &&
3944 CalleeName != "__cxa_rethrow@PLT" && CalleeName != "exit@PLT" &&
3945 CalleeName != "abort@PLT")
3946 continue;
3947
3948 BB->removeAllSuccessors();
3949 }
3950}
3951
3952bool BinaryFunction::isSymbolValidInScope(const SymbolRef &Symbol,
3953 uint64_t SymbolSize) const {
3954 // If this symbol is in a different section from the one where the
3955 // function symbol is, don't consider it as valid.
3956 if (!getOriginSection()->containsAddress(
3957 cantFail(Symbol.getAddress(), "cannot get symbol address")))
3958 return false;
3959
3960 // Some symbols are tolerated inside function bodies, others are not.
3961 // The real function boundaries may not be known at this point.
3962 if (BC.isMarker(Symbol))
3963 return true;
3964
3965 // It's okay to have a zero-sized symbol in the middle of non-zero-sized
3966 // function.
3967 if (SymbolSize == 0 && containsAddress(cantFail(Symbol.getAddress())))
3968 return true;
3969
3970 if (cantFail(Symbol.getType()) != SymbolRef::ST_Unknown)
3971 return false;
3972
3973 if (cantFail(Symbol.getFlags()) & SymbolRef::SF_Global)
3974 return false;
3975
3976 return true;
3977}
3978
3979void BinaryFunction::adjustExecutionCount(uint64_t Count) {
3980 if (getKnownExecutionCount() == 0 || Count == 0)
3981 return;
3982
3983 if (ExecutionCount < Count)
3984 Count = ExecutionCount;
3985
3986 double AdjustmentRatio = ((double)ExecutionCount - Count) / ExecutionCount;
3987 if (AdjustmentRatio < 0.0)
3988 AdjustmentRatio = 0.0;
3989
3990 for (BinaryBasicBlock &BB : blocks())
3991 BB.adjustExecutionCount(AdjustmentRatio);
3992
3993 ExecutionCount -= Count;
3994}
3995
3996BinaryFunction::~BinaryFunction() {
3997 for (BinaryBasicBlock *BB : BasicBlocks)
3998 delete BB;
3999 for (BinaryBasicBlock *BB : DeletedBasicBlocks)
4000 delete BB;
4001}
4002
4003void BinaryFunction::calculateLoopInfo() {
4004 // Discover loops.
4005 BinaryDominatorTree DomTree;
4006 DomTree.recalculate(*this);
4007 BLI.reset(new BinaryLoopInfo());
4008 BLI->analyze(DomTree);
4009
4010 // Traverse discovered loops and add depth and profile information.
4011 std::stack<BinaryLoop *> St;
4012 for (auto I = BLI->begin(), E = BLI->end(); I != E; ++I) {
4013 St.push(*I);
4014 ++BLI->OuterLoops;
4015 }
4016
4017 while (!St.empty()) {
4018 BinaryLoop *L = St.top();
4019 St.pop();
4020 ++BLI->TotalLoops;
4021 BLI->MaximumDepth = std::max(L->getLoopDepth(), BLI->MaximumDepth);
4022
4023 // Add nested loops in the stack.
4024 for (BinaryLoop::iterator I = L->begin(), E = L->end(); I != E; ++I)
4025 St.push(*I);
4026
4027 // Skip if no valid profile is found.
4028 if (!hasValidProfile()) {
4029 L->EntryCount = COUNT_NO_PROFILE;
4030 L->ExitCount = COUNT_NO_PROFILE;
4031 L->TotalBackEdgeCount = COUNT_NO_PROFILE;
4032 continue;
4033 }
4034
4035 // Compute back edge count.
4036 SmallVector<BinaryBasicBlock *, 1> Latches;
4037 L->getLoopLatches(Latches);
4038
4039 for (BinaryBasicBlock *Latch : Latches) {
4040 auto BI = Latch->branch_info_begin();
4041 for (BinaryBasicBlock *Succ : Latch->successors()) {
4042 if (Succ == L->getHeader()) {
4043 assert(BI->Count != BinaryBasicBlock::COUNT_NO_PROFILE &&(static_cast <bool> (BI->Count != BinaryBasicBlock::
COUNT_NO_PROFILE && "profile data not found") ? void (
0) : __assert_fail ("BI->Count != BinaryBasicBlock::COUNT_NO_PROFILE && \"profile data not found\""
, "bolt/lib/Core/BinaryFunction.cpp", 4044, __extension__ __PRETTY_FUNCTION__
))
4044 "profile data not found")(static_cast <bool> (BI->Count != BinaryBasicBlock::
COUNT_NO_PROFILE && "profile data not found") ? void (
0) : __assert_fail ("BI->Count != BinaryBasicBlock::COUNT_NO_PROFILE && \"profile data not found\""
, "bolt/lib/Core/BinaryFunction.cpp", 4044, __extension__ __PRETTY_FUNCTION__
))
;
4045 L->TotalBackEdgeCount += BI->Count;
4046 }
4047 ++BI;
4048 }
4049 }
4050
4051 // Compute entry count.
4052 L->EntryCount = L->getHeader()->getExecutionCount() - L->TotalBackEdgeCount;
4053
4054 // Compute exit count.
4055 SmallVector<BinaryLoop::Edge, 1> ExitEdges;
4056 L->getExitEdges(ExitEdges);
4057 for (BinaryLoop::Edge &Exit : ExitEdges) {
4058 const BinaryBasicBlock *Exiting = Exit.first;
4059 const BinaryBasicBlock *ExitTarget = Exit.second;
4060 auto BI = Exiting->branch_info_begin();
4061 for (BinaryBasicBlock *Succ : Exiting->successors()) {
4062 if (Succ == ExitTarget) {
4063 assert(BI->Count != BinaryBasicBlock::COUNT_NO_PROFILE &&(static_cast <bool> (BI->Count != BinaryBasicBlock::
COUNT_NO_PROFILE && "profile data not found") ? void (
0) : __assert_fail ("BI->Count != BinaryBasicBlock::COUNT_NO_PROFILE && \"profile data not found\""
, "bolt/lib/Core/BinaryFunction.cpp", 4064, __extension__ __PRETTY_FUNCTION__
))
4064 "profile data not found")(static_cast <bool> (BI->Count != BinaryBasicBlock::
COUNT_NO_PROFILE && "profile data not found") ? void (
0) : __assert_fail ("BI->Count != BinaryBasicBlock::COUNT_NO_PROFILE && \"profile data not found\""
, "bolt/lib/Core/BinaryFunction.cpp", 4064, __extension__ __PRETTY_FUNCTION__
))
;
4065 L->ExitCount += BI->Count;
4066 }
4067 ++BI;
4068 }
4069 }
4070 }
4071}
4072
4073void BinaryFunction::updateOutputValues(const MCAsmLayout &Layout) {
4074 if (!isEmitted()) {
1
Assuming the condition is false
2
Taking false branch
4075 assert(!isInjected() && "injected function should be emitted")(static_cast <bool> (!isInjected() && "injected function should be emitted"
) ? void (0) : __assert_fail ("!isInjected() && \"injected function should be emitted\""
, "bolt/lib/Core/BinaryFunction.cpp", 4075, __extension__ __PRETTY_FUNCTION__
))
;
4076 setOutputAddress(getAddress());
4077 setOutputSize(getSize());
4078 return;
4079 }
4080
4081 const uint64_t BaseAddress = getCodeSection()->getOutputAddress();
4082 if (BC.HasRelocations || isInjected()) {
3
Assuming field 'HasRelocations' is false
4
Assuming the condition is false
5
Taking false branch
4083 const uint64_t StartOffset = Layout.getSymbolOffset(*getSymbol());
4084 const uint64_t EndOffset = Layout.getSymbolOffset(*getFunctionEndLabel());
4085 setOutputAddress(BaseAddress + StartOffset);
4086 setOutputSize(EndOffset - StartOffset);
4087 if (hasConstantIsland()) {
4088 const uint64_t DataOffset =
4089 Layout.getSymbolOffset(*getFunctionConstantIslandLabel());
4090 setOutputDataAddress(BaseAddress + DataOffset);
4091 }
4092 if (isSplit()) {
4093 for (FunctionFragment &FF : getLayout().getSplitFragments()) {
4094 ErrorOr<BinarySection &> ColdSection =
4095 getCodeSection(FF.getFragmentNum());
4096 // If fragment is empty, cold section might not exist
4097 if (FF.empty() && ColdSection.getError())
4098 continue;
4099 const uint64_t ColdBaseAddress = ColdSection->getOutputAddress();
4100
4101 const MCSymbol *ColdStartSymbol = getSymbol(FF.getFragmentNum());
4102 // If fragment is empty, symbol might have not been emitted
4103 if (FF.empty() && (!ColdStartSymbol || !ColdStartSymbol->isDefined()) &&
4104 !hasConstantIsland())
4105 continue;
4106 assert(ColdStartSymbol && ColdStartSymbol->isDefined() &&(static_cast <bool> (ColdStartSymbol && ColdStartSymbol
->isDefined() && "split function should have defined cold symbol"
) ? void (0) : __assert_fail ("ColdStartSymbol && ColdStartSymbol->isDefined() && \"split function should have defined cold symbol\""
, "bolt/lib/Core/BinaryFunction.cpp", 4107, __extension__ __PRETTY_FUNCTION__
))
4107 "split function should have defined cold symbol")(static_cast <bool> (ColdStartSymbol && ColdStartSymbol
->isDefined() && "split function should have defined cold symbol"
) ? void (0) : __assert_fail ("ColdStartSymbol && ColdStartSymbol->isDefined() && \"split function should have defined cold symbol\""
, "bolt/lib/Core/BinaryFunction.cpp", 4107, __extension__ __PRETTY_FUNCTION__
))
;
4108 const MCSymbol *ColdEndSymbol =
4109 getFunctionEndLabel(FF.getFragmentNum());
4110 assert(ColdEndSymbol && ColdEndSymbol->isDefined() &&(static_cast <bool> (ColdEndSymbol && ColdEndSymbol
->isDefined() && "split function should have defined cold end symbol"
) ? void (0) : __assert_fail ("ColdEndSymbol && ColdEndSymbol->isDefined() && \"split function should have defined cold end symbol\""
, "bolt/lib/Core/BinaryFunction.cpp", 4111, __extension__ __PRETTY_FUNCTION__
))
4111 "split function should have defined cold end symbol")(static_cast <bool> (ColdEndSymbol && ColdEndSymbol
->isDefined() && "split function should have defined cold end symbol"
) ? void (0) : __assert_fail ("ColdEndSymbol && ColdEndSymbol->isDefined() && \"split function should have defined cold end symbol\""
, "bolt/lib/Core/BinaryFunction.cpp", 4111, __extension__ __PRETTY_FUNCTION__
))
;
4112 const uint64_t ColdStartOffset =
4113 Layout.getSymbolOffset(*ColdStartSymbol);
4114 const uint64_t ColdEndOffset = Layout.getSymbolOffset(*ColdEndSymbol);
4115 FF.setAddress(ColdBaseAddress + ColdStartOffset);
4116 FF.setImageSize(ColdEndOffset - ColdStartOffset);
4117 if (hasConstantIsland()) {
4118 const uint64_t DataOffset =
4119 Layout.getSymbolOffset(*getFunctionColdConstantIslandLabel());
4120 setOutputColdDataAddress(ColdBaseAddress + DataOffset);
4121 }
4122 }
4123 }
4124 } else {
4125 setOutputAddress(getAddress());
4126 setOutputSize(Layout.getSymbolOffset(*getFunctionEndLabel()));
4127 }
4128
4129 // Update basic block output ranges for the debug info, if we have
4130 // secondary entry points in the symbol table to update or if writing BAT.
4131 if (!opts::UpdateDebugSections && !isMultiEntry() &&
6
Assuming the condition is false
4132 !requiresAddressTranslation())
4133 return;
4134
4135 // Output ranges should match the input if the body hasn't changed.
4136 if (!isSimple() && !BC.HasRelocations)
7
Assuming the condition is false
4137 return;
4138
4139 // AArch64 may have functions that only contains a constant island (no code).
4140 if (getLayout().block_empty())
8
Taking false branch
4141 return;
4142
4143 for (FunctionFragment &FF : getLayout().fragments()) {
4144 if (FF.empty())
9
Assuming the condition is false
10
Taking false branch
4145 continue;
4146
4147 const uint64_t FragmentBaseAddress =
4148 getCodeSection(isSimple() ? FF.getFragmentNum() : FragmentNum::main())
11
Assuming the condition is true
12
'?' condition is true
4149 ->getOutputAddress();
4150
4151 BinaryBasicBlock *PrevBB = nullptr;
13
'PrevBB' initialized to a null pointer value
4152 for (BinaryBasicBlock *const BB : FF) {
14
Assuming '__begin3' is equal to '__end3'
4153 assert(BB->getLabel()->isDefined() && "symbol should be defined")(static_cast <bool> (BB->getLabel()->isDefined() &&
"symbol should be defined") ? void (0) : __assert_fail ("BB->getLabel()->isDefined() && \"symbol should be defined\""
, "bolt/lib/Core/BinaryFunction.cpp", 4153, __extension__ __PRETTY_FUNCTION__
))
;
4154 if (!BC.HasRelocations) {
4155 if (BB->isSplit())
4156 assert(FragmentBaseAddress == FF.getAddress())(static_cast <bool> (FragmentBaseAddress == FF.getAddress
()) ? void (0) : __assert_fail ("FragmentBaseAddress == FF.getAddress()"
, "bolt/lib/Core/BinaryFunction.cpp", 4156, __extension__ __PRETTY_FUNCTION__
))
;
4157 else
4158 assert(FragmentBaseAddress == getOutputAddress())(static_cast <bool> (FragmentBaseAddress == getOutputAddress
()) ? void (0) : __assert_fail ("FragmentBaseAddress == getOutputAddress()"
, "bolt/lib/Core/BinaryFunction.cpp", 4158, __extension__ __PRETTY_FUNCTION__
))
;
4159 }
4160
4161 const uint64_t BBOffset = Layout.getSymbolOffset(*BB->getLabel());
4162 const uint64_t BBAddress = FragmentBaseAddress + BBOffset;
4163 BB->setOutputStartAddress(BBAddress);
4164
4165 if (PrevBB)
4166 PrevBB->setOutputEndAddress(BBAddress);
4167 PrevBB = BB;
4168
4169 BB->updateOutputValues(Layout);
4170 }
4171
4172 PrevBB->setOutputEndAddress(PrevBB->isSplit()
15
Called C++ object pointer is null
4173 ? FF.getAddress() + FF.getImageSize()
4174 : getOutputAddress() + getOutputSize());
4175 }
4176}
4177
4178DebugAddressRangesVector BinaryFunction::getOutputAddressRanges() const {
4179 DebugAddressRangesVector OutputRanges;
4180
4181 if (isFolded())
4182 return OutputRanges;
4183
4184 if (IsFragment)
4185 return OutputRanges;
4186
4187 OutputRanges.emplace_back(getOutputAddress(),
4188 getOutputAddress() + getOutputSize());
4189 if (isSplit()) {
4190 assert(isEmitted() && "split function should be emitted")(static_cast <bool> (isEmitted() && "split function should be emitted"
) ? void (0) : __assert_fail ("isEmitted() && \"split function should be emitted\""
, "bolt/lib/Core/BinaryFunction.cpp", 4190, __extension__ __PRETTY_FUNCTION__
))
;
4191 for (const FunctionFragment &FF : getLayout().getSplitFragments())
4192 OutputRanges.emplace_back(FF.getAddress(),
4193 FF.getAddress() + FF.getImageSize());
4194 }
4195
4196 if (isSimple())
4197 return OutputRanges;
4198
4199 for (BinaryFunction *Frag : Fragments) {
4200 assert(!Frag->isSimple() &&(static_cast <bool> (!Frag->isSimple() && "fragment of non-simple function should also be non-simple"
) ? void (0) : __assert_fail ("!Frag->isSimple() && \"fragment of non-simple function should also be non-simple\""
, "bolt/lib/Core/BinaryFunction.cpp", 4201, __extension__ __PRETTY_FUNCTION__
))
4201 "fragment of non-simple function should also be non-simple")(static_cast <bool> (!Frag->isSimple() && "fragment of non-simple function should also be non-simple"
) ? void (0) : __assert_fail ("!Frag->isSimple() && \"fragment of non-simple function should also be non-simple\""
, "bolt/lib/Core/BinaryFunction.cpp", 4201, __extension__ __PRETTY_FUNCTION__
))
;
4202 OutputRanges.emplace_back(Frag->getOutputAddress(),
4203 Frag->getOutputAddress() + Frag->getOutputSize());
4204 }
4205
4206 return OutputRanges;
4207}
4208
4209uint64_t BinaryFunction::translateInputToOutputAddress(uint64_t Address) const {
4210 if (isFolded())
4211 return 0;
4212
4213 // If the function hasn't changed return the same address.
4214 if (!isEmitted())
4215 return Address;
4216
4217 if (Address < getAddress())
4218 return 0;
4219
4220 // Check if the address is associated with an instruction that is tracked
4221 // by address translation.
4222 auto KV = InputOffsetToAddressMap.find(Address - getAddress());
4223 if (KV != InputOffsetToAddressMap.end())
4224 return KV->second;
4225
4226 // FIXME: #18950828 - we rely on relative offsets inside basic blocks to stay
4227 // intact. Instead we can use pseudo instructions and/or annotations.
4228 const uint64_t Offset = Address - getAddress();
4229 const BinaryBasicBlock *BB = getBasicBlockContainingOffset(Offset);
4230 if (!BB) {
4231 // Special case for address immediately past the end of the function.
4232 if (Offset == getSize())
4233 return getOutputAddress() + getOutputSize();
4234
4235 return 0;
4236 }
4237
4238 return std::min(BB->getOutputAddressRange().first + Offset - BB->getOffset(),
4239 BB->getOutputAddressRange().second);
4240}
4241
4242DebugAddressRangesVector BinaryFunction::translateInputToOutputRanges(
4243 const DWARFAddressRangesVector &InputRanges) const {
4244 DebugAddressRangesVector OutputRanges;
4245
4246 if (isFolded())
4247 return OutputRanges;
4248
4249 // If the function hasn't changed return the same ranges.
4250 if (!isEmitted()) {
4251 OutputRanges.resize(InputRanges.size());
4252 llvm::transform(InputRanges, OutputRanges.begin(),
4253 [](const DWARFAddressRange &Range) {
4254 return DebugAddressRange(Range.LowPC, Range.HighPC);
4255 });
4256 return OutputRanges;
4257 }
4258
4259 // Even though we will merge ranges in a post-processing pass, we attempt to
4260 // merge them in a main processing loop as it improves the processing time.
4261 uint64_t PrevEndAddress = 0;
4262 for (const DWARFAddressRange &Range : InputRanges) {
4263 if (!containsAddress(Range.LowPC)) {
4264 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: invalid debug address range detected for "
<< *this << " : [0x" << Twine::utohexstr(Range
.LowPC) << ", 0x" << Twine::utohexstr(Range.HighPC
) << "]\n"; } } while (false)
4265 dbgs() << "BOLT-DEBUG: invalid debug address range detected for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: invalid debug address range detected for "
<< *this << " : [0x" << Twine::utohexstr(Range
.LowPC) << ", 0x" << Twine::utohexstr(Range.HighPC
) << "]\n"; } } while (false)
4266 << *this << " : [0x" << Twine::utohexstr(Range.LowPC) << ", 0x"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: invalid debug address range detected for "
<< *this << " : [0x" << Twine::utohexstr(Range
.LowPC) << ", 0x" << Twine::utohexstr(Range.HighPC
) << "]\n"; } } while (false)
4267 << Twine::utohexstr(Range.HighPC) << "]\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: invalid debug address range detected for "
<< *this << " : [0x" << Twine::utohexstr(Range
.LowPC) << ", 0x" << Twine::utohexstr(Range.HighPC
) << "]\n"; } } while (false)
;
4268 PrevEndAddress = 0;
4269 continue;
4270 }
4271 uint64_t InputOffset = Range.LowPC - getAddress();
4272 const uint64_t InputEndOffset =
4273 std::min(Range.HighPC - getAddress(), getSize());
4274
4275 auto BBI = llvm::upper_bound(BasicBlockOffsets,
4276 BasicBlockOffset(InputOffset, nullptr),
4277 CompareBasicBlockOffsets());
4278 --BBI;
4279 do {
4280 const BinaryBasicBlock *BB = BBI->second;
4281 if (InputOffset < BB->getOffset() || InputOffset >= BB->getEndOffset()) {
4282 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: invalid debug address range detected for "
<< *this << " : [0x" << Twine::utohexstr(Range
.LowPC) << ", 0x" << Twine::utohexstr(Range.HighPC
) << "]\n"; } } while (false)
4283 dbgs() << "BOLT-DEBUG: invalid debug address range detected for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: invalid debug address range detected for "
<< *this << " : [0x" << Twine::utohexstr(Range
.LowPC) << ", 0x" << Twine::utohexstr(Range.HighPC
) << "]\n"; } } while (false)
4284 << *this << " : [0x" << Twine::utohexstr(Range.LowPC)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: invalid debug address range detected for "
<< *this << " : [0x" << Twine::utohexstr(Range
.LowPC) << ", 0x" << Twine::utohexstr(Range.HighPC
) << "]\n"; } } while (false)
4285 << ", 0x" << Twine::utohexstr(Range.HighPC) << "]\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: invalid debug address range detected for "
<< *this << " : [0x" << Twine::utohexstr(Range
.LowPC) << ", 0x" << Twine::utohexstr(Range.HighPC
) << "]\n"; } } while (false)
;
4286 PrevEndAddress = 0;
4287 break;
4288 }
4289
4290 // Skip the range if the block was deleted.
4291 if (const uint64_t OutputStart = BB->getOutputAddressRange().first) {
4292 const uint64_t StartAddress =
4293 OutputStart + InputOffset - BB->getOffset();
4294 uint64_t EndAddress = BB->getOutputAddressRange().second;
4295 if (InputEndOffset < BB->getEndOffset())
4296 EndAddress = StartAddress + InputEndOffset - InputOffset;
4297
4298 if (StartAddress == PrevEndAddress) {
4299 OutputRanges.back().HighPC =
4300 std::max(OutputRanges.back().HighPC, EndAddress);
4301 } else {
4302 OutputRanges.emplace_back(StartAddress,
4303 std::max(StartAddress, EndAddress));
4304 }
4305 PrevEndAddress = OutputRanges.back().HighPC;
4306 }
4307
4308 InputOffset = BB->getEndOffset();
4309 ++BBI;
4310 } while (InputOffset < InputEndOffset);
4311 }
4312
4313 // Post-processing pass to sort and merge ranges.
4314 llvm::sort(OutputRanges);
4315 DebugAddressRangesVector MergedRanges;
4316 PrevEndAddress = 0;
4317 for (const DebugAddressRange &Range : OutputRanges) {
4318 if (Range.LowPC <= PrevEndAddress) {
4319 MergedRanges.back().HighPC =
4320 std::max(MergedRanges.back().HighPC, Range.HighPC);
4321 } else {
4322 MergedRanges.emplace_back(Range.LowPC, Range.HighPC);
4323 }
4324 PrevEndAddress = MergedRanges.back().HighPC;
4325 }
4326
4327 return MergedRanges;
4328}
4329
4330MCInst *BinaryFunction::getInstructionAtOffset(uint64_t Offset) {
4331 if (CurrentState == State::Disassembled) {
4332 auto II = Instructions.find(Offset);
4333 return (II == Instructions.end()) ? nullptr : &II->second;
4334 } else if (CurrentState == State::CFG) {
4335 BinaryBasicBlock *BB = getBasicBlockContainingOffset(Offset);
4336 if (!BB)
4337 return nullptr;
4338
4339 for (MCInst &Inst : *BB) {
4340 constexpr uint32_t InvalidOffset = std::numeric_limits<uint32_t>::max();
4341 if (Offset == BC.MIB->getOffsetWithDefault(Inst, InvalidOffset))
4342 return &Inst;
4343 }
4344
4345 if (MCInst *LastInstr = BB->getLastNonPseudoInstr()) {
4346 const uint32_t Size =
4347 BC.MIB->getAnnotationWithDefault<uint32_t>(*LastInstr, "Size");
4348 if (BB->getEndOffset() - Offset == Size)
4349 return LastInstr;
4350 }
4351
4352 return nullptr;
4353 } else {
4354 llvm_unreachable("invalid CFG state to use getInstructionAtOffset()")::llvm::llvm_unreachable_internal("invalid CFG state to use getInstructionAtOffset()"
, "bolt/lib/Core/BinaryFunction.cpp", 4354)
;
4355 }
4356}
4357
4358DebugLocationsVector BinaryFunction::translateInputToOutputLocationList(
4359 const DebugLocationsVector &InputLL) const {
4360 DebugLocationsVector OutputLL;
4361
4362 if (isFolded())
4363 return OutputLL;
4364
4365 // If the function hasn't changed - there's nothing to update.
4366 if (!isEmitted())
4367 return InputLL;
4368
4369 uint64_t PrevEndAddress = 0;
4370 SmallVectorImpl<uint8_t> *PrevExpr = nullptr;
4371 for (const DebugLocationEntry &Entry : InputLL) {
4372 const uint64_t Start = Entry.LowPC;
4373 const uint64_t End = Entry.HighPC;
4374 if (!containsAddress(Start)) {
4375 LLVM_DEBUG(dbgs() << "BOLT-DEBUG: invalid debug address range detected "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: invalid debug address range detected "
"for " << *this << " : [0x" << Twine::utohexstr
(Start) << ", 0x" << Twine::utohexstr(End) <<
"]\n"; } } while (false)
4376 "for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: invalid debug address range detected "
"for " << *this << " : [0x" << Twine::utohexstr
(Start) << ", 0x" << Twine::utohexstr(End) <<
"]\n"; } } while (false)
4377 << *this << " : [0x" << Twine::utohexstr(Start)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: invalid debug address range detected "
"for " << *this << " : [0x" << Twine::utohexstr
(Start) << ", 0x" << Twine::utohexstr(End) <<
"]\n"; } } while (false)
4378 << ", 0x" << Twine::utohexstr(End) << "]\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: invalid debug address range detected "
"for " << *this << " : [0x" << Twine::utohexstr
(Start) << ", 0x" << Twine::utohexstr(End) <<
"]\n"; } } while (false)
;
4379 continue;
4380 }
4381 uint64_t InputOffset = Start - getAddress();
4382 const uint64_t InputEndOffset = std::min(End - getAddress(), getSize());
4383 auto BBI = llvm::upper_bound(BasicBlockOffsets,
4384 BasicBlockOffset(InputOffset, nullptr),
4385 CompareBasicBlockOffsets());
4386 --BBI;
4387 do {
4388 const BinaryBasicBlock *BB = BBI->second;
4389 if (InputOffset < BB->getOffset() || InputOffset >= BB->getEndOffset()) {
4390 LLVM_DEBUG(dbgs() << "BOLT-DEBUG: invalid debug address range detected "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: invalid debug address range detected "
"for " << *this << " : [0x" << Twine::utohexstr
(Start) << ", 0x" << Twine::utohexstr(End) <<
"]\n"; } } while (false)
4391 "for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: invalid debug address range detected "
"for " << *this << " : [0x" << Twine::utohexstr
(Start) << ", 0x" << Twine::utohexstr(End) <<
"]\n"; } } while (false)
4392 << *this << " : [0x" << Twine::utohexstr(Start)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: invalid debug address range detected "
"for " << *this << " : [0x" << Twine::utohexstr
(Start) << ", 0x" << Twine::utohexstr(End) <<
"]\n"; } } while (false)
4393 << ", 0x" << Twine::utohexstr(End) << "]\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: invalid debug address range detected "
"for " << *this << " : [0x" << Twine::utohexstr
(Start) << ", 0x" << Twine::utohexstr(End) <<
"]\n"; } } while (false)
;
4394 PrevEndAddress = 0;
4395 break;
4396 }
4397
4398 // Skip the range if the block was deleted.
4399 if (const uint64_t OutputStart = BB->getOutputAddressRange().first) {
4400 const uint64_t StartAddress =
4401 OutputStart + InputOffset - BB->getOffset();
4402 uint64_t EndAddress = BB->getOutputAddressRange().second;
4403 if (InputEndOffset < BB->getEndOffset())
4404 EndAddress = StartAddress + InputEndOffset - InputOffset;
4405
4406 if (StartAddress == PrevEndAddress && Entry.Expr == *PrevExpr) {
4407 OutputLL.back().HighPC = std::max(OutputLL.back().HighPC, EndAddress);
4408 } else {
4409 OutputLL.emplace_back(DebugLocationEntry{
4410 StartAddress, std::max(StartAddress, EndAddress), Entry.Expr});
4411 }
4412 PrevEndAddress = OutputLL.back().HighPC;
4413 PrevExpr = &OutputLL.back().Expr;
4414 }
4415
4416 ++BBI;
4417 InputOffset = BB->getEndOffset();
4418 } while (InputOffset < InputEndOffset);
4419 }
4420
4421 // Sort and merge adjacent entries with identical location.
4422 llvm::stable_sort(
4423 OutputLL, [](const DebugLocationEntry &A, const DebugLocationEntry &B) {
4424 return A.LowPC < B.LowPC;
4425 });
4426 DebugLocationsVector MergedLL;
4427 PrevEndAddress = 0;
4428 PrevExpr = nullptr;
4429 for (const DebugLocationEntry &Entry : OutputLL) {
4430 if (Entry.LowPC <= PrevEndAddress && *PrevExpr == Entry.Expr) {
4431 MergedLL.back().HighPC = std::max(Entry.HighPC, MergedLL.back().HighPC);
4432 } else {
4433 const uint64_t Begin = std::max(Entry.LowPC, PrevEndAddress);
4434 const uint64_t End = std::max(Begin, Entry.HighPC);
4435 MergedLL.emplace_back(DebugLocationEntry{Begin, End, Entry.Expr});
4436 }
4437 PrevEndAddress = MergedLL.back().HighPC;
4438 PrevExpr = &MergedLL.back().Expr;
4439 }
4440
4441 return MergedLL;
4442}
4443
4444void BinaryFunction::printLoopInfo(raw_ostream &OS) const {
4445 if (!opts::shouldPrint(*this))
4446 return;
4447
4448 OS << "Loop Info for Function \"" << *this << "\"";
4449 if (hasValidProfile())
4450 OS << " (count: " << getExecutionCount() << ")";
4451 OS << "\n";
4452
4453 std::stack<BinaryLoop *> St;
4454 for (BinaryLoop *L : *BLI)
4455 St.push(L);
4456 while (!St.empty()) {
4457 BinaryLoop *L = St.top();
4458 St.pop();
4459
4460 for (BinaryLoop *Inner : *L)
4461 St.push(Inner);
4462
4463 if (!hasValidProfile())
4464 continue;
4465
4466 OS << (L->getLoopDepth() > 1 ? "Nested" : "Outer")
4467 << " loop header: " << L->getHeader()->getName();
4468 OS << "\n";
4469 OS << "Loop basic blocks: ";
4470 ListSeparator LS;
4471 for (BinaryBasicBlock *BB : L->blocks())
4472 OS << LS << BB->getName();
4473 OS << "\n";
4474 if (hasValidProfile()) {
4475 OS << "Total back edge count: " << L->TotalBackEdgeCount << "\n";
4476 OS << "Loop entry count: " << L->EntryCount << "\n";
4477 OS << "Loop exit count: " << L->ExitCount << "\n";
4478 if (L->EntryCount > 0) {
4479 OS << "Average iters per entry: "
4480 << format("%.4lf", (double)L->TotalBackEdgeCount / L->EntryCount)
4481 << "\n";
4482 }
4483 }
4484 OS << "----\n";
4485 }
4486
4487 OS << "Total number of loops: " << BLI->TotalLoops << "\n";
4488 OS << "Number of outer loops: " << BLI->OuterLoops << "\n";
4489 OS << "Maximum nested loop depth: " << BLI->MaximumDepth << "\n\n";
4490}
4491
4492bool BinaryFunction::isAArch64Veneer() const {
4493 if (empty() || hasIslandsInfo())
4494 return false;
4495
4496 BinaryBasicBlock &BB = **BasicBlocks.begin();
4497 for (MCInst &Inst : BB)
4498 if (!BC.MIB->hasAnnotation(Inst, "AArch64Veneer"))
4499 return false;
4500
4501 for (auto I = BasicBlocks.begin() + 1, E = BasicBlocks.end(); I != E; ++I) {
4502 for (MCInst &Inst : **I)
4503 if (!BC.MIB->isNoop(Inst))
4504 return false;
4505 }
4506
4507 return true;
4508}
4509
4510} // namespace bolt
4511} // namespace llvm