Skip to content

Commit 620141b

Browse files
committedJul 3, 2013
small improvement in codegen of constructors; avoid a couple stores
1 parent e8a94c0 commit 620141b

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed
 

‎src/codegen.cpp

+26-5
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ static Function *to_function(jl_lambda_info_t *li, bool cstyle)
247247
assert(f != NULL);
248248
nested_compile = last_n_c;
249249
//f->dump();
250-
verifyFunction(*f);
250+
//verifyFunction(*f);
251251
FPM->run(*f);
252252
//n_compile++;
253253
// print out the function's LLVM code
@@ -2025,23 +2025,44 @@ static Value *emit_expr(jl_value_t *expr, jl_codectx_t *ctx, bool isboxed,
20252025
}
20262026
return mark_julia_type(strct,ty);
20272027
}
2028+
Value *f1 = NULL;
2029+
size_t j = 0;
2030+
int fieldStart = ctx->argDepth;
2031+
if (nf > 0 && sty->fields[0].isptr && nargs>1) {
2032+
// emit first field before allocating struct to save
2033+
// a couple store instructions. avoids initializing
2034+
// the first field to NULL, and sometimes the GC root
2035+
// for the new struct.
2036+
f1 = boxed(emit_expr(args[1],ctx));
2037+
j++;
2038+
if (!jl_is_symbol(args[1]) && !jl_is_symbolnode(args[1]))
2039+
make_gcroot(f1, ctx);
2040+
}
20282041
Value *strct =
20292042
builder.CreateCall(jlallocobj_func,
20302043
ConstantInt::get(T_size,
20312044
sizeof(void*)+sty->size));
20322045
builder.CreateStore(literal_pointer_val((jl_value_t*)ty),
20332046
emit_nthptr_addr(strct, (size_t)0));
2034-
for(size_t i=0; i < nf; i++) {
2047+
if (f1) {
2048+
emit_setfield(sty, strct, 0, f1, ctx, false);
2049+
ctx->argDepth = fieldStart;
2050+
if (nf > 1)
2051+
make_gcroot(strct, ctx);
2052+
}
2053+
else if (nf > 0) {
2054+
make_gcroot(strct, ctx);
2055+
}
2056+
for(size_t i=j; i < nf; i++) {
20352057
if (sty->fields[i].isptr) {
20362058
emit_setfield(sty, strct, i, V_null, ctx, false);
20372059
}
20382060
}
2039-
make_gcroot(strct, ctx);
2040-
for(size_t i=1; i < nargs; i++) {
2061+
for(size_t i=j+1; i < nargs; i++) {
20412062
emit_setfield(sty, strct, i-1, emit_expr(args[i],ctx), ctx,
20422063
false);
20432064
}
2044-
ctx->argDepth--;
2065+
ctx->argDepth = fieldStart;
20452066
return strct;
20462067
}
20472068
else {

0 commit comments

Comments
 (0)
Please sign in to comment.