xref: /llvm-project/bolt/lib/Core/BinaryFunction.cpp (revision 3c357a49d61e4c81a1ac016502ee504521bc8dda)
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 void BinaryFunction::analyzeInstructionForFuncReference(const MCInst &Inst) {
1508   for (const MCOperand &Op : MCPlus::primeOperands(Inst)) {
1509     if (!Op.isExpr())
1510       continue;
1511     const MCExpr &Expr = *Op.getExpr();
1512     if (Expr.getKind() != MCExpr::SymbolRef)
1513       continue;
1514     const MCSymbol &Symbol = cast<MCSymbolRefExpr>(Expr).getSymbol();
1515     // Set HasAddressTaken for a function regardless of the ICF level.
1516     if (BinaryFunction *BF = BC.getFunctionForSymbol(&Symbol))
1517       BF->setHasAddressTaken(true);
1518   }
1519 }
1520 
1521 bool BinaryFunction::scanExternalRefs() {
1522   bool Success = true;
1523   bool DisassemblyFailed = false;
1524 
1525   // Ignore pseudo functions.
1526   if (isPseudo())
1527     return Success;
1528 
1529   if (opts::NoScan) {
1530     clearList(Relocations);
1531     clearList(ExternallyReferencedOffsets);
1532 
1533     return false;
1534   }
1535 
1536   // List of external references for this function.
1537   std::vector<Relocation> FunctionRelocations;
1538 
1539   static BinaryContext::IndependentCodeEmitter Emitter =
1540       BC.createIndependentMCCodeEmitter();
1541 
1542   ErrorOr<ArrayRef<uint8_t>> ErrorOrFunctionData = getData();
1543   assert(ErrorOrFunctionData && "function data is not available");
1544   ArrayRef<uint8_t> FunctionData = *ErrorOrFunctionData;
1545   assert(FunctionData.size() == getMaxSize() &&
1546          "function size does not match raw data size");
1547 
1548   BC.SymbolicDisAsm->setSymbolizer(
1549       BC.MIB->createTargetSymbolizer(*this, /*CreateSymbols*/ false));
1550 
1551   // Disassemble contents of the function. Detect code entry points and create
1552   // relocations for references to code that will be moved.
1553   uint64_t Size = 0; // instruction size
1554   for (uint64_t Offset = 0; Offset < getSize(); Offset += Size) {
1555     // Check for data inside code and ignore it
1556     if (const size_t DataInCodeSize = getSizeOfDataInCodeAt(Offset)) {
1557       Size = DataInCodeSize;
1558       continue;
1559     }
1560 
1561     const uint64_t AbsoluteInstrAddr = getAddress() + Offset;
1562     MCInst Instruction;
1563     if (!BC.SymbolicDisAsm->getInstruction(Instruction, Size,
1564                                            FunctionData.slice(Offset),
1565                                            AbsoluteInstrAddr, nulls())) {
1566       if (opts::Verbosity >= 1 && !isZeroPaddingAt(Offset)) {
1567         BC.errs()
1568             << "BOLT-WARNING: unable to disassemble instruction at offset 0x"
1569             << Twine::utohexstr(Offset) << " (address 0x"
1570             << Twine::utohexstr(AbsoluteInstrAddr) << ") in function " << *this
1571             << '\n';
1572       }
1573       Success = false;
1574       DisassemblyFailed = true;
1575       break;
1576     }
1577 
1578     // Return true if we can skip handling the Target function reference.
1579     auto ignoreFunctionRef = [&](const BinaryFunction &Target) {
1580       if (&Target == this)
1581         return true;
1582 
1583       // Note that later we may decide not to emit Target function. In that
1584       // case, we conservatively create references that will be ignored or
1585       // resolved to the same function.
1586       if (!BC.shouldEmit(Target))
1587         return true;
1588 
1589       return false;
1590     };
1591 
1592     // Return true if we can ignore reference to the symbol.
1593     auto ignoreReference = [&](const MCSymbol *TargetSymbol) {
1594       if (!TargetSymbol)
1595         return true;
1596 
1597       if (BC.forceSymbolRelocations(TargetSymbol->getName()))
1598         return false;
1599 
1600       BinaryFunction *TargetFunction = BC.getFunctionForSymbol(TargetSymbol);
1601       if (!TargetFunction)
1602         return true;
1603 
1604       return ignoreFunctionRef(*TargetFunction);
1605     };
1606 
1607     // Handle calls and branches separately as symbolization doesn't work for
1608     // them yet.
1609     MCSymbol *BranchTargetSymbol = nullptr;
1610     if (BC.MIB->isCall(Instruction) || BC.MIB->isBranch(Instruction)) {
1611       uint64_t TargetAddress = 0;
1612       BC.MIB->evaluateBranch(Instruction, AbsoluteInstrAddr, Size,
1613                              TargetAddress);
1614 
1615       // Create an entry point at reference address if needed.
1616       BinaryFunction *TargetFunction =
1617           BC.getBinaryFunctionContainingAddress(TargetAddress);
1618 
1619       if (!TargetFunction || ignoreFunctionRef(*TargetFunction))
1620         continue;
1621 
1622       const uint64_t FunctionOffset =
1623           TargetAddress - TargetFunction->getAddress();
1624       BranchTargetSymbol =
1625           FunctionOffset ? TargetFunction->addEntryPointAtOffset(FunctionOffset)
1626                          : TargetFunction->getSymbol();
1627     }
1628 
1629     // Can't find more references. Not creating relocations since we are not
1630     // moving code.
1631     if (!BC.HasRelocations)
1632       continue;
1633 
1634     if (BranchTargetSymbol) {
1635       BC.MIB->replaceBranchTarget(Instruction, BranchTargetSymbol,
1636                                   Emitter.LocalCtx.get());
1637     } else if (!llvm::any_of(Instruction,
1638                              [](const MCOperand &Op) { return Op.isExpr(); })) {
1639       // Skip assembly if the instruction may not have any symbolic operands.
1640       continue;
1641     } else {
1642       analyzeInstructionForFuncReference(Instruction);
1643     }
1644 
1645     // Emit the instruction using temp emitter and generate relocations.
1646     SmallString<256> Code;
1647     SmallVector<MCFixup, 4> Fixups;
1648     Emitter.MCE->encodeInstruction(Instruction, Code, Fixups, *BC.STI);
1649 
1650     // Create relocation for every fixup.
1651     for (const MCFixup &Fixup : Fixups) {
1652       std::optional<Relocation> Rel = BC.MIB->createRelocation(Fixup, *BC.MAB);
1653       if (!Rel) {
1654         Success = false;
1655         continue;
1656       }
1657 
1658       if (ignoreReference(Rel->Symbol))
1659         continue;
1660 
1661       if (Relocation::getSizeForType(Rel->Type) < 4) {
1662         // If the instruction uses a short form, then we might not be able
1663         // to handle the rewrite without relaxation, and hence cannot reliably
1664         // create an external reference relocation.
1665         Success = false;
1666         continue;
1667       }
1668       Rel->Offset += getAddress() - getOriginSection()->getAddress() + Offset;
1669       FunctionRelocations.push_back(*Rel);
1670     }
1671 
1672     if (!Success)
1673       break;
1674   }
1675 
1676   // Reset symbolizer for the disassembler.
1677   BC.SymbolicDisAsm->setSymbolizer(nullptr);
1678 
1679   // Add relocations unless disassembly failed for this function.
1680   if (!DisassemblyFailed)
1681     for (Relocation &Rel : FunctionRelocations)
1682       getOriginSection()->addPendingRelocation(Rel);
1683 
1684   // Inform BinaryContext that this function symbols will not be defined and
1685   // relocations should not be created against them.
1686   if (BC.HasRelocations) {
1687     for (std::pair<const uint32_t, MCSymbol *> &LI : Labels)
1688       BC.UndefinedSymbols.insert(LI.second);
1689     for (MCSymbol *const EndLabel : FunctionEndLabels)
1690       if (EndLabel)
1691         BC.UndefinedSymbols.insert(EndLabel);
1692   }
1693 
1694   clearList(Relocations);
1695   clearList(ExternallyReferencedOffsets);
1696 
1697   if (Success && BC.HasRelocations)
1698     HasExternalRefRelocations = true;
1699 
1700   if (opts::Verbosity >= 1 && !Success)
1701     BC.outs() << "BOLT-INFO: failed to scan refs for  " << *this << '\n';
1702 
1703   return Success;
1704 }
1705 
1706 void BinaryFunction::postProcessEntryPoints() {
1707   if (!isSimple())
1708     return;
1709 
1710   for (auto &KV : Labels) {
1711     MCSymbol *Label = KV.second;
1712     if (!getSecondaryEntryPointSymbol(Label))
1713       continue;
1714 
1715     // In non-relocation mode there's potentially an external undetectable
1716     // reference to the entry point and hence we cannot move this entry
1717     // point. Optimizing without moving could be difficult.
1718     // In BAT mode, register any known entry points for CFG construction.
1719     if (!BC.HasRelocations && !BC.HasBATSection)
1720       setSimple(false);
1721 
1722     const uint32_t Offset = KV.first;
1723 
1724     // If we are at Offset 0 and there is no instruction associated with it,
1725     // this means this is an empty function. Just ignore. If we find an
1726     // instruction at this offset, this entry point is valid.
1727     if (!Offset || getInstructionAtOffset(Offset))
1728       continue;
1729 
1730     // On AArch64 there are legitimate reasons to have references past the
1731     // end of the function, e.g. jump tables.
1732     if (BC.isAArch64() && Offset == getSize())
1733       continue;
1734 
1735     BC.errs() << "BOLT-WARNING: reference in the middle of instruction "
1736                  "detected in function "
1737               << *this << " at offset 0x" << Twine::utohexstr(Offset) << '\n';
1738     if (BC.HasRelocations)
1739       setIgnored();
1740     setSimple(false);
1741     return;
1742   }
1743 }
1744 
1745 void BinaryFunction::postProcessJumpTables() {
1746   // Create labels for all entries.
1747   for (auto &JTI : JumpTables) {
1748     JumpTable &JT = *JTI.second;
1749     if (JT.Type == JumpTable::JTT_PIC && opts::JumpTables == JTS_BASIC) {
1750       opts::JumpTables = JTS_MOVE;
1751       BC.outs() << "BOLT-INFO: forcing -jump-tables=move as PIC jump table was "
1752                    "detected in function "
1753                 << *this << '\n';
1754     }
1755     const uint64_t BDSize =
1756         BC.getBinaryDataAtAddress(JT.getAddress())->getSize();
1757     if (!BDSize) {
1758       BC.setBinaryDataSize(JT.getAddress(), JT.getSize());
1759     } else {
1760       assert(BDSize >= JT.getSize() &&
1761              "jump table cannot be larger than the containing object");
1762     }
1763     if (!JT.Entries.empty())
1764       continue;
1765 
1766     bool HasOneParent = (JT.Parents.size() == 1);
1767     for (uint64_t EntryAddress : JT.EntriesAsAddress) {
1768       // builtin_unreachable does not belong to any function
1769       // Need to handle separately
1770       bool IsBuiltinUnreachable =
1771           llvm::any_of(JT.Parents, [&](const BinaryFunction *Parent) {
1772             return EntryAddress == Parent->getAddress() + Parent->getSize();
1773           });
1774       if (IsBuiltinUnreachable) {
1775         MCSymbol *Label = getOrCreateLocalLabel(EntryAddress, true);
1776         JT.Entries.push_back(Label);
1777         continue;
1778       }
1779       // Create a local label for targets that cannot be reached by other
1780       // fragments. Otherwise, create a secondary entry point in the target
1781       // function.
1782       BinaryFunction *TargetBF =
1783           BC.getBinaryFunctionContainingAddress(EntryAddress);
1784       MCSymbol *Label;
1785       if (HasOneParent && TargetBF == this) {
1786         Label = getOrCreateLocalLabel(EntryAddress, true);
1787       } else {
1788         const uint64_t Offset = EntryAddress - TargetBF->getAddress();
1789         Label = Offset ? TargetBF->addEntryPointAtOffset(Offset)
1790                        : TargetBF->getSymbol();
1791       }
1792       JT.Entries.push_back(Label);
1793     }
1794   }
1795 
1796   // Add TakenBranches from JumpTables.
1797   //
1798   // We want to do it after initial processing since we don't know jump tables'
1799   // boundaries until we process them all.
1800   for (auto &JTSite : JTSites) {
1801     const uint64_t JTSiteOffset = JTSite.first;
1802     const uint64_t JTAddress = JTSite.second;
1803     const JumpTable *JT = getJumpTableContainingAddress(JTAddress);
1804     assert(JT && "cannot find jump table for address");
1805 
1806     uint64_t EntryOffset = JTAddress - JT->getAddress();
1807     while (EntryOffset < JT->getSize()) {
1808       uint64_t EntryAddress = JT->EntriesAsAddress[EntryOffset / JT->EntrySize];
1809       uint64_t TargetOffset = EntryAddress - getAddress();
1810       if (TargetOffset < getSize()) {
1811         TakenBranches.emplace_back(JTSiteOffset, TargetOffset);
1812 
1813         if (opts::StrictMode)
1814           registerReferencedOffset(TargetOffset);
1815       }
1816 
1817       EntryOffset += JT->EntrySize;
1818 
1819       // A label at the next entry means the end of this jump table.
1820       if (JT->Labels.count(EntryOffset))
1821         break;
1822     }
1823   }
1824   clearList(JTSites);
1825 
1826   // Conservatively populate all possible destinations for unknown indirect
1827   // branches.
1828   if (opts::StrictMode && hasInternalReference()) {
1829     for (uint64_t Offset : UnknownIndirectBranchOffsets) {
1830       for (uint64_t PossibleDestination : ExternallyReferencedOffsets) {
1831         // Ignore __builtin_unreachable().
1832         if (PossibleDestination == getSize())
1833           continue;
1834         TakenBranches.emplace_back(Offset, PossibleDestination);
1835       }
1836     }
1837   }
1838 }
1839 
1840 bool BinaryFunction::validateExternallyReferencedOffsets() {
1841   SmallPtrSet<MCSymbol *, 4> JTTargets;
1842   for (const JumpTable *JT : llvm::make_second_range(JumpTables))
1843     JTTargets.insert(JT->Entries.begin(), JT->Entries.end());
1844 
1845   bool HasUnclaimedReference = false;
1846   for (uint64_t Destination : ExternallyReferencedOffsets) {
1847     // Ignore __builtin_unreachable().
1848     if (Destination == getSize())
1849       continue;
1850     // Ignore constant islands
1851     if (isInConstantIsland(Destination + getAddress()))
1852       continue;
1853 
1854     if (BinaryBasicBlock *BB = getBasicBlockAtOffset(Destination)) {
1855       // Check if the externally referenced offset is a recognized jump table
1856       // target.
1857       if (JTTargets.contains(BB->getLabel()))
1858         continue;
1859 
1860       if (opts::Verbosity >= 1) {
1861         BC.errs() << "BOLT-WARNING: unclaimed data to code reference (possibly "
1862                   << "an unrecognized jump table entry) to " << BB->getName()
1863                   << " in " << *this << "\n";
1864       }
1865       auto L = BC.scopeLock();
1866       addEntryPoint(*BB);
1867     } else {
1868       BC.errs() << "BOLT-WARNING: unknown data to code reference to offset "
1869                 << Twine::utohexstr(Destination) << " in " << *this << "\n";
1870       setIgnored();
1871     }
1872     HasUnclaimedReference = true;
1873   }
1874   return !HasUnclaimedReference;
1875 }
1876 
1877 bool BinaryFunction::postProcessIndirectBranches(
1878     MCPlusBuilder::AllocatorIdTy AllocId) {
1879   auto addUnknownControlFlow = [&](BinaryBasicBlock &BB) {
1880     LLVM_DEBUG(dbgs() << "BOLT-DEBUG: adding unknown control flow in " << *this
1881                       << " for " << BB.getName() << "\n");
1882     HasUnknownControlFlow = true;
1883     BB.removeAllSuccessors();
1884     for (uint64_t PossibleDestination : ExternallyReferencedOffsets)
1885       if (BinaryBasicBlock *SuccBB = getBasicBlockAtOffset(PossibleDestination))
1886         BB.addSuccessor(SuccBB);
1887   };
1888 
1889   uint64_t NumIndirectJumps = 0;
1890   MCInst *LastIndirectJump = nullptr;
1891   BinaryBasicBlock *LastIndirectJumpBB = nullptr;
1892   uint64_t LastJT = 0;
1893   uint16_t LastJTIndexReg = BC.MIB->getNoRegister();
1894   for (BinaryBasicBlock &BB : blocks()) {
1895     for (BinaryBasicBlock::iterator II = BB.begin(); II != BB.end(); ++II) {
1896       MCInst &Instr = *II;
1897       if (!BC.MIB->isIndirectBranch(Instr))
1898         continue;
1899 
1900       // If there's an indirect branch in a single-block function -
1901       // it must be a tail call.
1902       if (BasicBlocks.size() == 1) {
1903         BC.MIB->convertJmpToTailCall(Instr);
1904         return true;
1905       }
1906 
1907       ++NumIndirectJumps;
1908 
1909       if (opts::StrictMode && !hasInternalReference()) {
1910         BC.MIB->convertJmpToTailCall(Instr);
1911         break;
1912       }
1913 
1914       // Validate the tail call or jump table assumptions now that we know
1915       // basic block boundaries.
1916       if (BC.MIB->isTailCall(Instr) || BC.MIB->getJumpTable(Instr)) {
1917         const unsigned PtrSize = BC.AsmInfo->getCodePointerSize();
1918         MCInst *MemLocInstr;
1919         unsigned BaseRegNum, IndexRegNum;
1920         int64_t DispValue;
1921         const MCExpr *DispExpr;
1922         MCInst *PCRelBaseInstr;
1923         MCInst *FixedEntryLoadInstr;
1924         IndirectBranchType Type = BC.MIB->analyzeIndirectBranch(
1925             Instr, BB.begin(), II, PtrSize, MemLocInstr, BaseRegNum,
1926             IndexRegNum, DispValue, DispExpr, PCRelBaseInstr,
1927             FixedEntryLoadInstr);
1928         if (Type != IndirectBranchType::UNKNOWN || MemLocInstr != nullptr)
1929           continue;
1930 
1931         if (!opts::StrictMode)
1932           return false;
1933 
1934         if (BC.MIB->isTailCall(Instr)) {
1935           BC.MIB->convertTailCallToJmp(Instr);
1936         } else {
1937           LastIndirectJump = &Instr;
1938           LastIndirectJumpBB = &BB;
1939           LastJT = BC.MIB->getJumpTable(Instr);
1940           LastJTIndexReg = BC.MIB->getJumpTableIndexReg(Instr);
1941           BC.MIB->unsetJumpTable(Instr);
1942 
1943           JumpTable *JT = BC.getJumpTableContainingAddress(LastJT);
1944           if (JT->Type == JumpTable::JTT_NORMAL) {
1945             // Invalidating the jump table may also invalidate other jump table
1946             // boundaries. Until we have/need a support for this, mark the
1947             // function as non-simple.
1948             LLVM_DEBUG(dbgs() << "BOLT-DEBUG: rejected jump table reference"
1949                               << JT->getName() << " in " << *this << '\n');
1950             return false;
1951           }
1952         }
1953 
1954         addUnknownControlFlow(BB);
1955         continue;
1956       }
1957 
1958       // If this block contains an epilogue code and has an indirect branch,
1959       // then most likely it's a tail call. Otherwise, we cannot tell for sure
1960       // what it is and conservatively reject the function's CFG.
1961       bool IsEpilogue = llvm::any_of(BB, [&](const MCInst &Instr) {
1962         return BC.MIB->isLeave(Instr) || BC.MIB->isPop(Instr);
1963       });
1964       if (IsEpilogue) {
1965         BC.MIB->convertJmpToTailCall(Instr);
1966         BB.removeAllSuccessors();
1967         continue;
1968       }
1969 
1970       if (opts::Verbosity >= 2) {
1971         BC.outs() << "BOLT-INFO: rejected potential indirect tail call in "
1972                   << "function " << *this << " in basic block " << BB.getName()
1973                   << ".\n";
1974         LLVM_DEBUG(BC.printInstructions(dbgs(), BB.begin(), BB.end(),
1975                                         BB.getOffset(), this, true));
1976       }
1977 
1978       if (!opts::StrictMode)
1979         return false;
1980 
1981       addUnknownControlFlow(BB);
1982     }
1983   }
1984 
1985   if (HasInternalLabelReference)
1986     return false;
1987 
1988   // If there's only one jump table, and one indirect jump, and no other
1989   // references, then we should be able to derive the jump table even if we
1990   // fail to match the pattern.
1991   if (HasUnknownControlFlow && NumIndirectJumps == 1 &&
1992       JumpTables.size() == 1 && LastIndirectJump &&
1993       !BC.getJumpTableContainingAddress(LastJT)->IsSplit) {
1994     LLVM_DEBUG(dbgs() << "BOLT-DEBUG: unsetting unknown control flow in "
1995                       << *this << '\n');
1996     BC.MIB->setJumpTable(*LastIndirectJump, LastJT, LastJTIndexReg, AllocId);
1997     HasUnknownControlFlow = false;
1998 
1999     LastIndirectJumpBB->updateJumpTableSuccessors();
2000   }
2001 
2002   // Validate that all data references to function offsets are claimed by
2003   // recognized jump tables. Register externally referenced blocks as entry
2004   // points.
2005   if (!opts::StrictMode && hasInternalReference()) {
2006     if (!validateExternallyReferencedOffsets())
2007       return false;
2008   }
2009 
2010   if (HasUnknownControlFlow && !BC.HasRelocations)
2011     return false;
2012 
2013   return true;
2014 }
2015 
2016 void BinaryFunction::recomputeLandingPads() {
2017   updateBBIndices(0);
2018 
2019   for (BinaryBasicBlock *BB : BasicBlocks) {
2020     BB->LandingPads.clear();
2021     BB->Throwers.clear();
2022   }
2023 
2024   for (BinaryBasicBlock *BB : BasicBlocks) {
2025     std::unordered_set<const BinaryBasicBlock *> BBLandingPads;
2026     for (MCInst &Instr : *BB) {
2027       if (!BC.MIB->isInvoke(Instr))
2028         continue;
2029 
2030       const std::optional<MCPlus::MCLandingPad> EHInfo =
2031           BC.MIB->getEHInfo(Instr);
2032       if (!EHInfo || !EHInfo->first)
2033         continue;
2034 
2035       BinaryBasicBlock *LPBlock = getBasicBlockForLabel(EHInfo->first);
2036       if (!BBLandingPads.count(LPBlock)) {
2037         BBLandingPads.insert(LPBlock);
2038         BB->LandingPads.emplace_back(LPBlock);
2039         LPBlock->Throwers.emplace_back(BB);
2040       }
2041     }
2042   }
2043 }
2044 
2045 Error BinaryFunction::buildCFG(MCPlusBuilder::AllocatorIdTy AllocatorId) {
2046   auto &MIB = BC.MIB;
2047 
2048   if (!isSimple()) {
2049     assert(!BC.HasRelocations &&
2050            "cannot process file with non-simple function in relocs mode");
2051     return createNonFatalBOLTError("");
2052   }
2053 
2054   if (CurrentState != State::Disassembled)
2055     return createNonFatalBOLTError("");
2056 
2057   assert(BasicBlocks.empty() && "basic block list should be empty");
2058   assert((Labels.find(getFirstInstructionOffset()) != Labels.end()) &&
2059          "first instruction should always have a label");
2060 
2061   // Create basic blocks in the original layout order:
2062   //
2063   //  * Every instruction with associated label marks
2064   //    the beginning of a basic block.
2065   //  * Conditional instruction marks the end of a basic block,
2066   //    except when the following instruction is an
2067   //    unconditional branch, and the unconditional branch is not
2068   //    a destination of another branch. In the latter case, the
2069   //    basic block will consist of a single unconditional branch
2070   //    (missed "double-jump" optimization).
2071   //
2072   // Created basic blocks are sorted in layout order since they are
2073   // created in the same order as instructions, and instructions are
2074   // sorted by offsets.
2075   BinaryBasicBlock *InsertBB = nullptr;
2076   BinaryBasicBlock *PrevBB = nullptr;
2077   bool IsLastInstrNop = false;
2078   // Offset of the last non-nop instruction.
2079   uint64_t LastInstrOffset = 0;
2080 
2081   auto addCFIPlaceholders = [this](uint64_t CFIOffset,
2082                                    BinaryBasicBlock *InsertBB) {
2083     for (auto FI = OffsetToCFI.lower_bound(CFIOffset),
2084               FE = OffsetToCFI.upper_bound(CFIOffset);
2085          FI != FE; ++FI) {
2086       addCFIPseudo(InsertBB, InsertBB->end(), FI->second);
2087     }
2088   };
2089 
2090   // For profiling purposes we need to save the offset of the last instruction
2091   // in the basic block.
2092   // NOTE: nops always have an Offset annotation. Annotate the last non-nop as
2093   //       older profiles ignored nops.
2094   auto updateOffset = [&](uint64_t Offset) {
2095     assert(PrevBB && PrevBB != InsertBB && "invalid previous block");
2096     MCInst *LastNonNop = nullptr;
2097     for (BinaryBasicBlock::reverse_iterator RII = PrevBB->getLastNonPseudo(),
2098                                             E = PrevBB->rend();
2099          RII != E; ++RII) {
2100       if (!BC.MIB->isPseudo(*RII) && !BC.MIB->isNoop(*RII)) {
2101         LastNonNop = &*RII;
2102         break;
2103       }
2104     }
2105     if (LastNonNop && !MIB->getOffset(*LastNonNop))
2106       MIB->setOffset(*LastNonNop, static_cast<uint32_t>(Offset));
2107   };
2108 
2109   for (auto I = Instructions.begin(), E = Instructions.end(); I != E; ++I) {
2110     const uint32_t Offset = I->first;
2111     MCInst &Instr = I->second;
2112 
2113     auto LI = Labels.find(Offset);
2114     if (LI != Labels.end()) {
2115       // Always create new BB at branch destination.
2116       PrevBB = InsertBB ? InsertBB : PrevBB;
2117       InsertBB = addBasicBlockAt(LI->first, LI->second);
2118       if (opts::PreserveBlocksAlignment && IsLastInstrNop)
2119         InsertBB->setDerivedAlignment();
2120 
2121       if (PrevBB)
2122         updateOffset(LastInstrOffset);
2123     }
2124 
2125     // Mark all nops with Offset for profile tracking purposes.
2126     if (MIB->isNoop(Instr) && !MIB->getOffset(Instr)) {
2127       // If "Offset" annotation is not present, set it and mark the nop for
2128       // deletion.
2129       MIB->setOffset(Instr, static_cast<uint32_t>(Offset));
2130       // Annotate ordinary nops, so we can safely delete them if required.
2131       MIB->addAnnotation(Instr, "NOP", static_cast<uint32_t>(1), AllocatorId);
2132     }
2133 
2134     if (!InsertBB) {
2135       // It must be a fallthrough or unreachable code. Create a new block unless
2136       // we see an unconditional branch following a conditional one. The latter
2137       // should not be a conditional tail call.
2138       assert(PrevBB && "no previous basic block for a fall through");
2139       MCInst *PrevInstr = PrevBB->getLastNonPseudoInstr();
2140       assert(PrevInstr && "no previous instruction for a fall through");
2141       if (MIB->isUnconditionalBranch(Instr) &&
2142           !MIB->isIndirectBranch(*PrevInstr) &&
2143           !MIB->isUnconditionalBranch(*PrevInstr) &&
2144           !MIB->getConditionalTailCall(*PrevInstr) &&
2145           !MIB->isReturn(*PrevInstr)) {
2146         // Temporarily restore inserter basic block.
2147         InsertBB = PrevBB;
2148       } else {
2149         MCSymbol *Label;
2150         {
2151           auto L = BC.scopeLock();
2152           Label = BC.Ctx->createNamedTempSymbol("FT");
2153         }
2154         InsertBB = addBasicBlockAt(Offset, Label);
2155         if (opts::PreserveBlocksAlignment && IsLastInstrNop)
2156           InsertBB->setDerivedAlignment();
2157         updateOffset(LastInstrOffset);
2158       }
2159     }
2160     if (Offset == getFirstInstructionOffset()) {
2161       // Add associated CFI pseudos in the first offset
2162       addCFIPlaceholders(Offset, InsertBB);
2163     }
2164 
2165     const bool IsBlockEnd = MIB->isTerminator(Instr);
2166     IsLastInstrNop = MIB->isNoop(Instr);
2167     if (!IsLastInstrNop)
2168       LastInstrOffset = Offset;
2169     InsertBB->addInstruction(std::move(Instr));
2170 
2171     // Add associated CFI instrs. We always add the CFI instruction that is
2172     // located immediately after this instruction, since the next CFI
2173     // instruction reflects the change in state caused by this instruction.
2174     auto NextInstr = std::next(I);
2175     uint64_t CFIOffset;
2176     if (NextInstr != E)
2177       CFIOffset = NextInstr->first;
2178     else
2179       CFIOffset = getSize();
2180 
2181     // Note: this potentially invalidates instruction pointers/iterators.
2182     addCFIPlaceholders(CFIOffset, InsertBB);
2183 
2184     if (IsBlockEnd) {
2185       PrevBB = InsertBB;
2186       InsertBB = nullptr;
2187     }
2188   }
2189 
2190   if (BasicBlocks.empty()) {
2191     setSimple(false);
2192     return createNonFatalBOLTError("");
2193   }
2194 
2195   // Intermediate dump.
2196   LLVM_DEBUG(print(dbgs(), "after creating basic blocks"));
2197 
2198   // TODO: handle properly calls to no-return functions,
2199   // e.g. exit(3), etc. Otherwise we'll see a false fall-through
2200   // blocks.
2201 
2202   // Remove duplicates branches. We can get a bunch of them from jump tables.
2203   // Without doing jump table value profiling we don't have a use for extra
2204   // (duplicate) branches.
2205   llvm::sort(TakenBranches);
2206   auto NewEnd = std::unique(TakenBranches.begin(), TakenBranches.end());
2207   TakenBranches.erase(NewEnd, TakenBranches.end());
2208 
2209   for (std::pair<uint32_t, uint32_t> &Branch : TakenBranches) {
2210     LLVM_DEBUG(dbgs() << "registering branch [0x"
2211                       << Twine::utohexstr(Branch.first) << "] -> [0x"
2212                       << Twine::utohexstr(Branch.second) << "]\n");
2213     BinaryBasicBlock *FromBB = getBasicBlockContainingOffset(Branch.first);
2214     BinaryBasicBlock *ToBB = getBasicBlockAtOffset(Branch.second);
2215     if (!FromBB || !ToBB) {
2216       if (!FromBB)
2217         BC.errs() << "BOLT-ERROR: cannot find BB containing the branch.\n";
2218       if (!ToBB)
2219         BC.errs()
2220             << "BOLT-ERROR: cannot find BB containing branch destination.\n";
2221       return createFatalBOLTError(BC.generateBugReportMessage(
2222           "disassembly failed - inconsistent branch found.", *this));
2223     }
2224 
2225     FromBB->addSuccessor(ToBB);
2226   }
2227 
2228   // Add fall-through branches.
2229   PrevBB = nullptr;
2230   bool IsPrevFT = false; // Is previous block a fall-through.
2231   for (BinaryBasicBlock *BB : BasicBlocks) {
2232     if (IsPrevFT)
2233       PrevBB->addSuccessor(BB);
2234 
2235     if (BB->empty()) {
2236       IsPrevFT = true;
2237       PrevBB = BB;
2238       continue;
2239     }
2240 
2241     MCInst *LastInstr = BB->getLastNonPseudoInstr();
2242     assert(LastInstr &&
2243            "should have non-pseudo instruction in non-empty block");
2244 
2245     if (BB->succ_size() == 0) {
2246       // Since there's no existing successors, we know the last instruction is
2247       // not a conditional branch. Thus if it's a terminator, it shouldn't be a
2248       // fall-through.
2249       //
2250       // Conditional tail call is a special case since we don't add a taken
2251       // branch successor for it.
2252       IsPrevFT = !MIB->isTerminator(*LastInstr) ||
2253                  MIB->getConditionalTailCall(*LastInstr);
2254     } else if (BB->succ_size() == 1) {
2255       IsPrevFT = MIB->isConditionalBranch(*LastInstr);
2256     } else {
2257       IsPrevFT = false;
2258     }
2259 
2260     PrevBB = BB;
2261   }
2262 
2263   // Assign landing pads and throwers info.
2264   recomputeLandingPads();
2265 
2266   // Assign CFI information to each BB entry.
2267   annotateCFIState();
2268 
2269   // Annotate invoke instructions with GNU_args_size data.
2270   propagateGnuArgsSizeInfo(AllocatorId);
2271 
2272   // Set the basic block layout to the original order and set end offsets.
2273   PrevBB = nullptr;
2274   for (BinaryBasicBlock *BB : BasicBlocks) {
2275     Layout.addBasicBlock(BB);
2276     if (PrevBB)
2277       PrevBB->setEndOffset(BB->getOffset());
2278     PrevBB = BB;
2279   }
2280   PrevBB->setEndOffset(getSize());
2281 
2282   Layout.updateLayoutIndices();
2283 
2284   normalizeCFIState();
2285 
2286   // Clean-up memory taken by intermediate structures.
2287   //
2288   // NB: don't clear Labels list as we may need them if we mark the function
2289   //     as non-simple later in the process of discovering extra entry points.
2290   clearList(Instructions);
2291   clearList(OffsetToCFI);
2292   clearList(TakenBranches);
2293 
2294   // Update the state.
2295   CurrentState = State::CFG;
2296 
2297   // Make any necessary adjustments for indirect branches.
2298   if (!postProcessIndirectBranches(AllocatorId)) {
2299     if (opts::Verbosity) {
2300       BC.errs() << "BOLT-WARNING: failed to post-process indirect branches for "
2301                 << *this << '\n';
2302     }
2303     // In relocation mode we want to keep processing the function but avoid
2304     // optimizing it.
2305     setSimple(false);
2306   }
2307 
2308   clearList(ExternallyReferencedOffsets);
2309   clearList(UnknownIndirectBranchOffsets);
2310 
2311   return Error::success();
2312 }
2313 
2314 void BinaryFunction::postProcessCFG() {
2315   if (isSimple() && !BasicBlocks.empty()) {
2316     // Convert conditional tail call branches to conditional branches that jump
2317     // to a tail call.
2318     removeConditionalTailCalls();
2319 
2320     postProcessProfile();
2321 
2322     // Eliminate inconsistencies between branch instructions and CFG.
2323     postProcessBranches();
2324   }
2325 
2326   // The final cleanup of intermediate structures.
2327   clearList(IgnoredBranches);
2328 
2329   // Remove "Offset" annotations, unless we need an address-translation table
2330   // later. This has no cost, since annotations are allocated by a bumpptr
2331   // allocator and won't be released anyway until late in the pipeline.
2332   if (!requiresAddressTranslation() && !opts::Instrument) {
2333     for (BinaryBasicBlock &BB : blocks())
2334       for (MCInst &Inst : BB)
2335         BC.MIB->clearOffset(Inst);
2336   }
2337 
2338   assert((!isSimple() || validateCFG()) &&
2339          "invalid CFG detected after post-processing");
2340 }
2341 
2342 void BinaryFunction::removeTagsFromProfile() {
2343   for (BinaryBasicBlock *BB : BasicBlocks) {
2344     if (BB->ExecutionCount == BinaryBasicBlock::COUNT_NO_PROFILE)
2345       BB->ExecutionCount = 0;
2346     for (BinaryBasicBlock::BinaryBranchInfo &BI : BB->branch_info()) {
2347       if (BI.Count != BinaryBasicBlock::COUNT_NO_PROFILE &&
2348           BI.MispredictedCount != BinaryBasicBlock::COUNT_NO_PROFILE)
2349         continue;
2350       BI.Count = 0;
2351       BI.MispredictedCount = 0;
2352     }
2353   }
2354 }
2355 
2356 void BinaryFunction::removeConditionalTailCalls() {
2357   // Blocks to be appended at the end.
2358   std::vector<std::unique_ptr<BinaryBasicBlock>> NewBlocks;
2359 
2360   for (auto BBI = begin(); BBI != end(); ++BBI) {
2361     BinaryBasicBlock &BB = *BBI;
2362     MCInst *CTCInstr = BB.getLastNonPseudoInstr();
2363     if (!CTCInstr)
2364       continue;
2365 
2366     std::optional<uint64_t> TargetAddressOrNone =
2367         BC.MIB->getConditionalTailCall(*CTCInstr);
2368     if (!TargetAddressOrNone)
2369       continue;
2370 
2371     // Gather all necessary information about CTC instruction before
2372     // annotations are destroyed.
2373     const int32_t CFIStateBeforeCTC = BB.getCFIStateAtInstr(CTCInstr);
2374     uint64_t CTCTakenCount = BinaryBasicBlock::COUNT_NO_PROFILE;
2375     uint64_t CTCMispredCount = BinaryBasicBlock::COUNT_NO_PROFILE;
2376     if (hasValidProfile()) {
2377       CTCTakenCount = BC.MIB->getAnnotationWithDefault<uint64_t>(
2378           *CTCInstr, "CTCTakenCount");
2379       CTCMispredCount = BC.MIB->getAnnotationWithDefault<uint64_t>(
2380           *CTCInstr, "CTCMispredCount");
2381     }
2382 
2383     // Assert that the tail call does not throw.
2384     assert(!BC.MIB->getEHInfo(*CTCInstr) &&
2385            "found tail call with associated landing pad");
2386 
2387     // Create a basic block with an unconditional tail call instruction using
2388     // the same destination.
2389     const MCSymbol *CTCTargetLabel = BC.MIB->getTargetSymbol(*CTCInstr);
2390     assert(CTCTargetLabel && "symbol expected for conditional tail call");
2391     MCInst TailCallInstr;
2392     BC.MIB->createTailCall(TailCallInstr, CTCTargetLabel, BC.Ctx.get());
2393 
2394     // Move offset from CTCInstr to TailCallInstr.
2395     if (const std::optional<uint32_t> Offset = BC.MIB->getOffset(*CTCInstr)) {
2396       BC.MIB->setOffset(TailCallInstr, *Offset);
2397       BC.MIB->clearOffset(*CTCInstr);
2398     }
2399 
2400     // Link new BBs to the original input offset of the BB where the CTC
2401     // is, so we can map samples recorded in new BBs back to the original BB
2402     // seem in the input binary (if using BAT)
2403     std::unique_ptr<BinaryBasicBlock> TailCallBB =
2404         createBasicBlock(BC.Ctx->createNamedTempSymbol("TC"));
2405     TailCallBB->setOffset(BB.getInputOffset());
2406     TailCallBB->addInstruction(TailCallInstr);
2407     TailCallBB->setCFIState(CFIStateBeforeCTC);
2408 
2409     // Add CFG edge with profile info from BB to TailCallBB.
2410     BB.addSuccessor(TailCallBB.get(), CTCTakenCount, CTCMispredCount);
2411 
2412     // Add execution count for the block.
2413     TailCallBB->setExecutionCount(CTCTakenCount);
2414 
2415     BC.MIB->convertTailCallToJmp(*CTCInstr);
2416 
2417     BC.MIB->replaceBranchTarget(*CTCInstr, TailCallBB->getLabel(),
2418                                 BC.Ctx.get());
2419 
2420     // Add basic block to the list that will be added to the end.
2421     NewBlocks.emplace_back(std::move(TailCallBB));
2422 
2423     // Swap edges as the TailCallBB corresponds to the taken branch.
2424     BB.swapConditionalSuccessors();
2425 
2426     // This branch is no longer a conditional tail call.
2427     BC.MIB->unsetConditionalTailCall(*CTCInstr);
2428   }
2429 
2430   insertBasicBlocks(std::prev(end()), std::move(NewBlocks),
2431                     /* UpdateLayout */ true,
2432                     /* UpdateCFIState */ false);
2433 }
2434 
2435 uint64_t BinaryFunction::getFunctionScore() const {
2436   if (FunctionScore != -1)
2437     return FunctionScore;
2438 
2439   if (!isSimple() || !hasValidProfile()) {
2440     FunctionScore = 0;
2441     return FunctionScore;
2442   }
2443 
2444   uint64_t TotalScore = 0ULL;
2445   for (const BinaryBasicBlock &BB : blocks()) {
2446     uint64_t BBExecCount = BB.getExecutionCount();
2447     if (BBExecCount == BinaryBasicBlock::COUNT_NO_PROFILE)
2448       continue;
2449     TotalScore += BBExecCount * BB.getNumNonPseudos();
2450   }
2451   FunctionScore = TotalScore;
2452   return FunctionScore;
2453 }
2454 
2455 void BinaryFunction::annotateCFIState() {
2456   assert(CurrentState == State::Disassembled && "unexpected function state");
2457   assert(!BasicBlocks.empty() && "basic block list should not be empty");
2458 
2459   // This is an index of the last processed CFI in FDE CFI program.
2460   uint32_t State = 0;
2461 
2462   // This is an index of RememberState CFI reflecting effective state right
2463   // after execution of RestoreState CFI.
2464   //
2465   // It differs from State iff the CFI at (State-1)
2466   // was RestoreState (modulo GNU_args_size CFIs, which are ignored).
2467   //
2468   // This allows us to generate shorter replay sequences when producing new
2469   // CFI programs.
2470   uint32_t EffectiveState = 0;
2471 
2472   // For tracking RememberState/RestoreState sequences.
2473   std::stack<uint32_t> StateStack;
2474 
2475   for (BinaryBasicBlock *BB : BasicBlocks) {
2476     BB->setCFIState(EffectiveState);
2477 
2478     for (const MCInst &Instr : *BB) {
2479       const MCCFIInstruction *CFI = getCFIFor(Instr);
2480       if (!CFI)
2481         continue;
2482 
2483       ++State;
2484 
2485       switch (CFI->getOperation()) {
2486       case MCCFIInstruction::OpRememberState:
2487         StateStack.push(EffectiveState);
2488         EffectiveState = State;
2489         break;
2490       case MCCFIInstruction::OpRestoreState:
2491         assert(!StateStack.empty() && "corrupt CFI stack");
2492         EffectiveState = StateStack.top();
2493         StateStack.pop();
2494         break;
2495       case MCCFIInstruction::OpGnuArgsSize:
2496         // OpGnuArgsSize CFIs do not affect the CFI state.
2497         break;
2498       default:
2499         // Any other CFI updates the state.
2500         EffectiveState = State;
2501         break;
2502       }
2503     }
2504   }
2505 
2506   if (opts::Verbosity >= 1 && !StateStack.empty()) {
2507     BC.errs() << "BOLT-WARNING: non-empty CFI stack at the end of " << *this
2508               << '\n';
2509   }
2510 }
2511 
2512 namespace {
2513 
2514 /// Our full interpretation of a DWARF CFI machine state at a given point
2515 struct CFISnapshot {
2516   /// CFA register number and offset defining the canonical frame at this
2517   /// point, or the number of a rule (CFI state) that computes it with a
2518   /// DWARF expression. This number will be negative if it refers to a CFI
2519   /// located in the CIE instead of the FDE.
2520   uint32_t CFAReg;
2521   int32_t CFAOffset;
2522   int32_t CFARule;
2523   /// Mapping of rules (CFI states) that define the location of each
2524   /// register. If absent, no rule defining the location of such register
2525   /// was ever read. This number will be negative if it refers to a CFI
2526   /// located in the CIE instead of the FDE.
2527   DenseMap<int32_t, int32_t> RegRule;
2528 
2529   /// References to CIE, FDE and expanded instructions after a restore state
2530   const BinaryFunction::CFIInstrMapType &CIE;
2531   const BinaryFunction::CFIInstrMapType &FDE;
2532   const DenseMap<int32_t, SmallVector<int32_t, 4>> &FrameRestoreEquivalents;
2533 
2534   /// Current FDE CFI number representing the state where the snapshot is at
2535   int32_t CurState;
2536 
2537   /// Used when we don't have information about which state/rule to apply
2538   /// to recover the location of either the CFA or a specific register
2539   constexpr static int32_t UNKNOWN = std::numeric_limits<int32_t>::min();
2540 
2541 private:
2542   /// Update our snapshot by executing a single CFI
2543   void update(const MCCFIInstruction &Instr, int32_t RuleNumber) {
2544     switch (Instr.getOperation()) {
2545     case MCCFIInstruction::OpSameValue:
2546     case MCCFIInstruction::OpRelOffset:
2547     case MCCFIInstruction::OpOffset:
2548     case MCCFIInstruction::OpRestore:
2549     case MCCFIInstruction::OpUndefined:
2550     case MCCFIInstruction::OpRegister:
2551       RegRule[Instr.getRegister()] = RuleNumber;
2552       break;
2553     case MCCFIInstruction::OpDefCfaRegister:
2554       CFAReg = Instr.getRegister();
2555       CFARule = UNKNOWN;
2556 
2557       // This shouldn't happen according to the spec but GNU binutils on RISC-V
2558       // emits a DW_CFA_def_cfa_register in CIE's which leaves the offset
2559       // unspecified. Both readelf and llvm-dwarfdump interpret the offset as 0
2560       // in this case so let's do the same.
2561       if (CFAOffset == UNKNOWN)
2562         CFAOffset = 0;
2563       break;
2564     case MCCFIInstruction::OpDefCfaOffset:
2565       CFAOffset = Instr.getOffset();
2566       CFARule = UNKNOWN;
2567       break;
2568     case MCCFIInstruction::OpDefCfa:
2569       CFAReg = Instr.getRegister();
2570       CFAOffset = Instr.getOffset();
2571       CFARule = UNKNOWN;
2572       break;
2573     case MCCFIInstruction::OpEscape: {
2574       std::optional<uint8_t> Reg =
2575           readDWARFExpressionTargetReg(Instr.getValues());
2576       // Handle DW_CFA_def_cfa_expression
2577       if (!Reg) {
2578         CFARule = RuleNumber;
2579         break;
2580       }
2581       RegRule[*Reg] = RuleNumber;
2582       break;
2583     }
2584     case MCCFIInstruction::OpAdjustCfaOffset:
2585     case MCCFIInstruction::OpWindowSave:
2586     case MCCFIInstruction::OpNegateRAState:
2587     case MCCFIInstruction::OpNegateRAStateWithPC:
2588     case MCCFIInstruction::OpLLVMDefAspaceCfa:
2589     case MCCFIInstruction::OpLabel:
2590     case MCCFIInstruction::OpValOffset:
2591       llvm_unreachable("unsupported CFI opcode");
2592       break;
2593     case MCCFIInstruction::OpRememberState:
2594     case MCCFIInstruction::OpRestoreState:
2595     case MCCFIInstruction::OpGnuArgsSize:
2596       // do not affect CFI state
2597       break;
2598     }
2599   }
2600 
2601 public:
2602   /// Advance state reading FDE CFI instructions up to State number
2603   void advanceTo(int32_t State) {
2604     for (int32_t I = CurState, E = State; I != E; ++I) {
2605       const MCCFIInstruction &Instr = FDE[I];
2606       if (Instr.getOperation() != MCCFIInstruction::OpRestoreState) {
2607         update(Instr, I);
2608         continue;
2609       }
2610       // If restore state instruction, fetch the equivalent CFIs that have
2611       // the same effect of this restore. This is used to ensure remember-
2612       // restore pairs are completely removed.
2613       auto Iter = FrameRestoreEquivalents.find(I);
2614       if (Iter == FrameRestoreEquivalents.end())
2615         continue;
2616       for (int32_t RuleNumber : Iter->second)
2617         update(FDE[RuleNumber], RuleNumber);
2618     }
2619 
2620     assert(((CFAReg != (uint32_t)UNKNOWN && CFAOffset != UNKNOWN) ||
2621             CFARule != UNKNOWN) &&
2622            "CIE did not define default CFA?");
2623 
2624     CurState = State;
2625   }
2626 
2627   /// Interpret all CIE and FDE instructions up until CFI State number and
2628   /// populate this snapshot
2629   CFISnapshot(
2630       const BinaryFunction::CFIInstrMapType &CIE,
2631       const BinaryFunction::CFIInstrMapType &FDE,
2632       const DenseMap<int32_t, SmallVector<int32_t, 4>> &FrameRestoreEquivalents,
2633       int32_t State)
2634       : CIE(CIE), FDE(FDE), FrameRestoreEquivalents(FrameRestoreEquivalents) {
2635     CFAReg = UNKNOWN;
2636     CFAOffset = UNKNOWN;
2637     CFARule = UNKNOWN;
2638     CurState = 0;
2639 
2640     for (int32_t I = 0, E = CIE.size(); I != E; ++I) {
2641       const MCCFIInstruction &Instr = CIE[I];
2642       update(Instr, -I);
2643     }
2644 
2645     advanceTo(State);
2646   }
2647 };
2648 
2649 /// A CFI snapshot with the capability of checking if incremental additions to
2650 /// it are redundant. This is used to ensure we do not emit two CFI instructions
2651 /// back-to-back that are doing the same state change, or to avoid emitting a
2652 /// CFI at all when the state at that point would not be modified after that CFI
2653 struct CFISnapshotDiff : public CFISnapshot {
2654   bool RestoredCFAReg{false};
2655   bool RestoredCFAOffset{false};
2656   DenseMap<int32_t, bool> RestoredRegs;
2657 
2658   CFISnapshotDiff(const CFISnapshot &S) : CFISnapshot(S) {}
2659 
2660   CFISnapshotDiff(
2661       const BinaryFunction::CFIInstrMapType &CIE,
2662       const BinaryFunction::CFIInstrMapType &FDE,
2663       const DenseMap<int32_t, SmallVector<int32_t, 4>> &FrameRestoreEquivalents,
2664       int32_t State)
2665       : CFISnapshot(CIE, FDE, FrameRestoreEquivalents, State) {}
2666 
2667   /// Return true if applying Instr to this state is redundant and can be
2668   /// dismissed.
2669   bool isRedundant(const MCCFIInstruction &Instr) {
2670     switch (Instr.getOperation()) {
2671     case MCCFIInstruction::OpSameValue:
2672     case MCCFIInstruction::OpRelOffset:
2673     case MCCFIInstruction::OpOffset:
2674     case MCCFIInstruction::OpRestore:
2675     case MCCFIInstruction::OpUndefined:
2676     case MCCFIInstruction::OpRegister:
2677     case MCCFIInstruction::OpEscape: {
2678       uint32_t Reg;
2679       if (Instr.getOperation() != MCCFIInstruction::OpEscape) {
2680         Reg = Instr.getRegister();
2681       } else {
2682         std::optional<uint8_t> R =
2683             readDWARFExpressionTargetReg(Instr.getValues());
2684         // Handle DW_CFA_def_cfa_expression
2685         if (!R) {
2686           if (RestoredCFAReg && RestoredCFAOffset)
2687             return true;
2688           RestoredCFAReg = true;
2689           RestoredCFAOffset = true;
2690           return false;
2691         }
2692         Reg = *R;
2693       }
2694       if (RestoredRegs[Reg])
2695         return true;
2696       RestoredRegs[Reg] = true;
2697       const int32_t CurRegRule = RegRule.contains(Reg) ? RegRule[Reg] : UNKNOWN;
2698       if (CurRegRule == UNKNOWN) {
2699         if (Instr.getOperation() == MCCFIInstruction::OpRestore ||
2700             Instr.getOperation() == MCCFIInstruction::OpSameValue)
2701           return true;
2702         return false;
2703       }
2704       const MCCFIInstruction &LastDef =
2705           CurRegRule < 0 ? CIE[-CurRegRule] : FDE[CurRegRule];
2706       return LastDef == Instr;
2707     }
2708     case MCCFIInstruction::OpDefCfaRegister:
2709       if (RestoredCFAReg)
2710         return true;
2711       RestoredCFAReg = true;
2712       return CFAReg == Instr.getRegister();
2713     case MCCFIInstruction::OpDefCfaOffset:
2714       if (RestoredCFAOffset)
2715         return true;
2716       RestoredCFAOffset = true;
2717       return CFAOffset == Instr.getOffset();
2718     case MCCFIInstruction::OpDefCfa:
2719       if (RestoredCFAReg && RestoredCFAOffset)
2720         return true;
2721       RestoredCFAReg = true;
2722       RestoredCFAOffset = true;
2723       return CFAReg == Instr.getRegister() && CFAOffset == Instr.getOffset();
2724     case MCCFIInstruction::OpAdjustCfaOffset:
2725     case MCCFIInstruction::OpWindowSave:
2726     case MCCFIInstruction::OpNegateRAState:
2727     case MCCFIInstruction::OpNegateRAStateWithPC:
2728     case MCCFIInstruction::OpLLVMDefAspaceCfa:
2729     case MCCFIInstruction::OpLabel:
2730     case MCCFIInstruction::OpValOffset:
2731       llvm_unreachable("unsupported CFI opcode");
2732       return false;
2733     case MCCFIInstruction::OpRememberState:
2734     case MCCFIInstruction::OpRestoreState:
2735     case MCCFIInstruction::OpGnuArgsSize:
2736       // do not affect CFI state
2737       return true;
2738     }
2739     return false;
2740   }
2741 };
2742 
2743 } // end anonymous namespace
2744 
2745 bool BinaryFunction::replayCFIInstrs(int32_t FromState, int32_t ToState,
2746                                      BinaryBasicBlock *InBB,
2747                                      BinaryBasicBlock::iterator InsertIt) {
2748   if (FromState == ToState)
2749     return true;
2750   assert(FromState < ToState && "can only replay CFIs forward");
2751 
2752   CFISnapshotDiff CFIDiff(CIEFrameInstructions, FrameInstructions,
2753                           FrameRestoreEquivalents, FromState);
2754 
2755   std::vector<uint32_t> NewCFIs;
2756   for (int32_t CurState = FromState; CurState < ToState; ++CurState) {
2757     MCCFIInstruction *Instr = &FrameInstructions[CurState];
2758     if (Instr->getOperation() == MCCFIInstruction::OpRestoreState) {
2759       auto Iter = FrameRestoreEquivalents.find(CurState);
2760       assert(Iter != FrameRestoreEquivalents.end());
2761       NewCFIs.insert(NewCFIs.end(), Iter->second.begin(), Iter->second.end());
2762       // RestoreState / Remember will be filtered out later by CFISnapshotDiff,
2763       // so we might as well fall-through here.
2764     }
2765     NewCFIs.push_back(CurState);
2766   }
2767 
2768   // Replay instructions while avoiding duplicates
2769   for (int32_t State : llvm::reverse(NewCFIs)) {
2770     if (CFIDiff.isRedundant(FrameInstructions[State]))
2771       continue;
2772     InsertIt = addCFIPseudo(InBB, InsertIt, State);
2773   }
2774 
2775   return true;
2776 }
2777 
2778 SmallVector<int32_t, 4>
2779 BinaryFunction::unwindCFIState(int32_t FromState, int32_t ToState,
2780                                BinaryBasicBlock *InBB,
2781                                BinaryBasicBlock::iterator &InsertIt) {
2782   SmallVector<int32_t, 4> NewStates;
2783 
2784   CFISnapshot ToCFITable(CIEFrameInstructions, FrameInstructions,
2785                          FrameRestoreEquivalents, ToState);
2786   CFISnapshotDiff FromCFITable(ToCFITable);
2787   FromCFITable.advanceTo(FromState);
2788 
2789   auto undoStateDefCfa = [&]() {
2790     if (ToCFITable.CFARule == CFISnapshot::UNKNOWN) {
2791       FrameInstructions.emplace_back(MCCFIInstruction::cfiDefCfa(
2792           nullptr, ToCFITable.CFAReg, ToCFITable.CFAOffset));
2793       if (FromCFITable.isRedundant(FrameInstructions.back())) {
2794         FrameInstructions.pop_back();
2795         return;
2796       }
2797       NewStates.push_back(FrameInstructions.size() - 1);
2798       InsertIt = addCFIPseudo(InBB, InsertIt, FrameInstructions.size() - 1);
2799       ++InsertIt;
2800     } else if (ToCFITable.CFARule < 0) {
2801       if (FromCFITable.isRedundant(CIEFrameInstructions[-ToCFITable.CFARule]))
2802         return;
2803       NewStates.push_back(FrameInstructions.size());
2804       InsertIt = addCFIPseudo(InBB, InsertIt, FrameInstructions.size());
2805       ++InsertIt;
2806       FrameInstructions.emplace_back(CIEFrameInstructions[-ToCFITable.CFARule]);
2807     } else if (!FromCFITable.isRedundant(
2808                    FrameInstructions[ToCFITable.CFARule])) {
2809       NewStates.push_back(ToCFITable.CFARule);
2810       InsertIt = addCFIPseudo(InBB, InsertIt, ToCFITable.CFARule);
2811       ++InsertIt;
2812     }
2813   };
2814 
2815   auto undoState = [&](const MCCFIInstruction &Instr) {
2816     switch (Instr.getOperation()) {
2817     case MCCFIInstruction::OpRememberState:
2818     case MCCFIInstruction::OpRestoreState:
2819       break;
2820     case MCCFIInstruction::OpSameValue:
2821     case MCCFIInstruction::OpRelOffset:
2822     case MCCFIInstruction::OpOffset:
2823     case MCCFIInstruction::OpRestore:
2824     case MCCFIInstruction::OpUndefined:
2825     case MCCFIInstruction::OpEscape:
2826     case MCCFIInstruction::OpRegister: {
2827       uint32_t Reg;
2828       if (Instr.getOperation() != MCCFIInstruction::OpEscape) {
2829         Reg = Instr.getRegister();
2830       } else {
2831         std::optional<uint8_t> R =
2832             readDWARFExpressionTargetReg(Instr.getValues());
2833         // Handle DW_CFA_def_cfa_expression
2834         if (!R) {
2835           undoStateDefCfa();
2836           return;
2837         }
2838         Reg = *R;
2839       }
2840 
2841       if (!ToCFITable.RegRule.contains(Reg)) {
2842         FrameInstructions.emplace_back(
2843             MCCFIInstruction::createRestore(nullptr, Reg));
2844         if (FromCFITable.isRedundant(FrameInstructions.back())) {
2845           FrameInstructions.pop_back();
2846           break;
2847         }
2848         NewStates.push_back(FrameInstructions.size() - 1);
2849         InsertIt = addCFIPseudo(InBB, InsertIt, FrameInstructions.size() - 1);
2850         ++InsertIt;
2851         break;
2852       }
2853       const int32_t Rule = ToCFITable.RegRule[Reg];
2854       if (Rule < 0) {
2855         if (FromCFITable.isRedundant(CIEFrameInstructions[-Rule]))
2856           break;
2857         NewStates.push_back(FrameInstructions.size());
2858         InsertIt = addCFIPseudo(InBB, InsertIt, FrameInstructions.size());
2859         ++InsertIt;
2860         FrameInstructions.emplace_back(CIEFrameInstructions[-Rule]);
2861         break;
2862       }
2863       if (FromCFITable.isRedundant(FrameInstructions[Rule]))
2864         break;
2865       NewStates.push_back(Rule);
2866       InsertIt = addCFIPseudo(InBB, InsertIt, Rule);
2867       ++InsertIt;
2868       break;
2869     }
2870     case MCCFIInstruction::OpDefCfaRegister:
2871     case MCCFIInstruction::OpDefCfaOffset:
2872     case MCCFIInstruction::OpDefCfa:
2873       undoStateDefCfa();
2874       break;
2875     case MCCFIInstruction::OpAdjustCfaOffset:
2876     case MCCFIInstruction::OpWindowSave:
2877     case MCCFIInstruction::OpNegateRAState:
2878     case MCCFIInstruction::OpNegateRAStateWithPC:
2879     case MCCFIInstruction::OpLLVMDefAspaceCfa:
2880     case MCCFIInstruction::OpLabel:
2881     case MCCFIInstruction::OpValOffset:
2882       llvm_unreachable("unsupported CFI opcode");
2883       break;
2884     case MCCFIInstruction::OpGnuArgsSize:
2885       // do not affect CFI state
2886       break;
2887     }
2888   };
2889 
2890   // Undo all modifications from ToState to FromState
2891   for (int32_t I = ToState, E = FromState; I != E; ++I) {
2892     const MCCFIInstruction &Instr = FrameInstructions[I];
2893     if (Instr.getOperation() != MCCFIInstruction::OpRestoreState) {
2894       undoState(Instr);
2895       continue;
2896     }
2897     auto Iter = FrameRestoreEquivalents.find(I);
2898     if (Iter == FrameRestoreEquivalents.end())
2899       continue;
2900     for (int32_t State : Iter->second)
2901       undoState(FrameInstructions[State]);
2902   }
2903 
2904   return NewStates;
2905 }
2906 
2907 void BinaryFunction::normalizeCFIState() {
2908   // Reordering blocks with remember-restore state instructions can be specially
2909   // tricky. When rewriting the CFI, we omit remember-restore state instructions
2910   // entirely. For restore state, we build a map expanding each restore to the
2911   // equivalent unwindCFIState sequence required at that point to achieve the
2912   // same effect of the restore. All remember state are then just ignored.
2913   std::stack<int32_t> Stack;
2914   for (BinaryBasicBlock *CurBB : Layout.blocks()) {
2915     for (auto II = CurBB->begin(); II != CurBB->end(); ++II) {
2916       if (const MCCFIInstruction *CFI = getCFIFor(*II)) {
2917         if (CFI->getOperation() == MCCFIInstruction::OpRememberState) {
2918           Stack.push(II->getOperand(0).getImm());
2919           continue;
2920         }
2921         if (CFI->getOperation() == MCCFIInstruction::OpRestoreState) {
2922           const int32_t RememberState = Stack.top();
2923           const int32_t CurState = II->getOperand(0).getImm();
2924           FrameRestoreEquivalents[CurState] =
2925               unwindCFIState(CurState, RememberState, CurBB, II);
2926           Stack.pop();
2927         }
2928       }
2929     }
2930   }
2931 }
2932 
2933 bool BinaryFunction::finalizeCFIState() {
2934   LLVM_DEBUG(
2935       dbgs() << "Trying to fix CFI states for each BB after reordering.\n");
2936   LLVM_DEBUG(dbgs() << "This is the list of CFI states for each BB of " << *this
2937                     << ": ");
2938 
2939   const char *Sep = "";
2940   (void)Sep;
2941   for (FunctionFragment &FF : Layout.fragments()) {
2942     // Hot-cold border: at start of each region (with a different FDE) we need
2943     // to reset the CFI state.
2944     int32_t State = 0;
2945 
2946     for (BinaryBasicBlock *BB : FF) {
2947       const int32_t CFIStateAtExit = BB->getCFIStateAtExit();
2948 
2949       // We need to recover the correct state if it doesn't match expected
2950       // state at BB entry point.
2951       if (BB->getCFIState() < State) {
2952         // In this case, State is currently higher than what this BB expect it
2953         // to be. To solve this, we need to insert CFI instructions to undo
2954         // the effect of all CFI from BB's state to current State.
2955         auto InsertIt = BB->begin();
2956         unwindCFIState(State, BB->getCFIState(), BB, InsertIt);
2957       } else if (BB->getCFIState() > State) {
2958         // If BB's CFI state is greater than State, it means we are behind in
2959         // the state. Just emit all instructions to reach this state at the
2960         // beginning of this BB. If this sequence of instructions involve
2961         // remember state or restore state, bail out.
2962         if (!replayCFIInstrs(State, BB->getCFIState(), BB, BB->begin()))
2963           return false;
2964       }
2965 
2966       State = CFIStateAtExit;
2967       LLVM_DEBUG(dbgs() << Sep << State; Sep = ", ");
2968     }
2969   }
2970   LLVM_DEBUG(dbgs() << "\n");
2971 
2972   for (BinaryBasicBlock &BB : blocks()) {
2973     for (auto II = BB.begin(); II != BB.end();) {
2974       const MCCFIInstruction *CFI = getCFIFor(*II);
2975       if (CFI && (CFI->getOperation() == MCCFIInstruction::OpRememberState ||
2976                   CFI->getOperation() == MCCFIInstruction::OpRestoreState)) {
2977         II = BB.eraseInstruction(II);
2978       } else {
2979         ++II;
2980       }
2981     }
2982   }
2983 
2984   return true;
2985 }
2986 
2987 bool BinaryFunction::requiresAddressTranslation() const {
2988   return opts::EnableBAT || hasSDTMarker() || hasPseudoProbe();
2989 }
2990 
2991 bool BinaryFunction::requiresAddressMap() const {
2992   if (isInjected())
2993     return false;
2994 
2995   return opts::UpdateDebugSections || isMultiEntry() ||
2996          requiresAddressTranslation();
2997 }
2998 
2999 uint64_t BinaryFunction::getInstructionCount() const {
3000   uint64_t Count = 0;
3001   for (const BinaryBasicBlock &BB : blocks())
3002     Count += BB.getNumNonPseudos();
3003   return Count;
3004 }
3005 
3006 void BinaryFunction::clearDisasmState() {
3007   clearList(Instructions);
3008   clearList(IgnoredBranches);
3009   clearList(TakenBranches);
3010 
3011   if (BC.HasRelocations) {
3012     for (std::pair<const uint32_t, MCSymbol *> &LI : Labels)
3013       BC.UndefinedSymbols.insert(LI.second);
3014     for (MCSymbol *const EndLabel : FunctionEndLabels)
3015       if (EndLabel)
3016         BC.UndefinedSymbols.insert(EndLabel);
3017   }
3018 }
3019 
3020 void BinaryFunction::setTrapOnEntry() {
3021   clearDisasmState();
3022 
3023   forEachEntryPoint([&](uint64_t Offset, const MCSymbol *Label) -> bool {
3024     MCInst TrapInstr;
3025     BC.MIB->createTrap(TrapInstr);
3026     addInstruction(Offset, std::move(TrapInstr));
3027     return true;
3028   });
3029 
3030   TrapsOnEntry = true;
3031 }
3032 
3033 void BinaryFunction::setIgnored() {
3034   if (opts::processAllFunctions()) {
3035     // We can accept ignored functions before they've been disassembled.
3036     // In that case, they would still get disassembled and emited, but not
3037     // optimized.
3038     assert(CurrentState == State::Empty &&
3039            "cannot ignore non-empty functions in current mode");
3040     IsIgnored = true;
3041     return;
3042   }
3043 
3044   clearDisasmState();
3045 
3046   // Clear CFG state too.
3047   if (hasCFG()) {
3048     releaseCFG();
3049 
3050     for (BinaryBasicBlock *BB : BasicBlocks)
3051       delete BB;
3052     clearList(BasicBlocks);
3053 
3054     for (BinaryBasicBlock *BB : DeletedBasicBlocks)
3055       delete BB;
3056     clearList(DeletedBasicBlocks);
3057 
3058     Layout.clear();
3059   }
3060 
3061   CurrentState = State::Empty;
3062 
3063   IsIgnored = true;
3064   IsSimple = false;
3065   LLVM_DEBUG(dbgs() << "Ignoring " << getPrintName() << '\n');
3066 }
3067 
3068 void BinaryFunction::duplicateConstantIslands() {
3069   assert(Islands && "function expected to have constant islands");
3070 
3071   for (BinaryBasicBlock *BB : getLayout().blocks()) {
3072     if (!BB->isCold())
3073       continue;
3074 
3075     for (MCInst &Inst : *BB) {
3076       int OpNum = 0;
3077       for (MCOperand &Operand : Inst) {
3078         if (!Operand.isExpr()) {
3079           ++OpNum;
3080           continue;
3081         }
3082         const MCSymbol *Symbol = BC.MIB->getTargetSymbol(Inst, OpNum);
3083         // Check if this is an island symbol
3084         if (!Islands->Symbols.count(Symbol) &&
3085             !Islands->ProxySymbols.count(Symbol))
3086           continue;
3087 
3088         // Create cold symbol, if missing
3089         auto ISym = Islands->ColdSymbols.find(Symbol);
3090         MCSymbol *ColdSymbol;
3091         if (ISym != Islands->ColdSymbols.end()) {
3092           ColdSymbol = ISym->second;
3093         } else {
3094           ColdSymbol = BC.Ctx->getOrCreateSymbol(Symbol->getName() + ".cold");
3095           Islands->ColdSymbols[Symbol] = ColdSymbol;
3096           // Check if this is a proxy island symbol and update owner proxy map
3097           if (Islands->ProxySymbols.count(Symbol)) {
3098             BinaryFunction *Owner = Islands->ProxySymbols[Symbol];
3099             auto IProxiedSym = Owner->Islands->Proxies[this].find(Symbol);
3100             Owner->Islands->ColdProxies[this][IProxiedSym->second] = ColdSymbol;
3101           }
3102         }
3103 
3104         // Update instruction reference
3105         Operand = MCOperand::createExpr(BC.MIB->getTargetExprFor(
3106             Inst,
3107             MCSymbolRefExpr::create(ColdSymbol, MCSymbolRefExpr::VK_None,
3108                                     *BC.Ctx),
3109             *BC.Ctx, 0));
3110         ++OpNum;
3111       }
3112     }
3113   }
3114 }
3115 
3116 #ifndef MAX_PATH
3117 #define MAX_PATH 255
3118 #endif
3119 
3120 static std::string constructFilename(std::string Filename,
3121                                      std::string Annotation,
3122                                      std::string Suffix) {
3123   std::replace(Filename.begin(), Filename.end(), '/', '-');
3124   if (!Annotation.empty())
3125     Annotation.insert(0, "-");
3126   if (Filename.size() + Annotation.size() + Suffix.size() > MAX_PATH) {
3127     assert(Suffix.size() + Annotation.size() <= MAX_PATH);
3128     Filename.resize(MAX_PATH - (Suffix.size() + Annotation.size()));
3129   }
3130   Filename += Annotation;
3131   Filename += Suffix;
3132   return Filename;
3133 }
3134 
3135 static std::string formatEscapes(const std::string &Str) {
3136   std::string Result;
3137   for (unsigned I = 0; I < Str.size(); ++I) {
3138     char C = Str[I];
3139     switch (C) {
3140     case '\n':
3141       Result += "&#13;";
3142       break;
3143     case '"':
3144       break;
3145     default:
3146       Result += C;
3147       break;
3148     }
3149   }
3150   return Result;
3151 }
3152 
3153 void BinaryFunction::dumpGraph(raw_ostream &OS) const {
3154   OS << "digraph \"" << getPrintName() << "\" {\n"
3155      << "node [fontname=courier, shape=box, style=filled, colorscheme=brbg9]\n";
3156   uint64_t Offset = Address;
3157   for (BinaryBasicBlock *BB : BasicBlocks) {
3158     auto LayoutPos = find(Layout.blocks(), BB);
3159     unsigned LayoutIndex = LayoutPos - Layout.block_begin();
3160     const char *ColdStr = BB->isCold() ? " (cold)" : "";
3161     std::vector<std::string> Attrs;
3162     // Bold box for entry points
3163     if (isEntryPoint(*BB))
3164       Attrs.push_back("penwidth=2");
3165     if (BLI && BLI->getLoopFor(BB)) {
3166       // Distinguish innermost loops
3167       const BinaryLoop *Loop = BLI->getLoopFor(BB);
3168       if (Loop->isInnermost())
3169         Attrs.push_back("fillcolor=6");
3170       else // some outer loop
3171         Attrs.push_back("fillcolor=4");
3172     } else { // non-loopy code
3173       Attrs.push_back("fillcolor=5");
3174     }
3175     ListSeparator LS;
3176     OS << "\"" << BB->getName() << "\" [";
3177     for (StringRef Attr : Attrs)
3178       OS << LS << Attr;
3179     OS << "]\n";
3180     OS << format("\"%s\" [label=\"%s%s\\n(C:%lu,O:%lu,I:%u,L:%u,CFI:%u)\\n",
3181                  BB->getName().data(), BB->getName().data(), ColdStr,
3182                  BB->getKnownExecutionCount(), BB->getOffset(), getIndex(BB),
3183                  LayoutIndex, BB->getCFIState());
3184 
3185     if (opts::DotToolTipCode) {
3186       std::string Str;
3187       raw_string_ostream CS(Str);
3188       Offset = BC.printInstructions(CS, BB->begin(), BB->end(), Offset, this,
3189                                     /* PrintMCInst = */ false,
3190                                     /* PrintMemData = */ false,
3191                                     /* PrintRelocations = */ false,
3192                                     /* Endl = */ R"(\\l)");
3193       OS << formatEscapes(CS.str()) << '\n';
3194     }
3195     OS << "\"]\n";
3196 
3197     // analyzeBranch is just used to get the names of the branch
3198     // opcodes.
3199     const MCSymbol *TBB = nullptr;
3200     const MCSymbol *FBB = nullptr;
3201     MCInst *CondBranch = nullptr;
3202     MCInst *UncondBranch = nullptr;
3203     const bool Success = BB->analyzeBranch(TBB, FBB, CondBranch, UncondBranch);
3204 
3205     const MCInst *LastInstr = BB->getLastNonPseudoInstr();
3206     const bool IsJumpTable = LastInstr && BC.MIB->getJumpTable(*LastInstr);
3207 
3208     auto BI = BB->branch_info_begin();
3209     for (BinaryBasicBlock *Succ : BB->successors()) {
3210       std::string Branch;
3211       if (Success) {
3212         if (Succ == BB->getConditionalSuccessor(true)) {
3213           Branch = CondBranch ? std::string(BC.InstPrinter->getOpcodeName(
3214                                     CondBranch->getOpcode()))
3215                               : "TB";
3216         } else if (Succ == BB->getConditionalSuccessor(false)) {
3217           Branch = UncondBranch ? std::string(BC.InstPrinter->getOpcodeName(
3218                                       UncondBranch->getOpcode()))
3219                                 : "FB";
3220         } else {
3221           Branch = "FT";
3222         }
3223       }
3224       if (IsJumpTable)
3225         Branch = "JT";
3226       OS << format("\"%s\" -> \"%s\" [label=\"%s", BB->getName().data(),
3227                    Succ->getName().data(), Branch.c_str());
3228 
3229       if (BB->getExecutionCount() != COUNT_NO_PROFILE &&
3230           BI->MispredictedCount != BinaryBasicBlock::COUNT_INFERRED) {
3231         OS << "\\n(C:" << BI->Count << ",M:" << BI->MispredictedCount << ")";
3232       } else if (ExecutionCount != COUNT_NO_PROFILE &&
3233                  BI->Count != BinaryBasicBlock::COUNT_NO_PROFILE) {
3234         OS << "\\n(IC:" << BI->Count << ")";
3235       }
3236       OS << "\"]\n";
3237 
3238       ++BI;
3239     }
3240     for (BinaryBasicBlock *LP : BB->landing_pads()) {
3241       OS << format("\"%s\" -> \"%s\" [constraint=false style=dashed]\n",
3242                    BB->getName().data(), LP->getName().data());
3243     }
3244   }
3245   OS << "}\n";
3246 }
3247 
3248 void BinaryFunction::viewGraph() const {
3249   SmallString<MAX_PATH> Filename;
3250   if (std::error_code EC =
3251           sys::fs::createTemporaryFile("bolt-cfg", "dot", Filename)) {
3252     BC.errs() << "BOLT-ERROR: " << EC.message() << ", unable to create "
3253               << " bolt-cfg-XXXXX.dot temporary file.\n";
3254     return;
3255   }
3256   dumpGraphToFile(std::string(Filename));
3257   if (DisplayGraph(Filename))
3258     BC.errs() << "BOLT-ERROR: Can't display " << Filename
3259               << " with graphviz.\n";
3260   if (std::error_code EC = sys::fs::remove(Filename)) {
3261     BC.errs() << "BOLT-WARNING: " << EC.message() << ", failed to remove "
3262               << Filename << "\n";
3263   }
3264 }
3265 
3266 void BinaryFunction::dumpGraphForPass(std::string Annotation) const {
3267   if (!opts::shouldPrint(*this))
3268     return;
3269 
3270   std::string Filename = constructFilename(getPrintName(), Annotation, ".dot");
3271   if (opts::Verbosity >= 1)
3272     BC.outs() << "BOLT-INFO: dumping CFG to " << Filename << "\n";
3273   dumpGraphToFile(Filename);
3274 }
3275 
3276 void BinaryFunction::dumpGraphToFile(std::string Filename) const {
3277   std::error_code EC;
3278   raw_fd_ostream of(Filename, EC, sys::fs::OF_None);
3279   if (EC) {
3280     if (opts::Verbosity >= 1) {
3281       BC.errs() << "BOLT-WARNING: " << EC.message() << ", unable to open "
3282                 << Filename << " for output.\n";
3283     }
3284     return;
3285   }
3286   dumpGraph(of);
3287 }
3288 
3289 bool BinaryFunction::validateCFG() const {
3290   // Skip the validation of CFG after it is finalized
3291   if (CurrentState == State::CFG_Finalized)
3292     return true;
3293 
3294   for (BinaryBasicBlock *BB : BasicBlocks)
3295     if (!BB->validateSuccessorInvariants())
3296       return false;
3297 
3298   // Make sure all blocks in CFG are valid.
3299   auto validateBlock = [this](const BinaryBasicBlock *BB, StringRef Desc) {
3300     if (!BB->isValid()) {
3301       BC.errs() << "BOLT-ERROR: deleted " << Desc << " " << BB->getName()
3302                 << " detected in:\n";
3303       this->dump();
3304       return false;
3305     }
3306     return true;
3307   };
3308   for (const BinaryBasicBlock *BB : BasicBlocks) {
3309     if (!validateBlock(BB, "block"))
3310       return false;
3311     for (const BinaryBasicBlock *PredBB : BB->predecessors())
3312       if (!validateBlock(PredBB, "predecessor"))
3313         return false;
3314     for (const BinaryBasicBlock *SuccBB : BB->successors())
3315       if (!validateBlock(SuccBB, "successor"))
3316         return false;
3317     for (const BinaryBasicBlock *LP : BB->landing_pads())
3318       if (!validateBlock(LP, "landing pad"))
3319         return false;
3320     for (const BinaryBasicBlock *Thrower : BB->throwers())
3321       if (!validateBlock(Thrower, "thrower"))
3322         return false;
3323   }
3324 
3325   for (const BinaryBasicBlock *BB : BasicBlocks) {
3326     std::unordered_set<const BinaryBasicBlock *> BBLandingPads;
3327     for (const BinaryBasicBlock *LP : BB->landing_pads()) {
3328       if (BBLandingPads.count(LP)) {
3329         BC.errs() << "BOLT-ERROR: duplicate landing pad detected in"
3330                   << BB->getName() << " in function " << *this << '\n';
3331         return false;
3332       }
3333       BBLandingPads.insert(LP);
3334     }
3335 
3336     std::unordered_set<const BinaryBasicBlock *> BBThrowers;
3337     for (const BinaryBasicBlock *Thrower : BB->throwers()) {
3338       if (BBThrowers.count(Thrower)) {
3339         BC.errs() << "BOLT-ERROR: duplicate thrower detected in"
3340                   << BB->getName() << " in function " << *this << '\n';
3341         return false;
3342       }
3343       BBThrowers.insert(Thrower);
3344     }
3345 
3346     for (const BinaryBasicBlock *LPBlock : BB->landing_pads()) {
3347       if (!llvm::is_contained(LPBlock->throwers(), BB)) {
3348         BC.errs() << "BOLT-ERROR: inconsistent landing pad detected in "
3349                   << *this << ": " << BB->getName()
3350                   << " is in LandingPads but not in " << LPBlock->getName()
3351                   << " Throwers\n";
3352         return false;
3353       }
3354     }
3355     for (const BinaryBasicBlock *Thrower : BB->throwers()) {
3356       if (!llvm::is_contained(Thrower->landing_pads(), BB)) {
3357         BC.errs() << "BOLT-ERROR: inconsistent thrower detected in " << *this
3358                   << ": " << BB->getName() << " is in Throwers list but not in "
3359                   << Thrower->getName() << " LandingPads\n";
3360         return false;
3361       }
3362     }
3363   }
3364 
3365   return true;
3366 }
3367 
3368 void BinaryFunction::fixBranches() {
3369   auto &MIB = BC.MIB;
3370   MCContext *Ctx = BC.Ctx.get();
3371 
3372   for (BinaryBasicBlock *BB : BasicBlocks) {
3373     const MCSymbol *TBB = nullptr;
3374     const MCSymbol *FBB = nullptr;
3375     MCInst *CondBranch = nullptr;
3376     MCInst *UncondBranch = nullptr;
3377     if (!BB->analyzeBranch(TBB, FBB, CondBranch, UncondBranch))
3378       continue;
3379 
3380     // We will create unconditional branch with correct destination if needed.
3381     if (UncondBranch)
3382       BB->eraseInstruction(BB->findInstruction(UncondBranch));
3383 
3384     // Basic block that follows the current one in the final layout.
3385     const BinaryBasicBlock *const NextBB =
3386         Layout.getBasicBlockAfter(BB, /*IgnoreSplits=*/false);
3387 
3388     if (BB->succ_size() == 1) {
3389       // __builtin_unreachable() could create a conditional branch that
3390       // falls-through into the next function - hence the block will have only
3391       // one valid successor. Since behaviour is undefined - we replace
3392       // the conditional branch with an unconditional if required.
3393       if (CondBranch)
3394         BB->eraseInstruction(BB->findInstruction(CondBranch));
3395       if (BB->getSuccessor() == NextBB)
3396         continue;
3397       BB->addBranchInstruction(BB->getSuccessor());
3398     } else if (BB->succ_size() == 2) {
3399       assert(CondBranch && "conditional branch expected");
3400       const BinaryBasicBlock *TSuccessor = BB->getConditionalSuccessor(true);
3401       const BinaryBasicBlock *FSuccessor = BB->getConditionalSuccessor(false);
3402 
3403       // Eliminate unnecessary conditional branch.
3404       if (TSuccessor == FSuccessor) {
3405         // FIXME: at the moment, we cannot safely remove static key branches.
3406         if (MIB->isDynamicBranch(*CondBranch)) {
3407           if (opts::Verbosity) {
3408             BC.outs()
3409                 << "BOLT-INFO: unable to remove redundant dynamic branch in "
3410                 << *this << '\n';
3411           }
3412           continue;
3413         }
3414 
3415         BB->removeDuplicateConditionalSuccessor(CondBranch);
3416         if (TSuccessor != NextBB)
3417           BB->addBranchInstruction(TSuccessor);
3418         continue;
3419       }
3420 
3421       // Reverse branch condition and swap successors.
3422       auto swapSuccessors = [&]() {
3423         if (!MIB->isReversibleBranch(*CondBranch)) {
3424           if (opts::Verbosity) {
3425             BC.outs() << "BOLT-INFO: unable to swap successors in " << *this
3426                       << '\n';
3427           }
3428           return false;
3429         }
3430         std::swap(TSuccessor, FSuccessor);
3431         BB->swapConditionalSuccessors();
3432         auto L = BC.scopeLock();
3433         MIB->reverseBranchCondition(*CondBranch, TSuccessor->getLabel(), Ctx);
3434         return true;
3435       };
3436 
3437       // Check whether the next block is a "taken" target and try to swap it
3438       // with a "fall-through" target.
3439       if (TSuccessor == NextBB && swapSuccessors())
3440         continue;
3441 
3442       // Update conditional branch destination if needed.
3443       if (MIB->getTargetSymbol(*CondBranch) != TSuccessor->getLabel()) {
3444         auto L = BC.scopeLock();
3445         MIB->replaceBranchTarget(*CondBranch, TSuccessor->getLabel(), Ctx);
3446       }
3447 
3448       // No need for the unconditional branch.
3449       if (FSuccessor == NextBB)
3450         continue;
3451 
3452       if (BC.isX86()) {
3453         // We are going to generate two branches. Check if their targets are in
3454         // the same fragment as this block. If only one target is in the same
3455         // fragment, make it the destination of the conditional branch. There
3456         // is a chance it will be a short branch which takes 4 bytes fewer than
3457         // a long conditional branch. For unconditional branch, the difference
3458         // is 3 bytes.
3459         if (BB->getFragmentNum() != TSuccessor->getFragmentNum() &&
3460             BB->getFragmentNum() == FSuccessor->getFragmentNum())
3461           swapSuccessors();
3462       }
3463 
3464       BB->addBranchInstruction(FSuccessor);
3465     }
3466     // Cases where the number of successors is 0 (block ends with a
3467     // terminator) or more than 2 (switch table) don't require branch
3468     // instruction adjustments.
3469   }
3470   assert((!isSimple() || validateCFG()) &&
3471          "Invalid CFG detected after fixing branches");
3472 }
3473 
3474 void BinaryFunction::propagateGnuArgsSizeInfo(
3475     MCPlusBuilder::AllocatorIdTy AllocId) {
3476   assert(CurrentState == State::Disassembled && "unexpected function state");
3477 
3478   if (!hasEHRanges() || !usesGnuArgsSize())
3479     return;
3480 
3481   // The current value of DW_CFA_GNU_args_size affects all following
3482   // invoke instructions until the next CFI overrides it.
3483   // It is important to iterate basic blocks in the original order when
3484   // assigning the value.
3485   uint64_t CurrentGnuArgsSize = 0;
3486   for (BinaryBasicBlock *BB : BasicBlocks) {
3487     for (auto II = BB->begin(); II != BB->end();) {
3488       MCInst &Instr = *II;
3489       if (BC.MIB->isCFI(Instr)) {
3490         const MCCFIInstruction *CFI = getCFIFor(Instr);
3491         if (CFI->getOperation() == MCCFIInstruction::OpGnuArgsSize) {
3492           CurrentGnuArgsSize = CFI->getOffset();
3493           // Delete DW_CFA_GNU_args_size instructions and only regenerate
3494           // during the final code emission. The information is embedded
3495           // inside call instructions.
3496           II = BB->erasePseudoInstruction(II);
3497           continue;
3498         }
3499       } else if (BC.MIB->isInvoke(Instr)) {
3500         // Add the value of GNU_args_size as an extra operand to invokes.
3501         BC.MIB->addGnuArgsSize(Instr, CurrentGnuArgsSize);
3502       }
3503       ++II;
3504     }
3505   }
3506 }
3507 
3508 void BinaryFunction::postProcessBranches() {
3509   if (!isSimple())
3510     return;
3511   for (BinaryBasicBlock &BB : blocks()) {
3512     auto LastInstrRI = BB.getLastNonPseudo();
3513     if (BB.succ_size() == 1) {
3514       if (LastInstrRI != BB.rend() &&
3515           BC.MIB->isConditionalBranch(*LastInstrRI)) {
3516         // __builtin_unreachable() could create a conditional branch that
3517         // falls-through into the next function - hence the block will have only
3518         // one valid successor. Such behaviour is undefined and thus we remove
3519         // the conditional branch while leaving a valid successor.
3520         BB.eraseInstruction(std::prev(LastInstrRI.base()));
3521         LLVM_DEBUG(dbgs() << "BOLT-DEBUG: erasing conditional branch in "
3522                           << BB.getName() << " in function " << *this << '\n');
3523       }
3524     } else if (BB.succ_size() == 0) {
3525       // Ignore unreachable basic blocks.
3526       if (BB.pred_size() == 0 || BB.isLandingPad())
3527         continue;
3528 
3529       // If it's the basic block that does not end up with a terminator - we
3530       // insert a return instruction unless it's a call instruction.
3531       if (LastInstrRI == BB.rend()) {
3532         LLVM_DEBUG(
3533             dbgs() << "BOLT-DEBUG: at least one instruction expected in BB "
3534                    << BB.getName() << " in function " << *this << '\n');
3535         continue;
3536       }
3537       if (!BC.MIB->isTerminator(*LastInstrRI) &&
3538           !BC.MIB->isCall(*LastInstrRI)) {
3539         LLVM_DEBUG(dbgs() << "BOLT-DEBUG: adding return to basic block "
3540                           << BB.getName() << " in function " << *this << '\n');
3541         MCInst ReturnInstr;
3542         BC.MIB->createReturn(ReturnInstr);
3543         BB.addInstruction(ReturnInstr);
3544       }
3545     }
3546   }
3547   assert(validateCFG() && "invalid CFG");
3548 }
3549 
3550 MCSymbol *BinaryFunction::addEntryPointAtOffset(uint64_t Offset) {
3551   assert(Offset && "cannot add primary entry point");
3552   assert(CurrentState == State::Empty || CurrentState == State::Disassembled);
3553 
3554   const uint64_t EntryPointAddress = getAddress() + Offset;
3555   MCSymbol *LocalSymbol = getOrCreateLocalLabel(EntryPointAddress);
3556 
3557   MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(LocalSymbol);
3558   if (EntrySymbol)
3559     return EntrySymbol;
3560 
3561   if (BinaryData *EntryBD = BC.getBinaryDataAtAddress(EntryPointAddress)) {
3562     EntrySymbol = EntryBD->getSymbol();
3563   } else {
3564     EntrySymbol = BC.getOrCreateGlobalSymbol(
3565         EntryPointAddress, Twine("__ENTRY_") + getOneName() + "@");
3566   }
3567   SecondaryEntryPoints[LocalSymbol] = EntrySymbol;
3568 
3569   BC.setSymbolToFunctionMap(EntrySymbol, this);
3570 
3571   return EntrySymbol;
3572 }
3573 
3574 MCSymbol *BinaryFunction::addEntryPoint(const BinaryBasicBlock &BB) {
3575   assert(CurrentState == State::CFG &&
3576          "basic block can be added as an entry only in a function with CFG");
3577 
3578   if (&BB == BasicBlocks.front())
3579     return getSymbol();
3580 
3581   MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(BB);
3582   if (EntrySymbol)
3583     return EntrySymbol;
3584 
3585   EntrySymbol =
3586       BC.Ctx->getOrCreateSymbol("__ENTRY_" + BB.getLabel()->getName());
3587 
3588   SecondaryEntryPoints[BB.getLabel()] = EntrySymbol;
3589 
3590   BC.setSymbolToFunctionMap(EntrySymbol, this);
3591 
3592   return EntrySymbol;
3593 }
3594 
3595 MCSymbol *BinaryFunction::getSymbolForEntryID(uint64_t EntryID) {
3596   if (EntryID == 0)
3597     return getSymbol();
3598 
3599   if (!isMultiEntry())
3600     return nullptr;
3601 
3602   uint64_t NumEntries = 1;
3603   if (hasCFG()) {
3604     for (BinaryBasicBlock *BB : BasicBlocks) {
3605       MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(*BB);
3606       if (!EntrySymbol)
3607         continue;
3608       if (NumEntries == EntryID)
3609         return EntrySymbol;
3610       ++NumEntries;
3611     }
3612   } else {
3613     for (std::pair<const uint32_t, MCSymbol *> &KV : Labels) {
3614       MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(KV.second);
3615       if (!EntrySymbol)
3616         continue;
3617       if (NumEntries == EntryID)
3618         return EntrySymbol;
3619       ++NumEntries;
3620     }
3621   }
3622 
3623   return nullptr;
3624 }
3625 
3626 uint64_t BinaryFunction::getEntryIDForSymbol(const MCSymbol *Symbol) const {
3627   if (!isMultiEntry())
3628     return 0;
3629 
3630   for (const MCSymbol *FunctionSymbol : getSymbols())
3631     if (FunctionSymbol == Symbol)
3632       return 0;
3633 
3634   // Check all secondary entries available as either basic blocks or lables.
3635   uint64_t NumEntries = 1;
3636   for (const BinaryBasicBlock *BB : BasicBlocks) {
3637     MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(*BB);
3638     if (!EntrySymbol)
3639       continue;
3640     if (EntrySymbol == Symbol)
3641       return NumEntries;
3642     ++NumEntries;
3643   }
3644   NumEntries = 1;
3645   for (const std::pair<const uint32_t, MCSymbol *> &KV : Labels) {
3646     MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(KV.second);
3647     if (!EntrySymbol)
3648       continue;
3649     if (EntrySymbol == Symbol)
3650       return NumEntries;
3651     ++NumEntries;
3652   }
3653 
3654   llvm_unreachable("symbol not found");
3655 }
3656 
3657 bool BinaryFunction::forEachEntryPoint(EntryPointCallbackTy Callback) const {
3658   bool Status = Callback(0, getSymbol());
3659   if (!isMultiEntry())
3660     return Status;
3661 
3662   for (const std::pair<const uint32_t, MCSymbol *> &KV : Labels) {
3663     if (!Status)
3664       break;
3665 
3666     MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(KV.second);
3667     if (!EntrySymbol)
3668       continue;
3669 
3670     Status = Callback(KV.first, EntrySymbol);
3671   }
3672 
3673   return Status;
3674 }
3675 
3676 BinaryFunction::BasicBlockListType BinaryFunction::dfs() const {
3677   BasicBlockListType DFS;
3678   std::stack<BinaryBasicBlock *> Stack;
3679   std::set<BinaryBasicBlock *> Visited;
3680 
3681   // Push entry points to the stack in reverse order.
3682   //
3683   // NB: we rely on the original order of entries to match.
3684   SmallVector<BinaryBasicBlock *> EntryPoints;
3685   llvm::copy_if(BasicBlocks, std::back_inserter(EntryPoints),
3686           [&](const BinaryBasicBlock *const BB) { return isEntryPoint(*BB); });
3687   // Sort entry points by their offset to make sure we got them in the right
3688   // order.
3689   llvm::stable_sort(EntryPoints, [](const BinaryBasicBlock *const A,
3690                               const BinaryBasicBlock *const B) {
3691     return A->getOffset() < B->getOffset();
3692   });
3693   for (BinaryBasicBlock *const BB : reverse(EntryPoints))
3694     Stack.push(BB);
3695 
3696   while (!Stack.empty()) {
3697     BinaryBasicBlock *BB = Stack.top();
3698     Stack.pop();
3699 
3700     if (!Visited.insert(BB).second)
3701       continue;
3702     DFS.push_back(BB);
3703 
3704     for (BinaryBasicBlock *SuccBB : BB->landing_pads()) {
3705       Stack.push(SuccBB);
3706     }
3707 
3708     const MCSymbol *TBB = nullptr;
3709     const MCSymbol *FBB = nullptr;
3710     MCInst *CondBranch = nullptr;
3711     MCInst *UncondBranch = nullptr;
3712     if (BB->analyzeBranch(TBB, FBB, CondBranch, UncondBranch) && CondBranch &&
3713         BB->succ_size() == 2) {
3714       if (BC.MIB->getCanonicalBranchCondCode(BC.MIB->getCondCode(
3715               *CondBranch)) == BC.MIB->getCondCode(*CondBranch)) {
3716         Stack.push(BB->getConditionalSuccessor(true));
3717         Stack.push(BB->getConditionalSuccessor(false));
3718       } else {
3719         Stack.push(BB->getConditionalSuccessor(false));
3720         Stack.push(BB->getConditionalSuccessor(true));
3721       }
3722     } else {
3723       for (BinaryBasicBlock *SuccBB : BB->successors()) {
3724         Stack.push(SuccBB);
3725       }
3726     }
3727   }
3728 
3729   return DFS;
3730 }
3731 
3732 size_t BinaryFunction::computeHash(bool UseDFS, HashFunction HashFunction,
3733                                    OperandHashFuncTy OperandHashFunc) const {
3734   LLVM_DEBUG({
3735     dbgs() << "BOLT-DEBUG: computeHash " << getPrintName() << ' '
3736            << (UseDFS ? "dfs" : "bin") << " order "
3737            << (HashFunction == HashFunction::StdHash ? "std::hash" : "xxh3")
3738            << '\n';
3739   });
3740 
3741   if (size() == 0)
3742     return 0;
3743 
3744   assert(hasCFG() && "function is expected to have CFG");
3745 
3746   SmallVector<const BinaryBasicBlock *, 0> Order;
3747   if (UseDFS)
3748     llvm::copy(dfs(), std::back_inserter(Order));
3749   else
3750     llvm::copy(Layout.blocks(), std::back_inserter(Order));
3751 
3752   // The hash is computed by creating a string of all instruction opcodes and
3753   // possibly their operands and then hashing that string with std::hash.
3754   std::string HashString;
3755   for (const BinaryBasicBlock *BB : Order)
3756     HashString.append(hashBlock(BC, *BB, OperandHashFunc));
3757 
3758   switch (HashFunction) {
3759   case HashFunction::StdHash:
3760     return Hash = std::hash<std::string>{}(HashString);
3761   case HashFunction::XXH3:
3762     return Hash = llvm::xxh3_64bits(HashString);
3763   }
3764   llvm_unreachable("Unhandled HashFunction");
3765 }
3766 
3767 void BinaryFunction::insertBasicBlocks(
3768     BinaryBasicBlock *Start,
3769     std::vector<std::unique_ptr<BinaryBasicBlock>> &&NewBBs,
3770     const bool UpdateLayout, const bool UpdateCFIState,
3771     const bool RecomputeLandingPads) {
3772   const int64_t StartIndex = Start ? getIndex(Start) : -1LL;
3773   const size_t NumNewBlocks = NewBBs.size();
3774 
3775   BasicBlocks.insert(BasicBlocks.begin() + (StartIndex + 1), NumNewBlocks,
3776                      nullptr);
3777 
3778   int64_t I = StartIndex + 1;
3779   for (std::unique_ptr<BinaryBasicBlock> &BB : NewBBs) {
3780     assert(!BasicBlocks[I]);
3781     BasicBlocks[I++] = BB.release();
3782   }
3783 
3784   if (RecomputeLandingPads)
3785     recomputeLandingPads();
3786   else
3787     updateBBIndices(0);
3788 
3789   if (UpdateLayout)
3790     updateLayout(Start, NumNewBlocks);
3791 
3792   if (UpdateCFIState)
3793     updateCFIState(Start, NumNewBlocks);
3794 }
3795 
3796 BinaryFunction::iterator BinaryFunction::insertBasicBlocks(
3797     BinaryFunction::iterator StartBB,
3798     std::vector<std::unique_ptr<BinaryBasicBlock>> &&NewBBs,
3799     const bool UpdateLayout, const bool UpdateCFIState,
3800     const bool RecomputeLandingPads) {
3801   const unsigned StartIndex = getIndex(&*StartBB);
3802   const size_t NumNewBlocks = NewBBs.size();
3803 
3804   BasicBlocks.insert(BasicBlocks.begin() + StartIndex + 1, NumNewBlocks,
3805                      nullptr);
3806   auto RetIter = BasicBlocks.begin() + StartIndex + 1;
3807 
3808   unsigned I = StartIndex + 1;
3809   for (std::unique_ptr<BinaryBasicBlock> &BB : NewBBs) {
3810     assert(!BasicBlocks[I]);
3811     BasicBlocks[I++] = BB.release();
3812   }
3813 
3814   if (RecomputeLandingPads)
3815     recomputeLandingPads();
3816   else
3817     updateBBIndices(0);
3818 
3819   if (UpdateLayout)
3820     updateLayout(*std::prev(RetIter), NumNewBlocks);
3821 
3822   if (UpdateCFIState)
3823     updateCFIState(*std::prev(RetIter), NumNewBlocks);
3824 
3825   return RetIter;
3826 }
3827 
3828 void BinaryFunction::updateBBIndices(const unsigned StartIndex) {
3829   for (unsigned I = StartIndex; I < BasicBlocks.size(); ++I)
3830     BasicBlocks[I]->Index = I;
3831 }
3832 
3833 void BinaryFunction::updateCFIState(BinaryBasicBlock *Start,
3834                                     const unsigned NumNewBlocks) {
3835   const int32_t CFIState = Start->getCFIStateAtExit();
3836   const unsigned StartIndex = getIndex(Start) + 1;
3837   for (unsigned I = 0; I < NumNewBlocks; ++I)
3838     BasicBlocks[StartIndex + I]->setCFIState(CFIState);
3839 }
3840 
3841 void BinaryFunction::updateLayout(BinaryBasicBlock *Start,
3842                                   const unsigned NumNewBlocks) {
3843   BasicBlockListType::iterator Begin;
3844   BasicBlockListType::iterator End;
3845 
3846   // If start not provided copy new blocks from the beginning of BasicBlocks
3847   if (!Start) {
3848     Begin = BasicBlocks.begin();
3849     End = BasicBlocks.begin() + NumNewBlocks;
3850   } else {
3851     unsigned StartIndex = getIndex(Start);
3852     Begin = std::next(BasicBlocks.begin(), StartIndex + 1);
3853     End = std::next(BasicBlocks.begin(), StartIndex + NumNewBlocks + 1);
3854   }
3855 
3856   // Insert new blocks in the layout immediately after Start.
3857   Layout.insertBasicBlocks(Start, {Begin, End});
3858   Layout.updateLayoutIndices();
3859 }
3860 
3861 bool BinaryFunction::checkForAmbiguousJumpTables() {
3862   SmallSet<uint64_t, 4> JumpTables;
3863   for (BinaryBasicBlock *&BB : BasicBlocks) {
3864     for (MCInst &Inst : *BB) {
3865       if (!BC.MIB->isIndirectBranch(Inst))
3866         continue;
3867       uint64_t JTAddress = BC.MIB->getJumpTable(Inst);
3868       if (!JTAddress)
3869         continue;
3870       // This address can be inside another jump table, but we only consider
3871       // it ambiguous when the same start address is used, not the same JT
3872       // object.
3873       if (!JumpTables.count(JTAddress)) {
3874         JumpTables.insert(JTAddress);
3875         continue;
3876       }
3877       return true;
3878     }
3879   }
3880   return false;
3881 }
3882 
3883 void BinaryFunction::disambiguateJumpTables(
3884     MCPlusBuilder::AllocatorIdTy AllocId) {
3885   assert((opts::JumpTables != JTS_BASIC && isSimple()) || !BC.HasRelocations);
3886   SmallPtrSet<JumpTable *, 4> JumpTables;
3887   for (BinaryBasicBlock *&BB : BasicBlocks) {
3888     for (MCInst &Inst : *BB) {
3889       if (!BC.MIB->isIndirectBranch(Inst))
3890         continue;
3891       JumpTable *JT = getJumpTable(Inst);
3892       if (!JT)
3893         continue;
3894       if (JumpTables.insert(JT).second)
3895         continue;
3896       // This instruction is an indirect jump using a jump table, but it is
3897       // using the same jump table of another jump. Try all our tricks to
3898       // extract the jump table symbol and make it point to a new, duplicated JT
3899       MCPhysReg BaseReg1;
3900       uint64_t Scale;
3901       const MCSymbol *Target;
3902       // In case we match if our first matcher, first instruction is the one to
3903       // patch
3904       MCInst *JTLoadInst = &Inst;
3905       // Try a standard indirect jump matcher, scale 8
3906       std::unique_ptr<MCPlusBuilder::MCInstMatcher> IndJmpMatcher =
3907           BC.MIB->matchIndJmp(BC.MIB->matchReg(BaseReg1),
3908                               BC.MIB->matchImm(Scale), BC.MIB->matchReg(),
3909                               /*Offset=*/BC.MIB->matchSymbol(Target));
3910       if (!IndJmpMatcher->match(
3911               *BC.MRI, *BC.MIB,
3912               MutableArrayRef<MCInst>(&*BB->begin(), &Inst + 1), -1) ||
3913           BaseReg1 != BC.MIB->getNoRegister() || Scale != 8) {
3914         MCPhysReg BaseReg2;
3915         uint64_t Offset;
3916         // Standard JT matching failed. Trying now:
3917         //     movq  "jt.2397/1"(,%rax,8), %rax
3918         //     jmpq  *%rax
3919         std::unique_ptr<MCPlusBuilder::MCInstMatcher> LoadMatcherOwner =
3920             BC.MIB->matchLoad(BC.MIB->matchReg(BaseReg1),
3921                               BC.MIB->matchImm(Scale), BC.MIB->matchReg(),
3922                               /*Offset=*/BC.MIB->matchSymbol(Target));
3923         MCPlusBuilder::MCInstMatcher *LoadMatcher = LoadMatcherOwner.get();
3924         std::unique_ptr<MCPlusBuilder::MCInstMatcher> IndJmpMatcher2 =
3925             BC.MIB->matchIndJmp(std::move(LoadMatcherOwner));
3926         if (!IndJmpMatcher2->match(
3927                 *BC.MRI, *BC.MIB,
3928                 MutableArrayRef<MCInst>(&*BB->begin(), &Inst + 1), -1) ||
3929             BaseReg1 != BC.MIB->getNoRegister() || Scale != 8) {
3930           // JT matching failed. Trying now:
3931           // PIC-style matcher, scale 4
3932           //    addq    %rdx, %rsi
3933           //    addq    %rdx, %rdi
3934           //    leaq    DATAat0x402450(%rip), %r11
3935           //    movslq  (%r11,%rdx,4), %rcx
3936           //    addq    %r11, %rcx
3937           //    jmpq    *%rcx # JUMPTABLE @0x402450
3938           std::unique_ptr<MCPlusBuilder::MCInstMatcher> PICIndJmpMatcher =
3939               BC.MIB->matchIndJmp(BC.MIB->matchAdd(
3940                   BC.MIB->matchReg(BaseReg1),
3941                   BC.MIB->matchLoad(BC.MIB->matchReg(BaseReg2),
3942                                     BC.MIB->matchImm(Scale), BC.MIB->matchReg(),
3943                                     BC.MIB->matchImm(Offset))));
3944           std::unique_ptr<MCPlusBuilder::MCInstMatcher> LEAMatcherOwner =
3945               BC.MIB->matchLoadAddr(BC.MIB->matchSymbol(Target));
3946           MCPlusBuilder::MCInstMatcher *LEAMatcher = LEAMatcherOwner.get();
3947           std::unique_ptr<MCPlusBuilder::MCInstMatcher> PICBaseAddrMatcher =
3948               BC.MIB->matchIndJmp(BC.MIB->matchAdd(std::move(LEAMatcherOwner),
3949                                                    BC.MIB->matchAnyOperand()));
3950           if (!PICIndJmpMatcher->match(
3951                   *BC.MRI, *BC.MIB,
3952                   MutableArrayRef<MCInst>(&*BB->begin(), &Inst + 1), -1) ||
3953               Scale != 4 || BaseReg1 != BaseReg2 || Offset != 0 ||
3954               !PICBaseAddrMatcher->match(
3955                   *BC.MRI, *BC.MIB,
3956                   MutableArrayRef<MCInst>(&*BB->begin(), &Inst + 1), -1)) {
3957             llvm_unreachable("Failed to extract jump table base");
3958             continue;
3959           }
3960           // Matched PIC, identify the instruction with the reference to the JT
3961           JTLoadInst = LEAMatcher->CurInst;
3962         } else {
3963           // Matched non-PIC
3964           JTLoadInst = LoadMatcher->CurInst;
3965         }
3966       }
3967 
3968       uint64_t NewJumpTableID = 0;
3969       const MCSymbol *NewJTLabel;
3970       std::tie(NewJumpTableID, NewJTLabel) =
3971           BC.duplicateJumpTable(*this, JT, Target);
3972       {
3973         auto L = BC.scopeLock();
3974         BC.MIB->replaceMemOperandDisp(*JTLoadInst, NewJTLabel, BC.Ctx.get());
3975       }
3976       // We use a unique ID with the high bit set as address for this "injected"
3977       // jump table (not originally in the input binary).
3978       BC.MIB->setJumpTable(Inst, NewJumpTableID, 0, AllocId);
3979     }
3980   }
3981 }
3982 
3983 bool BinaryFunction::replaceJumpTableEntryIn(BinaryBasicBlock *BB,
3984                                              BinaryBasicBlock *OldDest,
3985                                              BinaryBasicBlock *NewDest) {
3986   MCInst *Instr = BB->getLastNonPseudoInstr();
3987   if (!Instr || !BC.MIB->isIndirectBranch(*Instr))
3988     return false;
3989   uint64_t JTAddress = BC.MIB->getJumpTable(*Instr);
3990   assert(JTAddress && "Invalid jump table address");
3991   JumpTable *JT = getJumpTableContainingAddress(JTAddress);
3992   assert(JT && "No jump table structure for this indirect branch");
3993   bool Patched = JT->replaceDestination(JTAddress, OldDest->getLabel(),
3994                                         NewDest->getLabel());
3995   (void)Patched;
3996   assert(Patched && "Invalid entry to be replaced in jump table");
3997   return true;
3998 }
3999 
4000 BinaryBasicBlock *BinaryFunction::splitEdge(BinaryBasicBlock *From,
4001                                             BinaryBasicBlock *To) {
4002   // Create intermediate BB
4003   MCSymbol *Tmp;
4004   {
4005     auto L = BC.scopeLock();
4006     Tmp = BC.Ctx->createNamedTempSymbol("SplitEdge");
4007   }
4008   // Link new BBs to the original input offset of the From BB, so we can map
4009   // samples recorded in new BBs back to the original BB seem in the input
4010   // binary (if using BAT)
4011   std::unique_ptr<BinaryBasicBlock> NewBB = createBasicBlock(Tmp);
4012   NewBB->setOffset(From->getInputOffset());
4013   BinaryBasicBlock *NewBBPtr = NewBB.get();
4014 
4015   // Update "From" BB
4016   auto I = From->succ_begin();
4017   auto BI = From->branch_info_begin();
4018   for (; I != From->succ_end(); ++I) {
4019     if (*I == To)
4020       break;
4021     ++BI;
4022   }
4023   assert(I != From->succ_end() && "Invalid CFG edge in splitEdge!");
4024   uint64_t OrigCount = BI->Count;
4025   uint64_t OrigMispreds = BI->MispredictedCount;
4026   replaceJumpTableEntryIn(From, To, NewBBPtr);
4027   From->replaceSuccessor(To, NewBBPtr, OrigCount, OrigMispreds);
4028 
4029   NewBB->addSuccessor(To, OrigCount, OrigMispreds);
4030   NewBB->setExecutionCount(OrigCount);
4031   NewBB->setIsCold(From->isCold());
4032 
4033   // Update CFI and BB layout with new intermediate BB
4034   std::vector<std::unique_ptr<BinaryBasicBlock>> NewBBs;
4035   NewBBs.emplace_back(std::move(NewBB));
4036   insertBasicBlocks(From, std::move(NewBBs), true, true,
4037                     /*RecomputeLandingPads=*/false);
4038   return NewBBPtr;
4039 }
4040 
4041 void BinaryFunction::deleteConservativeEdges() {
4042   // Our goal is to aggressively remove edges from the CFG that we believe are
4043   // wrong. This is used for instrumentation, where it is safe to remove
4044   // fallthrough edges because we won't reorder blocks.
4045   for (auto I = BasicBlocks.begin(), E = BasicBlocks.end(); I != E; ++I) {
4046     BinaryBasicBlock *BB = *I;
4047     if (BB->succ_size() != 1 || BB->size() == 0)
4048       continue;
4049 
4050     auto NextBB = std::next(I);
4051     MCInst *Last = BB->getLastNonPseudoInstr();
4052     // Fallthrough is a landing pad? Delete this edge (as long as we don't
4053     // have a direct jump to it)
4054     if ((*BB->succ_begin())->isLandingPad() && NextBB != E &&
4055         *BB->succ_begin() == *NextBB && Last && !BC.MIB->isBranch(*Last)) {
4056       BB->removeAllSuccessors();
4057       continue;
4058     }
4059 
4060     // Look for suspicious calls at the end of BB where gcc may optimize it and
4061     // remove the jump to the epilogue when it knows the call won't return.
4062     if (!Last || !BC.MIB->isCall(*Last))
4063       continue;
4064 
4065     const MCSymbol *CalleeSymbol = BC.MIB->getTargetSymbol(*Last);
4066     if (!CalleeSymbol)
4067       continue;
4068 
4069     StringRef CalleeName = CalleeSymbol->getName();
4070     if (CalleeName != "__cxa_throw@PLT" && CalleeName != "_Unwind_Resume@PLT" &&
4071         CalleeName != "__cxa_rethrow@PLT" && CalleeName != "exit@PLT" &&
4072         CalleeName != "abort@PLT")
4073       continue;
4074 
4075     BB->removeAllSuccessors();
4076   }
4077 }
4078 
4079 bool BinaryFunction::isSymbolValidInScope(const SymbolRef &Symbol,
4080                                           uint64_t SymbolSize) const {
4081   // If this symbol is in a different section from the one where the
4082   // function symbol is, don't consider it as valid.
4083   if (!getOriginSection()->containsAddress(
4084           cantFail(Symbol.getAddress(), "cannot get symbol address")))
4085     return false;
4086 
4087   // Some symbols are tolerated inside function bodies, others are not.
4088   // The real function boundaries may not be known at this point.
4089   if (BC.isMarker(Symbol))
4090     return true;
4091 
4092   // It's okay to have a zero-sized symbol in the middle of non-zero-sized
4093   // function.
4094   if (SymbolSize == 0 && containsAddress(cantFail(Symbol.getAddress())))
4095     return true;
4096 
4097   if (cantFail(Symbol.getType()) != SymbolRef::ST_Unknown)
4098     return false;
4099 
4100   if (cantFail(Symbol.getFlags()) & SymbolRef::SF_Global)
4101     return false;
4102 
4103   return true;
4104 }
4105 
4106 void BinaryFunction::adjustExecutionCount(uint64_t Count) {
4107   if (getKnownExecutionCount() == 0 || Count == 0)
4108     return;
4109 
4110   if (ExecutionCount < Count)
4111     Count = ExecutionCount;
4112 
4113   double AdjustmentRatio = ((double)ExecutionCount - Count) / ExecutionCount;
4114   if (AdjustmentRatio < 0.0)
4115     AdjustmentRatio = 0.0;
4116 
4117   for (BinaryBasicBlock &BB : blocks())
4118     BB.adjustExecutionCount(AdjustmentRatio);
4119 
4120   ExecutionCount -= Count;
4121 }
4122 
4123 BinaryFunction::~BinaryFunction() {
4124   for (BinaryBasicBlock *BB : BasicBlocks)
4125     delete BB;
4126   for (BinaryBasicBlock *BB : DeletedBasicBlocks)
4127     delete BB;
4128 }
4129 
4130 void BinaryFunction::constructDomTree() {
4131   BDT.reset(new BinaryDominatorTree);
4132   BDT->recalculate(*this);
4133 }
4134 
4135 void BinaryFunction::calculateLoopInfo() {
4136   if (!hasDomTree())
4137     constructDomTree();
4138   // Discover loops.
4139   BLI.reset(new BinaryLoopInfo());
4140   BLI->analyze(getDomTree());
4141 
4142   // Traverse discovered loops and add depth and profile information.
4143   std::stack<BinaryLoop *> St;
4144   for (auto I = BLI->begin(), E = BLI->end(); I != E; ++I) {
4145     St.push(*I);
4146     ++BLI->OuterLoops;
4147   }
4148 
4149   while (!St.empty()) {
4150     BinaryLoop *L = St.top();
4151     St.pop();
4152     ++BLI->TotalLoops;
4153     BLI->MaximumDepth = std::max(L->getLoopDepth(), BLI->MaximumDepth);
4154 
4155     // Add nested loops in the stack.
4156     for (BinaryLoop::iterator I = L->begin(), E = L->end(); I != E; ++I)
4157       St.push(*I);
4158 
4159     // Skip if no valid profile is found.
4160     if (!hasValidProfile()) {
4161       L->EntryCount = COUNT_NO_PROFILE;
4162       L->ExitCount = COUNT_NO_PROFILE;
4163       L->TotalBackEdgeCount = COUNT_NO_PROFILE;
4164       continue;
4165     }
4166 
4167     // Compute back edge count.
4168     SmallVector<BinaryBasicBlock *, 1> Latches;
4169     L->getLoopLatches(Latches);
4170 
4171     for (BinaryBasicBlock *Latch : Latches) {
4172       auto BI = Latch->branch_info_begin();
4173       for (BinaryBasicBlock *Succ : Latch->successors()) {
4174         if (Succ == L->getHeader()) {
4175           assert(BI->Count != BinaryBasicBlock::COUNT_NO_PROFILE &&
4176                  "profile data not found");
4177           L->TotalBackEdgeCount += BI->Count;
4178         }
4179         ++BI;
4180       }
4181     }
4182 
4183     // Compute entry count.
4184     L->EntryCount = L->getHeader()->getExecutionCount() - L->TotalBackEdgeCount;
4185 
4186     // Compute exit count.
4187     SmallVector<BinaryLoop::Edge, 1> ExitEdges;
4188     L->getExitEdges(ExitEdges);
4189     for (BinaryLoop::Edge &Exit : ExitEdges) {
4190       const BinaryBasicBlock *Exiting = Exit.first;
4191       const BinaryBasicBlock *ExitTarget = Exit.second;
4192       auto BI = Exiting->branch_info_begin();
4193       for (BinaryBasicBlock *Succ : Exiting->successors()) {
4194         if (Succ == ExitTarget) {
4195           assert(BI->Count != BinaryBasicBlock::COUNT_NO_PROFILE &&
4196                  "profile data not found");
4197           L->ExitCount += BI->Count;
4198         }
4199         ++BI;
4200       }
4201     }
4202   }
4203 }
4204 
4205 void BinaryFunction::updateOutputValues(const BOLTLinker &Linker) {
4206   if (!isEmitted()) {
4207     assert(!isInjected() && "injected function should be emitted");
4208     setOutputAddress(getAddress());
4209     setOutputSize(getSize());
4210     return;
4211   }
4212 
4213   const auto SymbolInfo = Linker.lookupSymbolInfo(getSymbol()->getName());
4214   assert(SymbolInfo && "Cannot find function entry symbol");
4215   setOutputAddress(SymbolInfo->Address);
4216   setOutputSize(SymbolInfo->Size);
4217 
4218   if (BC.HasRelocations || isInjected()) {
4219     if (hasConstantIsland()) {
4220       const auto DataAddress =
4221           Linker.lookupSymbol(getFunctionConstantIslandLabel()->getName());
4222       assert(DataAddress && "Cannot find function CI symbol");
4223       setOutputDataAddress(*DataAddress);
4224       for (auto It : Islands->Offsets) {
4225         const uint64_t OldOffset = It.first;
4226         BinaryData *BD = BC.getBinaryDataAtAddress(getAddress() + OldOffset);
4227         if (!BD)
4228           continue;
4229 
4230         MCSymbol *Symbol = It.second;
4231         const auto NewAddress = Linker.lookupSymbol(Symbol->getName());
4232         assert(NewAddress && "Cannot find CI symbol");
4233         auto &Section = *getCodeSection();
4234         const auto NewOffset = *NewAddress - Section.getOutputAddress();
4235         BD->setOutputLocation(Section, NewOffset);
4236       }
4237     }
4238     if (isSplit()) {
4239       for (FunctionFragment &FF : getLayout().getSplitFragments()) {
4240         ErrorOr<BinarySection &> ColdSection =
4241             getCodeSection(FF.getFragmentNum());
4242         // If fragment is empty, cold section might not exist
4243         if (FF.empty() && ColdSection.getError())
4244           continue;
4245 
4246         const MCSymbol *ColdStartSymbol = getSymbol(FF.getFragmentNum());
4247         // If fragment is empty, symbol might have not been emitted
4248         if (FF.empty() && (!ColdStartSymbol || !ColdStartSymbol->isDefined()) &&
4249             !hasConstantIsland())
4250           continue;
4251         assert(ColdStartSymbol && ColdStartSymbol->isDefined() &&
4252                "split function should have defined cold symbol");
4253         const auto ColdStartSymbolInfo =
4254             Linker.lookupSymbolInfo(ColdStartSymbol->getName());
4255         assert(ColdStartSymbolInfo && "Cannot find cold start symbol");
4256         FF.setAddress(ColdStartSymbolInfo->Address);
4257         FF.setImageSize(ColdStartSymbolInfo->Size);
4258         if (hasConstantIsland()) {
4259           const auto DataAddress = Linker.lookupSymbol(
4260               getFunctionColdConstantIslandLabel()->getName());
4261           assert(DataAddress && "Cannot find cold CI symbol");
4262           setOutputColdDataAddress(*DataAddress);
4263         }
4264       }
4265     }
4266   }
4267 
4268   // Update basic block output ranges for the debug info, if we have
4269   // secondary entry points in the symbol table to update or if writing BAT.
4270   if (!requiresAddressMap())
4271     return;
4272 
4273   // AArch64 may have functions that only contains a constant island (no code).
4274   if (getLayout().block_empty())
4275     return;
4276 
4277   for (FunctionFragment &FF : getLayout().fragments()) {
4278     if (FF.empty())
4279       continue;
4280 
4281     const uint64_t FragmentBaseAddress =
4282         getCodeSection(isSimple() ? FF.getFragmentNum() : FragmentNum::main())
4283             ->getOutputAddress();
4284 
4285     BinaryBasicBlock *PrevBB = nullptr;
4286     for (BinaryBasicBlock *const BB : FF) {
4287       assert(BB->getLabel()->isDefined() && "symbol should be defined");
4288       if (!BC.HasRelocations) {
4289         if (BB->isSplit())
4290           assert(FragmentBaseAddress == FF.getAddress());
4291         else
4292           assert(FragmentBaseAddress == getOutputAddress());
4293         (void)FragmentBaseAddress;
4294       }
4295 
4296       // Injected functions likely will fail lookup, as they have no
4297       // input range. Just assign the BB the output address of the
4298       // function.
4299       auto MaybeBBAddress = BC.getIOAddressMap().lookup(BB->getLabel());
4300       const uint64_t BBAddress = MaybeBBAddress  ? *MaybeBBAddress
4301                                  : BB->isSplit() ? FF.getAddress()
4302                                                  : getOutputAddress();
4303       BB->setOutputStartAddress(BBAddress);
4304 
4305       if (PrevBB) {
4306         assert(PrevBB->getOutputAddressRange().first <= BBAddress &&
4307                "Bad output address for basic block.");
4308         assert((PrevBB->getOutputAddressRange().first != BBAddress ||
4309                 !hasInstructions() || !PrevBB->getNumNonPseudos()) &&
4310                "Bad output address for basic block.");
4311         PrevBB->setOutputEndAddress(BBAddress);
4312       }
4313       PrevBB = BB;
4314     }
4315 
4316     PrevBB->setOutputEndAddress(PrevBB->isSplit()
4317                                     ? FF.getAddress() + FF.getImageSize()
4318                                     : getOutputAddress() + getOutputSize());
4319   }
4320 
4321   // Reset output addresses for deleted blocks.
4322   for (BinaryBasicBlock *BB : DeletedBasicBlocks) {
4323     BB->setOutputStartAddress(0);
4324     BB->setOutputEndAddress(0);
4325   }
4326 }
4327 
4328 DebugAddressRangesVector BinaryFunction::getOutputAddressRanges() const {
4329   DebugAddressRangesVector OutputRanges;
4330 
4331   if (isFolded())
4332     return OutputRanges;
4333 
4334   if (IsFragment)
4335     return OutputRanges;
4336 
4337   OutputRanges.emplace_back(getOutputAddress(),
4338                             getOutputAddress() + getOutputSize());
4339   if (isSplit()) {
4340     assert(isEmitted() && "split function should be emitted");
4341     for (const FunctionFragment &FF : getLayout().getSplitFragments())
4342       OutputRanges.emplace_back(FF.getAddress(),
4343                                 FF.getAddress() + FF.getImageSize());
4344   }
4345 
4346   if (isSimple())
4347     return OutputRanges;
4348 
4349   for (BinaryFunction *Frag : Fragments) {
4350     assert(!Frag->isSimple() &&
4351            "fragment of non-simple function should also be non-simple");
4352     OutputRanges.emplace_back(Frag->getOutputAddress(),
4353                               Frag->getOutputAddress() + Frag->getOutputSize());
4354   }
4355 
4356   return OutputRanges;
4357 }
4358 
4359 uint64_t BinaryFunction::translateInputToOutputAddress(uint64_t Address) const {
4360   if (isFolded())
4361     return 0;
4362 
4363   // If the function hasn't changed return the same address.
4364   if (!isEmitted())
4365     return Address;
4366 
4367   if (Address < getAddress())
4368     return 0;
4369 
4370   // Check if the address is associated with an instruction that is tracked
4371   // by address translation.
4372   if (auto OutputAddress = BC.getIOAddressMap().lookup(Address))
4373     return *OutputAddress;
4374 
4375   // FIXME: #18950828 - we rely on relative offsets inside basic blocks to stay
4376   //        intact. Instead we can use pseudo instructions and/or annotations.
4377   const uint64_t Offset = Address - getAddress();
4378   const BinaryBasicBlock *BB = getBasicBlockContainingOffset(Offset);
4379   if (!BB) {
4380     // Special case for address immediately past the end of the function.
4381     if (Offset == getSize())
4382       return getOutputAddress() + getOutputSize();
4383 
4384     return 0;
4385   }
4386 
4387   return std::min(BB->getOutputAddressRange().first + Offset - BB->getOffset(),
4388                   BB->getOutputAddressRange().second);
4389 }
4390 
4391 DebugAddressRangesVector
4392 BinaryFunction::translateInputToOutputRange(DebugAddressRange InRange) const {
4393   DebugAddressRangesVector OutRanges;
4394 
4395   // The function was removed from the output. Return an empty range.
4396   if (isFolded())
4397     return OutRanges;
4398 
4399   // If the function hasn't changed return the same range.
4400   if (!isEmitted()) {
4401     OutRanges.emplace_back(InRange);
4402     return OutRanges;
4403   }
4404 
4405   if (!containsAddress(InRange.LowPC))
4406     return OutRanges;
4407 
4408   // Special case of an empty range [X, X). Some tools expect X to be updated.
4409   if (InRange.LowPC == InRange.HighPC) {
4410     if (uint64_t NewPC = translateInputToOutputAddress(InRange.LowPC))
4411       OutRanges.push_back(DebugAddressRange{NewPC, NewPC});
4412     return OutRanges;
4413   }
4414 
4415   uint64_t InputOffset = InRange.LowPC - getAddress();
4416   const uint64_t InputEndOffset =
4417       std::min(InRange.HighPC - getAddress(), getSize());
4418 
4419   auto BBI = llvm::upper_bound(BasicBlockOffsets,
4420                                BasicBlockOffset(InputOffset, nullptr),
4421                                CompareBasicBlockOffsets());
4422   assert(BBI != BasicBlockOffsets.begin());
4423 
4424   // Iterate over blocks in the input order using BasicBlockOffsets.
4425   for (--BBI; InputOffset < InputEndOffset && BBI != BasicBlockOffsets.end();
4426        InputOffset = BBI->second->getEndOffset(), ++BBI) {
4427     const BinaryBasicBlock &BB = *BBI->second;
4428     if (InputOffset < BB.getOffset() || InputOffset >= BB.getEndOffset()) {
4429       LLVM_DEBUG(
4430           dbgs() << "BOLT-DEBUG: invalid debug address range detected for "
4431                  << *this << " : [0x" << Twine::utohexstr(InRange.LowPC)
4432                  << ", 0x" << Twine::utohexstr(InRange.HighPC) << "]\n");
4433       break;
4434     }
4435 
4436     // Skip the block if it wasn't emitted.
4437     if (!BB.getOutputAddressRange().first)
4438       continue;
4439 
4440     // Find output address for an instruction with an offset greater or equal
4441     // to /p Offset. The output address should fall within the same basic
4442     // block boundaries.
4443     auto translateBlockOffset = [&](const uint64_t Offset) {
4444       const uint64_t OutAddress = BB.getOutputAddressRange().first + Offset;
4445       return std::min(OutAddress, BB.getOutputAddressRange().second);
4446     };
4447 
4448     uint64_t OutLowPC = BB.getOutputAddressRange().first;
4449     if (InputOffset > BB.getOffset())
4450       OutLowPC = translateBlockOffset(InputOffset - BB.getOffset());
4451 
4452     uint64_t OutHighPC = BB.getOutputAddressRange().second;
4453     if (InputEndOffset < BB.getEndOffset()) {
4454       assert(InputEndOffset >= BB.getOffset());
4455       OutHighPC = translateBlockOffset(InputEndOffset - BB.getOffset());
4456     }
4457 
4458     // Check if we can expand the last translated range.
4459     if (!OutRanges.empty() && OutRanges.back().HighPC == OutLowPC)
4460       OutRanges.back().HighPC = std::max(OutRanges.back().HighPC, OutHighPC);
4461     else
4462       OutRanges.emplace_back(OutLowPC, std::max(OutLowPC, OutHighPC));
4463   }
4464 
4465   LLVM_DEBUG({
4466     dbgs() << "BOLT-DEBUG: translated address range " << InRange << " -> ";
4467     for (const DebugAddressRange &R : OutRanges)
4468       dbgs() << R << ' ';
4469     dbgs() << '\n';
4470   });
4471 
4472   return OutRanges;
4473 }
4474 
4475 MCInst *BinaryFunction::getInstructionAtOffset(uint64_t Offset) {
4476   if (CurrentState == State::Disassembled) {
4477     auto II = Instructions.find(Offset);
4478     return (II == Instructions.end()) ? nullptr : &II->second;
4479   } else if (CurrentState == State::CFG) {
4480     BinaryBasicBlock *BB = getBasicBlockContainingOffset(Offset);
4481     if (!BB)
4482       return nullptr;
4483 
4484     for (MCInst &Inst : *BB) {
4485       constexpr uint32_t InvalidOffset = std::numeric_limits<uint32_t>::max();
4486       if (Offset == BC.MIB->getOffsetWithDefault(Inst, InvalidOffset))
4487         return &Inst;
4488     }
4489 
4490     if (MCInst *LastInstr = BB->getLastNonPseudoInstr()) {
4491       if (std::optional<uint32_t> Size = BC.MIB->getSize(*LastInstr)) {
4492         if (BB->getEndOffset() - Offset == Size) {
4493           return LastInstr;
4494         }
4495       }
4496     }
4497 
4498     return nullptr;
4499   } else {
4500     llvm_unreachable("invalid CFG state to use getInstructionAtOffset()");
4501   }
4502 }
4503 
4504 MCInst *BinaryFunction::getInstructionContainingOffset(uint64_t Offset) {
4505   assert(CurrentState == State::Disassembled && "Wrong function state");
4506 
4507   if (Offset > Size)
4508     return nullptr;
4509 
4510   auto II = Instructions.upper_bound(Offset);
4511   assert(II != Instructions.begin() && "First instruction not at offset 0");
4512   --II;
4513   return &II->second;
4514 }
4515 
4516 void BinaryFunction::printLoopInfo(raw_ostream &OS) const {
4517   if (!opts::shouldPrint(*this))
4518     return;
4519 
4520   OS << "Loop Info for Function \"" << *this << "\"";
4521   if (hasValidProfile())
4522     OS << " (count: " << getExecutionCount() << ")";
4523   OS << "\n";
4524 
4525   std::stack<BinaryLoop *> St;
4526   for (BinaryLoop *L : *BLI)
4527     St.push(L);
4528   while (!St.empty()) {
4529     BinaryLoop *L = St.top();
4530     St.pop();
4531 
4532     for (BinaryLoop *Inner : *L)
4533       St.push(Inner);
4534 
4535     if (!hasValidProfile())
4536       continue;
4537 
4538     OS << (L->getLoopDepth() > 1 ? "Nested" : "Outer")
4539        << " loop header: " << L->getHeader()->getName();
4540     OS << "\n";
4541     OS << "Loop basic blocks: ";
4542     ListSeparator LS;
4543     for (BinaryBasicBlock *BB : L->blocks())
4544       OS << LS << BB->getName();
4545     OS << "\n";
4546     if (hasValidProfile()) {
4547       OS << "Total back edge count: " << L->TotalBackEdgeCount << "\n";
4548       OS << "Loop entry count: " << L->EntryCount << "\n";
4549       OS << "Loop exit count: " << L->ExitCount << "\n";
4550       if (L->EntryCount > 0) {
4551         OS << "Average iters per entry: "
4552            << format("%.4lf", (double)L->TotalBackEdgeCount / L->EntryCount)
4553            << "\n";
4554       }
4555     }
4556     OS << "----\n";
4557   }
4558 
4559   OS << "Total number of loops: " << BLI->TotalLoops << "\n";
4560   OS << "Number of outer loops: " << BLI->OuterLoops << "\n";
4561   OS << "Maximum nested loop depth: " << BLI->MaximumDepth << "\n\n";
4562 }
4563 
4564 bool BinaryFunction::isAArch64Veneer() const {
4565   if (empty() || hasIslandsInfo())
4566     return false;
4567 
4568   BinaryBasicBlock &BB = **BasicBlocks.begin();
4569   for (MCInst &Inst : BB)
4570     if (!BC.MIB->hasAnnotation(Inst, "AArch64Veneer"))
4571       return false;
4572 
4573   for (auto I = BasicBlocks.begin() + 1, E = BasicBlocks.end(); I != E; ++I) {
4574     for (MCInst &Inst : **I)
4575       if (!BC.MIB->isNoop(Inst))
4576         return false;
4577   }
4578 
4579   return true;
4580 }
4581 
4582 void BinaryFunction::addRelocation(uint64_t Address, MCSymbol *Symbol,
4583                                    uint64_t RelType, uint64_t Addend,
4584                                    uint64_t Value) {
4585   assert(Address >= getAddress() && Address < getAddress() + getMaxSize() &&
4586          "address is outside of the function");
4587   uint64_t Offset = Address - getAddress();
4588   LLVM_DEBUG(dbgs() << "BOLT-DEBUG: addRelocation in "
4589                     << formatv("{0}@{1:x} against {2}\n", *this, Offset,
4590                                (Symbol ? Symbol->getName() : "<undef>")));
4591   bool IsCI = BC.isAArch64() && isInConstantIsland(Address);
4592   std::map<uint64_t, Relocation> &Rels =
4593       IsCI ? Islands->Relocations : Relocations;
4594   if (BC.MIB->shouldRecordCodeRelocation(RelType))
4595     Rels[Offset] = Relocation{Offset, Symbol, RelType, Addend, Value};
4596 }
4597 
4598 } // namespace bolt
4599 } // namespace llvm
4600