7c: fix long to vlong/pointer conversion, avoid negative immediate offsets
we have to explicitely convert to vlong by sign or zero extending as not every operation leaves a proper zero/sign extended result in the register. for example NEGW will zero extend, breaking negative int offsets on pointers. we explicitely insert SXTW or MOVWU instructions which the peephole optimizer takes out again when it is safe todo so. when promoting constant offsets to immediate offsets, make sure the offset will be in range. otherwise the linker will produce not so optimal pointer arithmetic instructions to calculate the offset.
This commit is contained in:
parent
b44440bd16
commit
15c3f45e5f
4 changed files with 49 additions and 15 deletions
|
@ -390,7 +390,7 @@ cgenrel(Node *n, Node *nn, int inrel)
|
|||
r = l;
|
||||
while(r->op == OADD)
|
||||
r = r->right;
|
||||
if(sconst(r) && (v = r->vconst+nod.xoffset) >= -4096 && v < 4096) {
|
||||
if(usableoffset(n, nod.xoffset, r)){
|
||||
v = r->vconst;
|
||||
r->vconst = 0;
|
||||
cgen(l, &nod);
|
||||
|
@ -613,7 +613,7 @@ reglcgen(Node *t, Node *n, Node *nn)
|
|||
r = n->left;
|
||||
while(r->op == OADD)
|
||||
r = r->right;
|
||||
if(sconst(r) && (v = r->vconst+t->xoffset) >= -4096 && v < 4096) {
|
||||
if(usableoffset(n, t->xoffset, r)) {
|
||||
v = r->vconst;
|
||||
r->vconst = 0;
|
||||
lcgen(n, t);
|
||||
|
@ -623,11 +623,17 @@ reglcgen(Node *t, Node *n, Node *nn)
|
|||
return;
|
||||
}
|
||||
} else if(n->op == OINDREG) {
|
||||
if((v = n->xoffset) >= -4096 && v < 4096) {
|
||||
if(usableoffset(n, t->xoffset+n->xoffset, nil)) {
|
||||
Type *tt = n->type;
|
||||
n->type = types[TIND];
|
||||
n->op = OREGISTER;
|
||||
v = n->xoffset;
|
||||
n->xoffset = 0;
|
||||
cgen(n, t);
|
||||
t->xoffset += v;
|
||||
n->xoffset = v;
|
||||
n->op = OINDREG;
|
||||
n->type = tt;
|
||||
regind(t, n);
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,9 @@
|
|||
|
||||
int xtramodes(Reg*, Adr*);
|
||||
|
||||
Reg* findpre(Reg *r, Adr *v);
|
||||
Reg* findinc(Reg *r, Reg *r2, Adr *v);
|
||||
|
||||
void
|
||||
peep(void)
|
||||
{
|
||||
|
@ -55,6 +58,21 @@ loop1:
|
|||
t++;
|
||||
}
|
||||
}
|
||||
if(p->as == ASXTW){
|
||||
r1 = findpre(r, &p->from);
|
||||
if(r1 != R){
|
||||
p1 = r1->prog;
|
||||
switch(p1->as){
|
||||
case AMOVB:
|
||||
case AMOVBU:
|
||||
case AMOVH:
|
||||
case AMOVHU:
|
||||
case AMOVW:
|
||||
p->as = AMOVW;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(p->as == AMOV || p->as == AMOVW || p->as == AFMOVS || p->as == AFMOVD)
|
||||
if(regtyp(&p->to)) {
|
||||
if(p->from.type == D_CONST)
|
||||
|
@ -425,6 +443,7 @@ subprop(Reg *r0)
|
|||
case AFMOVD:
|
||||
case AMOVW:
|
||||
case AMOV:
|
||||
case ASXTW:
|
||||
if(p->to.type == v1->type)
|
||||
if(p->to.reg == v1->reg)
|
||||
goto gotit;
|
||||
|
@ -954,8 +973,8 @@ copyu(Prog *p, Adr *v, Adr *s)
|
|||
case AMOVBU:
|
||||
case AMOVW:
|
||||
case AMOVWU:
|
||||
case ASXTW:
|
||||
case AMOV:
|
||||
|
||||
case AMVN:
|
||||
case AMVNW:
|
||||
case ANEG:
|
||||
|
|
|
@ -150,6 +150,7 @@ regopt(Prog *p)
|
|||
case AMOVHU:
|
||||
case AMOVW:
|
||||
case AMOVWU:
|
||||
case ASXTW:
|
||||
case AFMOVS:
|
||||
case AFCVTSD:
|
||||
case AFMOVD:
|
||||
|
|
|
@ -761,7 +761,10 @@ gmove(Node *f, Node *t)
|
|||
case TVLONG:
|
||||
case TUVLONG:
|
||||
case TIND:
|
||||
a = AMOV;
|
||||
if(typeu[ft])
|
||||
a = AMOVWU;
|
||||
else
|
||||
a = ASXTW;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
@ -785,14 +788,16 @@ gmove(Node *f, Node *t)
|
|||
case TUINT:
|
||||
case TLONG:
|
||||
case TULONG:
|
||||
case TVLONG:
|
||||
case TUVLONG:
|
||||
case TIND:
|
||||
case TSHORT:
|
||||
case TUSHORT:
|
||||
case TCHAR:
|
||||
case TUCHAR:
|
||||
a = AMOV; /* TO DO: conversion done? */
|
||||
a = AMOVWU;
|
||||
break;
|
||||
case TVLONG:
|
||||
case TUVLONG:
|
||||
case TIND:
|
||||
a = AMOV;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
@ -1323,16 +1328,19 @@ sval(vlong v)
|
|||
int
|
||||
usableoffset(Node *n, vlong o, Node *v)
|
||||
{
|
||||
int s, et;
|
||||
int s;
|
||||
|
||||
et = v->type->etype;
|
||||
if(v->op != OCONST || typefd[et])
|
||||
return 0;
|
||||
if(v != nil){
|
||||
if(v->op != OCONST || typefd[v->type->etype])
|
||||
return 0;
|
||||
o += v->vconst;
|
||||
}
|
||||
s = n->type->width;
|
||||
if(s > 16)
|
||||
s = 16;
|
||||
o += v->vconst;
|
||||
return o >= -256 || o < 4095*s;
|
||||
if((o % s) != 0)
|
||||
return 0;
|
||||
return o >= -256 && o < 4096*s;
|
||||
}
|
||||
|
||||
long
|
||||
|
|
Loading…
Reference in a new issue