Bug Summary

File:build/source/bolt/lib/Core/BinaryFunction.cpp
Warning:line 2170, column 3
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/= -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-04-12-033005-16440-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 Branch Count: " << RawBranchCount;
474 OS << "\n Profile Acc : " << format("%.1f%%", ProfileMatchRatio * 100.0f);
475 }
476
477 if (opts::PrintDynoStats && !getLayout().block_empty()) {
478 OS << '\n';
479 DynoStats dynoStats = getDynoStats(*this);
480 OS << dynoStats;
481 }
482
483 OS << "\n}\n";
484
485 if (opts::PrintDynoStatsOnly || !BC.InstPrinter)
486 return;
487
488 // Offset of the instruction in function.
489 uint64_t Offset = 0;
490
491 if (BasicBlocks.empty() && !Instructions.empty()) {
492 // Print before CFG was built.
493 for (const std::pair<const uint32_t, MCInst> &II : Instructions) {
494 Offset = II.first;
495
496 // Print label if exists at this offset.
497 auto LI = Labels.find(Offset);
498 if (LI != Labels.end()) {
499 if (const MCSymbol *EntrySymbol =
500 getSecondaryEntryPointSymbol(LI->second))
501 OS << EntrySymbol->getName() << " (Entry Point):\n";
502 OS << LI->second->getName() << ":\n";
503 }
504
505 BC.printInstruction(OS, II.second, Offset, this);
506 }
507 }
508
509 StringRef SplitPointMsg = "";
510 for (const FunctionFragment &FF : Layout.fragments()) {
511 OS << SplitPointMsg;
512 SplitPointMsg = "------- HOT-COLD SPLIT POINT -------\n\n";
513 for (const BinaryBasicBlock *BB : FF) {
514 OS << BB->getName() << " (" << BB->size()
515 << " instructions, align : " << BB->getAlignment() << ")\n";
516
517 if (isEntryPoint(*BB)) {
518 if (MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(*BB))
519 OS << " Secondary Entry Point: " << EntrySymbol->getName() << '\n';
520 else
521 OS << " Entry Point\n";
522 }
523
524 if (BB->isLandingPad())
525 OS << " Landing Pad\n";
526
527 uint64_t BBExecCount = BB->getExecutionCount();
528 if (hasValidProfile()) {
529 OS << " Exec Count : ";
530 if (BB->getExecutionCount() != BinaryBasicBlock::COUNT_NO_PROFILE)
531 OS << BBExecCount << '\n';
532 else
533 OS << "<unknown>\n";
534 }
535 if (BB->getCFIState() >= 0)
536 OS << " CFI State : " << BB->getCFIState() << '\n';
537 if (opts::EnableBAT) {
538 OS << " Input offset: " << Twine::utohexstr(BB->getInputOffset())
539 << "\n";
540 }
541 if (!BB->pred_empty()) {
542 OS << " Predecessors: ";
543 ListSeparator LS;
544 for (BinaryBasicBlock *Pred : BB->predecessors())
545 OS << LS << Pred->getName();
546 OS << '\n';
547 }
548 if (!BB->throw_empty()) {
549 OS << " Throwers: ";
550 ListSeparator LS;
551 for (BinaryBasicBlock *Throw : BB->throwers())
552 OS << LS << Throw->getName();
553 OS << '\n';
554 }
555
556 Offset = alignTo(Offset, BB->getAlignment());
557
558 // Note: offsets are imprecise since this is happening prior to
559 // relaxation.
560 Offset = BC.printInstructions(OS, BB->begin(), BB->end(), Offset, this);
561
562 if (!BB->succ_empty()) {
563 OS << " Successors: ";
564 // For more than 2 successors, sort them based on frequency.
565 std::vector<uint64_t> Indices(BB->succ_size());
566 std::iota(Indices.begin(), Indices.end(), 0);
567 if (BB->succ_size() > 2 && BB->getKnownExecutionCount()) {
568 llvm::stable_sort(Indices, [&](const uint64_t A, const uint64_t B) {
569 return BB->BranchInfo[B] < BB->BranchInfo[A];
570 });
571 }
572 ListSeparator LS;
573 for (unsigned I = 0; I < Indices.size(); ++I) {
574 BinaryBasicBlock *Succ = BB->Successors[Indices[I]];
575 const BinaryBasicBlock::BinaryBranchInfo &BI =
576 BB->BranchInfo[Indices[I]];
577 OS << LS << Succ->getName();
578 if (ExecutionCount != COUNT_NO_PROFILE &&
579 BI.MispredictedCount != BinaryBasicBlock::COUNT_INFERRED) {
580 OS << " (mispreds: " << BI.MispredictedCount
581 << ", count: " << BI.Count << ")";
582 } else if (ExecutionCount != COUNT_NO_PROFILE &&
583 BI.Count != BinaryBasicBlock::COUNT_NO_PROFILE) {
584 OS << " (inferred count: " << BI.Count << ")";
585 }
586 }
587 OS << '\n';
588 }
589
590 if (!BB->lp_empty()) {
591 OS << " Landing Pads: ";
592 ListSeparator LS;
593 for (BinaryBasicBlock *LP : BB->landing_pads()) {
594 OS << LS << LP->getName();
595 if (ExecutionCount != COUNT_NO_PROFILE) {
596 OS << " (count: " << LP->getExecutionCount() << ")";
597 }
598 }
599 OS << '\n';
600 }
601
602 // In CFG_Finalized state we can miscalculate CFI state at exit.
603 if (CurrentState == State::CFG) {
604 const int32_t CFIStateAtExit = BB->getCFIStateAtExit();
605 if (CFIStateAtExit >= 0)
606 OS << " CFI State: " << CFIStateAtExit << '\n';
607 }
608
609 OS << '\n';
610 }
611 }
612
613 // Dump new exception ranges for the function.
614 if (!CallSites.empty()) {
615 OS << "EH table:\n";
616 for (const FunctionFragment &FF : getLayout().fragments()) {
617 for (const auto &FCSI : getCallSites(FF.getFragmentNum())) {
618 const CallSite &CSI = FCSI.second;
619 OS << " [" << *CSI.Start << ", " << *CSI.End << ") landing pad : ";
620 if (CSI.LP)
621 OS << *CSI.LP;
622 else
623 OS << "0";
624 OS << ", action : " << CSI.Action << '\n';
625 }
626 }
627 OS << '\n';
628 }
629
630 // Print all jump tables.
631 for (const std::pair<const uint64_t, JumpTable *> &JTI : JumpTables)
632 JTI.second->print(OS);
633
634 OS << "DWARF CFI Instructions:\n";
635 if (OffsetToCFI.size()) {
636 // Pre-buildCFG information
637 for (const std::pair<const uint32_t, uint32_t> &Elmt : OffsetToCFI) {
638 OS << format(" %08x:\t", Elmt.first);
639 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", 639, __extension__ __PRETTY_FUNCTION__
))
;
640 BinaryContext::printCFI(OS, FrameInstructions[Elmt.second]);
641 OS << "\n";
642 }
643 } else {
644 // Post-buildCFG information
645 for (uint32_t I = 0, E = FrameInstructions.size(); I != E; ++I) {
646 const MCCFIInstruction &CFI = FrameInstructions[I];
647 OS << format(" %d:\t", I);
648 BinaryContext::printCFI(OS, CFI);
649 OS << "\n";
650 }
651 }
652 if (FrameInstructions.empty())
653 OS << " <empty>\n";
654
655 OS << "End of Function \"" << *this << "\"\n\n";
656}
657
658void BinaryFunction::printRelocations(raw_ostream &OS, uint64_t Offset,
659 uint64_t Size) const {
660 const char *Sep = " # Relocs: ";
661
662 auto RI = Relocations.lower_bound(Offset);
663 while (RI != Relocations.end() && RI->first < Offset + Size) {
664 OS << Sep << "(R: " << RI->second << ")";
665 Sep = ", ";
666 ++RI;
667 }
668}
669
670static std::string mutateDWARFExpressionTargetReg(const MCCFIInstruction &Instr,
671 MCPhysReg NewReg) {
672 StringRef ExprBytes = Instr.getValues();
673 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", 673, __extension__ __PRETTY_FUNCTION__
))
;
674 uint8_t Opcode = ExprBytes[0];
675 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", 677, __extension__ __PRETTY_FUNCTION__
))
676 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", 677, __extension__ __PRETTY_FUNCTION__
))
677 "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", 677, __extension__ __PRETTY_FUNCTION__
))
;
678 (void)Opcode;
679 const uint8_t *const Start =
680 reinterpret_cast<const uint8_t *>(ExprBytes.drop_front(1).data());
681 const uint8_t *const End =
682 reinterpret_cast<const uint8_t *>(Start + ExprBytes.size() - 1);
683 unsigned Size = 0;
684 decodeULEB128(Start, &Size, End);
685 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", 685, __extension__ __PRETTY_FUNCTION__
))
;
686 SmallString<8> Tmp;
687 raw_svector_ostream OSE(Tmp);
688 encodeULEB128(NewReg, OSE);
689 return Twine(ExprBytes.slice(0, 1))
690 .concat(OSE.str())
691 .concat(ExprBytes.drop_front(1 + Size))
692 .str();
693}
694
695void BinaryFunction::mutateCFIRegisterFor(const MCInst &Instr,
696 MCPhysReg NewReg) {
697 const MCCFIInstruction *OldCFI = getCFIFor(Instr);
698 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", 698, __extension__ __PRETTY_FUNCTION__
))
;
699 switch (OldCFI->getOperation()) {
700 default:
701 llvm_unreachable("Unexpected instruction")::llvm::llvm_unreachable_internal("Unexpected instruction", "bolt/lib/Core/BinaryFunction.cpp"
, 701)
;
702 case MCCFIInstruction::OpDefCfa:
703 setCFIFor(Instr, MCCFIInstruction::cfiDefCfa(nullptr, NewReg,
704 OldCFI->getOffset()));
705 break;
706 case MCCFIInstruction::OpDefCfaRegister:
707 setCFIFor(Instr, MCCFIInstruction::createDefCfaRegister(nullptr, NewReg));
708 break;
709 case MCCFIInstruction::OpOffset:
710 setCFIFor(Instr, MCCFIInstruction::createOffset(nullptr, NewReg,
711 OldCFI->getOffset()));
712 break;
713 case MCCFIInstruction::OpRegister:
714 setCFIFor(Instr, MCCFIInstruction::createRegister(nullptr, NewReg,
715 OldCFI->getRegister2()));
716 break;
717 case MCCFIInstruction::OpSameValue:
718 setCFIFor(Instr, MCCFIInstruction::createSameValue(nullptr, NewReg));
719 break;
720 case MCCFIInstruction::OpEscape:
721 setCFIFor(Instr,
722 MCCFIInstruction::createEscape(
723 nullptr,
724 StringRef(mutateDWARFExpressionTargetReg(*OldCFI, NewReg))));
725 break;
726 case MCCFIInstruction::OpRestore:
727 setCFIFor(Instr, MCCFIInstruction::createRestore(nullptr, NewReg));
728 break;
729 case MCCFIInstruction::OpUndefined:
730 setCFIFor(Instr, MCCFIInstruction::createUndefined(nullptr, NewReg));
731 break;
732 }
733}
734
735const MCCFIInstruction *BinaryFunction::mutateCFIOffsetFor(const MCInst &Instr,
736 int64_t NewOffset) {
737 const MCCFIInstruction *OldCFI = getCFIFor(Instr);
738 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", 738, __extension__ __PRETTY_FUNCTION__
))
;
739 switch (OldCFI->getOperation()) {
740 default:
741 llvm_unreachable("Unexpected instruction")::llvm::llvm_unreachable_internal("Unexpected instruction", "bolt/lib/Core/BinaryFunction.cpp"
, 741)
;
742 case MCCFIInstruction::OpDefCfaOffset:
743 setCFIFor(Instr, MCCFIInstruction::cfiDefCfaOffset(nullptr, NewOffset));
744 break;
745 case MCCFIInstruction::OpAdjustCfaOffset:
746 setCFIFor(Instr,
747 MCCFIInstruction::createAdjustCfaOffset(nullptr, NewOffset));
748 break;
749 case MCCFIInstruction::OpDefCfa:
750 setCFIFor(Instr, MCCFIInstruction::cfiDefCfa(nullptr, OldCFI->getRegister(),
751 NewOffset));
752 break;
753 case MCCFIInstruction::OpOffset:
754 setCFIFor(Instr, MCCFIInstruction::createOffset(
755 nullptr, OldCFI->getRegister(), NewOffset));
756 break;
757 }
758 return getCFIFor(Instr);
759}
760
761IndirectBranchType
762BinaryFunction::processIndirectBranch(MCInst &Instruction, unsigned Size,
763 uint64_t Offset,
764 uint64_t &TargetAddress) {
765 const unsigned PtrSize = BC.AsmInfo->getCodePointerSize();
766
767 // The instruction referencing memory used by the branch instruction.
768 // It could be the branch instruction itself or one of the instructions
769 // setting the value of the register used by the branch.
770 MCInst *MemLocInstr;
771
772 // Address of the table referenced by MemLocInstr. Could be either an
773 // array of function pointers, or a jump table.
774 uint64_t ArrayStart = 0;
775
776 unsigned BaseRegNum, IndexRegNum;
777 int64_t DispValue;
778 const MCExpr *DispExpr;
779
780 // In AArch, identify the instruction adding the PC-relative offset to
781 // jump table entries to correctly decode it.
782 MCInst *PCRelBaseInstr;
783 uint64_t PCRelAddr = 0;
784
785 auto Begin = Instructions.begin();
786 if (BC.isAArch64()) {
787 PreserveNops = BC.HasRelocations;
788 // Start at the last label as an approximation of the current basic block.
789 // This is a heuristic, since the full set of labels have yet to be
790 // determined
791 for (const uint32_t Offset :
792 llvm::make_first_range(llvm::reverse(Labels))) {
793 auto II = Instructions.find(Offset);
794 if (II != Instructions.end()) {
795 Begin = II;
796 break;
797 }
798 }
799 }
800
801 IndirectBranchType BranchType = BC.MIB->analyzeIndirectBranch(
802 Instruction, Begin, Instructions.end(), PtrSize, MemLocInstr, BaseRegNum,
803 IndexRegNum, DispValue, DispExpr, PCRelBaseInstr);
804
805 if (BranchType == IndirectBranchType::UNKNOWN && !MemLocInstr)
806 return BranchType;
807
808 if (MemLocInstr != &Instruction)
809 IndexRegNum = BC.MIB->getNoRegister();
810
811 if (BC.isAArch64()) {
812 const MCSymbol *Sym = BC.MIB->getTargetSymbol(*PCRelBaseInstr, 1);
813 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", 813, __extension__ __PRETTY_FUNCTION__
))
;
814 ErrorOr<uint64_t> SymValueOrError = BC.getSymbolValue(*Sym);
815 if (SymValueOrError) {
816 PCRelAddr = *SymValueOrError;
817 } else {
818 for (std::pair<const uint32_t, MCSymbol *> &Elmt : Labels) {
819 if (Elmt.second == Sym) {
820 PCRelAddr = Elmt.first + getAddress();
821 break;
822 }
823 }
824 }
825 uint64_t InstrAddr = 0;
826 for (auto II = Instructions.rbegin(); II != Instructions.rend(); ++II) {
827 if (&II->second == PCRelBaseInstr) {
828 InstrAddr = II->first + getAddress();
829 break;
830 }
831 }
832 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", 832, __extension__ __PRETTY_FUNCTION__
))
;
833 // We do this to avoid spurious references to code locations outside this
834 // function (for example, if the indirect jump lives in the last basic
835 // block of the function, it will create a reference to the next function).
836 // This replaces a symbol reference with an immediate.
837 BC.MIB->replaceMemOperandDisp(*PCRelBaseInstr,
838 MCOperand::createImm(PCRelAddr - InstrAddr));
839 // FIXME: Disable full jump table processing for AArch64 until we have a
840 // proper way of determining the jump table limits.
841 return IndirectBranchType::UNKNOWN;
842 }
843
844 // RIP-relative addressing should be converted to symbol form by now
845 // in processed instructions (but not in jump).
846 if (DispExpr) {
847 const MCSymbol *TargetSym;
848 uint64_t TargetOffset;
849 std::tie(TargetSym, TargetOffset) = BC.MIB->getTargetSymbolInfo(DispExpr);
850 ErrorOr<uint64_t> SymValueOrError = BC.getSymbolValue(*TargetSym);
851 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", 851, __extension__ __PRETTY_FUNCTION__
))
;
852 ArrayStart = *SymValueOrError + TargetOffset;
853 BaseRegNum = BC.MIB->getNoRegister();
854 if (BC.isAArch64()) {
855 ArrayStart &= ~0xFFFULL;
856 ArrayStart += DispValue & 0xFFFULL;
857 }
858 } else {
859 ArrayStart = static_cast<uint64_t>(DispValue);
860 }
861
862 if (BaseRegNum == BC.MRI->getProgramCounter())
863 ArrayStart += getAddress() + Offset + Size;
864
865 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)
866 << Twine::utohexstr(ArrayStart) << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: addressed memory is 0x"
<< Twine::utohexstr(ArrayStart) << '\n'; } } while
(false)
;
867
868 ErrorOr<BinarySection &> Section = BC.getSectionForAddress(ArrayStart);
869 if (!Section) {
870 // No section - possibly an absolute address. Since we don't allow
871 // internal function addresses to escape the function scope - we
872 // consider it a tail call.
873 if (opts::Verbosity >= 1) {
874 errs() << "BOLT-WARNING: no section for address 0x"
875 << Twine::utohexstr(ArrayStart) << " referenced from function "
876 << *this << '\n';
877 }
878 return IndirectBranchType::POSSIBLE_TAIL_CALL;
879 }
880 if (Section->isVirtual()) {
881 // The contents are filled at runtime.
882 return IndirectBranchType::POSSIBLE_TAIL_CALL;
883 }
884
885 if (BranchType == IndirectBranchType::POSSIBLE_FIXED_BRANCH) {
886 ErrorOr<uint64_t> Value = BC.getPointerAtAddress(ArrayStart);
887 if (!Value)
888 return IndirectBranchType::UNKNOWN;
889
890 if (BC.getSectionForAddress(ArrayStart)->isWritable())
891 return IndirectBranchType::UNKNOWN;
892
893 outs() << "BOLT-INFO: fixed indirect branch detected in " << *this
894 << " at 0x" << Twine::utohexstr(getAddress() + Offset)
895 << " referencing data at 0x" << Twine::utohexstr(ArrayStart)
896 << " the destination value is 0x" << Twine::utohexstr(*Value)
897 << '\n';
898
899 TargetAddress = *Value;
900 return BranchType;
901 }
902
903 // Check if there's already a jump table registered at this address.
904 MemoryContentsType MemType;
905 if (JumpTable *JT = BC.getJumpTableContainingAddress(ArrayStart)) {
906 switch (JT->Type) {
907 case JumpTable::JTT_NORMAL:
908 MemType = MemoryContentsType::POSSIBLE_JUMP_TABLE;
909 break;
910 case JumpTable::JTT_PIC:
911 MemType = MemoryContentsType::POSSIBLE_PIC_JUMP_TABLE;
912 break;
913 }
914 } else {
915 MemType = BC.analyzeMemoryAt(ArrayStart, *this);
916 }
917
918 // Check that jump table type in instruction pattern matches memory contents.
919 JumpTable::JumpTableType JTType;
920 if (BranchType == IndirectBranchType::POSSIBLE_PIC_JUMP_TABLE) {
921 if (MemType != MemoryContentsType::POSSIBLE_PIC_JUMP_TABLE)
922 return IndirectBranchType::UNKNOWN;
923 JTType = JumpTable::JTT_PIC;
924 } else {
925 if (MemType == MemoryContentsType::POSSIBLE_PIC_JUMP_TABLE)
926 return IndirectBranchType::UNKNOWN;
927
928 if (MemType == MemoryContentsType::UNKNOWN)
929 return IndirectBranchType::POSSIBLE_TAIL_CALL;
930
931 BranchType = IndirectBranchType::POSSIBLE_JUMP_TABLE;
932 JTType = JumpTable::JTT_NORMAL;
933 }
934
935 // Convert the instruction into jump table branch.
936 const MCSymbol *JTLabel = BC.getOrCreateJumpTable(*this, ArrayStart, JTType);
937 BC.MIB->replaceMemOperandDisp(*MemLocInstr, JTLabel, BC.Ctx.get());
938 BC.MIB->setJumpTable(Instruction, ArrayStart, IndexRegNum);
939
940 JTSites.emplace_back(Offset, ArrayStart);
941
942 return BranchType;
943}
944
945MCSymbol *BinaryFunction::getOrCreateLocalLabel(uint64_t Address,
946 bool CreatePastEnd) {
947 const uint64_t Offset = Address - getAddress();
948
949 if ((Offset == getSize()) && CreatePastEnd)
950 return getFunctionEndLabel();
951
952 auto LI = Labels.find(Offset);
953 if (LI != Labels.end())
954 return LI->second;
955
956 // For AArch64, check if this address is part of a constant island.
957 if (BC.isAArch64()) {
958 if (MCSymbol *IslandSym = getOrCreateIslandAccess(Address))
959 return IslandSym;
960 }
961
962 MCSymbol *Label = BC.Ctx->createNamedTempSymbol();
963 Labels[Offset] = Label;
964
965 return Label;
966}
967
968ErrorOr<ArrayRef<uint8_t>> BinaryFunction::getData() const {
969 BinarySection &Section = *getOriginSection();
970 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", 971, __extension__ __PRETTY_FUNCTION__
))
971 "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", 971, __extension__ __PRETTY_FUNCTION__
))
;
972
973 if (!Section.isText() || Section.isVirtual() || !Section.getSize())
974 return std::make_error_code(std::errc::bad_address);
975
976 StringRef SectionContents = Section.getContents();
977
978 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", 979, __extension__ __PRETTY_FUNCTION__
))
979 "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", 979, __extension__ __PRETTY_FUNCTION__
))
;
980
981 // Function offset from the section start.
982 uint64_t Offset = getAddress() - Section.getAddress();
983 auto *Bytes = reinterpret_cast<const uint8_t *>(SectionContents.data());
984 return ArrayRef<uint8_t>(Bytes + Offset, getMaxSize());
985}
986
987size_t BinaryFunction::getSizeOfDataInCodeAt(uint64_t Offset) const {
988 if (!Islands)
989 return 0;
990
991 if (!llvm::is_contained(Islands->DataOffsets, Offset))
992 return 0;
993
994 auto Iter = Islands->CodeOffsets.upper_bound(Offset);
995 if (Iter != Islands->CodeOffsets.end())
996 return *Iter - Offset;
997 return getSize() - Offset;
998}
999
1000bool BinaryFunction::isZeroPaddingAt(uint64_t Offset) const {
1001 ArrayRef<uint8_t> FunctionData = *getData();
1002 uint64_t EndOfCode = getSize();
1003 if (Islands) {
1004 auto Iter = Islands->DataOffsets.upper_bound(Offset);
1005 if (Iter != Islands->DataOffsets.end())
1006 EndOfCode = *Iter;
1007 }
1008 for (uint64_t I = Offset; I < EndOfCode; ++I)
1009 if (FunctionData[I] != 0)
1010 return false;
1011
1012 return true;
1013}
1014
1015void BinaryFunction::handlePCRelOperand(MCInst &Instruction, uint64_t Address,
1016 uint64_t Size) {
1017 auto &MIB = BC.MIB;
1018 uint64_t TargetAddress = 0;
1019 if (!MIB->evaluateMemOperandTarget(Instruction, TargetAddress, Address,
1020 Size)) {
1021 errs() << "BOLT-ERROR: PC-relative operand can't be evaluated:\n";
1022 BC.InstPrinter->printInst(&Instruction, 0, "", *BC.STI, errs());
1023 errs() << '\n';
1024 Instruction.dump_pretty(errs(), BC.InstPrinter.get());
1025 errs() << '\n';
1026 errs() << "BOLT-ERROR: cannot handle PC-relative operand at 0x"
1027 << Twine::utohexstr(Address) << ". Skipping function " << *this
1028 << ".\n";
1029 if (BC.HasRelocations)
1030 exit(1);
1031 IsSimple = false;
1032 return;
1033 }
1034 if (TargetAddress == 0 && opts::Verbosity >= 1) {
1035 outs() << "BOLT-INFO: PC-relative operand is zero in function " << *this
1036 << '\n';
1037 }
1038
1039 const MCSymbol *TargetSymbol;
1040 uint64_t TargetOffset;
1041 std::tie(TargetSymbol, TargetOffset) =
1042 BC.handleAddressRef(TargetAddress, *this, /*IsPCRel*/ true);
1043
1044 bool ReplaceSuccess = MIB->replaceMemOperandDisp(
1045 Instruction, TargetSymbol, static_cast<int64_t>(TargetOffset), &*BC.Ctx);
1046 (void)ReplaceSuccess;
1047 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", 1047, __extension__ __PRETTY_FUNCTION__
))
;
1048}
1049
1050MCSymbol *BinaryFunction::handleExternalReference(MCInst &Instruction,
1051 uint64_t Size,
1052 uint64_t Offset,
1053 uint64_t TargetAddress,
1054 bool &IsCall) {
1055 auto &MIB = BC.MIB;
1056
1057 const uint64_t AbsoluteInstrAddr = getAddress() + Offset;
1058 BC.addInterproceduralReference(this, TargetAddress);
1059 if (opts::Verbosity >= 2 && !IsCall && Size == 2 && !BC.HasRelocations) {
1060 errs() << "BOLT-WARNING: relaxed tail call detected at 0x"
1061 << Twine::utohexstr(AbsoluteInstrAddr) << " in function " << *this
1062 << ". Code size will be increased.\n";
1063 }
1064
1065 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", 1066, __extension__ __PRETTY_FUNCTION__
))
1066 "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", 1066, __extension__ __PRETTY_FUNCTION__
))
;
1067
1068 // This is a call regardless of the opcode.
1069 // Assign proper opcode for tail calls, so that they could be
1070 // treated as calls.
1071 if (!IsCall) {
1072 if (!MIB->convertJmpToTailCall(Instruction)) {
1073 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", 1074, __extension__ __PRETTY_FUNCTION__
))
1074 "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", 1074, __extension__ __PRETTY_FUNCTION__
))
;
1075 if (opts::Verbosity >= 2) {
1076 errs() << "BOLT-WARNING: conditional tail call detected in "
1077 << "function " << *this << " at 0x"
1078 << Twine::utohexstr(AbsoluteInstrAddr) << ".\n";
1079 }
1080 }
1081 IsCall = true;
1082 }
1083
1084 if (opts::Verbosity >= 2 && TargetAddress == 0) {
1085 // We actually see calls to address 0 in presence of weak
1086 // symbols originating from libraries. This code is never meant
1087 // to be executed.
1088 outs() << "BOLT-INFO: Function " << *this
1089 << " has a call to address zero.\n";
1090 }
1091
1092 return BC.getOrCreateGlobalSymbol(TargetAddress, "FUNCat");
1093}
1094
1095void BinaryFunction::handleIndirectBranch(MCInst &Instruction, uint64_t Size,
1096 uint64_t Offset) {
1097 auto &MIB = BC.MIB;
1098 uint64_t IndirectTarget = 0;
1099 IndirectBranchType Result =
1100 processIndirectBranch(Instruction, Size, Offset, IndirectTarget);
1101 switch (Result) {
1102 default:
1103 llvm_unreachable("unexpected result")::llvm::llvm_unreachable_internal("unexpected result", "bolt/lib/Core/BinaryFunction.cpp"
, 1103)
;
1104 case IndirectBranchType::POSSIBLE_TAIL_CALL: {
1105 bool Result = MIB->convertJmpToTailCall(Instruction);
1106 (void)Result;
1107 assert(Result)(static_cast <bool> (Result) ? void (0) : __assert_fail
("Result", "bolt/lib/Core/BinaryFunction.cpp", 1107, __extension__
__PRETTY_FUNCTION__))
;
1108 break;
1109 }
1110 case IndirectBranchType::POSSIBLE_JUMP_TABLE:
1111 case IndirectBranchType::POSSIBLE_PIC_JUMP_TABLE:
1112 if (opts::JumpTables == JTS_NONE)
1113 IsSimple = false;
1114 break;
1115 case IndirectBranchType::POSSIBLE_FIXED_BRANCH: {
1116 if (containsAddress(IndirectTarget)) {
1117 const MCSymbol *TargetSymbol = getOrCreateLocalLabel(IndirectTarget);
1118 Instruction.clear();
1119 MIB->createUncondBranch(Instruction, TargetSymbol, BC.Ctx.get());
1120 TakenBranches.emplace_back(Offset, IndirectTarget - getAddress());
1121 HasFixedIndirectBranch = true;
1122 } else {
1123 MIB->convertJmpToTailCall(Instruction);
1124 BC.addInterproceduralReference(this, IndirectTarget);
1125 }
1126 break;
1127 }
1128 case IndirectBranchType::UNKNOWN:
1129 // Keep processing. We'll do more checks and fixes in
1130 // postProcessIndirectBranches().
1131 UnknownIndirectBranchOffsets.emplace(Offset);
1132 break;
1133 }
1134}
1135
1136void BinaryFunction::handleAArch64IndirectCall(MCInst &Instruction,
1137 const uint64_t Offset) {
1138 auto &MIB = BC.MIB;
1139 const uint64_t AbsoluteInstrAddr = getAddress() + Offset;
1140 MCInst *TargetHiBits, *TargetLowBits;
1141 uint64_t TargetAddress, Count;
1142 Count = MIB->matchLinkerVeneer(Instructions.begin(), Instructions.end(),
1143 AbsoluteInstrAddr, Instruction, TargetHiBits,
1144 TargetLowBits, TargetAddress);
1145 if (Count) {
1146 MIB->addAnnotation(Instruction, "AArch64Veneer", true);
1147 --Count;
1148 for (auto It = std::prev(Instructions.end()); Count != 0;
1149 It = std::prev(It), --Count) {
1150 MIB->addAnnotation(It->second, "AArch64Veneer", true);
1151 }
1152
1153 BC.addAdrpAddRelocAArch64(*this, *TargetLowBits, *TargetHiBits,
1154 TargetAddress);
1155 }
1156}
1157
1158bool BinaryFunction::disassemble() {
1159 NamedRegionTimer T("disassemble", "Disassemble function", "buildfuncs",
1160 "Build Binary Functions", opts::TimeBuild);
1161 ErrorOr<ArrayRef<uint8_t>> ErrorOrFunctionData = getData();
1162 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", 1162, __extension__ __PRETTY_FUNCTION__
))
;
1163 ArrayRef<uint8_t> FunctionData = *ErrorOrFunctionData;
1164 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", 1165, __extension__ __PRETTY_FUNCTION__
))
1165 "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", 1165, __extension__ __PRETTY_FUNCTION__
))
;
1166
1167 auto &Ctx = BC.Ctx;
1168 auto &MIB = BC.MIB;
1169
1170 BC.SymbolicDisAsm->setSymbolizer(MIB->createTargetSymbolizer(*this));
1171
1172 // Insert a label at the beginning of the function. This will be our first
1173 // basic block.
1174 Labels[0] = Ctx->createNamedTempSymbol("BB0");
1175
1176 uint64_t Size = 0; // instruction size
1177 for (uint64_t Offset = 0; Offset < getSize(); Offset += Size) {
1178 MCInst Instruction;
1179 const uint64_t AbsoluteInstrAddr = getAddress() + Offset;
1180
1181 // Check for data inside code and ignore it
1182 if (const size_t DataInCodeSize = getSizeOfDataInCodeAt(Offset)) {
1183 Size = DataInCodeSize;
1184 continue;
1185 }
1186
1187 if (!BC.SymbolicDisAsm->getInstruction(Instruction, Size,
1188 FunctionData.slice(Offset),
1189 AbsoluteInstrAddr, nulls())) {
1190 // Functions with "soft" boundaries, e.g. coming from assembly source,
1191 // can have 0-byte padding at the end.
1192 if (isZeroPaddingAt(Offset))
1193 break;
1194
1195 errs() << "BOLT-WARNING: unable to disassemble instruction at offset 0x"
1196 << Twine::utohexstr(Offset) << " (address 0x"
1197 << Twine::utohexstr(AbsoluteInstrAddr) << ") in function " << *this
1198 << '\n';
1199 // Some AVX-512 instructions could not be disassembled at all.
1200 if (BC.HasRelocations && opts::TrapOnAVX512 && BC.isX86()) {
1201 setTrapOnEntry();
1202 BC.TrappedFunctions.push_back(this);
1203 } else {
1204 setIgnored();
1205 }
1206
1207 break;
1208 }
1209
1210 // Check integrity of LLVM assembler/disassembler.
1211 if (opts::CheckEncoding && !BC.MIB->isBranch(Instruction) &&
1212 !BC.MIB->isCall(Instruction) && !BC.MIB->isNoop(Instruction)) {
1213 if (!BC.validateInstructionEncoding(FunctionData.slice(Offset, Size))) {
1214 errs() << "BOLT-WARNING: mismatching LLVM encoding detected in "
1215 << "function " << *this << " for instruction :\n";
1216 BC.printInstruction(errs(), Instruction, AbsoluteInstrAddr);
1217 errs() << '\n';
1218 }
1219 }
1220
1221 // Special handling for AVX-512 instructions.
1222 if (MIB->hasEVEXEncoding(Instruction)) {
1223 if (BC.HasRelocations && opts::TrapOnAVX512) {
1224 setTrapOnEntry();
1225 BC.TrappedFunctions.push_back(this);
1226 break;
1227 }
1228
1229 if (!BC.validateInstructionEncoding(FunctionData.slice(Offset, Size))) {
1230 errs() << "BOLT-WARNING: internal assembler/disassembler error "
1231 "detected for AVX512 instruction:\n";
1232 BC.printInstruction(errs(), Instruction, AbsoluteInstrAddr);
1233 errs() << " in function " << *this << '\n';
1234 setIgnored();
1235 break;
1236 }
1237 }
1238
1239 if (MIB->isBranch(Instruction) || MIB->isCall(Instruction)) {
1240 uint64_t TargetAddress = 0;
1241 if (MIB->evaluateBranch(Instruction, AbsoluteInstrAddr, Size,
1242 TargetAddress)) {
1243 // Check if the target is within the same function. Otherwise it's
1244 // a call, possibly a tail call.
1245 //
1246 // If the target *is* the function address it could be either a branch
1247 // or a recursive call.
1248 bool IsCall = MIB->isCall(Instruction);
1249 const bool IsCondBranch = MIB->isConditionalBranch(Instruction);
1250 MCSymbol *TargetSymbol = nullptr;
1251
1252 if (BC.MIB->isUnsupportedBranch(Instruction.getOpcode())) {
1253 setIgnored();
1254 if (BinaryFunction *TargetFunc =
1255 BC.getBinaryFunctionContainingAddress(TargetAddress))
1256 TargetFunc->setIgnored();
1257 }
1258
1259 if (IsCall && containsAddress(TargetAddress)) {
1260 if (TargetAddress == getAddress()) {
1261 // Recursive call.
1262 TargetSymbol = getSymbol();
1263 } else {
1264 if (BC.isX86()) {
1265 // Dangerous old-style x86 PIC code. We may need to freeze this
1266 // function, so preserve the function as is for now.
1267 PreserveNops = true;
1268 } else {
1269 errs() << "BOLT-WARNING: internal call detected at 0x"
1270 << Twine::utohexstr(AbsoluteInstrAddr) << " in function "
1271 << *this << ". Skipping.\n";
1272 IsSimple = false;
1273 }
1274 }
1275 }
1276
1277 if (!TargetSymbol) {
1278 // Create either local label or external symbol.
1279 if (containsAddress(TargetAddress)) {
1280 TargetSymbol = getOrCreateLocalLabel(TargetAddress);
1281 } else {
1282 if (TargetAddress == getAddress() + getSize() &&
1283 TargetAddress < getAddress() + getMaxSize() &&
1284 !(BC.isAArch64() &&
1285 BC.handleAArch64Veneer(TargetAddress, /*MatchOnly*/ true))) {
1286 // Result of __builtin_unreachable().
1287 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)
1288 << 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)
1289 << " 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)
1290 << " : 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)
;
1291 BC.MIB->createNoop(Instruction);
1292 if (IsCondBranch) {
1293 // Register branch offset for profile validation.
1294 IgnoredBranches.emplace_back(Offset, Offset + Size);
1295 }
1296 goto add_instruction;
1297 }
1298 // May update Instruction and IsCall
1299 TargetSymbol = handleExternalReference(Instruction, Size, Offset,
1300 TargetAddress, IsCall);
1301 }
1302 }
1303
1304 if (!IsCall) {
1305 // Add taken branch info.
1306 TakenBranches.emplace_back(Offset, TargetAddress - getAddress());
1307 }
1308 BC.MIB->replaceBranchTarget(Instruction, TargetSymbol, &*Ctx);
1309
1310 // Mark CTC.
1311 if (IsCondBranch && IsCall)
1312 MIB->setConditionalTailCall(Instruction, TargetAddress);
1313 } else {
1314 // Could not evaluate branch. Should be an indirect call or an
1315 // indirect branch. Bail out on the latter case.
1316 if (MIB->isIndirectBranch(Instruction))
1317 handleIndirectBranch(Instruction, Size, Offset);
1318 // Indirect call. We only need to fix it if the operand is RIP-relative.
1319 if (IsSimple && MIB->hasPCRelOperand(Instruction))
1320 handlePCRelOperand(Instruction, AbsoluteInstrAddr, Size);
1321
1322 if (BC.isAArch64())
1323 handleAArch64IndirectCall(Instruction, Offset);
1324 }
1325 } else if (BC.isAArch64()) {
1326 // Check if there's a relocation associated with this instruction.
1327 bool UsedReloc = false;
1328 for (auto Itr = Relocations.lower_bound(Offset),
1329 ItrE = Relocations.lower_bound(Offset + Size);
1330 Itr != ItrE; ++Itr) {
1331 const Relocation &Relocation = Itr->second;
1332 int64_t Value = Relocation.Value;
1333 const bool Result = BC.MIB->replaceImmWithSymbolRef(
1334 Instruction, Relocation.Symbol, Relocation.Addend, Ctx.get(), Value,
1335 Relocation.Type);
1336 (void)Result;
1337 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", 1337, __extension__ __PRETTY_FUNCTION__
))
;
1338
1339 // For aarch64, if we replaced an immediate with a symbol from a
1340 // relocation, we mark it so we do not try to further process a
1341 // pc-relative operand. All we need is the symbol.
1342 UsedReloc = true;
1343 }
1344
1345 if (MIB->hasPCRelOperand(Instruction) && !UsedReloc)
1346 handlePCRelOperand(Instruction, AbsoluteInstrAddr, Size);
1347 }
1348
1349add_instruction:
1350 if (getDWARFLineTable()) {
1351 Instruction.setLoc(findDebugLineInformationForInstructionAt(
1352 AbsoluteInstrAddr, getDWARFUnit(), getDWARFLineTable()));
1353 }
1354
1355 // Record offset of the instruction for profile matching.
1356 if (BC.keepOffsetForInstruction(Instruction))
1357 MIB->setOffset(Instruction, static_cast<uint32_t>(Offset));
1358
1359 if (BC.MIB->isNoop(Instruction)) {
1360 // NOTE: disassembly loses the correct size information for noops.
1361 // E.g. nopw 0x0(%rax,%rax,1) is 9 bytes, but re-encoded it's only
1362 // 5 bytes. Preserve the size info using annotations.
1363 MIB->addAnnotation(Instruction, "Size", static_cast<uint32_t>(Size));
1364 }
1365
1366 addInstruction(Offset, std::move(Instruction));
1367 }
1368
1369 // Reset symbolizer for the disassembler.
1370 BC.SymbolicDisAsm->setSymbolizer(nullptr);
1371
1372 if (uint64_t Offset = getFirstInstructionOffset())
1373 Labels[Offset] = BC.Ctx->createNamedTempSymbol();
1374
1375 clearList(Relocations);
1376
1377 if (!IsSimple) {
1378 clearList(Instructions);
1379 return false;
1380 }
1381
1382 updateState(State::Disassembled);
1383
1384 return true;
1385}
1386
1387bool BinaryFunction::scanExternalRefs() {
1388 bool Success = true;
1389 bool DisassemblyFailed = false;
1390
1391 // Ignore pseudo functions.
1392 if (isPseudo())
1393 return Success;
1394
1395 if (opts::NoScan) {
1396 clearList(Relocations);
1397 clearList(ExternallyReferencedOffsets);
1398
1399 return false;
1400 }
1401
1402 // List of external references for this function.
1403 std::vector<Relocation> FunctionRelocations;
1404
1405 static BinaryContext::IndependentCodeEmitter Emitter =
1406 BC.createIndependentMCCodeEmitter();
1407
1408 ErrorOr<ArrayRef<uint8_t>> ErrorOrFunctionData = getData();
1409 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", 1409, __extension__ __PRETTY_FUNCTION__
))
;
1410 ArrayRef<uint8_t> FunctionData = *ErrorOrFunctionData;
1411 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", 1412, __extension__ __PRETTY_FUNCTION__
))
1412 "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", 1412, __extension__ __PRETTY_FUNCTION__
))
;
1413
1414 uint64_t Size = 0; // instruction size
1415 for (uint64_t Offset = 0; Offset < getSize(); Offset += Size) {
1416 // Check for data inside code and ignore it
1417 if (const size_t DataInCodeSize = getSizeOfDataInCodeAt(Offset)) {
1418 Size = DataInCodeSize;
1419 continue;
1420 }
1421
1422 const uint64_t AbsoluteInstrAddr = getAddress() + Offset;
1423 MCInst Instruction;
1424 if (!BC.DisAsm->getInstruction(Instruction, Size,
1425 FunctionData.slice(Offset),
1426 AbsoluteInstrAddr, nulls())) {
1427 if (opts::Verbosity >= 1 && !isZeroPaddingAt(Offset)) {
1428 errs() << "BOLT-WARNING: unable to disassemble instruction at offset 0x"
1429 << Twine::utohexstr(Offset) << " (address 0x"
1430 << Twine::utohexstr(AbsoluteInstrAddr) << ") in function "
1431 << *this << '\n';
1432 }
1433 Success = false;
1434 DisassemblyFailed = true;
1435 break;
1436 }
1437
1438 // Return true if we can skip handling the Target function reference.
1439 auto ignoreFunctionRef = [&](const BinaryFunction &Target) {
1440 if (&Target == this)
1441 return true;
1442
1443 // Note that later we may decide not to emit Target function. In that
1444 // case, we conservatively create references that will be ignored or
1445 // resolved to the same function.
1446 if (!BC.shouldEmit(Target))
1447 return true;
1448
1449 return false;
1450 };
1451
1452 // Return true if we can ignore reference to the symbol.
1453 auto ignoreReference = [&](const MCSymbol *TargetSymbol) {
1454 if (!TargetSymbol)
1455 return true;
1456
1457 if (BC.forceSymbolRelocations(TargetSymbol->getName()))
1458 return false;
1459
1460 BinaryFunction *TargetFunction = BC.getFunctionForSymbol(TargetSymbol);
1461 if (!TargetFunction)
1462 return true;
1463
1464 return ignoreFunctionRef(*TargetFunction);
1465 };
1466
1467 // Detect if the instruction references an address.
1468 // Without relocations, we can only trust PC-relative address modes.
1469 uint64_t TargetAddress = 0;
1470 bool IsPCRel = false;
1471 bool IsBranch = false;
1472 if (BC.MIB->hasPCRelOperand(Instruction)) {
1473 IsPCRel = BC.MIB->evaluateMemOperandTarget(Instruction, TargetAddress,
1474 AbsoluteInstrAddr, Size);
1475 } else if (BC.MIB->isCall(Instruction) || BC.MIB->isBranch(Instruction)) {
1476 IsBranch = BC.MIB->evaluateBranch(Instruction, AbsoluteInstrAddr, Size,
1477 TargetAddress);
1478 }
1479
1480 MCSymbol *TargetSymbol = nullptr;
1481
1482 // Create an entry point at reference address if needed.
1483 BinaryFunction *TargetFunction =
1484 BC.getBinaryFunctionContainingAddress(TargetAddress);
1485 if (TargetFunction && !ignoreFunctionRef(*TargetFunction)) {
1486 const uint64_t FunctionOffset =
1487 TargetAddress - TargetFunction->getAddress();
1488 TargetSymbol = FunctionOffset
1489 ? TargetFunction->addEntryPointAtOffset(FunctionOffset)
1490 : TargetFunction->getSymbol();
1491 }
1492
1493 // Can't find more references and not creating relocations.
1494 if (!BC.HasRelocations)
1495 continue;
1496
1497 // Create a relocation against the TargetSymbol as the symbol might get
1498 // moved.
1499 if (TargetSymbol) {
1500 if (IsBranch) {
1501 BC.MIB->replaceBranchTarget(Instruction, TargetSymbol,
1502 Emitter.LocalCtx.get());
1503 } else if (IsPCRel) {
1504 const MCExpr *Expr = MCSymbolRefExpr::create(
1505 TargetSymbol, MCSymbolRefExpr::VK_None, *Emitter.LocalCtx.get());
1506 BC.MIB->replaceMemOperandDisp(
1507 Instruction, MCOperand::createExpr(BC.MIB->getTargetExprFor(
1508 Instruction, Expr, *Emitter.LocalCtx.get(), 0)));
1509 }
1510 }
1511
1512 // Create more relocations based on input file relocations.
1513 bool HasRel = false;
1514 for (auto Itr = Relocations.lower_bound(Offset),
1515 ItrE = Relocations.lower_bound(Offset + Size);
1516 Itr != ItrE; ++Itr) {
1517 Relocation &Relocation = Itr->second;
1518 if (Relocation.isPCRelative() && BC.isX86())
1519 continue;
1520 if (ignoreReference(Relocation.Symbol))
1521 continue;
1522
1523 int64_t Value = Relocation.Value;
1524 const bool Result = BC.MIB->replaceImmWithSymbolRef(
1525 Instruction, Relocation.Symbol, Relocation.Addend,
1526 Emitter.LocalCtx.get(), Value, Relocation.Type);
1527 (void)Result;
1528 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", 1528, __extension__ __PRETTY_FUNCTION__
))
;
1529
1530 HasRel = true;
1531 }
1532
1533 if (!TargetSymbol && !HasRel)
1534 continue;
1535
1536 // Emit the instruction using temp emitter and generate relocations.
1537 SmallString<256> Code;
1538 SmallVector<MCFixup, 4> Fixups;
1539 Emitter.MCE->encodeInstruction(Instruction, Code, 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()) {
1
Assuming the condition is false
2
Taking false branch
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)
3
Assuming field 'CurrentState' is equal to 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__
))
;
4
Taking false branch
5
'?' condition is true
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__
))
6
'?' condition is true
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) {
7
Loop condition is false. Execution continues on line 2088
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)
;
8
Taking false branch
9
Assuming 'DebugFlag' is false
10
Loop condition is false. Exiting loop
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) {
11
Assuming '__begin2' is equal to '__end2'
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) {
12
Assuming '__begin2' is equal to '__end2'
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;
13
Null pointer value stored to 'PrevBB'
2164 for (BinaryBasicBlock *BB : BasicBlocks) {
14
Assuming '__begin2' is equal to '__end2'
2165 Layout.addBasicBlock(BB);
2166 if (PrevBB)
2167 PrevBB->setEndOffset(BB->getOffset());
2168 PrevBB = BB;
2169 }
2170 PrevBB->setEndOffset(getSize());
15
Called C++ object pointer is null
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 = RegRule.contains(Reg) ? RegRule[Reg] : UNKNOWN;
2593 if (CurRegRule == UNKNOWN) {
2594 if (Instr.getOperation() == MCCFIInstruction::OpRestore ||
2595 Instr.getOperation() == MCCFIInstruction::OpSameValue)
2596 return true;
2597 return false;
2598 }
2599 const MCCFIInstruction &LastDef =
2600 CurRegRule < 0 ? CIE[-CurRegRule] : FDE[CurRegRule];
2601 return LastDef == Instr;
2602 }
2603 case MCCFIInstruction::OpDefCfaRegister:
2604 if (RestoredCFAReg)
2605 return true;
2606 RestoredCFAReg = true;
2607 return CFAReg == Instr.getRegister();
2608 case MCCFIInstruction::OpDefCfaOffset:
2609 if (RestoredCFAOffset)
2610 return true;
2611 RestoredCFAOffset = true;
2612 return CFAOffset == Instr.getOffset();
2613 case MCCFIInstruction::OpDefCfa:
2614 if (RestoredCFAReg && RestoredCFAOffset)
2615 return true;
2616 RestoredCFAReg = true;
2617 RestoredCFAOffset = true;
2618 return CFAReg == Instr.getRegister() && CFAOffset == Instr.getOffset();
2619 case MCCFIInstruction::OpAdjustCfaOffset:
2620 case MCCFIInstruction::OpWindowSave:
2621 case MCCFIInstruction::OpNegateRAState:
2622 case MCCFIInstruction::OpLLVMDefAspaceCfa:
2623 llvm_unreachable("unsupported CFI opcode")::llvm::llvm_unreachable_internal("unsupported CFI opcode", "bolt/lib/Core/BinaryFunction.cpp"
, 2623)
;
2624 return false;
2625 case MCCFIInstruction::OpRememberState:
2626 case MCCFIInstruction::OpRestoreState:
2627 case MCCFIInstruction::OpGnuArgsSize:
2628 // do not affect CFI state
2629 return true;
2630 }
2631 return false;
2632 }
2633};
2634
2635} // end anonymous namespace
2636
2637bool BinaryFunction::replayCFIInstrs(int32_t FromState, int32_t ToState,
2638 BinaryBasicBlock *InBB,
2639 BinaryBasicBlock::iterator InsertIt) {
2640 if (FromState == ToState)
2641 return true;
2642 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", 2642, __extension__ __PRETTY_FUNCTION__
))
;
2643
2644 CFISnapshotDiff CFIDiff(CIEFrameInstructions, FrameInstructions,
2645 FrameRestoreEquivalents, FromState);
2646
2647 std::vector<uint32_t> NewCFIs;
2648 for (int32_t CurState = FromState; CurState < ToState; ++CurState) {
2649 MCCFIInstruction *Instr = &FrameInstructions[CurState];
2650 if (Instr->getOperation() == MCCFIInstruction::OpRestoreState) {
2651 auto Iter = FrameRestoreEquivalents.find(CurState);
2652 assert(Iter != FrameRestoreEquivalents.end())(static_cast <bool> (Iter != FrameRestoreEquivalents.end
()) ? void (0) : __assert_fail ("Iter != FrameRestoreEquivalents.end()"
, "bolt/lib/Core/BinaryFunction.cpp", 2652, __extension__ __PRETTY_FUNCTION__
))
;
2653 NewCFIs.insert(NewCFIs.end(), Iter->second.begin(), Iter->second.end());
2654 // RestoreState / Remember will be filtered out later by CFISnapshotDiff,
2655 // so we might as well fall-through here.
2656 }
2657 NewCFIs.push_back(CurState);
2658 }
2659
2660 // Replay instructions while avoiding duplicates
2661 for (int32_t State : llvm::reverse(NewCFIs)) {
2662 if (CFIDiff.isRedundant(FrameInstructions[State]))
2663 continue;
2664 InsertIt = addCFIPseudo(InBB, InsertIt, State);
2665 }
2666
2667 return true;
2668}
2669
2670SmallVector<int32_t, 4>
2671BinaryFunction::unwindCFIState(int32_t FromState, int32_t ToState,
2672 BinaryBasicBlock *InBB,
2673 BinaryBasicBlock::iterator &InsertIt) {
2674 SmallVector<int32_t, 4> NewStates;
2675
2676 CFISnapshot ToCFITable(CIEFrameInstructions, FrameInstructions,
2677 FrameRestoreEquivalents, ToState);
2678 CFISnapshotDiff FromCFITable(ToCFITable);
2679 FromCFITable.advanceTo(FromState);
2680
2681 auto undoStateDefCfa = [&]() {
2682 if (ToCFITable.CFARule == CFISnapshot::UNKNOWN) {
2683 FrameInstructions.emplace_back(MCCFIInstruction::cfiDefCfa(
2684 nullptr, ToCFITable.CFAReg, ToCFITable.CFAOffset));
2685 if (FromCFITable.isRedundant(FrameInstructions.back())) {
2686 FrameInstructions.pop_back();
2687 return;
2688 }
2689 NewStates.push_back(FrameInstructions.size() - 1);
2690 InsertIt = addCFIPseudo(InBB, InsertIt, FrameInstructions.size() - 1);
2691 ++InsertIt;
2692 } else if (ToCFITable.CFARule < 0) {
2693 if (FromCFITable.isRedundant(CIEFrameInstructions[-ToCFITable.CFARule]))
2694 return;
2695 NewStates.push_back(FrameInstructions.size());
2696 InsertIt = addCFIPseudo(InBB, InsertIt, FrameInstructions.size());
2697 ++InsertIt;
2698 FrameInstructions.emplace_back(CIEFrameInstructions[-ToCFITable.CFARule]);
2699 } else if (!FromCFITable.isRedundant(
2700 FrameInstructions[ToCFITable.CFARule])) {
2701 NewStates.push_back(ToCFITable.CFARule);
2702 InsertIt = addCFIPseudo(InBB, InsertIt, ToCFITable.CFARule);
2703 ++InsertIt;
2704 }
2705 };
2706
2707 auto undoState = [&](const MCCFIInstruction &Instr) {
2708 switch (Instr.getOperation()) {
2709 case MCCFIInstruction::OpRememberState:
2710 case MCCFIInstruction::OpRestoreState:
2711 break;
2712 case MCCFIInstruction::OpSameValue:
2713 case MCCFIInstruction::OpRelOffset:
2714 case MCCFIInstruction::OpOffset:
2715 case MCCFIInstruction::OpRestore:
2716 case MCCFIInstruction::OpUndefined:
2717 case MCCFIInstruction::OpEscape:
2718 case MCCFIInstruction::OpRegister: {
2719 uint32_t Reg;
2720 if (Instr.getOperation() != MCCFIInstruction::OpEscape) {
2721 Reg = Instr.getRegister();
2722 } else {
2723 std::optional<uint8_t> R =
2724 readDWARFExpressionTargetReg(Instr.getValues());
2725 // Handle DW_CFA_def_cfa_expression
2726 if (!R) {
2727 undoStateDefCfa();
2728 return;
2729 }
2730 Reg = *R;
2731 }
2732
2733 if (!ToCFITable.RegRule.contains(Reg)) {
2734 FrameInstructions.emplace_back(
2735 MCCFIInstruction::createRestore(nullptr, Reg));
2736 if (FromCFITable.isRedundant(FrameInstructions.back())) {
2737 FrameInstructions.pop_back();
2738 break;
2739 }
2740 NewStates.push_back(FrameInstructions.size() - 1);
2741 InsertIt = addCFIPseudo(InBB, InsertIt, FrameInstructions.size() - 1);
2742 ++InsertIt;
2743 break;
2744 }
2745 const int32_t Rule = ToCFITable.RegRule[Reg];
2746 if (Rule < 0) {
2747 if (FromCFITable.isRedundant(CIEFrameInstructions[-Rule]))
2748 break;
2749 NewStates.push_back(FrameInstructions.size());
2750 InsertIt = addCFIPseudo(InBB, InsertIt, FrameInstructions.size());
2751 ++InsertIt;
2752 FrameInstructions.emplace_back(CIEFrameInstructions[-Rule]);
2753 break;
2754 }
2755 if (FromCFITable.isRedundant(FrameInstructions[Rule]))
2756 break;
2757 NewStates.push_back(Rule);
2758 InsertIt = addCFIPseudo(InBB, InsertIt, Rule);
2759 ++InsertIt;
2760 break;
2761 }
2762 case MCCFIInstruction::OpDefCfaRegister:
2763 case MCCFIInstruction::OpDefCfaOffset:
2764 case MCCFIInstruction::OpDefCfa:
2765 undoStateDefCfa();
2766 break;
2767 case MCCFIInstruction::OpAdjustCfaOffset:
2768 case MCCFIInstruction::OpWindowSave:
2769 case MCCFIInstruction::OpNegateRAState:
2770 case MCCFIInstruction::OpLLVMDefAspaceCfa:
2771 llvm_unreachable("unsupported CFI opcode")::llvm::llvm_unreachable_internal("unsupported CFI opcode", "bolt/lib/Core/BinaryFunction.cpp"
, 2771)
;
2772 break;
2773 case MCCFIInstruction::OpGnuArgsSize:
2774 // do not affect CFI state
2775 break;
2776 }
2777 };
2778
2779 // Undo all modifications from ToState to FromState
2780 for (int32_t I = ToState, E = FromState; I != E; ++I) {
2781 const MCCFIInstruction &Instr = FrameInstructions[I];
2782 if (Instr.getOperation() != MCCFIInstruction::OpRestoreState) {
2783 undoState(Instr);
2784 continue;
2785 }
2786 auto Iter = FrameRestoreEquivalents.find(I);
2787 if (Iter == FrameRestoreEquivalents.end())
2788 continue;
2789 for (int32_t State : Iter->second)
2790 undoState(FrameInstructions[State]);
2791 }
2792
2793 return NewStates;
2794}
2795
2796void BinaryFunction::normalizeCFIState() {
2797 // Reordering blocks with remember-restore state instructions can be specially
2798 // tricky. When rewriting the CFI, we omit remember-restore state instructions
2799 // entirely. For restore state, we build a map expanding each restore to the
2800 // equivalent unwindCFIState sequence required at that point to achieve the
2801 // same effect of the restore. All remember state are then just ignored.
2802 std::stack<int32_t> Stack;
2803 for (BinaryBasicBlock *CurBB : Layout.blocks()) {
2804 for (auto II = CurBB->begin(); II != CurBB->end(); ++II) {
2805 if (const MCCFIInstruction *CFI = getCFIFor(*II)) {
2806 if (CFI->getOperation() == MCCFIInstruction::OpRememberState) {
2807 Stack.push(II->getOperand(0).getImm());
2808 continue;
2809 }
2810 if (CFI->getOperation() == MCCFIInstruction::OpRestoreState) {
2811 const int32_t RememberState = Stack.top();
2812 const int32_t CurState = II->getOperand(0).getImm();
2813 FrameRestoreEquivalents[CurState] =
2814 unwindCFIState(CurState, RememberState, CurBB, II);
2815 Stack.pop();
2816 }
2817 }
2818 }
2819 }
2820}
2821
2822bool BinaryFunction::finalizeCFIState() {
2823 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "Trying to fix CFI states for each BB after reordering.\n"
; } } while (false)
2824 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)
;
2825 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)
2826 << ": ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "This is the list of CFI states for each BB of "
<< *this << ": "; } } while (false)
;
2827
2828 const char *Sep = "";
2829 (void)Sep;
2830 for (FunctionFragment &FF : Layout.fragments()) {
2831 // Hot-cold border: at start of each region (with a different FDE) we need
2832 // to reset the CFI state.
2833 int32_t State = 0;
2834
2835 for (BinaryBasicBlock *BB : FF) {
2836 const int32_t CFIStateAtExit = BB->getCFIStateAtExit();
2837
2838 // We need to recover the correct state if it doesn't match expected
2839 // state at BB entry point.
2840 if (BB->getCFIState() < State) {
2841 // In this case, State is currently higher than what this BB expect it
2842 // to be. To solve this, we need to insert CFI instructions to undo
2843 // the effect of all CFI from BB's state to current State.
2844 auto InsertIt = BB->begin();
2845 unwindCFIState(State, BB->getCFIState(), BB, InsertIt);
2846 } else if (BB->getCFIState() > State) {
2847 // If BB's CFI state is greater than State, it means we are behind in
2848 // the state. Just emit all instructions to reach this state at the
2849 // beginning of this BB. If this sequence of instructions involve
2850 // remember state or restore state, bail out.
2851 if (!replayCFIInstrs(State, BB->getCFIState(), BB, BB->begin()))
2852 return false;
2853 }
2854
2855 State = CFIStateAtExit;
2856 LLVM_DEBUG(dbgs() << Sep << State; Sep = ", ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << Sep << State; Sep = ", "; }
} while (false)
;
2857 }
2858 }
2859 LLVM_DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "\n"; } } while (false)
;
2860
2861 for (BinaryBasicBlock &BB : blocks()) {
2862 for (auto II = BB.begin(); II != BB.end();) {
2863 const MCCFIInstruction *CFI = getCFIFor(*II);
2864 if (CFI && (CFI->getOperation() == MCCFIInstruction::OpRememberState ||
2865 CFI->getOperation() == MCCFIInstruction::OpRestoreState)) {
2866 II = BB.eraseInstruction(II);
2867 } else {
2868 ++II;
2869 }
2870 }
2871 }
2872
2873 return true;
2874}
2875
2876bool BinaryFunction::requiresAddressTranslation() const {
2877 return opts::EnableBAT || hasSDTMarker() || hasPseudoProbe();
2878}
2879
2880uint64_t BinaryFunction::getInstructionCount() const {
2881 uint64_t Count = 0;
2882 for (const BinaryBasicBlock &BB : blocks())
2883 Count += BB.getNumNonPseudos();
2884 return Count;
2885}
2886
2887void BinaryFunction::clearDisasmState() {
2888 clearList(Instructions);
2889 clearList(IgnoredBranches);
2890 clearList(TakenBranches);
2891
2892 if (BC.HasRelocations) {
2893 for (std::pair<const uint32_t, MCSymbol *> &LI : Labels)
2894 BC.UndefinedSymbols.insert(LI.second);
2895 for (MCSymbol *const EndLabel : FunctionEndLabels)
2896 if (EndLabel)
2897 BC.UndefinedSymbols.insert(EndLabel);
2898 }
2899}
2900
2901void BinaryFunction::setTrapOnEntry() {
2902 clearDisasmState();
2903
2904 forEachEntryPoint([&](uint64_t Offset, const MCSymbol *Label) -> bool {
2905 MCInst TrapInstr;
2906 BC.MIB->createTrap(TrapInstr);
2907 addInstruction(Offset, std::move(TrapInstr));
2908 return true;
2909 });
2910
2911 TrapsOnEntry = true;
2912}
2913
2914void BinaryFunction::setIgnored() {
2915 if (opts::processAllFunctions()) {
2916 // We can accept ignored functions before they've been disassembled.
2917 // In that case, they would still get disassembled and emited, but not
2918 // optimized.
2919 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", 2920, __extension__ __PRETTY_FUNCTION__
))
2920 "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", 2920, __extension__ __PRETTY_FUNCTION__
))
;
2921 IsIgnored = true;
2922 return;
2923 }
2924
2925 clearDisasmState();
2926
2927 // Clear CFG state too.
2928 if (hasCFG()) {
2929 releaseCFG();
2930
2931 for (BinaryBasicBlock *BB : BasicBlocks)
2932 delete BB;
2933 clearList(BasicBlocks);
2934
2935 for (BinaryBasicBlock *BB : DeletedBasicBlocks)
2936 delete BB;
2937 clearList(DeletedBasicBlocks);
2938
2939 Layout.clear();
2940 }
2941
2942 CurrentState = State::Empty;
2943
2944 IsIgnored = true;
2945 IsSimple = false;
2946 LLVM_DEBUG(dbgs() << "Ignoring " << getPrintName() << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "Ignoring " << getPrintName
() << '\n'; } } while (false)
;
2947}
2948
2949void BinaryFunction::duplicateConstantIslands() {
2950 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", 2950, __extension__ __PRETTY_FUNCTION__
))
;
2951
2952 for (BinaryBasicBlock *BB : getLayout().blocks()) {
2953 if (!BB->isCold())
2954 continue;
2955
2956 for (MCInst &Inst : *BB) {
2957 int OpNum = 0;
2958 for (MCOperand &Operand : Inst) {
2959 if (!Operand.isExpr()) {
2960 ++OpNum;
2961 continue;
2962 }
2963 const MCSymbol *Symbol = BC.MIB->getTargetSymbol(Inst, OpNum);
2964 // Check if this is an island symbol
2965 if (!Islands->Symbols.count(Symbol) &&
2966 !Islands->ProxySymbols.count(Symbol))
2967 continue;
2968
2969 // Create cold symbol, if missing
2970 auto ISym = Islands->ColdSymbols.find(Symbol);
2971 MCSymbol *ColdSymbol;
2972 if (ISym != Islands->ColdSymbols.end()) {
2973 ColdSymbol = ISym->second;
2974 } else {
2975 ColdSymbol = BC.Ctx->getOrCreateSymbol(Symbol->getName() + ".cold");
2976 Islands->ColdSymbols[Symbol] = ColdSymbol;
2977 // Check if this is a proxy island symbol and update owner proxy map
2978 if (Islands->ProxySymbols.count(Symbol)) {
2979 BinaryFunction *Owner = Islands->ProxySymbols[Symbol];
2980 auto IProxiedSym = Owner->Islands->Proxies[this].find(Symbol);
2981 Owner->Islands->ColdProxies[this][IProxiedSym->second] = ColdSymbol;
2982 }
2983 }
2984
2985 // Update instruction reference
2986 Operand = MCOperand::createExpr(BC.MIB->getTargetExprFor(
2987 Inst,
2988 MCSymbolRefExpr::create(ColdSymbol, MCSymbolRefExpr::VK_None,
2989 *BC.Ctx),
2990 *BC.Ctx, 0));
2991 ++OpNum;
2992 }
2993 }
2994 }
2995}
2996
2997#ifndef MAX_PATH255
2998#define MAX_PATH255 255
2999#endif
3000
3001static std::string constructFilename(std::string Filename,
3002 std::string Annotation,
3003 std::string Suffix) {
3004 std::replace(Filename.begin(), Filename.end(), '/', '-');
3005 if (!Annotation.empty())
3006 Annotation.insert(0, "-");
3007 if (Filename.size() + Annotation.size() + Suffix.size() > MAX_PATH255) {
3008 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", 3008, __extension__ __PRETTY_FUNCTION__
))
;
3009 if (opts::Verbosity >= 1) {
3010 errs() << "BOLT-WARNING: Filename \"" << Filename << Annotation << Suffix
3011 << "\" exceeds the " << MAX_PATH255 << " size limit, truncating.\n";
3012 }
3013 Filename.resize(MAX_PATH255 - (Suffix.size() + Annotation.size()));
3014 }
3015 Filename += Annotation;
3016 Filename += Suffix;
3017 return Filename;
3018}
3019
3020static std::string formatEscapes(const std::string &Str) {
3021 std::string Result;
3022 for (unsigned I = 0; I < Str.size(); ++I) {
3023 char C = Str[I];
3024 switch (C) {
3025 case '\n':
3026 Result += "&#13;";
3027 break;
3028 case '"':
3029 break;
3030 default:
3031 Result += C;
3032 break;
3033 }
3034 }
3035 return Result;
3036}
3037
3038void BinaryFunction::dumpGraph(raw_ostream &OS) const {
3039 OS << "digraph \"" << getPrintName() << "\" {\n"
3040 << "node [fontname=courier, shape=box, style=filled, colorscheme=brbg9]\n";
3041 uint64_t Offset = Address;
3042 for (BinaryBasicBlock *BB : BasicBlocks) {
3043 auto LayoutPos = find(Layout.blocks(), BB);
3044 unsigned LayoutIndex = LayoutPos - Layout.block_begin();
3045 const char *ColdStr = BB->isCold() ? " (cold)" : "";
3046 std::vector<std::string> Attrs;
3047 // Bold box for entry points
3048 if (isEntryPoint(*BB))
3049 Attrs.push_back("penwidth=2");
3050 if (BLI && BLI->getLoopFor(BB)) {
3051 // Distinguish innermost loops
3052 const BinaryLoop *Loop = BLI->getLoopFor(BB);
3053 if (Loop->isInnermost())
3054 Attrs.push_back("fillcolor=6");
3055 else // some outer loop
3056 Attrs.push_back("fillcolor=4");
3057 } else { // non-loopy code
3058 Attrs.push_back("fillcolor=5");
3059 }
3060 ListSeparator LS;
3061 OS << "\"" << BB->getName() << "\" [";
3062 for (StringRef Attr : Attrs)
3063 OS << LS << Attr;
3064 OS << "]\n";
3065 OS << format("\"%s\" [label=\"%s%s\\n(C:%lu,O:%lu,I:%u,L:%u,CFI:%u)\\n",
3066 BB->getName().data(), BB->getName().data(), ColdStr,
3067 BB->getKnownExecutionCount(), BB->getOffset(), getIndex(BB),
3068 LayoutIndex, BB->getCFIState());
3069
3070 if (opts::DotToolTipCode) {
3071 std::string Str;
3072 raw_string_ostream CS(Str);
3073 Offset = BC.printInstructions(CS, BB->begin(), BB->end(), Offset, this,
3074 /* PrintMCInst = */ false,
3075 /* PrintMemData = */ false,
3076 /* PrintRelocations = */ false,
3077 /* Endl = */ R"(\\l)");
3078 OS << formatEscapes(CS.str()) << '\n';
3079 }
3080 OS << "\"]\n";
3081
3082 // analyzeBranch is just used to get the names of the branch
3083 // opcodes.
3084 const MCSymbol *TBB = nullptr;
3085 const MCSymbol *FBB = nullptr;
3086 MCInst *CondBranch = nullptr;
3087 MCInst *UncondBranch = nullptr;
3088 const bool Success = BB->analyzeBranch(TBB, FBB, CondBranch, UncondBranch);
3089
3090 const MCInst *LastInstr = BB->getLastNonPseudoInstr();
3091 const bool IsJumpTable = LastInstr && BC.MIB->getJumpTable(*LastInstr);
3092
3093 auto BI = BB->branch_info_begin();
3094 for (BinaryBasicBlock *Succ : BB->successors()) {
3095 std::string Branch;
3096 if (Success) {
3097 if (Succ == BB->getConditionalSuccessor(true)) {
3098 Branch = CondBranch ? std::string(BC.InstPrinter->getOpcodeName(
3099 CondBranch->getOpcode()))
3100 : "TB";
3101 } else if (Succ == BB->getConditionalSuccessor(false)) {
3102 Branch = UncondBranch ? std::string(BC.InstPrinter->getOpcodeName(
3103 UncondBranch->getOpcode()))
3104 : "FB";
3105 } else {
3106 Branch = "FT";
3107 }
3108 }
3109 if (IsJumpTable)
3110 Branch = "JT";
3111 OS << format("\"%s\" -> \"%s\" [label=\"%s", BB->getName().data(),
3112 Succ->getName().data(), Branch.c_str());
3113
3114 if (BB->getExecutionCount() != COUNT_NO_PROFILE &&
3115 BI->MispredictedCount != BinaryBasicBlock::COUNT_INFERRED) {
3116 OS << "\\n(C:" << BI->Count << ",M:" << BI->MispredictedCount << ")";
3117 } else if (ExecutionCount != COUNT_NO_PROFILE &&
3118 BI->Count != BinaryBasicBlock::COUNT_NO_PROFILE) {
3119 OS << "\\n(IC:" << BI->Count << ")";
3120 }
3121 OS << "\"]\n";
3122
3123 ++BI;
3124 }
3125 for (BinaryBasicBlock *LP : BB->landing_pads()) {
3126 OS << format("\"%s\" -> \"%s\" [constraint=false style=dashed]\n",
3127 BB->getName().data(), LP->getName().data());
3128 }
3129 }
3130 OS << "}\n";
3131}
3132
3133void BinaryFunction::viewGraph() const {
3134 SmallString<MAX_PATH255> Filename;
3135 if (std::error_code EC =
3136 sys::fs::createTemporaryFile("bolt-cfg", "dot", Filename)) {
3137 errs() << "BOLT-ERROR: " << EC.message() << ", unable to create "
3138 << " bolt-cfg-XXXXX.dot temporary file.\n";
3139 return;
3140 }
3141 dumpGraphToFile(std::string(Filename));
3142 if (DisplayGraph(Filename))
3143 errs() << "BOLT-ERROR: Can't display " << Filename << " with graphviz.\n";
3144 if (std::error_code EC = sys::fs::remove(Filename)) {
3145 errs() << "BOLT-WARNING: " << EC.message() << ", failed to remove "
3146 << Filename << "\n";
3147 }
3148}
3149
3150void BinaryFunction::dumpGraphForPass(std::string Annotation) const {
3151 if (!opts::shouldPrint(*this))
3152 return;
3153
3154 std::string Filename = constructFilename(getPrintName(), Annotation, ".dot");
3155 if (opts::Verbosity >= 1)
3156 outs() << "BOLT-INFO: dumping CFG to " << Filename << "\n";
3157 dumpGraphToFile(Filename);
3158}
3159
3160void BinaryFunction::dumpGraphToFile(std::string Filename) const {
3161 std::error_code EC;
3162 raw_fd_ostream of(Filename, EC, sys::fs::OF_None);
3163 if (EC) {
3164 if (opts::Verbosity >= 1) {
3165 errs() << "BOLT-WARNING: " << EC.message() << ", unable to open "
3166 << Filename << " for output.\n";
3167 }
3168 return;
3169 }
3170 dumpGraph(of);
3171}
3172
3173bool BinaryFunction::validateCFG() const {
3174 bool Valid = true;
3175 for (BinaryBasicBlock *BB : BasicBlocks)
3176 Valid &= BB->validateSuccessorInvariants();
3177
3178 if (!Valid)
3179 return Valid;
3180
3181 // Make sure all blocks in CFG are valid.
3182 auto validateBlock = [this](const BinaryBasicBlock *BB, StringRef Desc) {
3183 if (!BB->isValid()) {
3184 errs() << "BOLT-ERROR: deleted " << Desc << " " << BB->getName()
3185 << " detected in:\n";
3186 this->dump();
3187 return false;
3188 }
3189 return true;
3190 };
3191 for (const BinaryBasicBlock *BB : BasicBlocks) {
3192 if (!validateBlock(BB, "block"))
3193 return false;
3194 for (const BinaryBasicBlock *PredBB : BB->predecessors())
3195 if (!validateBlock(PredBB, "predecessor"))
3196 return false;
3197 for (const BinaryBasicBlock *SuccBB : BB->successors())
3198 if (!validateBlock(SuccBB, "successor"))
3199 return false;
3200 for (const BinaryBasicBlock *LP : BB->landing_pads())
3201 if (!validateBlock(LP, "landing pad"))
3202 return false;
3203 for (const BinaryBasicBlock *Thrower : BB->throwers())
3204 if (!validateBlock(Thrower, "thrower"))
3205 return false;
3206 }
3207
3208 for (const BinaryBasicBlock *BB : BasicBlocks) {
3209 std::unordered_set<const BinaryBasicBlock *> BBLandingPads;
3210 for (const BinaryBasicBlock *LP : BB->landing_pads()) {
3211 if (BBLandingPads.count(LP)) {
3212 errs() << "BOLT-ERROR: duplicate landing pad detected in"
3213 << BB->getName() << " in function " << *this << '\n';
3214 return false;
3215 }
3216 BBLandingPads.insert(LP);
3217 }
3218
3219 std::unordered_set<const BinaryBasicBlock *> BBThrowers;
3220 for (const BinaryBasicBlock *Thrower : BB->throwers()) {
3221 if (BBThrowers.count(Thrower)) {
3222 errs() << "BOLT-ERROR: duplicate thrower detected in" << BB->getName()
3223 << " in function " << *this << '\n';
3224 return false;
3225 }
3226 BBThrowers.insert(Thrower);
3227 }
3228
3229 for (const BinaryBasicBlock *LPBlock : BB->landing_pads()) {
3230 if (!llvm::is_contained(LPBlock->throwers(), BB)) {
3231 errs() << "BOLT-ERROR: inconsistent landing pad detected in " << *this
3232 << ": " << BB->getName() << " is in LandingPads but not in "
3233 << LPBlock->getName() << " Throwers\n";
3234 return false;
3235 }
3236 }
3237 for (const BinaryBasicBlock *Thrower : BB->throwers()) {
3238 if (!llvm::is_contained(Thrower->landing_pads(), BB)) {
3239 errs() << "BOLT-ERROR: inconsistent thrower detected in " << *this
3240 << ": " << BB->getName() << " is in Throwers list but not in "
3241 << Thrower->getName() << " LandingPads\n";
3242 return false;
3243 }
3244 }
3245 }
3246
3247 return Valid;
3248}
3249
3250void BinaryFunction::fixBranches() {
3251 auto &MIB = BC.MIB;
3252 MCContext *Ctx = BC.Ctx.get();
3253
3254 for (BinaryBasicBlock *BB : BasicBlocks) {
3255 const MCSymbol *TBB = nullptr;
3256 const MCSymbol *FBB = nullptr;
3257 MCInst *CondBranch = nullptr;
3258 MCInst *UncondBranch = nullptr;
3259 if (!BB->analyzeBranch(TBB, FBB, CondBranch, UncondBranch))
3260 continue;
3261
3262 // We will create unconditional branch with correct destination if needed.
3263 if (UncondBranch)
3264 BB->eraseInstruction(BB->findInstruction(UncondBranch));
3265
3266 // Basic block that follows the current one in the final layout.
3267 const BinaryBasicBlock *NextBB =
3268 Layout.getBasicBlockAfter(BB, /*IgnoreSplits=*/false);
3269
3270 if (BB->succ_size() == 1) {
3271 // __builtin_unreachable() could create a conditional branch that
3272 // falls-through into the next function - hence the block will have only
3273 // one valid successor. Since behaviour is undefined - we replace
3274 // the conditional branch with an unconditional if required.
3275 if (CondBranch)
3276 BB->eraseInstruction(BB->findInstruction(CondBranch));
3277 if (BB->getSuccessor() == NextBB)
3278 continue;
3279 BB->addBranchInstruction(BB->getSuccessor());
3280 } else if (BB->succ_size() == 2) {
3281 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", 3281, __extension__ __PRETTY_FUNCTION__
))
;
3282 const BinaryBasicBlock *TSuccessor = BB->getConditionalSuccessor(true);
3283 const BinaryBasicBlock *FSuccessor = BB->getConditionalSuccessor(false);
3284 // Check whether we support reversing this branch direction
3285 const bool IsSupported =
3286 !MIB->isUnsupportedBranch(CondBranch->getOpcode());
3287 if (NextBB && NextBB == TSuccessor && IsSupported) {
3288 std::swap(TSuccessor, FSuccessor);
3289 {
3290 auto L = BC.scopeLock();
3291 MIB->reverseBranchCondition(*CondBranch, TSuccessor->getLabel(), Ctx);
3292 }
3293 BB->swapConditionalSuccessors();
3294 } else {
3295 auto L = BC.scopeLock();
3296 MIB->replaceBranchTarget(*CondBranch, TSuccessor->getLabel(), Ctx);
3297 }
3298 if (TSuccessor == FSuccessor)
3299 BB->removeDuplicateConditionalSuccessor(CondBranch);
3300 if (!NextBB ||
3301 ((NextBB != TSuccessor || !IsSupported) && NextBB != FSuccessor)) {
3302 // If one of the branches is guaranteed to be "long" while the other
3303 // could be "short", then prioritize short for "taken". This will
3304 // generate a sequence 1 byte shorter on x86.
3305 if (IsSupported && BC.isX86() &&
3306 TSuccessor->getFragmentNum() != FSuccessor->getFragmentNum() &&
3307 BB->getFragmentNum() != TSuccessor->getFragmentNum()) {
3308 std::swap(TSuccessor, FSuccessor);
3309 {
3310 auto L = BC.scopeLock();
3311 MIB->reverseBranchCondition(*CondBranch, TSuccessor->getLabel(),
3312 Ctx);
3313 }
3314 BB->swapConditionalSuccessors();
3315 }
3316 BB->addBranchInstruction(FSuccessor);
3317 }
3318 }
3319 // Cases where the number of successors is 0 (block ends with a
3320 // terminator) or more than 2 (switch table) don't require branch
3321 // instruction adjustments.
3322 }
3323 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", 3324, __extension__ __PRETTY_FUNCTION__
))
3324 "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", 3324, __extension__ __PRETTY_FUNCTION__
))
;
3325}
3326
3327void BinaryFunction::propagateGnuArgsSizeInfo(
3328 MCPlusBuilder::AllocatorIdTy AllocId) {
3329 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", 3329, __extension__ __PRETTY_FUNCTION__
))
;
3330
3331 if (!hasEHRanges() || !usesGnuArgsSize())
3332 return;
3333
3334 // The current value of DW_CFA_GNU_args_size affects all following
3335 // invoke instructions until the next CFI overrides it.
3336 // It is important to iterate basic blocks in the original order when
3337 // assigning the value.
3338 uint64_t CurrentGnuArgsSize = 0;
3339 for (BinaryBasicBlock *BB : BasicBlocks) {
3340 for (auto II = BB->begin(); II != BB->end();) {
3341 MCInst &Instr = *II;
3342 if (BC.MIB->isCFI(Instr)) {
3343 const MCCFIInstruction *CFI = getCFIFor(Instr);
3344 if (CFI->getOperation() == MCCFIInstruction::OpGnuArgsSize) {
3345 CurrentGnuArgsSize = CFI->getOffset();
3346 // Delete DW_CFA_GNU_args_size instructions and only regenerate
3347 // during the final code emission. The information is embedded
3348 // inside call instructions.
3349 II = BB->erasePseudoInstruction(II);
3350 continue;
3351 }
3352 } else if (BC.MIB->isInvoke(Instr)) {
3353 // Add the value of GNU_args_size as an extra operand to invokes.
3354 BC.MIB->addGnuArgsSize(Instr, CurrentGnuArgsSize, AllocId);
3355 }
3356 ++II;
3357 }
3358 }
3359}
3360
3361void BinaryFunction::postProcessBranches() {
3362 if (!isSimple())
3363 return;
3364 for (BinaryBasicBlock &BB : blocks()) {
3365 auto LastInstrRI = BB.getLastNonPseudo();
3366 if (BB.succ_size() == 1) {
3367 if (LastInstrRI != BB.rend() &&
3368 BC.MIB->isConditionalBranch(*LastInstrRI)) {
3369 // __builtin_unreachable() could create a conditional branch that
3370 // falls-through into the next function - hence the block will have only
3371 // one valid successor. Such behaviour is undefined and thus we remove
3372 // the conditional branch while leaving a valid successor.
3373 BB.eraseInstruction(std::prev(LastInstrRI.base()));
3374 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)
3375 << 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)
;
3376 }
3377 } else if (BB.succ_size() == 0) {
3378 // Ignore unreachable basic blocks.
3379 if (BB.pred_size() == 0 || BB.isLandingPad())
3380 continue;
3381
3382 // If it's the basic block that does not end up with a terminator - we
3383 // insert a return instruction unless it's a call instruction.
3384 if (LastInstrRI == BB.rend()) {
3385 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)
3386 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)
3387 << 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)
;
3388 continue;
3389 }
3390 if (!BC.MIB->isTerminator(*LastInstrRI) &&
3391 !BC.MIB->isCall(*LastInstrRI)) {
3392 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)
3393 << 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)
;
3394 MCInst ReturnInstr;
3395 BC.MIB->createReturn(ReturnInstr);
3396 BB.addInstruction(ReturnInstr);
3397 }
3398 }
3399 }
3400 assert(validateCFG() && "invalid CFG")(static_cast <bool> (validateCFG() && "invalid CFG"
) ? void (0) : __assert_fail ("validateCFG() && \"invalid CFG\""
, "bolt/lib/Core/BinaryFunction.cpp", 3400, __extension__ __PRETTY_FUNCTION__
))
;
3401}
3402
3403MCSymbol *BinaryFunction::addEntryPointAtOffset(uint64_t Offset) {
3404 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", 3404, __extension__ __PRETTY_FUNCTION__
))
;
3405 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", 3405, __extension__ __PRETTY_FUNCTION__
))
;
3406
3407 const uint64_t EntryPointAddress = getAddress() + Offset;
3408 MCSymbol *LocalSymbol = getOrCreateLocalLabel(EntryPointAddress);
3409
3410 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(LocalSymbol);
3411 if (EntrySymbol)
3412 return EntrySymbol;
3413
3414 if (BinaryData *EntryBD = BC.getBinaryDataAtAddress(EntryPointAddress)) {
3415 EntrySymbol = EntryBD->getSymbol();
3416 } else {
3417 EntrySymbol = BC.getOrCreateGlobalSymbol(
3418 EntryPointAddress, Twine("__ENTRY_") + getOneName() + "@");
3419 }
3420 SecondaryEntryPoints[LocalSymbol] = EntrySymbol;
3421
3422 BC.setSymbolToFunctionMap(EntrySymbol, this);
3423
3424 return EntrySymbol;
3425}
3426
3427MCSymbol *BinaryFunction::addEntryPoint(const BinaryBasicBlock &BB) {
3428 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", 3429, __extension__ __PRETTY_FUNCTION__
))
3429 "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", 3429, __extension__ __PRETTY_FUNCTION__
))
;
3430
3431 if (&BB == BasicBlocks.front())
3432 return getSymbol();
3433
3434 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(BB);
3435 if (EntrySymbol)
3436 return EntrySymbol;
3437
3438 EntrySymbol =
3439 BC.Ctx->getOrCreateSymbol("__ENTRY_" + BB.getLabel()->getName());
3440
3441 SecondaryEntryPoints[BB.getLabel()] = EntrySymbol;
3442
3443 BC.setSymbolToFunctionMap(EntrySymbol, this);
3444
3445 return EntrySymbol;
3446}
3447
3448MCSymbol *BinaryFunction::getSymbolForEntryID(uint64_t EntryID) {
3449 if (EntryID == 0)
3450 return getSymbol();
3451
3452 if (!isMultiEntry())
3453 return nullptr;
3454
3455 uint64_t NumEntries = 0;
3456 if (hasCFG()) {
3457 for (BinaryBasicBlock *BB : BasicBlocks) {
3458 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(*BB);
3459 if (!EntrySymbol)
3460 continue;
3461 if (NumEntries == EntryID)
3462 return EntrySymbol;
3463 ++NumEntries;
3464 }
3465 } else {
3466 for (std::pair<const uint32_t, MCSymbol *> &KV : Labels) {
3467 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(KV.second);
3468 if (!EntrySymbol)
3469 continue;
3470 if (NumEntries == EntryID)
3471 return EntrySymbol;
3472 ++NumEntries;
3473 }
3474 }
3475
3476 return nullptr;
3477}
3478
3479uint64_t BinaryFunction::getEntryIDForSymbol(const MCSymbol *Symbol) const {
3480 if (!isMultiEntry())
3481 return 0;
3482
3483 for (const MCSymbol *FunctionSymbol : getSymbols())
3484 if (FunctionSymbol == Symbol)
3485 return 0;
3486
3487 // Check all secondary entries available as either basic blocks or lables.
3488 uint64_t NumEntries = 0;
3489 for (const BinaryBasicBlock *BB : BasicBlocks) {
3490 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(*BB);
3491 if (!EntrySymbol)
3492 continue;
3493 if (EntrySymbol == Symbol)
3494 return NumEntries;
3495 ++NumEntries;
3496 }
3497 NumEntries = 0;
3498 for (const std::pair<const uint32_t, MCSymbol *> &KV : Labels) {
3499 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(KV.second);
3500 if (!EntrySymbol)
3501 continue;
3502 if (EntrySymbol == Symbol)
3503 return NumEntries;
3504 ++NumEntries;
3505 }
3506
3507 llvm_unreachable("symbol not found")::llvm::llvm_unreachable_internal("symbol not found", "bolt/lib/Core/BinaryFunction.cpp"
, 3507)
;
3508}
3509
3510bool BinaryFunction::forEachEntryPoint(EntryPointCallbackTy Callback) const {
3511 bool Status = Callback(0, getSymbol());
3512 if (!isMultiEntry())
3513 return Status;
3514
3515 for (const std::pair<const uint32_t, MCSymbol *> &KV : Labels) {
3516 if (!Status)
3517 break;
3518
3519 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(KV.second);
3520 if (!EntrySymbol)
3521 continue;
3522
3523 Status = Callback(KV.first, EntrySymbol);
3524 }
3525
3526 return Status;
3527}
3528
3529BinaryFunction::BasicBlockListType BinaryFunction::dfs() const {
3530 BasicBlockListType DFS;
3531 unsigned Index = 0;
3532 std::stack<BinaryBasicBlock *> Stack;
3533
3534 // Push entry points to the stack in reverse order.
3535 //
3536 // NB: we rely on the original order of entries to match.
3537 SmallVector<BinaryBasicBlock *> EntryPoints;
3538 llvm::copy_if(BasicBlocks, std::back_inserter(EntryPoints),
3539 [&](const BinaryBasicBlock *const BB) { return isEntryPoint(*BB); });
3540 // Sort entry points by their offset to make sure we got them in the right
3541 // order.
3542 llvm::stable_sort(EntryPoints, [](const BinaryBasicBlock *const A,
3543 const BinaryBasicBlock *const B) {
3544 return A->getOffset() < B->getOffset();
3545 });
3546 for (BinaryBasicBlock *const BB : reverse(EntryPoints))
3547 Stack.push(BB);
3548
3549 for (BinaryBasicBlock &BB : blocks())
3550 BB.setLayoutIndex(BinaryBasicBlock::InvalidIndex);
3551
3552 while (!Stack.empty()) {
3553 BinaryBasicBlock *BB = Stack.top();
3554 Stack.pop();
3555
3556 if (BB->getLayoutIndex() != BinaryBasicBlock::InvalidIndex)
3557 continue;
3558
3559 BB->setLayoutIndex(Index++);
3560 DFS.push_back(BB);
3561
3562 for (BinaryBasicBlock *SuccBB : BB->landing_pads()) {
3563 Stack.push(SuccBB);
3564 }
3565
3566 const MCSymbol *TBB = nullptr;
3567 const MCSymbol *FBB = nullptr;
3568 MCInst *CondBranch = nullptr;
3569 MCInst *UncondBranch = nullptr;
3570 if (BB->analyzeBranch(TBB, FBB, CondBranch, UncondBranch) && CondBranch &&
3571 BB->succ_size() == 2) {
3572 if (BC.MIB->getCanonicalBranchCondCode(BC.MIB->getCondCode(
3573 *CondBranch)) == BC.MIB->getCondCode(*CondBranch)) {
3574 Stack.push(BB->getConditionalSuccessor(true));
3575 Stack.push(BB->getConditionalSuccessor(false));
3576 } else {
3577 Stack.push(BB->getConditionalSuccessor(false));
3578 Stack.push(BB->getConditionalSuccessor(true));
3579 }
3580 } else {
3581 for (BinaryBasicBlock *SuccBB : BB->successors()) {
3582 Stack.push(SuccBB);
3583 }
3584 }
3585 }
3586
3587 return DFS;
3588}
3589
3590size_t BinaryFunction::computeHash(bool UseDFS,
3591 OperandHashFuncTy OperandHashFunc) const {
3592 if (size() == 0)
3593 return 0;
3594
3595 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", 3595, __extension__ __PRETTY_FUNCTION__
))
;
3596
3597 SmallVector<const BinaryBasicBlock *, 0> Order;
3598 if (UseDFS)
3599 llvm::copy(dfs(), std::back_inserter(Order));
3600 else
3601 llvm::copy(Layout.blocks(), std::back_inserter(Order));
3602
3603 // The hash is computed by creating a string of all instruction opcodes and
3604 // possibly their operands and then hashing that string with std::hash.
3605 std::string HashString;
3606 for (const BinaryBasicBlock *BB : Order) {
3607 for (const MCInst &Inst : *BB) {
3608 unsigned Opcode = Inst.getOpcode();
3609
3610 if (BC.MIB->isPseudo(Inst))
3611 continue;
3612
3613 // Ignore unconditional jumps since we check CFG consistency by processing
3614 // basic blocks in order and do not rely on branches to be in-sync with
3615 // CFG. Note that we still use condition code of conditional jumps.
3616 if (BC.MIB->isUnconditionalBranch(Inst))
3617 continue;
3618
3619 if (Opcode == 0)
3620 HashString.push_back(0);
3621
3622 while (Opcode) {
3623 uint8_t LSB = Opcode & 0xff;
3624 HashString.push_back(LSB);
3625 Opcode = Opcode >> 8;
3626 }
3627
3628 for (const MCOperand &Op : MCPlus::primeOperands(Inst))
3629 HashString.append(OperandHashFunc(Op));
3630 }
3631 }
3632
3633 return Hash = std::hash<std::string>{}(HashString);
3634}
3635
3636void BinaryFunction::insertBasicBlocks(
3637 BinaryBasicBlock *Start,
3638 std::vector<std::unique_ptr<BinaryBasicBlock>> &&NewBBs,
3639 const bool UpdateLayout, const bool UpdateCFIState,
3640 const bool RecomputeLandingPads) {
3641 const int64_t StartIndex = Start ? getIndex(Start) : -1LL;
3642 const size_t NumNewBlocks = NewBBs.size();
3643
3644 BasicBlocks.insert(BasicBlocks.begin() + (StartIndex + 1), NumNewBlocks,
3645 nullptr);
3646
3647 int64_t I = StartIndex + 1;
3648 for (std::unique_ptr<BinaryBasicBlock> &BB : NewBBs) {
3649 assert(!BasicBlocks[I])(static_cast <bool> (!BasicBlocks[I]) ? void (0) : __assert_fail
("!BasicBlocks[I]", "bolt/lib/Core/BinaryFunction.cpp", 3649
, __extension__ __PRETTY_FUNCTION__))
;
3650 BasicBlocks[I++] = BB.release();
3651 }
3652
3653 if (RecomputeLandingPads)
3654 recomputeLandingPads();
3655 else
3656 updateBBIndices(0);
3657
3658 if (UpdateLayout)
3659 updateLayout(Start, NumNewBlocks);
3660
3661 if (UpdateCFIState)
3662 updateCFIState(Start, NumNewBlocks);
3663}
3664
3665BinaryFunction::iterator BinaryFunction::insertBasicBlocks(
3666 BinaryFunction::iterator StartBB,
3667 std::vector<std::unique_ptr<BinaryBasicBlock>> &&NewBBs,
3668 const bool UpdateLayout, const bool UpdateCFIState,
3669 const bool RecomputeLandingPads) {
3670 const unsigned StartIndex = getIndex(&*StartBB);
3671 const size_t NumNewBlocks = NewBBs.size();
3672
3673 BasicBlocks.insert(BasicBlocks.begin() + StartIndex + 1, NumNewBlocks,
3674 nullptr);
3675 auto RetIter = BasicBlocks.begin() + StartIndex + 1;
3676
3677 unsigned I = StartIndex + 1;
3678 for (std::unique_ptr<BinaryBasicBlock> &BB : NewBBs) {
3679 assert(!BasicBlocks[I])(static_cast <bool> (!BasicBlocks[I]) ? void (0) : __assert_fail
("!BasicBlocks[I]", "bolt/lib/Core/BinaryFunction.cpp", 3679
, __extension__ __PRETTY_FUNCTION__))
;
3680 BasicBlocks[I++] = BB.release();
3681 }
3682
3683 if (RecomputeLandingPads)
3684 recomputeLandingPads();
3685 else
3686 updateBBIndices(0);
3687
3688 if (UpdateLayout)
3689 updateLayout(*std::prev(RetIter), NumNewBlocks);
3690
3691 if (UpdateCFIState)
3692 updateCFIState(*std::prev(RetIter), NumNewBlocks);
3693
3694 return RetIter;
3695}
3696
3697void BinaryFunction::updateBBIndices(const unsigned StartIndex) {
3698 for (unsigned I = StartIndex; I < BasicBlocks.size(); ++I)
3699 BasicBlocks[I]->Index = I;
3700}
3701
3702void BinaryFunction::updateCFIState(BinaryBasicBlock *Start,
3703 const unsigned NumNewBlocks) {
3704 const int32_t CFIState = Start->getCFIStateAtExit();
3705 const unsigned StartIndex = getIndex(Start) + 1;
3706 for (unsigned I = 0; I < NumNewBlocks; ++I)
3707 BasicBlocks[StartIndex + I]->setCFIState(CFIState);
3708}
3709
3710void BinaryFunction::updateLayout(BinaryBasicBlock *Start,
3711 const unsigned NumNewBlocks) {
3712 BasicBlockListType::iterator Begin;
3713 BasicBlockListType::iterator End;
3714
3715 // If start not provided copy new blocks from the beginning of BasicBlocks
3716 if (!Start) {
3717 Begin = BasicBlocks.begin();
3718 End = BasicBlocks.begin() + NumNewBlocks;
3719 } else {
3720 unsigned StartIndex = getIndex(Start);
3721 Begin = std::next(BasicBlocks.begin(), StartIndex + 1);
3722 End = std::next(BasicBlocks.begin(), StartIndex + NumNewBlocks + 1);
3723 }
3724
3725 // Insert new blocks in the layout immediately after Start.
3726 Layout.insertBasicBlocks(Start, {Begin, End});
3727 Layout.updateLayoutIndices();
3728}
3729
3730bool BinaryFunction::checkForAmbiguousJumpTables() {
3731 SmallSet<uint64_t, 4> JumpTables;
3732 for (BinaryBasicBlock *&BB : BasicBlocks) {
3733 for (MCInst &Inst : *BB) {
3734 if (!BC.MIB->isIndirectBranch(Inst))
3735 continue;
3736 uint64_t JTAddress = BC.MIB->getJumpTable(Inst);
3737 if (!JTAddress)
3738 continue;
3739 // This address can be inside another jump table, but we only consider
3740 // it ambiguous when the same start address is used, not the same JT
3741 // object.
3742 if (!JumpTables.count(JTAddress)) {
3743 JumpTables.insert(JTAddress);
3744 continue;
3745 }
3746 return true;
3747 }
3748 }
3749 return false;
3750}
3751
3752void BinaryFunction::disambiguateJumpTables(
3753 MCPlusBuilder::AllocatorIdTy AllocId) {
3754 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", 3754, __extension__ __PRETTY_FUNCTION__
))
;
3755 SmallPtrSet<JumpTable *, 4> JumpTables;
3756 for (BinaryBasicBlock *&BB : BasicBlocks) {
3757 for (MCInst &Inst : *BB) {
3758 if (!BC.MIB->isIndirectBranch(Inst))
3759 continue;
3760 JumpTable *JT = getJumpTable(Inst);
3761 if (!JT)
3762 continue;
3763 auto Iter = JumpTables.find(JT);
3764 if (Iter == JumpTables.end()) {
3765 JumpTables.insert(JT);
3766 continue;
3767 }
3768 // This instruction is an indirect jump using a jump table, but it is
3769 // using the same jump table of another jump. Try all our tricks to
3770 // extract the jump table symbol and make it point to a new, duplicated JT
3771 MCPhysReg BaseReg1;
3772 uint64_t Scale;
3773 const MCSymbol *Target;
3774 // In case we match if our first matcher, first instruction is the one to
3775 // patch
3776 MCInst *JTLoadInst = &Inst;
3777 // Try a standard indirect jump matcher, scale 8
3778 std::unique_ptr<MCPlusBuilder::MCInstMatcher> IndJmpMatcher =
3779 BC.MIB->matchIndJmp(BC.MIB->matchReg(BaseReg1),
3780 BC.MIB->matchImm(Scale), BC.MIB->matchReg(),
3781 /*Offset=*/BC.MIB->matchSymbol(Target));
3782 if (!IndJmpMatcher->match(
3783 *BC.MRI, *BC.MIB,
3784 MutableArrayRef<MCInst>(&*BB->begin(), &Inst + 1), -1) ||
3785 BaseReg1 != BC.MIB->getNoRegister() || Scale != 8) {
3786 MCPhysReg BaseReg2;
3787 uint64_t Offset;
3788 // Standard JT matching failed. Trying now:
3789 // movq "jt.2397/1"(,%rax,8), %rax
3790 // jmpq *%rax
3791 std::unique_ptr<MCPlusBuilder::MCInstMatcher> LoadMatcherOwner =
3792 BC.MIB->matchLoad(BC.MIB->matchReg(BaseReg1),
3793 BC.MIB->matchImm(Scale), BC.MIB->matchReg(),
3794 /*Offset=*/BC.MIB->matchSymbol(Target));
3795 MCPlusBuilder::MCInstMatcher *LoadMatcher = LoadMatcherOwner.get();
3796 std::unique_ptr<MCPlusBuilder::MCInstMatcher> IndJmpMatcher2 =
3797 BC.MIB->matchIndJmp(std::move(LoadMatcherOwner));
3798 if (!IndJmpMatcher2->match(
3799 *BC.MRI, *BC.MIB,
3800 MutableArrayRef<MCInst>(&*BB->begin(), &Inst + 1), -1) ||
3801 BaseReg1 != BC.MIB->getNoRegister() || Scale != 8) {
3802 // JT matching failed. Trying now:
3803 // PIC-style matcher, scale 4
3804 // addq %rdx, %rsi
3805 // addq %rdx, %rdi
3806 // leaq DATAat0x402450(%rip), %r11
3807 // movslq (%r11,%rdx,4), %rcx
3808 // addq %r11, %rcx
3809 // jmpq *%rcx # JUMPTABLE @0x402450
3810 std::unique_ptr<MCPlusBuilder::MCInstMatcher> PICIndJmpMatcher =
3811 BC.MIB->matchIndJmp(BC.MIB->matchAdd(
3812 BC.MIB->matchReg(BaseReg1),
3813 BC.MIB->matchLoad(BC.MIB->matchReg(BaseReg2),
3814 BC.MIB->matchImm(Scale), BC.MIB->matchReg(),
3815 BC.MIB->matchImm(Offset))));
3816 std::unique_ptr<MCPlusBuilder::MCInstMatcher> LEAMatcherOwner =
3817 BC.MIB->matchLoadAddr(BC.MIB->matchSymbol(Target));
3818 MCPlusBuilder::MCInstMatcher *LEAMatcher = LEAMatcherOwner.get();
3819 std::unique_ptr<MCPlusBuilder::MCInstMatcher> PICBaseAddrMatcher =
3820 BC.MIB->matchIndJmp(BC.MIB->matchAdd(std::move(LEAMatcherOwner),
3821 BC.MIB->matchAnyOperand()));
3822 if (!PICIndJmpMatcher->match(
3823 *BC.MRI, *BC.MIB,
3824 MutableArrayRef<MCInst>(&*BB->begin(), &Inst + 1), -1) ||
3825 Scale != 4 || BaseReg1 != BaseReg2 || Offset != 0 ||
3826 !PICBaseAddrMatcher->match(
3827 *BC.MRI, *BC.MIB,
3828 MutableArrayRef<MCInst>(&*BB->begin(), &Inst + 1), -1)) {
3829 llvm_unreachable("Failed to extract jump table base")::llvm::llvm_unreachable_internal("Failed to extract jump table base"
, "bolt/lib/Core/BinaryFunction.cpp", 3829)
;
3830 continue;
3831 }
3832 // Matched PIC, identify the instruction with the reference to the JT
3833 JTLoadInst = LEAMatcher->CurInst;
3834 } else {
3835 // Matched non-PIC
3836 JTLoadInst = LoadMatcher->CurInst;
3837 }
3838 }
3839
3840 uint64_t NewJumpTableID = 0;
3841 const MCSymbol *NewJTLabel;
3842 std::tie(NewJumpTableID, NewJTLabel) =
3843 BC.duplicateJumpTable(*this, JT, Target);
3844 {
3845 auto L = BC.scopeLock();
3846 BC.MIB->replaceMemOperandDisp(*JTLoadInst, NewJTLabel, BC.Ctx.get());
3847 }
3848 // We use a unique ID with the high bit set as address for this "injected"
3849 // jump table (not originally in the input binary).
3850 BC.MIB->setJumpTable(Inst, NewJumpTableID, 0, AllocId);
3851 }
3852 }
3853}
3854
3855bool BinaryFunction::replaceJumpTableEntryIn(BinaryBasicBlock *BB,
3856 BinaryBasicBlock *OldDest,
3857 BinaryBasicBlock *NewDest) {
3858 MCInst *Instr = BB->getLastNonPseudoInstr();
3859 if (!Instr || !BC.MIB->isIndirectBranch(*Instr))
3860 return false;
3861 uint64_t JTAddress = BC.MIB->getJumpTable(*Instr);
3862 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", 3862, __extension__ __PRETTY_FUNCTION__
))
;
3863 JumpTable *JT = getJumpTableContainingAddress(JTAddress);
3864 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", 3864, __extension__ __PRETTY_FUNCTION__
))
;
3865 bool Patched = JT->replaceDestination(JTAddress, OldDest->getLabel(),
3866 NewDest->getLabel());
3867 (void)Patched;
3868 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", 3868, __extension__ __PRETTY_FUNCTION__
))
;
3869 return true;
3870}
3871
3872BinaryBasicBlock *BinaryFunction::splitEdge(BinaryBasicBlock *From,
3873 BinaryBasicBlock *To) {
3874 // Create intermediate BB
3875 MCSymbol *Tmp;
3876 {
3877 auto L = BC.scopeLock();
3878 Tmp = BC.Ctx->createNamedTempSymbol("SplitEdge");
3879 }
3880 // Link new BBs to the original input offset of the From BB, so we can map
3881 // samples recorded in new BBs back to the original BB seem in the input
3882 // binary (if using BAT)
3883 std::unique_ptr<BinaryBasicBlock> NewBB = createBasicBlock(Tmp);
3884 NewBB->setOffset(From->getInputOffset());
3885 BinaryBasicBlock *NewBBPtr = NewBB.get();
3886
3887 // Update "From" BB
3888 auto I = From->succ_begin();
3889 auto BI = From->branch_info_begin();
3890 for (; I != From->succ_end(); ++I) {
3891 if (*I == To)
3892 break;
3893 ++BI;
3894 }
3895 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", 3895, __extension__ __PRETTY_FUNCTION__
))
;
3896 uint64_t OrigCount = BI->Count;
3897 uint64_t OrigMispreds = BI->MispredictedCount;
3898 replaceJumpTableEntryIn(From, To, NewBBPtr);
3899 From->replaceSuccessor(To, NewBBPtr, OrigCount, OrigMispreds);
3900
3901 NewBB->addSuccessor(To, OrigCount, OrigMispreds);
3902 NewBB->setExecutionCount(OrigCount);
3903 NewBB->setIsCold(From->isCold());
3904
3905 // Update CFI and BB layout with new intermediate BB
3906 std::vector<std::unique_ptr<BinaryBasicBlock>> NewBBs;
3907 NewBBs.emplace_back(std::move(NewBB));
3908 insertBasicBlocks(From, std::move(NewBBs), true, true,
3909 /*RecomputeLandingPads=*/false);
3910 return NewBBPtr;
3911}
3912
3913void BinaryFunction::deleteConservativeEdges() {
3914 // Our goal is to aggressively remove edges from the CFG that we believe are
3915 // wrong. This is used for instrumentation, where it is safe to remove
3916 // fallthrough edges because we won't reorder blocks.
3917 for (auto I = BasicBlocks.begin(), E = BasicBlocks.end(); I != E; ++I) {
3918 BinaryBasicBlock *BB = *I;
3919 if (BB->succ_size() != 1 || BB->size() == 0)
3920 continue;
3921
3922 auto NextBB = std::next(I);
3923 MCInst *Last = BB->getLastNonPseudoInstr();
3924 // Fallthrough is a landing pad? Delete this edge (as long as we don't
3925 // have a direct jump to it)
3926 if ((*BB->succ_begin())->isLandingPad() && NextBB != E &&
3927 *BB->succ_begin() == *NextBB && Last && !BC.MIB->isBranch(*Last)) {
3928 BB->removeAllSuccessors();
3929 continue;
3930 }
3931
3932 // Look for suspicious calls at the end of BB where gcc may optimize it and
3933 // remove the jump to the epilogue when it knows the call won't return.
3934 if (!Last || !BC.MIB->isCall(*Last))
3935 continue;
3936
3937 const MCSymbol *CalleeSymbol = BC.MIB->getTargetSymbol(*Last);
3938 if (!CalleeSymbol)
3939 continue;
3940
3941 StringRef CalleeName = CalleeSymbol->getName();
3942 if (CalleeName != "__cxa_throw@PLT" && CalleeName != "_Unwind_Resume@PLT" &&
3943 CalleeName != "__cxa_rethrow@PLT" && CalleeName != "exit@PLT" &&
3944 CalleeName != "abort@PLT")
3945 continue;
3946
3947 BB->removeAllSuccessors();
3948 }
3949}
3950
3951bool BinaryFunction::isSymbolValidInScope(const SymbolRef &Symbol,
3952 uint64_t SymbolSize) const {
3953 // If this symbol is in a different section from the one where the
3954 // function symbol is, don't consider it as valid.
3955 if (!getOriginSection()->containsAddress(
3956 cantFail(Symbol.getAddress(), "cannot get symbol address")))
3957 return false;
3958
3959 // Some symbols are tolerated inside function bodies, others are not.
3960 // The real function boundaries may not be known at this point.
3961 if (BC.isMarker(Symbol))
3962 return true;
3963
3964 // It's okay to have a zero-sized symbol in the middle of non-zero-sized
3965 // function.
3966 if (SymbolSize == 0 && containsAddress(cantFail(Symbol.getAddress())))
3967 return true;
3968
3969 if (cantFail(Symbol.getType()) != SymbolRef::ST_Unknown)
3970 return false;
3971
3972 if (cantFail(Symbol.getFlags()) & SymbolRef::SF_Global)
3973 return false;
3974
3975 return true;
3976}
3977
3978void BinaryFunction::adjustExecutionCount(uint64_t Count) {
3979 if (getKnownExecutionCount() == 0 || Count == 0)
3980 return;
3981
3982 if (ExecutionCount < Count)
3983 Count = ExecutionCount;
3984
3985 double AdjustmentRatio = ((double)ExecutionCount - Count) / ExecutionCount;
3986 if (AdjustmentRatio < 0.0)
3987 AdjustmentRatio = 0.0;
3988
3989 for (BinaryBasicBlock &BB : blocks())
3990 BB.adjustExecutionCount(AdjustmentRatio);
3991
3992 ExecutionCount -= Count;
3993}
3994
3995BinaryFunction::~BinaryFunction() {
3996 for (BinaryBasicBlock *BB : BasicBlocks)
3997 delete BB;
3998 for (BinaryBasicBlock *BB : DeletedBasicBlocks)
3999 delete BB;
4000}
4001
4002void BinaryFunction::calculateLoopInfo() {
4003 // Discover loops.
4004 BinaryDominatorTree DomTree;
4005 DomTree.recalculate(*this);
4006 BLI.reset(new BinaryLoopInfo());
4007 BLI->analyze(DomTree);
4008
4009 // Traverse discovered loops and add depth and profile information.
4010 std::stack<BinaryLoop *> St;
4011 for (auto I = BLI->begin(), E = BLI->end(); I != E; ++I) {
4012 St.push(*I);
4013 ++BLI->OuterLoops;
4014 }
4015
4016 while (!St.empty()) {
4017 BinaryLoop *L = St.top();
4018 St.pop();
4019 ++BLI->TotalLoops;
4020 BLI->MaximumDepth = std::max(L->getLoopDepth(), BLI->MaximumDepth);
4021
4022 // Add nested loops in the stack.
4023 for (BinaryLoop::iterator I = L->begin(), E = L->end(); I != E; ++I)
4024 St.push(*I);
4025
4026 // Skip if no valid profile is found.
4027 if (!hasValidProfile()) {
4028 L->EntryCount = COUNT_NO_PROFILE;
4029 L->ExitCount = COUNT_NO_PROFILE;
4030 L->TotalBackEdgeCount = COUNT_NO_PROFILE;
4031 continue;
4032 }
4033
4034 // Compute back edge count.
4035 SmallVector<BinaryBasicBlock *, 1> Latches;
4036 L->getLoopLatches(Latches);
4037
4038 for (BinaryBasicBlock *Latch : Latches) {
4039 auto BI = Latch->branch_info_begin();
4040 for (BinaryBasicBlock *Succ : Latch->successors()) {
4041 if (Succ == L->getHeader()) {
4042 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", 4043, __extension__ __PRETTY_FUNCTION__
))
4043 "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", 4043, __extension__ __PRETTY_FUNCTION__
))
;
4044 L->TotalBackEdgeCount += BI->Count;
4045 }
4046 ++BI;
4047 }
4048 }
4049
4050 // Compute entry count.
4051 L->EntryCount = L->getHeader()->getExecutionCount() - L->TotalBackEdgeCount;
4052
4053 // Compute exit count.
4054 SmallVector<BinaryLoop::Edge, 1> ExitEdges;
4055 L->getExitEdges(ExitEdges);
4056 for (BinaryLoop::Edge &Exit : ExitEdges) {
4057 const BinaryBasicBlock *Exiting = Exit.first;
4058 const BinaryBasicBlock *ExitTarget = Exit.second;
4059 auto BI = Exiting->branch_info_begin();
4060 for (BinaryBasicBlock *Succ : Exiting->successors()) {
4061 if (Succ == ExitTarget) {
4062 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", 4063, __extension__ __PRETTY_FUNCTION__
))
4063 "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", 4063, __extension__ __PRETTY_FUNCTION__
))
;
4064 L->ExitCount += BI->Count;
4065 }
4066 ++BI;
4067 }
4068 }
4069 }
4070}
4071
4072void BinaryFunction::updateOutputValues(const MCAsmLayout &Layout) {
4073 if (!isEmitted()) {
4074 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", 4074, __extension__ __PRETTY_FUNCTION__
))
;
4075 setOutputAddress(getAddress());
4076 setOutputSize(getSize());
4077 return;
4078 }
4079
4080 const uint64_t BaseAddress = getCodeSection()->getOutputAddress();
4081 if (BC.HasRelocations || isInjected()) {
4082 const uint64_t StartOffset = Layout.getSymbolOffset(*getSymbol());
4083 const uint64_t EndOffset = Layout.getSymbolOffset(*getFunctionEndLabel());
4084 setOutputAddress(BaseAddress + StartOffset);
4085 setOutputSize(EndOffset - StartOffset);
4086 if (hasConstantIsland()) {
4087 const uint64_t DataOffset =
4088 Layout.getSymbolOffset(*getFunctionConstantIslandLabel());
4089 setOutputDataAddress(BaseAddress + DataOffset);
4090 for (auto It : Islands->Offsets) {
4091 const uint64_t OldOffset = It.first;
4092 BinaryData *BD = BC.getBinaryDataAtAddress(getAddress() + OldOffset);
4093 if (!BD)
4094 continue;
4095
4096 MCSymbol *Symbol = It.second;
4097 const uint64_t NewOffset = Layout.getSymbolOffset(*Symbol);
4098 BD->setOutputLocation(*getCodeSection(), NewOffset);
4099 }
4100 }
4101 if (isSplit()) {
4102 for (FunctionFragment &FF : getLayout().getSplitFragments()) {
4103 ErrorOr<BinarySection &> ColdSection =
4104 getCodeSection(FF.getFragmentNum());
4105 // If fragment is empty, cold section might not exist
4106 if (FF.empty() && ColdSection.getError())
4107 continue;
4108 const uint64_t ColdBaseAddress = ColdSection->getOutputAddress();
4109
4110 const MCSymbol *ColdStartSymbol = getSymbol(FF.getFragmentNum());
4111 // If fragment is empty, symbol might have not been emitted
4112 if (FF.empty() && (!ColdStartSymbol || !ColdStartSymbol->isDefined()) &&
4113 !hasConstantIsland())
4114 continue;
4115 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", 4116, __extension__ __PRETTY_FUNCTION__
))
4116 "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", 4116, __extension__ __PRETTY_FUNCTION__
))
;
4117 const MCSymbol *ColdEndSymbol =
4118 getFunctionEndLabel(FF.getFragmentNum());
4119 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", 4120, __extension__ __PRETTY_FUNCTION__
))
4120 "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", 4120, __extension__ __PRETTY_FUNCTION__
))
;
4121 const uint64_t ColdStartOffset =
4122 Layout.getSymbolOffset(*ColdStartSymbol);
4123 const uint64_t ColdEndOffset = Layout.getSymbolOffset(*ColdEndSymbol);
4124 FF.setAddress(ColdBaseAddress + ColdStartOffset);
4125 FF.setImageSize(ColdEndOffset - ColdStartOffset);
4126 if (hasConstantIsland()) {
4127 const uint64_t DataOffset =
4128 Layout.getSymbolOffset(*getFunctionColdConstantIslandLabel());
4129 setOutputColdDataAddress(ColdBaseAddress + DataOffset);
4130 }
4131 }
4132 }
4133 } else {
4134 setOutputAddress(getAddress());
4135 setOutputSize(Layout.getSymbolOffset(*getFunctionEndLabel()));
4136 }
4137
4138 // Update basic block output ranges for the debug info, if we have
4139 // secondary entry points in the symbol table to update or if writing BAT.
4140 if (!opts::UpdateDebugSections && !isMultiEntry() &&
4141 !requiresAddressTranslation())
4142 return;
4143
4144 // Output ranges should match the input if the body hasn't changed.
4145 if (!isSimple() && !BC.HasRelocations)
4146 return;
4147
4148 // AArch64 may have functions that only contains a constant island (no code).
4149 if (getLayout().block_empty())
4150 return;
4151
4152 for (FunctionFragment &FF : getLayout().fragments()) {
4153 if (FF.empty())
4154 continue;
4155
4156 const uint64_t FragmentBaseAddress =
4157 getCodeSection(isSimple() ? FF.getFragmentNum() : FragmentNum::main())
4158 ->getOutputAddress();
4159
4160 BinaryBasicBlock *PrevBB = nullptr;
4161 for (BinaryBasicBlock *const BB : FF) {
4162 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", 4162, __extension__ __PRETTY_FUNCTION__
))
;
4163 if (!BC.HasRelocations) {
4164 if (BB->isSplit())
4165 assert(FragmentBaseAddress == FF.getAddress())(static_cast <bool> (FragmentBaseAddress == FF.getAddress
()) ? void (0) : __assert_fail ("FragmentBaseAddress == FF.getAddress()"
, "bolt/lib/Core/BinaryFunction.cpp", 4165, __extension__ __PRETTY_FUNCTION__
))
;
4166 else
4167 assert(FragmentBaseAddress == getOutputAddress())(static_cast <bool> (FragmentBaseAddress == getOutputAddress
()) ? void (0) : __assert_fail ("FragmentBaseAddress == getOutputAddress()"
, "bolt/lib/Core/BinaryFunction.cpp", 4167, __extension__ __PRETTY_FUNCTION__
))
;
4168 }
4169
4170 const uint64_t BBOffset = Layout.getSymbolOffset(*BB->getLabel());
4171 const uint64_t BBAddress = FragmentBaseAddress + BBOffset;
4172 BB->setOutputStartAddress(BBAddress);
4173
4174 if (PrevBB)
4175 PrevBB->setOutputEndAddress(BBAddress);
4176 PrevBB = BB;
4177
4178 BB->updateOutputValues(Layout);
4179 }
4180
4181 PrevBB->setOutputEndAddress(PrevBB->isSplit()
4182 ? FF.getAddress() + FF.getImageSize()
4183 : getOutputAddress() + getOutputSize());
4184 }
4185}
4186
4187DebugAddressRangesVector BinaryFunction::getOutputAddressRanges() const {
4188 DebugAddressRangesVector OutputRanges;
4189
4190 if (isFolded())
4191 return OutputRanges;
4192
4193 if (IsFragment)
4194 return OutputRanges;
4195
4196 OutputRanges.emplace_back(getOutputAddress(),
4197 getOutputAddress() + getOutputSize());
4198 if (isSplit()) {
4199 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", 4199, __extension__ __PRETTY_FUNCTION__
))
;
4200 for (const FunctionFragment &FF : getLayout().getSplitFragments())
4201 OutputRanges.emplace_back(FF.getAddress(),
4202 FF.getAddress() + FF.getImageSize());
4203 }
4204
4205 if (isSimple())
4206 return OutputRanges;
4207
4208 for (BinaryFunction *Frag : Fragments) {
4209 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", 4210, __extension__ __PRETTY_FUNCTION__
))
4210 "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", 4210, __extension__ __PRETTY_FUNCTION__
))
;
4211 OutputRanges.emplace_back(Frag->getOutputAddress(),
4212 Frag->getOutputAddress() + Frag->getOutputSize());
4213 }
4214
4215 return OutputRanges;
4216}
4217
4218uint64_t BinaryFunction::translateInputToOutputAddress(uint64_t Address) const {
4219 if (isFolded())
4220 return 0;
4221
4222 // If the function hasn't changed return the same address.
4223 if (!isEmitted())
4224 return Address;
4225
4226 if (Address < getAddress())
4227 return 0;
4228
4229 // Check if the address is associated with an instruction that is tracked
4230 // by address translation.
4231 auto KV = InputOffsetToAddressMap.find(Address - getAddress());
4232 if (KV != InputOffsetToAddressMap.end())
4233 return KV->second;
4234
4235 // FIXME: #18950828 - we rely on relative offsets inside basic blocks to stay
4236 // intact. Instead we can use pseudo instructions and/or annotations.
4237 const uint64_t Offset = Address - getAddress();
4238 const BinaryBasicBlock *BB = getBasicBlockContainingOffset(Offset);
4239 if (!BB) {
4240 // Special case for address immediately past the end of the function.
4241 if (Offset == getSize())
4242 return getOutputAddress() + getOutputSize();
4243
4244 return 0;
4245 }
4246
4247 return std::min(BB->getOutputAddressRange().first + Offset - BB->getOffset(),
4248 BB->getOutputAddressRange().second);
4249}
4250
4251DebugAddressRangesVector BinaryFunction::translateInputToOutputRanges(
4252 const DWARFAddressRangesVector &InputRanges) const {
4253 DebugAddressRangesVector OutputRanges;
4254
4255 if (isFolded())
4256 return OutputRanges;
4257
4258 // If the function hasn't changed return the same ranges.
4259 if (!isEmitted()) {
4260 OutputRanges.resize(InputRanges.size());
4261 llvm::transform(InputRanges, OutputRanges.begin(),
4262 [](const DWARFAddressRange &Range) {
4263 return DebugAddressRange(Range.LowPC, Range.HighPC);
4264 });
4265 return OutputRanges;
4266 }
4267
4268 // Even though we will merge ranges in a post-processing pass, we attempt to
4269 // merge them in a main processing loop as it improves the processing time.
4270 uint64_t PrevEndAddress = 0;
4271 for (const DWARFAddressRange &Range : InputRanges) {
4272 if (!containsAddress(Range.LowPC)) {
4273 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)
4274 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)
4275 << *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)
4276 << 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)
;
4277 PrevEndAddress = 0;
4278 continue;
4279 }
4280 uint64_t InputOffset = Range.LowPC - getAddress();
4281 const uint64_t InputEndOffset =
4282 std::min(Range.HighPC - getAddress(), getSize());
4283
4284 auto BBI = llvm::upper_bound(BasicBlockOffsets,
4285 BasicBlockOffset(InputOffset, nullptr),
4286 CompareBasicBlockOffsets());
4287 --BBI;
4288 do {
4289 const BinaryBasicBlock *BB = BBI->second;
4290 if (InputOffset < BB->getOffset() || InputOffset >= BB->getEndOffset()) {
4291 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)
4292 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)
4293 << *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)
4294 << ", 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)
;
4295 PrevEndAddress = 0;
4296 break;
4297 }
4298
4299 // Skip the range if the block was deleted.
4300 if (const uint64_t OutputStart = BB->getOutputAddressRange().first) {
4301 const uint64_t StartAddress =
4302 OutputStart + InputOffset - BB->getOffset();
4303 uint64_t EndAddress = BB->getOutputAddressRange().second;
4304 if (InputEndOffset < BB->getEndOffset())
4305 EndAddress = StartAddress + InputEndOffset - InputOffset;
4306
4307 if (StartAddress == PrevEndAddress) {
4308 OutputRanges.back().HighPC =
4309 std::max(OutputRanges.back().HighPC, EndAddress);
4310 } else {
4311 OutputRanges.emplace_back(StartAddress,
4312 std::max(StartAddress, EndAddress));
4313 }
4314 PrevEndAddress = OutputRanges.back().HighPC;
4315 }
4316
4317 InputOffset = BB->getEndOffset();
4318 ++BBI;
4319 } while (InputOffset < InputEndOffset);
4320 }
4321
4322 // Post-processing pass to sort and merge ranges.
4323 llvm::sort(OutputRanges);
4324 DebugAddressRangesVector MergedRanges;
4325 PrevEndAddress = 0;
4326 for (const DebugAddressRange &Range : OutputRanges) {
4327 if (Range.LowPC <= PrevEndAddress) {
4328 MergedRanges.back().HighPC =
4329 std::max(MergedRanges.back().HighPC, Range.HighPC);
4330 } else {
4331 MergedRanges.emplace_back(Range.LowPC, Range.HighPC);
4332 }
4333 PrevEndAddress = MergedRanges.back().HighPC;
4334 }
4335
4336 return MergedRanges;
4337}
4338
4339MCInst *BinaryFunction::getInstructionAtOffset(uint64_t Offset) {
4340 if (CurrentState == State::Disassembled) {
4341 auto II = Instructions.find(Offset);
4342 return (II == Instructions.end()) ? nullptr : &II->second;
4343 } else if (CurrentState == State::CFG) {
4344 BinaryBasicBlock *BB = getBasicBlockContainingOffset(Offset);
4345 if (!BB)
4346 return nullptr;
4347
4348 for (MCInst &Inst : *BB) {
4349 constexpr uint32_t InvalidOffset = std::numeric_limits<uint32_t>::max();
4350 if (Offset == BC.MIB->getOffsetWithDefault(Inst, InvalidOffset))
4351 return &Inst;
4352 }
4353
4354 if (MCInst *LastInstr = BB->getLastNonPseudoInstr()) {
4355 const uint32_t Size =
4356 BC.MIB->getAnnotationWithDefault<uint32_t>(*LastInstr, "Size");
4357 if (BB->getEndOffset() - Offset == Size)
4358 return LastInstr;
4359 }
4360
4361 return nullptr;
4362 } else {
4363 llvm_unreachable("invalid CFG state to use getInstructionAtOffset()")::llvm::llvm_unreachable_internal("invalid CFG state to use getInstructionAtOffset()"
, "bolt/lib/Core/BinaryFunction.cpp", 4363)
;
4364 }
4365}
4366
4367DebugLocationsVector BinaryFunction::translateInputToOutputLocationList(
4368 const DebugLocationsVector &InputLL) const {
4369 DebugLocationsVector OutputLL;
4370
4371 if (isFolded())
4372 return OutputLL;
4373
4374 // If the function hasn't changed - there's nothing to update.
4375 if (!isEmitted())
4376 return InputLL;
4377
4378 uint64_t PrevEndAddress = 0;
4379 SmallVectorImpl<uint8_t> *PrevExpr = nullptr;
4380 for (const DebugLocationEntry &Entry : InputLL) {
4381 const uint64_t Start = Entry.LowPC;
4382 const uint64_t End = Entry.HighPC;
4383 if (!containsAddress(Start)) {
4384 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)
4385 "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)
4386 << *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)
4387 << ", 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)
;
4388 continue;
4389 }
4390 uint64_t InputOffset = Start - getAddress();
4391 const uint64_t InputEndOffset = std::min(End - getAddress(), getSize());
4392 auto BBI = llvm::upper_bound(BasicBlockOffsets,
4393 BasicBlockOffset(InputOffset, nullptr),
4394 CompareBasicBlockOffsets());
4395 --BBI;
4396 do {
4397 const BinaryBasicBlock *BB = BBI->second;
4398 if (InputOffset < BB->getOffset() || InputOffset >= BB->getEndOffset()) {
4399 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)
4400 "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)
4401 << *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)
4402 << ", 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)
;
4403 PrevEndAddress = 0;
4404 break;
4405 }
4406
4407 // Skip the range if the block was deleted.
4408 if (const uint64_t OutputStart = BB->getOutputAddressRange().first) {
4409 const uint64_t StartAddress =
4410 OutputStart + InputOffset - BB->getOffset();
4411 uint64_t EndAddress = BB->getOutputAddressRange().second;
4412 if (InputEndOffset < BB->getEndOffset())
4413 EndAddress = StartAddress + InputEndOffset - InputOffset;
4414
4415 if (StartAddress == PrevEndAddress && Entry.Expr == *PrevExpr) {
4416 OutputLL.back().HighPC = std::max(OutputLL.back().HighPC, EndAddress);
4417 } else {
4418 OutputLL.emplace_back(DebugLocationEntry{
4419 StartAddress, std::max(StartAddress, EndAddress), Entry.Expr});
4420 }
4421 PrevEndAddress = OutputLL.back().HighPC;
4422 PrevExpr = &OutputLL.back().Expr;
4423 }
4424
4425 ++BBI;
4426 InputOffset = BB->getEndOffset();
4427 } while (InputOffset < InputEndOffset);
4428 }
4429
4430 // Sort and merge adjacent entries with identical location.
4431 llvm::stable_sort(
4432 OutputLL, [](const DebugLocationEntry &A, const DebugLocationEntry &B) {
4433 return A.LowPC < B.LowPC;
4434 });
4435 DebugLocationsVector MergedLL;
4436 PrevEndAddress = 0;
4437 PrevExpr = nullptr;
4438 for (const DebugLocationEntry &Entry : OutputLL) {
4439 if (Entry.LowPC <= PrevEndAddress && *PrevExpr == Entry.Expr) {
4440 MergedLL.back().HighPC = std::max(Entry.HighPC, MergedLL.back().HighPC);
4441 } else {
4442 const uint64_t Begin = std::max(Entry.LowPC, PrevEndAddress);
4443 const uint64_t End = std::max(Begin, Entry.HighPC);
4444 MergedLL.emplace_back(DebugLocationEntry{Begin, End, Entry.Expr});
4445 }
4446 PrevEndAddress = MergedLL.back().HighPC;
4447 PrevExpr = &MergedLL.back().Expr;
4448 }
4449
4450 return MergedLL;
4451}
4452
4453void BinaryFunction::printLoopInfo(raw_ostream &OS) const {
4454 if (!opts::shouldPrint(*this))
4455 return;
4456
4457 OS << "Loop Info for Function \"" << *this << "\"";
4458 if (hasValidProfile())
4459 OS << " (count: " << getExecutionCount() << ")";
4460 OS << "\n";
4461
4462 std::stack<BinaryLoop *> St;
4463 for (BinaryLoop *L : *BLI)
4464 St.push(L);
4465 while (!St.empty()) {
4466 BinaryLoop *L = St.top();
4467 St.pop();
4468
4469 for (BinaryLoop *Inner : *L)
4470 St.push(Inner);
4471
4472 if (!hasValidProfile())
4473 continue;
4474
4475 OS << (L->getLoopDepth() > 1 ? "Nested" : "Outer")
4476 << " loop header: " << L->getHeader()->getName();
4477 OS << "\n";
4478 OS << "Loop basic blocks: ";
4479 ListSeparator LS;
4480 for (BinaryBasicBlock *BB : L->blocks())
4481 OS << LS << BB->getName();
4482 OS << "\n";
4483 if (hasValidProfile()) {
4484 OS << "Total back edge count: " << L->TotalBackEdgeCount << "\n";
4485 OS << "Loop entry count: " << L->EntryCount << "\n";
4486 OS << "Loop exit count: " << L->ExitCount << "\n";
4487 if (L->EntryCount > 0) {
4488 OS << "Average iters per entry: "
4489 << format("%.4lf", (double)L->TotalBackEdgeCount / L->EntryCount)
4490 << "\n";
4491 }
4492 }
4493 OS << "----\n";
4494 }
4495
4496 OS << "Total number of loops: " << BLI->TotalLoops << "\n";
4497 OS << "Number of outer loops: " << BLI->OuterLoops << "\n";
4498 OS << "Maximum nested loop depth: " << BLI->MaximumDepth << "\n\n";
4499}
4500
4501bool BinaryFunction::isAArch64Veneer() const {
4502 if (empty() || hasIslandsInfo())
4503 return false;
4504
4505 BinaryBasicBlock &BB = **BasicBlocks.begin();
4506 for (MCInst &Inst : BB)
4507 if (!BC.MIB->hasAnnotation(Inst, "AArch64Veneer"))
4508 return false;
4509
4510 for (auto I = BasicBlocks.begin() + 1, E = BasicBlocks.end(); I != E; ++I) {
4511 for (MCInst &Inst : **I)
4512 if (!BC.MIB->isNoop(Inst))
4513 return false;
4514 }
4515
4516 return true;
4517}
4518
4519void BinaryFunction::addRelocation(uint64_t Address, MCSymbol *Symbol,
4520 uint64_t RelType, uint64_t Addend,
4521 uint64_t Value) {
4522 assert(Address >= getAddress() && Address < getAddress() + getMaxSize() &&(static_cast <bool> (Address >= getAddress() &&
Address < getAddress() + getMaxSize() && "address is outside of the function"
) ? void (0) : __assert_fail ("Address >= getAddress() && Address < getAddress() + getMaxSize() && \"address is outside of the function\""
, "bolt/lib/Core/BinaryFunction.cpp", 4523, __extension__ __PRETTY_FUNCTION__
))
4523 "address is outside of the function")(static_cast <bool> (Address >= getAddress() &&
Address < getAddress() + getMaxSize() && "address is outside of the function"
) ? void (0) : __assert_fail ("Address >= getAddress() && Address < getAddress() + getMaxSize() && \"address is outside of the function\""
, "bolt/lib/Core/BinaryFunction.cpp", 4523, __extension__ __PRETTY_FUNCTION__
))
;
4524 uint64_t Offset = Address - getAddress();
4525 LLVM_DEBUG(dbgs() << "BOLT-DEBUG: addRelocation in "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: addRelocation in " <<
formatv("{0}@{1:x} against {2}\n", this, Offset, Symbol->
getName()); } } while (false)
4526 << formatv("{0}@{1:x} against {2}\n", this, Offset,do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: addRelocation in " <<
formatv("{0}@{1:x} against {2}\n", this, Offset, Symbol->
getName()); } } while (false)
4527 Symbol->getName()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("bolt")) { dbgs() << "BOLT-DEBUG: addRelocation in " <<
formatv("{0}@{1:x} against {2}\n", this, Offset, Symbol->
getName()); } } while (false)
;
4528 bool IsCI = BC.isAArch64() && isInConstantIsland(Address);
4529 std::map<uint64_t, Relocation> &Rels =
4530 IsCI ? Islands->Relocations : Relocations;
4531 if (BC.MIB->shouldRecordCodeRelocation(RelType))
4532 Rels[Offset] = Relocation{Offset, Symbol, RelType, Addend, Value};
4533}
4534
4535} // namespace bolt
4536} // namespace llvm