6g etc: groundwork for eliminating redundant bounds checks.

drop check in range over array.
	drop check in [256]array indexed by byte.

R=ken2
https://golang.org/cl/163088
This commit is contained in:
Russ Cox 2009-12-02 17:30:07 -08:00
parent dc7355a974
commit fdb030d86f
7 changed files with 34 additions and 22 deletions

View File

@ -547,7 +547,7 @@ agen(Node *n, Node *res)
v = mpgetfix(nr->val.u.xval);
if(isslice(nl->type)) {
if(!debug['B']) {
if(!debug['B'] && !n->etype) {
n1 = n3;
n1.op = OINDREG;
n1.type = types[tptr];
@ -599,7 +599,7 @@ agen(Node *n, Node *res)
gmove(&n1, &n2);
regfree(&n1);
if(!debug['B']) {
if(!debug['B'] && !n->etype) {
// check bounds
regalloc(&n4, types[TUINT32], N);
if(isslice(nl->type)) {

View File

@ -1767,7 +1767,7 @@ oindex_const:
v = mpgetfix(r->val.u.xval);
if(o & ODynam) {
if(!debug['B']) {
if(!debug['B'] && !n->etype) {
n1 = *reg;
n1.op = OINDREG;
n1.type = types[tptr];

View File

@ -504,7 +504,7 @@ agen(Node *n, Node *res)
v = mpgetfix(nr->val.u.xval);
if(isslice(nl->type)) {
if(!debug['B']) {
if(!debug['B'] && !n->etype) {
n1 = n3;
n1.op = OINDREG;
n1.type = types[tptr];
@ -547,7 +547,7 @@ agen(Node *n, Node *res)
gmove(&n1, &n2);
regfree(&n1);
if(!debug['B']) {
if(!debug['B'] && !n->etype) {
// check bounds
if(isslice(nl->type)) {
n1 = n3;

View File

@ -1827,23 +1827,24 @@ oindex:
agen(l, reg);
}
if(!(o & ODynam) && l->type->width >= unmappedzero && l->op == OIND) {
// cannot rely on page protections to
// catch array ptr == 0, so dereference.
n2 = *reg;
n2.op = OINDREG;
n2.type = types[TUINT8];
n2.xoffset = 0;
gins(ATESTB, nodintconst(0), &n2);
}
// check bounds
if(!debug['B']) {
if(!debug['B'] && !n->etype) {
if(o & ODynam) {
n2 = *reg;
n2.op = OINDREG;
n2.type = types[tptr];
n2.xoffset = Array_nel;
} else {
if(l->type->width >= unmappedzero && l->op == OIND) {
// cannot rely on page protections to
// catch array ptr == 0, so dereference.
n2 = *reg;
n2.op = OINDREG;
n2.type = types[TUINT8];
n2.xoffset = 0;
gins(ATESTB, nodintconst(0), &n2);
}
nodconst(&n2, types[TUINT64], l->type->bound);
}
gins(optoas(OCMP, types[TUINT32]), reg1, &n2);
@ -1879,7 +1880,7 @@ oindex_const:
v = mpgetfix(r->val.u.xval);
if(o & ODynam) {
if(!debug['B']) {
if(!debug['B'] && !n->etype) {
n1 = *reg;
n1.op = OINDREG;
n1.type = types[tptr];

View File

@ -540,7 +540,7 @@ agen(Node *n, Node *res)
v = mpgetfix(nr->val.u.xval);
if(isslice(nl->type)) {
if(!debug['B']) {
if(!debug['B'] && !n->etype) {
n1 = n3;
n1.op = OINDREG;
n1.type = types[tptr];
@ -558,7 +558,7 @@ agen(Node *n, Node *res)
n1.xoffset = Array_array;
gmove(&n1, &n3);
} else
if(!debug['B']) {
if(!debug['B'] && !n->etype) {
if(v < 0)
yyerror("out of bounds on array");
else
@ -583,7 +583,7 @@ agen(Node *n, Node *res)
gmove(&n1, &n2);
regfree(&n1);
if(!debug['B']) {
if(!debug['B'] && !n->etype) {
// check bounds
if(isslice(nl->type)) {
n1 = n3;

View File

@ -91,7 +91,7 @@ walkrange(Node *n)
Node *ohv1, *hv1, *hv2; // hidden (old) val 1, 2
Node *ha, *hit; // hidden aggregate, iterator
Node *a, *v1, *v2; // not hidden aggregate, val 1, 2
Node *fn;
Node *fn, *tmp;
NodeList *body, *init;
Type *th, *t;
@ -128,8 +128,11 @@ walkrange(Node *n)
n->nincr = nod(OASOP, hv1, nodintconst(1));
n->nincr->etype = OADD;
body = list1(nod(OAS, v1, hv1));
if(v2)
body = list(body, nod(OAS, v2, nod(OINDEX, ha, hv1)));
if(v2) {
tmp = nod(OINDEX, ha, hv1);
tmp->etype = 1; // no bounds check
body = list(body, nod(OAS, v2, tmp));
}
break;
case TMAP:

View File

@ -787,6 +787,14 @@ walkexpr(Node **np, NodeList **init)
case OINDEX:
walkexpr(&n->left, init);
walkexpr(&n->right, init);
// if range of type cannot exceed static array bound,
// disable bounds check
if(!isslice(n->left->type))
if(n->right->type->width < 4)
if((1<<(8*n->right->type->width)) <= n->left->type->bound)
n->etype = 1;
goto ret;
case OINDEXMAP: