[TargetVersion] Only enable on RISC-V and AArch64 (#115991)
[llvm-project.git] / bolt / lib / Core / BinaryFunction.cpp
blob5da777411ba7a12841ebcc10cca1e1f055e8d37c
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.
11 //===----------------------------------------------------------------------===//
13 #include "bolt/Core/BinaryFunction.h"
14 #include "bolt/Core/BinaryBasicBlock.h"
15 #include "bolt/Core/DynoStats.h"
16 #include "bolt/Core/HashUtilities.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/MCContext.h"
28 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
29 #include "llvm/MC/MCExpr.h"
30 #include "llvm/MC/MCInst.h"
31 #include "llvm/MC/MCInstPrinter.h"
32 #include "llvm/MC/MCRegisterInfo.h"
33 #include "llvm/MC/MCSymbol.h"
34 #include "llvm/Object/ObjectFile.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/GenericDomTreeConstruction.h"
38 #include "llvm/Support/GenericLoopInfoImpl.h"
39 #include "llvm/Support/GraphWriter.h"
40 #include "llvm/Support/LEB128.h"
41 #include "llvm/Support/Regex.h"
42 #include "llvm/Support/Timer.h"
43 #include "llvm/Support/raw_ostream.h"
44 #include "llvm/Support/xxhash.h"
45 #include <functional>
46 #include <limits>
47 #include <numeric>
48 #include <stack>
49 #include <string>
51 #define DEBUG_TYPE "bolt"
53 using namespace llvm;
54 using namespace bolt;
56 namespace opts {
58 extern cl::OptionCategory BoltCategory;
59 extern cl::OptionCategory BoltOptCategory;
61 extern cl::opt<bool> EnableBAT;
62 extern cl::opt<bool> Instrument;
63 extern cl::opt<bool> StrictMode;
64 extern cl::opt<bool> UpdateDebugSections;
65 extern cl::opt<unsigned> Verbosity;
67 extern bool processAllFunctions();
69 cl::opt<bool> CheckEncoding(
70 "check-encoding",
71 cl::desc("perform verification of LLVM instruction encoding/decoding. "
72 "Every instruction in the input is decoded and re-encoded. "
73 "If the resulting bytes do not match the input, a warning message "
74 "is printed."),
75 cl::Hidden, cl::cat(BoltCategory));
77 static cl::opt<bool> DotToolTipCode(
78 "dot-tooltip-code",
79 cl::desc("add basic block instructions as tool tips on nodes"), cl::Hidden,
80 cl::cat(BoltCategory));
82 cl::opt<JumpTableSupportLevel>
83 JumpTables("jump-tables",
84 cl::desc("jump tables support (default=basic)"),
85 cl::init(JTS_BASIC),
86 cl::values(
87 clEnumValN(JTS_NONE, "none",
88 "do not optimize functions with jump tables"),
89 clEnumValN(JTS_BASIC, "basic",
90 "optimize functions with jump tables"),
91 clEnumValN(JTS_MOVE, "move",
92 "move jump tables to a separate section"),
93 clEnumValN(JTS_SPLIT, "split",
94 "split jump tables section into hot and cold based on "
95 "function execution frequency"),
96 clEnumValN(JTS_AGGRESSIVE, "aggressive",
97 "aggressively split jump tables section based on usage "
98 "of the tables")),
99 cl::ZeroOrMore,
100 cl::cat(BoltOptCategory));
102 static cl::opt<bool> NoScan(
103 "no-scan",
104 cl::desc(
105 "do not scan cold functions for external references (may result in "
106 "slower binary)"),
107 cl::Hidden, cl::cat(BoltOptCategory));
109 cl::opt<bool>
110 PreserveBlocksAlignment("preserve-blocks-alignment",
111 cl::desc("try to preserve basic block alignment"),
112 cl::cat(BoltOptCategory));
114 static cl::opt<bool> PrintOutputAddressRange(
115 "print-output-address-range",
116 cl::desc(
117 "print output address range for each basic block in the function when"
118 "BinaryFunction::print is called"),
119 cl::Hidden, cl::cat(BoltOptCategory));
121 cl::opt<bool>
122 PrintDynoStats("dyno-stats",
123 cl::desc("print execution info based on profile"),
124 cl::cat(BoltCategory));
126 static cl::opt<bool>
127 PrintDynoStatsOnly("print-dyno-stats-only",
128 cl::desc("while printing functions output dyno-stats and skip instructions"),
129 cl::init(false),
130 cl::Hidden,
131 cl::cat(BoltCategory));
133 static cl::list<std::string>
134 PrintOnly("print-only",
135 cl::CommaSeparated,
136 cl::desc("list of functions to print"),
137 cl::value_desc("func1,func2,func3,..."),
138 cl::Hidden,
139 cl::cat(BoltCategory));
141 cl::opt<bool>
142 TimeBuild("time-build",
143 cl::desc("print time spent constructing binary functions"),
144 cl::Hidden, cl::cat(BoltCategory));
146 cl::opt<bool>
147 TrapOnAVX512("trap-avx512",
148 cl::desc("in relocation mode trap upon entry to any function that uses "
149 "AVX-512 instructions"),
150 cl::init(false),
151 cl::ZeroOrMore,
152 cl::Hidden,
153 cl::cat(BoltCategory));
155 bool shouldPrint(const BinaryFunction &Function) {
156 if (Function.isIgnored())
157 return false;
159 if (PrintOnly.empty())
160 return true;
162 for (std::string &Name : opts::PrintOnly) {
163 if (Function.hasNameRegex(Name)) {
164 return true;
168 std::optional<StringRef> Origin = Function.getOriginSectionName();
169 if (Origin && llvm::any_of(opts::PrintOnly, [&](const std::string &Name) {
170 return Name == *Origin;
172 return true;
174 return false;
177 } // namespace opts
179 namespace llvm {
180 namespace bolt {
182 template <typename R> static bool emptyRange(const R &Range) {
183 return Range.begin() == Range.end();
186 /// Gets debug line information for the instruction located at the given
187 /// address in the original binary. The SMLoc's pointer is used
188 /// to point to this information, which is represented by a
189 /// DebugLineTableRowRef. The returned pointer is null if no debug line
190 /// information for this instruction was found.
191 static SMLoc findDebugLineInformationForInstructionAt(
192 uint64_t Address, DWARFUnit *Unit,
193 const DWARFDebugLine::LineTable *LineTable) {
194 // We use the pointer in SMLoc to store an instance of DebugLineTableRowRef,
195 // which occupies 64 bits. Thus, we can only proceed if the struct fits into
196 // the pointer itself.
197 static_assert(
198 sizeof(decltype(SMLoc().getPointer())) >= sizeof(DebugLineTableRowRef),
199 "Cannot fit instruction debug line information into SMLoc's pointer");
201 SMLoc NullResult = DebugLineTableRowRef::NULL_ROW.toSMLoc();
202 uint32_t RowIndex = LineTable->lookupAddress(
203 {Address, object::SectionedAddress::UndefSection});
204 if (RowIndex == LineTable->UnknownRowIndex)
205 return NullResult;
207 assert(RowIndex < LineTable->Rows.size() &&
208 "Line Table lookup returned invalid index.");
210 decltype(SMLoc().getPointer()) Ptr;
211 DebugLineTableRowRef *InstructionLocation =
212 reinterpret_cast<DebugLineTableRowRef *>(&Ptr);
214 InstructionLocation->DwCompileUnitIndex = Unit->getOffset();
215 InstructionLocation->RowIndex = RowIndex + 1;
217 return SMLoc::getFromPointer(Ptr);
220 static std::string buildSectionName(StringRef Prefix, StringRef Name,
221 const BinaryContext &BC) {
222 if (BC.isELF())
223 return (Prefix + Name).str();
224 static NameShortener NS;
225 return (Prefix + Twine(NS.getID(Name))).str();
228 static raw_ostream &operator<<(raw_ostream &OS,
229 const BinaryFunction::State State) {
230 switch (State) {
231 case BinaryFunction::State::Empty: OS << "empty"; break;
232 case BinaryFunction::State::Disassembled: OS << "disassembled"; break;
233 case BinaryFunction::State::CFG: OS << "CFG constructed"; break;
234 case BinaryFunction::State::CFG_Finalized: OS << "CFG finalized"; break;
235 case BinaryFunction::State::EmittedCFG: OS << "emitted with CFG"; break;
236 case BinaryFunction::State::Emitted: OS << "emitted"; break;
239 return OS;
242 std::string BinaryFunction::buildCodeSectionName(StringRef Name,
243 const BinaryContext &BC) {
244 return buildSectionName(BC.isELF() ? ".local.text." : ".l.text.", Name, BC);
247 std::string BinaryFunction::buildColdCodeSectionName(StringRef Name,
248 const BinaryContext &BC) {
249 return buildSectionName(BC.isELF() ? ".local.cold.text." : ".l.c.text.", Name,
250 BC);
253 uint64_t BinaryFunction::Count = 0;
255 std::optional<StringRef>
256 BinaryFunction::hasNameRegex(const StringRef Name) const {
257 const std::string RegexName = (Twine("^") + StringRef(Name) + "$").str();
258 Regex MatchName(RegexName);
259 return forEachName(
260 [&MatchName](StringRef Name) { return MatchName.match(Name); });
263 std::optional<StringRef>
264 BinaryFunction::hasRestoredNameRegex(const StringRef Name) const {
265 const std::string RegexName = (Twine("^") + StringRef(Name) + "$").str();
266 Regex MatchName(RegexName);
267 return forEachName([&MatchName](StringRef Name) {
268 return MatchName.match(NameResolver::restore(Name));
272 std::string BinaryFunction::getDemangledName() const {
273 StringRef MangledName = NameResolver::restore(getOneName());
274 return demangle(MangledName.str());
277 BinaryBasicBlock *
278 BinaryFunction::getBasicBlockContainingOffset(uint64_t Offset) {
279 if (Offset > Size)
280 return nullptr;
282 if (BasicBlockOffsets.empty())
283 return nullptr;
286 * This is commented out because it makes BOLT too slow.
287 * assert(std::is_sorted(BasicBlockOffsets.begin(),
288 * BasicBlockOffsets.end(),
289 * CompareBasicBlockOffsets())));
291 auto I =
292 llvm::upper_bound(BasicBlockOffsets, BasicBlockOffset(Offset, nullptr),
293 CompareBasicBlockOffsets());
294 assert(I != BasicBlockOffsets.begin() && "first basic block not at offset 0");
295 --I;
296 BinaryBasicBlock *BB = I->second;
297 return (Offset < BB->getOffset() + BB->getOriginalSize()) ? BB : nullptr;
300 void BinaryFunction::markUnreachableBlocks() {
301 std::stack<BinaryBasicBlock *> Stack;
303 for (BinaryBasicBlock &BB : blocks())
304 BB.markValid(false);
306 // Add all entries and landing pads as roots.
307 for (BinaryBasicBlock *BB : BasicBlocks) {
308 if (isEntryPoint(*BB) || BB->isLandingPad()) {
309 Stack.push(BB);
310 BB->markValid(true);
311 continue;
313 // FIXME:
314 // Also mark BBs with indirect jumps as reachable, since we do not
315 // support removing unused jump tables yet (GH-issue20).
316 for (const MCInst &Inst : *BB) {
317 if (BC.MIB->getJumpTable(Inst)) {
318 Stack.push(BB);
319 BB->markValid(true);
320 break;
325 // Determine reachable BBs from the entry point
326 while (!Stack.empty()) {
327 BinaryBasicBlock *BB = Stack.top();
328 Stack.pop();
329 for (BinaryBasicBlock *Succ : BB->successors()) {
330 if (Succ->isValid())
331 continue;
332 Succ->markValid(true);
333 Stack.push(Succ);
338 // Any unnecessary fallthrough jumps revealed after calling eraseInvalidBBs
339 // will be cleaned up by fixBranches().
340 std::pair<unsigned, uint64_t>
341 BinaryFunction::eraseInvalidBBs(const MCCodeEmitter *Emitter) {
342 DenseSet<const BinaryBasicBlock *> InvalidBBs;
343 unsigned Count = 0;
344 uint64_t Bytes = 0;
345 for (BinaryBasicBlock *const BB : BasicBlocks) {
346 if (!BB->isValid()) {
347 assert(!isEntryPoint(*BB) && "all entry blocks must be valid");
348 InvalidBBs.insert(BB);
349 ++Count;
350 Bytes += BC.computeCodeSize(BB->begin(), BB->end(), Emitter);
354 Layout.eraseBasicBlocks(InvalidBBs);
356 BasicBlockListType NewBasicBlocks;
357 for (auto I = BasicBlocks.begin(), E = BasicBlocks.end(); I != E; ++I) {
358 BinaryBasicBlock *BB = *I;
359 if (InvalidBBs.contains(BB)) {
360 // Make sure the block is removed from the list of predecessors.
361 BB->removeAllSuccessors();
362 DeletedBasicBlocks.push_back(BB);
363 } else {
364 NewBasicBlocks.push_back(BB);
367 BasicBlocks = std::move(NewBasicBlocks);
369 assert(BasicBlocks.size() == Layout.block_size());
371 // Update CFG state if needed
372 if (Count > 0)
373 recomputeLandingPads();
375 return std::make_pair(Count, Bytes);
378 bool BinaryFunction::isForwardCall(const MCSymbol *CalleeSymbol) const {
379 // This function should work properly before and after function reordering.
380 // In order to accomplish this, we use the function index (if it is valid).
381 // If the function indices are not valid, we fall back to the original
382 // addresses. This should be ok because the functions without valid indices
383 // should have been ordered with a stable sort.
384 const BinaryFunction *CalleeBF = BC.getFunctionForSymbol(CalleeSymbol);
385 if (CalleeBF) {
386 if (CalleeBF->isInjected())
387 return true;
389 if (hasValidIndex() && CalleeBF->hasValidIndex()) {
390 return getIndex() < CalleeBF->getIndex();
391 } else if (hasValidIndex() && !CalleeBF->hasValidIndex()) {
392 return true;
393 } else if (!hasValidIndex() && CalleeBF->hasValidIndex()) {
394 return false;
395 } else {
396 return getAddress() < CalleeBF->getAddress();
398 } else {
399 // Absolute symbol.
400 ErrorOr<uint64_t> CalleeAddressOrError = BC.getSymbolValue(*CalleeSymbol);
401 assert(CalleeAddressOrError && "unregistered symbol found");
402 return *CalleeAddressOrError > getAddress();
406 void BinaryFunction::dump() const {
407 // getDynoStats calls FunctionLayout::updateLayoutIndices and
408 // BasicBlock::analyzeBranch. The former cannot be const, but should be
409 // removed, the latter should be made const, but seems to require refactoring.
410 // Forcing all callers to have a non-const reference to BinaryFunction to call
411 // dump non-const however is not ideal either. Adding this const_cast is right
412 // now the best solution. It is safe, because BinaryFunction itself is not
413 // modified. Only BinaryBasicBlocks are actually modified (if it all) and we
414 // have mutable pointers to those regardless whether this function is
415 // const-qualified or not.
416 const_cast<BinaryFunction &>(*this).print(dbgs(), "");
419 void BinaryFunction::print(raw_ostream &OS, std::string Annotation) {
420 if (!opts::shouldPrint(*this))
421 return;
423 StringRef SectionName =
424 OriginSection ? OriginSection->getName() : "<no origin section>";
425 OS << "Binary Function \"" << *this << "\" " << Annotation << " {";
426 std::vector<StringRef> AllNames = getNames();
427 if (AllNames.size() > 1) {
428 OS << "\n All names : ";
429 const char *Sep = "";
430 for (const StringRef &Name : AllNames) {
431 OS << Sep << Name;
432 Sep = "\n ";
435 OS << "\n Number : " << FunctionNumber;
436 OS << "\n State : " << CurrentState;
437 OS << "\n Address : 0x" << Twine::utohexstr(Address);
438 OS << "\n Size : 0x" << Twine::utohexstr(Size);
439 OS << "\n MaxSize : 0x" << Twine::utohexstr(MaxSize);
440 OS << "\n Offset : 0x" << Twine::utohexstr(getFileOffset());
441 OS << "\n Section : " << SectionName;
442 OS << "\n Orc Section : " << getCodeSectionName();
443 OS << "\n LSDA : 0x" << Twine::utohexstr(getLSDAAddress());
444 OS << "\n IsSimple : " << IsSimple;
445 OS << "\n IsMultiEntry: " << isMultiEntry();
446 OS << "\n IsSplit : " << isSplit();
447 OS << "\n BB Count : " << size();
449 if (HasUnknownControlFlow)
450 OS << "\n Unknown CF : true";
451 if (getPersonalityFunction())
452 OS << "\n Personality : " << getPersonalityFunction()->getName();
453 if (IsFragment)
454 OS << "\n IsFragment : true";
455 if (isFolded())
456 OS << "\n FoldedInto : " << *getFoldedIntoFunction();
457 for (BinaryFunction *ParentFragment : ParentFragments)
458 OS << "\n Parent : " << *ParentFragment;
459 if (!Fragments.empty()) {
460 OS << "\n Fragments : ";
461 ListSeparator LS;
462 for (BinaryFunction *Frag : Fragments)
463 OS << LS << *Frag;
465 if (hasCFG())
466 OS << "\n Hash : " << Twine::utohexstr(computeHash());
467 if (isMultiEntry()) {
468 OS << "\n Secondary Entry Points : ";
469 ListSeparator LS;
470 for (const auto &KV : SecondaryEntryPoints)
471 OS << LS << KV.second->getName();
473 if (FrameInstructions.size())
474 OS << "\n CFI Instrs : " << FrameInstructions.size();
475 if (!Layout.block_empty()) {
476 OS << "\n BB Layout : ";
477 ListSeparator LS;
478 for (const BinaryBasicBlock *BB : Layout.blocks())
479 OS << LS << BB->getName();
481 if (getImageAddress())
482 OS << "\n Image : 0x" << Twine::utohexstr(getImageAddress());
483 if (ExecutionCount != COUNT_NO_PROFILE) {
484 OS << "\n Exec Count : " << ExecutionCount;
485 OS << "\n Branch Count: " << RawBranchCount;
486 OS << "\n Profile Acc : " << format("%.1f%%", ProfileMatchRatio * 100.0f);
489 if (opts::PrintDynoStats && !getLayout().block_empty()) {
490 OS << '\n';
491 DynoStats dynoStats = getDynoStats(*this);
492 OS << dynoStats;
495 OS << "\n}\n";
497 if (opts::PrintDynoStatsOnly || !BC.InstPrinter)
498 return;
500 // Offset of the instruction in function.
501 uint64_t Offset = 0;
503 if (BasicBlocks.empty() && !Instructions.empty()) {
504 // Print before CFG was built.
505 for (const std::pair<const uint32_t, MCInst> &II : Instructions) {
506 Offset = II.first;
508 // Print label if exists at this offset.
509 auto LI = Labels.find(Offset);
510 if (LI != Labels.end()) {
511 if (const MCSymbol *EntrySymbol =
512 getSecondaryEntryPointSymbol(LI->second))
513 OS << EntrySymbol->getName() << " (Entry Point):\n";
514 OS << LI->second->getName() << ":\n";
517 BC.printInstruction(OS, II.second, Offset, this);
521 StringRef SplitPointMsg = "";
522 for (const FunctionFragment &FF : Layout.fragments()) {
523 OS << SplitPointMsg;
524 SplitPointMsg = "------- HOT-COLD SPLIT POINT -------\n\n";
525 for (const BinaryBasicBlock *BB : FF) {
526 OS << BB->getName() << " (" << BB->size()
527 << " instructions, align : " << BB->getAlignment() << ")\n";
529 if (opts::PrintOutputAddressRange)
530 OS << formatv(" Output Address Range: [{0:x}, {1:x}) ({2} bytes)\n",
531 BB->getOutputAddressRange().first,
532 BB->getOutputAddressRange().second, BB->getOutputSize());
534 if (isEntryPoint(*BB)) {
535 if (MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(*BB))
536 OS << " Secondary Entry Point: " << EntrySymbol->getName() << '\n';
537 else
538 OS << " Entry Point\n";
541 if (BB->isLandingPad())
542 OS << " Landing Pad\n";
544 uint64_t BBExecCount = BB->getExecutionCount();
545 if (hasValidProfile()) {
546 OS << " Exec Count : ";
547 if (BB->getExecutionCount() != BinaryBasicBlock::COUNT_NO_PROFILE)
548 OS << BBExecCount << '\n';
549 else
550 OS << "<unknown>\n";
552 if (hasCFI())
553 OS << " CFI State : " << BB->getCFIState() << '\n';
554 if (opts::EnableBAT) {
555 OS << " Input offset: 0x" << Twine::utohexstr(BB->getInputOffset())
556 << "\n";
558 if (!BB->pred_empty()) {
559 OS << " Predecessors: ";
560 ListSeparator LS;
561 for (BinaryBasicBlock *Pred : BB->predecessors())
562 OS << LS << Pred->getName();
563 OS << '\n';
565 if (!BB->throw_empty()) {
566 OS << " Throwers: ";
567 ListSeparator LS;
568 for (BinaryBasicBlock *Throw : BB->throwers())
569 OS << LS << Throw->getName();
570 OS << '\n';
573 Offset = alignTo(Offset, BB->getAlignment());
575 // Note: offsets are imprecise since this is happening prior to
576 // relaxation.
577 Offset = BC.printInstructions(OS, BB->begin(), BB->end(), Offset, this);
579 if (!BB->succ_empty()) {
580 OS << " Successors: ";
581 // For more than 2 successors, sort them based on frequency.
582 std::vector<uint64_t> Indices(BB->succ_size());
583 std::iota(Indices.begin(), Indices.end(), 0);
584 if (BB->succ_size() > 2 && BB->getKnownExecutionCount()) {
585 llvm::stable_sort(Indices, [&](const uint64_t A, const uint64_t B) {
586 return BB->BranchInfo[B] < BB->BranchInfo[A];
589 ListSeparator LS;
590 for (unsigned I = 0; I < Indices.size(); ++I) {
591 BinaryBasicBlock *Succ = BB->Successors[Indices[I]];
592 const BinaryBasicBlock::BinaryBranchInfo &BI =
593 BB->BranchInfo[Indices[I]];
594 OS << LS << Succ->getName();
595 if (ExecutionCount != COUNT_NO_PROFILE &&
596 BI.MispredictedCount != BinaryBasicBlock::COUNT_INFERRED) {
597 OS << " (mispreds: " << BI.MispredictedCount
598 << ", count: " << BI.Count << ")";
599 } else if (ExecutionCount != COUNT_NO_PROFILE &&
600 BI.Count != BinaryBasicBlock::COUNT_NO_PROFILE) {
601 OS << " (inferred count: " << BI.Count << ")";
604 OS << '\n';
607 if (!BB->lp_empty()) {
608 OS << " Landing Pads: ";
609 ListSeparator LS;
610 for (BinaryBasicBlock *LP : BB->landing_pads()) {
611 OS << LS << LP->getName();
612 if (ExecutionCount != COUNT_NO_PROFILE) {
613 OS << " (count: " << LP->getExecutionCount() << ")";
616 OS << '\n';
619 // In CFG_Finalized state we can miscalculate CFI state at exit.
620 if (CurrentState == State::CFG && hasCFI()) {
621 const int32_t CFIStateAtExit = BB->getCFIStateAtExit();
622 if (CFIStateAtExit >= 0)
623 OS << " CFI State: " << CFIStateAtExit << '\n';
626 OS << '\n';
630 // Dump new exception ranges for the function.
631 if (!CallSites.empty()) {
632 OS << "EH table:\n";
633 for (const FunctionFragment &FF : getLayout().fragments()) {
634 for (const auto &FCSI : getCallSites(FF.getFragmentNum())) {
635 const CallSite &CSI = FCSI.second;
636 OS << " [" << *CSI.Start << ", " << *CSI.End << ") landing pad : ";
637 if (CSI.LP)
638 OS << *CSI.LP;
639 else
640 OS << "0";
641 OS << ", action : " << CSI.Action << '\n';
644 OS << '\n';
647 // Print all jump tables.
648 for (const std::pair<const uint64_t, JumpTable *> &JTI : JumpTables)
649 JTI.second->print(OS);
651 OS << "DWARF CFI Instructions:\n";
652 if (OffsetToCFI.size()) {
653 // Pre-buildCFG information
654 for (const std::pair<const uint32_t, uint32_t> &Elmt : OffsetToCFI) {
655 OS << format(" %08x:\t", Elmt.first);
656 assert(Elmt.second < FrameInstructions.size() && "Incorrect CFI offset");
657 BinaryContext::printCFI(OS, FrameInstructions[Elmt.second]);
658 OS << "\n";
660 } else {
661 // Post-buildCFG information
662 for (uint32_t I = 0, E = FrameInstructions.size(); I != E; ++I) {
663 const MCCFIInstruction &CFI = FrameInstructions[I];
664 OS << format(" %d:\t", I);
665 BinaryContext::printCFI(OS, CFI);
666 OS << "\n";
669 if (FrameInstructions.empty())
670 OS << " <empty>\n";
672 OS << "End of Function \"" << *this << "\"\n\n";
675 void BinaryFunction::printRelocations(raw_ostream &OS, uint64_t Offset,
676 uint64_t Size) const {
677 const char *Sep = " # Relocs: ";
679 auto RI = Relocations.lower_bound(Offset);
680 while (RI != Relocations.end() && RI->first < Offset + Size) {
681 OS << Sep << "(R: " << RI->second << ")";
682 Sep = ", ";
683 ++RI;
687 static std::string mutateDWARFExpressionTargetReg(const MCCFIInstruction &Instr,
688 MCPhysReg NewReg) {
689 StringRef ExprBytes = Instr.getValues();
690 assert(ExprBytes.size() > 1 && "DWARF expression CFI is too short");
691 uint8_t Opcode = ExprBytes[0];
692 assert((Opcode == dwarf::DW_CFA_expression ||
693 Opcode == dwarf::DW_CFA_val_expression) &&
694 "invalid DWARF expression CFI");
695 (void)Opcode;
696 const uint8_t *const Start =
697 reinterpret_cast<const uint8_t *>(ExprBytes.drop_front(1).data());
698 const uint8_t *const End =
699 reinterpret_cast<const uint8_t *>(Start + ExprBytes.size() - 1);
700 unsigned Size = 0;
701 decodeULEB128(Start, &Size, End);
702 assert(Size > 0 && "Invalid reg encoding for DWARF expression CFI");
703 SmallString<8> Tmp;
704 raw_svector_ostream OSE(Tmp);
705 encodeULEB128(NewReg, OSE);
706 return Twine(ExprBytes.slice(0, 1))
707 .concat(OSE.str())
708 .concat(ExprBytes.drop_front(1 + Size))
709 .str();
712 void BinaryFunction::mutateCFIRegisterFor(const MCInst &Instr,
713 MCPhysReg NewReg) {
714 const MCCFIInstruction *OldCFI = getCFIFor(Instr);
715 assert(OldCFI && "invalid CFI instr");
716 switch (OldCFI->getOperation()) {
717 default:
718 llvm_unreachable("Unexpected instruction");
719 case MCCFIInstruction::OpDefCfa:
720 setCFIFor(Instr, MCCFIInstruction::cfiDefCfa(nullptr, NewReg,
721 OldCFI->getOffset()));
722 break;
723 case MCCFIInstruction::OpDefCfaRegister:
724 setCFIFor(Instr, MCCFIInstruction::createDefCfaRegister(nullptr, NewReg));
725 break;
726 case MCCFIInstruction::OpOffset:
727 setCFIFor(Instr, MCCFIInstruction::createOffset(nullptr, NewReg,
728 OldCFI->getOffset()));
729 break;
730 case MCCFIInstruction::OpRegister:
731 setCFIFor(Instr, MCCFIInstruction::createRegister(nullptr, NewReg,
732 OldCFI->getRegister2()));
733 break;
734 case MCCFIInstruction::OpSameValue:
735 setCFIFor(Instr, MCCFIInstruction::createSameValue(nullptr, NewReg));
736 break;
737 case MCCFIInstruction::OpEscape:
738 setCFIFor(Instr,
739 MCCFIInstruction::createEscape(
740 nullptr,
741 StringRef(mutateDWARFExpressionTargetReg(*OldCFI, NewReg))));
742 break;
743 case MCCFIInstruction::OpRestore:
744 setCFIFor(Instr, MCCFIInstruction::createRestore(nullptr, NewReg));
745 break;
746 case MCCFIInstruction::OpUndefined:
747 setCFIFor(Instr, MCCFIInstruction::createUndefined(nullptr, NewReg));
748 break;
752 const MCCFIInstruction *BinaryFunction::mutateCFIOffsetFor(const MCInst &Instr,
753 int64_t NewOffset) {
754 const MCCFIInstruction *OldCFI = getCFIFor(Instr);
755 assert(OldCFI && "invalid CFI instr");
756 switch (OldCFI->getOperation()) {
757 default:
758 llvm_unreachable("Unexpected instruction");
759 case MCCFIInstruction::OpDefCfaOffset:
760 setCFIFor(Instr, MCCFIInstruction::cfiDefCfaOffset(nullptr, NewOffset));
761 break;
762 case MCCFIInstruction::OpAdjustCfaOffset:
763 setCFIFor(Instr,
764 MCCFIInstruction::createAdjustCfaOffset(nullptr, NewOffset));
765 break;
766 case MCCFIInstruction::OpDefCfa:
767 setCFIFor(Instr, MCCFIInstruction::cfiDefCfa(nullptr, OldCFI->getRegister(),
768 NewOffset));
769 break;
770 case MCCFIInstruction::OpOffset:
771 setCFIFor(Instr, MCCFIInstruction::createOffset(
772 nullptr, OldCFI->getRegister(), NewOffset));
773 break;
775 return getCFIFor(Instr);
778 IndirectBranchType
779 BinaryFunction::processIndirectBranch(MCInst &Instruction, unsigned Size,
780 uint64_t Offset,
781 uint64_t &TargetAddress) {
782 const unsigned PtrSize = BC.AsmInfo->getCodePointerSize();
784 // The instruction referencing memory used by the branch instruction.
785 // It could be the branch instruction itself or one of the instructions
786 // setting the value of the register used by the branch.
787 MCInst *MemLocInstr;
789 // The instruction loading the fixed PIC jump table entry value.
790 MCInst *FixedEntryLoadInstr;
792 // Address of the table referenced by MemLocInstr. Could be either an
793 // array of function pointers, or a jump table.
794 uint64_t ArrayStart = 0;
796 unsigned BaseRegNum, IndexRegNum;
797 int64_t DispValue;
798 const MCExpr *DispExpr;
800 // In AArch, identify the instruction adding the PC-relative offset to
801 // jump table entries to correctly decode it.
802 MCInst *PCRelBaseInstr;
803 uint64_t PCRelAddr = 0;
805 auto Begin = Instructions.begin();
806 if (BC.isAArch64()) {
807 PreserveNops = BC.HasRelocations;
808 // Start at the last label as an approximation of the current basic block.
809 // This is a heuristic, since the full set of labels have yet to be
810 // determined
811 for (const uint32_t Offset :
812 llvm::make_first_range(llvm::reverse(Labels))) {
813 auto II = Instructions.find(Offset);
814 if (II != Instructions.end()) {
815 Begin = II;
816 break;
821 IndirectBranchType BranchType = BC.MIB->analyzeIndirectBranch(
822 Instruction, Begin, Instructions.end(), PtrSize, MemLocInstr, BaseRegNum,
823 IndexRegNum, DispValue, DispExpr, PCRelBaseInstr, FixedEntryLoadInstr);
825 if (BranchType == IndirectBranchType::UNKNOWN && !MemLocInstr)
826 return BranchType;
828 if (MemLocInstr != &Instruction)
829 IndexRegNum = BC.MIB->getNoRegister();
831 if (BC.isAArch64()) {
832 const MCSymbol *Sym = BC.MIB->getTargetSymbol(*PCRelBaseInstr, 1);
833 assert(Sym && "Symbol extraction failed");
834 ErrorOr<uint64_t> SymValueOrError = BC.getSymbolValue(*Sym);
835 if (SymValueOrError) {
836 PCRelAddr = *SymValueOrError;
837 } else {
838 for (std::pair<const uint32_t, MCSymbol *> &Elmt : Labels) {
839 if (Elmt.second == Sym) {
840 PCRelAddr = Elmt.first + getAddress();
841 break;
845 uint64_t InstrAddr = 0;
846 for (auto II = Instructions.rbegin(); II != Instructions.rend(); ++II) {
847 if (&II->second == PCRelBaseInstr) {
848 InstrAddr = II->first + getAddress();
849 break;
852 assert(InstrAddr != 0 && "instruction not found");
853 // We do this to avoid spurious references to code locations outside this
854 // function (for example, if the indirect jump lives in the last basic
855 // block of the function, it will create a reference to the next function).
856 // This replaces a symbol reference with an immediate.
857 BC.MIB->replaceMemOperandDisp(*PCRelBaseInstr,
858 MCOperand::createImm(PCRelAddr - InstrAddr));
859 // FIXME: Disable full jump table processing for AArch64 until we have a
860 // proper way of determining the jump table limits.
861 return IndirectBranchType::UNKNOWN;
864 auto getExprValue = [&](const MCExpr *Expr) {
865 const MCSymbol *TargetSym;
866 uint64_t TargetOffset;
867 std::tie(TargetSym, TargetOffset) = BC.MIB->getTargetSymbolInfo(Expr);
868 ErrorOr<uint64_t> SymValueOrError = BC.getSymbolValue(*TargetSym);
869 assert(SymValueOrError && "Global symbol needs a value");
870 return *SymValueOrError + TargetOffset;
873 // RIP-relative addressing should be converted to symbol form by now
874 // in processed instructions (but not in jump).
875 if (DispExpr) {
876 ArrayStart = getExprValue(DispExpr);
877 BaseRegNum = BC.MIB->getNoRegister();
878 if (BC.isAArch64()) {
879 ArrayStart &= ~0xFFFULL;
880 ArrayStart += DispValue & 0xFFFULL;
882 } else {
883 ArrayStart = static_cast<uint64_t>(DispValue);
886 if (BaseRegNum == BC.MRI->getProgramCounter())
887 ArrayStart += getAddress() + Offset + Size;
889 if (FixedEntryLoadInstr) {
890 assert(BranchType == IndirectBranchType::POSSIBLE_PIC_FIXED_BRANCH &&
891 "Invalid IndirectBranch type");
892 MCInst::iterator FixedEntryDispOperand =
893 BC.MIB->getMemOperandDisp(*FixedEntryLoadInstr);
894 assert(FixedEntryDispOperand != FixedEntryLoadInstr->end() &&
895 "Invalid memory instruction");
896 const MCExpr *FixedEntryDispExpr = FixedEntryDispOperand->getExpr();
897 const uint64_t EntryAddress = getExprValue(FixedEntryDispExpr);
898 uint64_t EntrySize = BC.getJumpTableEntrySize(JumpTable::JTT_PIC);
899 ErrorOr<int64_t> Value =
900 BC.getSignedValueAtAddress(EntryAddress, EntrySize);
901 if (!Value)
902 return IndirectBranchType::UNKNOWN;
904 BC.outs() << "BOLT-INFO: fixed PIC indirect branch detected in " << *this
905 << " at 0x" << Twine::utohexstr(getAddress() + Offset)
906 << " referencing data at 0x" << Twine::utohexstr(EntryAddress)
907 << " the destination value is 0x"
908 << Twine::utohexstr(ArrayStart + *Value) << '\n';
910 TargetAddress = ArrayStart + *Value;
912 // Remove spurious JumpTable at EntryAddress caused by PIC reference from
913 // the load instruction.
914 BC.deleteJumpTable(EntryAddress);
916 // Replace FixedEntryDispExpr used in target address calculation with outer
917 // jump table reference.
918 JumpTable *JT = BC.getJumpTableContainingAddress(ArrayStart);
919 assert(JT && "Must have a containing jump table for PIC fixed branch");
920 BC.MIB->replaceMemOperandDisp(*FixedEntryLoadInstr, JT->getFirstLabel(),
921 EntryAddress - ArrayStart, &*BC.Ctx);
923 return BranchType;
926 LLVM_DEBUG(dbgs() << "BOLT-DEBUG: addressed memory is 0x"
927 << Twine::utohexstr(ArrayStart) << '\n');
929 ErrorOr<BinarySection &> Section = BC.getSectionForAddress(ArrayStart);
930 if (!Section) {
931 // No section - possibly an absolute address. Since we don't allow
932 // internal function addresses to escape the function scope - we
933 // consider it a tail call.
934 if (opts::Verbosity >= 1) {
935 BC.errs() << "BOLT-WARNING: no section for address 0x"
936 << Twine::utohexstr(ArrayStart) << " referenced from function "
937 << *this << '\n';
939 return IndirectBranchType::POSSIBLE_TAIL_CALL;
941 if (Section->isVirtual()) {
942 // The contents are filled at runtime.
943 return IndirectBranchType::POSSIBLE_TAIL_CALL;
946 if (BranchType == IndirectBranchType::POSSIBLE_FIXED_BRANCH) {
947 ErrorOr<uint64_t> Value = BC.getPointerAtAddress(ArrayStart);
948 if (!Value)
949 return IndirectBranchType::UNKNOWN;
951 if (BC.getSectionForAddress(ArrayStart)->isWritable())
952 return IndirectBranchType::UNKNOWN;
954 BC.outs() << "BOLT-INFO: fixed indirect branch detected in " << *this
955 << " at 0x" << Twine::utohexstr(getAddress() + Offset)
956 << " referencing data at 0x" << Twine::utohexstr(ArrayStart)
957 << " the destination value is 0x" << Twine::utohexstr(*Value)
958 << '\n';
960 TargetAddress = *Value;
961 return BranchType;
964 // Check if there's already a jump table registered at this address.
965 MemoryContentsType MemType;
966 if (JumpTable *JT = BC.getJumpTableContainingAddress(ArrayStart)) {
967 switch (JT->Type) {
968 case JumpTable::JTT_NORMAL:
969 MemType = MemoryContentsType::POSSIBLE_JUMP_TABLE;
970 break;
971 case JumpTable::JTT_PIC:
972 MemType = MemoryContentsType::POSSIBLE_PIC_JUMP_TABLE;
973 break;
975 } else {
976 MemType = BC.analyzeMemoryAt(ArrayStart, *this);
979 // Check that jump table type in instruction pattern matches memory contents.
980 JumpTable::JumpTableType JTType;
981 if (BranchType == IndirectBranchType::POSSIBLE_PIC_JUMP_TABLE) {
982 if (MemType != MemoryContentsType::POSSIBLE_PIC_JUMP_TABLE)
983 return IndirectBranchType::UNKNOWN;
984 JTType = JumpTable::JTT_PIC;
985 } else {
986 if (MemType == MemoryContentsType::POSSIBLE_PIC_JUMP_TABLE)
987 return IndirectBranchType::UNKNOWN;
989 if (MemType == MemoryContentsType::UNKNOWN)
990 return IndirectBranchType::POSSIBLE_TAIL_CALL;
992 BranchType = IndirectBranchType::POSSIBLE_JUMP_TABLE;
993 JTType = JumpTable::JTT_NORMAL;
996 // Convert the instruction into jump table branch.
997 const MCSymbol *JTLabel = BC.getOrCreateJumpTable(*this, ArrayStart, JTType);
998 BC.MIB->replaceMemOperandDisp(*MemLocInstr, JTLabel, BC.Ctx.get());
999 BC.MIB->setJumpTable(Instruction, ArrayStart, IndexRegNum);
1001 JTSites.emplace_back(Offset, ArrayStart);
1003 return BranchType;
1006 MCSymbol *BinaryFunction::getOrCreateLocalLabel(uint64_t Address,
1007 bool CreatePastEnd) {
1008 const uint64_t Offset = Address - getAddress();
1010 if ((Offset == getSize()) && CreatePastEnd)
1011 return getFunctionEndLabel();
1013 auto LI = Labels.find(Offset);
1014 if (LI != Labels.end())
1015 return LI->second;
1017 // For AArch64, check if this address is part of a constant island.
1018 if (BC.isAArch64()) {
1019 if (MCSymbol *IslandSym = getOrCreateIslandAccess(Address))
1020 return IslandSym;
1023 MCSymbol *Label = BC.Ctx->createNamedTempSymbol();
1024 Labels[Offset] = Label;
1026 return Label;
1029 ErrorOr<ArrayRef<uint8_t>> BinaryFunction::getData() const {
1030 BinarySection &Section = *getOriginSection();
1031 assert(Section.containsRange(getAddress(), getMaxSize()) &&
1032 "wrong section for function");
1034 if (!Section.isText() || Section.isVirtual() || !Section.getSize())
1035 return std::make_error_code(std::errc::bad_address);
1037 StringRef SectionContents = Section.getContents();
1039 assert(SectionContents.size() == Section.getSize() &&
1040 "section size mismatch");
1042 // Function offset from the section start.
1043 uint64_t Offset = getAddress() - Section.getAddress();
1044 auto *Bytes = reinterpret_cast<const uint8_t *>(SectionContents.data());
1045 return ArrayRef<uint8_t>(Bytes + Offset, getMaxSize());
1048 size_t BinaryFunction::getSizeOfDataInCodeAt(uint64_t Offset) const {
1049 if (!Islands)
1050 return 0;
1052 if (!llvm::is_contained(Islands->DataOffsets, Offset))
1053 return 0;
1055 auto Iter = Islands->CodeOffsets.upper_bound(Offset);
1056 if (Iter != Islands->CodeOffsets.end())
1057 return *Iter - Offset;
1058 return getSize() - Offset;
1061 bool BinaryFunction::isZeroPaddingAt(uint64_t Offset) const {
1062 ArrayRef<uint8_t> FunctionData = *getData();
1063 uint64_t EndOfCode = getSize();
1064 if (Islands) {
1065 auto Iter = Islands->DataOffsets.upper_bound(Offset);
1066 if (Iter != Islands->DataOffsets.end())
1067 EndOfCode = *Iter;
1069 for (uint64_t I = Offset; I < EndOfCode; ++I)
1070 if (FunctionData[I] != 0)
1071 return false;
1073 return true;
1076 Error BinaryFunction::handlePCRelOperand(MCInst &Instruction, uint64_t Address,
1077 uint64_t Size) {
1078 auto &MIB = BC.MIB;
1079 uint64_t TargetAddress = 0;
1080 if (!MIB->evaluateMemOperandTarget(Instruction, TargetAddress, Address,
1081 Size)) {
1082 std::string Msg;
1083 raw_string_ostream SS(Msg);
1084 SS << "BOLT-ERROR: PC-relative operand can't be evaluated:\n";
1085 BC.InstPrinter->printInst(&Instruction, 0, "", *BC.STI, SS);
1086 SS << '\n';
1087 Instruction.dump_pretty(SS, BC.InstPrinter.get());
1088 SS << '\n';
1089 SS << "BOLT-ERROR: cannot handle PC-relative operand at 0x"
1090 << Twine::utohexstr(Address) << ". Skipping function " << *this << ".\n";
1091 if (BC.HasRelocations)
1092 return createFatalBOLTError(Msg);
1093 IsSimple = false;
1094 return createNonFatalBOLTError(Msg);
1096 if (TargetAddress == 0 && opts::Verbosity >= 1) {
1097 BC.outs() << "BOLT-INFO: PC-relative operand is zero in function " << *this
1098 << '\n';
1101 const MCSymbol *TargetSymbol;
1102 uint64_t TargetOffset;
1103 std::tie(TargetSymbol, TargetOffset) =
1104 BC.handleAddressRef(TargetAddress, *this, /*IsPCRel*/ true);
1106 bool ReplaceSuccess = MIB->replaceMemOperandDisp(
1107 Instruction, TargetSymbol, static_cast<int64_t>(TargetOffset), &*BC.Ctx);
1108 (void)ReplaceSuccess;
1109 assert(ReplaceSuccess && "Failed to replace mem operand with symbol+off.");
1110 return Error::success();
1113 MCSymbol *BinaryFunction::handleExternalReference(MCInst &Instruction,
1114 uint64_t Size,
1115 uint64_t Offset,
1116 uint64_t TargetAddress,
1117 bool &IsCall) {
1118 auto &MIB = BC.MIB;
1120 const uint64_t AbsoluteInstrAddr = getAddress() + Offset;
1121 BC.addInterproceduralReference(this, TargetAddress);
1122 if (opts::Verbosity >= 2 && !IsCall && Size == 2 && !BC.HasRelocations) {
1123 BC.errs() << "BOLT-WARNING: relaxed tail call detected at 0x"
1124 << Twine::utohexstr(AbsoluteInstrAddr) << " in function " << *this
1125 << ". Code size will be increased.\n";
1128 assert(!MIB->isTailCall(Instruction) &&
1129 "synthetic tail call instruction found");
1131 // This is a call regardless of the opcode.
1132 // Assign proper opcode for tail calls, so that they could be
1133 // treated as calls.
1134 if (!IsCall) {
1135 if (!MIB->convertJmpToTailCall(Instruction)) {
1136 assert(MIB->isConditionalBranch(Instruction) &&
1137 "unknown tail call instruction");
1138 if (opts::Verbosity >= 2) {
1139 BC.errs() << "BOLT-WARNING: conditional tail call detected in "
1140 << "function " << *this << " at 0x"
1141 << Twine::utohexstr(AbsoluteInstrAddr) << ".\n";
1144 IsCall = true;
1147 if (opts::Verbosity >= 2 && TargetAddress == 0) {
1148 // We actually see calls to address 0 in presence of weak
1149 // symbols originating from libraries. This code is never meant
1150 // to be executed.
1151 BC.outs() << "BOLT-INFO: Function " << *this
1152 << " has a call to address zero.\n";
1155 return BC.getOrCreateGlobalSymbol(TargetAddress, "FUNCat");
1158 void BinaryFunction::handleIndirectBranch(MCInst &Instruction, uint64_t Size,
1159 uint64_t Offset) {
1160 auto &MIB = BC.MIB;
1161 uint64_t IndirectTarget = 0;
1162 IndirectBranchType Result =
1163 processIndirectBranch(Instruction, Size, Offset, IndirectTarget);
1164 switch (Result) {
1165 default:
1166 llvm_unreachable("unexpected result");
1167 case IndirectBranchType::POSSIBLE_TAIL_CALL: {
1168 bool Result = MIB->convertJmpToTailCall(Instruction);
1169 (void)Result;
1170 assert(Result);
1171 break;
1173 case IndirectBranchType::POSSIBLE_JUMP_TABLE:
1174 case IndirectBranchType::POSSIBLE_PIC_JUMP_TABLE:
1175 case IndirectBranchType::POSSIBLE_PIC_FIXED_BRANCH:
1176 if (opts::JumpTables == JTS_NONE)
1177 IsSimple = false;
1178 break;
1179 case IndirectBranchType::POSSIBLE_FIXED_BRANCH: {
1180 if (containsAddress(IndirectTarget)) {
1181 const MCSymbol *TargetSymbol = getOrCreateLocalLabel(IndirectTarget);
1182 Instruction.clear();
1183 MIB->createUncondBranch(Instruction, TargetSymbol, BC.Ctx.get());
1184 TakenBranches.emplace_back(Offset, IndirectTarget - getAddress());
1185 addEntryPointAtOffset(IndirectTarget - getAddress());
1186 } else {
1187 MIB->convertJmpToTailCall(Instruction);
1188 BC.addInterproceduralReference(this, IndirectTarget);
1190 break;
1192 case IndirectBranchType::UNKNOWN:
1193 // Keep processing. We'll do more checks and fixes in
1194 // postProcessIndirectBranches().
1195 UnknownIndirectBranchOffsets.emplace(Offset);
1196 break;
1200 void BinaryFunction::handleAArch64IndirectCall(MCInst &Instruction,
1201 const uint64_t Offset) {
1202 auto &MIB = BC.MIB;
1203 const uint64_t AbsoluteInstrAddr = getAddress() + Offset;
1204 MCInst *TargetHiBits, *TargetLowBits;
1205 uint64_t TargetAddress, Count;
1206 Count = MIB->matchLinkerVeneer(Instructions.begin(), Instructions.end(),
1207 AbsoluteInstrAddr, Instruction, TargetHiBits,
1208 TargetLowBits, TargetAddress);
1209 if (Count) {
1210 MIB->addAnnotation(Instruction, "AArch64Veneer", true);
1211 --Count;
1212 for (auto It = std::prev(Instructions.end()); Count != 0;
1213 It = std::prev(It), --Count) {
1214 MIB->addAnnotation(It->second, "AArch64Veneer", true);
1217 BC.addAdrpAddRelocAArch64(*this, *TargetLowBits, *TargetHiBits,
1218 TargetAddress);
1222 std::optional<MCInst>
1223 BinaryFunction::disassembleInstructionAtOffset(uint64_t Offset) const {
1224 assert(CurrentState == State::Empty && "Function should not be disassembled");
1225 assert(Offset < MaxSize && "Invalid offset");
1226 ErrorOr<ArrayRef<unsigned char>> FunctionData = getData();
1227 assert(FunctionData && "Cannot get function as data");
1228 MCInst Instr;
1229 uint64_t InstrSize = 0;
1230 const uint64_t InstrAddress = getAddress() + Offset;
1231 if (BC.DisAsm->getInstruction(Instr, InstrSize, FunctionData->slice(Offset),
1232 InstrAddress, nulls()))
1233 return Instr;
1234 return std::nullopt;
1237 Error BinaryFunction::disassemble() {
1238 NamedRegionTimer T("disassemble", "Disassemble function", "buildfuncs",
1239 "Build Binary Functions", opts::TimeBuild);
1240 ErrorOr<ArrayRef<uint8_t>> ErrorOrFunctionData = getData();
1241 assert(ErrorOrFunctionData && "function data is not available");
1242 ArrayRef<uint8_t> FunctionData = *ErrorOrFunctionData;
1243 assert(FunctionData.size() == getMaxSize() &&
1244 "function size does not match raw data size");
1246 auto &Ctx = BC.Ctx;
1247 auto &MIB = BC.MIB;
1249 BC.SymbolicDisAsm->setSymbolizer(MIB->createTargetSymbolizer(*this));
1251 // Insert a label at the beginning of the function. This will be our first
1252 // basic block.
1253 Labels[0] = Ctx->createNamedTempSymbol("BB0");
1255 // Map offsets in the function to a label that should always point to the
1256 // corresponding instruction. This is used for labels that shouldn't point to
1257 // the start of a basic block but always to a specific instruction. This is
1258 // used, for example, on RISC-V where %pcrel_lo relocations point to the
1259 // corresponding %pcrel_hi.
1260 LabelsMapType InstructionLabels;
1262 uint64_t Size = 0; // instruction size
1263 for (uint64_t Offset = 0; Offset < getSize(); Offset += Size) {
1264 MCInst Instruction;
1265 const uint64_t AbsoluteInstrAddr = getAddress() + Offset;
1267 // Check for data inside code and ignore it
1268 if (const size_t DataInCodeSize = getSizeOfDataInCodeAt(Offset)) {
1269 Size = DataInCodeSize;
1270 continue;
1273 if (!BC.SymbolicDisAsm->getInstruction(Instruction, Size,
1274 FunctionData.slice(Offset),
1275 AbsoluteInstrAddr, nulls())) {
1276 // Functions with "soft" boundaries, e.g. coming from assembly source,
1277 // can have 0-byte padding at the end.
1278 if (isZeroPaddingAt(Offset))
1279 break;
1281 BC.errs()
1282 << "BOLT-WARNING: unable to disassemble instruction at offset 0x"
1283 << Twine::utohexstr(Offset) << " (address 0x"
1284 << Twine::utohexstr(AbsoluteInstrAddr) << ") in function " << *this
1285 << '\n';
1286 // Some AVX-512 instructions could not be disassembled at all.
1287 if (BC.HasRelocations && opts::TrapOnAVX512 && BC.isX86()) {
1288 setTrapOnEntry();
1289 BC.TrappedFunctions.push_back(this);
1290 } else {
1291 setIgnored();
1294 break;
1297 // Check integrity of LLVM assembler/disassembler.
1298 if (opts::CheckEncoding && !BC.MIB->isBranch(Instruction) &&
1299 !BC.MIB->isCall(Instruction) && !BC.MIB->isNoop(Instruction)) {
1300 if (!BC.validateInstructionEncoding(FunctionData.slice(Offset, Size))) {
1301 BC.errs() << "BOLT-WARNING: mismatching LLVM encoding detected in "
1302 << "function " << *this << " for instruction :\n";
1303 BC.printInstruction(BC.errs(), Instruction, AbsoluteInstrAddr);
1304 BC.errs() << '\n';
1308 // Special handling for AVX-512 instructions.
1309 if (MIB->hasEVEXEncoding(Instruction)) {
1310 if (BC.HasRelocations && opts::TrapOnAVX512) {
1311 setTrapOnEntry();
1312 BC.TrappedFunctions.push_back(this);
1313 break;
1316 if (!BC.validateInstructionEncoding(FunctionData.slice(Offset, Size))) {
1317 BC.errs() << "BOLT-WARNING: internal assembler/disassembler error "
1318 "detected for AVX512 instruction:\n";
1319 BC.printInstruction(BC.errs(), Instruction, AbsoluteInstrAddr);
1320 BC.errs() << " in function " << *this << '\n';
1321 setIgnored();
1322 break;
1326 bool IsUnsupported = BC.MIB->isUnsupportedInstruction(Instruction);
1327 if (IsUnsupported)
1328 setIgnored();
1330 if (MIB->isBranch(Instruction) || MIB->isCall(Instruction)) {
1331 uint64_t TargetAddress = 0;
1332 if (MIB->evaluateBranch(Instruction, AbsoluteInstrAddr, Size,
1333 TargetAddress)) {
1334 // Check if the target is within the same function. Otherwise it's
1335 // a call, possibly a tail call.
1337 // If the target *is* the function address it could be either a branch
1338 // or a recursive call.
1339 bool IsCall = MIB->isCall(Instruction);
1340 const bool IsCondBranch = MIB->isConditionalBranch(Instruction);
1341 MCSymbol *TargetSymbol = nullptr;
1343 if (IsUnsupported)
1344 if (auto *TargetFunc =
1345 BC.getBinaryFunctionContainingAddress(TargetAddress))
1346 TargetFunc->setIgnored();
1348 if (IsCall && TargetAddress == getAddress()) {
1349 // A recursive call. Calls to internal blocks are handled by
1350 // ValidateInternalCalls pass.
1351 TargetSymbol = getSymbol();
1354 if (!TargetSymbol) {
1355 // Create either local label or external symbol.
1356 if (containsAddress(TargetAddress)) {
1357 TargetSymbol = getOrCreateLocalLabel(TargetAddress);
1358 } else {
1359 if (TargetAddress == getAddress() + getSize() &&
1360 TargetAddress < getAddress() + getMaxSize() &&
1361 !(BC.isAArch64() &&
1362 BC.handleAArch64Veneer(TargetAddress, /*MatchOnly*/ true))) {
1363 // Result of __builtin_unreachable().
1364 LLVM_DEBUG(dbgs() << "BOLT-DEBUG: jump past end detected at 0x"
1365 << Twine::utohexstr(AbsoluteInstrAddr)
1366 << " in function " << *this
1367 << " : replacing with nop.\n");
1368 BC.MIB->createNoop(Instruction);
1369 if (IsCondBranch) {
1370 // Register branch offset for profile validation.
1371 IgnoredBranches.emplace_back(Offset, Offset + Size);
1373 goto add_instruction;
1375 // May update Instruction and IsCall
1376 TargetSymbol = handleExternalReference(Instruction, Size, Offset,
1377 TargetAddress, IsCall);
1381 if (!IsCall) {
1382 // Add taken branch info.
1383 TakenBranches.emplace_back(Offset, TargetAddress - getAddress());
1385 BC.MIB->replaceBranchTarget(Instruction, TargetSymbol, &*Ctx);
1387 // Mark CTC.
1388 if (IsCondBranch && IsCall)
1389 MIB->setConditionalTailCall(Instruction, TargetAddress);
1390 } else {
1391 // Could not evaluate branch. Should be an indirect call or an
1392 // indirect branch. Bail out on the latter case.
1393 if (MIB->isIndirectBranch(Instruction))
1394 handleIndirectBranch(Instruction, Size, Offset);
1395 // Indirect call. We only need to fix it if the operand is RIP-relative.
1396 if (IsSimple && MIB->hasPCRelOperand(Instruction)) {
1397 if (auto NewE = handleErrors(
1398 handlePCRelOperand(Instruction, AbsoluteInstrAddr, Size),
1399 [&](const BOLTError &E) -> Error {
1400 if (E.isFatal())
1401 return Error(std::make_unique<BOLTError>(std::move(E)));
1402 if (!E.getMessage().empty())
1403 E.log(BC.errs());
1404 return Error::success();
1405 })) {
1406 return Error(std::move(NewE));
1410 if (BC.isAArch64())
1411 handleAArch64IndirectCall(Instruction, Offset);
1413 } else if (BC.isAArch64() || BC.isRISCV()) {
1414 // Check if there's a relocation associated with this instruction.
1415 bool UsedReloc = false;
1416 for (auto Itr = Relocations.lower_bound(Offset),
1417 ItrE = Relocations.lower_bound(Offset + Size);
1418 Itr != ItrE; ++Itr) {
1419 const Relocation &Relocation = Itr->second;
1420 MCSymbol *Symbol = Relocation.Symbol;
1422 if (Relocation::isInstructionReference(Relocation.Type)) {
1423 uint64_t RefOffset = Relocation.Value - getAddress();
1424 LabelsMapType::iterator LI = InstructionLabels.find(RefOffset);
1426 if (LI == InstructionLabels.end()) {
1427 Symbol = BC.Ctx->createNamedTempSymbol();
1428 InstructionLabels.emplace(RefOffset, Symbol);
1429 } else {
1430 Symbol = LI->second;
1434 int64_t Value = Relocation.Value;
1435 const bool Result = BC.MIB->replaceImmWithSymbolRef(
1436 Instruction, Symbol, Relocation.Addend, Ctx.get(), Value,
1437 Relocation.Type);
1438 (void)Result;
1439 assert(Result && "cannot replace immediate with relocation");
1441 // For aarch64, if we replaced an immediate with a symbol from a
1442 // relocation, we mark it so we do not try to further process a
1443 // pc-relative operand. All we need is the symbol.
1444 UsedReloc = true;
1447 if (!BC.isRISCV() && MIB->hasPCRelOperand(Instruction) && !UsedReloc) {
1448 if (auto NewE = handleErrors(
1449 handlePCRelOperand(Instruction, AbsoluteInstrAddr, Size),
1450 [&](const BOLTError &E) -> Error {
1451 if (E.isFatal())
1452 return Error(std::make_unique<BOLTError>(std::move(E)));
1453 if (!E.getMessage().empty())
1454 E.log(BC.errs());
1455 return Error::success();
1457 return Error(std::move(NewE));
1461 add_instruction:
1462 if (getDWARFLineTable()) {
1463 Instruction.setLoc(findDebugLineInformationForInstructionAt(
1464 AbsoluteInstrAddr, getDWARFUnit(), getDWARFLineTable()));
1467 // Record offset of the instruction for profile matching.
1468 if (BC.keepOffsetForInstruction(Instruction))
1469 MIB->setOffset(Instruction, static_cast<uint32_t>(Offset));
1471 if (BC.isX86() && BC.MIB->isNoop(Instruction)) {
1472 // NOTE: disassembly loses the correct size information for noops on x86.
1473 // E.g. nopw 0x0(%rax,%rax,1) is 9 bytes, but re-encoded it's only
1474 // 5 bytes. Preserve the size info using annotations.
1475 MIB->setSize(Instruction, Size);
1478 addInstruction(Offset, std::move(Instruction));
1481 for (auto [Offset, Label] : InstructionLabels) {
1482 InstrMapType::iterator II = Instructions.find(Offset);
1483 assert(II != Instructions.end() && "reference to non-existing instruction");
1485 BC.MIB->setInstLabel(II->second, Label);
1488 // Reset symbolizer for the disassembler.
1489 BC.SymbolicDisAsm->setSymbolizer(nullptr);
1491 if (uint64_t Offset = getFirstInstructionOffset())
1492 Labels[Offset] = BC.Ctx->createNamedTempSymbol();
1494 clearList(Relocations);
1496 if (!IsSimple) {
1497 clearList(Instructions);
1498 return createNonFatalBOLTError("");
1501 updateState(State::Disassembled);
1503 return Error::success();
1506 MCSymbol *BinaryFunction::registerBranch(uint64_t Src, uint64_t Dst) {
1507 assert(CurrentState == State::Disassembled &&
1508 "Cannot register branch unless function is in disassembled state.");
1509 assert(containsAddress(Src) && containsAddress(Dst) &&
1510 "Cannot register external branch.");
1511 MCSymbol *Target = getOrCreateLocalLabel(Dst);
1512 TakenBranches.emplace_back(Src - getAddress(), Dst - getAddress());
1513 return Target;
1516 bool BinaryFunction::scanExternalRefs() {
1517 bool Success = true;
1518 bool DisassemblyFailed = false;
1520 // Ignore pseudo functions.
1521 if (isPseudo())
1522 return Success;
1524 if (opts::NoScan) {
1525 clearList(Relocations);
1526 clearList(ExternallyReferencedOffsets);
1528 return false;
1531 // List of external references for this function.
1532 std::vector<Relocation> FunctionRelocations;
1534 static BinaryContext::IndependentCodeEmitter Emitter =
1535 BC.createIndependentMCCodeEmitter();
1537 ErrorOr<ArrayRef<uint8_t>> ErrorOrFunctionData = getData();
1538 assert(ErrorOrFunctionData && "function data is not available");
1539 ArrayRef<uint8_t> FunctionData = *ErrorOrFunctionData;
1540 assert(FunctionData.size() == getMaxSize() &&
1541 "function size does not match raw data size");
1543 BC.SymbolicDisAsm->setSymbolizer(
1544 BC.MIB->createTargetSymbolizer(*this, /*CreateSymbols*/ false));
1546 // Disassemble contents of the function. Detect code entry points and create
1547 // relocations for references to code that will be moved.
1548 uint64_t Size = 0; // instruction size
1549 for (uint64_t Offset = 0; Offset < getSize(); Offset += Size) {
1550 // Check for data inside code and ignore it
1551 if (const size_t DataInCodeSize = getSizeOfDataInCodeAt(Offset)) {
1552 Size = DataInCodeSize;
1553 continue;
1556 const uint64_t AbsoluteInstrAddr = getAddress() + Offset;
1557 MCInst Instruction;
1558 if (!BC.SymbolicDisAsm->getInstruction(Instruction, Size,
1559 FunctionData.slice(Offset),
1560 AbsoluteInstrAddr, nulls())) {
1561 if (opts::Verbosity >= 1 && !isZeroPaddingAt(Offset)) {
1562 BC.errs()
1563 << "BOLT-WARNING: unable to disassemble instruction at offset 0x"
1564 << Twine::utohexstr(Offset) << " (address 0x"
1565 << Twine::utohexstr(AbsoluteInstrAddr) << ") in function " << *this
1566 << '\n';
1568 Success = false;
1569 DisassemblyFailed = true;
1570 break;
1573 // Return true if we can skip handling the Target function reference.
1574 auto ignoreFunctionRef = [&](const BinaryFunction &Target) {
1575 if (&Target == this)
1576 return true;
1578 // Note that later we may decide not to emit Target function. In that
1579 // case, we conservatively create references that will be ignored or
1580 // resolved to the same function.
1581 if (!BC.shouldEmit(Target))
1582 return true;
1584 return false;
1587 // Return true if we can ignore reference to the symbol.
1588 auto ignoreReference = [&](const MCSymbol *TargetSymbol) {
1589 if (!TargetSymbol)
1590 return true;
1592 if (BC.forceSymbolRelocations(TargetSymbol->getName()))
1593 return false;
1595 BinaryFunction *TargetFunction = BC.getFunctionForSymbol(TargetSymbol);
1596 if (!TargetFunction)
1597 return true;
1599 return ignoreFunctionRef(*TargetFunction);
1602 // Handle calls and branches separately as symbolization doesn't work for
1603 // them yet.
1604 MCSymbol *BranchTargetSymbol = nullptr;
1605 if (BC.MIB->isCall(Instruction) || BC.MIB->isBranch(Instruction)) {
1606 uint64_t TargetAddress = 0;
1607 BC.MIB->evaluateBranch(Instruction, AbsoluteInstrAddr, Size,
1608 TargetAddress);
1610 // Create an entry point at reference address if needed.
1611 BinaryFunction *TargetFunction =
1612 BC.getBinaryFunctionContainingAddress(TargetAddress);
1614 if (!TargetFunction || ignoreFunctionRef(*TargetFunction))
1615 continue;
1617 const uint64_t FunctionOffset =
1618 TargetAddress - TargetFunction->getAddress();
1619 BranchTargetSymbol =
1620 FunctionOffset ? TargetFunction->addEntryPointAtOffset(FunctionOffset)
1621 : TargetFunction->getSymbol();
1624 // Can't find more references. Not creating relocations since we are not
1625 // moving code.
1626 if (!BC.HasRelocations)
1627 continue;
1629 if (BranchTargetSymbol) {
1630 BC.MIB->replaceBranchTarget(Instruction, BranchTargetSymbol,
1631 Emitter.LocalCtx.get());
1632 } else if (!llvm::any_of(Instruction,
1633 [](const MCOperand &Op) { return Op.isExpr(); })) {
1634 // Skip assembly if the instruction may not have any symbolic operands.
1635 continue;
1638 // Emit the instruction using temp emitter and generate relocations.
1639 SmallString<256> Code;
1640 SmallVector<MCFixup, 4> Fixups;
1641 Emitter.MCE->encodeInstruction(Instruction, Code, Fixups, *BC.STI);
1643 // Create relocation for every fixup.
1644 for (const MCFixup &Fixup : Fixups) {
1645 std::optional<Relocation> Rel = BC.MIB->createRelocation(Fixup, *BC.MAB);
1646 if (!Rel) {
1647 Success = false;
1648 continue;
1651 if (ignoreReference(Rel->Symbol))
1652 continue;
1654 if (Relocation::getSizeForType(Rel->Type) < 4) {
1655 // If the instruction uses a short form, then we might not be able
1656 // to handle the rewrite without relaxation, and hence cannot reliably
1657 // create an external reference relocation.
1658 Success = false;
1659 continue;
1661 Rel->Offset += getAddress() - getOriginSection()->getAddress() + Offset;
1662 FunctionRelocations.push_back(*Rel);
1665 if (!Success)
1666 break;
1669 // Reset symbolizer for the disassembler.
1670 BC.SymbolicDisAsm->setSymbolizer(nullptr);
1672 // Add relocations unless disassembly failed for this function.
1673 if (!DisassemblyFailed)
1674 for (Relocation &Rel : FunctionRelocations)
1675 getOriginSection()->addPendingRelocation(Rel);
1677 // Inform BinaryContext that this function symbols will not be defined and
1678 // relocations should not be created against them.
1679 if (BC.HasRelocations) {
1680 for (std::pair<const uint32_t, MCSymbol *> &LI : Labels)
1681 BC.UndefinedSymbols.insert(LI.second);
1682 for (MCSymbol *const EndLabel : FunctionEndLabels)
1683 if (EndLabel)
1684 BC.UndefinedSymbols.insert(EndLabel);
1687 clearList(Relocations);
1688 clearList(ExternallyReferencedOffsets);
1690 if (Success && BC.HasRelocations)
1691 HasExternalRefRelocations = true;
1693 if (opts::Verbosity >= 1 && !Success)
1694 BC.outs() << "BOLT-INFO: failed to scan refs for " << *this << '\n';
1696 return Success;
1699 void BinaryFunction::postProcessEntryPoints() {
1700 if (!isSimple())
1701 return;
1703 for (auto &KV : Labels) {
1704 MCSymbol *Label = KV.second;
1705 if (!getSecondaryEntryPointSymbol(Label))
1706 continue;
1708 // In non-relocation mode there's potentially an external undetectable
1709 // reference to the entry point and hence we cannot move this entry
1710 // point. Optimizing without moving could be difficult.
1711 // In BAT mode, register any known entry points for CFG construction.
1712 if (!BC.HasRelocations && !BC.HasBATSection)
1713 setSimple(false);
1715 const uint32_t Offset = KV.first;
1717 // If we are at Offset 0 and there is no instruction associated with it,
1718 // this means this is an empty function. Just ignore. If we find an
1719 // instruction at this offset, this entry point is valid.
1720 if (!Offset || getInstructionAtOffset(Offset))
1721 continue;
1723 // On AArch64 there are legitimate reasons to have references past the
1724 // end of the function, e.g. jump tables.
1725 if (BC.isAArch64() && Offset == getSize())
1726 continue;
1728 BC.errs() << "BOLT-WARNING: reference in the middle of instruction "
1729 "detected in function "
1730 << *this << " at offset 0x" << Twine::utohexstr(Offset) << '\n';
1731 if (BC.HasRelocations)
1732 setIgnored();
1733 setSimple(false);
1734 return;
1738 void BinaryFunction::postProcessJumpTables() {
1739 // Create labels for all entries.
1740 for (auto &JTI : JumpTables) {
1741 JumpTable &JT = *JTI.second;
1742 if (JT.Type == JumpTable::JTT_PIC && opts::JumpTables == JTS_BASIC) {
1743 opts::JumpTables = JTS_MOVE;
1744 BC.outs() << "BOLT-INFO: forcing -jump-tables=move as PIC jump table was "
1745 "detected in function "
1746 << *this << '\n';
1748 const uint64_t BDSize =
1749 BC.getBinaryDataAtAddress(JT.getAddress())->getSize();
1750 if (!BDSize) {
1751 BC.setBinaryDataSize(JT.getAddress(), JT.getSize());
1752 } else {
1753 assert(BDSize >= JT.getSize() &&
1754 "jump table cannot be larger than the containing object");
1756 if (!JT.Entries.empty())
1757 continue;
1759 bool HasOneParent = (JT.Parents.size() == 1);
1760 for (uint64_t EntryAddress : JT.EntriesAsAddress) {
1761 // builtin_unreachable does not belong to any function
1762 // Need to handle separately
1763 bool IsBuiltinUnreachable =
1764 llvm::any_of(JT.Parents, [&](const BinaryFunction *Parent) {
1765 return EntryAddress == Parent->getAddress() + Parent->getSize();
1767 if (IsBuiltinUnreachable) {
1768 MCSymbol *Label = getOrCreateLocalLabel(EntryAddress, true);
1769 JT.Entries.push_back(Label);
1770 continue;
1772 // Create a local label for targets that cannot be reached by other
1773 // fragments. Otherwise, create a secondary entry point in the target
1774 // function.
1775 BinaryFunction *TargetBF =
1776 BC.getBinaryFunctionContainingAddress(EntryAddress);
1777 MCSymbol *Label;
1778 if (HasOneParent && TargetBF == this) {
1779 Label = getOrCreateLocalLabel(EntryAddress, true);
1780 } else {
1781 const uint64_t Offset = EntryAddress - TargetBF->getAddress();
1782 Label = Offset ? TargetBF->addEntryPointAtOffset(Offset)
1783 : TargetBF->getSymbol();
1785 JT.Entries.push_back(Label);
1789 // Add TakenBranches from JumpTables.
1791 // We want to do it after initial processing since we don't know jump tables'
1792 // boundaries until we process them all.
1793 for (auto &JTSite : JTSites) {
1794 const uint64_t JTSiteOffset = JTSite.first;
1795 const uint64_t JTAddress = JTSite.second;
1796 const JumpTable *JT = getJumpTableContainingAddress(JTAddress);
1797 assert(JT && "cannot find jump table for address");
1799 uint64_t EntryOffset = JTAddress - JT->getAddress();
1800 while (EntryOffset < JT->getSize()) {
1801 uint64_t EntryAddress = JT->EntriesAsAddress[EntryOffset / JT->EntrySize];
1802 uint64_t TargetOffset = EntryAddress - getAddress();
1803 if (TargetOffset < getSize()) {
1804 TakenBranches.emplace_back(JTSiteOffset, TargetOffset);
1806 if (opts::StrictMode)
1807 registerReferencedOffset(TargetOffset);
1810 EntryOffset += JT->EntrySize;
1812 // A label at the next entry means the end of this jump table.
1813 if (JT->Labels.count(EntryOffset))
1814 break;
1817 clearList(JTSites);
1819 // Conservatively populate all possible destinations for unknown indirect
1820 // branches.
1821 if (opts::StrictMode && hasInternalReference()) {
1822 for (uint64_t Offset : UnknownIndirectBranchOffsets) {
1823 for (uint64_t PossibleDestination : ExternallyReferencedOffsets) {
1824 // Ignore __builtin_unreachable().
1825 if (PossibleDestination == getSize())
1826 continue;
1827 TakenBranches.emplace_back(Offset, PossibleDestination);
1833 bool BinaryFunction::validateExternallyReferencedOffsets() {
1834 SmallPtrSet<MCSymbol *, 4> JTTargets;
1835 for (const JumpTable *JT : llvm::make_second_range(JumpTables))
1836 JTTargets.insert(JT->Entries.begin(), JT->Entries.end());
1838 bool HasUnclaimedReference = false;
1839 for (uint64_t Destination : ExternallyReferencedOffsets) {
1840 // Ignore __builtin_unreachable().
1841 if (Destination == getSize())
1842 continue;
1843 // Ignore constant islands
1844 if (isInConstantIsland(Destination + getAddress()))
1845 continue;
1847 if (BinaryBasicBlock *BB = getBasicBlockAtOffset(Destination)) {
1848 // Check if the externally referenced offset is a recognized jump table
1849 // target.
1850 if (JTTargets.contains(BB->getLabel()))
1851 continue;
1853 if (opts::Verbosity >= 1) {
1854 BC.errs() << "BOLT-WARNING: unclaimed data to code reference (possibly "
1855 << "an unrecognized jump table entry) to " << BB->getName()
1856 << " in " << *this << "\n";
1858 auto L = BC.scopeLock();
1859 addEntryPoint(*BB);
1860 } else {
1861 BC.errs() << "BOLT-WARNING: unknown data to code reference to offset "
1862 << Twine::utohexstr(Destination) << " in " << *this << "\n";
1863 setIgnored();
1865 HasUnclaimedReference = true;
1867 return !HasUnclaimedReference;
1870 bool BinaryFunction::postProcessIndirectBranches(
1871 MCPlusBuilder::AllocatorIdTy AllocId) {
1872 auto addUnknownControlFlow = [&](BinaryBasicBlock &BB) {
1873 LLVM_DEBUG(dbgs() << "BOLT-DEBUG: adding unknown control flow in " << *this
1874 << " for " << BB.getName() << "\n");
1875 HasUnknownControlFlow = true;
1876 BB.removeAllSuccessors();
1877 for (uint64_t PossibleDestination : ExternallyReferencedOffsets)
1878 if (BinaryBasicBlock *SuccBB = getBasicBlockAtOffset(PossibleDestination))
1879 BB.addSuccessor(SuccBB);
1882 uint64_t NumIndirectJumps = 0;
1883 MCInst *LastIndirectJump = nullptr;
1884 BinaryBasicBlock *LastIndirectJumpBB = nullptr;
1885 uint64_t LastJT = 0;
1886 uint16_t LastJTIndexReg = BC.MIB->getNoRegister();
1887 for (BinaryBasicBlock &BB : blocks()) {
1888 for (BinaryBasicBlock::iterator II = BB.begin(); II != BB.end(); ++II) {
1889 MCInst &Instr = *II;
1890 if (!BC.MIB->isIndirectBranch(Instr))
1891 continue;
1893 // If there's an indirect branch in a single-block function -
1894 // it must be a tail call.
1895 if (BasicBlocks.size() == 1) {
1896 BC.MIB->convertJmpToTailCall(Instr);
1897 return true;
1900 ++NumIndirectJumps;
1902 if (opts::StrictMode && !hasInternalReference()) {
1903 BC.MIB->convertJmpToTailCall(Instr);
1904 break;
1907 // Validate the tail call or jump table assumptions now that we know
1908 // basic block boundaries.
1909 if (BC.MIB->isTailCall(Instr) || BC.MIB->getJumpTable(Instr)) {
1910 const unsigned PtrSize = BC.AsmInfo->getCodePointerSize();
1911 MCInst *MemLocInstr;
1912 unsigned BaseRegNum, IndexRegNum;
1913 int64_t DispValue;
1914 const MCExpr *DispExpr;
1915 MCInst *PCRelBaseInstr;
1916 MCInst *FixedEntryLoadInstr;
1917 IndirectBranchType Type = BC.MIB->analyzeIndirectBranch(
1918 Instr, BB.begin(), II, PtrSize, MemLocInstr, BaseRegNum,
1919 IndexRegNum, DispValue, DispExpr, PCRelBaseInstr,
1920 FixedEntryLoadInstr);
1921 if (Type != IndirectBranchType::UNKNOWN || MemLocInstr != nullptr)
1922 continue;
1924 if (!opts::StrictMode)
1925 return false;
1927 if (BC.MIB->isTailCall(Instr)) {
1928 BC.MIB->convertTailCallToJmp(Instr);
1929 } else {
1930 LastIndirectJump = &Instr;
1931 LastIndirectJumpBB = &BB;
1932 LastJT = BC.MIB->getJumpTable(Instr);
1933 LastJTIndexReg = BC.MIB->getJumpTableIndexReg(Instr);
1934 BC.MIB->unsetJumpTable(Instr);
1936 JumpTable *JT = BC.getJumpTableContainingAddress(LastJT);
1937 if (JT->Type == JumpTable::JTT_NORMAL) {
1938 // Invalidating the jump table may also invalidate other jump table
1939 // boundaries. Until we have/need a support for this, mark the
1940 // function as non-simple.
1941 LLVM_DEBUG(dbgs() << "BOLT-DEBUG: rejected jump table reference"
1942 << JT->getName() << " in " << *this << '\n');
1943 return false;
1947 addUnknownControlFlow(BB);
1948 continue;
1951 // If this block contains an epilogue code and has an indirect branch,
1952 // then most likely it's a tail call. Otherwise, we cannot tell for sure
1953 // what it is and conservatively reject the function's CFG.
1954 bool IsEpilogue = llvm::any_of(BB, [&](const MCInst &Instr) {
1955 return BC.MIB->isLeave(Instr) || BC.MIB->isPop(Instr);
1957 if (IsEpilogue) {
1958 BC.MIB->convertJmpToTailCall(Instr);
1959 BB.removeAllSuccessors();
1960 continue;
1963 if (opts::Verbosity >= 2) {
1964 BC.outs() << "BOLT-INFO: rejected potential indirect tail call in "
1965 << "function " << *this << " in basic block " << BB.getName()
1966 << ".\n";
1967 LLVM_DEBUG(BC.printInstructions(dbgs(), BB.begin(), BB.end(),
1968 BB.getOffset(), this, true));
1971 if (!opts::StrictMode)
1972 return false;
1974 addUnknownControlFlow(BB);
1978 if (HasInternalLabelReference)
1979 return false;
1981 // If there's only one jump table, and one indirect jump, and no other
1982 // references, then we should be able to derive the jump table even if we
1983 // fail to match the pattern.
1984 if (HasUnknownControlFlow && NumIndirectJumps == 1 &&
1985 JumpTables.size() == 1 && LastIndirectJump &&
1986 !BC.getJumpTableContainingAddress(LastJT)->IsSplit) {
1987 LLVM_DEBUG(dbgs() << "BOLT-DEBUG: unsetting unknown control flow in "
1988 << *this << '\n');
1989 BC.MIB->setJumpTable(*LastIndirectJump, LastJT, LastJTIndexReg, AllocId);
1990 HasUnknownControlFlow = false;
1992 LastIndirectJumpBB->updateJumpTableSuccessors();
1995 // Validate that all data references to function offsets are claimed by
1996 // recognized jump tables. Register externally referenced blocks as entry
1997 // points.
1998 if (!opts::StrictMode && hasInternalReference()) {
1999 if (!validateExternallyReferencedOffsets())
2000 return false;
2003 if (HasUnknownControlFlow && !BC.HasRelocations)
2004 return false;
2006 return true;
2009 void BinaryFunction::recomputeLandingPads() {
2010 updateBBIndices(0);
2012 for (BinaryBasicBlock *BB : BasicBlocks) {
2013 BB->LandingPads.clear();
2014 BB->Throwers.clear();
2017 for (BinaryBasicBlock *BB : BasicBlocks) {
2018 std::unordered_set<const BinaryBasicBlock *> BBLandingPads;
2019 for (MCInst &Instr : *BB) {
2020 if (!BC.MIB->isInvoke(Instr))
2021 continue;
2023 const std::optional<MCPlus::MCLandingPad> EHInfo =
2024 BC.MIB->getEHInfo(Instr);
2025 if (!EHInfo || !EHInfo->first)
2026 continue;
2028 BinaryBasicBlock *LPBlock = getBasicBlockForLabel(EHInfo->first);
2029 if (!BBLandingPads.count(LPBlock)) {
2030 BBLandingPads.insert(LPBlock);
2031 BB->LandingPads.emplace_back(LPBlock);
2032 LPBlock->Throwers.emplace_back(BB);
2038 Error BinaryFunction::buildCFG(MCPlusBuilder::AllocatorIdTy AllocatorId) {
2039 auto &MIB = BC.MIB;
2041 if (!isSimple()) {
2042 assert(!BC.HasRelocations &&
2043 "cannot process file with non-simple function in relocs mode");
2044 return createNonFatalBOLTError("");
2047 if (CurrentState != State::Disassembled)
2048 return createNonFatalBOLTError("");
2050 assert(BasicBlocks.empty() && "basic block list should be empty");
2051 assert((Labels.find(getFirstInstructionOffset()) != Labels.end()) &&
2052 "first instruction should always have a label");
2054 // Create basic blocks in the original layout order:
2056 // * Every instruction with associated label marks
2057 // the beginning of a basic block.
2058 // * Conditional instruction marks the end of a basic block,
2059 // except when the following instruction is an
2060 // unconditional branch, and the unconditional branch is not
2061 // a destination of another branch. In the latter case, the
2062 // basic block will consist of a single unconditional branch
2063 // (missed "double-jump" optimization).
2065 // Created basic blocks are sorted in layout order since they are
2066 // created in the same order as instructions, and instructions are
2067 // sorted by offsets.
2068 BinaryBasicBlock *InsertBB = nullptr;
2069 BinaryBasicBlock *PrevBB = nullptr;
2070 bool IsLastInstrNop = false;
2071 // Offset of the last non-nop instruction.
2072 uint64_t LastInstrOffset = 0;
2074 auto addCFIPlaceholders = [this](uint64_t CFIOffset,
2075 BinaryBasicBlock *InsertBB) {
2076 for (auto FI = OffsetToCFI.lower_bound(CFIOffset),
2077 FE = OffsetToCFI.upper_bound(CFIOffset);
2078 FI != FE; ++FI) {
2079 addCFIPseudo(InsertBB, InsertBB->end(), FI->second);
2083 // For profiling purposes we need to save the offset of the last instruction
2084 // in the basic block.
2085 // NOTE: nops always have an Offset annotation. Annotate the last non-nop as
2086 // older profiles ignored nops.
2087 auto updateOffset = [&](uint64_t Offset) {
2088 assert(PrevBB && PrevBB != InsertBB && "invalid previous block");
2089 MCInst *LastNonNop = nullptr;
2090 for (BinaryBasicBlock::reverse_iterator RII = PrevBB->getLastNonPseudo(),
2091 E = PrevBB->rend();
2092 RII != E; ++RII) {
2093 if (!BC.MIB->isPseudo(*RII) && !BC.MIB->isNoop(*RII)) {
2094 LastNonNop = &*RII;
2095 break;
2098 if (LastNonNop && !MIB->getOffset(*LastNonNop))
2099 MIB->setOffset(*LastNonNop, static_cast<uint32_t>(Offset));
2102 for (auto I = Instructions.begin(), E = Instructions.end(); I != E; ++I) {
2103 const uint32_t Offset = I->first;
2104 MCInst &Instr = I->second;
2106 auto LI = Labels.find(Offset);
2107 if (LI != Labels.end()) {
2108 // Always create new BB at branch destination.
2109 PrevBB = InsertBB ? InsertBB : PrevBB;
2110 InsertBB = addBasicBlockAt(LI->first, LI->second);
2111 if (opts::PreserveBlocksAlignment && IsLastInstrNop)
2112 InsertBB->setDerivedAlignment();
2114 if (PrevBB)
2115 updateOffset(LastInstrOffset);
2118 // Mark all nops with Offset for profile tracking purposes.
2119 if (MIB->isNoop(Instr) && !MIB->getOffset(Instr)) {
2120 // If "Offset" annotation is not present, set it and mark the nop for
2121 // deletion.
2122 MIB->setOffset(Instr, static_cast<uint32_t>(Offset));
2123 // Annotate ordinary nops, so we can safely delete them if required.
2124 MIB->addAnnotation(Instr, "NOP", static_cast<uint32_t>(1), AllocatorId);
2127 if (!InsertBB) {
2128 // It must be a fallthrough or unreachable code. Create a new block unless
2129 // we see an unconditional branch following a conditional one. The latter
2130 // should not be a conditional tail call.
2131 assert(PrevBB && "no previous basic block for a fall through");
2132 MCInst *PrevInstr = PrevBB->getLastNonPseudoInstr();
2133 assert(PrevInstr && "no previous instruction for a fall through");
2134 if (MIB->isUnconditionalBranch(Instr) &&
2135 !MIB->isIndirectBranch(*PrevInstr) &&
2136 !MIB->isUnconditionalBranch(*PrevInstr) &&
2137 !MIB->getConditionalTailCall(*PrevInstr) &&
2138 !MIB->isReturn(*PrevInstr)) {
2139 // Temporarily restore inserter basic block.
2140 InsertBB = PrevBB;
2141 } else {
2142 MCSymbol *Label;
2144 auto L = BC.scopeLock();
2145 Label = BC.Ctx->createNamedTempSymbol("FT");
2147 InsertBB = addBasicBlockAt(Offset, Label);
2148 if (opts::PreserveBlocksAlignment && IsLastInstrNop)
2149 InsertBB->setDerivedAlignment();
2150 updateOffset(LastInstrOffset);
2153 if (Offset == getFirstInstructionOffset()) {
2154 // Add associated CFI pseudos in the first offset
2155 addCFIPlaceholders(Offset, InsertBB);
2158 const bool IsBlockEnd = MIB->isTerminator(Instr);
2159 IsLastInstrNop = MIB->isNoop(Instr);
2160 if (!IsLastInstrNop)
2161 LastInstrOffset = Offset;
2162 InsertBB->addInstruction(std::move(Instr));
2164 // Add associated CFI instrs. We always add the CFI instruction that is
2165 // located immediately after this instruction, since the next CFI
2166 // instruction reflects the change in state caused by this instruction.
2167 auto NextInstr = std::next(I);
2168 uint64_t CFIOffset;
2169 if (NextInstr != E)
2170 CFIOffset = NextInstr->first;
2171 else
2172 CFIOffset = getSize();
2174 // Note: this potentially invalidates instruction pointers/iterators.
2175 addCFIPlaceholders(CFIOffset, InsertBB);
2177 if (IsBlockEnd) {
2178 PrevBB = InsertBB;
2179 InsertBB = nullptr;
2183 if (BasicBlocks.empty()) {
2184 setSimple(false);
2185 return createNonFatalBOLTError("");
2188 // Intermediate dump.
2189 LLVM_DEBUG(print(dbgs(), "after creating basic blocks"));
2191 // TODO: handle properly calls to no-return functions,
2192 // e.g. exit(3), etc. Otherwise we'll see a false fall-through
2193 // blocks.
2195 // Remove duplicates branches. We can get a bunch of them from jump tables.
2196 // Without doing jump table value profiling we don't have a use for extra
2197 // (duplicate) branches.
2198 llvm::sort(TakenBranches);
2199 auto NewEnd = std::unique(TakenBranches.begin(), TakenBranches.end());
2200 TakenBranches.erase(NewEnd, TakenBranches.end());
2202 for (std::pair<uint32_t, uint32_t> &Branch : TakenBranches) {
2203 LLVM_DEBUG(dbgs() << "registering branch [0x"
2204 << Twine::utohexstr(Branch.first) << "] -> [0x"
2205 << Twine::utohexstr(Branch.second) << "]\n");
2206 BinaryBasicBlock *FromBB = getBasicBlockContainingOffset(Branch.first);
2207 BinaryBasicBlock *ToBB = getBasicBlockAtOffset(Branch.second);
2208 if (!FromBB || !ToBB) {
2209 if (!FromBB)
2210 BC.errs() << "BOLT-ERROR: cannot find BB containing the branch.\n";
2211 if (!ToBB)
2212 BC.errs()
2213 << "BOLT-ERROR: cannot find BB containing branch destination.\n";
2214 return createFatalBOLTError(BC.generateBugReportMessage(
2215 "disassembly failed - inconsistent branch found.", *this));
2218 FromBB->addSuccessor(ToBB);
2221 // Add fall-through branches.
2222 PrevBB = nullptr;
2223 bool IsPrevFT = false; // Is previous block a fall-through.
2224 for (BinaryBasicBlock *BB : BasicBlocks) {
2225 if (IsPrevFT)
2226 PrevBB->addSuccessor(BB);
2228 if (BB->empty()) {
2229 IsPrevFT = true;
2230 PrevBB = BB;
2231 continue;
2234 MCInst *LastInstr = BB->getLastNonPseudoInstr();
2235 assert(LastInstr &&
2236 "should have non-pseudo instruction in non-empty block");
2238 if (BB->succ_size() == 0) {
2239 // Since there's no existing successors, we know the last instruction is
2240 // not a conditional branch. Thus if it's a terminator, it shouldn't be a
2241 // fall-through.
2243 // Conditional tail call is a special case since we don't add a taken
2244 // branch successor for it.
2245 IsPrevFT = !MIB->isTerminator(*LastInstr) ||
2246 MIB->getConditionalTailCall(*LastInstr);
2247 } else if (BB->succ_size() == 1) {
2248 IsPrevFT = MIB->isConditionalBranch(*LastInstr);
2249 } else {
2250 IsPrevFT = false;
2253 PrevBB = BB;
2256 // Assign landing pads and throwers info.
2257 recomputeLandingPads();
2259 // Assign CFI information to each BB entry.
2260 annotateCFIState();
2262 // Annotate invoke instructions with GNU_args_size data.
2263 propagateGnuArgsSizeInfo(AllocatorId);
2265 // Set the basic block layout to the original order and set end offsets.
2266 PrevBB = nullptr;
2267 for (BinaryBasicBlock *BB : BasicBlocks) {
2268 Layout.addBasicBlock(BB);
2269 if (PrevBB)
2270 PrevBB->setEndOffset(BB->getOffset());
2271 PrevBB = BB;
2273 PrevBB->setEndOffset(getSize());
2275 Layout.updateLayoutIndices();
2277 normalizeCFIState();
2279 // Clean-up memory taken by intermediate structures.
2281 // NB: don't clear Labels list as we may need them if we mark the function
2282 // as non-simple later in the process of discovering extra entry points.
2283 clearList(Instructions);
2284 clearList(OffsetToCFI);
2285 clearList(TakenBranches);
2287 // Update the state.
2288 CurrentState = State::CFG;
2290 // Make any necessary adjustments for indirect branches.
2291 if (!postProcessIndirectBranches(AllocatorId)) {
2292 if (opts::Verbosity) {
2293 BC.errs() << "BOLT-WARNING: failed to post-process indirect branches for "
2294 << *this << '\n';
2296 // In relocation mode we want to keep processing the function but avoid
2297 // optimizing it.
2298 setSimple(false);
2301 clearList(ExternallyReferencedOffsets);
2302 clearList(UnknownIndirectBranchOffsets);
2304 return Error::success();
2307 void BinaryFunction::postProcessCFG() {
2308 if (isSimple() && !BasicBlocks.empty()) {
2309 // Convert conditional tail call branches to conditional branches that jump
2310 // to a tail call.
2311 removeConditionalTailCalls();
2313 postProcessProfile();
2315 // Eliminate inconsistencies between branch instructions and CFG.
2316 postProcessBranches();
2319 // The final cleanup of intermediate structures.
2320 clearList(IgnoredBranches);
2322 // Remove "Offset" annotations, unless we need an address-translation table
2323 // later. This has no cost, since annotations are allocated by a bumpptr
2324 // allocator and won't be released anyway until late in the pipeline.
2325 if (!requiresAddressTranslation() && !opts::Instrument) {
2326 for (BinaryBasicBlock &BB : blocks())
2327 for (MCInst &Inst : BB)
2328 BC.MIB->clearOffset(Inst);
2331 assert((!isSimple() || validateCFG()) &&
2332 "invalid CFG detected after post-processing");
2335 void BinaryFunction::removeTagsFromProfile() {
2336 for (BinaryBasicBlock *BB : BasicBlocks) {
2337 if (BB->ExecutionCount == BinaryBasicBlock::COUNT_NO_PROFILE)
2338 BB->ExecutionCount = 0;
2339 for (BinaryBasicBlock::BinaryBranchInfo &BI : BB->branch_info()) {
2340 if (BI.Count != BinaryBasicBlock::COUNT_NO_PROFILE &&
2341 BI.MispredictedCount != BinaryBasicBlock::COUNT_NO_PROFILE)
2342 continue;
2343 BI.Count = 0;
2344 BI.MispredictedCount = 0;
2349 void BinaryFunction::removeConditionalTailCalls() {
2350 // Blocks to be appended at the end.
2351 std::vector<std::unique_ptr<BinaryBasicBlock>> NewBlocks;
2353 for (auto BBI = begin(); BBI != end(); ++BBI) {
2354 BinaryBasicBlock &BB = *BBI;
2355 MCInst *CTCInstr = BB.getLastNonPseudoInstr();
2356 if (!CTCInstr)
2357 continue;
2359 std::optional<uint64_t> TargetAddressOrNone =
2360 BC.MIB->getConditionalTailCall(*CTCInstr);
2361 if (!TargetAddressOrNone)
2362 continue;
2364 // Gather all necessary information about CTC instruction before
2365 // annotations are destroyed.
2366 const int32_t CFIStateBeforeCTC = BB.getCFIStateAtInstr(CTCInstr);
2367 uint64_t CTCTakenCount = BinaryBasicBlock::COUNT_NO_PROFILE;
2368 uint64_t CTCMispredCount = BinaryBasicBlock::COUNT_NO_PROFILE;
2369 if (hasValidProfile()) {
2370 CTCTakenCount = BC.MIB->getAnnotationWithDefault<uint64_t>(
2371 *CTCInstr, "CTCTakenCount");
2372 CTCMispredCount = BC.MIB->getAnnotationWithDefault<uint64_t>(
2373 *CTCInstr, "CTCMispredCount");
2376 // Assert that the tail call does not throw.
2377 assert(!BC.MIB->getEHInfo(*CTCInstr) &&
2378 "found tail call with associated landing pad");
2380 // Create a basic block with an unconditional tail call instruction using
2381 // the same destination.
2382 const MCSymbol *CTCTargetLabel = BC.MIB->getTargetSymbol(*CTCInstr);
2383 assert(CTCTargetLabel && "symbol expected for conditional tail call");
2384 MCInst TailCallInstr;
2385 BC.MIB->createTailCall(TailCallInstr, CTCTargetLabel, BC.Ctx.get());
2387 // Move offset from CTCInstr to TailCallInstr.
2388 if (const std::optional<uint32_t> Offset = BC.MIB->getOffset(*CTCInstr)) {
2389 BC.MIB->setOffset(TailCallInstr, *Offset);
2390 BC.MIB->clearOffset(*CTCInstr);
2393 // Link new BBs to the original input offset of the BB where the CTC
2394 // is, so we can map samples recorded in new BBs back to the original BB
2395 // seem in the input binary (if using BAT)
2396 std::unique_ptr<BinaryBasicBlock> TailCallBB =
2397 createBasicBlock(BC.Ctx->createNamedTempSymbol("TC"));
2398 TailCallBB->setOffset(BB.getInputOffset());
2399 TailCallBB->addInstruction(TailCallInstr);
2400 TailCallBB->setCFIState(CFIStateBeforeCTC);
2402 // Add CFG edge with profile info from BB to TailCallBB.
2403 BB.addSuccessor(TailCallBB.get(), CTCTakenCount, CTCMispredCount);
2405 // Add execution count for the block.
2406 TailCallBB->setExecutionCount(CTCTakenCount);
2408 BC.MIB->convertTailCallToJmp(*CTCInstr);
2410 BC.MIB->replaceBranchTarget(*CTCInstr, TailCallBB->getLabel(),
2411 BC.Ctx.get());
2413 // Add basic block to the list that will be added to the end.
2414 NewBlocks.emplace_back(std::move(TailCallBB));
2416 // Swap edges as the TailCallBB corresponds to the taken branch.
2417 BB.swapConditionalSuccessors();
2419 // This branch is no longer a conditional tail call.
2420 BC.MIB->unsetConditionalTailCall(*CTCInstr);
2423 insertBasicBlocks(std::prev(end()), std::move(NewBlocks),
2424 /* UpdateLayout */ true,
2425 /* UpdateCFIState */ false);
2428 uint64_t BinaryFunction::getFunctionScore() const {
2429 if (FunctionScore != -1)
2430 return FunctionScore;
2432 if (!isSimple() || !hasValidProfile()) {
2433 FunctionScore = 0;
2434 return FunctionScore;
2437 uint64_t TotalScore = 0ULL;
2438 for (const BinaryBasicBlock &BB : blocks()) {
2439 uint64_t BBExecCount = BB.getExecutionCount();
2440 if (BBExecCount == BinaryBasicBlock::COUNT_NO_PROFILE)
2441 continue;
2442 TotalScore += BBExecCount * BB.getNumNonPseudos();
2444 FunctionScore = TotalScore;
2445 return FunctionScore;
2448 void BinaryFunction::annotateCFIState() {
2449 assert(CurrentState == State::Disassembled && "unexpected function state");
2450 assert(!BasicBlocks.empty() && "basic block list should not be empty");
2452 // This is an index of the last processed CFI in FDE CFI program.
2453 uint32_t State = 0;
2455 // This is an index of RememberState CFI reflecting effective state right
2456 // after execution of RestoreState CFI.
2458 // It differs from State iff the CFI at (State-1)
2459 // was RestoreState (modulo GNU_args_size CFIs, which are ignored).
2461 // This allows us to generate shorter replay sequences when producing new
2462 // CFI programs.
2463 uint32_t EffectiveState = 0;
2465 // For tracking RememberState/RestoreState sequences.
2466 std::stack<uint32_t> StateStack;
2468 for (BinaryBasicBlock *BB : BasicBlocks) {
2469 BB->setCFIState(EffectiveState);
2471 for (const MCInst &Instr : *BB) {
2472 const MCCFIInstruction *CFI = getCFIFor(Instr);
2473 if (!CFI)
2474 continue;
2476 ++State;
2478 switch (CFI->getOperation()) {
2479 case MCCFIInstruction::OpRememberState:
2480 StateStack.push(EffectiveState);
2481 EffectiveState = State;
2482 break;
2483 case MCCFIInstruction::OpRestoreState:
2484 assert(!StateStack.empty() && "corrupt CFI stack");
2485 EffectiveState = StateStack.top();
2486 StateStack.pop();
2487 break;
2488 case MCCFIInstruction::OpGnuArgsSize:
2489 // OpGnuArgsSize CFIs do not affect the CFI state.
2490 break;
2491 default:
2492 // Any other CFI updates the state.
2493 EffectiveState = State;
2494 break;
2499 if (opts::Verbosity >= 1 && !StateStack.empty()) {
2500 BC.errs() << "BOLT-WARNING: non-empty CFI stack at the end of " << *this
2501 << '\n';
2505 namespace {
2507 /// Our full interpretation of a DWARF CFI machine state at a given point
2508 struct CFISnapshot {
2509 /// CFA register number and offset defining the canonical frame at this
2510 /// point, or the number of a rule (CFI state) that computes it with a
2511 /// DWARF expression. This number will be negative if it refers to a CFI
2512 /// located in the CIE instead of the FDE.
2513 uint32_t CFAReg;
2514 int32_t CFAOffset;
2515 int32_t CFARule;
2516 /// Mapping of rules (CFI states) that define the location of each
2517 /// register. If absent, no rule defining the location of such register
2518 /// was ever read. This number will be negative if it refers to a CFI
2519 /// located in the CIE instead of the FDE.
2520 DenseMap<int32_t, int32_t> RegRule;
2522 /// References to CIE, FDE and expanded instructions after a restore state
2523 const BinaryFunction::CFIInstrMapType &CIE;
2524 const BinaryFunction::CFIInstrMapType &FDE;
2525 const DenseMap<int32_t, SmallVector<int32_t, 4>> &FrameRestoreEquivalents;
2527 /// Current FDE CFI number representing the state where the snapshot is at
2528 int32_t CurState;
2530 /// Used when we don't have information about which state/rule to apply
2531 /// to recover the location of either the CFA or a specific register
2532 constexpr static int32_t UNKNOWN = std::numeric_limits<int32_t>::min();
2534 private:
2535 /// Update our snapshot by executing a single CFI
2536 void update(const MCCFIInstruction &Instr, int32_t RuleNumber) {
2537 switch (Instr.getOperation()) {
2538 case MCCFIInstruction::OpSameValue:
2539 case MCCFIInstruction::OpRelOffset:
2540 case MCCFIInstruction::OpOffset:
2541 case MCCFIInstruction::OpRestore:
2542 case MCCFIInstruction::OpUndefined:
2543 case MCCFIInstruction::OpRegister:
2544 RegRule[Instr.getRegister()] = RuleNumber;
2545 break;
2546 case MCCFIInstruction::OpDefCfaRegister:
2547 CFAReg = Instr.getRegister();
2548 CFARule = UNKNOWN;
2550 // This shouldn't happen according to the spec but GNU binutils on RISC-V
2551 // emits a DW_CFA_def_cfa_register in CIE's which leaves the offset
2552 // unspecified. Both readelf and llvm-dwarfdump interpret the offset as 0
2553 // in this case so let's do the same.
2554 if (CFAOffset == UNKNOWN)
2555 CFAOffset = 0;
2556 break;
2557 case MCCFIInstruction::OpDefCfaOffset:
2558 CFAOffset = Instr.getOffset();
2559 CFARule = UNKNOWN;
2560 break;
2561 case MCCFIInstruction::OpDefCfa:
2562 CFAReg = Instr.getRegister();
2563 CFAOffset = Instr.getOffset();
2564 CFARule = UNKNOWN;
2565 break;
2566 case MCCFIInstruction::OpEscape: {
2567 std::optional<uint8_t> Reg =
2568 readDWARFExpressionTargetReg(Instr.getValues());
2569 // Handle DW_CFA_def_cfa_expression
2570 if (!Reg) {
2571 CFARule = RuleNumber;
2572 break;
2574 RegRule[*Reg] = RuleNumber;
2575 break;
2577 case MCCFIInstruction::OpAdjustCfaOffset:
2578 case MCCFIInstruction::OpWindowSave:
2579 case MCCFIInstruction::OpNegateRAState:
2580 case MCCFIInstruction::OpNegateRAStateWithPC:
2581 case MCCFIInstruction::OpLLVMDefAspaceCfa:
2582 case MCCFIInstruction::OpLabel:
2583 case MCCFIInstruction::OpValOffset:
2584 llvm_unreachable("unsupported CFI opcode");
2585 break;
2586 case MCCFIInstruction::OpRememberState:
2587 case MCCFIInstruction::OpRestoreState:
2588 case MCCFIInstruction::OpGnuArgsSize:
2589 // do not affect CFI state
2590 break;
2594 public:
2595 /// Advance state reading FDE CFI instructions up to State number
2596 void advanceTo(int32_t State) {
2597 for (int32_t I = CurState, E = State; I != E; ++I) {
2598 const MCCFIInstruction &Instr = FDE[I];
2599 if (Instr.getOperation() != MCCFIInstruction::OpRestoreState) {
2600 update(Instr, I);
2601 continue;
2603 // If restore state instruction, fetch the equivalent CFIs that have
2604 // the same effect of this restore. This is used to ensure remember-
2605 // restore pairs are completely removed.
2606 auto Iter = FrameRestoreEquivalents.find(I);
2607 if (Iter == FrameRestoreEquivalents.end())
2608 continue;
2609 for (int32_t RuleNumber : Iter->second)
2610 update(FDE[RuleNumber], RuleNumber);
2613 assert(((CFAReg != (uint32_t)UNKNOWN && CFAOffset != UNKNOWN) ||
2614 CFARule != UNKNOWN) &&
2615 "CIE did not define default CFA?");
2617 CurState = State;
2620 /// Interpret all CIE and FDE instructions up until CFI State number and
2621 /// populate this snapshot
2622 CFISnapshot(
2623 const BinaryFunction::CFIInstrMapType &CIE,
2624 const BinaryFunction::CFIInstrMapType &FDE,
2625 const DenseMap<int32_t, SmallVector<int32_t, 4>> &FrameRestoreEquivalents,
2626 int32_t State)
2627 : CIE(CIE), FDE(FDE), FrameRestoreEquivalents(FrameRestoreEquivalents) {
2628 CFAReg = UNKNOWN;
2629 CFAOffset = UNKNOWN;
2630 CFARule = UNKNOWN;
2631 CurState = 0;
2633 for (int32_t I = 0, E = CIE.size(); I != E; ++I) {
2634 const MCCFIInstruction &Instr = CIE[I];
2635 update(Instr, -I);
2638 advanceTo(State);
2642 /// A CFI snapshot with the capability of checking if incremental additions to
2643 /// it are redundant. This is used to ensure we do not emit two CFI instructions
2644 /// back-to-back that are doing the same state change, or to avoid emitting a
2645 /// CFI at all when the state at that point would not be modified after that CFI
2646 struct CFISnapshotDiff : public CFISnapshot {
2647 bool RestoredCFAReg{false};
2648 bool RestoredCFAOffset{false};
2649 DenseMap<int32_t, bool> RestoredRegs;
2651 CFISnapshotDiff(const CFISnapshot &S) : CFISnapshot(S) {}
2653 CFISnapshotDiff(
2654 const BinaryFunction::CFIInstrMapType &CIE,
2655 const BinaryFunction::CFIInstrMapType &FDE,
2656 const DenseMap<int32_t, SmallVector<int32_t, 4>> &FrameRestoreEquivalents,
2657 int32_t State)
2658 : CFISnapshot(CIE, FDE, FrameRestoreEquivalents, State) {}
2660 /// Return true if applying Instr to this state is redundant and can be
2661 /// dismissed.
2662 bool isRedundant(const MCCFIInstruction &Instr) {
2663 switch (Instr.getOperation()) {
2664 case MCCFIInstruction::OpSameValue:
2665 case MCCFIInstruction::OpRelOffset:
2666 case MCCFIInstruction::OpOffset:
2667 case MCCFIInstruction::OpRestore:
2668 case MCCFIInstruction::OpUndefined:
2669 case MCCFIInstruction::OpRegister:
2670 case MCCFIInstruction::OpEscape: {
2671 uint32_t Reg;
2672 if (Instr.getOperation() != MCCFIInstruction::OpEscape) {
2673 Reg = Instr.getRegister();
2674 } else {
2675 std::optional<uint8_t> R =
2676 readDWARFExpressionTargetReg(Instr.getValues());
2677 // Handle DW_CFA_def_cfa_expression
2678 if (!R) {
2679 if (RestoredCFAReg && RestoredCFAOffset)
2680 return true;
2681 RestoredCFAReg = true;
2682 RestoredCFAOffset = true;
2683 return false;
2685 Reg = *R;
2687 if (RestoredRegs[Reg])
2688 return true;
2689 RestoredRegs[Reg] = true;
2690 const int32_t CurRegRule = RegRule.contains(Reg) ? RegRule[Reg] : UNKNOWN;
2691 if (CurRegRule == UNKNOWN) {
2692 if (Instr.getOperation() == MCCFIInstruction::OpRestore ||
2693 Instr.getOperation() == MCCFIInstruction::OpSameValue)
2694 return true;
2695 return false;
2697 const MCCFIInstruction &LastDef =
2698 CurRegRule < 0 ? CIE[-CurRegRule] : FDE[CurRegRule];
2699 return LastDef == Instr;
2701 case MCCFIInstruction::OpDefCfaRegister:
2702 if (RestoredCFAReg)
2703 return true;
2704 RestoredCFAReg = true;
2705 return CFAReg == Instr.getRegister();
2706 case MCCFIInstruction::OpDefCfaOffset:
2707 if (RestoredCFAOffset)
2708 return true;
2709 RestoredCFAOffset = true;
2710 return CFAOffset == Instr.getOffset();
2711 case MCCFIInstruction::OpDefCfa:
2712 if (RestoredCFAReg && RestoredCFAOffset)
2713 return true;
2714 RestoredCFAReg = true;
2715 RestoredCFAOffset = true;
2716 return CFAReg == Instr.getRegister() && CFAOffset == Instr.getOffset();
2717 case MCCFIInstruction::OpAdjustCfaOffset:
2718 case MCCFIInstruction::OpWindowSave:
2719 case MCCFIInstruction::OpNegateRAState:
2720 case MCCFIInstruction::OpNegateRAStateWithPC:
2721 case MCCFIInstruction::OpLLVMDefAspaceCfa:
2722 case MCCFIInstruction::OpLabel:
2723 case MCCFIInstruction::OpValOffset:
2724 llvm_unreachable("unsupported CFI opcode");
2725 return false;
2726 case MCCFIInstruction::OpRememberState:
2727 case MCCFIInstruction::OpRestoreState:
2728 case MCCFIInstruction::OpGnuArgsSize:
2729 // do not affect CFI state
2730 return true;
2732 return false;
2736 } // end anonymous namespace
2738 bool BinaryFunction::replayCFIInstrs(int32_t FromState, int32_t ToState,
2739 BinaryBasicBlock *InBB,
2740 BinaryBasicBlock::iterator InsertIt) {
2741 if (FromState == ToState)
2742 return true;
2743 assert(FromState < ToState && "can only replay CFIs forward");
2745 CFISnapshotDiff CFIDiff(CIEFrameInstructions, FrameInstructions,
2746 FrameRestoreEquivalents, FromState);
2748 std::vector<uint32_t> NewCFIs;
2749 for (int32_t CurState = FromState; CurState < ToState; ++CurState) {
2750 MCCFIInstruction *Instr = &FrameInstructions[CurState];
2751 if (Instr->getOperation() == MCCFIInstruction::OpRestoreState) {
2752 auto Iter = FrameRestoreEquivalents.find(CurState);
2753 assert(Iter != FrameRestoreEquivalents.end());
2754 NewCFIs.insert(NewCFIs.end(), Iter->second.begin(), Iter->second.end());
2755 // RestoreState / Remember will be filtered out later by CFISnapshotDiff,
2756 // so we might as well fall-through here.
2758 NewCFIs.push_back(CurState);
2761 // Replay instructions while avoiding duplicates
2762 for (int32_t State : llvm::reverse(NewCFIs)) {
2763 if (CFIDiff.isRedundant(FrameInstructions[State]))
2764 continue;
2765 InsertIt = addCFIPseudo(InBB, InsertIt, State);
2768 return true;
2771 SmallVector<int32_t, 4>
2772 BinaryFunction::unwindCFIState(int32_t FromState, int32_t ToState,
2773 BinaryBasicBlock *InBB,
2774 BinaryBasicBlock::iterator &InsertIt) {
2775 SmallVector<int32_t, 4> NewStates;
2777 CFISnapshot ToCFITable(CIEFrameInstructions, FrameInstructions,
2778 FrameRestoreEquivalents, ToState);
2779 CFISnapshotDiff FromCFITable(ToCFITable);
2780 FromCFITable.advanceTo(FromState);
2782 auto undoStateDefCfa = [&]() {
2783 if (ToCFITable.CFARule == CFISnapshot::UNKNOWN) {
2784 FrameInstructions.emplace_back(MCCFIInstruction::cfiDefCfa(
2785 nullptr, ToCFITable.CFAReg, ToCFITable.CFAOffset));
2786 if (FromCFITable.isRedundant(FrameInstructions.back())) {
2787 FrameInstructions.pop_back();
2788 return;
2790 NewStates.push_back(FrameInstructions.size() - 1);
2791 InsertIt = addCFIPseudo(InBB, InsertIt, FrameInstructions.size() - 1);
2792 ++InsertIt;
2793 } else if (ToCFITable.CFARule < 0) {
2794 if (FromCFITable.isRedundant(CIEFrameInstructions[-ToCFITable.CFARule]))
2795 return;
2796 NewStates.push_back(FrameInstructions.size());
2797 InsertIt = addCFIPseudo(InBB, InsertIt, FrameInstructions.size());
2798 ++InsertIt;
2799 FrameInstructions.emplace_back(CIEFrameInstructions[-ToCFITable.CFARule]);
2800 } else if (!FromCFITable.isRedundant(
2801 FrameInstructions[ToCFITable.CFARule])) {
2802 NewStates.push_back(ToCFITable.CFARule);
2803 InsertIt = addCFIPseudo(InBB, InsertIt, ToCFITable.CFARule);
2804 ++InsertIt;
2808 auto undoState = [&](const MCCFIInstruction &Instr) {
2809 switch (Instr.getOperation()) {
2810 case MCCFIInstruction::OpRememberState:
2811 case MCCFIInstruction::OpRestoreState:
2812 break;
2813 case MCCFIInstruction::OpSameValue:
2814 case MCCFIInstruction::OpRelOffset:
2815 case MCCFIInstruction::OpOffset:
2816 case MCCFIInstruction::OpRestore:
2817 case MCCFIInstruction::OpUndefined:
2818 case MCCFIInstruction::OpEscape:
2819 case MCCFIInstruction::OpRegister: {
2820 uint32_t Reg;
2821 if (Instr.getOperation() != MCCFIInstruction::OpEscape) {
2822 Reg = Instr.getRegister();
2823 } else {
2824 std::optional<uint8_t> R =
2825 readDWARFExpressionTargetReg(Instr.getValues());
2826 // Handle DW_CFA_def_cfa_expression
2827 if (!R) {
2828 undoStateDefCfa();
2829 return;
2831 Reg = *R;
2834 if (!ToCFITable.RegRule.contains(Reg)) {
2835 FrameInstructions.emplace_back(
2836 MCCFIInstruction::createRestore(nullptr, Reg));
2837 if (FromCFITable.isRedundant(FrameInstructions.back())) {
2838 FrameInstructions.pop_back();
2839 break;
2841 NewStates.push_back(FrameInstructions.size() - 1);
2842 InsertIt = addCFIPseudo(InBB, InsertIt, FrameInstructions.size() - 1);
2843 ++InsertIt;
2844 break;
2846 const int32_t Rule = ToCFITable.RegRule[Reg];
2847 if (Rule < 0) {
2848 if (FromCFITable.isRedundant(CIEFrameInstructions[-Rule]))
2849 break;
2850 NewStates.push_back(FrameInstructions.size());
2851 InsertIt = addCFIPseudo(InBB, InsertIt, FrameInstructions.size());
2852 ++InsertIt;
2853 FrameInstructions.emplace_back(CIEFrameInstructions[-Rule]);
2854 break;
2856 if (FromCFITable.isRedundant(FrameInstructions[Rule]))
2857 break;
2858 NewStates.push_back(Rule);
2859 InsertIt = addCFIPseudo(InBB, InsertIt, Rule);
2860 ++InsertIt;
2861 break;
2863 case MCCFIInstruction::OpDefCfaRegister:
2864 case MCCFIInstruction::OpDefCfaOffset:
2865 case MCCFIInstruction::OpDefCfa:
2866 undoStateDefCfa();
2867 break;
2868 case MCCFIInstruction::OpAdjustCfaOffset:
2869 case MCCFIInstruction::OpWindowSave:
2870 case MCCFIInstruction::OpNegateRAState:
2871 case MCCFIInstruction::OpNegateRAStateWithPC:
2872 case MCCFIInstruction::OpLLVMDefAspaceCfa:
2873 case MCCFIInstruction::OpLabel:
2874 case MCCFIInstruction::OpValOffset:
2875 llvm_unreachable("unsupported CFI opcode");
2876 break;
2877 case MCCFIInstruction::OpGnuArgsSize:
2878 // do not affect CFI state
2879 break;
2883 // Undo all modifications from ToState to FromState
2884 for (int32_t I = ToState, E = FromState; I != E; ++I) {
2885 const MCCFIInstruction &Instr = FrameInstructions[I];
2886 if (Instr.getOperation() != MCCFIInstruction::OpRestoreState) {
2887 undoState(Instr);
2888 continue;
2890 auto Iter = FrameRestoreEquivalents.find(I);
2891 if (Iter == FrameRestoreEquivalents.end())
2892 continue;
2893 for (int32_t State : Iter->second)
2894 undoState(FrameInstructions[State]);
2897 return NewStates;
2900 void BinaryFunction::normalizeCFIState() {
2901 // Reordering blocks with remember-restore state instructions can be specially
2902 // tricky. When rewriting the CFI, we omit remember-restore state instructions
2903 // entirely. For restore state, we build a map expanding each restore to the
2904 // equivalent unwindCFIState sequence required at that point to achieve the
2905 // same effect of the restore. All remember state are then just ignored.
2906 std::stack<int32_t> Stack;
2907 for (BinaryBasicBlock *CurBB : Layout.blocks()) {
2908 for (auto II = CurBB->begin(); II != CurBB->end(); ++II) {
2909 if (const MCCFIInstruction *CFI = getCFIFor(*II)) {
2910 if (CFI->getOperation() == MCCFIInstruction::OpRememberState) {
2911 Stack.push(II->getOperand(0).getImm());
2912 continue;
2914 if (CFI->getOperation() == MCCFIInstruction::OpRestoreState) {
2915 const int32_t RememberState = Stack.top();
2916 const int32_t CurState = II->getOperand(0).getImm();
2917 FrameRestoreEquivalents[CurState] =
2918 unwindCFIState(CurState, RememberState, CurBB, II);
2919 Stack.pop();
2926 bool BinaryFunction::finalizeCFIState() {
2927 LLVM_DEBUG(
2928 dbgs() << "Trying to fix CFI states for each BB after reordering.\n");
2929 LLVM_DEBUG(dbgs() << "This is the list of CFI states for each BB of " << *this
2930 << ": ");
2932 const char *Sep = "";
2933 (void)Sep;
2934 for (FunctionFragment &FF : Layout.fragments()) {
2935 // Hot-cold border: at start of each region (with a different FDE) we need
2936 // to reset the CFI state.
2937 int32_t State = 0;
2939 for (BinaryBasicBlock *BB : FF) {
2940 const int32_t CFIStateAtExit = BB->getCFIStateAtExit();
2942 // We need to recover the correct state if it doesn't match expected
2943 // state at BB entry point.
2944 if (BB->getCFIState() < State) {
2945 // In this case, State is currently higher than what this BB expect it
2946 // to be. To solve this, we need to insert CFI instructions to undo
2947 // the effect of all CFI from BB's state to current State.
2948 auto InsertIt = BB->begin();
2949 unwindCFIState(State, BB->getCFIState(), BB, InsertIt);
2950 } else if (BB->getCFIState() > State) {
2951 // If BB's CFI state is greater than State, it means we are behind in
2952 // the state. Just emit all instructions to reach this state at the
2953 // beginning of this BB. If this sequence of instructions involve
2954 // remember state or restore state, bail out.
2955 if (!replayCFIInstrs(State, BB->getCFIState(), BB, BB->begin()))
2956 return false;
2959 State = CFIStateAtExit;
2960 LLVM_DEBUG(dbgs() << Sep << State; Sep = ", ");
2963 LLVM_DEBUG(dbgs() << "\n");
2965 for (BinaryBasicBlock &BB : blocks()) {
2966 for (auto II = BB.begin(); II != BB.end();) {
2967 const MCCFIInstruction *CFI = getCFIFor(*II);
2968 if (CFI && (CFI->getOperation() == MCCFIInstruction::OpRememberState ||
2969 CFI->getOperation() == MCCFIInstruction::OpRestoreState)) {
2970 II = BB.eraseInstruction(II);
2971 } else {
2972 ++II;
2977 return true;
2980 bool BinaryFunction::requiresAddressTranslation() const {
2981 return opts::EnableBAT || hasSDTMarker() || hasPseudoProbe();
2984 bool BinaryFunction::requiresAddressMap() const {
2985 if (isInjected())
2986 return false;
2988 return opts::UpdateDebugSections || isMultiEntry() ||
2989 requiresAddressTranslation();
2992 uint64_t BinaryFunction::getInstructionCount() const {
2993 uint64_t Count = 0;
2994 for (const BinaryBasicBlock &BB : blocks())
2995 Count += BB.getNumNonPseudos();
2996 return Count;
2999 void BinaryFunction::clearDisasmState() {
3000 clearList(Instructions);
3001 clearList(IgnoredBranches);
3002 clearList(TakenBranches);
3004 if (BC.HasRelocations) {
3005 for (std::pair<const uint32_t, MCSymbol *> &LI : Labels)
3006 BC.UndefinedSymbols.insert(LI.second);
3007 for (MCSymbol *const EndLabel : FunctionEndLabels)
3008 if (EndLabel)
3009 BC.UndefinedSymbols.insert(EndLabel);
3013 void BinaryFunction::setTrapOnEntry() {
3014 clearDisasmState();
3016 forEachEntryPoint([&](uint64_t Offset, const MCSymbol *Label) -> bool {
3017 MCInst TrapInstr;
3018 BC.MIB->createTrap(TrapInstr);
3019 addInstruction(Offset, std::move(TrapInstr));
3020 return true;
3023 TrapsOnEntry = true;
3026 void BinaryFunction::setIgnored() {
3027 if (opts::processAllFunctions()) {
3028 // We can accept ignored functions before they've been disassembled.
3029 // In that case, they would still get disassembled and emited, but not
3030 // optimized.
3031 assert(CurrentState == State::Empty &&
3032 "cannot ignore non-empty functions in current mode");
3033 IsIgnored = true;
3034 return;
3037 clearDisasmState();
3039 // Clear CFG state too.
3040 if (hasCFG()) {
3041 releaseCFG();
3043 for (BinaryBasicBlock *BB : BasicBlocks)
3044 delete BB;
3045 clearList(BasicBlocks);
3047 for (BinaryBasicBlock *BB : DeletedBasicBlocks)
3048 delete BB;
3049 clearList(DeletedBasicBlocks);
3051 Layout.clear();
3054 CurrentState = State::Empty;
3056 IsIgnored = true;
3057 IsSimple = false;
3058 LLVM_DEBUG(dbgs() << "Ignoring " << getPrintName() << '\n');
3061 void BinaryFunction::duplicateConstantIslands() {
3062 assert(Islands && "function expected to have constant islands");
3064 for (BinaryBasicBlock *BB : getLayout().blocks()) {
3065 if (!BB->isCold())
3066 continue;
3068 for (MCInst &Inst : *BB) {
3069 int OpNum = 0;
3070 for (MCOperand &Operand : Inst) {
3071 if (!Operand.isExpr()) {
3072 ++OpNum;
3073 continue;
3075 const MCSymbol *Symbol = BC.MIB->getTargetSymbol(Inst, OpNum);
3076 // Check if this is an island symbol
3077 if (!Islands->Symbols.count(Symbol) &&
3078 !Islands->ProxySymbols.count(Symbol))
3079 continue;
3081 // Create cold symbol, if missing
3082 auto ISym = Islands->ColdSymbols.find(Symbol);
3083 MCSymbol *ColdSymbol;
3084 if (ISym != Islands->ColdSymbols.end()) {
3085 ColdSymbol = ISym->second;
3086 } else {
3087 ColdSymbol = BC.Ctx->getOrCreateSymbol(Symbol->getName() + ".cold");
3088 Islands->ColdSymbols[Symbol] = ColdSymbol;
3089 // Check if this is a proxy island symbol and update owner proxy map
3090 if (Islands->ProxySymbols.count(Symbol)) {
3091 BinaryFunction *Owner = Islands->ProxySymbols[Symbol];
3092 auto IProxiedSym = Owner->Islands->Proxies[this].find(Symbol);
3093 Owner->Islands->ColdProxies[this][IProxiedSym->second] = ColdSymbol;
3097 // Update instruction reference
3098 Operand = MCOperand::createExpr(BC.MIB->getTargetExprFor(
3099 Inst,
3100 MCSymbolRefExpr::create(ColdSymbol, MCSymbolRefExpr::VK_None,
3101 *BC.Ctx),
3102 *BC.Ctx, 0));
3103 ++OpNum;
3109 #ifndef MAX_PATH
3110 #define MAX_PATH 255
3111 #endif
3113 static std::string constructFilename(std::string Filename,
3114 std::string Annotation,
3115 std::string Suffix) {
3116 std::replace(Filename.begin(), Filename.end(), '/', '-');
3117 if (!Annotation.empty())
3118 Annotation.insert(0, "-");
3119 if (Filename.size() + Annotation.size() + Suffix.size() > MAX_PATH) {
3120 assert(Suffix.size() + Annotation.size() <= MAX_PATH);
3121 Filename.resize(MAX_PATH - (Suffix.size() + Annotation.size()));
3123 Filename += Annotation;
3124 Filename += Suffix;
3125 return Filename;
3128 static std::string formatEscapes(const std::string &Str) {
3129 std::string Result;
3130 for (unsigned I = 0; I < Str.size(); ++I) {
3131 char C = Str[I];
3132 switch (C) {
3133 case '\n':
3134 Result += "&#13;";
3135 break;
3136 case '"':
3137 break;
3138 default:
3139 Result += C;
3140 break;
3143 return Result;
3146 void BinaryFunction::dumpGraph(raw_ostream &OS) const {
3147 OS << "digraph \"" << getPrintName() << "\" {\n"
3148 << "node [fontname=courier, shape=box, style=filled, colorscheme=brbg9]\n";
3149 uint64_t Offset = Address;
3150 for (BinaryBasicBlock *BB : BasicBlocks) {
3151 auto LayoutPos = find(Layout.blocks(), BB);
3152 unsigned LayoutIndex = LayoutPos - Layout.block_begin();
3153 const char *ColdStr = BB->isCold() ? " (cold)" : "";
3154 std::vector<std::string> Attrs;
3155 // Bold box for entry points
3156 if (isEntryPoint(*BB))
3157 Attrs.push_back("penwidth=2");
3158 if (BLI && BLI->getLoopFor(BB)) {
3159 // Distinguish innermost loops
3160 const BinaryLoop *Loop = BLI->getLoopFor(BB);
3161 if (Loop->isInnermost())
3162 Attrs.push_back("fillcolor=6");
3163 else // some outer loop
3164 Attrs.push_back("fillcolor=4");
3165 } else { // non-loopy code
3166 Attrs.push_back("fillcolor=5");
3168 ListSeparator LS;
3169 OS << "\"" << BB->getName() << "\" [";
3170 for (StringRef Attr : Attrs)
3171 OS << LS << Attr;
3172 OS << "]\n";
3173 OS << format("\"%s\" [label=\"%s%s\\n(C:%lu,O:%lu,I:%u,L:%u,CFI:%u)\\n",
3174 BB->getName().data(), BB->getName().data(), ColdStr,
3175 BB->getKnownExecutionCount(), BB->getOffset(), getIndex(BB),
3176 LayoutIndex, BB->getCFIState());
3178 if (opts::DotToolTipCode) {
3179 std::string Str;
3180 raw_string_ostream CS(Str);
3181 Offset = BC.printInstructions(CS, BB->begin(), BB->end(), Offset, this,
3182 /* PrintMCInst = */ false,
3183 /* PrintMemData = */ false,
3184 /* PrintRelocations = */ false,
3185 /* Endl = */ R"(\\l)");
3186 OS << formatEscapes(CS.str()) << '\n';
3188 OS << "\"]\n";
3190 // analyzeBranch is just used to get the names of the branch
3191 // opcodes.
3192 const MCSymbol *TBB = nullptr;
3193 const MCSymbol *FBB = nullptr;
3194 MCInst *CondBranch = nullptr;
3195 MCInst *UncondBranch = nullptr;
3196 const bool Success = BB->analyzeBranch(TBB, FBB, CondBranch, UncondBranch);
3198 const MCInst *LastInstr = BB->getLastNonPseudoInstr();
3199 const bool IsJumpTable = LastInstr && BC.MIB->getJumpTable(*LastInstr);
3201 auto BI = BB->branch_info_begin();
3202 for (BinaryBasicBlock *Succ : BB->successors()) {
3203 std::string Branch;
3204 if (Success) {
3205 if (Succ == BB->getConditionalSuccessor(true)) {
3206 Branch = CondBranch ? std::string(BC.InstPrinter->getOpcodeName(
3207 CondBranch->getOpcode()))
3208 : "TB";
3209 } else if (Succ == BB->getConditionalSuccessor(false)) {
3210 Branch = UncondBranch ? std::string(BC.InstPrinter->getOpcodeName(
3211 UncondBranch->getOpcode()))
3212 : "FB";
3213 } else {
3214 Branch = "FT";
3217 if (IsJumpTable)
3218 Branch = "JT";
3219 OS << format("\"%s\" -> \"%s\" [label=\"%s", BB->getName().data(),
3220 Succ->getName().data(), Branch.c_str());
3222 if (BB->getExecutionCount() != COUNT_NO_PROFILE &&
3223 BI->MispredictedCount != BinaryBasicBlock::COUNT_INFERRED) {
3224 OS << "\\n(C:" << BI->Count << ",M:" << BI->MispredictedCount << ")";
3225 } else if (ExecutionCount != COUNT_NO_PROFILE &&
3226 BI->Count != BinaryBasicBlock::COUNT_NO_PROFILE) {
3227 OS << "\\n(IC:" << BI->Count << ")";
3229 OS << "\"]\n";
3231 ++BI;
3233 for (BinaryBasicBlock *LP : BB->landing_pads()) {
3234 OS << format("\"%s\" -> \"%s\" [constraint=false style=dashed]\n",
3235 BB->getName().data(), LP->getName().data());
3238 OS << "}\n";
3241 void BinaryFunction::viewGraph() const {
3242 SmallString<MAX_PATH> Filename;
3243 if (std::error_code EC =
3244 sys::fs::createTemporaryFile("bolt-cfg", "dot", Filename)) {
3245 BC.errs() << "BOLT-ERROR: " << EC.message() << ", unable to create "
3246 << " bolt-cfg-XXXXX.dot temporary file.\n";
3247 return;
3249 dumpGraphToFile(std::string(Filename));
3250 if (DisplayGraph(Filename))
3251 BC.errs() << "BOLT-ERROR: Can't display " << Filename
3252 << " with graphviz.\n";
3253 if (std::error_code EC = sys::fs::remove(Filename)) {
3254 BC.errs() << "BOLT-WARNING: " << EC.message() << ", failed to remove "
3255 << Filename << "\n";
3259 void BinaryFunction::dumpGraphForPass(std::string Annotation) const {
3260 if (!opts::shouldPrint(*this))
3261 return;
3263 std::string Filename = constructFilename(getPrintName(), Annotation, ".dot");
3264 if (opts::Verbosity >= 1)
3265 BC.outs() << "BOLT-INFO: dumping CFG to " << Filename << "\n";
3266 dumpGraphToFile(Filename);
3269 void BinaryFunction::dumpGraphToFile(std::string Filename) const {
3270 std::error_code EC;
3271 raw_fd_ostream of(Filename, EC, sys::fs::OF_None);
3272 if (EC) {
3273 if (opts::Verbosity >= 1) {
3274 BC.errs() << "BOLT-WARNING: " << EC.message() << ", unable to open "
3275 << Filename << " for output.\n";
3277 return;
3279 dumpGraph(of);
3282 bool BinaryFunction::validateCFG() const {
3283 // Skip the validation of CFG after it is finalized
3284 if (CurrentState == State::CFG_Finalized)
3285 return true;
3287 for (BinaryBasicBlock *BB : BasicBlocks)
3288 if (!BB->validateSuccessorInvariants())
3289 return false;
3291 // Make sure all blocks in CFG are valid.
3292 auto validateBlock = [this](const BinaryBasicBlock *BB, StringRef Desc) {
3293 if (!BB->isValid()) {
3294 BC.errs() << "BOLT-ERROR: deleted " << Desc << " " << BB->getName()
3295 << " detected in:\n";
3296 this->dump();
3297 return false;
3299 return true;
3301 for (const BinaryBasicBlock *BB : BasicBlocks) {
3302 if (!validateBlock(BB, "block"))
3303 return false;
3304 for (const BinaryBasicBlock *PredBB : BB->predecessors())
3305 if (!validateBlock(PredBB, "predecessor"))
3306 return false;
3307 for (const BinaryBasicBlock *SuccBB : BB->successors())
3308 if (!validateBlock(SuccBB, "successor"))
3309 return false;
3310 for (const BinaryBasicBlock *LP : BB->landing_pads())
3311 if (!validateBlock(LP, "landing pad"))
3312 return false;
3313 for (const BinaryBasicBlock *Thrower : BB->throwers())
3314 if (!validateBlock(Thrower, "thrower"))
3315 return false;
3318 for (const BinaryBasicBlock *BB : BasicBlocks) {
3319 std::unordered_set<const BinaryBasicBlock *> BBLandingPads;
3320 for (const BinaryBasicBlock *LP : BB->landing_pads()) {
3321 if (BBLandingPads.count(LP)) {
3322 BC.errs() << "BOLT-ERROR: duplicate landing pad detected in"
3323 << BB->getName() << " in function " << *this << '\n';
3324 return false;
3326 BBLandingPads.insert(LP);
3329 std::unordered_set<const BinaryBasicBlock *> BBThrowers;
3330 for (const BinaryBasicBlock *Thrower : BB->throwers()) {
3331 if (BBThrowers.count(Thrower)) {
3332 BC.errs() << "BOLT-ERROR: duplicate thrower detected in"
3333 << BB->getName() << " in function " << *this << '\n';
3334 return false;
3336 BBThrowers.insert(Thrower);
3339 for (const BinaryBasicBlock *LPBlock : BB->landing_pads()) {
3340 if (!llvm::is_contained(LPBlock->throwers(), BB)) {
3341 BC.errs() << "BOLT-ERROR: inconsistent landing pad detected in "
3342 << *this << ": " << BB->getName()
3343 << " is in LandingPads but not in " << LPBlock->getName()
3344 << " Throwers\n";
3345 return false;
3348 for (const BinaryBasicBlock *Thrower : BB->throwers()) {
3349 if (!llvm::is_contained(Thrower->landing_pads(), BB)) {
3350 BC.errs() << "BOLT-ERROR: inconsistent thrower detected in " << *this
3351 << ": " << BB->getName() << " is in Throwers list but not in "
3352 << Thrower->getName() << " LandingPads\n";
3353 return false;
3358 return true;
3361 void BinaryFunction::fixBranches() {
3362 auto &MIB = BC.MIB;
3363 MCContext *Ctx = BC.Ctx.get();
3365 for (BinaryBasicBlock *BB : BasicBlocks) {
3366 const MCSymbol *TBB = nullptr;
3367 const MCSymbol *FBB = nullptr;
3368 MCInst *CondBranch = nullptr;
3369 MCInst *UncondBranch = nullptr;
3370 if (!BB->analyzeBranch(TBB, FBB, CondBranch, UncondBranch))
3371 continue;
3373 // We will create unconditional branch with correct destination if needed.
3374 if (UncondBranch)
3375 BB->eraseInstruction(BB->findInstruction(UncondBranch));
3377 // Basic block that follows the current one in the final layout.
3378 const BinaryBasicBlock *const NextBB =
3379 Layout.getBasicBlockAfter(BB, /*IgnoreSplits=*/false);
3381 if (BB->succ_size() == 1) {
3382 // __builtin_unreachable() could create a conditional branch that
3383 // falls-through into the next function - hence the block will have only
3384 // one valid successor. Since behaviour is undefined - we replace
3385 // the conditional branch with an unconditional if required.
3386 if (CondBranch)
3387 BB->eraseInstruction(BB->findInstruction(CondBranch));
3388 if (BB->getSuccessor() == NextBB)
3389 continue;
3390 BB->addBranchInstruction(BB->getSuccessor());
3391 } else if (BB->succ_size() == 2) {
3392 assert(CondBranch && "conditional branch expected");
3393 const BinaryBasicBlock *TSuccessor = BB->getConditionalSuccessor(true);
3394 const BinaryBasicBlock *FSuccessor = BB->getConditionalSuccessor(false);
3396 // Eliminate unnecessary conditional branch.
3397 if (TSuccessor == FSuccessor) {
3398 // FIXME: at the moment, we cannot safely remove static key branches.
3399 if (MIB->isDynamicBranch(*CondBranch)) {
3400 if (opts::Verbosity) {
3401 BC.outs()
3402 << "BOLT-INFO: unable to remove redundant dynamic branch in "
3403 << *this << '\n';
3405 continue;
3408 BB->removeDuplicateConditionalSuccessor(CondBranch);
3409 if (TSuccessor != NextBB)
3410 BB->addBranchInstruction(TSuccessor);
3411 continue;
3414 // Reverse branch condition and swap successors.
3415 auto swapSuccessors = [&]() {
3416 if (!MIB->isReversibleBranch(*CondBranch)) {
3417 if (opts::Verbosity) {
3418 BC.outs() << "BOLT-INFO: unable to swap successors in " << *this
3419 << '\n';
3421 return false;
3423 std::swap(TSuccessor, FSuccessor);
3424 BB->swapConditionalSuccessors();
3425 auto L = BC.scopeLock();
3426 MIB->reverseBranchCondition(*CondBranch, TSuccessor->getLabel(), Ctx);
3427 return true;
3430 // Check whether the next block is a "taken" target and try to swap it
3431 // with a "fall-through" target.
3432 if (TSuccessor == NextBB && swapSuccessors())
3433 continue;
3435 // Update conditional branch destination if needed.
3436 if (MIB->getTargetSymbol(*CondBranch) != TSuccessor->getLabel()) {
3437 auto L = BC.scopeLock();
3438 MIB->replaceBranchTarget(*CondBranch, TSuccessor->getLabel(), Ctx);
3441 // No need for the unconditional branch.
3442 if (FSuccessor == NextBB)
3443 continue;
3445 if (BC.isX86()) {
3446 // We are going to generate two branches. Check if their targets are in
3447 // the same fragment as this block. If only one target is in the same
3448 // fragment, make it the destination of the conditional branch. There
3449 // is a chance it will be a short branch which takes 4 bytes fewer than
3450 // a long conditional branch. For unconditional branch, the difference
3451 // is 3 bytes.
3452 if (BB->getFragmentNum() != TSuccessor->getFragmentNum() &&
3453 BB->getFragmentNum() == FSuccessor->getFragmentNum())
3454 swapSuccessors();
3457 BB->addBranchInstruction(FSuccessor);
3459 // Cases where the number of successors is 0 (block ends with a
3460 // terminator) or more than 2 (switch table) don't require branch
3461 // instruction adjustments.
3463 assert((!isSimple() || validateCFG()) &&
3464 "Invalid CFG detected after fixing branches");
3467 void BinaryFunction::propagateGnuArgsSizeInfo(
3468 MCPlusBuilder::AllocatorIdTy AllocId) {
3469 assert(CurrentState == State::Disassembled && "unexpected function state");
3471 if (!hasEHRanges() || !usesGnuArgsSize())
3472 return;
3474 // The current value of DW_CFA_GNU_args_size affects all following
3475 // invoke instructions until the next CFI overrides it.
3476 // It is important to iterate basic blocks in the original order when
3477 // assigning the value.
3478 uint64_t CurrentGnuArgsSize = 0;
3479 for (BinaryBasicBlock *BB : BasicBlocks) {
3480 for (auto II = BB->begin(); II != BB->end();) {
3481 MCInst &Instr = *II;
3482 if (BC.MIB->isCFI(Instr)) {
3483 const MCCFIInstruction *CFI = getCFIFor(Instr);
3484 if (CFI->getOperation() == MCCFIInstruction::OpGnuArgsSize) {
3485 CurrentGnuArgsSize = CFI->getOffset();
3486 // Delete DW_CFA_GNU_args_size instructions and only regenerate
3487 // during the final code emission. The information is embedded
3488 // inside call instructions.
3489 II = BB->erasePseudoInstruction(II);
3490 continue;
3492 } else if (BC.MIB->isInvoke(Instr)) {
3493 // Add the value of GNU_args_size as an extra operand to invokes.
3494 BC.MIB->addGnuArgsSize(Instr, CurrentGnuArgsSize);
3496 ++II;
3501 void BinaryFunction::postProcessBranches() {
3502 if (!isSimple())
3503 return;
3504 for (BinaryBasicBlock &BB : blocks()) {
3505 auto LastInstrRI = BB.getLastNonPseudo();
3506 if (BB.succ_size() == 1) {
3507 if (LastInstrRI != BB.rend() &&
3508 BC.MIB->isConditionalBranch(*LastInstrRI)) {
3509 // __builtin_unreachable() could create a conditional branch that
3510 // falls-through into the next function - hence the block will have only
3511 // one valid successor. Such behaviour is undefined and thus we remove
3512 // the conditional branch while leaving a valid successor.
3513 BB.eraseInstruction(std::prev(LastInstrRI.base()));
3514 LLVM_DEBUG(dbgs() << "BOLT-DEBUG: erasing conditional branch in "
3515 << BB.getName() << " in function " << *this << '\n');
3517 } else if (BB.succ_size() == 0) {
3518 // Ignore unreachable basic blocks.
3519 if (BB.pred_size() == 0 || BB.isLandingPad())
3520 continue;
3522 // If it's the basic block that does not end up with a terminator - we
3523 // insert a return instruction unless it's a call instruction.
3524 if (LastInstrRI == BB.rend()) {
3525 LLVM_DEBUG(
3526 dbgs() << "BOLT-DEBUG: at least one instruction expected in BB "
3527 << BB.getName() << " in function " << *this << '\n');
3528 continue;
3530 if (!BC.MIB->isTerminator(*LastInstrRI) &&
3531 !BC.MIB->isCall(*LastInstrRI)) {
3532 LLVM_DEBUG(dbgs() << "BOLT-DEBUG: adding return to basic block "
3533 << BB.getName() << " in function " << *this << '\n');
3534 MCInst ReturnInstr;
3535 BC.MIB->createReturn(ReturnInstr);
3536 BB.addInstruction(ReturnInstr);
3540 assert(validateCFG() && "invalid CFG");
3543 MCSymbol *BinaryFunction::addEntryPointAtOffset(uint64_t Offset) {
3544 assert(Offset && "cannot add primary entry point");
3545 assert(CurrentState == State::Empty || CurrentState == State::Disassembled);
3547 const uint64_t EntryPointAddress = getAddress() + Offset;
3548 MCSymbol *LocalSymbol = getOrCreateLocalLabel(EntryPointAddress);
3550 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(LocalSymbol);
3551 if (EntrySymbol)
3552 return EntrySymbol;
3554 if (BinaryData *EntryBD = BC.getBinaryDataAtAddress(EntryPointAddress)) {
3555 EntrySymbol = EntryBD->getSymbol();
3556 } else {
3557 EntrySymbol = BC.getOrCreateGlobalSymbol(
3558 EntryPointAddress, Twine("__ENTRY_") + getOneName() + "@");
3560 SecondaryEntryPoints[LocalSymbol] = EntrySymbol;
3562 BC.setSymbolToFunctionMap(EntrySymbol, this);
3564 return EntrySymbol;
3567 MCSymbol *BinaryFunction::addEntryPoint(const BinaryBasicBlock &BB) {
3568 assert(CurrentState == State::CFG &&
3569 "basic block can be added as an entry only in a function with CFG");
3571 if (&BB == BasicBlocks.front())
3572 return getSymbol();
3574 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(BB);
3575 if (EntrySymbol)
3576 return EntrySymbol;
3578 EntrySymbol =
3579 BC.Ctx->getOrCreateSymbol("__ENTRY_" + BB.getLabel()->getName());
3581 SecondaryEntryPoints[BB.getLabel()] = EntrySymbol;
3583 BC.setSymbolToFunctionMap(EntrySymbol, this);
3585 return EntrySymbol;
3588 MCSymbol *BinaryFunction::getSymbolForEntryID(uint64_t EntryID) {
3589 if (EntryID == 0)
3590 return getSymbol();
3592 if (!isMultiEntry())
3593 return nullptr;
3595 uint64_t NumEntries = 1;
3596 if (hasCFG()) {
3597 for (BinaryBasicBlock *BB : BasicBlocks) {
3598 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(*BB);
3599 if (!EntrySymbol)
3600 continue;
3601 if (NumEntries == EntryID)
3602 return EntrySymbol;
3603 ++NumEntries;
3605 } else {
3606 for (std::pair<const uint32_t, MCSymbol *> &KV : Labels) {
3607 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(KV.second);
3608 if (!EntrySymbol)
3609 continue;
3610 if (NumEntries == EntryID)
3611 return EntrySymbol;
3612 ++NumEntries;
3616 return nullptr;
3619 uint64_t BinaryFunction::getEntryIDForSymbol(const MCSymbol *Symbol) const {
3620 if (!isMultiEntry())
3621 return 0;
3623 for (const MCSymbol *FunctionSymbol : getSymbols())
3624 if (FunctionSymbol == Symbol)
3625 return 0;
3627 // Check all secondary entries available as either basic blocks or lables.
3628 uint64_t NumEntries = 1;
3629 for (const BinaryBasicBlock *BB : BasicBlocks) {
3630 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(*BB);
3631 if (!EntrySymbol)
3632 continue;
3633 if (EntrySymbol == Symbol)
3634 return NumEntries;
3635 ++NumEntries;
3637 NumEntries = 1;
3638 for (const std::pair<const uint32_t, MCSymbol *> &KV : Labels) {
3639 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(KV.second);
3640 if (!EntrySymbol)
3641 continue;
3642 if (EntrySymbol == Symbol)
3643 return NumEntries;
3644 ++NumEntries;
3647 llvm_unreachable("symbol not found");
3650 bool BinaryFunction::forEachEntryPoint(EntryPointCallbackTy Callback) const {
3651 bool Status = Callback(0, getSymbol());
3652 if (!isMultiEntry())
3653 return Status;
3655 for (const std::pair<const uint32_t, MCSymbol *> &KV : Labels) {
3656 if (!Status)
3657 break;
3659 MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(KV.second);
3660 if (!EntrySymbol)
3661 continue;
3663 Status = Callback(KV.first, EntrySymbol);
3666 return Status;
3669 BinaryFunction::BasicBlockListType BinaryFunction::dfs() const {
3670 BasicBlockListType DFS;
3671 std::stack<BinaryBasicBlock *> Stack;
3672 std::set<BinaryBasicBlock *> Visited;
3674 // Push entry points to the stack in reverse order.
3676 // NB: we rely on the original order of entries to match.
3677 SmallVector<BinaryBasicBlock *> EntryPoints;
3678 llvm::copy_if(BasicBlocks, std::back_inserter(EntryPoints),
3679 [&](const BinaryBasicBlock *const BB) { return isEntryPoint(*BB); });
3680 // Sort entry points by their offset to make sure we got them in the right
3681 // order.
3682 llvm::stable_sort(EntryPoints, [](const BinaryBasicBlock *const A,
3683 const BinaryBasicBlock *const B) {
3684 return A->getOffset() < B->getOffset();
3686 for (BinaryBasicBlock *const BB : reverse(EntryPoints))
3687 Stack.push(BB);
3689 while (!Stack.empty()) {
3690 BinaryBasicBlock *BB = Stack.top();
3691 Stack.pop();
3693 if (!Visited.insert(BB).second)
3694 continue;
3695 DFS.push_back(BB);
3697 for (BinaryBasicBlock *SuccBB : BB->landing_pads()) {
3698 Stack.push(SuccBB);
3701 const MCSymbol *TBB = nullptr;
3702 const MCSymbol *FBB = nullptr;
3703 MCInst *CondBranch = nullptr;
3704 MCInst *UncondBranch = nullptr;
3705 if (BB->analyzeBranch(TBB, FBB, CondBranch, UncondBranch) && CondBranch &&
3706 BB->succ_size() == 2) {
3707 if (BC.MIB->getCanonicalBranchCondCode(BC.MIB->getCondCode(
3708 *CondBranch)) == BC.MIB->getCondCode(*CondBranch)) {
3709 Stack.push(BB->getConditionalSuccessor(true));
3710 Stack.push(BB->getConditionalSuccessor(false));
3711 } else {
3712 Stack.push(BB->getConditionalSuccessor(false));
3713 Stack.push(BB->getConditionalSuccessor(true));
3715 } else {
3716 for (BinaryBasicBlock *SuccBB : BB->successors()) {
3717 Stack.push(SuccBB);
3722 return DFS;
3725 size_t BinaryFunction::computeHash(bool UseDFS, HashFunction HashFunction,
3726 OperandHashFuncTy OperandHashFunc) const {
3727 LLVM_DEBUG({
3728 dbgs() << "BOLT-DEBUG: computeHash " << getPrintName() << ' '
3729 << (UseDFS ? "dfs" : "bin") << " order "
3730 << (HashFunction == HashFunction::StdHash ? "std::hash" : "xxh3")
3731 << '\n';
3734 if (size() == 0)
3735 return 0;
3737 assert(hasCFG() && "function is expected to have CFG");
3739 SmallVector<const BinaryBasicBlock *, 0> Order;
3740 if (UseDFS)
3741 llvm::copy(dfs(), std::back_inserter(Order));
3742 else
3743 llvm::copy(Layout.blocks(), std::back_inserter(Order));
3745 // The hash is computed by creating a string of all instruction opcodes and
3746 // possibly their operands and then hashing that string with std::hash.
3747 std::string HashString;
3748 for (const BinaryBasicBlock *BB : Order)
3749 HashString.append(hashBlock(BC, *BB, OperandHashFunc));
3751 switch (HashFunction) {
3752 case HashFunction::StdHash:
3753 return Hash = std::hash<std::string>{}(HashString);
3754 case HashFunction::XXH3:
3755 return Hash = llvm::xxh3_64bits(HashString);
3757 llvm_unreachable("Unhandled HashFunction");
3760 void BinaryFunction::insertBasicBlocks(
3761 BinaryBasicBlock *Start,
3762 std::vector<std::unique_ptr<BinaryBasicBlock>> &&NewBBs,
3763 const bool UpdateLayout, const bool UpdateCFIState,
3764 const bool RecomputeLandingPads) {
3765 const int64_t StartIndex = Start ? getIndex(Start) : -1LL;
3766 const size_t NumNewBlocks = NewBBs.size();
3768 BasicBlocks.insert(BasicBlocks.begin() + (StartIndex + 1), NumNewBlocks,
3769 nullptr);
3771 int64_t I = StartIndex + 1;
3772 for (std::unique_ptr<BinaryBasicBlock> &BB : NewBBs) {
3773 assert(!BasicBlocks[I]);
3774 BasicBlocks[I++] = BB.release();
3777 if (RecomputeLandingPads)
3778 recomputeLandingPads();
3779 else
3780 updateBBIndices(0);
3782 if (UpdateLayout)
3783 updateLayout(Start, NumNewBlocks);
3785 if (UpdateCFIState)
3786 updateCFIState(Start, NumNewBlocks);
3789 BinaryFunction::iterator BinaryFunction::insertBasicBlocks(
3790 BinaryFunction::iterator StartBB,
3791 std::vector<std::unique_ptr<BinaryBasicBlock>> &&NewBBs,
3792 const bool UpdateLayout, const bool UpdateCFIState,
3793 const bool RecomputeLandingPads) {
3794 const unsigned StartIndex = getIndex(&*StartBB);
3795 const size_t NumNewBlocks = NewBBs.size();
3797 BasicBlocks.insert(BasicBlocks.begin() + StartIndex + 1, NumNewBlocks,
3798 nullptr);
3799 auto RetIter = BasicBlocks.begin() + StartIndex + 1;
3801 unsigned I = StartIndex + 1;
3802 for (std::unique_ptr<BinaryBasicBlock> &BB : NewBBs) {
3803 assert(!BasicBlocks[I]);
3804 BasicBlocks[I++] = BB.release();
3807 if (RecomputeLandingPads)
3808 recomputeLandingPads();
3809 else
3810 updateBBIndices(0);
3812 if (UpdateLayout)
3813 updateLayout(*std::prev(RetIter), NumNewBlocks);
3815 if (UpdateCFIState)
3816 updateCFIState(*std::prev(RetIter), NumNewBlocks);
3818 return RetIter;
3821 void BinaryFunction::updateBBIndices(const unsigned StartIndex) {
3822 for (unsigned I = StartIndex; I < BasicBlocks.size(); ++I)
3823 BasicBlocks[I]->Index = I;
3826 void BinaryFunction::updateCFIState(BinaryBasicBlock *Start,
3827 const unsigned NumNewBlocks) {
3828 const int32_t CFIState = Start->getCFIStateAtExit();
3829 const unsigned StartIndex = getIndex(Start) + 1;
3830 for (unsigned I = 0; I < NumNewBlocks; ++I)
3831 BasicBlocks[StartIndex + I]->setCFIState(CFIState);
3834 void BinaryFunction::updateLayout(BinaryBasicBlock *Start,
3835 const unsigned NumNewBlocks) {
3836 BasicBlockListType::iterator Begin;
3837 BasicBlockListType::iterator End;
3839 // If start not provided copy new blocks from the beginning of BasicBlocks
3840 if (!Start) {
3841 Begin = BasicBlocks.begin();
3842 End = BasicBlocks.begin() + NumNewBlocks;
3843 } else {
3844 unsigned StartIndex = getIndex(Start);
3845 Begin = std::next(BasicBlocks.begin(), StartIndex + 1);
3846 End = std::next(BasicBlocks.begin(), StartIndex + NumNewBlocks + 1);
3849 // Insert new blocks in the layout immediately after Start.
3850 Layout.insertBasicBlocks(Start, {Begin, End});
3851 Layout.updateLayoutIndices();
3854 bool BinaryFunction::checkForAmbiguousJumpTables() {
3855 SmallSet<uint64_t, 4> JumpTables;
3856 for (BinaryBasicBlock *&BB : BasicBlocks) {
3857 for (MCInst &Inst : *BB) {
3858 if (!BC.MIB->isIndirectBranch(Inst))
3859 continue;
3860 uint64_t JTAddress = BC.MIB->getJumpTable(Inst);
3861 if (!JTAddress)
3862 continue;
3863 // This address can be inside another jump table, but we only consider
3864 // it ambiguous when the same start address is used, not the same JT
3865 // object.
3866 if (!JumpTables.count(JTAddress)) {
3867 JumpTables.insert(JTAddress);
3868 continue;
3870 return true;
3873 return false;
3876 void BinaryFunction::disambiguateJumpTables(
3877 MCPlusBuilder::AllocatorIdTy AllocId) {
3878 assert((opts::JumpTables != JTS_BASIC && isSimple()) || !BC.HasRelocations);
3879 SmallPtrSet<JumpTable *, 4> JumpTables;
3880 for (BinaryBasicBlock *&BB : BasicBlocks) {
3881 for (MCInst &Inst : *BB) {
3882 if (!BC.MIB->isIndirectBranch(Inst))
3883 continue;
3884 JumpTable *JT = getJumpTable(Inst);
3885 if (!JT)
3886 continue;
3887 if (JumpTables.insert(JT).second)
3888 continue;
3889 // This instruction is an indirect jump using a jump table, but it is
3890 // using the same jump table of another jump. Try all our tricks to
3891 // extract the jump table symbol and make it point to a new, duplicated JT
3892 MCPhysReg BaseReg1;
3893 uint64_t Scale;
3894 const MCSymbol *Target;
3895 // In case we match if our first matcher, first instruction is the one to
3896 // patch
3897 MCInst *JTLoadInst = &Inst;
3898 // Try a standard indirect jump matcher, scale 8
3899 std::unique_ptr<MCPlusBuilder::MCInstMatcher> IndJmpMatcher =
3900 BC.MIB->matchIndJmp(BC.MIB->matchReg(BaseReg1),
3901 BC.MIB->matchImm(Scale), BC.MIB->matchReg(),
3902 /*Offset=*/BC.MIB->matchSymbol(Target));
3903 if (!IndJmpMatcher->match(
3904 *BC.MRI, *BC.MIB,
3905 MutableArrayRef<MCInst>(&*BB->begin(), &Inst + 1), -1) ||
3906 BaseReg1 != BC.MIB->getNoRegister() || Scale != 8) {
3907 MCPhysReg BaseReg2;
3908 uint64_t Offset;
3909 // Standard JT matching failed. Trying now:
3910 // movq "jt.2397/1"(,%rax,8), %rax
3911 // jmpq *%rax
3912 std::unique_ptr<MCPlusBuilder::MCInstMatcher> LoadMatcherOwner =
3913 BC.MIB->matchLoad(BC.MIB->matchReg(BaseReg1),
3914 BC.MIB->matchImm(Scale), BC.MIB->matchReg(),
3915 /*Offset=*/BC.MIB->matchSymbol(Target));
3916 MCPlusBuilder::MCInstMatcher *LoadMatcher = LoadMatcherOwner.get();
3917 std::unique_ptr<MCPlusBuilder::MCInstMatcher> IndJmpMatcher2 =
3918 BC.MIB->matchIndJmp(std::move(LoadMatcherOwner));
3919 if (!IndJmpMatcher2->match(
3920 *BC.MRI, *BC.MIB,
3921 MutableArrayRef<MCInst>(&*BB->begin(), &Inst + 1), -1) ||
3922 BaseReg1 != BC.MIB->getNoRegister() || Scale != 8) {
3923 // JT matching failed. Trying now:
3924 // PIC-style matcher, scale 4
3925 // addq %rdx, %rsi
3926 // addq %rdx, %rdi
3927 // leaq DATAat0x402450(%rip), %r11
3928 // movslq (%r11,%rdx,4), %rcx
3929 // addq %r11, %rcx
3930 // jmpq *%rcx # JUMPTABLE @0x402450
3931 std::unique_ptr<MCPlusBuilder::MCInstMatcher> PICIndJmpMatcher =
3932 BC.MIB->matchIndJmp(BC.MIB->matchAdd(
3933 BC.MIB->matchReg(BaseReg1),
3934 BC.MIB->matchLoad(BC.MIB->matchReg(BaseReg2),
3935 BC.MIB->matchImm(Scale), BC.MIB->matchReg(),
3936 BC.MIB->matchImm(Offset))));
3937 std::unique_ptr<MCPlusBuilder::MCInstMatcher> LEAMatcherOwner =
3938 BC.MIB->matchLoadAddr(BC.MIB->matchSymbol(Target));
3939 MCPlusBuilder::MCInstMatcher *LEAMatcher = LEAMatcherOwner.get();
3940 std::unique_ptr<MCPlusBuilder::MCInstMatcher> PICBaseAddrMatcher =
3941 BC.MIB->matchIndJmp(BC.MIB->matchAdd(std::move(LEAMatcherOwner),
3942 BC.MIB->matchAnyOperand()));
3943 if (!PICIndJmpMatcher->match(
3944 *BC.MRI, *BC.MIB,
3945 MutableArrayRef<MCInst>(&*BB->begin(), &Inst + 1), -1) ||
3946 Scale != 4 || BaseReg1 != BaseReg2 || Offset != 0 ||
3947 !PICBaseAddrMatcher->match(
3948 *BC.MRI, *BC.MIB,
3949 MutableArrayRef<MCInst>(&*BB->begin(), &Inst + 1), -1)) {
3950 llvm_unreachable("Failed to extract jump table base");
3951 continue;
3953 // Matched PIC, identify the instruction with the reference to the JT
3954 JTLoadInst = LEAMatcher->CurInst;
3955 } else {
3956 // Matched non-PIC
3957 JTLoadInst = LoadMatcher->CurInst;
3961 uint64_t NewJumpTableID = 0;
3962 const MCSymbol *NewJTLabel;
3963 std::tie(NewJumpTableID, NewJTLabel) =
3964 BC.duplicateJumpTable(*this, JT, Target);
3966 auto L = BC.scopeLock();
3967 BC.MIB->replaceMemOperandDisp(*JTLoadInst, NewJTLabel, BC.Ctx.get());
3969 // We use a unique ID with the high bit set as address for this "injected"
3970 // jump table (not originally in the input binary).
3971 BC.MIB->setJumpTable(Inst, NewJumpTableID, 0, AllocId);
3976 bool BinaryFunction::replaceJumpTableEntryIn(BinaryBasicBlock *BB,
3977 BinaryBasicBlock *OldDest,
3978 BinaryBasicBlock *NewDest) {
3979 MCInst *Instr = BB->getLastNonPseudoInstr();
3980 if (!Instr || !BC.MIB->isIndirectBranch(*Instr))
3981 return false;
3982 uint64_t JTAddress = BC.MIB->getJumpTable(*Instr);
3983 assert(JTAddress && "Invalid jump table address");
3984 JumpTable *JT = getJumpTableContainingAddress(JTAddress);
3985 assert(JT && "No jump table structure for this indirect branch");
3986 bool Patched = JT->replaceDestination(JTAddress, OldDest->getLabel(),
3987 NewDest->getLabel());
3988 (void)Patched;
3989 assert(Patched && "Invalid entry to be replaced in jump table");
3990 return true;
3993 BinaryBasicBlock *BinaryFunction::splitEdge(BinaryBasicBlock *From,
3994 BinaryBasicBlock *To) {
3995 // Create intermediate BB
3996 MCSymbol *Tmp;
3998 auto L = BC.scopeLock();
3999 Tmp = BC.Ctx->createNamedTempSymbol("SplitEdge");
4001 // Link new BBs to the original input offset of the From BB, so we can map
4002 // samples recorded in new BBs back to the original BB seem in the input
4003 // binary (if using BAT)
4004 std::unique_ptr<BinaryBasicBlock> NewBB = createBasicBlock(Tmp);
4005 NewBB->setOffset(From->getInputOffset());
4006 BinaryBasicBlock *NewBBPtr = NewBB.get();
4008 // Update "From" BB
4009 auto I = From->succ_begin();
4010 auto BI = From->branch_info_begin();
4011 for (; I != From->succ_end(); ++I) {
4012 if (*I == To)
4013 break;
4014 ++BI;
4016 assert(I != From->succ_end() && "Invalid CFG edge in splitEdge!");
4017 uint64_t OrigCount = BI->Count;
4018 uint64_t OrigMispreds = BI->MispredictedCount;
4019 replaceJumpTableEntryIn(From, To, NewBBPtr);
4020 From->replaceSuccessor(To, NewBBPtr, OrigCount, OrigMispreds);
4022 NewBB->addSuccessor(To, OrigCount, OrigMispreds);
4023 NewBB->setExecutionCount(OrigCount);
4024 NewBB->setIsCold(From->isCold());
4026 // Update CFI and BB layout with new intermediate BB
4027 std::vector<std::unique_ptr<BinaryBasicBlock>> NewBBs;
4028 NewBBs.emplace_back(std::move(NewBB));
4029 insertBasicBlocks(From, std::move(NewBBs), true, true,
4030 /*RecomputeLandingPads=*/false);
4031 return NewBBPtr;
4034 void BinaryFunction::deleteConservativeEdges() {
4035 // Our goal is to aggressively remove edges from the CFG that we believe are
4036 // wrong. This is used for instrumentation, where it is safe to remove
4037 // fallthrough edges because we won't reorder blocks.
4038 for (auto I = BasicBlocks.begin(), E = BasicBlocks.end(); I != E; ++I) {
4039 BinaryBasicBlock *BB = *I;
4040 if (BB->succ_size() != 1 || BB->size() == 0)
4041 continue;
4043 auto NextBB = std::next(I);
4044 MCInst *Last = BB->getLastNonPseudoInstr();
4045 // Fallthrough is a landing pad? Delete this edge (as long as we don't
4046 // have a direct jump to it)
4047 if ((*BB->succ_begin())->isLandingPad() && NextBB != E &&
4048 *BB->succ_begin() == *NextBB && Last && !BC.MIB->isBranch(*Last)) {
4049 BB->removeAllSuccessors();
4050 continue;
4053 // Look for suspicious calls at the end of BB where gcc may optimize it and
4054 // remove the jump to the epilogue when it knows the call won't return.
4055 if (!Last || !BC.MIB->isCall(*Last))
4056 continue;
4058 const MCSymbol *CalleeSymbol = BC.MIB->getTargetSymbol(*Last);
4059 if (!CalleeSymbol)
4060 continue;
4062 StringRef CalleeName = CalleeSymbol->getName();
4063 if (CalleeName != "__cxa_throw@PLT" && CalleeName != "_Unwind_Resume@PLT" &&
4064 CalleeName != "__cxa_rethrow@PLT" && CalleeName != "exit@PLT" &&
4065 CalleeName != "abort@PLT")
4066 continue;
4068 BB->removeAllSuccessors();
4072 bool BinaryFunction::isSymbolValidInScope(const SymbolRef &Symbol,
4073 uint64_t SymbolSize) const {
4074 // If this symbol is in a different section from the one where the
4075 // function symbol is, don't consider it as valid.
4076 if (!getOriginSection()->containsAddress(
4077 cantFail(Symbol.getAddress(), "cannot get symbol address")))
4078 return false;
4080 // Some symbols are tolerated inside function bodies, others are not.
4081 // The real function boundaries may not be known at this point.
4082 if (BC.isMarker(Symbol))
4083 return true;
4085 // It's okay to have a zero-sized symbol in the middle of non-zero-sized
4086 // function.
4087 if (SymbolSize == 0 && containsAddress(cantFail(Symbol.getAddress())))
4088 return true;
4090 if (cantFail(Symbol.getType()) != SymbolRef::ST_Unknown)
4091 return false;
4093 if (cantFail(Symbol.getFlags()) & SymbolRef::SF_Global)
4094 return false;
4096 return true;
4099 void BinaryFunction::adjustExecutionCount(uint64_t Count) {
4100 if (getKnownExecutionCount() == 0 || Count == 0)
4101 return;
4103 if (ExecutionCount < Count)
4104 Count = ExecutionCount;
4106 double AdjustmentRatio = ((double)ExecutionCount - Count) / ExecutionCount;
4107 if (AdjustmentRatio < 0.0)
4108 AdjustmentRatio = 0.0;
4110 for (BinaryBasicBlock &BB : blocks())
4111 BB.adjustExecutionCount(AdjustmentRatio);
4113 ExecutionCount -= Count;
4116 BinaryFunction::~BinaryFunction() {
4117 for (BinaryBasicBlock *BB : BasicBlocks)
4118 delete BB;
4119 for (BinaryBasicBlock *BB : DeletedBasicBlocks)
4120 delete BB;
4123 void BinaryFunction::constructDomTree() {
4124 BDT.reset(new BinaryDominatorTree);
4125 BDT->recalculate(*this);
4128 void BinaryFunction::calculateLoopInfo() {
4129 if (!hasDomTree())
4130 constructDomTree();
4131 // Discover loops.
4132 BLI.reset(new BinaryLoopInfo());
4133 BLI->analyze(getDomTree());
4135 // Traverse discovered loops and add depth and profile information.
4136 std::stack<BinaryLoop *> St;
4137 for (auto I = BLI->begin(), E = BLI->end(); I != E; ++I) {
4138 St.push(*I);
4139 ++BLI->OuterLoops;
4142 while (!St.empty()) {
4143 BinaryLoop *L = St.top();
4144 St.pop();
4145 ++BLI->TotalLoops;
4146 BLI->MaximumDepth = std::max(L->getLoopDepth(), BLI->MaximumDepth);
4148 // Add nested loops in the stack.
4149 for (BinaryLoop::iterator I = L->begin(), E = L->end(); I != E; ++I)
4150 St.push(*I);
4152 // Skip if no valid profile is found.
4153 if (!hasValidProfile()) {
4154 L->EntryCount = COUNT_NO_PROFILE;
4155 L->ExitCount = COUNT_NO_PROFILE;
4156 L->TotalBackEdgeCount = COUNT_NO_PROFILE;
4157 continue;
4160 // Compute back edge count.
4161 SmallVector<BinaryBasicBlock *, 1> Latches;
4162 L->getLoopLatches(Latches);
4164 for (BinaryBasicBlock *Latch : Latches) {
4165 auto BI = Latch->branch_info_begin();
4166 for (BinaryBasicBlock *Succ : Latch->successors()) {
4167 if (Succ == L->getHeader()) {
4168 assert(BI->Count != BinaryBasicBlock::COUNT_NO_PROFILE &&
4169 "profile data not found");
4170 L->TotalBackEdgeCount += BI->Count;
4172 ++BI;
4176 // Compute entry count.
4177 L->EntryCount = L->getHeader()->getExecutionCount() - L->TotalBackEdgeCount;
4179 // Compute exit count.
4180 SmallVector<BinaryLoop::Edge, 1> ExitEdges;
4181 L->getExitEdges(ExitEdges);
4182 for (BinaryLoop::Edge &Exit : ExitEdges) {
4183 const BinaryBasicBlock *Exiting = Exit.first;
4184 const BinaryBasicBlock *ExitTarget = Exit.second;
4185 auto BI = Exiting->branch_info_begin();
4186 for (BinaryBasicBlock *Succ : Exiting->successors()) {
4187 if (Succ == ExitTarget) {
4188 assert(BI->Count != BinaryBasicBlock::COUNT_NO_PROFILE &&
4189 "profile data not found");
4190 L->ExitCount += BI->Count;
4192 ++BI;
4198 void BinaryFunction::updateOutputValues(const BOLTLinker &Linker) {
4199 if (!isEmitted()) {
4200 assert(!isInjected() && "injected function should be emitted");
4201 setOutputAddress(getAddress());
4202 setOutputSize(getSize());
4203 return;
4206 const auto SymbolInfo = Linker.lookupSymbolInfo(getSymbol()->getName());
4207 assert(SymbolInfo && "Cannot find function entry symbol");
4208 setOutputAddress(SymbolInfo->Address);
4209 setOutputSize(SymbolInfo->Size);
4211 if (BC.HasRelocations || isInjected()) {
4212 if (hasConstantIsland()) {
4213 const auto DataAddress =
4214 Linker.lookupSymbol(getFunctionConstantIslandLabel()->getName());
4215 assert(DataAddress && "Cannot find function CI symbol");
4216 setOutputDataAddress(*DataAddress);
4217 for (auto It : Islands->Offsets) {
4218 const uint64_t OldOffset = It.first;
4219 BinaryData *BD = BC.getBinaryDataAtAddress(getAddress() + OldOffset);
4220 if (!BD)
4221 continue;
4223 MCSymbol *Symbol = It.second;
4224 const auto NewAddress = Linker.lookupSymbol(Symbol->getName());
4225 assert(NewAddress && "Cannot find CI symbol");
4226 auto &Section = *getCodeSection();
4227 const auto NewOffset = *NewAddress - Section.getOutputAddress();
4228 BD->setOutputLocation(Section, NewOffset);
4231 if (isSplit()) {
4232 for (FunctionFragment &FF : getLayout().getSplitFragments()) {
4233 ErrorOr<BinarySection &> ColdSection =
4234 getCodeSection(FF.getFragmentNum());
4235 // If fragment is empty, cold section might not exist
4236 if (FF.empty() && ColdSection.getError())
4237 continue;
4239 const MCSymbol *ColdStartSymbol = getSymbol(FF.getFragmentNum());
4240 // If fragment is empty, symbol might have not been emitted
4241 if (FF.empty() && (!ColdStartSymbol || !ColdStartSymbol->isDefined()) &&
4242 !hasConstantIsland())
4243 continue;
4244 assert(ColdStartSymbol && ColdStartSymbol->isDefined() &&
4245 "split function should have defined cold symbol");
4246 const auto ColdStartSymbolInfo =
4247 Linker.lookupSymbolInfo(ColdStartSymbol->getName());
4248 assert(ColdStartSymbolInfo && "Cannot find cold start symbol");
4249 FF.setAddress(ColdStartSymbolInfo->Address);
4250 FF.setImageSize(ColdStartSymbolInfo->Size);
4251 if (hasConstantIsland()) {
4252 const auto DataAddress = Linker.lookupSymbol(
4253 getFunctionColdConstantIslandLabel()->getName());
4254 assert(DataAddress && "Cannot find cold CI symbol");
4255 setOutputColdDataAddress(*DataAddress);
4261 // Update basic block output ranges for the debug info, if we have
4262 // secondary entry points in the symbol table to update or if writing BAT.
4263 if (!requiresAddressMap())
4264 return;
4266 // AArch64 may have functions that only contains a constant island (no code).
4267 if (getLayout().block_empty())
4268 return;
4270 for (FunctionFragment &FF : getLayout().fragments()) {
4271 if (FF.empty())
4272 continue;
4274 const uint64_t FragmentBaseAddress =
4275 getCodeSection(isSimple() ? FF.getFragmentNum() : FragmentNum::main())
4276 ->getOutputAddress();
4278 BinaryBasicBlock *PrevBB = nullptr;
4279 for (BinaryBasicBlock *const BB : FF) {
4280 assert(BB->getLabel()->isDefined() && "symbol should be defined");
4281 if (!BC.HasRelocations) {
4282 if (BB->isSplit())
4283 assert(FragmentBaseAddress == FF.getAddress());
4284 else
4285 assert(FragmentBaseAddress == getOutputAddress());
4286 (void)FragmentBaseAddress;
4289 // Injected functions likely will fail lookup, as they have no
4290 // input range. Just assign the BB the output address of the
4291 // function.
4292 auto MaybeBBAddress = BC.getIOAddressMap().lookup(BB->getLabel());
4293 const uint64_t BBAddress = MaybeBBAddress ? *MaybeBBAddress
4294 : BB->isSplit() ? FF.getAddress()
4295 : getOutputAddress();
4296 BB->setOutputStartAddress(BBAddress);
4298 if (PrevBB) {
4299 assert(PrevBB->getOutputAddressRange().first <= BBAddress &&
4300 "Bad output address for basic block.");
4301 assert((PrevBB->getOutputAddressRange().first != BBAddress ||
4302 !hasInstructions() || !PrevBB->getNumNonPseudos()) &&
4303 "Bad output address for basic block.");
4304 PrevBB->setOutputEndAddress(BBAddress);
4306 PrevBB = BB;
4309 PrevBB->setOutputEndAddress(PrevBB->isSplit()
4310 ? FF.getAddress() + FF.getImageSize()
4311 : getOutputAddress() + getOutputSize());
4314 // Reset output addresses for deleted blocks.
4315 for (BinaryBasicBlock *BB : DeletedBasicBlocks) {
4316 BB->setOutputStartAddress(0);
4317 BB->setOutputEndAddress(0);
4321 DebugAddressRangesVector BinaryFunction::getOutputAddressRanges() const {
4322 DebugAddressRangesVector OutputRanges;
4324 if (isFolded())
4325 return OutputRanges;
4327 if (IsFragment)
4328 return OutputRanges;
4330 OutputRanges.emplace_back(getOutputAddress(),
4331 getOutputAddress() + getOutputSize());
4332 if (isSplit()) {
4333 assert(isEmitted() && "split function should be emitted");
4334 for (const FunctionFragment &FF : getLayout().getSplitFragments())
4335 OutputRanges.emplace_back(FF.getAddress(),
4336 FF.getAddress() + FF.getImageSize());
4339 if (isSimple())
4340 return OutputRanges;
4342 for (BinaryFunction *Frag : Fragments) {
4343 assert(!Frag->isSimple() &&
4344 "fragment of non-simple function should also be non-simple");
4345 OutputRanges.emplace_back(Frag->getOutputAddress(),
4346 Frag->getOutputAddress() + Frag->getOutputSize());
4349 return OutputRanges;
4352 uint64_t BinaryFunction::translateInputToOutputAddress(uint64_t Address) const {
4353 if (isFolded())
4354 return 0;
4356 // If the function hasn't changed return the same address.
4357 if (!isEmitted())
4358 return Address;
4360 if (Address < getAddress())
4361 return 0;
4363 // Check if the address is associated with an instruction that is tracked
4364 // by address translation.
4365 if (auto OutputAddress = BC.getIOAddressMap().lookup(Address))
4366 return *OutputAddress;
4368 // FIXME: #18950828 - we rely on relative offsets inside basic blocks to stay
4369 // intact. Instead we can use pseudo instructions and/or annotations.
4370 const uint64_t Offset = Address - getAddress();
4371 const BinaryBasicBlock *BB = getBasicBlockContainingOffset(Offset);
4372 if (!BB) {
4373 // Special case for address immediately past the end of the function.
4374 if (Offset == getSize())
4375 return getOutputAddress() + getOutputSize();
4377 return 0;
4380 return std::min(BB->getOutputAddressRange().first + Offset - BB->getOffset(),
4381 BB->getOutputAddressRange().second);
4384 DebugAddressRangesVector
4385 BinaryFunction::translateInputToOutputRange(DebugAddressRange InRange) const {
4386 DebugAddressRangesVector OutRanges;
4388 // The function was removed from the output. Return an empty range.
4389 if (isFolded())
4390 return OutRanges;
4392 // If the function hasn't changed return the same range.
4393 if (!isEmitted()) {
4394 OutRanges.emplace_back(InRange);
4395 return OutRanges;
4398 if (!containsAddress(InRange.LowPC))
4399 return OutRanges;
4401 // Special case of an empty range [X, X). Some tools expect X to be updated.
4402 if (InRange.LowPC == InRange.HighPC) {
4403 if (uint64_t NewPC = translateInputToOutputAddress(InRange.LowPC))
4404 OutRanges.push_back(DebugAddressRange{NewPC, NewPC});
4405 return OutRanges;
4408 uint64_t InputOffset = InRange.LowPC - getAddress();
4409 const uint64_t InputEndOffset =
4410 std::min(InRange.HighPC - getAddress(), getSize());
4412 auto BBI = llvm::upper_bound(BasicBlockOffsets,
4413 BasicBlockOffset(InputOffset, nullptr),
4414 CompareBasicBlockOffsets());
4415 assert(BBI != BasicBlockOffsets.begin());
4417 // Iterate over blocks in the input order using BasicBlockOffsets.
4418 for (--BBI; InputOffset < InputEndOffset && BBI != BasicBlockOffsets.end();
4419 InputOffset = BBI->second->getEndOffset(), ++BBI) {
4420 const BinaryBasicBlock &BB = *BBI->second;
4421 if (InputOffset < BB.getOffset() || InputOffset >= BB.getEndOffset()) {
4422 LLVM_DEBUG(
4423 dbgs() << "BOLT-DEBUG: invalid debug address range detected for "
4424 << *this << " : [0x" << Twine::utohexstr(InRange.LowPC)
4425 << ", 0x" << Twine::utohexstr(InRange.HighPC) << "]\n");
4426 break;
4429 // Skip the block if it wasn't emitted.
4430 if (!BB.getOutputAddressRange().first)
4431 continue;
4433 // Find output address for an instruction with an offset greater or equal
4434 // to /p Offset. The output address should fall within the same basic
4435 // block boundaries.
4436 auto translateBlockOffset = [&](const uint64_t Offset) {
4437 const uint64_t OutAddress = BB.getOutputAddressRange().first + Offset;
4438 return std::min(OutAddress, BB.getOutputAddressRange().second);
4441 uint64_t OutLowPC = BB.getOutputAddressRange().first;
4442 if (InputOffset > BB.getOffset())
4443 OutLowPC = translateBlockOffset(InputOffset - BB.getOffset());
4445 uint64_t OutHighPC = BB.getOutputAddressRange().second;
4446 if (InputEndOffset < BB.getEndOffset()) {
4447 assert(InputEndOffset >= BB.getOffset());
4448 OutHighPC = translateBlockOffset(InputEndOffset - BB.getOffset());
4451 // Check if we can expand the last translated range.
4452 if (!OutRanges.empty() && OutRanges.back().HighPC == OutLowPC)
4453 OutRanges.back().HighPC = std::max(OutRanges.back().HighPC, OutHighPC);
4454 else
4455 OutRanges.emplace_back(OutLowPC, std::max(OutLowPC, OutHighPC));
4458 LLVM_DEBUG({
4459 dbgs() << "BOLT-DEBUG: translated address range " << InRange << " -> ";
4460 for (const DebugAddressRange &R : OutRanges)
4461 dbgs() << R << ' ';
4462 dbgs() << '\n';
4465 return OutRanges;
4468 MCInst *BinaryFunction::getInstructionAtOffset(uint64_t Offset) {
4469 if (CurrentState == State::Disassembled) {
4470 auto II = Instructions.find(Offset);
4471 return (II == Instructions.end()) ? nullptr : &II->second;
4472 } else if (CurrentState == State::CFG) {
4473 BinaryBasicBlock *BB = getBasicBlockContainingOffset(Offset);
4474 if (!BB)
4475 return nullptr;
4477 for (MCInst &Inst : *BB) {
4478 constexpr uint32_t InvalidOffset = std::numeric_limits<uint32_t>::max();
4479 if (Offset == BC.MIB->getOffsetWithDefault(Inst, InvalidOffset))
4480 return &Inst;
4483 if (MCInst *LastInstr = BB->getLastNonPseudoInstr()) {
4484 if (std::optional<uint32_t> Size = BC.MIB->getSize(*LastInstr)) {
4485 if (BB->getEndOffset() - Offset == Size) {
4486 return LastInstr;
4491 return nullptr;
4492 } else {
4493 llvm_unreachable("invalid CFG state to use getInstructionAtOffset()");
4497 MCInst *BinaryFunction::getInstructionContainingOffset(uint64_t Offset) {
4498 assert(CurrentState == State::Disassembled && "Wrong function state");
4500 if (Offset > Size)
4501 return nullptr;
4503 auto II = Instructions.upper_bound(Offset);
4504 assert(II != Instructions.begin() && "First instruction not at offset 0");
4505 --II;
4506 return &II->second;
4509 void BinaryFunction::printLoopInfo(raw_ostream &OS) const {
4510 if (!opts::shouldPrint(*this))
4511 return;
4513 OS << "Loop Info for Function \"" << *this << "\"";
4514 if (hasValidProfile())
4515 OS << " (count: " << getExecutionCount() << ")";
4516 OS << "\n";
4518 std::stack<BinaryLoop *> St;
4519 for (BinaryLoop *L : *BLI)
4520 St.push(L);
4521 while (!St.empty()) {
4522 BinaryLoop *L = St.top();
4523 St.pop();
4525 for (BinaryLoop *Inner : *L)
4526 St.push(Inner);
4528 if (!hasValidProfile())
4529 continue;
4531 OS << (L->getLoopDepth() > 1 ? "Nested" : "Outer")
4532 << " loop header: " << L->getHeader()->getName();
4533 OS << "\n";
4534 OS << "Loop basic blocks: ";
4535 ListSeparator LS;
4536 for (BinaryBasicBlock *BB : L->blocks())
4537 OS << LS << BB->getName();
4538 OS << "\n";
4539 if (hasValidProfile()) {
4540 OS << "Total back edge count: " << L->TotalBackEdgeCount << "\n";
4541 OS << "Loop entry count: " << L->EntryCount << "\n";
4542 OS << "Loop exit count: " << L->ExitCount << "\n";
4543 if (L->EntryCount > 0) {
4544 OS << "Average iters per entry: "
4545 << format("%.4lf", (double)L->TotalBackEdgeCount / L->EntryCount)
4546 << "\n";
4549 OS << "----\n";
4552 OS << "Total number of loops: " << BLI->TotalLoops << "\n";
4553 OS << "Number of outer loops: " << BLI->OuterLoops << "\n";
4554 OS << "Maximum nested loop depth: " << BLI->MaximumDepth << "\n\n";
4557 bool BinaryFunction::isAArch64Veneer() const {
4558 if (empty() || hasIslandsInfo())
4559 return false;
4561 BinaryBasicBlock &BB = **BasicBlocks.begin();
4562 for (MCInst &Inst : BB)
4563 if (!BC.MIB->hasAnnotation(Inst, "AArch64Veneer"))
4564 return false;
4566 for (auto I = BasicBlocks.begin() + 1, E = BasicBlocks.end(); I != E; ++I) {
4567 for (MCInst &Inst : **I)
4568 if (!BC.MIB->isNoop(Inst))
4569 return false;
4572 return true;
4575 void BinaryFunction::addRelocation(uint64_t Address, MCSymbol *Symbol,
4576 uint64_t RelType, uint64_t Addend,
4577 uint64_t Value) {
4578 assert(Address >= getAddress() && Address < getAddress() + getMaxSize() &&
4579 "address is outside of the function");
4580 uint64_t Offset = Address - getAddress();
4581 LLVM_DEBUG(dbgs() << "BOLT-DEBUG: addRelocation in "
4582 << formatv("{0}@{1:x} against {2}\n", *this, Offset,
4583 (Symbol ? Symbol->getName() : "<undef>")));
4584 bool IsCI = BC.isAArch64() && isInConstantIsland(Address);
4585 std::map<uint64_t, Relocation> &Rels =
4586 IsCI ? Islands->Relocations : Relocations;
4587 if (BC.MIB->shouldRecordCodeRelocation(RelType))
4588 Rels[Offset] = Relocation{Offset, Symbol, RelType, Addend, Value};
4591 } // namespace bolt
4592 } // namespace llvm