Skip to content

Commit 943f851

Browse files
committed
[NFC][SimplifyCFG] Make 'conditional block' handling more straight-forward
This will simplify making use of profile weights to not perform the speculation when obviously unprofitable.
1 parent 418dba0 commit 943f851

File tree

1 file changed

+17
-30
lines changed

1 file changed

+17
-30
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2716,6 +2716,14 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
27162716
if (isa<ConstantInt>(IfCond))
27172717
return false;
27182718

2719+
BasicBlock *DomBlock = DomBI->getParent();
2720+
SmallVector<BasicBlock *, 2> IfBlocks;
2721+
llvm::copy_if(
2722+
PN->blocks(), std::back_inserter(IfBlocks), [](BasicBlock *IfBlock) {
2723+
return cast<BranchInst>(IfBlock->getTerminator())->isUnconditional();
2724+
});
2725+
assert(!IfBlocks.empty() && "Will have at least one block to speculate.");
2726+
27192727
// Don't try to fold an unreachable block. For example, the phi node itself
27202728
// can't be the candidate if-condition for a select that we want to form.
27212729
if (auto *IfCondPhiInst = dyn_cast<PHINode>(IfCond))
@@ -2794,38 +2802,19 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
27942802
// in the predecessor blocks can be promoted as well. If not, we won't be able
27952803
// to get rid of the control flow, so it's not worth promoting to select
27962804
// instructions.
2797-
BasicBlock *DomBlock = DomBI->getParent();
2798-
BasicBlock *IfBlock1 = PN->getIncomingBlock(0);
2799-
BasicBlock *IfBlock2 = PN->getIncomingBlock(1);
2800-
if (cast<BranchInst>(IfBlock1->getTerminator())->isConditional()) {
2801-
IfBlock1 = nullptr;
2802-
} else {
2803-
for (BasicBlock::iterator I = IfBlock1->begin(); !I->isTerminator(); ++I)
2804-
if (!AggressiveInsts.count(&*I) && !isa<DbgInfoIntrinsic>(I) &&
2805-
!isa<PseudoProbeInst>(I)) {
2806-
// This is not an aggressive instruction that we can promote.
2807-
// Because of this, we won't be able to get rid of the control flow, so
2808-
// the xform is not worth it.
2809-
return Changed;
2810-
}
2811-
}
2812-
2813-
if (cast<BranchInst>(IfBlock2->getTerminator())->isConditional()) {
2814-
IfBlock2 = nullptr;
2815-
} else {
2816-
for (BasicBlock::iterator I = IfBlock2->begin(); !I->isTerminator(); ++I)
2805+
for (BasicBlock *IfBlock : IfBlocks)
2806+
for (BasicBlock::iterator I = IfBlock->begin(); !I->isTerminator(); ++I)
28172807
if (!AggressiveInsts.count(&*I) && !isa<DbgInfoIntrinsic>(I) &&
28182808
!isa<PseudoProbeInst>(I)) {
28192809
// This is not an aggressive instruction that we can promote.
28202810
// Because of this, we won't be able to get rid of the control flow, so
28212811
// the xform is not worth it.
28222812
return Changed;
28232813
}
2824-
}
28252814

28262815
// If either of the blocks has it's address taken, we can't do this fold.
2827-
if ((IfBlock1 && IfBlock1->hasAddressTaken()) ||
2828-
(IfBlock2 && IfBlock2->hasAddressTaken()))
2816+
if (any_of(IfBlocks,
2817+
[](BasicBlock *IfBlock) { return IfBlock->hasAddressTaken(); }))
28292818
return Changed;
28302819

28312820
LLVM_DEBUG(dbgs() << "FOUND IF CONDITION! " << *IfCond
@@ -2837,10 +2826,8 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
28372826

28382827
// Move all 'aggressive' instructions, which are defined in the
28392828
// conditional parts of the if's up to the dominating block.
2840-
if (IfBlock1)
2841-
hoistAllInstructionsInto(DomBlock, DomBI, IfBlock1);
2842-
if (IfBlock2)
2843-
hoistAllInstructionsInto(DomBlock, DomBI, IfBlock2);
2829+
for (BasicBlock *IfBlock : IfBlocks)
2830+
hoistAllInstructionsInto(DomBlock, DomBI, IfBlock);
28442831

28452832
IRBuilder<NoFolder> Builder(DomBI);
28462833
// Propagate fast-math-flags from phi nodes to replacement selects.
@@ -2850,16 +2837,16 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
28502837
Builder.setFastMathFlags(PN->getFastMathFlags());
28512838

28522839
// Change the PHI node into a select instruction.
2853-
Value *TrueVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfFalse);
2854-
Value *FalseVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfTrue);
2840+
Value *TrueVal = PN->getIncomingValueForBlock(IfTrue);
2841+
Value *FalseVal = PN->getIncomingValueForBlock(IfFalse);
28552842

28562843
Value *Sel = Builder.CreateSelect(IfCond, TrueVal, FalseVal, "", DomBI);
28572844
PN->replaceAllUsesWith(Sel);
28582845
Sel->takeName(PN);
28592846
PN->eraseFromParent();
28602847
}
28612848

2862-
// At this point, IfBlock1 and IfBlock2 are both empty, so our if statement
2849+
// At this point, all IfBlocks are empty, so our if statement
28632850
// has been flattened. Change DomBlock to jump directly to our new block to
28642851
// avoid other simplifycfg's kicking in on the diamond.
28652852
Builder.CreateBr(BB);

0 commit comments

Comments
 (0)