Skip to content

Commit 8ed5c35

Browse files
committed
Change memory indexing to use the type as index instead of i8, also add nsw/nuw flags whenever possible.
1 parent 40fbc88 commit 8ed5c35

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/cgutils.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,7 @@ static bool bounds_check_enabled(jl_codectx_t &ctx, jl_value_t *inbounds) {
20532053

20542054
static Value *emit_bounds_check(jl_codectx_t &ctx, const jl_cgval_t &ainfo, jl_value_t *ty, Value *i, Value *len, jl_value_t *boundscheck)
20552055
{
2056-
Value *im1 = ctx.builder.CreateSub(i, ConstantInt::get(ctx.types().T_size, 1));
2056+
Value *im1 = ctx.builder.CreateSub(i, ConstantInt::get(ctx.types().T_size, 1)); // Can this be nuw/nsw
20572057
if (bounds_check_enabled(ctx, boundscheck)) {
20582058
++EmittedBoundschecks;
20592059
Value *ok = ctx.builder.CreateICmpULT(im1, len);
@@ -4659,7 +4659,7 @@ static jl_cgval_t emit_memoryref(jl_codectx_t &ctx, const jl_cgval_t &ref, jl_cg
46594659
Value *mem = CreateSimplifiedExtractValue(ctx, V, 1);
46604660
maybeSetName(ctx.emission_context, mem, "memoryref_mem");
46614661
Value *i = emit_unbox(ctx, ctx.types().T_size, idx, (jl_value_t*)jl_long_type);
4662-
Value *offset = ctx.builder.CreateSub(i, ConstantInt::get(ctx.types().T_size, 1));
4662+
Value *offset = ctx.builder.CreateSub(i, ConstantInt::get(ctx.types().T_size, 1), "", true, true);
46634663
setName(ctx.emission_context, offset, "memoryref_offset");
46644664
Value *elsz = emit_genericmemoryelsize(ctx, mem, ref.typ, false);
46654665
bool bc = bounds_check_enabled(ctx, inbounds);
@@ -4671,7 +4671,7 @@ static jl_cgval_t emit_memoryref(jl_codectx_t &ctx, const jl_cgval_t &ref, jl_cg
46714671
bool isunion = layout->flags.arrayelem_isunion;
46724672
bool isghost = layout->size == 0;
46734673
if ((!isboxed && isunion) || isghost) {
4674-
newdata = ctx.builder.CreateAdd(data, offset);
4674+
newdata = ctx.builder.CreateAdd(data, offset, "", true, true);
46754675
setName(ctx.emission_context, newdata, "memoryref_data+offset");
46764676
if (bc) {
46774677
BasicBlock *failBB, *endBB;
@@ -4710,10 +4710,9 @@ static jl_cgval_t emit_memoryref(jl_codectx_t &ctx, const jl_cgval_t &ref, jl_cg
47104710
setName(ctx.emission_context, ovflw, "memoryref_ovflw");
47114711
}
47124712
#endif
4713-
boffset = ctx.builder.CreateMul(offset, elsz);
4714-
setName(ctx.emission_context, boffset, "memoryref_byteoffset");
4715-
newdata = ctx.builder.CreateGEP(getInt8Ty(ctx.builder.getContext()), data, boffset);
4716-
setName(ctx.emission_context, newdata, "memoryref_data_byteoffset");
4713+
Type *elty = isboxed ? ctx.types().T_prjlvalue : julia_type_to_llvm(ctx, jl_tparam1(ref.typ));
4714+
newdata = ctx.builder.CreateInBoundsGEP(elty, data, offset);
4715+
setName(ctx.emission_context, newdata, "memoryref_data_offset");
47174716
(void)boffset; // LLVM is very bad at handling GEP with types different from the load
47184717
if (bc) {
47194718
BasicBlock *failBB, *endBB;
@@ -4734,7 +4733,7 @@ static jl_cgval_t emit_memoryref(jl_codectx_t &ctx, const jl_cgval_t &ref, jl_cg
47344733
#elif 1
47354734
Value *bidx0 = ctx.builder.CreateSub(
47364735
ctx.builder.CreatePtrToInt(newdata, ctx.types().T_size),
4737-
ctx.builder.CreatePtrToInt(mptr, ctx.types().T_size));
4736+
ctx.builder.CreatePtrToInt(mptr, ctx.types().T_size), "", true, true);
47384737
Value *blen = ctx.builder.CreateMul(mlen, elsz, "", true, true);
47394738
setName(ctx.emission_context, blen, "memoryref_bytelen");
47404739
Value *inbound = ctx.builder.CreateICmpULT(bidx0, blen);
@@ -4776,13 +4775,13 @@ static jl_cgval_t emit_memoryref_offset(jl_codectx_t &ctx, const jl_cgval_t &ref
47764775
// (data - mptr) / elsz
47774776
offset = ctx.builder.CreateSub(
47784777
ctx.builder.CreatePtrToInt(data, ctx.types().T_size),
4779-
ctx.builder.CreatePtrToInt(mptr, ctx.types().T_size));
4778+
ctx.builder.CreatePtrToInt(mptr, ctx.types().T_size), "", true, true);
47804779
setName(ctx.emission_context, offset, "memoryref_offset");
47814780
Value *elsz = emit_genericmemoryelsize(ctx, mem, ref.typ, false);
47824781
offset = ctx.builder.CreateExactUDiv(offset, elsz);
47834782
setName(ctx.emission_context, offset, "memoryref_offsetidx");
47844783
}
4785-
offset = ctx.builder.CreateAdd(offset, ConstantInt::get(ctx.types().T_size, 1));
4784+
offset = ctx.builder.CreateAdd(offset, ConstantInt::get(ctx.types().T_size, 1), "", true, true);
47864785
return mark_julia_type(ctx, offset, false, jl_long_type);
47874786
}
47884787

src/codegen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4870,7 +4870,7 @@ static bool emit_builtin_call(jl_codectx_t &ctx, jl_cgval_t *ret, jl_value_t *f,
48704870
// This is not necessary for correctness, but allows to omit
48714871
// the extra code for getting the length of the tuple
48724872
if (!bounds_check_enabled(ctx, boundscheck)) {
4873-
vidx = ctx.builder.CreateSub(vidx, ConstantInt::get(ctx.types().T_size, 1));
4873+
vidx = ctx.builder.CreateSub(vidx, ConstantInt::get(ctx.types().T_size, 1), "", true, true);
48744874
}
48754875
else {
48764876
vidx = emit_bounds_check(ctx, ptrobj, (jl_value_t*)ptrobj.typ, vidx,

0 commit comments

Comments
 (0)