[llvm] [TableGen] Add TreePatternNode::children and use it in for loops (NFCI) (PR #119877)

Sergei Barannikov via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 13 05:46:33 PST 2024


https://github.com/s-barannikov created https://github.com/llvm/llvm-project/pull/119877

None

>From 986526fb94bbf8a6cafc78bc34a6f8e5e6936b82 Mon Sep 17 00:00:00 2001
From: Sergei Barannikov <barannikov88 at gmail.com>
Date: Fri, 13 Dec 2024 16:40:26 +0300
Subject: [PATCH] [TableGen] Add TreePatternNode::children and use it in for
 loops

---
 .../TableGen/Common/CodeGenDAGPatterns.cpp    | 71 +++++++++----------
 .../TableGen/Common/CodeGenDAGPatterns.h      | 15 +++-
 llvm/utils/TableGen/DAGISelEmitter.cpp        | 12 ++--
 llvm/utils/TableGen/DAGISelMatcherGen.cpp     | 14 ++--
 llvm/utils/TableGen/FastISelEmitter.cpp       | 18 ++---
 llvm/utils/TableGen/GlobalISelEmitter.cpp     | 11 +--
 6 files changed, 75 insertions(+), 66 deletions(-)

diff --git a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
index b6b669f18cfbce..10f6590e9c7aa3 100644
--- a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
+++ b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
@@ -1440,8 +1440,7 @@ static unsigned getPatternSize(const TreePatternNode &P,
     ++Size;
 
   // Count children in the count if they are also nodes.
-  for (unsigned i = 0, e = P.getNumChildren(); i != e; ++i) {
-    const TreePatternNode &Child = P.getChild(i);
+  for (const TreePatternNode &Child : P.children()) {
     if (!Child.isLeaf() && Child.getNumTypes()) {
       const TypeSetByHwMode &T0 = Child.getExtType(0);
       // At this point, all variable type sets should be simple, i.e. only
@@ -1747,8 +1746,8 @@ bool TreePatternNode::ContainsUnresolvedType(TreePattern &TP) const {
   for (unsigned i = 0, e = Types.size(); i != e; ++i)
     if (!TP.getInfer().isConcrete(Types[i], true))
       return true;
-  for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
-    if (getChild(i).ContainsUnresolvedType(TP))
+  for (const TreePatternNode &Child : children())
+    if (Child.ContainsUnresolvedType(TP))
       return true;
   return false;
 }
@@ -1923,9 +1922,9 @@ void TreePatternNode::print(raw_ostream &OS) const {
     if (getNumChildren() != 0) {
       OS << " ";
       ListSeparator LS;
-      for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
+      for (const TreePatternNode &Child : children()) {
         OS << LS;
-        getChild(i).print(OS);
+        Child.print(OS);
       }
     }
     OS << ")";
@@ -1998,8 +1997,8 @@ TreePatternNodePtr TreePatternNode::clone() const {
   } else {
     std::vector<TreePatternNodePtr> CChildren;
     CChildren.reserve(Children.size());
-    for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
-      CChildren.push_back(getChild(i).clone());
+    for (const TreePatternNode &Child : children())
+      CChildren.push_back(Child.clone());
     New = makeIntrusiveRefCnt<TreePatternNode>(
         getOperator(), std::move(CChildren), getNumTypes());
   }
@@ -2018,8 +2017,8 @@ void TreePatternNode::RemoveAllTypes() {
   std::fill(Types.begin(), Types.end(), TypeSetByHwMode());
   if (isLeaf())
     return;
-  for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
-    getChild(i).RemoveAllTypes();
+  for (TreePatternNode &Child : children())
+    Child.RemoveAllTypes();
 }
 
 /// SubstituteFormalArguments - Replace the formal arguments in this tree
@@ -2392,8 +2391,8 @@ bool TreePatternNode::TreeHasProperty(SDNP Property,
                                       const CodeGenDAGPatterns &CGP) const {
   if (NodeHasProperty(Property, CGP))
     return true;
-  for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
-    if (getChild(i).TreeHasProperty(Property, CGP))
+  for (const TreePatternNode &Child : children())
+    if (Child.TreeHasProperty(Property, CGP))
       return true;
   return false;
 }
@@ -2528,8 +2527,8 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
     }
 
     bool MadeChange = false;
-    for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
-      MadeChange |= getChild(i).ApplyTypeConstraints(TP, NotRegisters);
+    for (TreePatternNode &Child : children())
+      MadeChange |= Child.ApplyTypeConstraints(TP, NotRegisters);
     MadeChange |= NI.ApplyTypeConstraints(*this, TP);
     return MadeChange;
   }
@@ -2679,8 +2678,8 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
       return false;
     }
 
-    for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
-      MadeChange |= getChild(i).ApplyTypeConstraints(TP, NotRegisters);
+    for (TreePatternNode &Child : children())
+      MadeChange |= Child.ApplyTypeConstraints(TP, NotRegisters);
     return MadeChange;
   }
 
@@ -2703,8 +2702,8 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
         MadeChange |= UpdateNodeType(0, VVT, TP);
     }
 
-    for (unsigned i = 0; i < getNumChildren(); ++i)
-      MadeChange |= getChild(i).ApplyTypeConstraints(TP, NotRegisters);
+    for (TreePatternNode &Child : children())
+      MadeChange |= Child.ApplyTypeConstraints(TP, NotRegisters);
 
     return MadeChange;
   }
@@ -2724,7 +2723,7 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
 
 /// OnlyOnRHSOfCommutative - Return true if this value is only allowed on the
 /// RHS of a commutative operation, not the on LHS.
-static bool OnlyOnRHSOfCommutative(TreePatternNode &N) {
+static bool OnlyOnRHSOfCommutative(const TreePatternNode &N) {
   if (!N.isLeaf() && N.getOperator()->getName() == "imm")
     return true;
   if (N.isLeaf() && isa<IntInit>(N.getLeafValue()))
@@ -2740,12 +2739,12 @@ static bool OnlyOnRHSOfCommutative(TreePatternNode &N) {
 /// that can never possibly work), and to prevent the pattern permuter from
 /// generating stuff that is useless.
 bool TreePatternNode::canPatternMatch(std::string &Reason,
-                                      const CodeGenDAGPatterns &CDP) {
+                                      const CodeGenDAGPatterns &CDP) const {
   if (isLeaf())
     return true;
 
-  for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
-    if (!getChild(i).canPatternMatch(Reason, CDP))
+  for (const TreePatternNode &Child : children())
+    if (!Child.canPatternMatch(Reason, CDP))
       return false;
 
   // If this is an intrinsic, handle cases that would make it not match.  For
@@ -2822,8 +2821,8 @@ void TreePattern::ComputeNamedNodes(TreePatternNode &N) {
   if (!N.getName().empty())
     NamedNodes[N.getName()].push_back(&N);
 
-  for (unsigned i = 0, e = N.getNumChildren(); i != e; ++i)
-    ComputeNamedNodes(N.getChild(i));
+  for (TreePatternNode &Child : N.children())
+    ComputeNamedNodes(Child);
 }
 
 TreePatternNodePtr TreePattern::ParseTreePattern(const Init *TheInit,
@@ -3595,8 +3594,8 @@ class InstAnalyzer {
     }
 
     // Analyze children.
-    for (unsigned i = 0, e = N.getNumChildren(); i != e; ++i)
-      AnalyzeNode(N.getChild(i));
+    for (const TreePatternNode &Child : N.children())
+      AnalyzeNode(Child);
 
     // Notice properties of the node.
     if (N.NodeHasProperty(SDNPMayStore, CDP))
@@ -3730,8 +3729,8 @@ static void getInstructionsInTree(TreePatternNode &Tree,
     return;
   if (Tree.getOperator()->isSubClassOf("Instruction"))
     Instrs.push_back(Tree.getOperator());
-  for (unsigned i = 0, e = Tree.getNumChildren(); i != e; ++i)
-    getInstructionsInTree(Tree.getChild(i), Instrs);
+  for (TreePatternNode &Child : Tree.children())
+    getInstructionsInTree(Child, Instrs);
 }
 
 /// Check the class of a pattern leaf node against the instruction operand it
@@ -4010,8 +4009,8 @@ static void FindNames(TreePatternNode &P,
   }
 
   if (!P.isLeaf()) {
-    for (unsigned i = 0, e = P.getNumChildren(); i != e; ++i)
-      FindNames(P.getChild(i), Names, PatternTop);
+    for (TreePatternNode &Child : P.children())
+      FindNames(Child, Names, PatternTop);
   }
 }
 
@@ -4195,8 +4194,8 @@ static bool ForceArbitraryInstResultType(TreePatternNode &N, TreePattern &TP) {
     return false;
 
   // Analyze children.
-  for (unsigned i = 0, e = N.getNumChildren(); i != e; ++i)
-    if (ForceArbitraryInstResultType(N.getChild(i), TP))
+  for (TreePatternNode &Child : N.children())
+    if (ForceArbitraryInstResultType(Child, TP))
       return true;
 
   if (!N.getOperator()->isSubClassOf("Instruction"))
@@ -4378,8 +4377,8 @@ static void collectModes(std::set<unsigned> &Modes, const TreePatternNode &N) {
     for (const auto &I : VTS)
       Modes.insert(I.first);
 
-  for (unsigned i = 0, e = N.getNumChildren(); i != e; ++i)
-    collectModes(Modes, N.getChild(i));
+  for (const TreePatternNode &Child : N.children())
+    collectModes(Modes, Child);
 }
 
 void CodeGenDAGPatterns::ExpandHwModeBasedTypes() {
@@ -4464,8 +4463,8 @@ static void FindDepVarsOf(TreePatternNode &N, DepVarMap &DepMap) {
     if (N.hasName() && isa<DefInit>(N.getLeafValue()))
       DepMap[N.getName()]++;
   } else {
-    for (size_t i = 0, e = N.getNumChildren(); i != e; ++i)
-      FindDepVarsOf(N.getChild(i), DepMap);
+    for (TreePatternNode &Child : N.children())
+      FindDepVarsOf(Child, DepMap);
   }
 }
 
diff --git a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.h b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.h
index f85753ff5ac80b..f8c39172938256 100644
--- a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.h
+++ b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.h
@@ -722,6 +722,18 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
     return cast<const Record *>(OperatorOrVal);
   }
 
+  using child_iterator = pointee_iterator<decltype(Children)::iterator>;
+  using child_const_iterator =
+      pointee_iterator<decltype(Children)::const_iterator>;
+
+  iterator_range<child_iterator> children() {
+    return make_pointee_range(Children);
+  }
+
+  iterator_range<child_const_iterator> children() const {
+    return make_pointee_range(Children);
+  }
+
   unsigned getNumChildren() const { return Children.size(); }
   const TreePatternNode &getChild(unsigned N) const {
     return *Children[N].get();
@@ -855,7 +867,8 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
 
   /// canPatternMatch - If it is impossible for this pattern to match on this
   /// target, fill in Reason and return false.  Otherwise, return true.
-  bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
+  bool canPatternMatch(std::string &Reason,
+                       const CodeGenDAGPatterns &CDP) const;
 };
 
 inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
diff --git a/llvm/utils/TableGen/DAGISelEmitter.cpp b/llvm/utils/TableGen/DAGISelEmitter.cpp
index 3d39ee148373fd..6d6d72eb70a5b8 100644
--- a/llvm/utils/TableGen/DAGISelEmitter.cpp
+++ b/llvm/utils/TableGen/DAGISelEmitter.cpp
@@ -42,7 +42,7 @@ class DAGISelEmitter {
 /// Compute the number of instructions for this pattern.
 /// This is a temporary hack.  We should really include the instruction
 /// latencies in this calculation.
-static unsigned getResultPatternCost(TreePatternNode &P,
+static unsigned getResultPatternCost(const TreePatternNode &P,
                                      const CodeGenDAGPatterns &CGP) {
   if (P.isLeaf())
     return 0;
@@ -55,14 +55,14 @@ static unsigned getResultPatternCost(TreePatternNode &P,
     if (II.usesCustomInserter)
       Cost += 10;
   }
-  for (unsigned I = 0, E = P.getNumChildren(); I != E; ++I)
-    Cost += getResultPatternCost(P.getChild(I), CGP);
+  for (const TreePatternNode &Child : P.children())
+    Cost += getResultPatternCost(Child, CGP);
   return Cost;
 }
 
 /// getResultPatternCodeSize - Compute the code size of instructions for this
 /// pattern.
-static unsigned getResultPatternSize(TreePatternNode &P,
+static unsigned getResultPatternSize(const TreePatternNode &P,
                                      const CodeGenDAGPatterns &CGP) {
   if (P.isLeaf())
     return 0;
@@ -72,8 +72,8 @@ static unsigned getResultPatternSize(TreePatternNode &P,
   if (Op->isSubClassOf("Instruction")) {
     Cost += Op->getValueAsInt("CodeSize");
   }
-  for (unsigned I = 0, E = P.getNumChildren(); I != E; ++I)
-    Cost += getResultPatternSize(P.getChild(I), CGP);
+  for (const TreePatternNode &Child : P.children())
+    Cost += getResultPatternSize(Child, CGP);
   return Cost;
 }
 
diff --git a/llvm/utils/TableGen/DAGISelMatcherGen.cpp b/llvm/utils/TableGen/DAGISelMatcherGen.cpp
index a3569bc1770b20..dd05f4df0d7236 100644
--- a/llvm/utils/TableGen/DAGISelMatcherGen.cpp
+++ b/llvm/utils/TableGen/DAGISelMatcherGen.cpp
@@ -307,9 +307,9 @@ void MatcherGen::EmitOperatorMatchCode(const TreePatternNode &N,
     // "MY_PAT:op1:op2". We should already have validated that the uses are
     // consistent.
     std::string PatternName = std::string(N.getOperator()->getName());
-    for (unsigned i = 0; i < N.getNumChildren(); ++i) {
+    for (const TreePatternNode &Child : N.children()) {
       PatternName += ":";
-      PatternName += N.getChild(i).getName();
+      PatternName += Child.getName();
     }
 
     if (recordUniqueNode(PatternName)) {
@@ -588,9 +588,9 @@ bool MatcherGen::EmitMatcherCode(unsigned Variant) {
       NamedComplexPatternOperands[N.getName()] = NextRecordedOperandNo + 1;
     } else {
       unsigned CurOp = NextRecordedOperandNo;
-      for (unsigned i = 0; i < N.getNumChildren(); ++i) {
-        NamedComplexPatternOperands[N.getChild(i).getName()] = CurOp + 1;
-        CurOp += N.getChild(i).getNumMIResults(CGP);
+      for (const TreePatternNode &Child : N.children()) {
+        NamedComplexPatternOperands[Child.getName()] = CurOp + 1;
+        CurOp += Child.getNumMIResults(CGP);
       }
     }
 
@@ -771,8 +771,8 @@ static unsigned numNodesThatMayLoadOrStore(const TreePatternNode &N,
   if (mayInstNodeLoadOrStore(N, CGP))
     ++Count;
 
-  for (unsigned i = 0, e = N.getNumChildren(); i != e; ++i)
-    Count += numNodesThatMayLoadOrStore(N.getChild(i), CGP);
+  for (const TreePatternNode &Child : N.children())
+    Count += numNodesThatMayLoadOrStore(Child, CGP);
 
   return Count;
 }
diff --git a/llvm/utils/TableGen/FastISelEmitter.cpp b/llvm/utils/TableGen/FastISelEmitter.cpp
index 58d2ecc3aaebc2..f60c63c212d619 100644
--- a/llvm/utils/TableGen/FastISelEmitter.cpp
+++ b/llvm/utils/TableGen/FastISelEmitter.cpp
@@ -218,9 +218,7 @@ struct OperandsSignature {
 
     const CodeGenRegisterClass *DstRC = nullptr;
 
-    for (unsigned i = 0, e = InstPatNode.getNumChildren(); i != e; ++i) {
-      TreePatternNode &Op = InstPatNode.getChild(i);
-
+    for (const TreePatternNode &Op : InstPatNode.children()) {
       // Handle imm operands specially.
       if (!Op.isLeaf() && Op.getOperator()->getName() == "imm") {
         unsigned PredNo = 0;
@@ -431,8 +429,8 @@ static std::string getLegalCName(std::string OpName) {
 
 FastISelMap::FastISelMap(StringRef instns) : InstNS(instns) {}
 
-static std::string PhyRegForNode(TreePatternNode &Op,
-                                 const CodeGenTarget &Target) {
+static std::string PhysRegForNode(const TreePatternNode &Op,
+                                  const CodeGenTarget &Target) {
   std::string PhysReg;
 
   if (!Op.isLeaf())
@@ -478,8 +476,7 @@ void FastISelMap::collectPatterns(const CodeGenDAGPatterns &CGP) {
 
     // For now, ignore multi-instruction patterns.
     bool MultiInsts = false;
-    for (unsigned i = 0, e = Dst.getNumChildren(); i != e; ++i) {
-      TreePatternNode &ChildOp = Dst.getChild(i);
+    for (const TreePatternNode &ChildOp : Dst.children()) {
       if (ChildOp.isLeaf())
         continue;
       if (ChildOp.getOperator()->isSubClassOf("Instruction")) {
@@ -555,12 +552,11 @@ void FastISelMap::collectPatterns(const CodeGenDAGPatterns &CGP) {
       // the mapping from the src to dst patterns is simple.
       bool FoundNonSimplePattern = false;
       unsigned DstIndex = 0;
-      for (unsigned i = 0, e = InstPatNode.getNumChildren(); i != e; ++i) {
-        std::string PhysReg = PhyRegForNode(InstPatNode.getChild(i), Target);
+      for (const TreePatternNode &SrcChild : InstPatNode.children()) {
+        std::string PhysReg = PhysRegForNode(SrcChild, Target);
         if (PhysReg.empty()) {
           if (DstIndex >= Dst.getNumChildren() ||
-              Dst.getChild(DstIndex).getName() !=
-                  InstPatNode.getChild(i).getName()) {
+              Dst.getChild(DstIndex).getName() != SrcChild.getName()) {
             FoundNonSimplePattern = true;
             break;
           }
diff --git a/llvm/utils/TableGen/GlobalISelEmitter.cpp b/llvm/utils/TableGen/GlobalISelEmitter.cpp
index a3344718cb3626..2259253ab48a00 100644
--- a/llvm/utils/TableGen/GlobalISelEmitter.cpp
+++ b/llvm/utils/TableGen/GlobalISelEmitter.cpp
@@ -974,9 +974,9 @@ Error GlobalISelEmitter::importChildMatcher(
     // The "name" of a non-leaf complex pattern (MY_PAT $op1, $op2) is
     // "MY_PAT:op1:op2" and the ones with same "name" represent same operand.
     std::string PatternName = std::string(SrcChild.getOperator()->getName());
-    for (unsigned I = 0; I < SrcChild.getNumChildren(); ++I) {
+    for (const TreePatternNode &Child : SrcChild.children()) {
       PatternName += ":";
-      PatternName += SrcChild.getChild(I).getName();
+      PatternName += Child.getName();
     }
     SrcChildName = PatternName;
   }
@@ -1348,11 +1348,12 @@ Expected<action_iterator> GlobalISelEmitter::importExplicitUseRenderer(
   // Handle the case where the MVT/register class is omitted in the dest pattern
   // but MVT exists in the source pattern.
   if (isa<UnsetInit>(DstChild.getLeafValue())) {
-    for (unsigned NumOp = 0; NumOp < Src.getNumChildren(); NumOp++)
-      if (Src.getChild(NumOp).getName() == DstChild.getName()) {
-        DstMIBuilder.addRenderer<CopyRenderer>(Src.getChild(NumOp).getName());
+    for (const TreePatternNode &SrcChild : Src.children()) {
+      if (SrcChild.getName() == DstChild.getName()) {
+        DstMIBuilder.addRenderer<CopyRenderer>(SrcChild.getName());
         return InsertPt;
       }
+    }
   }
   return failedImport("Dst pattern child is an unsupported kind");
 }



More information about the llvm-commits mailing list