diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp index 50605f50769d..fb7c423e54e2 100644 --- a/llvm/lib/IR/Verifier.cpp +++ b/llvm/lib/IR/Verifier.cpp @@ -602,17 +602,22 @@ void Verifier::visit(Instruction &I) { InstVisitor::visit(I); } -// Helper to recursively iterate over indirect users. By -// returning false, the callback can ask to stop recursing -// further. +// Helper to iterate over indirect users. By returning false, the callback can ask to stop traversing further. static void forEachUser(const Value *User, SmallPtrSet &Visited, llvm::function_ref Callback) { if (!Visited.insert(User).second) return; - for (const Value *TheNextUser : User->materialized_users()) - if (Callback(TheNextUser)) - forEachUser(TheNextUser, Visited, Callback); + + SmallVector WorkList; + append_range(WorkList, User->materialized_users()); + while (!WorkList.empty()) { + const Value *Cur = WorkList.pop_back_val(); + if (!Visited.insert(Cur).second) + continue; + if (Callback(Cur)) + append_range(WorkList, Cur->materialized_users()); + } } void Verifier::visitGlobalValue(const GlobalValue &GV) { diff --git a/llvm/unittests/IR/VerifierTest.cpp b/llvm/unittests/IR/VerifierTest.cpp index 6031c808f584..8747e171cfba 100644 --- a/llvm/unittests/IR/VerifierTest.cpp +++ b/llvm/unittests/IR/VerifierTest.cpp @@ -136,17 +136,17 @@ TEST(VerifierTest, CrossModuleRef) { raw_string_ostream ErrorOS(Error); EXPECT_TRUE(verifyModule(M2, &ErrorOS)); EXPECT_TRUE(StringRef(ErrorOS.str()) - .equals("Global is used by function in a different module\n" - "i32 ()* @foo2\n" - "; ModuleID = 'M2'\n" - "i32 ()* @foo3\n" - "; ModuleID = 'M3'\n" - "Global is referenced in a different module!\n" + .equals("Global is referenced in a different module!\n" "i32 ()* @foo2\n" "; ModuleID = 'M2'\n" " %call = call i32 @foo2()\n" "i32 ()* @foo1\n" - "; ModuleID = 'M1'\n")); + "; ModuleID = 'M1'\n" + "Global is used by function in a different module\n" + "i32 ()* @foo2\n" + "; ModuleID = 'M2'\n" + "i32 ()* @foo3\n" + "; ModuleID = 'M3'\n")); Error.clear(); EXPECT_TRUE(verifyModule(M1, &ErrorOS));