diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index fb942bbd0574d6..7a9c4b2e50750e 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -2984,9 +2984,7 @@ static Optional FoldCondBranchOnPHIImpl(BranchInst *BI, AssumptionCache *AC) { BasicBlock *BB = BI->getParent(); PHINode *PN = dyn_cast(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. @@ -2996,6 +2994,8 @@ static Optional 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; diff --git a/llvm/test/Transforms/SimplifyCFG/jump-threading.ll b/llvm/test/Transforms/SimplifyCFG/jump-threading.ll index aed090fed4640b..8316710dbd0dd4 100644 --- a/llvm/test/Transforms/SimplifyCFG/jump-threading.ll +++ b/llvm/test/Transforms/SimplifyCFG/jump-threading.ll @@ -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: