@@ -349,6 +349,7 @@ struct ICallBranchFunnel final
349349
350350struct ScopedSaveAliaseesAndUsed {
351351 Module &M;
352+ std::set<GlobalAlias *> *ExcludedAliases;
352353 SmallVector<GlobalValue *, 4 > Used, CompilerUsed;
353354 std::vector<std::pair<GlobalAlias *, Function *>> FunctionAliases;
354355 std::vector<std::pair<GlobalIFunc *, Function *>> ResolverIFuncs;
@@ -377,7 +378,9 @@ struct ScopedSaveAliaseesAndUsed {
377378 Vec.resize (NonFuncBegin - Vec.begin ());
378379 }
379380
380- ScopedSaveAliaseesAndUsed (Module &M) : M(M) {
381+ ScopedSaveAliaseesAndUsed (Module &M,
382+ std::set<GlobalAlias *> *ExcludedAliases = nullptr )
383+ : M(M), ExcludedAliases(ExcludedAliases) {
381384 // The users of this class want to replace all function references except
382385 // for aliases and llvm.used/llvm.compiler.used with references to a jump
383386 // table. We avoid replacing aliases in order to avoid introducing a double
@@ -396,8 +399,9 @@ struct ScopedSaveAliaseesAndUsed {
396399 for (auto &GA : M.aliases ()) {
397400 // FIXME: This should look past all aliases not just interposable ones,
398401 // see discussion on D65118.
399- if (auto *F = dyn_cast<Function>(GA.getAliasee ()->stripPointerCasts ()))
400- FunctionAliases.push_back ({&GA, F});
402+ if (!ExcludedAliases || !ExcludedAliases->count (&GA))
403+ if (auto *F = dyn_cast<Function>(GA.getAliasee ()->stripPointerCasts ()))
404+ FunctionAliases.push_back ({&GA, F});
401405 }
402406
403407 for (auto &GI : M.ifuncs ())
@@ -2137,6 +2141,18 @@ bool LowerTypeTestsModule::lower() {
21372141 if (auto Alias = dyn_cast<AliasSummary>(RefGVS.get ()))
21382142 AddressTaken.insert (Alias->getAliaseeGUID ());
21392143 }
2144+ auto IsAddressTaken = [&](GlobalValue::GUID GUID) {
2145+ if (AddressTaken.count (GUID))
2146+ return true ;
2147+ auto VI = ExportSummary->getValueInfo (GUID);
2148+ if (!VI)
2149+ return false ;
2150+ for (auto &I : VI.getSummaryList ())
2151+ if (auto Alias = dyn_cast<AliasSummary>(I.get ()))
2152+ if (AddressTaken.count (Alias->getAliaseeGUID ()))
2153+ return true ;
2154+ return false ;
2155+ };
21402156 for (auto *FuncMD : CfiFunctionsMD->operands ()) {
21412157 assert (FuncMD->getNumOperands () >= 2 );
21422158 StringRef FunctionName =
@@ -2153,7 +2169,7 @@ bool LowerTypeTestsModule::lower() {
21532169 // have no live references (and are not exported with cross-DSO CFI.)
21542170 if (!ExportSummary->isGUIDLive (GUID))
21552171 continue ;
2156- if (!AddressTaken. count (GUID)) {
2172+ if (!IsAddressTaken (GUID)) {
21572173 if (!CrossDsoCfi || Linkage != CFL_Definition)
21582174 continue ;
21592175
@@ -2227,6 +2243,44 @@ bool LowerTypeTestsModule::lower() {
22272243 }
22282244 }
22292245
2246+ struct AliasToCreate {
2247+ Function *Alias;
2248+ std::string TargetName;
2249+ };
2250+ std::vector<AliasToCreate> AliasesToCreate;
2251+
2252+ // Parse alias data to replace stand-in function declarations for aliases
2253+ // with an alias to the intended target.
2254+ std::set<GlobalAlias *> ExcludedAliases;
2255+ if (ExportSummary) {
2256+ if (NamedMDNode *AliasesMD = M.getNamedMetadata (" aliases" )) {
2257+ for (auto *AliasMD : AliasesMD->operands ()) {
2258+ std::vector<Function *> Aliases;
2259+ for (Metadata *MD : AliasMD->operands ()) {
2260+ auto *MDS = dyn_cast<MDString>(MD);
2261+ if (!MDS)
2262+ continue ;
2263+ StringRef AliasName = MDS->getString ();
2264+ if (!ExportedFunctions.count (AliasName))
2265+ continue ;
2266+ auto *AliasF = M.getFunction (AliasName);
2267+ if (AliasF)
2268+ Aliases.push_back (AliasF);
2269+ }
2270+
2271+ if (Aliases.empty ())
2272+ continue ;
2273+
2274+ for (unsigned I = 1 ; I != Aliases.size (); ++I) {
2275+ auto *AliasF = Aliases[I];
2276+ ExportedFunctions.erase (AliasF->getName ());
2277+ AliasesToCreate.push_back (
2278+ {AliasF, std::string (Aliases[0 ]->getName ())});
2279+ }
2280+ }
2281+ }
2282+ }
2283+
22302284 DenseMap<GlobalObject *, GlobalTypeMember *> GlobalTypeMembers;
22312285 for (GlobalObject &GO : M.global_objects ()) {
22322286 if (isa<GlobalVariable>(GO) && GO.isDeclarationForLinker ())
@@ -2374,7 +2428,7 @@ bool LowerTypeTestsModule::lower() {
23742428 return false ;
23752429
23762430 {
2377- ScopedSaveAliaseesAndUsed S (M);
2431+ ScopedSaveAliaseesAndUsed S (M, &ExcludedAliases );
23782432 // For each disjoint set we found...
23792433 for (const auto &C : GlobalClasses) {
23802434 if (!C->isLeader ())
@@ -2414,49 +2468,18 @@ bool LowerTypeTestsModule::lower() {
24142468
24152469 allocateByteArrays ();
24162470
2417- // Parse alias data to replace stand-in function declarations for aliases
2418- // with an alias to the intended target.
2419- if (ExportSummary) {
2420- if (NamedMDNode *AliasesMD = M.getNamedMetadata (" aliases" )) {
2421- for (auto *AliasMD : AliasesMD->operands ()) {
2422- assert (AliasMD->getNumOperands () >= 4 );
2423- StringRef AliasName =
2424- cast<MDString>(AliasMD->getOperand (0 ))->getString ();
2425- StringRef Aliasee = cast<MDString>(AliasMD->getOperand (1 ))->getString ();
2426-
2427- if (auto It = ExportedFunctions.find (Aliasee);
2428- It == ExportedFunctions.end () ||
2429- It->second .Linkage != CFL_Definition || !M.getNamedAlias (Aliasee))
2430- continue ;
2431-
2432- GlobalValue::VisibilityTypes Visibility =
2433- static_cast <GlobalValue::VisibilityTypes>(
2434- cast<ConstantAsMetadata>(AliasMD->getOperand (2 ))
2435- ->getValue ()
2436- ->getUniqueInteger ()
2437- .getZExtValue ());
2438- bool Weak =
2439- static_cast <bool >(cast<ConstantAsMetadata>(AliasMD->getOperand (3 ))
2440- ->getValue ()
2441- ->getUniqueInteger ()
2442- .getZExtValue ());
2443-
2444- auto *Alias = GlobalAlias::create (" " , M.getNamedAlias (Aliasee));
2445- Alias->setVisibility (Visibility);
2446- if (Weak)
2447- Alias->setLinkage (GlobalValue::WeakAnyLinkage);
2448-
2449- if (auto *F = M.getFunction (AliasName)) {
2450- Alias->takeName (F);
2451- F->replaceAllUsesWith (Alias);
2452- F->eraseFromParent ();
2453- } else {
2454- Alias->setName (AliasName);
2455- }
2456- }
2457- }
2458- }
2459-
2471+ for (auto A : AliasesToCreate) {
2472+ auto *Target = M.getNamedValue (A.TargetName );
2473+ if (!isa<GlobalAlias>(Target))
2474+ continue ;
2475+ auto *AliasGA = GlobalAlias::create (" " , Target);
2476+ AliasGA->setVisibility (A.Alias ->getVisibility ());
2477+ AliasGA->setLinkage (A.Alias ->getLinkage ());
2478+ AliasGA->takeName (A.Alias );
2479+ A.Alias ->replaceAllUsesWith (AliasGA);
2480+ A.Alias ->eraseFromParent ();
2481+ }
2482+
24602483 // Emit .symver directives for exported functions, if they exist.
24612484 if (ExportSummary) {
24622485 if (NamedMDNode *SymversMD = M.getNamedMetadata (" symvers" )) {
0 commit comments