@@ -502,8 +502,7 @@ class LowerTypeTestsModule {
502502 uint8_t *exportTypeId (StringRef TypeId, const TypeIdLowering &TIL);
503503 TypeIdLowering importTypeId (StringRef TypeId);
504504 void importTypeTest (CallInst *CI);
505- void importFunction (Function *F, bool isJumpTableCanonical,
506- std::vector<GlobalAlias *> &AliasesToErase);
505+ void importFunction (Function *F, bool isJumpTableCanonical);
507506
508507 BitSetInfo
509508 buildBitSet (Metadata *TypeId,
@@ -1103,9 +1102,8 @@ void LowerTypeTestsModule::maybeReplaceComdat(Function *F,
11031102
11041103// ThinLTO backend: the function F has a jump table entry; update this module
11051104// accordingly. isJumpTableCanonical describes the type of the jump table entry.
1106- void LowerTypeTestsModule::importFunction (
1107- Function *F, bool isJumpTableCanonical,
1108- std::vector<GlobalAlias *> &AliasesToErase) {
1105+ void LowerTypeTestsModule::importFunction (Function *F,
1106+ bool isJumpTableCanonical) {
11091107 assert (F->getType ()->getAddressSpace () == 0 );
11101108
11111109 GlobalValue::VisibilityTypes Visibility = F->getVisibility ();
@@ -1135,23 +1133,23 @@ void LowerTypeTestsModule::importFunction(
11351133 } else {
11361134 F->setName (Name + " .cfi" );
11371135 maybeReplaceComdat (F, Name);
1138- F->setLinkage (GlobalValue::ExternalLinkage);
11391136 FDecl = Function::Create (F->getFunctionType (), GlobalValue::ExternalLinkage,
11401137 F->getAddressSpace (), Name, &M);
11411138 FDecl->setVisibility (Visibility);
11421139 Visibility = GlobalValue::HiddenVisibility;
11431140
1144- // Delete aliases pointing to this function, they'll be re-created in the
1145- // merged output. Don't do it yet though because ScopedSaveAliaseesAndUsed
1146- // will want to reset the aliasees first .
1141+ // Update aliases pointing to this function to also include the ".cfi" suffix,
1142+ // We expect the jump table entry to either point to the real function or an
1143+ // alias. Redirect all other users to the jump table entry .
11471144 for (auto &U : F->uses ()) {
11481145 if (auto *A = dyn_cast<GlobalAlias>(U.getUser ())) {
1146+ std::string AliasName = A->getName ().str () + " .cfi" ;
11491147 Function *AliasDecl = Function::Create (
11501148 F->getFunctionType (), GlobalValue::ExternalLinkage,
11511149 F->getAddressSpace (), " " , &M);
11521150 AliasDecl->takeName (A);
11531151 A->replaceAllUsesWith (AliasDecl);
1154- AliasesToErase. push_back (A );
1152+ A-> setName (AliasName );
11551153 }
11561154 }
11571155 }
@@ -2077,16 +2075,13 @@ bool LowerTypeTestsModule::lower() {
20772075 Decls.push_back (&F);
20782076 }
20792077
2080- std::vector<GlobalAlias *> AliasesToErase;
20812078 {
20822079 ScopedSaveAliaseesAndUsed S (M);
20832080 for (auto *F : Defs)
2084- importFunction (F, /* isJumpTableCanonical*/ true , AliasesToErase );
2081+ importFunction (F, /* isJumpTableCanonical*/ true );
20852082 for (auto *F : Decls)
2086- importFunction (F, /* isJumpTableCanonical*/ false , AliasesToErase );
2083+ importFunction (F, /* isJumpTableCanonical*/ false );
20872084 }
2088- for (GlobalAlias *GA : AliasesToErase)
2089- GA->eraseFromParent ();
20902085
20912086 return true ;
20922087 }
@@ -2137,6 +2132,18 @@ bool LowerTypeTestsModule::lower() {
21372132 if (auto Alias = dyn_cast<AliasSummary>(RefGVS.get ()))
21382133 AddressTaken.insert (Alias->getAliaseeGUID ());
21392134 }
2135+ auto IsAddressTaken = [&](GlobalValue::GUID GUID) {
2136+ if (AddressTaken.count (GUID))
2137+ return true ;
2138+ auto VI = ExportSummary->getValueInfo (GUID);
2139+ if (!VI)
2140+ return false ;
2141+ for (auto &I : VI.getSummaryList ())
2142+ if (auto Alias = dyn_cast<AliasSummary>(I.get ()))
2143+ if (AddressTaken.count (Alias->getAliaseeGUID ()))
2144+ return true ;
2145+ return false ;
2146+ };
21402147 for (auto *FuncMD : CfiFunctionsMD->operands ()) {
21412148 assert (FuncMD->getNumOperands () >= 2 );
21422149 StringRef FunctionName =
@@ -2153,7 +2160,7 @@ bool LowerTypeTestsModule::lower() {
21532160 // have no live references (and are not exported with cross-DSO CFI.)
21542161 if (!ExportSummary->isGUIDLive (GUID))
21552162 continue ;
2156- if (!AddressTaken. count (GUID)) {
2163+ if (!IsAddressTaken (GUID)) {
21572164 if (!CrossDsoCfi || Linkage != CFL_Definition)
21582165 continue ;
21592166
@@ -2227,6 +2234,43 @@ bool LowerTypeTestsModule::lower() {
22272234 }
22282235 }
22292236
2237+ struct AliasToCreate {
2238+ Function *Alias;
2239+ std::string TargetName;
2240+ };
2241+ std::vector<AliasToCreate> AliasesToCreate;
2242+
2243+ // Parse alias data to replace stand-in function declarations for aliases
2244+ // with an alias to the intended target.
2245+ if (ExportSummary) {
2246+ if (NamedMDNode *AliasesMD = M.getNamedMetadata (" aliases" )) {
2247+ for (auto *AliasMD : AliasesMD->operands ()) {
2248+ SmallVector<Function *> Aliases;
2249+ for (Metadata *MD : AliasMD->operands ()) {
2250+ auto *MDS = dyn_cast<MDString>(MD);
2251+ if (!MDS)
2252+ continue ;
2253+ StringRef AliasName = MDS->getString ();
2254+ if (!ExportedFunctions.count (AliasName))
2255+ continue ;
2256+ auto *AliasF = M.getFunction (AliasName);
2257+ if (AliasF)
2258+ Aliases.push_back (AliasF);
2259+ }
2260+
2261+ if (Aliases.empty ())
2262+ continue ;
2263+
2264+ for (unsigned I = 1 ; I != Aliases.size (); ++I) {
2265+ auto *AliasF = Aliases[I];
2266+ ExportedFunctions.erase (AliasF->getName ());
2267+ AliasesToCreate.push_back (
2268+ {AliasF, std::string (Aliases[0 ]->getName ())});
2269+ }
2270+ }
2271+ }
2272+ }
2273+
22302274 DenseMap<GlobalObject *, GlobalTypeMember *> GlobalTypeMembers;
22312275 for (GlobalObject &GO : M.global_objects ()) {
22322276 if (isa<GlobalVariable>(GO) && GO.isDeclarationForLinker ())
@@ -2414,47 +2458,16 @@ bool LowerTypeTestsModule::lower() {
24142458
24152459 allocateByteArrays ();
24162460
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- }
2461+ for (auto A : AliasesToCreate) {
2462+ auto *Target = M.getNamedValue (A.TargetName );
2463+ if (!isa<GlobalAlias>(Target))
2464+ continue ;
2465+ auto *AliasGA = GlobalAlias::create (" " , Target);
2466+ AliasGA->setVisibility (A.Alias ->getVisibility ());
2467+ AliasGA->setLinkage (A.Alias ->getLinkage ());
2468+ AliasGA->takeName (A.Alias );
2469+ A.Alias ->replaceAllUsesWith (AliasGA);
2470+ A.Alias ->eraseFromParent ();
24582471 }
24592472
24602473 // Emit .symver directives for exported functions, if they exist.
0 commit comments