@@ -162,10 +162,10 @@ class DCE : public SILFunctionTransform {
162
162
void addReverseDependency (SILInstruction *From, SILInstruction *To);
163
163
bool removeDead (SILFunction &F);
164
164
165
- void computeLevelNumbers (PostDomTreeNode *Node, unsigned Level );
165
+ void computeLevelNumbers (PostDomTreeNode *root );
166
166
bool hasInfiniteLoops (SILFunction &F);
167
167
void computePredecessorDependence (SILFunction &F);
168
- unsigned computeMinPredecessorLevels (PostDomTreeNode *Node );
168
+ void computeMinPredecessorLevels (PostDomTreeNode *root );
169
169
void insertControllingInfo (SILBasicBlock *Block, unsigned Level);
170
170
171
171
void markValueLive (SILNode *V);
@@ -175,7 +175,7 @@ class DCE : public SILFunctionTransform {
175
175
void propagateLiveBlockArgument (SILArgument *Arg);
176
176
void propagateLiveness (SILInstruction *I);
177
177
void collectControllingBlocksInTree (ControllingInfo &QueryInfo,
178
- PostDomTreeNode *Node ,
178
+ PostDomTreeNode *root ,
179
179
llvm::SmallPtrSetImpl<SILBasicBlock *> &Controlling);
180
180
void collectControllingBlocks (SILBasicBlock *Block,
181
181
llvm::SmallPtrSetImpl<SILBasicBlock *> &);
@@ -537,7 +537,7 @@ bool DCE::removeDead(SILFunction &F) {
537
537
// Returns true upon success, false if nodes that are not present in the
538
538
// post-dominator tree are detected.
539
539
bool DCE::precomputeControlInfo (SILFunction &F) {
540
- computeLevelNumbers (PDT->getRootNode (), 0 );
540
+ computeLevelNumbers (PDT->getRootNode ());
541
541
if (hasInfiniteLoops (F))
542
542
return false ;
543
543
computePredecessorDependence (F);
@@ -557,12 +557,22 @@ void DCE::insertControllingInfo(SILBasicBlock *Block, unsigned Level) {
557
557
ControllingInfoMap[Block] = Info;
558
558
}
559
559
560
- // Assign a level number to each node in the post-dominator tree, and
561
- void DCE::computeLevelNumbers (PostDomTreeNode *Node, unsigned Level) {
562
- insertControllingInfo (Node->getBlock (), Level);
563
-
564
- for (auto Child = Node->begin (), End = Node->end (); Child != End; ++Child)
565
- computeLevelNumbers (*Child, Level + 1 );
560
+ // Assign a level number to each node in the post-dominator tree.
561
+ void DCE::computeLevelNumbers (PostDomTreeNode *root) {
562
+ llvm::SmallVector<std::pair<PostDomTreeNode *, unsigned >, 32 > workList;
563
+ workList.push_back ({root, 0 });
564
+
565
+ while (!workList.empty ()) {
566
+ auto entry = workList.pop_back_val ();
567
+ PostDomTreeNode *node = entry.first ;
568
+ unsigned level = entry.second ;
569
+
570
+ insertControllingInfo (node->getBlock (), level);
571
+
572
+ for (PostDomTreeNode *child : *node) {
573
+ workList.push_back ({child, level + 1 });
574
+ }
575
+ }
566
576
}
567
577
568
578
// Structurally infinite loops like:
@@ -605,51 +615,65 @@ void DCE::computePredecessorDependence(SILFunction &F) {
605
615
}
606
616
}
607
617
608
- // Return the minimum post-dominator tree level of any of the
609
- // direct controlling predecessors of this node or any child.
610
- unsigned DCE::computeMinPredecessorLevels (PostDomTreeNode *Node) {
611
- unsigned Min = -1 ;
612
-
613
- auto *Block = Node->getBlock ();
614
-
615
- assert (ControllingInfoMap.find (Block) != ControllingInfoMap.end () &&
616
- " Expected to have map entry for node!" );
618
+ // Assign the minimum post-dominator tree level to each node in the tree.
619
+ void DCE::computeMinPredecessorLevels (PostDomTreeNode *root) {
620
+ llvm::SmallVector<PostDomTreeNode *, 32 > postDomOrder;
621
+ postDomOrder.reserve (ControllingInfoMap.size ());
622
+ postDomOrder.push_back (root);
617
623
618
- auto &MapElement = ControllingInfoMap[Block];
619
- for ( auto &PredInfo : MapElement. ControllingPreds )
620
- Min = std::min (Min, PredInfo. second );
621
-
622
- for ( auto Child = Node-> begin (), End = Node-> end (); Child != End; ++Child)
623
- Min = std::min (Min, computeMinPredecessorLevels (*Child));
624
+ for ( unsigned idx = 0 ; idx < postDomOrder. size (); ++idx) {
625
+ PostDomTreeNode *node = postDomOrder[idx];
626
+ for (PostDomTreeNode *child : *node) {
627
+ postDomOrder. push_back (child);
628
+ }
629
+ }
624
630
625
- MapElement.MinTreePredLevel = Min;
631
+ for (PostDomTreeNode *node : llvm::reverse (postDomOrder)) {
632
+ SILBasicBlock *block = node->getBlock ();
633
+ assert (ControllingInfoMap.find (block) != ControllingInfoMap.end () &&
634
+ " Expected to have map entry for node!" );
626
635
627
- return Min;
636
+ ControllingInfo &nodeInfo = ControllingInfoMap[block];
637
+ for (auto &pred : nodeInfo.ControllingPreds ) {
638
+ nodeInfo.MinTreePredLevel = std::min (nodeInfo.MinTreePredLevel , pred.second );
639
+ }
640
+ if (PostDomTreeNode *parentNode = node->getIDom ()) {
641
+ ControllingInfo &parentInfo = ControllingInfoMap[parentNode->getBlock ()];
642
+ parentInfo.MinTreePredLevel = std::min (parentInfo.MinTreePredLevel , nodeInfo.MinTreePredLevel );
643
+ }
644
+ }
628
645
}
629
646
630
647
void DCE::collectControllingBlocksInTree (ControllingInfo &QueryInfo,
631
- PostDomTreeNode *Node ,
648
+ PostDomTreeNode *root ,
632
649
llvm::SmallPtrSetImpl<SILBasicBlock *> &Controlling) {
633
- auto *Block = Node->getBlock ();
634
-
635
- assert (ControllingInfoMap.find (Block) != ControllingInfoMap.end () &&
636
- " Expected to have map entry for node!" );
637
-
638
- auto &MapEntry = ControllingInfoMap[Block];
639
- if (MapEntry.MinTreePredLevel > QueryInfo.Level )
640
- return ;
641
-
642
- for (auto &PredInfo : MapEntry.ControllingPreds )
643
- if (PredInfo.second <= QueryInfo.Level ) {
644
- assert (PDT->properlyDominates (
645
- PDT->getNode (PredInfo.first )->getIDom ()->getBlock (),
646
- QueryInfo.Block ) &&
647
- " Expected predecessor's post-dominator to post-dominate node." );
648
- Controlling.insert (PredInfo.first );
650
+ llvm::SmallVector<PostDomTreeNode *, 32 > workList;
651
+ workList.push_back (root);
652
+
653
+ while (!workList.empty ()) {
654
+ PostDomTreeNode *node = workList.pop_back_val ();
655
+ SILBasicBlock *block = node->getBlock ();
656
+
657
+ assert (ControllingInfoMap.find (block) != ControllingInfoMap.end () &&
658
+ " Expected to have map entry for node!" );
659
+
660
+ auto &nodeInfo = ControllingInfoMap[block];
661
+ if (nodeInfo.MinTreePredLevel > QueryInfo.Level )
662
+ continue ;
663
+
664
+ for (auto &PredInfo : nodeInfo.ControllingPreds ) {
665
+ if (PredInfo.second <= QueryInfo.Level ) {
666
+ assert (PDT->properlyDominates (
667
+ PDT->getNode (PredInfo.first )->getIDom ()->getBlock (),
668
+ QueryInfo.Block ) &&
669
+ " Expected predecessor's post-dominator to post-dominate node." );
670
+ Controlling.insert (PredInfo.first );
671
+ }
649
672
}
650
-
651
- for (auto Child = Node->begin (), End = Node->end (); Child != End; ++Child)
652
- collectControllingBlocksInTree (QueryInfo, (*Child), Controlling);
673
+ for (PostDomTreeNode *child : *node) {
674
+ workList.push_back (child);
675
+ }
676
+ }
653
677
}
654
678
655
679
// Walk the post-dominator tree from the query block down, building
0 commit comments