[llvm] r313783 - [TableGen] Some optimizations to TableGen.

Zachary Turner via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 20 11:01:40 PDT 2017


Author: zturner
Date: Wed Sep 20 11:01:40 2017
New Revision: 313783

URL: http://llvm.org/viewvc/llvm-project?rev=313783&view=rev
Log:
[TableGen] Some optimizations to TableGen.

This changes some STL data types to corresponding LLVM
data types that have better performance characteristics.

Differential Revision: https://reviews.llvm.org/D37957

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

Modified: llvm/trunk/utils/TableGen/CodeGenDAGPatterns.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/CodeGenDAGPatterns.cpp?rev=313783&r1=313782&r2=313783&view=diff
==============================================================================
--- llvm/trunk/utils/TableGen/CodeGenDAGPatterns.cpp (original)
+++ llvm/trunk/utils/TableGen/CodeGenDAGPatterns.cpp Wed Sep 20 11:01:40 2017
@@ -13,10 +13,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "CodeGenDAGPatterns.h"
+#include "llvm/ADT/DenseSet.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringSet.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -25,7 +27,6 @@
 #include <algorithm>
 #include <cstdio>
 #include <set>
-#include <sstream>
 using namespace llvm;
 
 #define DEBUG_TYPE "dag-patterns"
@@ -98,7 +99,7 @@ bool TypeSetByHwMode::isPossible() const
 
 bool TypeSetByHwMode::insert(const ValueTypeByHwMode &VVT) {
   bool Changed = false;
-  std::set<unsigned> Modes;
+  SmallDenseSet<unsigned, 4> Modes;
   for (const auto &P : VVT) {
     unsigned M = P.first;
     Modes.insert(M);
@@ -114,7 +115,6 @@ bool TypeSetByHwMode::insert(const Value
       if (!Modes.count(I.first))
         Changed |= I.second.insert(DT).second;
   }
-
   return Changed;
 }
 
@@ -164,40 +164,37 @@ bool TypeSetByHwMode::assign_if(const Ty
   return !empty();
 }
 
-std::string TypeSetByHwMode::getAsString() const {
-  std::stringstream str;
-  std::vector<unsigned> Modes;
+void TypeSetByHwMode::writeToStream(raw_ostream &OS) const {
+  SmallVector<unsigned, 4> Modes;
+  Modes.reserve(Map.size());
 
   for (const auto &I : *this)
     Modes.push_back(I.first);
-  if (Modes.empty())
-    return "{}";
+  if (Modes.empty()) {
+    OS << "{}";
+    return;
+  }
   array_pod_sort(Modes.begin(), Modes.end());
 
-  str << '{';
+  OS << '{';
   for (unsigned M : Modes) {
-    const SetType &S = get(M);
-    str << ' ' << getModeName(M) << ':' << getAsString(S);
+    OS << ' ' << getModeName(M) << ':';
+    writeToStream(get(M), OS);
   }
-  str << " }";
-  return str.str();
+  OS << " }";
 }
 
-std::string TypeSetByHwMode::getAsString(const SetType &S) {
-  std::vector<MVT> Types;
-  for (MVT T : S)
-    Types.push_back(T);
+void TypeSetByHwMode::writeToStream(const SetType &S, raw_ostream &OS) {
+  SmallVector<MVT, 4> Types(S.begin(), S.end());
   array_pod_sort(Types.begin(), Types.end());
 
-  std::stringstream str;
-  str << '[';
+  OS << '[';
   for (unsigned i = 0, e = Types.size(); i != e; ++i) {
-    str << ValueTypeByHwMode::getMVTName(Types[i]);
+    OS << ValueTypeByHwMode::getMVTName(Types[i]);
     if (i != e-1)
-      str << ' ';
+      OS << ' ';
   }
-  str << ']';
-  return str.str();
+  OS << ']';
 }
 
 bool TypeSetByHwMode::operator==(const TypeSetByHwMode &VTS) const {
@@ -211,7 +208,7 @@ bool TypeSetByHwMode::operator==(const T
     return false;
   }
 
-  std::set<unsigned> Modes;
+  SmallDenseSet<unsigned, 4> Modes;
   for (auto &I : *this)
     Modes.insert(I.first);
   for (const auto &I : VTS)
@@ -243,7 +240,8 @@ bool TypeSetByHwMode::operator==(const T
 
 LLVM_DUMP_METHOD
 void TypeSetByHwMode::dump() const {
-  dbgs() << getAsString() << '\n';
+  writeToStream(dbgs());
+  dbgs() << '\n';
 }
 
 bool TypeSetByHwMode::intersect(SetType &Out, const SetType &In) {
@@ -784,6 +782,7 @@ void TypeInfer::expandOverloads(TypeSetB
   for (MVT T : Out) {
     if (!T.isOverloaded())
       continue;
+
     Ovs.insert(T);
     // MachineValueTypeSet allows iteration and erasing.
     Out.erase(T);
@@ -1410,8 +1409,10 @@ void TreePatternNode::print(raw_ostream
   else
     OS << '(' << getOperator()->getName();
 
-  for (unsigned i = 0, e = Types.size(); i != e; ++i)
-    OS << ':' << getExtType(i).getAsString();
+  for (unsigned i = 0, e = Types.size(); i != e; ++i) {
+    OS << ':';
+    getExtType(i).writeToStream(OS);
+  }
 
   if (!isLeaf()) {
     if (getNumChildren() != 0) {
@@ -2628,7 +2629,10 @@ void CodeGenDAGPatterns::ParsePatternFra
 
     // Validate the argument list, converting it to set, to discard duplicates.
     std::vector<std::string> &Args = P->getArgList();
-    std::set<std::string> OperandsSet(Args.begin(), Args.end());
+    // Copy the args so we can take StringRefs to them.
+    auto ArgsCopy = Args;
+    SmallDenseSet<StringRef, 4> OperandsSet;
+    OperandsSet.insert(ArgsCopy.begin(), ArgsCopy.end());
 
     if (OperandsSet.count(""))
       P->error("Cannot have unnamed 'node' values in pattern fragment!");
@@ -3120,17 +3124,20 @@ const DAGInstruction &CodeGenDAGPatterns
 
   // Verify that the top-level forms in the instruction are of void type, and
   // fill in the InstResults map.
+  SmallString<32> TypesString;
   for (unsigned j = 0, e = I->getNumTrees(); j != e; ++j) {
+    TypesString.clear();
     TreePatternNode *Pat = I->getTree(j);
     if (Pat->getNumTypes() != 0) {
-      std::string Types;
+      raw_svector_ostream OS(TypesString);
       for (unsigned k = 0, ke = Pat->getNumTypes(); k != ke; ++k) {
         if (k > 0)
-          Types += ", ";
-        Types += Pat->getExtType(k).getAsString();
+          OS << ", ";
+        Pat->getExtType(k).writeToStream(OS);
       }
       I->error("Top-level forms in instruction pattern should have"
-               " void types, has types " + Types);
+               " void types, has types " +
+               OS.str());
     }
 
     // Find inputs and outputs, and verify the structure of the uses/defs.
@@ -3812,11 +3819,11 @@ void CodeGenDAGPatterns::ExpandHwModeBas
 }
 
 /// Dependent variable map for CodeGenDAGPattern variant generation
-typedef std::map<std::string, int> DepVarMap;
+typedef StringMap<int> DepVarMap;
 
 static void FindDepVarsOf(TreePatternNode *N, DepVarMap &DepMap) {
   if (N->isLeaf()) {
-    if (isa<DefInit>(N->getLeafValue()))
+    if (N->hasName() && isa<DefInit>(N->getLeafValue()))
       DepMap[N->getName()]++;
   } else {
     for (size_t i = 0, e = N->getNumChildren(); i != e; ++i)
@@ -3828,9 +3835,9 @@ static void FindDepVarsOf(TreePatternNod
 static void FindDepVars(TreePatternNode *N, MultipleUseVarSet &DepVars) {
   DepVarMap depcounts;
   FindDepVarsOf(N, depcounts);
-  for (const std::pair<std::string, int> &Pair : depcounts) {
-    if (Pair.second > 1)
-      DepVars.insert(Pair.first);
+  for (const auto &Pair : depcounts) {
+    if (Pair.getValue() > 1)
+      DepVars.insert(Pair.getKey());
   }
 }
 
@@ -3841,8 +3848,8 @@ static void DumpDepVars(MultipleUseVarSe
     DEBUG(errs() << "<empty set>");
   } else {
     DEBUG(errs() << "[ ");
-    for (const std::string &DepVar : DepVars) {
-      DEBUG(errs() << DepVar << " ");
+    for (const auto &DepVar : DepVars) {
+      DEBUG(errs() << DepVar.getKey() << " ");
     }
     DEBUG(errs() << "]");
   }

Modified: llvm/trunk/utils/TableGen/CodeGenDAGPatterns.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/CodeGenDAGPatterns.h?rev=313783&r1=313782&r2=313783&view=diff
==============================================================================
--- llvm/trunk/utils/TableGen/CodeGenDAGPatterns.h (original)
+++ llvm/trunk/utils/TableGen/CodeGenDAGPatterns.h Wed Sep 20 11:01:40 2017
@@ -20,6 +20,7 @@
 #include "CodeGenTarget.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/StringSet.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/MathExtras.h"
 #include <algorithm>
@@ -222,11 +223,11 @@ struct TypeSetByHwMode : public InfoByHw
   bool insert(const ValueTypeByHwMode &VVT);
   bool constrain(const TypeSetByHwMode &VTS);
   template <typename Predicate> bool constrain(Predicate P);
-  template <typename Predicate> bool assign_if(const TypeSetByHwMode &VTS,
-                                               Predicate P);
+  template <typename Predicate>
+  bool assign_if(const TypeSetByHwMode &VTS, Predicate P);
 
-  std::string getAsString() const;
-  static std::string getAsString(const SetType &S);
+  void writeToStream(raw_ostream &OS) const;
+  static void writeToStream(const SetType &S, raw_ostream &OS);
 
   bool operator==(const TypeSetByHwMode &VTS) const;
   bool operator!=(const TypeSetByHwMode &VTS) const { return !(*this == VTS); }
@@ -333,7 +334,7 @@ private:
 };
 
 /// Set type used to track multiply used variables in patterns
-typedef std::set<std::string> MultipleUseVarSet;
+typedef StringSet<> MultipleUseVarSet;
 
 /// SDTypeConstraint - This is a discriminated union of constraints,
 /// corresponding to the SDTypeConstraint tablegen class in Target.td.




More information about the llvm-commits mailing list