xref: /llvm-project/bolt/lib/Profile/BoltAddressTranslation.cpp (revision dc1da93958be0311b79dce39d71bd954c478cf19)
1 //===- bolt/Profile/BoltAddressTranslation.cpp ----------------------------===//
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 #include "bolt/Profile/BoltAddressTranslation.h"
10 #include "bolt/Core/BinaryFunction.h"
11 #include "llvm/ADT/APInt.h"
12 #include "llvm/Support/Errc.h"
13 #include "llvm/Support/Error.h"
14 #include "llvm/Support/LEB128.h"
15 
16 #define DEBUG_TYPE "bolt-bat"
17 
18 namespace llvm {
19 namespace bolt {
20 
21 const char *BoltAddressTranslation::SECTION_NAME = ".note.bolt_bat";
22 
23 void BoltAddressTranslation::writeEntriesForBB(
24     MapTy &Map, const BinaryBasicBlock &BB, uint64_t FuncInputAddress,
25     uint64_t FuncOutputAddress) const {
26   const uint64_t BBOutputOffset =
27       BB.getOutputAddressRange().first - FuncOutputAddress;
28   const uint32_t BBInputOffset = BB.getInputOffset();
29 
30   // Every output BB must track back to an input BB for profile collection
31   // in bolted binaries. If we are missing an offset, it means this block was
32   // created by a pass. We will skip writing any entries for it, and this means
33   // any traffic happening in this block will map to the previous block in the
34   // layout. This covers the case where an input basic block is split into two,
35   // and the second one lacks any offset.
36   if (BBInputOffset == BinaryBasicBlock::INVALID_OFFSET)
37     return;
38 
39   LLVM_DEBUG(dbgs() << "BB " << BB.getName() << "\n");
40   LLVM_DEBUG(dbgs() << "  Key: " << Twine::utohexstr(BBOutputOffset)
41                     << " Val: " << Twine::utohexstr(BBInputOffset) << "\n");
42   // NB: in `writeEntriesForBB` we use the input address because hashes are
43   // saved early in `saveMetadata` before output addresses are assigned.
44   const BBHashMapTy &BBHashMap = getBBHashMap(FuncInputAddress);
45   (void)BBHashMap;
46   LLVM_DEBUG(
47       dbgs() << formatv(" Hash: {0:x}\n", BBHashMap.getBBHash(BBInputOffset)));
48   LLVM_DEBUG(
49       dbgs() << formatv(" Index: {0}\n", BBHashMap.getBBIndex(BBInputOffset)));
50   // In case of conflicts (same Key mapping to different Vals), the last
51   // update takes precedence. Of course it is not ideal to have conflicts and
52   // those happen when we have an empty BB that either contained only
53   // NOPs or a jump to the next block (successor). Either way, the successor
54   // and this deleted block will both share the same output address (the same
55   // key), and we need to map back. We choose here to privilege the successor by
56   // allowing it to overwrite the previously inserted key in the map.
57   Map.emplace(BBOutputOffset, BBInputOffset << 1);
58 
59   const auto &IOAddressMap =
60       BB.getFunction()->getBinaryContext().getIOAddressMap();
61 
62   for (const auto &[InputOffset, Sym] : BB.getLocSyms()) {
63     const auto InputAddress = BB.getFunction()->getAddress() + InputOffset;
64     const auto OutputAddress = IOAddressMap.lookup(InputAddress);
65     assert(OutputAddress && "Unknown instruction address");
66     const auto OutputOffset = *OutputAddress - FuncOutputAddress;
67 
68     // Is this the first instruction in the BB? No need to duplicate the entry.
69     if (OutputOffset == BBOutputOffset)
70       continue;
71 
72     LLVM_DEBUG(dbgs() << "  Key: " << Twine::utohexstr(OutputOffset) << " Val: "
73                       << Twine::utohexstr(InputOffset) << " (branch)\n");
74     Map.emplace(OutputOffset, (InputOffset << 1) | BRANCHENTRY);
75   }
76 }
77 
78 void BoltAddressTranslation::write(const BinaryContext &BC, raw_ostream &OS) {
79   LLVM_DEBUG(dbgs() << "BOLT-DEBUG: Writing BOLT Address Translation Tables\n");
80   for (auto &BFI : BC.getBinaryFunctions()) {
81     const BinaryFunction &Function = BFI.second;
82     const uint64_t InputAddress = Function.getAddress();
83     const uint64_t OutputAddress = Function.getOutputAddress();
84     // We don't need a translation table if the body of the function hasn't
85     // changed
86     if (Function.isIgnored() || (!BC.HasRelocations && !Function.isSimple()))
87       continue;
88 
89     uint32_t NumSecondaryEntryPoints = 0;
90     Function.forEachEntryPoint([&](uint64_t Offset, const MCSymbol *) {
91       if (!Offset)
92         return true;
93       ++NumSecondaryEntryPoints;
94       SecondaryEntryPointsMap[OutputAddress].push_back(Offset);
95       return true;
96     });
97 
98     LLVM_DEBUG(dbgs() << "Function name: " << Function.getPrintName() << "\n");
99     LLVM_DEBUG(dbgs() << " Address reference: 0x"
100                       << Twine::utohexstr(Function.getOutputAddress()) << "\n");
101     LLVM_DEBUG(dbgs() << formatv(" Hash: {0:x}\n", getBFHash(InputAddress)));
102     LLVM_DEBUG(dbgs() << " Secondary Entry Points: " << NumSecondaryEntryPoints
103                       << '\n');
104 
105     MapTy Map;
106     for (const BinaryBasicBlock *const BB :
107          Function.getLayout().getMainFragment())
108       writeEntriesForBB(Map, *BB, InputAddress, OutputAddress);
109     // Add entries for deleted blocks. They are still required for correct BB
110     // mapping of branches modified by SCTC. By convention, they would have the
111     // end of the function as output address.
112     const BBHashMapTy &BBHashMap = getBBHashMap(InputAddress);
113     if (BBHashMap.size() != Function.size()) {
114       const uint64_t EndOffset = Function.getOutputSize();
115       std::unordered_set<uint32_t> MappedInputOffsets;
116       for (const BinaryBasicBlock &BB : Function)
117         MappedInputOffsets.emplace(BB.getInputOffset());
118       for (const auto &[InputOffset, _] : BBHashMap)
119         if (!llvm::is_contained(MappedInputOffsets, InputOffset))
120           Map.emplace(EndOffset, InputOffset << 1);
121     }
122     Maps.emplace(Function.getOutputAddress(), std::move(Map));
123     ReverseMap.emplace(OutputAddress, InputAddress);
124 
125     if (!Function.isSplit())
126       continue;
127 
128     // Split maps
129     LLVM_DEBUG(dbgs() << " Cold part\n");
130     for (const FunctionFragment &FF :
131          Function.getLayout().getSplitFragments()) {
132       // Skip empty fragments to avoid adding zero-address entries to maps.
133       if (FF.empty())
134         continue;
135       ColdPartSource.emplace(FF.getAddress(), Function.getOutputAddress());
136       Map.clear();
137       for (const BinaryBasicBlock *const BB : FF)
138         writeEntriesForBB(Map, *BB, InputAddress, FF.getAddress());
139 
140       Maps.emplace(FF.getAddress(), std::move(Map));
141     }
142   }
143 
144   // Output addresses are delta-encoded
145   uint64_t PrevAddress = 0;
146   writeMaps</*Cold=*/false>(Maps, PrevAddress, OS);
147   writeMaps</*Cold=*/true>(Maps, PrevAddress, OS);
148 
149   BC.outs() << "BOLT-INFO: Wrote " << Maps.size() << " BAT maps\n";
150   BC.outs() << "BOLT-INFO: Wrote " << FuncHashes.getNumFunctions()
151             << " function and " << FuncHashes.getNumBasicBlocks()
152             << " basic block hashes\n";
153 }
154 
155 APInt BoltAddressTranslation::calculateBranchEntriesBitMask(
156     MapTy &Map, size_t EqualElems) const {
157   APInt BitMask(alignTo(EqualElems, 8), 0);
158   size_t Index = 0;
159   for (std::pair<const uint32_t, uint32_t> &KeyVal : Map) {
160     if (Index == EqualElems)
161       break;
162     const uint32_t OutputOffset = KeyVal.second;
163     if (OutputOffset & BRANCHENTRY)
164       BitMask.setBit(Index);
165     ++Index;
166   }
167   return BitMask;
168 }
169 
170 size_t BoltAddressTranslation::getNumEqualOffsets(const MapTy &Map,
171                                                   uint32_t Skew) const {
172   size_t EqualOffsets = 0;
173   for (const std::pair<const uint32_t, uint32_t> &KeyVal : Map) {
174     const uint32_t OutputOffset = KeyVal.first;
175     const uint32_t InputOffset = KeyVal.second >> 1;
176     if (OutputOffset == InputOffset - Skew)
177       ++EqualOffsets;
178     else
179       break;
180   }
181   return EqualOffsets;
182 }
183 
184 template <bool Cold>
185 void BoltAddressTranslation::writeMaps(std::map<uint64_t, MapTy> &Maps,
186                                        uint64_t &PrevAddress, raw_ostream &OS) {
187   const uint32_t NumFuncs =
188       llvm::count_if(llvm::make_first_range(Maps), [&](const uint64_t Address) {
189         return Cold == ColdPartSource.count(Address);
190       });
191   encodeULEB128(NumFuncs, OS);
192   LLVM_DEBUG(dbgs() << "Writing " << NumFuncs << (Cold ? " cold" : "")
193                     << " functions for BAT.\n");
194   size_t PrevIndex = 0;
195   for (auto &MapEntry : Maps) {
196     const uint64_t Address = MapEntry.first;
197     // Only process cold fragments in cold mode, and vice versa.
198     if (Cold != ColdPartSource.count(Address))
199       continue;
200     // NB: in `writeMaps` we use the input address because hashes are saved
201     // early in `saveMetadata` before output addresses are assigned.
202     const uint64_t HotInputAddress =
203         ReverseMap[Cold ? ColdPartSource[Address] : Address];
204     MapTy &Map = MapEntry.second;
205     const uint32_t NumEntries = Map.size();
206     LLVM_DEBUG(dbgs() << "Writing " << NumEntries << " entries for 0x"
207                       << Twine::utohexstr(Address) << ".\n");
208     encodeULEB128(Address - PrevAddress, OS);
209     PrevAddress = Address;
210     const uint32_t NumSecondaryEntryPoints =
211         SecondaryEntryPointsMap.count(Address)
212             ? SecondaryEntryPointsMap[Address].size()
213             : 0;
214     uint32_t Skew = 0;
215     if (Cold) {
216       auto HotEntryIt = Maps.find(ColdPartSource[Address]);
217       assert(HotEntryIt != Maps.end());
218       size_t HotIndex = std::distance(Maps.begin(), HotEntryIt);
219       encodeULEB128(HotIndex - PrevIndex, OS);
220       PrevIndex = HotIndex;
221       // Skew of all input offsets for cold fragments is simply the first input
222       // offset.
223       Skew = Map.begin()->second >> 1;
224       encodeULEB128(Skew, OS);
225     } else {
226       // Function hash
227       size_t BFHash = getBFHash(HotInputAddress);
228       LLVM_DEBUG(dbgs() << "Hash: " << formatv("{0:x}\n", BFHash));
229       OS.write(reinterpret_cast<char *>(&BFHash), 8);
230       // Number of basic blocks
231       size_t NumBasicBlocks = NumBasicBlocksMap[HotInputAddress];
232       LLVM_DEBUG(dbgs() << "Basic blocks: " << NumBasicBlocks << '\n');
233       encodeULEB128(NumBasicBlocks, OS);
234       // Secondary entry points
235       encodeULEB128(NumSecondaryEntryPoints, OS);
236       LLVM_DEBUG(dbgs() << "Secondary Entry Points: " << NumSecondaryEntryPoints
237                         << '\n');
238     }
239     encodeULEB128(NumEntries, OS);
240     // Encode the number of equal offsets (output = input - skew) in the
241     // beginning of the function. Only encode one offset in these cases.
242     const size_t EqualElems = getNumEqualOffsets(Map, Skew);
243     encodeULEB128(EqualElems, OS);
244     if (EqualElems) {
245       const size_t BranchEntriesBytes = alignTo(EqualElems, 8) / 8;
246       APInt BranchEntries = calculateBranchEntriesBitMask(Map, EqualElems);
247       OS.write(reinterpret_cast<const char *>(BranchEntries.getRawData()),
248                BranchEntriesBytes);
249       LLVM_DEBUG({
250         dbgs() << "BranchEntries: ";
251         SmallString<8> BitMaskStr;
252         BranchEntries.toString(BitMaskStr, 2, false);
253         dbgs() << BitMaskStr << '\n';
254       });
255     }
256     const BBHashMapTy &BBHashMap = getBBHashMap(HotInputAddress);
257     size_t Index = 0;
258     uint64_t InOffset = 0;
259     size_t PrevBBIndex = 0;
260     // Output and Input addresses and delta-encoded
261     for (std::pair<const uint32_t, uint32_t> &KeyVal : Map) {
262       const uint64_t OutputAddress = KeyVal.first + Address;
263       encodeULEB128(OutputAddress - PrevAddress, OS);
264       PrevAddress = OutputAddress;
265       if (Index++ >= EqualElems)
266         encodeSLEB128(KeyVal.second - InOffset, OS);
267       InOffset = KeyVal.second; // Keeping InOffset as if BRANCHENTRY is encoded
268       if ((InOffset & BRANCHENTRY) == 0) {
269         const bool IsBlock = BBHashMap.isInputBlock(InOffset >> 1);
270         unsigned BBIndex = IsBlock ? BBHashMap.getBBIndex(InOffset >> 1) : 0;
271         size_t BBHash = IsBlock ? BBHashMap.getBBHash(InOffset >> 1) : 0;
272         OS.write(reinterpret_cast<char *>(&BBHash), 8);
273         // Basic block index in the input binary
274         encodeULEB128(BBIndex - PrevBBIndex, OS);
275         PrevBBIndex = BBIndex;
276         LLVM_DEBUG(dbgs() << formatv("{0:x} -> {1:x} {2:x} {3}\n", KeyVal.first,
277                                      InOffset >> 1, BBHash, BBIndex));
278       }
279     }
280     uint32_t PrevOffset = 0;
281     if (!Cold && NumSecondaryEntryPoints) {
282       LLVM_DEBUG(dbgs() << "Secondary entry points: ");
283       // Secondary entry point offsets, delta-encoded
284       for (uint32_t Offset : SecondaryEntryPointsMap[Address]) {
285         encodeULEB128(Offset - PrevOffset, OS);
286         LLVM_DEBUG(dbgs() << formatv("{0:x} ", Offset));
287         PrevOffset = Offset;
288       }
289       LLVM_DEBUG(dbgs() << '\n');
290     }
291   }
292 }
293 
294 std::error_code BoltAddressTranslation::parse(raw_ostream &OS, StringRef Buf) {
295   DataExtractor DE = DataExtractor(Buf, true, 8);
296   uint64_t Offset = 0;
297   if (Buf.size() < 12)
298     return make_error_code(llvm::errc::io_error);
299 
300   const uint32_t NameSz = DE.getU32(&Offset);
301   const uint32_t DescSz = DE.getU32(&Offset);
302   const uint32_t Type = DE.getU32(&Offset);
303 
304   if (Type != BinarySection::NT_BOLT_BAT ||
305       Buf.size() + Offset < alignTo(NameSz, 4) + DescSz)
306     return make_error_code(llvm::errc::io_error);
307 
308   StringRef Name = Buf.slice(Offset, Offset + NameSz);
309   Offset = alignTo(Offset + NameSz, 4);
310   if (!Name.starts_with("BOLT"))
311     return make_error_code(llvm::errc::io_error);
312 
313   Error Err(Error::success());
314   std::vector<uint64_t> HotFuncs;
315   uint64_t PrevAddress = 0;
316   parseMaps</*Cold=*/false>(HotFuncs, PrevAddress, DE, Offset, Err);
317   parseMaps</*Cold=*/true>(HotFuncs, PrevAddress, DE, Offset, Err);
318   OS << "BOLT-INFO: Parsed " << Maps.size() << " BAT entries\n";
319   return errorToErrorCode(std::move(Err));
320 }
321 
322 template <bool Cold>
323 void BoltAddressTranslation::parseMaps(std::vector<uint64_t> &HotFuncs,
324                                        uint64_t &PrevAddress, DataExtractor &DE,
325                                        uint64_t &Offset, Error &Err) {
326   const uint32_t NumFunctions = DE.getULEB128(&Offset, &Err);
327   LLVM_DEBUG(dbgs() << "Parsing " << NumFunctions << (Cold ? " cold" : "")
328                     << " functions\n");
329   size_t HotIndex = 0;
330   for (uint32_t I = 0; I < NumFunctions; ++I) {
331     const uint64_t Address = PrevAddress + DE.getULEB128(&Offset, &Err);
332     uint64_t HotAddress = Cold ? 0 : Address;
333     PrevAddress = Address;
334     uint32_t SecondaryEntryPoints = 0;
335     uint64_t ColdInputSkew = 0;
336     if (Cold) {
337       HotIndex += DE.getULEB128(&Offset, &Err);
338       HotAddress = HotFuncs[HotIndex];
339       ColdPartSource.emplace(Address, HotAddress);
340       ColdInputSkew = DE.getULEB128(&Offset, &Err);
341     } else {
342       HotFuncs.push_back(Address);
343       // Function hash
344       const size_t FuncHash = DE.getU64(&Offset, &Err);
345       FuncHashes.addEntry(Address, FuncHash);
346       LLVM_DEBUG(dbgs() << formatv("{0:x}: hash {1:x}\n", Address, FuncHash));
347       // Number of basic blocks
348       const size_t NumBasicBlocks = DE.getULEB128(&Offset, &Err);
349       NumBasicBlocksMap.emplace(Address, NumBasicBlocks);
350       LLVM_DEBUG(dbgs() << formatv("{0:x}: #bbs {1}, {2} bytes\n", Address,
351                                    NumBasicBlocks,
352                                    getULEB128Size(NumBasicBlocks)));
353       // Secondary entry points
354       SecondaryEntryPoints = DE.getULEB128(&Offset, &Err);
355       LLVM_DEBUG(
356           dbgs() << formatv("{0:x}: secondary entry points {1}, {2} bytes\n",
357                             Address, SecondaryEntryPoints,
358                             getULEB128Size(SecondaryEntryPoints)));
359     }
360     const uint32_t NumEntries = DE.getULEB128(&Offset, &Err);
361     // Equal offsets.
362     const size_t EqualElems = DE.getULEB128(&Offset, &Err);
363     APInt BEBitMask;
364     LLVM_DEBUG(dbgs() << formatv("Equal offsets: {0}, {1} bytes\n", EqualElems,
365                                  getULEB128Size(EqualElems)));
366     if (EqualElems) {
367       const size_t BranchEntriesBytes = alignTo(EqualElems, 8) / 8;
368       BEBitMask = APInt(alignTo(EqualElems, 8), 0);
369       LoadIntFromMemory(
370           BEBitMask,
371           reinterpret_cast<const uint8_t *>(
372               DE.getBytes(&Offset, BranchEntriesBytes, &Err).data()),
373           BranchEntriesBytes);
374       LLVM_DEBUG({
375         dbgs() << "BEBitMask: ";
376         SmallString<8> BitMaskStr;
377         BEBitMask.toString(BitMaskStr, 2, false);
378         dbgs() << BitMaskStr << ", " << BranchEntriesBytes << " bytes\n";
379       });
380     }
381     MapTy Map;
382 
383     LLVM_DEBUG(dbgs() << "Parsing " << NumEntries << " entries for 0x"
384                       << Twine::utohexstr(Address) << "\n");
385     uint64_t InputOffset = 0;
386     size_t BBIndex = 0;
387     for (uint32_t J = 0; J < NumEntries; ++J) {
388       const uint64_t OutputDelta = DE.getULEB128(&Offset, &Err);
389       const uint64_t OutputAddress = PrevAddress + OutputDelta;
390       const uint64_t OutputOffset = OutputAddress - Address;
391       PrevAddress = OutputAddress;
392       int64_t InputDelta = 0;
393       if (J < EqualElems) {
394         InputOffset = ((OutputOffset + ColdInputSkew) << 1) | BEBitMask[J];
395       } else {
396         InputDelta = DE.getSLEB128(&Offset, &Err);
397         InputOffset += InputDelta;
398       }
399       Map.insert(std::pair<uint32_t, uint32_t>(OutputOffset, InputOffset));
400       size_t BBHash = 0;
401       size_t BBIndexDelta = 0;
402       const bool IsBranchEntry = InputOffset & BRANCHENTRY;
403       if (!IsBranchEntry) {
404         BBHash = DE.getU64(&Offset, &Err);
405         BBIndexDelta = DE.getULEB128(&Offset, &Err);
406         BBIndex += BBIndexDelta;
407         // Map basic block hash to hot fragment by input offset
408         getBBHashMap(HotAddress).addEntry(InputOffset >> 1, BBIndex, BBHash);
409       }
410       LLVM_DEBUG({
411         dbgs() << formatv(
412             "{0:x} -> {1:x} ({2}/{3}b -> {4}/{5}b), {6:x}", OutputOffset,
413             InputOffset, OutputDelta, getULEB128Size(OutputDelta), InputDelta,
414             (J < EqualElems) ? 0 : getSLEB128Size(InputDelta), OutputAddress);
415         if (!IsBranchEntry) {
416           dbgs() << formatv(" {0:x} {1}/{2}b", BBHash, BBIndex,
417                             getULEB128Size(BBIndexDelta));
418         }
419         dbgs() << '\n';
420       });
421     }
422     Maps.insert(std::pair<uint64_t, MapTy>(Address, Map));
423     if (!Cold && SecondaryEntryPoints) {
424       uint32_t EntryPointOffset = 0;
425       LLVM_DEBUG(dbgs() << "Secondary entry points: ");
426       for (uint32_t EntryPointId = 0; EntryPointId != SecondaryEntryPoints;
427            ++EntryPointId) {
428         uint32_t OffsetDelta = DE.getULEB128(&Offset, &Err);
429         EntryPointOffset += OffsetDelta;
430         SecondaryEntryPointsMap[Address].push_back(EntryPointOffset);
431         LLVM_DEBUG(dbgs() << formatv("{0:x}/{1}b ", EntryPointOffset,
432                                      getULEB128Size(OffsetDelta)));
433       }
434       LLVM_DEBUG(dbgs() << '\n');
435     }
436   }
437 }
438 
439 void BoltAddressTranslation::dump(raw_ostream &OS) const {
440   const size_t NumTables = Maps.size();
441   OS << "BAT tables for " << NumTables << " functions:\n";
442   for (const auto &MapEntry : Maps) {
443     const uint64_t Address = MapEntry.first;
444     const uint64_t HotAddress = fetchParentAddress(Address);
445     const bool IsHotFunction = HotAddress == 0;
446     OS << "Function Address: 0x" << Twine::utohexstr(Address);
447     if (IsHotFunction)
448       OS << formatv(", hash: {0:x}", getBFHash(Address));
449     OS << "\n";
450     OS << "BB mappings:\n";
451     const BBHashMapTy &BBHashMap =
452         getBBHashMap(HotAddress ? HotAddress : Address);
453     for (const auto &Entry : MapEntry.second) {
454       const bool IsBranch = Entry.second & BRANCHENTRY;
455       const uint32_t Val = Entry.second >> 1; // dropping BRANCHENTRY bit
456       OS << "0x" << Twine::utohexstr(Entry.first) << " -> "
457          << "0x" << Twine::utohexstr(Val);
458       if (IsBranch)
459         OS << " (branch)";
460       else
461         OS << formatv(" hash: {0:x}", BBHashMap.getBBHash(Val));
462       OS << "\n";
463     }
464     if (IsHotFunction) {
465       auto NumBasicBlocksIt = NumBasicBlocksMap.find(Address);
466       assert(NumBasicBlocksIt != NumBasicBlocksMap.end());
467       OS << "NumBlocks: " << NumBasicBlocksIt->second << '\n';
468     }
469     auto SecondaryEntryPointsIt = SecondaryEntryPointsMap.find(Address);
470     if (SecondaryEntryPointsIt != SecondaryEntryPointsMap.end()) {
471       const std::vector<uint32_t> &SecondaryEntryPoints =
472           SecondaryEntryPointsIt->second;
473       OS << SecondaryEntryPoints.size() << " secondary entry points:\n";
474       for (uint32_t EntryPointOffset : SecondaryEntryPoints)
475         OS << formatv("{0:x}\n", EntryPointOffset);
476     }
477     OS << "\n";
478   }
479   const size_t NumColdParts = ColdPartSource.size();
480   if (!NumColdParts)
481     return;
482 
483   OS << NumColdParts << " cold mappings:\n";
484   for (const auto &Entry : ColdPartSource) {
485     OS << "0x" << Twine::utohexstr(Entry.first) << " -> "
486        << Twine::utohexstr(Entry.second) << "\n";
487   }
488   OS << "\n";
489 }
490 
491 uint64_t BoltAddressTranslation::translate(uint64_t FuncAddress,
492                                            uint64_t Offset,
493                                            bool IsBranchSrc) const {
494   auto Iter = Maps.find(FuncAddress);
495   if (Iter == Maps.end())
496     return Offset;
497 
498   const MapTy &Map = Iter->second;
499   auto KeyVal = Map.upper_bound(Offset);
500   if (KeyVal == Map.begin())
501     return Offset;
502 
503   --KeyVal;
504 
505   const uint32_t Val = KeyVal->second >> 1; // dropping BRANCHENTRY bit
506   // Branch source addresses are translated to the first instruction of the
507   // source BB to avoid accounting for modifications BOLT may have made in the
508   // BB regarding deletion/addition of instructions.
509   if (IsBranchSrc)
510     return Val;
511   return Offset - KeyVal->first + Val;
512 }
513 
514 std::optional<BoltAddressTranslation::FallthroughListTy>
515 BoltAddressTranslation::getFallthroughsInTrace(uint64_t FuncAddress,
516                                                uint64_t From,
517                                                uint64_t To) const {
518   SmallVector<std::pair<uint64_t, uint64_t>, 16> Res;
519 
520   // Filter out trivial case
521   if (From >= To)
522     return Res;
523 
524   From -= FuncAddress;
525   To -= FuncAddress;
526 
527   auto Iter = Maps.find(FuncAddress);
528   if (Iter == Maps.end())
529     return std::nullopt;
530 
531   const MapTy &Map = Iter->second;
532   auto FromIter = Map.upper_bound(From);
533   if (FromIter == Map.begin())
534     return Res;
535   // Skip instruction entries, to create fallthroughs we are only interested in
536   // BB boundaries
537   do {
538     if (FromIter == Map.begin())
539       return Res;
540     --FromIter;
541   } while (FromIter->second & BRANCHENTRY);
542 
543   auto ToIter = Map.upper_bound(To);
544   if (ToIter == Map.begin())
545     return Res;
546   --ToIter;
547   if (FromIter->first >= ToIter->first)
548     return Res;
549 
550   for (auto Iter = FromIter; Iter != ToIter;) {
551     const uint32_t Src = Iter->first;
552     if (Iter->second & BRANCHENTRY) {
553       ++Iter;
554       continue;
555     }
556 
557     ++Iter;
558     while (Iter->second & BRANCHENTRY && Iter != ToIter)
559       ++Iter;
560     if (Iter->second & BRANCHENTRY)
561       break;
562     Res.emplace_back(Src, Iter->first);
563   }
564 
565   return Res;
566 }
567 
568 bool BoltAddressTranslation::enabledFor(
569     llvm::object::ELFObjectFileBase *InputFile) const {
570   for (const SectionRef &Section : InputFile->sections()) {
571     Expected<StringRef> SectionNameOrErr = Section.getName();
572     if (Error E = SectionNameOrErr.takeError())
573       continue;
574 
575     if (SectionNameOrErr.get() == SECTION_NAME)
576       return true;
577   }
578   return false;
579 }
580 
581 void BoltAddressTranslation::saveMetadata(BinaryContext &BC) {
582   for (BinaryFunction &BF : llvm::make_second_range(BC.getBinaryFunctions())) {
583     // We don't need a translation table if the body of the function hasn't
584     // changed
585     if (BF.isIgnored() || (!BC.HasRelocations && !BF.isSimple()))
586       continue;
587     // Prepare function and block hashes
588     FuncHashes.addEntry(BF.getAddress(), BF.computeHash());
589     BF.computeBlockHashes();
590     BBHashMapTy &BBHashMap = getBBHashMap(BF.getAddress());
591     // Set BF/BB metadata
592     for (const BinaryBasicBlock &BB : BF)
593       BBHashMap.addEntry(BB.getInputOffset(), BB.getIndex(), BB.getHash());
594     NumBasicBlocksMap.emplace(BF.getAddress(), BF.size());
595   }
596 }
597 
598 unsigned
599 BoltAddressTranslation::getSecondaryEntryPointId(uint64_t Address,
600                                                  uint32_t Offset) const {
601   auto FunctionIt = SecondaryEntryPointsMap.find(Address);
602   if (FunctionIt == SecondaryEntryPointsMap.end())
603     return 0;
604   const std::vector<uint32_t> &Offsets = FunctionIt->second;
605   auto OffsetIt = std::find(Offsets.begin(), Offsets.end(), Offset);
606   if (OffsetIt == Offsets.end())
607     return 0;
608   // Adding one here because main entry point is not stored in BAT, and
609   // enumeration for secondary entry points starts with 1.
610   return OffsetIt - Offsets.begin() + 1;
611 }
612 
613 std::pair<const BinaryFunction *, unsigned>
614 BoltAddressTranslation::translateSymbol(const BinaryContext &BC,
615                                         const MCSymbol &Symbol,
616                                         uint32_t Offset) const {
617   // The symbol could be a secondary entry in a cold fragment.
618   uint64_t SymbolValue = cantFail(errorOrToExpected(BC.getSymbolValue(Symbol)));
619 
620   const BinaryFunction *Callee = BC.getFunctionForSymbol(&Symbol);
621   assert(Callee);
622 
623   // Containing function, not necessarily the same as symbol value.
624   const uint64_t CalleeAddress = Callee->getAddress();
625   const uint32_t OutputOffset = SymbolValue - CalleeAddress;
626 
627   const uint64_t ParentAddress = fetchParentAddress(CalleeAddress);
628   const uint64_t HotAddress = ParentAddress ? ParentAddress : CalleeAddress;
629 
630   const BinaryFunction *ParentBF = BC.getBinaryFunctionAtAddress(HotAddress);
631 
632   const uint32_t InputOffset =
633       translate(CalleeAddress, OutputOffset, /*IsBranchSrc*/ false) + Offset;
634 
635   unsigned SecondaryEntryId{0};
636   if (InputOffset)
637     SecondaryEntryId = getSecondaryEntryPointId(HotAddress, InputOffset);
638 
639   return std::pair(ParentBF, SecondaryEntryId);
640 }
641 
642 } // namespace bolt
643 } // namespace llvm
644