Skip to content

Commit 418dba0

Browse files
committed
[NFC][SimplifyCFG] FoldTwoEntryPHINode(): make better use of GetIfCondition() returning dom block
1 parent 2aa2fde commit 418dba0

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2794,13 +2794,12 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
27942794
// in the predecessor blocks can be promoted as well. If not, we won't be able
27952795
// to get rid of the control flow, so it's not worth promoting to select
27962796
// instructions.
2797-
BasicBlock *DomBlock = nullptr;
2797+
BasicBlock *DomBlock = DomBI->getParent();
27982798
BasicBlock *IfBlock1 = PN->getIncomingBlock(0);
27992799
BasicBlock *IfBlock2 = PN->getIncomingBlock(1);
28002800
if (cast<BranchInst>(IfBlock1->getTerminator())->isConditional()) {
28012801
IfBlock1 = nullptr;
28022802
} else {
2803-
DomBlock = *pred_begin(IfBlock1);
28042803
for (BasicBlock::iterator I = IfBlock1->begin(); !I->isTerminator(); ++I)
28052804
if (!AggressiveInsts.count(&*I) && !isa<DbgInfoIntrinsic>(I) &&
28062805
!isa<PseudoProbeInst>(I)) {
@@ -2814,7 +2813,6 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
28142813
if (cast<BranchInst>(IfBlock2->getTerminator())->isConditional()) {
28152814
IfBlock2 = nullptr;
28162815
} else {
2817-
DomBlock = *pred_begin(IfBlock2);
28182816
for (BasicBlock::iterator I = IfBlock2->begin(); !I->isTerminator(); ++I)
28192817
if (!AggressiveInsts.count(&*I) && !isa<DbgInfoIntrinsic>(I) &&
28202818
!isa<PseudoProbeInst>(I)) {
@@ -2824,7 +2822,6 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
28242822
return Changed;
28252823
}
28262824
}
2827-
assert(DomBlock && "Failed to find root DomBlock");
28282825

28292826
// If either of the blocks has it's address taken, we can't do this fold.
28302827
if ((IfBlock1 && IfBlock1->hasAddressTaken()) ||
@@ -2837,16 +2834,15 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
28372834

28382835
// If we can still promote the PHI nodes after this gauntlet of tests,
28392836
// do all of the PHI's now.
2840-
Instruction *InsertPt = DomBlock->getTerminator();
2841-
IRBuilder<NoFolder> Builder(InsertPt);
28422837

28432838
// Move all 'aggressive' instructions, which are defined in the
28442839
// conditional parts of the if's up to the dominating block.
28452840
if (IfBlock1)
2846-
hoistAllInstructionsInto(DomBlock, InsertPt, IfBlock1);
2841+
hoistAllInstructionsInto(DomBlock, DomBI, IfBlock1);
28472842
if (IfBlock2)
2848-
hoistAllInstructionsInto(DomBlock, InsertPt, IfBlock2);
2843+
hoistAllInstructionsInto(DomBlock, DomBI, IfBlock2);
28492844

2845+
IRBuilder<NoFolder> Builder(DomBI);
28502846
// Propagate fast-math-flags from phi nodes to replacement selects.
28512847
IRBuilder<>::FastMathFlagGuard FMFGuard(Builder);
28522848
while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
@@ -2857,7 +2853,7 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
28572853
Value *TrueVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfFalse);
28582854
Value *FalseVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfTrue);
28592855

2860-
Value *Sel = Builder.CreateSelect(IfCond, TrueVal, FalseVal, "", InsertPt);
2856+
Value *Sel = Builder.CreateSelect(IfCond, TrueVal, FalseVal, "", DomBI);
28612857
PN->replaceAllUsesWith(Sel);
28622858
Sel->takeName(PN);
28632859
PN->eraseFromParent();
@@ -2866,8 +2862,6 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
28662862
// At this point, IfBlock1 and IfBlock2 are both empty, so our if statement
28672863
// has been flattened. Change DomBlock to jump directly to our new block to
28682864
// avoid other simplifycfg's kicking in on the diamond.
2869-
Instruction *OldTI = DomBlock->getTerminator();
2870-
Builder.SetInsertPoint(OldTI);
28712865
Builder.CreateBr(BB);
28722866

28732867
SmallVector<DominatorTree::UpdateType, 3> Updates;
@@ -2877,7 +2871,7 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
28772871
Updates.push_back({DominatorTree::Delete, DomBlock, Successor});
28782872
}
28792873

2880-
OldTI->eraseFromParent();
2874+
DomBI->eraseFromParent();
28812875
if (DTU)
28822876
DTU->applyUpdates(Updates);
28832877

0 commit comments

Comments
 (0)