Skip to content

Commit

Permalink
[SimplifyCFG] Remove one-use limitation in FoldCondBranchOnPHI()
Browse files Browse the repository at this point in the history
BlockIsSimpleEnoughToThreadThrough() already checks that the phi
(and all other instructions) are not used outside the block, so
this one-use check is not necessary for legality. I also don't
see any reason why it would be necessary for profitability (in
fact, those extra uses will be replaced with constants, which
should be generally profitable).
  • Loading branch information
nikic committed Apr 20, 2022
1 parent 53d8858 commit d727505
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
6 changes: 3 additions & 3 deletions llvm/lib/Transforms/Utils/SimplifyCFG.cpp
Expand Up @@ -2984,9 +2984,7 @@ static Optional<bool> FoldCondBranchOnPHIImpl(BranchInst *BI,
AssumptionCache *AC) {
BasicBlock *BB = BI->getParent();
PHINode *PN = dyn_cast<PHINode>(BI->getCondition());
// NOTE: we currently cannot transform this case if the PHI node is used
// outside of the block.
if (!PN || PN->getParent() != BB || !PN->hasOneUse())
if (!PN || PN->getParent() != BB)
return false;

// Degenerate case of a single entry PHI.
Expand All @@ -2996,6 +2994,8 @@ static Optional<bool> FoldCondBranchOnPHIImpl(BranchInst *BI,
}

// Now we know that this block has multiple preds and two succs.
// Check that the block is small enough and values defined in the block are
// not used outside of it.
if (!BlockIsSimpleEnoughToThreadThrough(BB))
return false;

Expand Down
14 changes: 4 additions & 10 deletions llvm/test/Transforms/SimplifyCFG/jump-threading.ll
Expand Up @@ -50,18 +50,12 @@ define void @test_phi_extra_use(i1 %c) {
; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
; CHECK: if:
; CHECK-NEXT: call void @foo()
; CHECK-NEXT: br label [[JOIN:%.*]]
; CHECK: else:
; CHECK-NEXT: call void @bar()
; CHECK-NEXT: br label [[JOIN]]
; CHECK: join:
; CHECK-NEXT: [[C2:%.*]] = phi i1 [ true, [[IF]] ], [ false, [[ELSE]] ]
; CHECK-NEXT: call void @use.i1(i1 [[C2]])
; CHECK-NEXT: br i1 [[C2]], label [[IF2:%.*]], label [[ELSE2:%.*]]
; CHECK: if2:
; CHECK-NEXT: call void @use.i1(i1 true)
; CHECK-NEXT: call void @foo()
; CHECK-NEXT: br label [[JOIN2:%.*]]
; CHECK: else2:
; CHECK: else:
; CHECK-NEXT: call void @bar()
; CHECK-NEXT: call void @use.i1(i1 false)
; CHECK-NEXT: call void @bar()
; CHECK-NEXT: br label [[JOIN2]]
; CHECK: join2:
Expand Down

0 comments on commit d727505

Please sign in to comment.