Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 35 additions & 36 deletions llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 << ")";
Expand Down Expand Up @@ -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());
}
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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()))
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
}

Expand Down
15 changes: 14 additions & 1 deletion llvm/utils/TableGen/Common/CodeGenDAGPatterns.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions llvm/utils/TableGen/DAGISelEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down
14 changes: 7 additions & 7 deletions llvm/utils/TableGen/DAGISelMatcherGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
18 changes: 7 additions & 11 deletions llvm/utils/TableGen/FastISelEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -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;
}
Expand Down
11 changes: 6 additions & 5 deletions llvm/utils/TableGen/GlobalISelEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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");
}
Expand Down
Loading