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