improve compilation of arithmetic and other builtins, this should

make it more aggressive (and still correct).
This commit is contained in:
Vitor Santos Costa 2009-02-25 00:13:56 +00:00
parent 91657556ee
commit 083b7c9b92
4 changed files with 132 additions and 88 deletions

View File

@ -2471,7 +2471,13 @@ a_f2(int var, cmp_op_info *cmp_info, yamop *code_p, int pass_no, struct intermed
{ {
Int opc = cip->cpc->rnd2; Int opc = cip->cpc->rnd2;
Ventry *ve = (Ventry *)(cip->cpc->rnd1); Ventry *ve = (Ventry *)(cip->cpc->rnd1);
int is_y_var = (ve->KindOfVE == PermVar); int is_y_var = FALSE;
Int xpos = 0;
if (ve) {
is_y_var = (ve->KindOfVE == PermVar);
xpos = ve->NoOfVE & MaskVarAdrs;
}
if (opc <= _primitive) { if (opc <= _primitive) {
if (is_y_var) { if (is_y_var) {
@ -2518,7 +2524,7 @@ a_f2(int var, cmp_op_info *cmp_info, yamop *code_p, int pass_no, struct intermed
return code_p; return code_p;
} else { } else {
if (pass_no) { if (pass_no) {
code_p->u.xl.x = emit_x(ve->NoOfVE & MaskVarAdrs); code_p->u.xl.x = emit_x(xpos);
switch (opc) { switch (opc) {
case _atom: case _atom:
code_p->opc = opcode(_p_atom_x); code_p->opc = opcode(_p_atom_x);
@ -2560,13 +2566,21 @@ a_f2(int var, cmp_op_info *cmp_info, yamop *code_p, int pass_no, struct intermed
return code_p; return code_p;
} }
} }
if (opc == _functor && cip->cpc->nextInst->op == f_var_op) { if (opc == _functor
&& (cip->cpc->nextInst->op == f_var_op ||
cip->cpc->nextInst->op == f_0_op)) {
Ventry *nve; Ventry *nve;
int is_y_nvar = FALSE;
Int nxpos = 0;
cip->cpc = cip->cpc->nextInst; cip->cpc = cip->cpc->nextInst;
nve = (Ventry *)(cip->cpc->rnd1); nve = (Ventry *)(cip->cpc->rnd1);
if (nve) {
is_y_nvar = (nve->KindOfVE == PermVar);
nxpos = nve->NoOfVE & MaskVarAdrs;
}
if (is_y_var) { if (is_y_var) {
if (nve->KindOfVE == PermVar) { if (is_y_nvar) {
if (pass_no) { if (pass_no) {
code_p->opc = emit_op(_p_func2f_yy); code_p->opc = emit_op(_p_func2f_yy);
code_p->u.yyx.y1 = emit_y(ve); code_p->u.yyx.y1 = emit_y(ve);
@ -2579,17 +2593,17 @@ a_f2(int var, cmp_op_info *cmp_info, yamop *code_p, int pass_no, struct intermed
if (pass_no) { if (pass_no) {
code_p->opc = emit_op(_p_func2f_yx); code_p->opc = emit_op(_p_func2f_yx);
code_p->u.yxx.y = emit_y(ve); code_p->u.yxx.y = emit_y(ve);
code_p->u.yxx.x1 = emit_x(nve->NoOfVE & MaskVarAdrs); code_p->u.yxx.x1 = emit_x(nxpos);
code_p->u.yxx.x2 = cmp_info->x1_arg; code_p->u.yxx.x2 = cmp_info->x1_arg;
} }
GONEXT(yxx); GONEXT(yxx);
return code_p; return code_p;
} }
} else { } else {
if (nve->KindOfVE == PermVar) { if (is_y_nvar) {
if (pass_no) { if (pass_no) {
code_p->opc = emit_op(_p_func2f_xy); code_p->opc = emit_op(_p_func2f_xy);
code_p->u.xxy.x1 = emit_x(ve->NoOfVE & MaskVarAdrs); code_p->u.xxy.x1 = emit_x(xpos);
code_p->u.xxy.y2 = emit_y(nve); code_p->u.xxy.y2 = emit_y(nve);
code_p->u.xxy.x = cmp_info->x1_arg; code_p->u.xxy.x = cmp_info->x1_arg;
} }
@ -2598,8 +2612,8 @@ a_f2(int var, cmp_op_info *cmp_info, yamop *code_p, int pass_no, struct intermed
} else { } else {
if (pass_no) { if (pass_no) {
code_p->opc = emit_op(_p_func2f_xx); code_p->opc = emit_op(_p_func2f_xx);
code_p->u.xxx.x1 = emit_x(ve->NoOfVE & MaskVarAdrs); code_p->u.xxx.x1 = emit_x(xpos);
code_p->u.xxx.x2 = emit_x(nve->NoOfVE & MaskVarAdrs); code_p->u.xxx.x2 = emit_x(nxpos);
code_p->u.xxx.x = cmp_info->x1_arg; code_p->u.xxx.x = cmp_info->x1_arg;
} }
GONEXT(xxx); GONEXT(xxx);
@ -2788,7 +2802,7 @@ a_f2(int var, cmp_op_info *cmp_info, yamop *code_p, int pass_no, struct intermed
code_p->opc = emit_op(_p_func2s_vv); code_p->opc = emit_op(_p_func2s_vv);
break; break;
} }
code_p->u.xxx.x = emit_x(ve->NoOfVE & MaskVarAdrs); code_p->u.xxx.x = emit_x(xpos);
code_p->u.xxx.x1 = cmp_info->x1_arg; code_p->u.xxx.x1 = cmp_info->x1_arg;
code_p->u.xxx.x2 = cmp_info->x2_arg; code_p->u.xxx.x2 = cmp_info->x2_arg;
} }
@ -2836,7 +2850,7 @@ a_f2(int var, cmp_op_info *cmp_info, yamop *code_p, int pass_no, struct intermed
code_p->opc = emit_op(_p_func2s_cv); code_p->opc = emit_op(_p_func2s_cv);
break; break;
} }
code_p->u.xxn.x = emit_x(ve->NoOfVE & MaskVarAdrs); code_p->u.xxn.x = emit_x(xpos);
code_p->u.xxn.c = cmp_info->c_arg; code_p->u.xxn.c = cmp_info->c_arg;
code_p->u.xxn.xi = cmp_info->x1_arg; code_p->u.xxn.xi = cmp_info->x1_arg;
} }
@ -2890,7 +2904,7 @@ a_f2(int var, cmp_op_info *cmp_info, yamop *code_p, int pass_no, struct intermed
code_p->opc = emit_op(_p_func2s_vc); code_p->opc = emit_op(_p_func2s_vc);
break; break;
} }
code_p->u.xxn.x = emit_x(ve->NoOfVE & MaskVarAdrs); code_p->u.xxn.x = emit_x(xpos);
code_p->u.xxn.c = cmp_info->c_arg; code_p->u.xxn.c = cmp_info->c_arg;
code_p->u.xxn.xi = cmp_info->x1_arg; code_p->u.xxn.xi = cmp_info->x1_arg;
} }
@ -3799,6 +3813,9 @@ do_pass(int pass_no, yamop **entry_codep, int assembling, int *clause_has_blobsp
case f_var_op: case f_var_op:
code_p = a_f2(TRUE, &cmp_info, code_p, pass_no, cip); code_p = a_f2(TRUE, &cmp_info, code_p, pass_no, cip);
break; break;
case f_0_op:
code_p = a_f2(TRUE, &cmp_info, code_p, pass_no, cip);
break;
case enter_profiling_op: case enter_profiling_op:
code_p = a_pl(_enter_profiling, (PredEntry *)(cip->cpc->rnd1), code_p, pass_no); code_p = a_pl(_enter_profiling, (PredEntry *)(cip->cpc->rnd1), code_p, pass_no);
break; break;

View File

@ -332,7 +332,7 @@ check_var(Term t, unsigned int level, Int argno, compiler_struct *cglobs) {
/* Be careful with eithers. I may make a variable global in a branch, /* Be careful with eithers. I may make a variable global in a branch,
and not in another. and not in another.
a :- (b([X]) ; c), go(X). a :- (b([X]) ; c), go(X).
This variaiable will not be globalised if we are coming from This variable will not be globalised if we are coming from
the second branch. the second branch.
I also need to protect the onhead because Luis uses that to I also need to protect the onhead because Luis uses that to
@ -872,15 +872,20 @@ c_eq(Term t1, Term t2, compiler_struct *cglobs)
} }
/* first argument is an unbound var */ /* first argument is an unbound var */
if (IsNewVar(t1)) { if (IsNewVar(t1)) {
Int v = --cglobs->tmpreg; Int v;
if (IsVarTerm(t2)) { if (IsVarTerm(t2)) {
v = 0;
c_var(t2, v, 0, 0, cglobs); c_var(t2, v, 0, 0, cglobs);
cglobs->onhead = TRUE;
c_var(t1, v, 0, 0, cglobs);
cglobs->onhead = FALSE;
} else { } else {
v = --cglobs->tmpreg;
c_arg(v, t2, 0, 0, cglobs); c_arg(v, t2, 0, 0, cglobs);
cglobs->onhead = TRUE;
c_var(t1, v, 0, 0, cglobs);
cglobs->onhead = FALSE;
} }
cglobs->onhead = TRUE;
c_var(t1, v, 0, 0, cglobs);
cglobs->onhead = FALSE;
} else { } else {
Int v = --cglobs->tmpreg; Int v = --cglobs->tmpreg;
c_var(t1, v, 0, 0, cglobs); c_var(t1, v, 0, 0, cglobs);
@ -1006,7 +1011,7 @@ c_bifun(Int Op, Term t1, Term t2, Term t3, Term Goal, int mod, compiler_struct *
/* it has to be either an integer or a floating point */ /* it has to be either an integer or a floating point */
} else if (IsIntegerTerm(t2)) { } else if (IsIntegerTerm(t2)) {
/* first temp */ /* first temp */
Int v1 = --cglobs->tmpreg; Int v1 = 0;
Yap_emit(fetch_args_vi_op, IntegerOfTerm(t2), 0L, &cglobs->cint); Yap_emit(fetch_args_vi_op, IntegerOfTerm(t2), 0L, &cglobs->cint);
/* these should be the arguments */ /* these should be the arguments */
@ -1248,7 +1253,7 @@ c_bifun(Int Op, Term t1, Term t2, Term t3, Term Goal, int mod, compiler_struct *
return; return;
} else { } else {
/* first temp */ /* first temp */
Int v1 = --cglobs->tmpreg; Int v1 = 0;
Yap_emit(fetch_args_cv_op, t1, Zero, &cglobs->cint); Yap_emit(fetch_args_cv_op, t1, Zero, &cglobs->cint);
/* these should be the arguments */ /* these should be the arguments */
c_var(t2, v1, 0, 0, cglobs); c_var(t2, v1, 0, 0, cglobs);
@ -1257,7 +1262,7 @@ c_bifun(Int Op, Term t1, Term t2, Term t3, Term Goal, int mod, compiler_struct *
} }
} else if (IsIntegerTerm(t1)) { } else if (IsIntegerTerm(t1)) {
/* first temp */ /* first temp */
Int v1 = --cglobs->tmpreg; Int v1 = 0;
Yap_emit(fetch_args_iv_op, IntegerOfTerm(t1), 0L, &cglobs->cint); Yap_emit(fetch_args_iv_op, IntegerOfTerm(t1), 0L, &cglobs->cint);
/* these should be the arguments */ /* these should be the arguments */
c_var(t2, v1, 0, 0, cglobs); c_var(t2, v1, 0, 0, cglobs);
@ -1304,19 +1309,15 @@ c_bifun(Int Op, Term t1, Term t2, Term t3, Term Goal, int mod, compiler_struct *
} }
} else { } else {
/* generate code for a temp and then unify temp with previous variable */ /* generate code for a temp and then unify temp with previous variable */
Term tmpvar = MkVarTerm(); Yap_emit(f_0_op, 0, (unsigned int)Op, &cglobs->cint);
if (H == (CELL *)cglobs->cint.freep0) {
/* oops, too many new variables */
save_machine_regs();
longjmp(cglobs->cint.CompilerBotch,OUT_OF_TEMPS_BOTCH);
}
c_var(tmpvar,f_flag,(unsigned int)Op, 0, cglobs);
/* I have to dit here, before I do the unification */ /* I have to dit here, before I do the unification */
if (Op == _functor) { if (Op == _functor) {
Yap_emit(empty_call_op, Zero, Zero, &cglobs->cint); Yap_emit(empty_call_op, Zero, (unsigned int)Op, &cglobs->cint);
Yap_emit(restore_tmps_and_skip_op, Zero, Zero, &cglobs->cint); Yap_emit(restore_tmps_and_skip_op, Zero, Zero, &cglobs->cint);
} }
c_eq(tmpvar,t3, cglobs); cglobs->onhead = TRUE;
c_var(t3, 0, 0, 0, cglobs);
cglobs->onhead = FALSE;
} }
} }
@ -2725,6 +2726,7 @@ checktemp(Int arg, Int rn, compiler_vm_op ic, compiler_struct *cglobs)
Int vadr; Int vadr;
Int vreg; Int vreg;
cglobs->vadr = vadr = (v->NoOfVE); cglobs->vadr = vadr = (v->NoOfVE);
cglobs->vreg = vreg = vadr & MaskVarAdrs; cglobs->vreg = vreg = vadr & MaskVarAdrs;
if (v->KindOfVE == PermVar || v->KindOfVE == VoidVar) if (v->KindOfVE == PermVar || v->KindOfVE == VoidVar)
@ -2823,7 +2825,6 @@ checkreg(Int arg, Int rn, compiler_vm_op ic, int var_arg, compiler_struct *cglob
if (rn >= 0) if (rn >= 0)
return rn; return rn;
vreg = 0;
if (var_arg) { if (var_arg) {
Ventry *v = (Ventry *) arg; Ventry *v = (Ventry *) arg;
@ -2832,16 +2833,21 @@ checkreg(Int arg, Int rn, compiler_vm_op ic, int var_arg, compiler_struct *cglob
vreg = 0; vreg = 0;
else if (vreg == 0) { else if (vreg == 0) {
checktemp(arg, rn, ic, cglobs); checktemp(arg, rn, ic, cglobs);
vreg = (v->NoOfVE) & MaskVarAdrs;
++cglobs->Uses[vreg]; ++cglobs->Uses[vreg];
} }
} if (!vreg) {
if (vreg == 0) { vreg = 1;
vreg = cglobs->MaxCTemps; while (cglobs->Uses[vreg] != 0) {
do ++vreg;
--vreg; }
while (vreg && cglobs->Uses[vreg] == 0); cglobs->Uses[vreg] = v->RCountOfVE;
++vreg; }
++cglobs->Uses[vreg]; } else {
vreg = 1;
while (cglobs->Uses[vreg] != 0) {
++vreg;
}
} }
while (p) { while (p) {
if (p->op >= get_var_op && p->op <= put_unsafe_op && p->rnd2 == rn) if (p->op >= get_var_op && p->op <= put_unsafe_op && p->rnd2 == rn)
@ -2953,6 +2959,13 @@ c_layout(compiler_struct *cglobs)
compiler_vm_op ic = cglobs->cint.cpc->op; compiler_vm_op ic = cglobs->cint.cpc->op;
Int arg = cglobs->cint.cpc->rnd1; Int arg = cglobs->cint.cpc->rnd1;
Int rn = cglobs->cint.cpc->rnd2; Int rn = cglobs->cint.cpc->rnd2;
#if LOW_LEVEL_TRACER
{
extern int Yap_do_low_level_trace;
if (0 && Yap_do_low_level_trace)
fprintf(stderr,"%d %x uses %d,%d,%d,%d,%d,6:%d,%d,%d,%d,%d\n",ic,cglobs->Contents[10],cglobs->Uses[1],cglobs->Uses[2],cglobs->Uses[3],cglobs->Uses[4],cglobs->Uses[5],cglobs->Uses[6],cglobs->Uses[7],cglobs->Uses[8],cglobs->Uses[9],cglobs->Uses[10]);
}
#endif
switch (ic) { switch (ic) {
case pop_or_op: case pop_or_op:
if (needs_either) if (needs_either)
@ -2999,12 +3012,18 @@ c_layout(compiler_struct *cglobs)
#endif #endif
cglobs->cint.cpc->op = nop_op; cglobs->cint.cpc->op = nop_op;
} }
cglobs->Contents[rn] = cglobs->vadr; if (!cglobs->Uses[rn])
cglobs->Contents[rn] = cglobs->vadr;
break; break;
case get_val_op: case get_val_op:
--cglobs->Uses[rn]; --cglobs->Uses[rn];
checktemp(arg, rn, ic, cglobs); checktemp(arg, rn, ic, cglobs);
cglobs->Contents[rn] = cglobs->vadr; if (!cglobs->Uses[rn])
cglobs->Contents[rn] = cglobs->vadr;
break;
case f_0_op:
if (rn_to_kill[0]) --cglobs->Uses[rn_to_kill[0]];
rn_to_kill[1]=rn_to_kill[0]=0;
break; break;
case f_var_op: case f_var_op:
if (rn_to_kill[0]) --cglobs->Uses[rn_to_kill[0]]; if (rn_to_kill[0]) --cglobs->Uses[rn_to_kill[0]];
@ -3036,12 +3055,17 @@ c_layout(compiler_struct *cglobs)
case get_longint_op: case get_longint_op:
case get_bigint_op: case get_bigint_op:
--cglobs->Uses[rn]; --cglobs->Uses[rn];
cglobs->Contents[rn] = arg; /* This is not safe if we are in the middle of a disjunction and there
is something ahead.
*/
if (!cglobs->Uses[rn])
cglobs->Contents[rn] = arg;
break; break;
case get_list_op: case get_list_op:
case get_struct_op: case get_struct_op:
cglobs->Contents[rn] = NIL;
--cglobs->Uses[rn]; --cglobs->Uses[rn];
if (!cglobs->Uses[rn])
cglobs->Contents[rn] = NIL;
break; break;
case put_var_op: case put_var_op:
case put_unsafe_op: case put_unsafe_op:
@ -3074,7 +3098,8 @@ c_layout(compiler_struct *cglobs)
if (cglobs->cint.cpc->nextInst && if (cglobs->cint.cpc->nextInst &&
cglobs->cint.cpc->nextInst->op == put_val_op && cglobs->cint.cpc->nextInst->op == put_val_op &&
cglobs->cint.cpc->nextInst->nextInst && cglobs->cint.cpc->nextInst->nextInst &&
cglobs->cint.cpc->nextInst->nextInst->op == f_var_op) (cglobs->cint.cpc->nextInst->nextInst->op == f_var_op ||
cglobs->cint.cpc->nextInst->nextInst->op == f_0_op) )
rn_kills = 1; rn_kills = 1;
break; break;
case f_val_op: case f_val_op:

View File

@ -693,7 +693,8 @@ static char *opformat[] =
"fetch_integer_reg\t%d,%N", "fetch_integer_reg\t%d,%N",
"fetch_reg_integer\t%d,%N", "fetch_reg_integer\t%d,%N",
"function_to_var\t%v,%B", "function_to_var\t%v,%B",
"function_to_al\t%v,%B", "function_to_val\t%v,%B",
"function_to_0\t%B",
"enter_profiling\t\t%g", "enter_profiling\t\t%g",
"retry_profiled\t\t%g", "retry_profiled\t\t%g",
"count_call_op\t\t%g", "count_call_op\t\t%g",
@ -748,49 +749,49 @@ static char *opformat[] =
"put_number_on_val\t", "put_number_on_val\t",
"put_float_on_val\t", "put_float_on_val\t",
"put_integer_on_val\t", "put_integer_on_val\t",
"equal_floats\n", "equal_floats\t",
"equal_ints\n", "equal_ints\t",
"equal_numbers\n", "equal_numbers\t",
"lt_floats\n", "lt_floats\t",
"lt_ints\n", "lt_ints\t",
"lt_numbers\n", "lt_numbers\t",
"gt_floats\n", "gt_floats\t",
"gt_ints\n", "gt_ints\t",
"add_float\n", "add_float\t",
"add_int\n", "add_int\t",
"add\n", "add\t",
"sub_float\n", "sub_float\t",
"sub_int\n", "sub_int\t",
"sub\n", "sub\t",
"mul_float\n", "mul_float\t",
"mul_int\n", "mul_int\t",
"mul\n", "mul\t",
"shift_right_constant\n", "shift_right_constant\t",
"shift_right_by_constant\n", "shift_right_by_constant\t",
"shift_right\n", "shift_right\t",
"shift_left_constant\n", "shift_left_constant\t",
"shift_left_by_constant\n", "shift_left_by_constant\t",
"shift_left\n", "shift_left\t",
"divide_constant\n", "divide_constant\t",
"divide_by_constant\n", "divide_by_constant\t",
"divide\n", "divide\t",
"integer_divide_constant\n", "integer_divide_constant\t",
"integer_divide_by_constant\n", "integer_divide_by_constant\t",
"integer_divide\n", "integer_divide\t",
"mod_constant\n", "mod_constant\t",
"mod_by_constant\n", "mod_by_constant\t",
"mod\n", "mod\t",
"rem_constant\n", "rem_constant\t",
"rem_by_constant\n", "rem_by_constant\t",
"rem\n", "rem\t",
"and_constant\n", "and_constant\t",
"and\n", "and\t",
"or_constant\n", "or_constant\t",
"or\n", "or\t",
"xor_constant\n", "xor_constant\t",
"xor\n", "xor\t",
"uminus\n", "uminus\t",
"label_control\n" "label_control\t"
#ifdef SFUNC #ifdef SFUNC
, ,
"get_s_f_op\t%f,%r", "get_s_f_op\t%f,%r",

View File

@ -134,6 +134,7 @@ typedef enum compiler_op {
fetch_args_vi_op, fetch_args_vi_op,
f_var_op, f_var_op,
f_val_op, f_val_op,
f_0_op,
enter_profiling_op, enter_profiling_op,
retry_profiled_op, retry_profiled_op,
count_call_op, count_call_op,
@ -173,7 +174,6 @@ typedef enum compiler_op {
wait_while_var_op, wait_while_var_op,
force_wait_op, force_wait_op,
write_op, write_op,
is_op,
equal_op, equal_op,
exit_op, exit_op,
#endif #endif
@ -276,6 +276,7 @@ typedef struct VENTRY {
PInstr *LastOpForV; PInstr *LastOpForV;
BITS16 AgeOfVE; BITS16 AgeOfVE;
BITS16 BranchOfVE; BITS16 BranchOfVE;
BITS16 LastBranchOfVE;
BITS16 FirstOfVE; BITS16 FirstOfVE;
BITS16 RCountOfVE; BITS16 RCountOfVE;
BITS16 FlagsOfVE; BITS16 FlagsOfVE;