Index: include/llvm/ADT/DepthFirstIterator.h =================================================================== --- include/llvm/ADT/DepthFirstIterator.h +++ include/llvm/ADT/DepthFirstIterator.h @@ -107,7 +107,11 @@ if (!Opt) Opt.emplace(GT::child_begin(Node)); - for (NodeRef Next : make_range(*Opt, GT::child_end(Node))) { + // Notice that we use `auto &It =` here, as opposed to `auto It =` or + // range-based for loop, so that VisitStack.back().second actually gets + // updated. + for (auto &It = *Opt; It != GT::child_end(Node); ++It) { + NodeRef Next = *It; // Has our next sibling been visited? if (this->Visited.insert(Next).second) { // No, do it now.