diff --git a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp --- a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp +++ b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp @@ -2014,12 +2014,13 @@ // this function could be recursively (indirectly) called. Note that this // also detects if F is directly recursive as F is not yet marked as // a norecurse function. - for (auto *U : F.users()) { - auto *I = dyn_cast(U); + for (auto &U : F.uses()) { + auto *I = dyn_cast(U.getUser()); if (!I) return false; CallBase *CB = dyn_cast(I); - if (!CB || !CB->getParent()->getParent()->doesNotRecurse()) + if (!CB || !CB->isCallee(&U) || + !CB->getParent()->getParent()->doesNotRecurse()) return false; } F.setDoesNotRecurse(); diff --git a/llvm/test/Transforms/FunctionAttrs/norecurse.ll b/llvm/test/Transforms/FunctionAttrs/norecurse.ll --- a/llvm/test/Transforms/FunctionAttrs/norecurse.ll +++ b/llvm/test/Transforms/FunctionAttrs/norecurse.ll @@ -99,7 +99,9 @@ } ; CHECK: Function Attrs -; CHECK-SAME: norecurse nosync readnone +; CHECK-NOT: norecurse +; CHECK-SAME: nosync readnone +; CHECK-NOT: norecurse ; CHECK-NEXT: define internal i32 @escapes_as_parameter define internal i32 @escapes_as_parameter(ptr %p) { %a = call i32 @k()