diff --git a/src/codegen.cpp b/src/codegen.cpp index 6e71293eff1e6..fa02318c28588 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -6242,7 +6242,10 @@ static std::unique_ptr emit_function( workstack.push_back(lname - 1); BasicBlock *ifnot = BB[lname]; BasicBlock *ifso = BB[cursor+2]; - ctx.builder.CreateCondBr(isfalse, ifnot, ifso); + if (ifnot == ifso) + ctx.builder.CreateBr(ifnot); + else + ctx.builder.CreateCondBr(isfalse, ifnot, ifso); find_next_stmt(cursor + 1); continue; } @@ -6332,6 +6335,19 @@ static std::unique_ptr emit_function( // This edge was statically unreachable. Don't codegen it. if (!FromBB) continue; + // We folded this branch to an unconditional branch, only codegen it once + if (cast(FromBB->getTerminator())->isUnconditional()) { + bool found = false; + for (size_t j = 0; j < i; ++j) { + size_t j_edge = jl_unbox_long(jl_array_ptr_ref(edges, j)); + if (j_edge == edge) { + found = true; + assert(jl_egal(value, jl_array_ptr_ref(values, j))); + } + } + if (found) + continue; + } #ifndef JL_NDEBUG if (FromBB) { bool found_pred = false; diff --git a/test/core.jl b/test/core.jl index 92ff2617d4289..d17be11a27309 100644 --- a/test/core.jl +++ b/test/core.jl @@ -6202,3 +6202,36 @@ function test27566(a,b) end test27566(a, b, c, d) = a.*(b, c, d) @test test27566(1,1) == (1,0,1) + +# Issue #27594 +struct Iter27594 end +Base.iterate(::Iter27594) = (1, nothing) +Base.iterate(::Iter27594, ::Any) = nothing + +function foo27594() + ind = 0 + for x in (1,) + for y in Iter27594() + ind += 1 + end + end + ind +end + +@test foo27594() == 1 + +# Issue 27597 +function f27597(y) + x = Int[] + + if isempty(y) + y = 1:length(x) + elseif false + ; + end + + length(y) + return y +end +@test f27597([1]) == [1] +@test f27597([]) == 1:0