@@ -1391,7 +1391,8 @@ static uint32_t getBBAddrMapMetadata(const MachineBasicBlock &MBB) {
13911391}
13921392
13931393static llvm::object::BBAddrMap::Features
1394- getBBAddrMapFeature (const MachineFunction &MF, int NumMBBSectionRanges) {
1394+ getBBAddrMapFeature (const MachineFunction &MF, int NumMBBSectionRanges,
1395+ bool HasCalls) {
13951396 // Ensure that the user has not passed in additional options while also
13961397 // specifying all or none.
13971398 if ((PgoAnalysisMapFeatures.isSet (PGOMapFeaturesEnum::None) ||
@@ -1424,13 +1425,14 @@ getBBAddrMapFeature(const MachineFunction &MF, int NumMBBSectionRanges) {
14241425 BrProbEnabled,
14251426 MF.hasBBSections () && NumMBBSectionRanges > 1 ,
14261427 static_cast <bool >(BBAddrMapSkipEmitBBEntries),
1427- false };
1428+ HasCalls };
14281429}
14291430
14301431void AsmPrinter::emitBBAddrMapSection (const MachineFunction &MF) {
14311432 MCSection *BBAddrMapSection =
14321433 getObjFileLowering ().getBBAddrMapSection (*MF.getSection ());
14331434 assert (BBAddrMapSection && " .llvm_bb_addr_map section is not initialized." );
1435+ bool HasCalls = !CurrentFnCallsiteSymbols.empty ();
14341436
14351437 const MCSymbol *FunctionSymbol = getFunctionBegin ();
14361438
@@ -1440,7 +1442,7 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
14401442 uint8_t BBAddrMapVersion = OutStreamer->getContext ().getBBAddrMapVersion ();
14411443 OutStreamer->emitInt8 (BBAddrMapVersion);
14421444 OutStreamer->AddComment (" feature" );
1443- auto Features = getBBAddrMapFeature (MF, MBBSectionRanges.size ());
1445+ auto Features = getBBAddrMapFeature (MF, MBBSectionRanges.size (), HasCalls );
14441446 OutStreamer->emitInt8 (Features.encode ());
14451447 // Emit BB Information for each basic block in the function.
14461448 if (Features.MultiBBRange ) {
@@ -1493,13 +1495,23 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
14931495 // Emit the basic block offset relative to the end of the previous block.
14941496 // This is zero unless the block is padded due to alignment.
14951497 emitLabelDifferenceAsULEB128 (MBBSymbol, PrevMBBEndSymbol);
1496- // Emit the basic block size. When BBs have alignments, their size cannot
1497- // always be computed from their offsets.
1498- emitLabelDifferenceAsULEB128 (MBB.getEndSymbol (), MBBSymbol);
1498+ const MCSymbol *CurrentLabel = MBBSymbol;
1499+ if (HasCalls) {
1500+ auto CallsiteSymbols = CurrentFnCallsiteSymbols.lookup (&MBB);
1501+ OutStreamer->AddComment (" number of callsites" );
1502+ OutStreamer->emitULEB128IntValue (CallsiteSymbols.size ());
1503+ for (const MCSymbol *CallsiteSymbol : CallsiteSymbols) {
1504+ // Emit the callsite offset.
1505+ emitLabelDifferenceAsULEB128 (CallsiteSymbol, CurrentLabel);
1506+ CurrentLabel = CallsiteSymbol;
1507+ }
1508+ }
1509+ // Emit the offset to the end of the block, which can be used to compute
1510+ // the total block size.
1511+ emitLabelDifferenceAsULEB128 (MBB.getEndSymbol (), CurrentLabel);
14991512 // Emit the Metadata.
15001513 OutStreamer->emitULEB128IntValue (getBBAddrMapMetadata (MBB));
15011514 }
1502-
15031515 PrevMBBEndSymbol = MBB.getEndSymbol ();
15041516 }
15051517
@@ -1828,6 +1840,8 @@ void AsmPrinter::emitFunctionBody() {
18281840 !MI.isDebugInstr ()) {
18291841 HasAnyRealCode = true ;
18301842 }
1843+ if (MI.isCall () && MF->getTarget ().Options .BBAddrMap )
1844+ OutStreamer->emitLabel (createCallsiteSymbol (MBB));
18311845
18321846 // If there is a pre-instruction symbol, emit a label for it here.
18331847 if (MCSymbol *S = MI.getPreInstrSymbol ())
@@ -2775,6 +2789,14 @@ MCSymbol *AsmPrinter::getMBBExceptionSym(const MachineBasicBlock &MBB) {
27752789 return Res.first ->second ;
27762790}
27772791
2792+ MCSymbol *AsmPrinter::createCallsiteSymbol (const MachineBasicBlock &MBB) {
2793+ MCContext &Ctx = MF->getContext ();
2794+ MCSymbol *Sym = Ctx.createTempSymbol (" BB" + Twine (MF->getFunctionNumber ()) +
2795+ " _" + Twine (MBB.getNumber ()) + " _CS" );
2796+ CurrentFnCallsiteSymbols[&MBB].push_back (Sym);
2797+ return Sym;
2798+ }
2799+
27782800void AsmPrinter::SetupMachineFunction (MachineFunction &MF) {
27792801 this ->MF = &MF;
27802802 const Function &F = MF.getFunction ();
@@ -2809,6 +2831,7 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
28092831 CurrentFnBegin = nullptr ;
28102832 CurrentFnBeginLocal = nullptr ;
28112833 CurrentSectionBeginSym = nullptr ;
2834+ CurrentFnCallsiteSymbols.clear ();
28122835 MBBSectionRanges.clear ();
28132836 MBBSectionExceptionSyms.clear ();
28142837 bool NeedsLocalForSize = MAI->needsLocalForSize ();
0 commit comments