Index: lib/IR/Verifier.cpp =================================================================== --- lib/IR/Verifier.cpp +++ lib/IR/Verifier.cpp @@ -193,6 +193,8 @@ /// instruction has an operand that is an instruction in the same block. SmallPtrSet InstsInThisBlock; + SmallPtrSet Visited; + /// \brief Keep track of the metadata nodes that have been checked already. SmallPtrSet MDNodes; @@ -219,6 +221,11 @@ void checkAtomicMemAccessSize(const Module *M, Type *Ty, const Instruction *I); + + bool VisitIt(const GlobalValue &GV, const Value *V); + void ForEachUser(const Value *User, + SmallPtrSet &Visited, + const GlobalValue &GV); public: explicit Verifier(raw_ostream &OS) : VerifierSupport(OS), Context(nullptr), LandingPadResultTy(nullptr), @@ -462,17 +469,36 @@ InstVisitor::visit(I); } +bool Verifier::VisitIt(const GlobalValue &GV, const Value *V) { + if (const Instruction *I = dyn_cast(V)) { + if (!I->getParent() || !I->getParent()->getParent()) + CheckFailed("Global is referenced by parentless instruction!", &GV, + M, I); + else if (I->getParent()->getParent()->getParent() != M) + CheckFailed("Global is referenced in a different module!", &GV, + M, I, I->getParent()->getParent(), + I->getParent()->getParent()->getParent()); + return false; + } else if (const Function *F = dyn_cast(V)) { + if (F->getParent() != M) + CheckFailed("Global is used by function in a different module", &GV, + M, F, F->getParent()); + return false; + } + return true; +} + // Helper to recursively iterate over indirect users. By // returning false, the callback can ask to stop recursing // further. -static void forEachUser(const Value *User, - SmallPtrSet &Visited, - llvm::function_ref Callback) { +void Verifier::ForEachUser(const Value *User, + SmallPtrSet &Visited, + const GlobalValue &GV) { if (!Visited.insert(User).second) return; for (const Value *TheNextUser : User->users()) - if (Callback(TheNextUser)) - forEachUser(TheNextUser, Visited, Callback); + if (VisitIt(GV, TheNextUser)) + ForEachUser(TheNextUser, Visited, GV); } void Verifier::visitGlobalValue(const GlobalValue &GV) { @@ -498,25 +524,7 @@ // This map is used to avoid visiting uses twice. We can arrive at a user // twice, if they have multiple operands. In particular for very large // constant expressions, we can arrive at a particular user many times. - SmallPtrSet Visited; - forEachUser(&GV, Visited, [&](const Value *V) -> bool { - if (const Instruction *I = dyn_cast(V)) { - if (!I->getParent() || !I->getParent()->getParent()) - CheckFailed("Global is referenced by parentless instruction!", &GV, - M, I); - else if (I->getParent()->getParent()->getParent() != M) - CheckFailed("Global is referenced in a different module!", &GV, - M, I, I->getParent()->getParent(), - I->getParent()->getParent()->getParent()); - return false; - } else if (const Function *F = dyn_cast(V)) { - if (F->getParent() != M) - CheckFailed("Global is used by function in a different module", &GV, - M, F, F->getParent()); - return false; - } - return true; - }); + ForEachUser(&GV, Visited, GV); } void Verifier::visitGlobalVariable(const GlobalVariable &GV) {