diff --git a/llvm/lib/Transforms/Scalar/LowerGuardIntrinsic.cpp b/llvm/lib/Transforms/Scalar/LowerGuardIntrinsic.cpp --- a/llvm/lib/Transforms/Scalar/LowerGuardIntrinsic.cpp +++ b/llvm/lib/Transforms/Scalar/LowerGuardIntrinsic.cpp @@ -48,9 +48,13 @@ return false; SmallVector ToLower; - for (auto &I : instructions(F)) - if (isGuard(&I)) - ToLower.push_back(cast(&I)); + // Traverse through the users of GuardDecl. + // This is presumably cheaper than traversing all instructions in the + // function. + for (auto *U : GuardDecl->users()) + if (auto *CI = dyn_cast(U)) + if (CI->getFunction() == &F) + ToLower.push_back(CI); if (ToLower.empty()) return false; diff --git a/llvm/lib/Transforms/Scalar/LowerWidenableCondition.cpp b/llvm/lib/Transforms/Scalar/LowerWidenableCondition.cpp --- a/llvm/lib/Transforms/Scalar/LowerWidenableCondition.cpp +++ b/llvm/lib/Transforms/Scalar/LowerWidenableCondition.cpp @@ -47,9 +47,13 @@ using namespace llvm::PatternMatch; SmallVector ToLower; - for (auto &I : instructions(F)) - if (match(&I, m_Intrinsic())) - ToLower.push_back(cast(&I)); + // Traverse through the users of WCDecl. + // This is presumably cheaper than traversing all instructions in the + // function. + for (auto *U : WCDecl->users()) + if (auto *CI = dyn_cast(U)) + if (CI->getFunction() == &F) + ToLower.push_back(CI); if (ToLower.empty()) return false;