@@ -2069,8 +2069,7 @@ static void inferAttrsFromFunctionBodies(const SCCNodeSet &SCCNodes,
2069
2069
2070
2070
static void addNoRecurseAttrs (const SCCNodeSet &SCCNodes,
2071
2071
SmallPtrSet<Function *, 8 > &Changed,
2072
- bool AnyFunctionsAddressIsTaken,
2073
- bool IsLTOPostLink) {
2072
+ bool NoFunctionsAddressIsTaken) {
2074
2073
// Try and identify functions that do not recurse.
2075
2074
2076
2075
// If the SCC contains multiple nodes we know for sure there is recursion.
@@ -2081,9 +2080,6 @@ static void addNoRecurseAttrs(const SCCNodeSet &SCCNodes,
2081
2080
if (!F || !F->hasExactDefinition () || F->doesNotRecurse ())
2082
2081
return ;
2083
2082
2084
- if (F->hasAddressTaken ())
2085
- return ;
2086
-
2087
2083
Module *M = F->getParent ();
2088
2084
llvm::TargetLibraryInfoImpl TLII (llvm::Triple (M->getTargetTriple ()));
2089
2085
llvm::TargetLibraryInfo TLI (TLII);
@@ -2094,6 +2090,9 @@ static void addNoRecurseAttrs(const SCCNodeSet &SCCNodes,
2094
2090
for (auto &BB : *F) {
2095
2091
for (auto &I : BB.instructionsWithoutDebug ()) {
2096
2092
if (auto *CB = dyn_cast<CallBase>(&I)) {
2093
+ if (F->hasAddressTaken ())
2094
+ return ;
2095
+
2097
2096
Function *Callee = CB->getCalledFunction ();
2098
2097
2099
2098
if (!Callee || Callee == F)
@@ -2104,19 +2103,11 @@ static void addNoRecurseAttrs(const SCCNodeSet &SCCNodes,
2104
2103
2105
2104
LibFunc LF;
2106
2105
if (Callee->isDeclaration ()) {
2107
- // External call with NoCallback attribute.
2108
- if (Callee->hasFnAttribute (Attribute::NoCallback))
2106
+ if (Callee->hasFnAttribute (Attribute::NoCallback) ||
2107
+ (NoFunctionsAddressIsTaken &&
2108
+ TLI.getLibFunc (Callee->getName (), LF)))
2109
2109
continue ;
2110
- // We rely on this only in post link stage when all functions in the
2111
- // program are known.
2112
- if (IsLTOPostLink && !AnyFunctionsAddressIsTaken &&
2113
- TLI.getLibFunc (Callee->getName (), LF))
2114
- continue ;
2115
- // Do not consider external functions safe unless we are in LTO
2116
- // post-link stage and have information about all functions in the
2117
- // program.
2118
- if (!IsLTOPostLink)
2119
- return ;
2110
+ return ;
2120
2111
}
2121
2112
}
2122
2113
}
@@ -2268,8 +2259,8 @@ static SCCNodesResult createSCCNodeSet(ArrayRef<Function *> Functions) {
2268
2259
template <typename AARGetterT>
2269
2260
static SmallPtrSet<Function *, 8 >
2270
2261
deriveAttrsInPostOrder (ArrayRef<Function *> Functions, AARGetterT &&AARGetter,
2271
- bool ArgAttrsOnly, bool AnyFunctionAddressTaken = false ,
2272
- bool IsLTOPostLink = false ) {
2262
+ bool ArgAttrsOnly,
2263
+ bool NoFunctionAddressIsTaken = false ) {
2273
2264
SCCNodesResult Nodes = createSCCNodeSet (Functions);
2274
2265
2275
2266
// Bail if the SCC only contains optnone functions.
@@ -2299,8 +2290,7 @@ deriveAttrsInPostOrder(ArrayRef<Function *> Functions, AARGetterT &&AARGetter,
2299
2290
addNoAliasAttrs (Nodes.SCCNodes , Changed);
2300
2291
addNonNullAttrs (Nodes.SCCNodes , Changed);
2301
2292
inferAttrsFromFunctionBodies (Nodes.SCCNodes , Changed);
2302
- addNoRecurseAttrs (Nodes.SCCNodes , Changed, AnyFunctionAddressTaken,
2303
- IsLTOPostLink);
2293
+ addNoRecurseAttrs (Nodes.SCCNodes , Changed, NoFunctionAddressIsTaken);
2304
2294
2305
2295
// Finally, infer the maximal set of attributes from the ones we've inferred
2306
2296
// above. This is handling the cases where one attribute on a signature
@@ -2342,12 +2332,13 @@ PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C,
2342
2332
Functions.push_back (&N.getFunction ());
2343
2333
}
2344
2334
2345
- bool AnyFunctionsAddressIsTaken = false ;
2335
+ bool NoFunctionsAddressIsTaken = false ;
2346
2336
// Check if any function in the whole program has its address taken.
2347
2337
// We use this information when inferring norecurse attribute: If there is
2348
2338
// no function whose address is taken, we conclude that any external function
2349
2339
// cannot callback into any user function.
2350
2340
if (IsLTOPostLink) {
2341
+ bool AnyFunctionsAddressIsTaken = false ;
2351
2342
// Get the parent Module of the Function
2352
2343
Module &M = *C.begin ()->getFunction ().getParent ();
2353
2344
for (Function &F : M) {
@@ -2356,15 +2347,16 @@ PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C,
2356
2347
if (F.isDeclaration ())
2357
2348
continue ;
2358
2349
2359
- if (F.hasAddressTaken ()) {
2350
+ if (!F. hasLocalLinkage () || F.hasAddressTaken ()) {
2360
2351
AnyFunctionsAddressIsTaken = true ;
2361
2352
break ; // break if we found one
2362
2353
}
2363
2354
}
2355
+ NoFunctionsAddressIsTaken = !AnyFunctionsAddressIsTaken;
2364
2356
}
2365
- auto ChangedFunctions =
2366
- deriveAttrsInPostOrder ( Functions, AARGetter, ArgAttrsOnly,
2367
- AnyFunctionsAddressIsTaken, IsLTOPostLink);
2357
+ auto ChangedFunctions = deriveAttrsInPostOrder (
2358
+ Functions, AARGetter, ArgAttrsOnly, NoFunctionsAddressIsTaken);
2359
+
2368
2360
if (ChangedFunctions.empty ())
2369
2361
return PreservedAnalyses::all ();
2370
2362
0 commit comments