Skip to content

Commit 93f4d2f

Browse files
committed
replace all uses of null pointers in cgutils.cpp and codegen.cpp
1 parent 577c5d1 commit 93f4d2f

File tree

2 files changed

+79
-70
lines changed

2 files changed

+79
-70
lines changed

src/cgutils.cpp

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ STATISTIC(EmittedErrors, "Number of emitted errors");
1313
STATISTIC(EmittedConditionalErrors, "Number of emitted conditional errors");
1414
STATISTIC(EmittedExceptions, "Number of emitted exceptions");
1515
STATISTIC(EmittedConditionalExceptions, "Number of emitted conditional exceptions");
16-
STATISTIC(EmittedNullchecks, "Number of emitted nullchecks");
1716
STATISTIC(EmittedGuards, "Number of emitted guards");
1817
STATISTIC(EmittedIsaUnions, "Number of emitted isa-union checks");
1918
STATISTIC(EmittedIsa, "Number of emitted isa checks");
@@ -563,7 +562,7 @@ static inline Instruction *maybe_mark_load_dereferenceable(Instruction *LI, bool
563562
static Value *literal_pointer_val(jl_codectx_t &ctx, jl_value_t *p)
564563
{
565564
if (p == NULL)
566-
return Constant::getNullValue(ctx.types().T_pjlvalue);
565+
return null_pointer(ctx, ctx.types().T_pjlvalue);
567566
Value *pgv = literal_pointer_val_slot(ctx.emission_context, jl_Module, p);
568567
jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, ctx.tbaa().tbaa_const);
569568
auto load = ai.decorateInst(maybe_mark_load_dereferenceable(
@@ -1336,7 +1335,7 @@ static Value *emit_typeof(jl_codectx_t &ctx, const jl_cgval_t &p, bool maybenull
13361335
Value *tindex = ctx.builder.CreateAnd(p.TIndex, ConstantInt::get(getInt8Ty(ctx.builder.getContext()), 0x7f));
13371336
bool allunboxed = is_uniontype_allunboxed(p.typ);
13381337
Type *expr_type = justtag ? ctx.types().T_size : ctx.types().T_pjlvalue;
1339-
Value *datatype_or_p = Constant::getNullValue(PointerType::getUnqual(expr_type->getContext()));
1338+
Value *datatype_or_p = null_pointer(ctx, PointerType::getUnqual(expr_type->getContext()));
13401339
unsigned counter = 0;
13411340
for_each_uniontype_small(
13421341
[&](unsigned idx, jl_datatype_t *jt) {
@@ -1361,7 +1360,7 @@ static Value *emit_typeof(jl_codectx_t &ctx, const jl_cgval_t &p, bool maybenull
13611360
};
13621361
Value *res;
13631362
if (!allunboxed) {
1364-
Value *isnull = ctx.builder.CreateIsNull(datatype_or_p);
1363+
Value *isnull = null_pointer_cmp_eq(ctx, datatype_or_p);
13651364
setName(ctx.emission_context, isnull, "typetag_isnull");
13661365
BasicBlock *boxBB = BasicBlock::Create(ctx.builder.getContext(), "boxed", ctx.f);
13671366
BasicBlock *unboxBB = BasicBlock::Create(ctx.builder.getContext(), "unboxed", ctx.f);
@@ -1601,20 +1600,6 @@ static void undef_var_error_ifnot(jl_codectx_t &ctx, Value *ok, jl_sym_t *name,
16011600
ctx.builder.SetInsertPoint(ifok);
16021601
}
16031602

1604-
// ctx.builder.CreateIsNotNull(v) lowers incorrectly in non-standard
1605-
// address spaces where null is not zero
1606-
// TODO: adapt to https://github.com/llvm/llvm-project/pull/131557 once merged
1607-
static Value *null_pointer_cmp(jl_codectx_t &ctx, Value *v)
1608-
{
1609-
++EmittedNullchecks;
1610-
Type *T = v->getType();
1611-
return ctx.builder.CreateICmpNE(
1612-
v,
1613-
ctx.builder.CreateAddrSpaceCast(
1614-
Constant::getNullValue(ctx.builder.getPtrTy(0)), T));
1615-
}
1616-
1617-
16181603
// If `nullcheck` is not NULL and a pointer NULL check is necessary
16191604
// store the pointer to be checked in `*nullcheck` instead of checking it
16201605
static void null_pointer_check(jl_codectx_t &ctx, Value *v, Value **nullcheck)
@@ -1733,7 +1718,7 @@ static Value *emit_typeof(jl_codectx_t &ctx, Value *v, bool maybenull, bool just
17331718
assert(v != NULL && !isa<AllocaInst>(v) && "expected a conditionally boxed value");
17341719
Value *nonnull = maybenull ? null_pointer_cmp(ctx, v) : ConstantInt::get(getInt1Ty(ctx.builder.getContext()), 1);
17351720
Function *typeof = prepare_call(jl_typeof_func);
1736-
auto val = emit_guarded_test(ctx, nonnull, Constant::getNullValue(justtag ? ctx.types().T_size : typeof->getReturnType()), [&] {
1721+
auto val = emit_guarded_test(ctx, nonnull, null_pointer(ctx, justtag ? ctx.types().T_size : typeof->getReturnType()), [&] {
17371722
// e.g. emit_typeof(ctx, v)
17381723
Value *typetag = ctx.builder.CreateCall(typeof, {v});
17391724
if (notag)
@@ -2078,7 +2063,7 @@ static Value *emit_bounds_check(jl_codectx_t &ctx, const jl_cgval_t &ainfo, jl_v
20782063
else { // unboxed jl_value_t*
20792064
Value *a = ainfo.V;
20802065
if (ainfo.isghost) {
2081-
a = Constant::getNullValue(getPointerTy(ctx.builder.getContext()));
2066+
a = null_pointer(ctx, getPointerTy(ctx.builder.getContext()));
20822067
}
20832068
else if (!ainfo.inline_roots.empty()) {
20842069
a = value_to_pointer(ctx, ainfo).V;
@@ -2228,7 +2213,7 @@ static jl_cgval_t typed_load(jl_codectx_t &ctx, Value *ptr, Value *idx_0based, j
22282213
maybe_mark_load_dereferenceable(load1, true, sizeof(void*)*2, alignof(void*));
22292214
load1->setOrdering(Order);
22302215
ai.decorateInst(load1);
2231-
instr = Constant::getNullValue(elty);
2216+
instr = null_pointer(ctx, elty);
22322217
instr = ctx.builder.CreateInsertValue(instr, load0, 0);
22332218
instr = ctx.builder.CreateInsertValue(instr, load1, 1);
22342219
}
@@ -2503,14 +2488,14 @@ static jl_cgval_t typed_store(jl_codectx_t &ctx,
25032488
cmpop = mark_julia_type(ctx, Compare, true, cmpop.typ);
25042489
}
25052490
else {
2506-
Compare = Constant::getNullValue(ctx.types().T_prjlvalue); // TODO: does this need to be an invalid bit pattern?
2491+
Compare = null_pointer(ctx, ctx.types().T_prjlvalue); // TODO: does this need to be an invalid bit pattern?
25072492
needloop = true;
25082493
}
25092494
}
25102495
else if (issetfieldonce) {
25112496
needloop = !isboxed && Order != AtomicOrdering::NotAtomic && nb > sizeof(void*);
25122497
if (Order != AtomicOrdering::NotAtomic)
2513-
Compare = Constant::getNullValue(elty);
2498+
Compare = null_pointer(ctx, elty);
25142499
}
25152500
else { // swap or modify
25162501
LoadInst *Current = ctx.builder.CreateAlignedLoad(elty, ptr, Align(alignment));
@@ -2588,7 +2573,7 @@ static jl_cgval_t typed_store(jl_codectx_t &ctx,
25882573
oldval = mark_julia_type(ctx, load, isboxed, jltype);
25892574
assert(!issetfieldonce || first_ptr != nullptr);
25902575
if (issetfieldonce)
2591-
Success = ctx.builder.CreateIsNull(first_ptr);
2576+
Success = null_pointer_cmp_eq(ctx, first_ptr);
25922577
else
25932578
Success = emit_f_is(ctx, oldval, cmpop, first_ptr, nullptr);
25942579
if (needloop && ismodifyfield)
@@ -2645,7 +2630,7 @@ static jl_cgval_t typed_store(jl_codectx_t &ctx,
26452630
assert(!isboxed && maybe_null_if_boxed);
26462631
Value *first_ptr = extract_first_ptr(ctx, realinstr);
26472632
assert(first_ptr != nullptr);
2648-
Done = ctx.builder.CreateIsNotNull(first_ptr);
2633+
Done = null_pointer_cmp(ctx, first_ptr);
26492634
}
26502635
else {
26512636
// Done = !(!Success && (first_ptr != NULL && oldval == cmpop))
@@ -3382,7 +3367,7 @@ static Value *emit_genericmemoryowner(jl_codectx_t &ctx, Value *t)
33823367
LoadInst *owner = ctx.builder.CreateAlignedLoad(ctx.types().T_prjlvalue, addr, Align(sizeof(void*)));
33833368
jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, ctx.tbaa().tbaa_memoryptr);
33843369
ai.decorateInst(owner);
3385-
return ctx.builder.CreateSelect(ctx.builder.CreateIsNull(owner), t, owner);
3370+
return ctx.builder.CreateSelect(null_pointer_cmp_eq(ctx, owner), t, owner);
33863371
});
33873372
}
33883373

@@ -3648,7 +3633,7 @@ static AllocaInst *try_emit_union_alloca(jl_codectx_t &ctx, jl_uniontype_t *ut,
36483633

36493634
/*
36503635
* Box unboxed values in a union. Optionally, skip certain unboxed values,
3651-
* returning `Constant::getNullValue(ctx.types().T_pjlvalue)` in one of the skipped cases. If `skip` is not empty,
3636+
* returning `null` in one of the skipped cases. If `skip` is not empty,
36523637
* skip[0] (corresponding to unknown boxed) must always be set. In that
36533638
* case, the calling code must separately deal with the case where
36543639
* `vinfo` is already an unknown boxed union (union tag UNION_BOX_MARKER).
@@ -3708,7 +3693,7 @@ static Value *box_union(jl_codectx_t &ctx, const jl_cgval_t &vinfo, const SmallB
37083693
ctx.builder.SetInsertPoint(defaultBB);
37093694
if (skip.size() > 0) {
37103695
assert(skip[0]);
3711-
box_merge->addIncoming(Constant::getNullValue(ctx.types().T_prjlvalue), defaultBB);
3696+
box_merge->addIncoming(null_pointer(ctx, ctx.types().T_prjlvalue), defaultBB);
37123697
ctx.builder.CreateBr(postBB);
37133698
}
37143699
else if (!vinfo.Vboxed) {
@@ -4196,7 +4181,7 @@ static jl_cgval_t emit_new_struct(jl_codectx_t &ctx, jl_value_t *ty, size_t narg
41964181
}
41974182
else if (init_as_value) {
41984183
if (tracked.second) {
4199-
strct = Constant::getNullValue(lt);
4184+
strct = null_pointer(ctx, lt);
42004185
}
42014186
else {
42024187
strct = UndefValue::get(lt);
@@ -4205,7 +4190,7 @@ static jl_cgval_t emit_new_struct(jl_codectx_t &ctx, jl_value_t *ty, size_t narg
42054190
}
42064191
}
42074192
else if (tracked.second) {
4208-
inline_roots.resize(tracked.second, Constant::getNullValue(ctx.types().T_prjlvalue));
4193+
inline_roots.resize(tracked.second, null_pointer(ctx, ctx.types().T_prjlvalue));
42094194
strct = nullptr;
42104195
if (tracked.first) {
42114196
AllocaInst *bits = emit_static_alloca(ctx, tracked.first, Align(julia_alignment(ty)));
@@ -4663,7 +4648,7 @@ static jl_cgval_t _emit_memoryref(jl_codectx_t &ctx, Value *mem, Value *data, co
46634648
// mark_julia_type(ctx, data, false, jl_voidpointer_type)
46644649
//};
46654650
//return emit_new_struct(ctx, typ, 3, argv);
4666-
Value *ref = Constant::getNullValue(get_memoryref_type(ctx.builder.getContext(), ctx.types().T_size, layout, 0));
4651+
Value *ref = null_pointer(ctx, get_memoryref_type(ctx.builder.getContext(), ctx.types().T_size, layout, 0));
46674652
ref = ctx.builder.CreateInsertValue(ref, data, 0);
46684653
ref = ctx.builder.CreateInsertValue(ref, mem, 1);
46694654
setName(ctx.emission_context, ref, "memory_ref");
@@ -4689,7 +4674,7 @@ static Value *emit_memoryref_FCA(jl_codectx_t &ctx, const jl_cgval_t &ref, const
46894674
ai0.decorateInst(load0);
46904675
setName(ctx.emission_context, load0, "memory_ref_FCA0");
46914676
Value *root = ctx.builder.CreateBitCast(ref.inline_roots[0], type->getElementType(1));
4692-
Value *load = Constant::getNullValue(type);
4677+
Value *load = null_pointer(ctx, type);
46934678
load = ctx.builder.CreateInsertValue(load, load0, 0);
46944679
load = ctx.builder.CreateInsertValue(load, root, 1);
46954680
return load;

0 commit comments

Comments
 (0)