Skip to content

Commit

Permalink
codegen: fix PhiNode incoming basic block
Browse files Browse the repository at this point in the history
Sometimes doing codegen will change the basic block, resulting in our
making the phi node from the wrong branch. Instead be careful to use
exactly the basic block into which we just put the branch.
  • Loading branch information
vtjnash committed Mar 3, 2020
1 parent 3b53f54 commit 4eb0943
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -944,9 +944,11 @@ static jl_cgval_t emit_typeof(jl_codectx_t &ctx, const jl_cgval_t &p)
ctx.builder.SetInsertPoint(boxBB);
auto boxTy = emit_typeof(ctx, p.Vboxed);
ctx.builder.CreateBr(mergeBB);
boxBB = ctx.builder.GetInsertBlock(); // could have changed
ctx.builder.SetInsertPoint(unboxBB);
auto unboxTy = emit_unboxty();
ctx.builder.CreateBr(mergeBB);
unboxBB = ctx.builder.GetInsertBlock(); // could have changed
ctx.builder.SetInsertPoint(mergeBB);
auto phi = ctx.builder.CreatePHI(T_prjlvalue, 2);
phi->addIncoming(boxTy, boxBB);
Expand Down Expand Up @@ -1012,6 +1014,7 @@ static Value *emit_sizeof(jl_codectx_t &ctx, const jl_cgval_t &p)
Value *datatype = emit_typeof(p.V);
Value *dyn_size = emit_datatype_size(ctx, datatype);
ctx.builder.CreateBr(postBB);
dynloadBB = ctx.builder.GetInsertBlock(); // could have changed
ctx.builder.SetInsertPoint(postBB);
PHINode *sizeof_merge = ctx.builder.CreatePHI(T_int32, 2);
sizeof_merge->addIncoming(dyn_size, dynloadBB);
Expand Down Expand Up @@ -1207,6 +1210,7 @@ static std::pair<Value*, bool> emit_isa(jl_codectx_t &ctx, const jl_cgval_t &x,
Value *istype_boxed = ctx.builder.CreateICmpEQ(emit_typeof(ctx, x.Vboxed),
maybe_decay_untracked(literal_pointer_val(ctx, intersected_type)));
ctx.builder.CreateBr(postBB);
isaBB = ctx.builder.GetInsertBlock(); // could have changed
ctx.builder.SetInsertPoint(postBB);
PHINode *istype = ctx.builder.CreatePHI(T_int1, 2);
istype->addIncoming(ConstantInt::get(T_int1, 0), currBB);
Expand Down Expand Up @@ -2405,15 +2409,18 @@ static Value *box_union(jl_codectx_t &ctx, const jl_cgval_t &vinfo, const SmallB
init_bits_cgval(ctx, box, vinfo_r, jl_is_mutable(jt) ? tbaa_mutab : tbaa_immut);
}
}
box_merge->addIncoming(maybe_decay_untracked(box), tempBB);
box = maybe_decay_untracked(box);
tempBB = ctx.builder.GetInsertBlock(); // could have changed
box_merge->addIncoming(box, tempBB);
ctx.builder.CreateBr(postBB);
},
vinfo.typ,
counter);
ctx.builder.SetInsertPoint(defaultBB);
if (skip.size() > 0) {
assert(skip[0]);
box_merge->addIncoming(maybe_decay_untracked(V_null), defaultBB);
Value *box = maybe_decay_untracked(V_null);
box_merge->addIncoming(box, defaultBB);
ctx.builder.CreateBr(postBB);
}
else if (!vinfo.Vboxed) {
Expand Down
7 changes: 6 additions & 1 deletion src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1739,7 +1739,9 @@ static Value *emit_bitsunion_compare(jl_codectx_t &ctx, const jl_cgval_t &arg1,
switchInst->addCase(ConstantInt::get(T_int8, idx), tempBB);
jl_cgval_t sel_arg1(arg1, (jl_value_t*)jt, NULL);
jl_cgval_t sel_arg2(arg2, (jl_value_t*)jt, NULL);
phi->addIncoming(emit_bits_compare(ctx, sel_arg1, sel_arg2), tempBB);
Value *cmp = emit_bits_compare(ctx, sel_arg1, sel_arg2);
tempBB = ctx.builder.GetInsertBlock(); // could have changed
phi->addIncoming(cmp, tempBB);
ctx.builder.CreateBr(postBB);
},
arg1.typ,
Expand Down Expand Up @@ -1928,6 +1930,7 @@ static Value *emit_f_is(jl_codectx_t &ctx, const jl_cgval_t &arg1, const jl_cgva
Value *bitcmp = emit_bits_compare(ctx,
jl_cgval_t(arg1, typ, NULL),
jl_cgval_t(arg2, typ, NULL));
isaBB = ctx.builder.GetInsertBlock(); // might have changed
ctx.builder.CreateBr(postBB);
ctx.builder.SetInsertPoint(postBB);
PHINode *cmp = ctx.builder.CreatePHI(T_int1, 2);
Expand Down Expand Up @@ -2085,6 +2088,7 @@ static bool emit_builtin_call(jl_codectx_t &ctx, jl_cgval_t *ret, jl_value_t *f,
ctx.builder.SetInsertPoint(inBB);
Value *v_sz = emit_arraysize(ctx, ary, idx_dyn);
ctx.builder.CreateBr(ansBB);
inBB = ctx.builder.GetInsertBlock(); // could have changed
ctx.f->getBasicBlockList().push_back(ansBB);
ctx.builder.SetInsertPoint(ansBB);
PHINode *result = ctx.builder.CreatePHI(T_size, 2);
Expand Down Expand Up @@ -4198,6 +4202,7 @@ static Function* gen_cfun_wrapper(
ctx.builder.CreateBr(afterBB);
ctx.builder.SetInsertPoint(unboxedBB);
Value *p3 = emit_new_bits(ctx, runtime_dt, val);
unboxedBB = ctx.builder.GetInsertBlock(); // could have changed
ctx.builder.CreateBr(afterBB);
ctx.builder.SetInsertPoint(afterBB);
PHINode *p = ctx.builder.CreatePHI(T_prjlvalue, 3);
Expand Down
3 changes: 2 additions & 1 deletion src/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,8 +847,9 @@ static jl_cgval_t emit_ifelse(jl_codectx_t &ctx, jl_cgval_t c, jl_cgval_t x, jl_
tindex = compute_tindex_unboxed(ctx, x, rt_hint);
}
tindex = ctx.builder.CreateOr(tindex, ConstantInt::get(T_int8, 0x80));
ret->addIncoming(tindex, compute);
compute = ctx.builder.GetInsertBlock(); // could have changed
ctx.builder.CreateBr(post);
ret->addIncoming(tindex, compute);
ctx.builder.SetInsertPoint(post);
ctx.builder.Insert(ret);
tindex = ret;
Expand Down

0 comments on commit 4eb0943

Please sign in to comment.