Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit e444299

Browse files
author
Zachary Turner
committed
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@313783 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent d76e3d5 commit e444299

File tree

2 files changed

+52
-44
lines changed

2 files changed

+52
-44
lines changed

utils/TableGen/CodeGenDAGPatterns.cpp

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
#include "CodeGenDAGPatterns.h"
16+
#include "llvm/ADT/DenseSet.h"
1617
#include "llvm/ADT/STLExtras.h"
1718
#include "llvm/ADT/SmallSet.h"
1819
#include "llvm/ADT/SmallString.h"
1920
#include "llvm/ADT/StringExtras.h"
21+
#include "llvm/ADT/StringSet.h"
2022
#include "llvm/ADT/Twine.h"
2123
#include "llvm/Support/Debug.h"
2224
#include "llvm/Support/ErrorHandling.h"
@@ -25,7 +27,6 @@
2527
#include <algorithm>
2628
#include <cstdio>
2729
#include <set>
28-
#include <sstream>
2930
using namespace llvm;
3031

3132
#define DEBUG_TYPE "dag-patterns"
@@ -98,7 +99,7 @@ bool TypeSetByHwMode::isPossible() const {
9899

99100
bool TypeSetByHwMode::insert(const ValueTypeByHwMode &VVT) {
100101
bool Changed = false;
101-
std::set<unsigned> Modes;
102+
SmallDenseSet<unsigned, 4> Modes;
102103
for (const auto &P : VVT) {
103104
unsigned M = P.first;
104105
Modes.insert(M);
@@ -114,7 +115,6 @@ bool TypeSetByHwMode::insert(const ValueTypeByHwMode &VVT) {
114115
if (!Modes.count(I.first))
115116
Changed |= I.second.insert(DT).second;
116117
}
117-
118118
return Changed;
119119
}
120120

@@ -164,40 +164,37 @@ bool TypeSetByHwMode::assign_if(const TypeSetByHwMode &VTS, Predicate P) {
164164
return !empty();
165165
}
166166

167-
std::string TypeSetByHwMode::getAsString() const {
168-
std::stringstream str;
169-
std::vector<unsigned> Modes;
167+
void TypeSetByHwMode::writeToStream(raw_ostream &OS) const {
168+
SmallVector<unsigned, 4> Modes;
169+
Modes.reserve(Map.size());
170170

171171
for (const auto &I : *this)
172172
Modes.push_back(I.first);
173-
if (Modes.empty())
174-
return "{}";
173+
if (Modes.empty()) {
174+
OS << "{}";
175+
return;
176+
}
175177
array_pod_sort(Modes.begin(), Modes.end());
176178

177-
str << '{';
179+
OS << '{';
178180
for (unsigned M : Modes) {
179-
const SetType &S = get(M);
180-
str << ' ' << getModeName(M) << ':' << getAsString(S);
181+
OS << ' ' << getModeName(M) << ':';
182+
writeToStream(get(M), OS);
181183
}
182-
str << " }";
183-
return str.str();
184+
OS << " }";
184185
}
185186

186-
std::string TypeSetByHwMode::getAsString(const SetType &S) {
187-
std::vector<MVT> Types;
188-
for (MVT T : S)
189-
Types.push_back(T);
187+
void TypeSetByHwMode::writeToStream(const SetType &S, raw_ostream &OS) {
188+
SmallVector<MVT, 4> Types(S.begin(), S.end());
190189
array_pod_sort(Types.begin(), Types.end());
191190

192-
std::stringstream str;
193-
str << '[';
191+
OS << '[';
194192
for (unsigned i = 0, e = Types.size(); i != e; ++i) {
195-
str << ValueTypeByHwMode::getMVTName(Types[i]);
193+
OS << ValueTypeByHwMode::getMVTName(Types[i]);
196194
if (i != e-1)
197-
str << ' ';
195+
OS << ' ';
198196
}
199-
str << ']';
200-
return str.str();
197+
OS << ']';
201198
}
202199

203200
bool TypeSetByHwMode::operator==(const TypeSetByHwMode &VTS) const {
@@ -211,7 +208,7 @@ bool TypeSetByHwMode::operator==(const TypeSetByHwMode &VTS) const {
211208
return false;
212209
}
213210

214-
std::set<unsigned> Modes;
211+
SmallDenseSet<unsigned, 4> Modes;
215212
for (auto &I : *this)
216213
Modes.insert(I.first);
217214
for (const auto &I : VTS)
@@ -243,7 +240,8 @@ bool TypeSetByHwMode::operator==(const TypeSetByHwMode &VTS) const {
243240

244241
LLVM_DUMP_METHOD
245242
void TypeSetByHwMode::dump() const {
246-
dbgs() << getAsString() << '\n';
243+
writeToStream(dbgs());
244+
dbgs() << '\n';
247245
}
248246

249247
bool TypeSetByHwMode::intersect(SetType &Out, const SetType &In) {
@@ -784,6 +782,7 @@ void TypeInfer::expandOverloads(TypeSetByHwMode::SetType &Out,
784782
for (MVT T : Out) {
785783
if (!T.isOverloaded())
786784
continue;
785+
787786
Ovs.insert(T);
788787
// MachineValueTypeSet allows iteration and erasing.
789788
Out.erase(T);
@@ -1410,8 +1409,10 @@ void TreePatternNode::print(raw_ostream &OS) const {
14101409
else
14111410
OS << '(' << getOperator()->getName();
14121411

1413-
for (unsigned i = 0, e = Types.size(); i != e; ++i)
1414-
OS << ':' << getExtType(i).getAsString();
1412+
for (unsigned i = 0, e = Types.size(); i != e; ++i) {
1413+
OS << ':';
1414+
getExtType(i).writeToStream(OS);
1415+
}
14151416

14161417
if (!isLeaf()) {
14171418
if (getNumChildren() != 0) {
@@ -2628,7 +2629,10 @@ void CodeGenDAGPatterns::ParsePatternFragments(bool OutFrags) {
26282629

26292630
// Validate the argument list, converting it to set, to discard duplicates.
26302631
std::vector<std::string> &Args = P->getArgList();
2631-
std::set<std::string> OperandsSet(Args.begin(), Args.end());
2632+
// Copy the args so we can take StringRefs to them.
2633+
auto ArgsCopy = Args;
2634+
SmallDenseSet<StringRef, 4> OperandsSet;
2635+
OperandsSet.insert(ArgsCopy.begin(), ArgsCopy.end());
26322636

26332637
if (OperandsSet.count(""))
26342638
P->error("Cannot have unnamed 'node' values in pattern fragment!");
@@ -3120,17 +3124,20 @@ const DAGInstruction &CodeGenDAGPatterns::parseInstructionPattern(
31203124

31213125
// Verify that the top-level forms in the instruction are of void type, and
31223126
// fill in the InstResults map.
3127+
SmallString<32> TypesString;
31233128
for (unsigned j = 0, e = I->getNumTrees(); j != e; ++j) {
3129+
TypesString.clear();
31243130
TreePatternNode *Pat = I->getTree(j);
31253131
if (Pat->getNumTypes() != 0) {
3126-
std::string Types;
3132+
raw_svector_ostream OS(TypesString);
31273133
for (unsigned k = 0, ke = Pat->getNumTypes(); k != ke; ++k) {
31283134
if (k > 0)
3129-
Types += ", ";
3130-
Types += Pat->getExtType(k).getAsString();
3135+
OS << ", ";
3136+
Pat->getExtType(k).writeToStream(OS);
31313137
}
31323138
I->error("Top-level forms in instruction pattern should have"
3133-
" void types, has types " + Types);
3139+
" void types, has types " +
3140+
OS.str());
31343141
}
31353142

31363143
// Find inputs and outputs, and verify the structure of the uses/defs.
@@ -3812,11 +3819,11 @@ void CodeGenDAGPatterns::ExpandHwModeBasedTypes() {
38123819
}
38133820

38143821
/// Dependent variable map for CodeGenDAGPattern variant generation
3815-
typedef std::map<std::string, int> DepVarMap;
3822+
typedef StringMap<int> DepVarMap;
38163823

38173824
static void FindDepVarsOf(TreePatternNode *N, DepVarMap &DepMap) {
38183825
if (N->isLeaf()) {
3819-
if (isa<DefInit>(N->getLeafValue()))
3826+
if (N->hasName() && isa<DefInit>(N->getLeafValue()))
38203827
DepMap[N->getName()]++;
38213828
} else {
38223829
for (size_t i = 0, e = N->getNumChildren(); i != e; ++i)
@@ -3828,9 +3835,9 @@ static void FindDepVarsOf(TreePatternNode *N, DepVarMap &DepMap) {
38283835
static void FindDepVars(TreePatternNode *N, MultipleUseVarSet &DepVars) {
38293836
DepVarMap depcounts;
38303837
FindDepVarsOf(N, depcounts);
3831-
for (const std::pair<std::string, int> &Pair : depcounts) {
3832-
if (Pair.second > 1)
3833-
DepVars.insert(Pair.first);
3838+
for (const auto &Pair : depcounts) {
3839+
if (Pair.getValue() > 1)
3840+
DepVars.insert(Pair.getKey());
38343841
}
38353842
}
38363843

@@ -3841,8 +3848,8 @@ static void DumpDepVars(MultipleUseVarSet &DepVars) {
38413848
DEBUG(errs() << "<empty set>");
38423849
} else {
38433850
DEBUG(errs() << "[ ");
3844-
for (const std::string &DepVar : DepVars) {
3845-
DEBUG(errs() << DepVar << " ");
3851+
for (const auto &DepVar : DepVars) {
3852+
DEBUG(errs() << DepVar.getKey() << " ");
38463853
}
38473854
DEBUG(errs() << "]");
38483855
}

utils/TableGen/CodeGenDAGPatterns.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "CodeGenTarget.h"
2121
#include "llvm/ADT/SmallVector.h"
2222
#include "llvm/ADT/StringMap.h"
23+
#include "llvm/ADT/StringSet.h"
2324
#include "llvm/Support/ErrorHandling.h"
2425
#include "llvm/Support/MathExtras.h"
2526
#include <algorithm>
@@ -222,11 +223,11 @@ struct TypeSetByHwMode : public InfoByHwMode<MachineValueTypeSet> {
222223
bool insert(const ValueTypeByHwMode &VVT);
223224
bool constrain(const TypeSetByHwMode &VTS);
224225
template <typename Predicate> bool constrain(Predicate P);
225-
template <typename Predicate> bool assign_if(const TypeSetByHwMode &VTS,
226-
Predicate P);
226+
template <typename Predicate>
227+
bool assign_if(const TypeSetByHwMode &VTS, Predicate P);
227228

228-
std::string getAsString() const;
229-
static std::string getAsString(const SetType &S);
229+
void writeToStream(raw_ostream &OS) const;
230+
static void writeToStream(const SetType &S, raw_ostream &OS);
230231

231232
bool operator==(const TypeSetByHwMode &VTS) const;
232233
bool operator!=(const TypeSetByHwMode &VTS) const { return !(*this == VTS); }
@@ -333,7 +334,7 @@ struct TypeInfer {
333334
};
334335

335336
/// Set type used to track multiply used variables in patterns
336-
typedef std::set<std::string> MultipleUseVarSet;
337+
typedef StringSet<> MultipleUseVarSet;
337338

338339
/// SDTypeConstraint - This is a discriminated union of constraints,
339340
/// corresponding to the SDTypeConstraint tablegen class in Target.td.

0 commit comments

Comments
 (0)