Commit 2dd16a32 authored by Ken Thompson's avatar Ken Thompson

first cut at optimizing

R=r
OCL=19564
CL=19564
parent 9dc4b1ca
......@@ -11,6 +11,7 @@ HFILES=\
../gc/go.h\
../6l/6.out.h\
gg.h\
opt.h\
OFILES=\
list.$O\
......@@ -19,6 +20,9 @@ OFILES=\
cgen.$O\
gsubr.$O\
obj.$O\
peep.$O\
reg.$O\
bits.$O\
../6l/enam.$O\
LIB=\
......
// Inferno utils/cc/bits.c
// http://code.google.com/p/inferno-os/source/browse/utils/cc/bits.c
//
// Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
// Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
// Portions Copyright © 1997-1999 Vita Nuova Limited
// Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
// Portions Copyright © 2004,2006 Bruce Ellis
// Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
// Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
// Portions Copyright © 2009 The Go Authors. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "gg.h"
#include "opt.h"
Bits
bor(Bits a, Bits b)
{
Bits c;
int i;
for(i=0; i<BITS; i++)
c.b[i] = a.b[i] | b.b[i];
return c;
}
Bits
band(Bits a, Bits b)
{
Bits c;
int i;
for(i=0; i<BITS; i++)
c.b[i] = a.b[i] & b.b[i];
return c;
}
/*
Bits
bnot(Bits a)
{
Bits c;
int i;
for(i=0; i<BITS; i++)
c.b[i] = ~a.b[i];
return c;
}
*/
int
bany(Bits *a)
{
int i;
for(i=0; i<BITS; i++)
if(a->b[i])
return 1;
return 0;
}
int
beq(Bits a, Bits b)
{
int i;
for(i=0; i<BITS; i++)
if(a.b[i] != b.b[i])
return 0;
return 1;
}
int
bnum(Bits a)
{
int i;
int32 b;
for(i=0; i<BITS; i++)
if(b = a.b[i])
return 32*i + bitno(b);
fatal("bad in bnum");
return 0;
}
Bits
blsh(uint n)
{
Bits c;
c = zbits;
c.b[n/32] = 1L << (n%32);
return c;
}
int
bset(Bits a, uint n)
{
if(a.b[n/32] & (1L << (n%32)))
return 1;
return 0;
}
int
bitno(int32 b)
{
int i;
for(i=0; i<32; i++)
if(b & (1L<<i))
return i;
fatal("bad in bitno");
return 0;
}
int
Qconv(Fmt *fp)
{
char str[STRINGSZ], ss[STRINGSZ], *s;
Bits bits;
int i;
str[0] = 0;
bits = va_arg(fp->args, Bits);
while(bany(&bits)) {
i = bnum(bits);
if(str[0])
strcat(str, " ");
if(var[i].sym == S) {
sprint(ss, "$%lld", var[i].offset);
s = ss;
} else
s = var[i].sym->name;
if(strlen(str) + strlen(s) + 1 >= STRINGSZ)
break;
strcat(str, s);
bits.b[i/32] &= ~(1L << (i%32));
}
return fmtstrcpy(fp, str);
}
......@@ -99,10 +99,9 @@ if(throwreturn == N) {
pc->as = ARET; // overwrite AEND
pc->lineno = lineno;
// if(debug['N']) {
// regopt(ptxt);
// debug['N'] = 0;
// }
if(debug['N']) {
regopt(ptxt);
}
// fill in argument size
ptxt->to.offset = rnd(curfn->type->argwid, maxround);
......
......@@ -39,6 +39,7 @@ struct Prog
Addr from; // src address
Addr to; // dst address
Prog* link; // next instruction in this func
void* reg; // pointer to containing Reg struct
};
#define P ((Prog*)0)
......@@ -102,7 +103,6 @@ EXTERN Pool* poolast;
EXTERN Biobuf* bout;
EXTERN int32 dynloc;
EXTERN uchar reg[D_NONE];
EXTERN ushort txt[NTYPE*NTYPE];
EXTERN int32 maxround;
EXTERN int32 widthptr;
EXTERN Sym* symstringo; // string objects
......
......@@ -829,76 +829,6 @@ gmove(Node *f, Node *t)
gins(a, f, t);
}
void
buildtxt(void)
{
Type t1, t2;
int i, j, a;
memset(&t1, 0, sizeof(t1));
memset(&t2, 0, sizeof(t2));
for(i=0; i<NTYPE; i++)
for(j=0; j<NTYPE; j++) {
a = AGOK;
txt[i*NTYPE+j] = a;
t1.etype = i;
t2.etype = j;
if(isint[i] || isptr[i] || i==TBOOL) {
if(isint[j] || isptr[j] || j==TBOOL) {
dowidth(&t1);
dowidth(&t2);
if(t1.width >= t2.width) {
a = AMOVL;
if(t1.width >= 8)
a = AMOVQ;
txt[i*NTYPE+j] = a;
continue;
}
switch(i) {
case TINT8:
a = AMOVBLSX;
if(t1.width >= 8)
a = AMOVBQSX;
break;
case TINT16:
a = AMOVWLSX;
if(t1.width >= 8)
a = AMOVWQSX;
break;
case TINT32:
a = AMOVLQSX;
break;
case TBOOL:
case TUINT8:
a = AMOVBLZX;
if(t1.width >= 8)
a = AMOVBQZX;
break;
case TUINT16:
a = AMOVWLZX;
if(t1.width >= 8)
a = AMOVLQZX;
break;
case TPTR32:
case TUINT32:
a = AMOVWQZX;
break;
}
txt[i*NTYPE+j] = a;
continue;
}
if(isfloat[j]) {
}
}
if(isint[j] || isptr[j] || j==TBOOL) {
if(isfloat[i]) {
}
}
}
}
void
regsalloc(Node *f, Type *t)
{
......@@ -1000,7 +930,9 @@ naddr(Node *n, Addr *a)
break;
case ONAME:
a->etype = n->etype;
a->etype = 0;
if(n->type != T)
a->etype = n->type->etype;
a->offset = n->xoffset;
a->sym = n->sym;
if(a->sym == S)
......
......@@ -90,6 +90,15 @@ dumpobj(void)
}
sym = 1;
// fix up pc
pcloc = 0;
for(pl=plist; pl!=nil; pl=pl->link) {
for(p=pl->firstpc; p!=P; p=p->link) {
p->loc = pcloc;
pcloc++;
}
}
// put out functions
for(pl=plist; pl!=nil; pl=pl->link) {
......@@ -204,8 +213,13 @@ zaddr(Biobuf *b, Addr *a, int s)
t |= T_SYM;
switch(a->type) {
case D_BRANCH:
a->offset = a->branch->loc;
default:
t |= T_TYPE;
case D_NONE:
if(a->offset != 0) {
t |= T_OFFSET;
......
// Derived from Inferno utils/6c/gc.h
// http://code.google.com/p/inferno-os/source/browse/utils/6c/gc.h
//
// Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
// Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
// Portions Copyright © 1997-1999 Vita Nuova Limited
// Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
// Portions Copyright © 2004,2006 Bruce Ellis
// Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
// Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
// Portions Copyright © 2009 The Go Authors. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#define Z N
#define Adr Addr
#define BITS 5
#define NVAR (BITS*sizeof(uint32)*8)
#define D_HI D_NONE
#define D_LO D_NONE
#define isregtype(t) ((t)>= D_AX && (t)<=D_R15)
#define BLOAD(r) band(bnot(r->refbehind), r->refahead)
#define BSTORE(r) band(bnot(r->calbehind), r->calahead)
#define LOAD(r) (~r->refbehind.b[z] & r->refahead.b[z])
#define STORE(r) (~r->calbehind.b[z] & r->calahead.b[z])
#define CLOAD 5
#define CREF 5
#define CINF 1000
#define LOOP 3
typedef struct Bits Bits;
typedef struct Reg Reg;
typedef struct Var Var;
typedef struct Rgn Rgn;
struct Bits
{
uint32 b[BITS];
};
struct Reg
{
Bits set;
Bits use1;
Bits use2;
Bits refbehind;
Bits refahead;
Bits calbehind;
Bits calahead;
Bits regdiff;
Bits act;
int32 regu;
int32 loop; /* could be shorter */
int32 rpo; /* reverse post ordering */
int32 active;
// uint32 magic;
// int32 pc;
// Reg* log5;
Reg* p1;
Reg* p2;
Reg* p2link;
Reg* s1;
Reg* s2;
Reg* link;
Prog* prog;
};
#define R ((Reg*)0)
struct Var
{
vlong offset;
Sym* sym;
char name;
char etype;
};
#define NRGN 600
struct Rgn
{
Reg* enter;
short cost;
short varno;
short regno;
};
EXTERN int32 exregoffset; // not set
EXTERN int32 exfregoffset; // not set
EXTERN Reg* firstr;
EXTERN Reg* lastr;
EXTERN Reg zreg;
EXTERN Reg* freer;
EXTERN Var var[NVAR];
EXTERN Reg** rpo2r;
EXTERN Rgn region[NRGN];
EXTERN Rgn* rgp;
EXTERN int nregion;
EXTERN int nvar;
EXTERN int32 regbits;
EXTERN int32 exregbits;
EXTERN Bits externs;
EXTERN Bits params;
EXTERN Bits consts;
EXTERN Bits addrs;
EXTERN int change;
EXTERN Bits zbits;
EXTERN uchar typechlpfd[NTYPE]; // botch
EXTERN uchar typev[NTYPE]; // botch
EXTERN int32 maxnr;
EXTERN int32* idom;
/*
* bits.c
*/
Bits bor(Bits, Bits);
Bits band(Bits, Bits);
Bits bnot(Bits);
int bany(Bits*);
int bnum(Bits);
Bits blsh(uint);
int beq(Bits, Bits);
int bset(Bits, uint);
int Qconv(Fmt *fp);
/*
* reg.c
*/
Reg* rega(void);
int rcmp(const void*, const void*);
void regopt(Prog*);
void addmove(Reg*, int, int, int);
Bits mkvar(Reg*, Adr*);
void prop(Reg*, Bits, Bits);
void loopit(Reg*, int32);
void synch(Reg*, Bits);
uint32 allreg(uint32, Rgn*);
void paint1(Reg*, int);
uint32 paint2(Reg*, int);
void paint3(Reg*, int, int32, int);
void addreg(Adr*, int);
/*
* peep.c
*/
void peep(void);
void excise(Reg*);
Reg* uniqp(Reg*);
Reg* uniqs(Reg*);
int regtyp(Adr*);
int anyvar(Adr*);
int subprop(Reg*);
int copyprop(Reg*);
int copy1(Adr*, Adr*, Reg*, int);
int copyu(Prog*, Adr*, Adr*);
int copyas(Adr*, Adr*);
int copyau(Adr*, Adr*);
int copysub(Adr*, Adr*, Adr*, int);
int copysub1(Prog*, Adr*, Adr*, int);
int32 RtoB(int);
int32 FtoB(int);
int BtoR(int32);
int BtoF(int32);
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment