From 2be67e14ba4fb65b07b04b5a74e58e4749fd7dc2 Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Sun, 2 Apr 2023 11:38:26 -0700 Subject: [PATCH] [TableGen] Avoid creating a ScopeMatcher full of nullptrs. The call to FactorNodes will catch it and remove it, but it's easy to catch at creation. Remove the now unnecessary null checks from a loop in factor nodes. --- llvm/utils/TableGen/DAGISelMatcherOpt.cpp | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/llvm/utils/TableGen/DAGISelMatcherOpt.cpp b/llvm/utils/TableGen/DAGISelMatcherOpt.cpp index 7dfd425..6983cce 100644 --- a/llvm/utils/TableGen/DAGISelMatcherOpt.cpp +++ b/llvm/utils/TableGen/DAGISelMatcherOpt.cpp @@ -209,15 +209,13 @@ static void FactorNodes(std::unique_ptr &InputMatcherPtr) { // Factor the subexpression. std::unique_ptr Child(Scope->takeChild(i)); FactorNodes(Child); - - if (Child) { - // If the child is a ScopeMatcher we can just merge its contents. - if (auto *SM = dyn_cast(Child.get())) { - for (unsigned j = 0, e = SM->getNumChildren(); j != e; ++j) - OptionsToMatch.push_back(SM->takeChild(j)); - } else { - OptionsToMatch.push_back(Child.release()); - } + + // If the child is a ScopeMatcher we can just merge its contents. + if (auto *SM = dyn_cast(Child.get())) { + for (unsigned j = 0, e = SM->getNumChildren(); j != e; ++j) + OptionsToMatch.push_back(SM->takeChild(j)); + } else { + OptionsToMatch.push_back(Child.release()); } } @@ -323,13 +321,16 @@ static void FactorNodes(std::unique_ptr &InputMatcherPtr) { Matcher *Tmp = EqualMatchers[i]->takeNext(); delete EqualMatchers[i]; EqualMatchers[i] = Tmp; + assert(!Optn == !Tmp && "Expected all to be null if any are null"); } - Shared->setNext(new ScopeMatcher(std::move(EqualMatchers))); + if (EqualMatchers[0]) { + Shared->setNext(new ScopeMatcher(std::move(EqualMatchers))); + + // Recursively factor the newly created node. + FactorNodes(Shared->getNextPtr()); + } - // Recursively factor the newly created node. - FactorNodes(Shared->getNextPtr()); - NewOptionsToMatch.push_back(Shared); } -- 2.7.4