[llvm] 2be67e1 - [TableGen] Avoid creating a ScopeMatcher full of nullptrs.

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 2 12:04:52 PDT 2023


Author: Craig Topper
Date: 2023-04-02T12:02:55-07:00
New Revision: 2be67e14ba4fb65b07b04b5a74e58e4749fd7dc2

URL: https://github.com/llvm/llvm-project/commit/2be67e14ba4fb65b07b04b5a74e58e4749fd7dc2
DIFF: https://github.com/llvm/llvm-project/commit/2be67e14ba4fb65b07b04b5a74e58e4749fd7dc2.diff

LOG: [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.

Added: 
    

Modified: 
    llvm/utils/TableGen/DAGISelMatcherOpt.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/utils/TableGen/DAGISelMatcherOpt.cpp b/llvm/utils/TableGen/DAGISelMatcherOpt.cpp
index 7dfd425f9010..6983ccef4413 100644
--- a/llvm/utils/TableGen/DAGISelMatcherOpt.cpp
+++ b/llvm/utils/TableGen/DAGISelMatcherOpt.cpp
@@ -209,15 +209,13 @@ static void FactorNodes(std::unique_ptr<Matcher> &InputMatcherPtr) {
     // Factor the subexpression.
     std::unique_ptr<Matcher> 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<ScopeMatcher>(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<ScopeMatcher>(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<Matcher> &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);
   }
   


        


More information about the llvm-commits mailing list