Skip to content

Commit d727505

Browse files
committed
[SimplifyCFG] Remove one-use limitation in FoldCondBranchOnPHI()
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).
1 parent 53d8858 commit d727505

File tree

2 files changed

+7
-13
lines changed

2 files changed

+7
-13
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2984,9 +2984,7 @@ static Optional<bool> FoldCondBranchOnPHIImpl(BranchInst *BI,
29842984
AssumptionCache *AC) {
29852985
BasicBlock *BB = BI->getParent();
29862986
PHINode *PN = dyn_cast<PHINode>(BI->getCondition());
2987-
// NOTE: we currently cannot transform this case if the PHI node is used
2988-
// outside of the block.
2989-
if (!PN || PN->getParent() != BB || !PN->hasOneUse())
2987+
if (!PN || PN->getParent() != BB)
29902988
return false;
29912989

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

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

llvm/test/Transforms/SimplifyCFG/jump-threading.ll

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,12 @@ define void @test_phi_extra_use(i1 %c) {
5050
; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
5151
; CHECK: if:
5252
; CHECK-NEXT: call void @foo()
53-
; CHECK-NEXT: br label [[JOIN:%.*]]
54-
; CHECK: else:
55-
; CHECK-NEXT: call void @bar()
56-
; CHECK-NEXT: br label [[JOIN]]
57-
; CHECK: join:
58-
; CHECK-NEXT: [[C2:%.*]] = phi i1 [ true, [[IF]] ], [ false, [[ELSE]] ]
59-
; CHECK-NEXT: call void @use.i1(i1 [[C2]])
60-
; CHECK-NEXT: br i1 [[C2]], label [[IF2:%.*]], label [[ELSE2:%.*]]
61-
; CHECK: if2:
53+
; CHECK-NEXT: call void @use.i1(i1 true)
6254
; CHECK-NEXT: call void @foo()
6355
; CHECK-NEXT: br label [[JOIN2:%.*]]
64-
; CHECK: else2:
56+
; CHECK: else:
57+
; CHECK-NEXT: call void @bar()
58+
; CHECK-NEXT: call void @use.i1(i1 false)
6559
; CHECK-NEXT: call void @bar()
6660
; CHECK-NEXT: br label [[JOIN2]]
6761
; CHECK: join2:

0 commit comments

Comments
 (0)