Skip to content

Commit d1f51c6

Browse files
authored
[TableGen] Add TreePatternNode::children and use it in for loops (NFC) (#119877)
1 parent 3b3394b commit d1f51c6

File tree

6 files changed

+75
-66
lines changed

6 files changed

+75
-66
lines changed

llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,8 +1440,7 @@ static unsigned getPatternSize(const TreePatternNode &P,
14401440
++Size;
14411441

14421442
// Count children in the count if they are also nodes.
1443-
for (unsigned i = 0, e = P.getNumChildren(); i != e; ++i) {
1444-
const TreePatternNode &Child = P.getChild(i);
1443+
for (const TreePatternNode &Child : P.children()) {
14451444
if (!Child.isLeaf() && Child.getNumTypes()) {
14461445
const TypeSetByHwMode &T0 = Child.getExtType(0);
14471446
// At this point, all variable type sets should be simple, i.e. only
@@ -1747,8 +1746,8 @@ bool TreePatternNode::ContainsUnresolvedType(TreePattern &TP) const {
17471746
for (unsigned i = 0, e = Types.size(); i != e; ++i)
17481747
if (!TP.getInfer().isConcrete(Types[i], true))
17491748
return true;
1750-
for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1751-
if (getChild(i).ContainsUnresolvedType(TP))
1749+
for (const TreePatternNode &Child : children())
1750+
if (Child.ContainsUnresolvedType(TP))
17521751
return true;
17531752
return false;
17541753
}
@@ -1923,9 +1922,9 @@ void TreePatternNode::print(raw_ostream &OS) const {
19231922
if (getNumChildren() != 0) {
19241923
OS << " ";
19251924
ListSeparator LS;
1926-
for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
1925+
for (const TreePatternNode &Child : children()) {
19271926
OS << LS;
1928-
getChild(i).print(OS);
1927+
Child.print(OS);
19291928
}
19301929
}
19311930
OS << ")";
@@ -1998,8 +1997,8 @@ TreePatternNodePtr TreePatternNode::clone() const {
19981997
} else {
19991998
std::vector<TreePatternNodePtr> CChildren;
20001999
CChildren.reserve(Children.size());
2001-
for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
2002-
CChildren.push_back(getChild(i).clone());
2000+
for (const TreePatternNode &Child : children())
2001+
CChildren.push_back(Child.clone());
20032002
New = makeIntrusiveRefCnt<TreePatternNode>(
20042003
getOperator(), std::move(CChildren), getNumTypes());
20052004
}
@@ -2018,8 +2017,8 @@ void TreePatternNode::RemoveAllTypes() {
20182017
std::fill(Types.begin(), Types.end(), TypeSetByHwMode());
20192018
if (isLeaf())
20202019
return;
2021-
for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
2022-
getChild(i).RemoveAllTypes();
2020+
for (TreePatternNode &Child : children())
2021+
Child.RemoveAllTypes();
20232022
}
20242023

20252024
/// SubstituteFormalArguments - Replace the formal arguments in this tree
@@ -2392,8 +2391,8 @@ bool TreePatternNode::TreeHasProperty(SDNP Property,
23922391
const CodeGenDAGPatterns &CGP) const {
23932392
if (NodeHasProperty(Property, CGP))
23942393
return true;
2395-
for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
2396-
if (getChild(i).TreeHasProperty(Property, CGP))
2394+
for (const TreePatternNode &Child : children())
2395+
if (Child.TreeHasProperty(Property, CGP))
23972396
return true;
23982397
return false;
23992398
}
@@ -2528,8 +2527,8 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
25282527
}
25292528

25302529
bool MadeChange = false;
2531-
for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
2532-
MadeChange |= getChild(i).ApplyTypeConstraints(TP, NotRegisters);
2530+
for (TreePatternNode &Child : children())
2531+
MadeChange |= Child.ApplyTypeConstraints(TP, NotRegisters);
25332532
MadeChange |= NI.ApplyTypeConstraints(*this, TP);
25342533
return MadeChange;
25352534
}
@@ -2679,8 +2678,8 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
26792678
return false;
26802679
}
26812680

2682-
for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
2683-
MadeChange |= getChild(i).ApplyTypeConstraints(TP, NotRegisters);
2681+
for (TreePatternNode &Child : children())
2682+
MadeChange |= Child.ApplyTypeConstraints(TP, NotRegisters);
26842683
return MadeChange;
26852684
}
26862685

@@ -2703,8 +2702,8 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
27032702
MadeChange |= UpdateNodeType(0, VVT, TP);
27042703
}
27052704

2706-
for (unsigned i = 0; i < getNumChildren(); ++i)
2707-
MadeChange |= getChild(i).ApplyTypeConstraints(TP, NotRegisters);
2705+
for (TreePatternNode &Child : children())
2706+
MadeChange |= Child.ApplyTypeConstraints(TP, NotRegisters);
27082707

27092708
return MadeChange;
27102709
}
@@ -2724,7 +2723,7 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
27242723

27252724
/// OnlyOnRHSOfCommutative - Return true if this value is only allowed on the
27262725
/// RHS of a commutative operation, not the on LHS.
2727-
static bool OnlyOnRHSOfCommutative(TreePatternNode &N) {
2726+
static bool OnlyOnRHSOfCommutative(const TreePatternNode &N) {
27282727
if (!N.isLeaf() && N.getOperator()->getName() == "imm")
27292728
return true;
27302729
if (N.isLeaf() && isa<IntInit>(N.getLeafValue()))
@@ -2740,12 +2739,12 @@ static bool OnlyOnRHSOfCommutative(TreePatternNode &N) {
27402739
/// that can never possibly work), and to prevent the pattern permuter from
27412740
/// generating stuff that is useless.
27422741
bool TreePatternNode::canPatternMatch(std::string &Reason,
2743-
const CodeGenDAGPatterns &CDP) {
2742+
const CodeGenDAGPatterns &CDP) const {
27442743
if (isLeaf())
27452744
return true;
27462745

2747-
for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
2748-
if (!getChild(i).canPatternMatch(Reason, CDP))
2746+
for (const TreePatternNode &Child : children())
2747+
if (!Child.canPatternMatch(Reason, CDP))
27492748
return false;
27502749

27512750
// If this is an intrinsic, handle cases that would make it not match. For
@@ -2822,8 +2821,8 @@ void TreePattern::ComputeNamedNodes(TreePatternNode &N) {
28222821
if (!N.getName().empty())
28232822
NamedNodes[N.getName()].push_back(&N);
28242823

2825-
for (unsigned i = 0, e = N.getNumChildren(); i != e; ++i)
2826-
ComputeNamedNodes(N.getChild(i));
2824+
for (TreePatternNode &Child : N.children())
2825+
ComputeNamedNodes(Child);
28272826
}
28282827

28292828
TreePatternNodePtr TreePattern::ParseTreePattern(const Init *TheInit,
@@ -3595,8 +3594,8 @@ class InstAnalyzer {
35953594
}
35963595

35973596
// Analyze children.
3598-
for (unsigned i = 0, e = N.getNumChildren(); i != e; ++i)
3599-
AnalyzeNode(N.getChild(i));
3597+
for (const TreePatternNode &Child : N.children())
3598+
AnalyzeNode(Child);
36003599

36013600
// Notice properties of the node.
36023601
if (N.NodeHasProperty(SDNPMayStore, CDP))
@@ -3730,8 +3729,8 @@ static void getInstructionsInTree(TreePatternNode &Tree,
37303729
return;
37313730
if (Tree.getOperator()->isSubClassOf("Instruction"))
37323731
Instrs.push_back(Tree.getOperator());
3733-
for (unsigned i = 0, e = Tree.getNumChildren(); i != e; ++i)
3734-
getInstructionsInTree(Tree.getChild(i), Instrs);
3732+
for (TreePatternNode &Child : Tree.children())
3733+
getInstructionsInTree(Child, Instrs);
37353734
}
37363735

37373736
/// Check the class of a pattern leaf node against the instruction operand it
@@ -4010,8 +4009,8 @@ static void FindNames(TreePatternNode &P,
40104009
}
40114010

40124011
if (!P.isLeaf()) {
4013-
for (unsigned i = 0, e = P.getNumChildren(); i != e; ++i)
4014-
FindNames(P.getChild(i), Names, PatternTop);
4012+
for (TreePatternNode &Child : P.children())
4013+
FindNames(Child, Names, PatternTop);
40154014
}
40164015
}
40174016

@@ -4195,8 +4194,8 @@ static bool ForceArbitraryInstResultType(TreePatternNode &N, TreePattern &TP) {
41954194
return false;
41964195

41974196
// Analyze children.
4198-
for (unsigned i = 0, e = N.getNumChildren(); i != e; ++i)
4199-
if (ForceArbitraryInstResultType(N.getChild(i), TP))
4197+
for (TreePatternNode &Child : N.children())
4198+
if (ForceArbitraryInstResultType(Child, TP))
42004199
return true;
42014200

42024201
if (!N.getOperator()->isSubClassOf("Instruction"))
@@ -4378,8 +4377,8 @@ static void collectModes(std::set<unsigned> &Modes, const TreePatternNode &N) {
43784377
for (const auto &I : VTS)
43794378
Modes.insert(I.first);
43804379

4381-
for (unsigned i = 0, e = N.getNumChildren(); i != e; ++i)
4382-
collectModes(Modes, N.getChild(i));
4380+
for (const TreePatternNode &Child : N.children())
4381+
collectModes(Modes, Child);
43834382
}
43844383

43854384
void CodeGenDAGPatterns::ExpandHwModeBasedTypes() {
@@ -4464,8 +4463,8 @@ static void FindDepVarsOf(TreePatternNode &N, DepVarMap &DepMap) {
44644463
if (N.hasName() && isa<DefInit>(N.getLeafValue()))
44654464
DepMap[N.getName()]++;
44664465
} else {
4467-
for (size_t i = 0, e = N.getNumChildren(); i != e; ++i)
4468-
FindDepVarsOf(N.getChild(i), DepMap);
4466+
for (TreePatternNode &Child : N.children())
4467+
FindDepVarsOf(Child, DepMap);
44694468
}
44704469
}
44714470

llvm/utils/TableGen/Common/CodeGenDAGPatterns.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,18 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
722722
return cast<const Record *>(OperatorOrVal);
723723
}
724724

725+
using child_iterator = pointee_iterator<decltype(Children)::iterator>;
726+
using child_const_iterator =
727+
pointee_iterator<decltype(Children)::const_iterator>;
728+
729+
iterator_range<child_iterator> children() {
730+
return make_pointee_range(Children);
731+
}
732+
733+
iterator_range<child_const_iterator> children() const {
734+
return make_pointee_range(Children);
735+
}
736+
725737
unsigned getNumChildren() const { return Children.size(); }
726738
const TreePatternNode &getChild(unsigned N) const {
727739
return *Children[N].get();
@@ -855,7 +867,8 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
855867

856868
/// canPatternMatch - If it is impossible for this pattern to match on this
857869
/// target, fill in Reason and return false. Otherwise, return true.
858-
bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
870+
bool canPatternMatch(std::string &Reason,
871+
const CodeGenDAGPatterns &CDP) const;
859872
};
860873

861874
inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {

llvm/utils/TableGen/DAGISelEmitter.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class DAGISelEmitter {
4242
/// Compute the number of instructions for this pattern.
4343
/// This is a temporary hack. We should really include the instruction
4444
/// latencies in this calculation.
45-
static unsigned getResultPatternCost(TreePatternNode &P,
45+
static unsigned getResultPatternCost(const TreePatternNode &P,
4646
const CodeGenDAGPatterns &CGP) {
4747
if (P.isLeaf())
4848
return 0;
@@ -55,14 +55,14 @@ static unsigned getResultPatternCost(TreePatternNode &P,
5555
if (II.usesCustomInserter)
5656
Cost += 10;
5757
}
58-
for (unsigned I = 0, E = P.getNumChildren(); I != E; ++I)
59-
Cost += getResultPatternCost(P.getChild(I), CGP);
58+
for (const TreePatternNode &Child : P.children())
59+
Cost += getResultPatternCost(Child, CGP);
6060
return Cost;
6161
}
6262

6363
/// getResultPatternCodeSize - Compute the code size of instructions for this
6464
/// pattern.
65-
static unsigned getResultPatternSize(TreePatternNode &P,
65+
static unsigned getResultPatternSize(const TreePatternNode &P,
6666
const CodeGenDAGPatterns &CGP) {
6767
if (P.isLeaf())
6868
return 0;
@@ -72,8 +72,8 @@ static unsigned getResultPatternSize(TreePatternNode &P,
7272
if (Op->isSubClassOf("Instruction")) {
7373
Cost += Op->getValueAsInt("CodeSize");
7474
}
75-
for (unsigned I = 0, E = P.getNumChildren(); I != E; ++I)
76-
Cost += getResultPatternSize(P.getChild(I), CGP);
75+
for (const TreePatternNode &Child : P.children())
76+
Cost += getResultPatternSize(Child, CGP);
7777
return Cost;
7878
}
7979

llvm/utils/TableGen/DAGISelMatcherGen.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ void MatcherGen::EmitOperatorMatchCode(const TreePatternNode &N,
307307
// "MY_PAT:op1:op2". We should already have validated that the uses are
308308
// consistent.
309309
std::string PatternName = std::string(N.getOperator()->getName());
310-
for (unsigned i = 0; i < N.getNumChildren(); ++i) {
310+
for (const TreePatternNode &Child : N.children()) {
311311
PatternName += ":";
312-
PatternName += N.getChild(i).getName();
312+
PatternName += Child.getName();
313313
}
314314

315315
if (recordUniqueNode(PatternName)) {
@@ -588,9 +588,9 @@ bool MatcherGen::EmitMatcherCode(unsigned Variant) {
588588
NamedComplexPatternOperands[N.getName()] = NextRecordedOperandNo + 1;
589589
} else {
590590
unsigned CurOp = NextRecordedOperandNo;
591-
for (unsigned i = 0; i < N.getNumChildren(); ++i) {
592-
NamedComplexPatternOperands[N.getChild(i).getName()] = CurOp + 1;
593-
CurOp += N.getChild(i).getNumMIResults(CGP);
591+
for (const TreePatternNode &Child : N.children()) {
592+
NamedComplexPatternOperands[Child.getName()] = CurOp + 1;
593+
CurOp += Child.getNumMIResults(CGP);
594594
}
595595
}
596596

@@ -771,8 +771,8 @@ static unsigned numNodesThatMayLoadOrStore(const TreePatternNode &N,
771771
if (mayInstNodeLoadOrStore(N, CGP))
772772
++Count;
773773

774-
for (unsigned i = 0, e = N.getNumChildren(); i != e; ++i)
775-
Count += numNodesThatMayLoadOrStore(N.getChild(i), CGP);
774+
for (const TreePatternNode &Child : N.children())
775+
Count += numNodesThatMayLoadOrStore(Child, CGP);
776776

777777
return Count;
778778
}

llvm/utils/TableGen/FastISelEmitter.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,7 @@ struct OperandsSignature {
218218

219219
const CodeGenRegisterClass *DstRC = nullptr;
220220

221-
for (unsigned i = 0, e = InstPatNode.getNumChildren(); i != e; ++i) {
222-
TreePatternNode &Op = InstPatNode.getChild(i);
223-
221+
for (const TreePatternNode &Op : InstPatNode.children()) {
224222
// Handle imm operands specially.
225223
if (!Op.isLeaf() && Op.getOperator()->getName() == "imm") {
226224
unsigned PredNo = 0;
@@ -431,8 +429,8 @@ static std::string getLegalCName(std::string OpName) {
431429

432430
FastISelMap::FastISelMap(StringRef instns) : InstNS(instns) {}
433431

434-
static std::string PhyRegForNode(TreePatternNode &Op,
435-
const CodeGenTarget &Target) {
432+
static std::string PhysRegForNode(const TreePatternNode &Op,
433+
const CodeGenTarget &Target) {
436434
std::string PhysReg;
437435

438436
if (!Op.isLeaf())
@@ -478,8 +476,7 @@ void FastISelMap::collectPatterns(const CodeGenDAGPatterns &CGP) {
478476

479477
// For now, ignore multi-instruction patterns.
480478
bool MultiInsts = false;
481-
for (unsigned i = 0, e = Dst.getNumChildren(); i != e; ++i) {
482-
TreePatternNode &ChildOp = Dst.getChild(i);
479+
for (const TreePatternNode &ChildOp : Dst.children()) {
483480
if (ChildOp.isLeaf())
484481
continue;
485482
if (ChildOp.getOperator()->isSubClassOf("Instruction")) {
@@ -555,12 +552,11 @@ void FastISelMap::collectPatterns(const CodeGenDAGPatterns &CGP) {
555552
// the mapping from the src to dst patterns is simple.
556553
bool FoundNonSimplePattern = false;
557554
unsigned DstIndex = 0;
558-
for (unsigned i = 0, e = InstPatNode.getNumChildren(); i != e; ++i) {
559-
std::string PhysReg = PhyRegForNode(InstPatNode.getChild(i), Target);
555+
for (const TreePatternNode &SrcChild : InstPatNode.children()) {
556+
std::string PhysReg = PhysRegForNode(SrcChild, Target);
560557
if (PhysReg.empty()) {
561558
if (DstIndex >= Dst.getNumChildren() ||
562-
Dst.getChild(DstIndex).getName() !=
563-
InstPatNode.getChild(i).getName()) {
559+
Dst.getChild(DstIndex).getName() != SrcChild.getName()) {
564560
FoundNonSimplePattern = true;
565561
break;
566562
}

llvm/utils/TableGen/GlobalISelEmitter.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -974,9 +974,9 @@ Error GlobalISelEmitter::importChildMatcher(
974974
// The "name" of a non-leaf complex pattern (MY_PAT $op1, $op2) is
975975
// "MY_PAT:op1:op2" and the ones with same "name" represent same operand.
976976
std::string PatternName = std::string(SrcChild.getOperator()->getName());
977-
for (unsigned I = 0; I < SrcChild.getNumChildren(); ++I) {
977+
for (const TreePatternNode &Child : SrcChild.children()) {
978978
PatternName += ":";
979-
PatternName += SrcChild.getChild(I).getName();
979+
PatternName += Child.getName();
980980
}
981981
SrcChildName = PatternName;
982982
}
@@ -1348,11 +1348,12 @@ Expected<action_iterator> GlobalISelEmitter::importExplicitUseRenderer(
13481348
// Handle the case where the MVT/register class is omitted in the dest pattern
13491349
// but MVT exists in the source pattern.
13501350
if (isa<UnsetInit>(DstChild.getLeafValue())) {
1351-
for (unsigned NumOp = 0; NumOp < Src.getNumChildren(); NumOp++)
1352-
if (Src.getChild(NumOp).getName() == DstChild.getName()) {
1353-
DstMIBuilder.addRenderer<CopyRenderer>(Src.getChild(NumOp).getName());
1351+
for (const TreePatternNode &SrcChild : Src.children()) {
1352+
if (SrcChild.getName() == DstChild.getName()) {
1353+
DstMIBuilder.addRenderer<CopyRenderer>(SrcChild.getName());
13541354
return InsertPt;
13551355
}
1356+
}
13561357
}
13571358
return failedImport("Dst pattern child is an unsupported kind");
13581359
}

0 commit comments

Comments
 (0)