@@ -86,21 +86,16 @@ void BoltAddressTranslation::write(const BinaryContext &BC, raw_ostream &OS) {
8686 if (Function.isIgnored () || (!BC.HasRelocations && !Function.isSimple ()))
8787 continue ;
8888
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-
9889 LLVM_DEBUG (dbgs () << " Function name: " << Function.getPrintName () << " \n " );
9990 LLVM_DEBUG (dbgs () << " Address reference: 0x"
10091 << Twine::utohexstr (Function.getOutputAddress ()) << " \n " );
10192 LLVM_DEBUG (dbgs () << formatv (" Hash: {0:x}\n " , getBFHash (InputAddress)));
102- LLVM_DEBUG (dbgs () << " Secondary Entry Points: " << NumSecondaryEntryPoints
103- << ' \n ' );
93+ LLVM_DEBUG ({
94+ uint32_t NumSecondaryEntryPoints = 0 ;
95+ if (SecondaryEntryPointsMap.count (InputAddress))
96+ NumSecondaryEntryPoints = SecondaryEntryPointsMap[InputAddress].size ();
97+ dbgs () << " Secondary Entry Points: " << NumSecondaryEntryPoints << ' \n ' ;
98+ });
10499
105100 MapTy Map;
106101 for (const BinaryBasicBlock *const BB :
@@ -206,10 +201,9 @@ void BoltAddressTranslation::writeMaps(uint64_t &PrevAddress, raw_ostream &OS) {
206201 << Twine::utohexstr (Address) << " .\n " );
207202 encodeULEB128 (Address - PrevAddress, OS);
208203 PrevAddress = Address;
209- const uint32_t NumSecondaryEntryPoints =
210- SecondaryEntryPointsMap.count (Address)
211- ? SecondaryEntryPointsMap[Address].size ()
212- : 0 ;
204+ uint32_t NumSecondaryEntryPoints = 0 ;
205+ if (SecondaryEntryPointsMap.count (HotInputAddress))
206+ NumSecondaryEntryPoints = SecondaryEntryPointsMap[HotInputAddress].size ();
213207 uint32_t Skew = 0 ;
214208 if (Cold) {
215209 auto HotEntryIt = llvm::lower_bound (HotFuncs, ColdPartSource[Address]);
@@ -281,7 +275,7 @@ void BoltAddressTranslation::writeMaps(uint64_t &PrevAddress, raw_ostream &OS) {
281275 if (!Cold && NumSecondaryEntryPoints) {
282276 LLVM_DEBUG (dbgs () << " Secondary entry points: " );
283277 // Secondary entry point offsets, delta-encoded
284- for (uint32_t Offset : SecondaryEntryPointsMap[Address ]) {
278+ for (uint32_t Offset : SecondaryEntryPointsMap[HotInputAddress ]) {
285279 encodeULEB128 (Offset - PrevOffset, OS);
286280 LLVM_DEBUG (dbgs () << formatv (" {0:x} " , Offset));
287281 PrevOffset = Offset;
@@ -469,8 +463,12 @@ void BoltAddressTranslation::dump(raw_ostream &OS) const {
469463 const std::vector<uint32_t > &SecondaryEntryPoints =
470464 SecondaryEntryPointsIt->second ;
471465 OS << SecondaryEntryPoints.size () << " secondary entry points:\n " ;
472- for (uint32_t EntryPointOffset : SecondaryEntryPoints)
473- OS << formatv (" {0:x}\n " , EntryPointOffset);
466+ for (uint32_t EntryPointOffset : SecondaryEntryPoints) {
467+ OS << formatv (" {0:x}" , EntryPointOffset >> 1 );
468+ if (EntryPointOffset & LPENTRY)
469+ OS << " (lp)" ;
470+ OS << ' \n ' ;
471+ }
474472 }
475473 OS << " \n " ;
476474 }
@@ -582,14 +580,21 @@ void BoltAddressTranslation::saveMetadata(BinaryContext &BC) {
582580 // changed
583581 if (BF.isIgnored () || (!BC.HasRelocations && !BF.isSimple ()))
584582 continue ;
583+ const uint64_t FuncAddress = BF.getAddress ();
585584 // Prepare function and block hashes
586- FuncHashes.addEntry (BF. getAddress () , BF.computeHash ());
585+ FuncHashes.addEntry (FuncAddress , BF.computeHash ());
587586 BF.computeBlockHashes ();
588- BBHashMapTy &BBHashMap = getBBHashMap (BF.getAddress ());
587+ BBHashMapTy &BBHashMap = getBBHashMap (FuncAddress);
588+ std::vector<uint32_t > SecondaryEntryPoints;
589589 // Set BF/BB metadata
590- for (const BinaryBasicBlock &BB : BF)
590+ for (const BinaryBasicBlock &BB : BF) {
591591 BBHashMap.addEntry (BB.getInputOffset (), BB.getIndex (), BB.getHash ());
592- NumBasicBlocksMap.emplace (BF.getAddress (), BF.size ());
592+ bool IsLandingPad = BB.isLandingPad ();
593+ if (IsLandingPad || BF.getSecondaryEntryPointSymbol (BB))
594+ SecondaryEntryPoints.emplace_back (BB.getOffset () << 1 | IsLandingPad);
595+ }
596+ SecondaryEntryPointsMap.emplace (FuncAddress, SecondaryEntryPoints);
597+ NumBasicBlocksMap.emplace (FuncAddress, BF.size ());
593598 }
594599}
595600
@@ -599,13 +604,20 @@ BoltAddressTranslation::getSecondaryEntryPointId(uint64_t Address,
599604 auto FunctionIt = SecondaryEntryPointsMap.find (Address);
600605 if (FunctionIt == SecondaryEntryPointsMap.end ())
601606 return 0 ;
602- const std::vector<uint32_t > &Offsets = FunctionIt->second ;
603- auto OffsetIt = std::find (Offsets.begin (), Offsets.end (), Offset);
604- if (OffsetIt == Offsets.end ())
605- return 0 ;
606- // Adding one here because main entry point is not stored in BAT, and
607- // enumeration for secondary entry points starts with 1.
608- return OffsetIt - Offsets.begin () + 1 ;
607+ unsigned EntryPoints = 0 ;
608+ // Note we need to scan the vector to get the entry point id because it
609+ // contains both entry points and landing pads.
610+ for (uint32_t Off : FunctionIt->second ) {
611+ // Skip landing pads.
612+ if (Off & LPENTRY)
613+ continue ;
614+ // Adding one here because main entry point is not stored in BAT, and
615+ // enumeration for secondary entry points starts with 1.
616+ if (Off >> 1 == Offset)
617+ return EntryPoints + 1 ;
618+ ++EntryPoints;
619+ }
620+ return 0 ;
609621}
610622
611623std::pair<const BinaryFunction *, unsigned >
@@ -637,5 +649,16 @@ BoltAddressTranslation::translateSymbol(const BinaryContext &BC,
637649 return std::pair (ParentBF, SecondaryEntryId);
638650}
639651
652+ bool BoltAddressTranslation::isSecondaryEntry (uint64_t Address,
653+ uint32_t Offset) const {
654+ auto FunctionIt = SecondaryEntryPointsMap.find (Address);
655+ if (FunctionIt == SecondaryEntryPointsMap.end ())
656+ return false ;
657+ const std::vector<uint32_t > &Offsets = FunctionIt->second ;
658+ uint64_t InputOffset = translate (Address, Offset, /* IsBranchSrc*/ false );
659+ auto OffsetIt = llvm::lower_bound (Offsets, InputOffset << 1 );
660+ return OffsetIt != Offsets.end () && *OffsetIt >> 1 == InputOffset;
661+ }
662+
640663} // namespace bolt
641664} // namespace llvm
0 commit comments