Skip to content

Commit 2aa2fde

Browse files
committed
[NFC][BasicBlockUtils] Refactor GetIfCondition() to return the branch, not it's condition
Otherwise e.g. the FoldTwoEntryPHINode() has to do a lot of legwork to re-deduce what is the dominant block (i.e. for which block is this branch the terminator).
1 parent c45e17f commit 2aa2fde

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,15 +487,15 @@ void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
487487
MDNode *BranchWeights = nullptr);
488488

489489
/// Check whether BB is the merge point of a if-region.
490-
/// If so, return the boolean condition that determines which entry into
490+
/// If so, return the branch instruction that determines which entry into
491491
/// BB will be taken. Also, return by references the block that will be
492492
/// entered from if the condition is true, and the block that will be
493493
/// entered if the condition is false.
494494
///
495495
/// This does no checking to see if the true/false blocks have large or unsavory
496496
/// instructions in them.
497-
Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
498-
BasicBlock *&IfFalse);
497+
BranchInst *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
498+
BasicBlock *&IfFalse);
499499

500500
// Split critical edges where the source of the edge is an indirectbr
501501
// instruction. This isn't always possible, but we can handle some easy cases.

llvm/lib/Transforms/Utils/BasicBlockUtils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,8 +1456,8 @@ void llvm::SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
14561456
ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
14571457
}
14581458

1459-
Value *llvm::GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
1460-
BasicBlock *&IfFalse) {
1459+
BranchInst *llvm::GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
1460+
BasicBlock *&IfFalse) {
14611461
PHINode *SomePHI = dyn_cast<PHINode>(BB->begin());
14621462
BasicBlock *Pred1 = nullptr;
14631463
BasicBlock *Pred2 = nullptr;
@@ -1523,7 +1523,7 @@ Value *llvm::GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
15231523
return nullptr;
15241524
}
15251525

1526-
return Pred1Br->getCondition();
1526+
return Pred1Br;
15271527
}
15281528

15291529
// Ok, if we got here, both predecessors end with an unconditional branch to
@@ -1545,7 +1545,7 @@ Value *llvm::GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
15451545
IfTrue = Pred2;
15461546
IfFalse = Pred1;
15471547
}
1548-
return BI->getCondition();
1548+
return BI;
15491549
}
15501550

15511551
// After creating a control flow hub, the operands of PHINodes in an outgoing

llvm/lib/Transforms/Utils/FlattenCFG.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,10 @@ bool FlattenCFGOpt::CompareIfRegionBlock(BasicBlock *Block1, BasicBlock *Block2,
411411
/// approach goes for the opposite case.
412412
bool FlattenCFGOpt::MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder) {
413413
BasicBlock *IfTrue2, *IfFalse2;
414-
Value *IfCond2 = GetIfCondition(BB, IfTrue2, IfFalse2);
415-
Instruction *CInst2 = dyn_cast_or_null<Instruction>(IfCond2);
414+
BranchInst *DomBI2 = GetIfCondition(BB, IfTrue2, IfFalse2);
415+
if (!DomBI2)
416+
return false;
417+
Instruction *CInst2 = dyn_cast<Instruction>(DomBI2->getCondition());
416418
if (!CInst2)
417419
return false;
418420

@@ -421,8 +423,10 @@ bool FlattenCFGOpt::MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder) {
421423
return false;
422424

423425
BasicBlock *IfTrue1, *IfFalse1;
424-
Value *IfCond1 = GetIfCondition(SecondEntryBlock, IfTrue1, IfFalse1);
425-
Instruction *CInst1 = dyn_cast_or_null<Instruction>(IfCond1);
426+
BranchInst *DomBI1 = GetIfCondition(SecondEntryBlock, IfTrue1, IfFalse1);
427+
if (!DomBI1)
428+
return false;
429+
Instruction *CInst1 = dyn_cast<Instruction>(DomBI1->getCondition());
426430
if (!CInst1)
427431
return false;
428432

@@ -479,7 +483,7 @@ bool FlattenCFGOpt::MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder) {
479483
FirstEntryBlock->getInstList()
480484
.splice(FirstEntryBlock->end(), SecondEntryBlock->getInstList());
481485
BranchInst *PBI = cast<BranchInst>(FirstEntryBlock->getTerminator());
482-
assert(PBI->getCondition() == IfCond2);
486+
assert(PBI->getCondition() == CInst2);
483487
BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
484488
BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
485489
Builder.SetInsertPoint(PBI);
@@ -494,7 +498,7 @@ bool FlattenCFGOpt::MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder) {
494498
PBI->swapSuccessors();
495499
}
496500
Value *NC = Builder.CreateBinOp(CombineOp, CInst1, CInst2);
497-
PBI->replaceUsesOfWith(IfCond2, NC);
501+
PBI->replaceUsesOfWith(CInst2, NC);
498502
Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
499503

500504
// Handle PHI node to replace its predecessors to FirstEntryBlock.

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2708,10 +2708,12 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
27082708
BasicBlock *BB = PN->getParent();
27092709

27102710
BasicBlock *IfTrue, *IfFalse;
2711-
Value *IfCond = GetIfCondition(BB, IfTrue, IfFalse);
2712-
if (!IfCond ||
2713-
// Don't bother if the branch will be constant folded trivially.
2714-
isa<ConstantInt>(IfCond))
2711+
BranchInst *DomBI = GetIfCondition(BB, IfTrue, IfFalse);
2712+
if (!DomBI)
2713+
return false;
2714+
Value *IfCond = DomBI->getCondition();
2715+
// Don't bother if the branch will be constant folded trivially.
2716+
if (isa<ConstantInt>(IfCond))
27152717
return false;
27162718

27172719
// Don't try to fold an unreachable block. For example, the phi node itself

0 commit comments

Comments
 (0)