Skip to content

Commit 76c5cb0

Browse files
committed
DomTree: Remove getChildren() accessor
Summary: Avoid exposing details about how children are stored. This will enable subsequent type-erasure changes. New methods are introduced to cover common access patterns. Change-Id: Idb5f4b1b9c84e4cc71ddb39bb52a388682f5674f Reviewers: arsenm, RKSimon, mehdi_amini, courbet Subscribers: qcolombet, sdardis, wdng, hiraditya, jrtc27, zzheng, atanasyan, asbirlea, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D83083
1 parent 16d83c3 commit 76c5cb0

15 files changed

+46
-49
lines changed

llvm/include/llvm/Support/GenericDomTree.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,25 @@ template <class NodeT> class DomTreeNodeBase {
7777
const_iterator begin() const { return Children.begin(); }
7878
const_iterator end() const { return Children.end(); }
7979

80+
DomTreeNodeBase *const &back() const { return Children.back(); }
81+
DomTreeNodeBase *&back() { return Children.back(); }
82+
83+
iterator_range<iterator> children() { return make_range(begin(), end()); }
84+
iterator_range<const_iterator> children() const {
85+
return make_range(begin(), end());
86+
}
87+
8088
NodeT *getBlock() const { return TheBB; }
8189
DomTreeNodeBase *getIDom() const { return IDom; }
8290
unsigned getLevel() const { return Level; }
8391

84-
const std::vector<DomTreeNodeBase *> &getChildren() const { return Children; }
85-
8692
std::unique_ptr<DomTreeNodeBase> addChild(
8793
std::unique_ptr<DomTreeNodeBase> C) {
8894
Children.push_back(C.get());
8995
return C;
9096
}
9197

98+
bool isLeaf() const { return Children.empty(); }
9299
size_t getNumChildren() const { return Children.size(); }
93100

94101
void clearAllChildren() { Children.clear(); }
@@ -619,7 +626,7 @@ class DominatorTreeBase {
619626
void eraseNode(NodeT *BB) {
620627
DomTreeNodeBase<NodeT> *Node = getNode(BB);
621628
assert(Node && "Removing node that isn't in dominator tree.");
622-
assert(Node->getChildren().empty() && "Node is not a leaf node.");
629+
assert(Node->isLeaf() && "Node is not a leaf node.");
623630

624631
DFSInfoValid = false;
625632

llvm/include/llvm/Support/GenericDomTreeConstruction.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ struct SemiNCAInfo {
13961396
const TreeNodePtr Node = NodeToTN.second.get();
13971397

13981398
// Handle tree leaves.
1399-
if (Node->getChildren().empty()) {
1399+
if (Node->isLeaf()) {
14001400
if (Node->getDFSNumIn() + 1 != Node->getDFSNumOut()) {
14011401
errs() << "Tree leaf should have DFSOut = DFSIn + 1:\n\t";
14021402
PrintNodeAndDFSNums(Node);
@@ -1508,7 +1508,8 @@ struct SemiNCAInfo {
15081508
for (auto &NodeToTN : DT.DomTreeNodes) {
15091509
const TreeNodePtr TN = NodeToTN.second.get();
15101510
const NodePtr BB = TN->getBlock();
1511-
if (!BB || TN->getChildren().empty()) continue;
1511+
if (!BB || TN->isLeaf())
1512+
continue;
15121513

15131514
LLVM_DEBUG(dbgs() << "Verifying parent property of node "
15141515
<< BlockNamePrinter(TN) << "\n");
@@ -1517,7 +1518,7 @@ struct SemiNCAInfo {
15171518
return From != BB && To != BB;
15181519
});
15191520

1520-
for (TreeNodePtr Child : TN->getChildren())
1521+
for (TreeNodePtr Child : TN->children())
15211522
if (NodeToInfo.count(Child->getBlock()) != 0) {
15221523
errs() << "Child " << BlockNamePrinter(Child)
15231524
<< " reachable after its parent " << BlockNamePrinter(BB)
@@ -1541,17 +1542,17 @@ struct SemiNCAInfo {
15411542
for (auto &NodeToTN : DT.DomTreeNodes) {
15421543
const TreeNodePtr TN = NodeToTN.second.get();
15431544
const NodePtr BB = TN->getBlock();
1544-
if (!BB || TN->getChildren().empty()) continue;
1545+
if (!BB || TN->isLeaf())
1546+
continue;
15451547

1546-
const auto &Siblings = TN->getChildren();
1547-
for (const TreeNodePtr N : Siblings) {
1548+
for (const TreeNodePtr N : TN->children()) {
15481549
clear();
15491550
NodePtr BBN = N->getBlock();
15501551
doFullDFSWalk(DT, [BBN](NodePtr From, NodePtr To) {
15511552
return From != BBN && To != BBN;
15521553
});
15531554

1554-
for (const TreeNodePtr S : Siblings) {
1555+
for (const TreeNodePtr S : TN->children()) {
15551556
if (S == N) continue;
15561557

15571558
if (NodeToInfo.count(S->getBlock()) == 0) {

llvm/lib/CodeGen/EarlyIfConversion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ void updateDomTree(MachineDominatorTree *DomTree, const SSAIfConv &IfConv,
759759
assert(Node != HeadNode && "Cannot erase the head node");
760760
while (Node->getNumChildren()) {
761761
assert(Node->getBlock() == IfConv.Tail && "Unexpected children");
762-
DomTree->changeImmediateDominator(Node->getChildren().back(), HeadNode);
762+
DomTree->changeImmediateDominator(Node->back(), HeadNode);
763763
}
764764
DomTree->eraseNode(B);
765765
}

llvm/lib/CodeGen/InlineSpiller.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,10 +1306,7 @@ void HoistSpillHelper::getVisitOrders(
13061306
Orders.push_back(MDT.getBase().getNode(Root));
13071307
do {
13081308
MachineDomTreeNode *Node = Orders[idx++];
1309-
const std::vector<MachineDomTreeNode *> &Children = Node->getChildren();
1310-
unsigned NumChildren = Children.size();
1311-
for (unsigned i = 0; i != NumChildren; ++i) {
1312-
MachineDomTreeNode *Child = Children[i];
1309+
for (MachineDomTreeNode *Child : Node->children()) {
13131310
if (WorkSet.count(Child))
13141311
Orders.push_back(Child);
13151312
}
@@ -1377,10 +1374,7 @@ void HoistSpillHelper::runHoistSpills(
13771374

13781375
// Collect spills in subtree of current node (*RIt) to
13791376
// SpillsInSubTreeMap[*RIt].first.
1380-
const std::vector<MachineDomTreeNode *> &Children = (*RIt)->getChildren();
1381-
unsigned NumChildren = Children.size();
1382-
for (unsigned i = 0; i != NumChildren; ++i) {
1383-
MachineDomTreeNode *Child = Children[i];
1377+
for (MachineDomTreeNode *Child : (*RIt)->children()) {
13841378
if (SpillsInSubTreeMap.find(Child) == SpillsInSubTreeMap.end())
13851379
continue;
13861380
// The stmt "SpillsInSubTree = SpillsInSubTreeMap[*RIt].first" below

llvm/lib/CodeGen/MachineCSE.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -747,9 +747,8 @@ bool MachineCSE::PerformCSE(MachineDomTreeNode *Node) {
747747
do {
748748
Node = WorkList.pop_back_val();
749749
Scopes.push_back(Node);
750-
const std::vector<MachineDomTreeNode*> &Children = Node->getChildren();
751-
OpenChildren[Node] = Children.size();
752-
for (MachineDomTreeNode *Child : Children)
750+
OpenChildren[Node] = Node->getNumChildren();
751+
for (MachineDomTreeNode *Child : Node->children())
753752
WorkList.push_back(Child);
754753
} while (!WorkList.empty());
755754

@@ -862,8 +861,7 @@ bool MachineCSE::PerformSimplePRE(MachineDominatorTree *DT) {
862861
BBs.push_back(DT->getRootNode());
863862
do {
864863
auto Node = BBs.pop_back_val();
865-
const std::vector<MachineDomTreeNode *> &Children = Node->getChildren();
866-
for (MachineDomTreeNode *Child : Children)
864+
for (MachineDomTreeNode *Child : Node->children())
867865
BBs.push_back(Child);
868866

869867
MachineBasicBlock *MBB = Node->getBlock();

llvm/lib/CodeGen/MachineLICM.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -737,8 +737,7 @@ void MachineLICMBase::HoistOutOfLoop(MachineDomTreeNode *HeaderN) {
737737
continue;
738738

739739
Scopes.push_back(Node);
740-
const std::vector<MachineDomTreeNode*> &Children = Node->getChildren();
741-
unsigned NumChildren = Children.size();
740+
unsigned NumChildren = Node->getNumChildren();
742741

743742
// Don't hoist things out of a large switch statement. This often causes
744743
// code to be hoisted that wasn't going to be executed, and increases
@@ -747,13 +746,14 @@ void MachineLICMBase::HoistOutOfLoop(MachineDomTreeNode *HeaderN) {
747746
NumChildren = 0;
748747

749748
OpenChildren[Node] = NumChildren;
750-
// Add children in reverse order as then the next popped worklist node is
751-
// the first child of this node. This means we ultimately traverse the
752-
// DOM tree in exactly the same order as if we'd recursed.
753-
for (int i = (int)NumChildren-1; i >= 0; --i) {
754-
MachineDomTreeNode *Child = Children[i];
755-
ParentMap[Child] = Node;
756-
WorkList.push_back(Child);
749+
if (NumChildren) {
750+
// Add children in reverse order as then the next popped worklist node is
751+
// the first child of this node. This means we ultimately traverse the
752+
// DOM tree in exactly the same order as if we'd recursed.
753+
for (MachineDomTreeNode *Child : reverse(Node->children())) {
754+
ParentMap[Child] = Node;
755+
WorkList.push_back(Child);
756+
}
757757
}
758758
}
759759

llvm/lib/CodeGen/MachineSink.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,14 +623,13 @@ MachineSinking::GetAllSortedSuccessors(MachineInstr &MI, MachineBasicBlock *MBB,
623623
// if () {} else {}
624624
// use x
625625
//
626-
const std::vector<MachineDomTreeNode *> &Children =
627-
DT->getNode(MBB)->getChildren();
628-
for (const auto &DTChild : Children)
626+
for (MachineDomTreeNode *DTChild : DT->getNode(MBB)->children()) {
629627
// DomTree children of MBB that have MBB as immediate dominator are added.
630628
if (DTChild->getIDom()->getBlock() == MI.getParent() &&
631629
// Skip MBBs already added to the AllSuccs vector above.
632630
!MBB->isSuccessor(DTChild->getBlock()))
633631
AllSuccs.push_back(DTChild->getBlock());
632+
}
634633

635634
// Sort Successors according to their loop depth or block frequency info.
636635
llvm::stable_sort(

llvm/lib/Target/AArch64/AArch64ConditionalCompares.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ void AArch64ConditionalCompares::updateDomTree(
828828
assert(Node != HeadNode && "Cannot erase the head node");
829829
assert(Node->getIDom() == HeadNode && "CmpBB should be dominated by Head");
830830
while (Node->getNumChildren())
831-
DomTree->changeImmediateDominator(Node->getChildren().back(), HeadNode);
831+
DomTree->changeImmediateDominator(Node->back(), HeadNode);
832832
DomTree->eraseNode(RemovedMBB);
833833
}
834834
}

llvm/lib/Target/Mips/MipsOptimizePICCall.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,7 @@ bool OptimizePICCall::runOnMachineFunction(MachineFunction &F) {
218218
MBBI.preVisit(ScopedHT);
219219
Changed |= visitNode(MBBI);
220220
const MachineDomTreeNode *Node = MBBI.getNode();
221-
const std::vector<MachineDomTreeNode *> &Children = Node->getChildren();
222-
WorkList.append(Children.begin(), Children.end());
221+
WorkList.append(Node->begin(), Node->end());
223222
}
224223

225224
return Changed;

llvm/lib/Transforms/Scalar/ConstantHoisting.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ static void findBestInsertionSet(DominatorTree &DT, BlockFrequencyInfo &BFI,
250250
Orders.push_back(Entry);
251251
while (Idx != Orders.size()) {
252252
BasicBlock *Node = Orders[Idx++];
253-
for (auto ChildDomNode : DT.getNode(Node)->getChildren()) {
253+
for (auto ChildDomNode : DT.getNode(Node)->children()) {
254254
if (Candidates.count(ChildDomNode->getBlock()))
255255
Orders.push_back(ChildDomNode->getBlock());
256256
}

0 commit comments

Comments
 (0)