[llvm] d34125a - [TableGen] Use heap allocated arrays instead of vectors for TreePatternNode::Types and ResultPerm. NFC

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 19 15:23:15 PDT 2023


Author: Craig Topper
Date: 2023-04-19T15:22:58-07:00
New Revision: d34125a1a825208b592cfa8f5fc3566303d691a4

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

LOG: [TableGen] Use heap allocated arrays instead of vectors for TreePatternNode::Types and ResultPerm. NFC

These vectors are resized in the constructor and never change size.
We can manually allocate two arrays instead.

This reduces the size of TreePatternNode by removing the
unneeded capacity end pointer fields from the std::vector.

Added: 
    

Modified: 
    llvm/utils/TableGen/CodeGenDAGPatterns.cpp
    llvm/utils/TableGen/CodeGenDAGPatterns.h
    llvm/utils/TableGen/DAGISelMatcherGen.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/utils/TableGen/CodeGenDAGPatterns.cpp b/llvm/utils/TableGen/CodeGenDAGPatterns.cpp
index 2b6e252b9863b..3a7452600595f 100644
--- a/llvm/utils/TableGen/CodeGenDAGPatterns.cpp
+++ b/llvm/utils/TableGen/CodeGenDAGPatterns.cpp
@@ -1759,7 +1759,7 @@ bool TreePatternNode::UpdateNodeTypeFromInst(unsigned ResNo,
 }
 
 bool TreePatternNode::ContainsUnresolvedType(TreePattern &TP) const {
-  for (unsigned i = 0, e = Types.size(); i != e; ++i)
+  for (unsigned i = 0, e = getNumTypes(); i != e; ++i)
     if (!TP.getInfer().isConcrete(Types[i], true))
       return true;
   for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
@@ -1769,7 +1769,7 @@ bool TreePatternNode::ContainsUnresolvedType(TreePattern &TP) const {
 }
 
 bool TreePatternNode::hasProperTypeByHwMode() const {
-  for (const TypeSetByHwMode &S : Types)
+  for (const TypeSetByHwMode &S : getExtTypes())
     if (!S.isSimple())
       return true;
   for (const TreePatternNodePtr &C : Children)
@@ -1779,7 +1779,7 @@ bool TreePatternNode::hasProperTypeByHwMode() const {
 }
 
 bool TreePatternNode::hasPossibleType() const {
-  for (const TypeSetByHwMode &S : Types)
+  for (const TypeSetByHwMode &S : getExtTypes())
     if (!S.isPossible())
       return false;
   for (const TreePatternNodePtr &C : Children)
@@ -1789,7 +1789,7 @@ bool TreePatternNode::hasPossibleType() const {
 }
 
 bool TreePatternNode::setDefaultMode(unsigned Mode) {
-  for (TypeSetByHwMode &S : Types) {
+  for (TypeSetByHwMode &S : getExtTypes()) {
     S.makeSimple(Mode);
     // Check if the selected mode had a type conflict.
     if (S.get(DefaultMode).empty())
@@ -1929,7 +1929,7 @@ void TreePatternNode::print(raw_ostream &OS) const {
   else
     OS << '(' << getOperator()->getName();
 
-  for (unsigned i = 0, e = Types.size(); i != e; ++i) {
+  for (unsigned i = 0, e = getNumTypes(); i != e; ++i) {
     OS << ':';
     getExtType(i).writeToStream(OS);
   }
@@ -2021,7 +2021,7 @@ TreePatternNodePtr TreePatternNode::clone() const {
   }
   New->setName(getName());
   New->setNamesAsPredicateArg(getNamesAsPredicateArg());
-  New->Types = Types;
+  llvm::copy(getExtTypes(), New->getExtTypes().begin());
   New->setPredicateCalls(getPredicateCalls());
   New->setGISelFlagsRecord(getGISelFlagsRecord());
   New->setTransformFn(getTransformFn());
@@ -2031,7 +2031,7 @@ TreePatternNodePtr TreePatternNode::clone() const {
 /// RemoveAllTypes - Recursively strip all the types of this tree.
 void TreePatternNode::RemoveAllTypes() {
   // Reset to unknown type.
-  std::fill(Types.begin(), Types.end(), TypeSetByHwMode());
+  std::fill(getExtTypes().begin(), getExtTypes().end(), TypeSetByHwMode());
   if (isLeaf()) return;
   for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
     getChild(i)->RemoveAllTypes();
@@ -2127,10 +2127,10 @@ void TreePatternNode::InlinePatternFragments(
       R->setPredicateCalls(getPredicateCalls());
       R->setGISelFlagsRecord(getGISelFlagsRecord());
       R->setTransformFn(getTransformFn());
-      for (unsigned i = 0, e = getNumTypes(); i != e; ++i)
+      for (unsigned i = 0, e = getNumTypes(); i != e; ++i) {
         R->setType(i, getExtType(i));
-      for (unsigned i = 0, e = getNumResults(); i != e; ++i)
         R->setResultIndex(i, getResultIndex(i));
+      }
 
       // Register alternative.
       OutAlternatives.push_back(R);
@@ -2465,7 +2465,7 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
     if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) {
       // If it's a regclass or something else known, include the type.
       bool MadeChange = false;
-      for (unsigned i = 0, e = Types.size(); i != e; ++i)
+      for (unsigned i = 0, e = getNumTypes(); i != e; ++i)
         MadeChange |= UpdateNodeType(i, getImplicitType(DI->getDef(), i,
                                                         NotRegisters,
                                                         !hasName(), TP), TP);
@@ -2473,7 +2473,7 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
     }
 
     if (IntInit *II = dyn_cast<IntInit>(getLeafValue())) {
-      assert(Types.size() == 1 && "Invalid IntInit");
+      assert(getNumTypes() == 1 && "Invalid IntInit");
 
       // Int inits are always integers. :)
       bool MadeChange = TP.getInfer().EnforceInteger(Types[0]);
@@ -2708,7 +2708,7 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
     bool MadeChange = false;
 
     if (!NotRegisters) {
-      assert(Types.size() == 1 && "ComplexPatterns only produce one result!");
+      assert(getNumTypes() == 1 && "ComplexPatterns only produce one result!");
       Record *T = CDP.getComplexPattern(getOperator()).getValueType();
       const CodeGenHwModes &CGH = CDP.getTargetInfo().getHwModes();
       const ValueTypeByHwMode VVT = getValueTypeByHwMode(T, CGH);

diff  --git a/llvm/utils/TableGen/CodeGenDAGPatterns.h b/llvm/utils/TableGen/CodeGenDAGPatterns.h
index d52ab2603cfae..2f11535171de3 100644
--- a/llvm/utils/TableGen/CodeGenDAGPatterns.h
+++ b/llvm/utils/TableGen/CodeGenDAGPatterns.h
@@ -626,13 +626,16 @@ struct TreePredicateCall {
 };
 
 class TreePatternNode : public RefCountedBase<TreePatternNode> {
+  /// Number of results for this node.
+  unsigned NumResults;
+
   /// The type of each node result.  Before and during type inference, each
   /// result may be a set of possible types.  After (successful) type inference,
   /// each is a single concrete type.
-  std::vector<TypeSetByHwMode> Types;
+  std::unique_ptr<TypeSetByHwMode[]> Types;
 
   /// The index of each result in results of the pattern.
-  std::vector<unsigned> ResultPerm;
+  std::unique_ptr<unsigned[]> ResultPerm;
 
   /// OperatorOrVal - The Record for the operator if this is an interior node
   /// (not a leaf) or the init value (e.g. the "GPRC" record, or "7") for a
@@ -651,7 +654,7 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
 
   /// TransformFn - The transformation function to execute on this node before
   /// it can be substituted into the resulting instruction on a pattern match.
-  Record *TransformFn;
+  Record *TransformFn = nullptr;
 
   std::vector<TreePatternNodePtr> Children;
 
@@ -661,17 +664,16 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
 
 public:
   TreePatternNode(Record *Op, std::vector<TreePatternNodePtr> Ch,
-                  unsigned NumResults)
-      : OperatorOrVal(Op), TransformFn(nullptr), Children(std::move(Ch)) {
-    Types.resize(NumResults);
-    ResultPerm.resize(NumResults);
-    std::iota(ResultPerm.begin(), ResultPerm.end(), 0);
+                  unsigned numResults)
+      : NumResults(numResults), Types(new TypeSetByHwMode[numResults]),
+        ResultPerm(new unsigned[numResults]), OperatorOrVal(Op),
+        Children(std::move(Ch)) {
+    std::iota(ResultPerm.get(), ResultPerm.get() + numResults, 0);
   }
-  TreePatternNode(Init *val, unsigned NumResults) // leaf ctor
-      : OperatorOrVal(val), TransformFn(nullptr) {
-    Types.resize(NumResults);
-    ResultPerm.resize(NumResults);
-    std::iota(ResultPerm.begin(), ResultPerm.end(), 0);
+  TreePatternNode(Init *val, unsigned numResults) // leaf ctor
+      : NumResults(numResults), Types(new TypeSetByHwMode[numResults]),
+        ResultPerm(new unsigned[numResults]), OperatorOrVal(val) {
+    std::iota(ResultPerm.get(), ResultPerm.get() + numResults, 0);
   }
 
   bool hasName() const { return !Name.empty(); }
@@ -691,11 +693,16 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
   bool isLeaf() const { return isa<Init *>(OperatorOrVal); }
 
   // Type accessors.
-  unsigned getNumTypes() const { return Types.size(); }
+  unsigned getNumTypes() const { return NumResults; }
   ValueTypeByHwMode getType(unsigned ResNo) const {
     return Types[ResNo].getValueTypeByHwMode();
   }
-  const std::vector<TypeSetByHwMode> &getExtTypes() const { return Types; }
+  ArrayRef<TypeSetByHwMode> getExtTypes() const {
+    return ArrayRef(Types.get(), NumResults);
+  }
+  MutableArrayRef<TypeSetByHwMode> getExtTypes() {
+    return MutableArrayRef(Types.get(), NumResults);
+  }
   const TypeSetByHwMode &getExtType(unsigned ResNo) const {
     return Types[ResNo];
   }
@@ -712,7 +719,6 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
     return Types[ResNo].empty();
   }
 
-  unsigned getNumResults() const { return ResultPerm.size(); }
   unsigned getResultIndex(unsigned ResNo) const { return ResultPerm[ResNo]; }
   void setResultIndex(unsigned ResNo, unsigned RI) { ResultPerm[ResNo] = RI; }
 
@@ -867,7 +873,6 @@ inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
   return OS;
 }
 
-
 /// TreePattern - Represent a pattern, used for instructions, pattern
 /// fragments, etc.
 ///

diff  --git a/llvm/utils/TableGen/DAGISelMatcherGen.cpp b/llvm/utils/TableGen/DAGISelMatcherGen.cpp
index f5144aa2bb871..c8d47e9a7503f 100644
--- a/llvm/utils/TableGen/DAGISelMatcherGen.cpp
+++ b/llvm/utils/TableGen/DAGISelMatcherGen.cpp
@@ -1074,7 +1074,7 @@ void MatcherGen::EmitResultCode() {
   SmallVector<unsigned, 8> Results(Ops);
 
   // Apply result permutation.
-  for (unsigned ResNo = 0; ResNo < Pattern.getDstPattern()->getNumResults();
+  for (unsigned ResNo = 0; ResNo < Pattern.getDstPattern()->getNumTypes();
        ++ResNo) {
     Results[ResNo] = Ops[Pattern.getDstPattern()->getResultIndex(ResNo)];
   }


        


More information about the llvm-commits mailing list