@@ -107,7 +107,6 @@ static void GetObjCImageInfo(Module &M, unsigned &Version, unsigned &Flags,
107107void TargetLoweringObjectFileELF::Initialize (MCContext &Ctx,
108108 const TargetMachine &TgtM) {
109109 TargetLoweringObjectFile::Initialize (Ctx, TgtM);
110- TM = &TgtM;
111110
112111 CodeModel::Model CM = TgtM.getCodeModel ();
113112 InitializeELF (TgtM.Options .UseInitArray );
@@ -324,46 +323,7 @@ void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer &Streamer,
324323 Streamer.AddBlankLine ();
325324 }
326325
327- SmallVector<Module::ModuleFlagEntry, 8 > ModuleFlags;
328- M.getModuleFlagsMetadata (ModuleFlags);
329-
330- MDNode *CFGProfile = nullptr ;
331-
332- for (const auto &MFE : ModuleFlags) {
333- StringRef Key = MFE.Key ->getString ();
334- if (Key == " CG Profile" ) {
335- CFGProfile = cast<MDNode>(MFE.Val );
336- break ;
337- }
338- }
339-
340- if (!CFGProfile)
341- return ;
342-
343- auto GetSym = [this ](const MDOperand &MDO) -> MCSymbol * {
344- if (!MDO)
345- return nullptr ;
346- auto V = cast<ValueAsMetadata>(MDO);
347- const Function *F = cast<Function>(V->getValue ());
348- return TM->getSymbol (F);
349- };
350-
351- for (const auto &Edge : CFGProfile->operands ()) {
352- MDNode *E = cast<MDNode>(Edge);
353- const MCSymbol *From = GetSym (E->getOperand (0 ));
354- const MCSymbol *To = GetSym (E->getOperand (1 ));
355- // Skip null functions. This can happen if functions are dead stripped after
356- // the CGProfile pass has been run.
357- if (!From || !To)
358- continue ;
359- uint64_t Count = cast<ConstantAsMetadata>(E->getOperand (2 ))
360- ->getValue ()
361- ->getUniqueInteger ()
362- .getZExtValue ();
363- Streamer.emitCGProfileEntry (
364- MCSymbolRefExpr::create (From, MCSymbolRefExpr::VK_None, C),
365- MCSymbolRefExpr::create (To, MCSymbolRefExpr::VK_None, C), Count);
366- }
326+ emitCGProfile (Streamer, M);
367327}
368328
369329MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol (
@@ -1598,18 +1558,20 @@ void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer &Streamer,
15981558 StringRef Section;
15991559
16001560 GetObjCImageInfo (M, Version, Flags, Section);
1601- if (Section.empty ())
1602- return ;
1561+ if (!Section.empty ()) {
1562+ auto &C = getContext ();
1563+ auto *S = C.getCOFFSection (Section,
1564+ COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1565+ COFF::IMAGE_SCN_MEM_READ,
1566+ SectionKind::getReadOnly ());
1567+ Streamer.SwitchSection (S);
1568+ Streamer.emitLabel (C.getOrCreateSymbol (StringRef (" OBJC_IMAGE_INFO" )));
1569+ Streamer.emitInt32 (Version);
1570+ Streamer.emitInt32 (Flags);
1571+ Streamer.AddBlankLine ();
1572+ }
16031573
1604- auto &C = getContext ();
1605- auto *S = C.getCOFFSection (
1606- Section, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
1607- SectionKind::getReadOnly ());
1608- Streamer.SwitchSection (S);
1609- Streamer.emitLabel (C.getOrCreateSymbol (StringRef (" OBJC_IMAGE_INFO" )));
1610- Streamer.emitInt32 (Version);
1611- Streamer.emitInt32 (Flags);
1612- Streamer.AddBlankLine ();
1574+ emitCGProfile (Streamer, M);
16131575}
16141576
16151577void TargetLoweringObjectFileCOFF::emitLinkerDirectives (
0 commit comments