changeset 341:ca34f02b2056

ptr cache speed up
author kono
date Sat, 26 Jun 2004 11:09:11 +0900
parents 0150de6a3244
children 3e0b1183983d
files mc-code-ia32.c mc-code-mips.c mc-code-powerpc.c mc-code.h mc-codegen.c mc-codegen.h rsyncs test/bitfield1.c
diffstat 8 files changed, 149 insertions(+), 157 deletions(-) [+]
line wrap: on
line diff
--- a/mc-code-ia32.c	Sat Jun 26 00:26:01 2004 +0900
+++ b/mc-code-ia32.c	Sat Jun 26 11:09:11 2004 +0900
@@ -341,6 +341,18 @@
     }
 }
 
+extern void
+use_ptr_cache(int r)
+{
+    error(-1);
+}
+
+extern void
+code_ptr_cache_def(int r,NMTBL *nptr)
+{
+    error(-1);
+}
+
 int 
 get_input_register_var(int i,NMTBL *nptr,int is_code)
 {
--- a/mc-code-mips.c	Sat Jun 26 00:26:01 2004 +0900
+++ b/mc-code-mips.c	Sat Jun 26 11:09:11 2004 +0900
@@ -24,7 +24,6 @@
 
 static void data_mode(char *name);
 static void text_mode(int alignment);
-static void init_ptr_cache();
 static void ld_indexx(int byte, int n, int xreg,int reg,int sign);
 static void local_table(void);
 static void shift(char *op, int creg,int reg);
@@ -267,7 +266,6 @@
 
 static void code_save_stacks();
 static void code_save_input_registers(int dots);
-static void clear_ptr_cache_reg(int r);
 static void    set_ireg(int,int);
 static void    set_dreg(int,int);
 static void    set_freg(int,int);
@@ -782,6 +780,12 @@
     }
 }
 
+extern void
+use_ptr_cache(int r)
+{
+    regs[r]=PTRC_REG;
+}
+
 int
 get_input_dregister_var(int i,NMTBL *n,int is_code,int d)
 {
@@ -1084,80 +1088,11 @@
     return xreg;
 }
 
-#define MAX_PTR_CACHE 10
-
-int ptr_cache=0;
-
-void
-init_ptr_cache()
-{
-    int i;
-    for(i=0;i<MAX_PTR_CACHE;i++) {
-	ptr_cache=glist3(0,ptr_cache,0);
-    }
-}
-
-void
-clear_ptr_cache_reg(int r)
-{
-    int ptcptr=ptr_cache;
-    while(ptcptr) {
-	if(car(ptcptr)&&caddr(ptcptr)==r) {
-	    car(ptcptr)=0;
-	    caddr(ptcptr)=0;
-	    free_register(r);
-	    return;
-	}
-	ptcptr=cadr(ptcptr);
-    }
-}
-
-void
-clear_ptr_cache()
+extern void
+code_ptr_cache_def(int r, NMTBL *nptr)
 {
-    int ptcptr=ptr_cache;
-    while(ptcptr) {
-	if(car(ptcptr))
-	    free_register(caddr(ptcptr));
-	car(ptcptr)=0;
-	caddr(ptcptr)=0;
-	ptcptr=cadr(ptcptr);
-    }
-}
-
-
-int
-get_ptr_cache(NMTBL *nptr)
-{
-    int r;
-    int ptcptr=ptr_cache;
-    int g = (int)nptr;
-    int p,p1;
-    char *rrn;
-
-    p1 = ptcptr; p = cadr(p1); /* unnecessary, if ptcptr is initialized */
-    while(ptcptr) {
-	if(car(ptcptr)==g) return caddr(ptcptr);
-	p1=p; p=ptcptr;
-	ptcptr=cadr(ptcptr);
-    }
-    cadr(p1)=0;            /* remove the last one */
-    cadr(p) = ptr_cache;   /* connect current queue to the last one */
-    ptr_cache = p;         /* now the last one is the top */
-    if (!caddr(p)) {
-	if((r=get_register())) {
-	    caddr(p)=r; regs[r]=PTRC_REG;
-	} else {
-	    error(-1);
-	    r=creg; /* this can't happen */
-	}
-	car(p)=g;
-    } else {
-	r = caddr(p);
-    }
-    rrn = register_name(r);
+    char *rrn = register_name(r);
     printf("\tla %s,%s\n",rrn,nptr->nm);
-    return r;
 }
 
 static char *cload(int sz,int sign) { 
--- a/mc-code-powerpc.c	Sat Jun 26 00:26:01 2004 +0900
+++ b/mc-code-powerpc.c	Sat Jun 26 11:09:11 2004 +0900
@@ -32,12 +32,12 @@
 
 static void data_mode(char *name);
 static void text_mode();
-static void init_ptr_cache();
 static void ld_indexx(int byte, int n, int xreg,int reg,int sign);
 static void local_table(void);
 static void shift(char *op, int creg,int reg);
 static int struct_push(int e4,int t,int arg);
 
+
 static int creg;
 
 static int output_mode = TEXT_EMIT_MODE;
@@ -240,7 +240,6 @@
 
 static void code_save_stacks();
 static void code_save_input_registers(int dots);
-static void clear_ptr_cache_reg(int r);
 static void    set_ireg(int,int);
 static void    set_freg(int,int);
 static void    set_lreg(int,int);
@@ -510,11 +509,8 @@
 	return i;   /* その場所を表す番号を返す */
     }
     /* PTR_CACHE をつぶす */
-    for(i=MAX_TMP_REG;i>MIN_TMP_REG;i--) {
-	if (regs[i]==PTRC_REG) {
-	    clear_ptr_cache_reg(i);
-	} else 
-	    continue;
+    if ((i=last_ptr_cache())) {
+	clear_ptr_cache_reg(i);
 	regs[i]=USING_REG;      /* そのレジスタを使うことを宣言し */
 	return i;   /* その場所を表す番号を返す */
     }
@@ -701,6 +697,12 @@
     }
 }
 
+extern void
+use_ptr_cache(int r)
+{
+    regs[r]=PTRC_REG;
+}
+
 int
 get_input_dregister_var(int i,NMTBL *n,int is_code,int d)
 {
@@ -1017,78 +1019,10 @@
 
 static int code_base;
 
-#define MAX_PTR_CACHE 10
-
-int ptr_cache=0;
-
-void
-init_ptr_cache()
-{
-    int i;
-    for(i=0;i<MAX_PTR_CACHE;i++) {
-	ptr_cache=glist3(0,ptr_cache,0);
-    }
-}
-
-void
-clear_ptr_cache_reg(int r)
-{
-    int ptcptr=ptr_cache;
-    while(ptcptr) {
-	if(car(ptcptr)&&caddr(ptcptr)==r) {
-	    car(ptcptr)=0;
-	    caddr(ptcptr)=0;
-	    free_register(r);
-	    return;
-	}
-	ptcptr=cadr(ptcptr);
-    }
-}
-
-void
-clear_ptr_cache()
+extern void
+code_ptr_cache_def(int r,NMTBL *nptr)
 {
-    int ptcptr=ptr_cache;
-    while(ptcptr) {
-	if(car(ptcptr))
-	    free_register(caddr(ptcptr));
-	car(ptcptr)=0;
-	caddr(ptcptr)=0;
-	ptcptr=cadr(ptcptr);
-    }
-}
-
-
-int
-get_ptr_cache(NMTBL *nptr)
-{
-    int r;
-    int ptcptr=ptr_cache;
-    int g = (int)nptr;
-    int p,p1;
-    char *rrn;
-
-    p1 = ptcptr; p = cadr(p1); /* unnecessary, if ptcptr is initialized */
-    while(ptcptr) {
-	if(car(ptcptr)==g) return caddr(ptcptr);
-	p1=p; p=ptcptr;
-	ptcptr=cadr(ptcptr);
-    }
-    cadr(p1)=0;            /* remove the last one */
-    cadr(p) = ptr_cache;   /* connect current queue to the last one */
-    ptr_cache = p;         /* now the last one is the top */
-    if (!caddr(p)) {
-	if((r=get_register())) {
-	    caddr(p)=r; regs[r]=PTRC_REG;
-	} else {
-	    error(-1);
-	    r=creg; /* this can't happen */
-	}
-	car(p)=g;
-    } else {
-	r = caddr(p);
-    }
-    rrn = register_name(r);
+    char *rrn = register_name(r);
     if (nptr->sc==STATIC) {
 	printf("\taddis %s,r31,ha16(_%s-L_%d)\n",
 		 rrn,nptr->nm,code_base);
@@ -1100,7 +1034,6 @@
 	printf("\tlwz %s,lo16(L_%s$non_lazy_ptr-L_%d)(%s)\n",
 		 rrn,nptr->nm,code_base,rrn);
     }
-    return r;
 }
 
 static char *cload(int sz) { return sz==1?"lbz":sz==SIZE_OF_SHORT?"lhz":"lwz"; }
@@ -2970,8 +2903,8 @@
 emit_longlong(int e)
 {
 #if LONGLONG_CODE
+    long long d = lcadr(e);
     data_mode(0);
-    long long d = lcadr(e);
     printf("\t.long\t0x%x,0x%x\n",code_l2(d),code_l1(d));
 #endif
 }
@@ -2980,8 +2913,8 @@
 emit_double(int e)
 {
 #if FLOAT_CODE
+    double d = dcadr(e);
     data_mode(0);
-    double d = dcadr(e);
     printf("\t.long\t0x%x,0x%x\n",code_d2(d),code_d1(d));
 #endif
 }
@@ -2990,8 +2923,8 @@
 emit_float(int e)
 {
 #if FLOAT_CODE
+    float f = dcadr(e);
     data_mode(0);
-    float f = dcadr(e);
     printf("\t.long\t0x%x\n",*(int *)&f);
 #endif
 }
--- a/mc-code.h	Sat Jun 26 00:26:01 2004 +0900
+++ b/mc-code.h	Sat Jun 26 11:09:11 2004 +0900
@@ -52,6 +52,9 @@
 extern int get_input_register_var(int,NMTBL *,int);
 extern int get_input_dregister_var(int,NMTBL *,int,int);
 extern int get_input_lregister_var(int,NMTBL *,int);
+extern void code_ptr_cache_def(int r,NMTBL *nptr);
+extern void use_ptr_cache(int r);
+
 extern void emit_push();
 extern int emit_pop(int type);
 extern void gexpr_code_init();
--- a/mc-codegen.c	Sat Jun 26 00:26:01 2004 +0900
+++ b/mc-codegen.c	Sat Jun 26 11:09:11 2004 +0900
@@ -3531,4 +3531,107 @@
     return mask;
 }
 
+#define MAX_PTR_CACHE 10
+
+static int ptr_cache=0;
+static int ptr_cache_last=0;
+
+extern void
+init_ptr_cache()
+{
+    int i;
+    for(i=0;i<MAX_PTR_CACHE-1;i++) {
+	ptr_cache=glist3(0,ptr_cache,0);
+    }
+    ptr_cache_last=ptr_cache;
+}
+
+extern void
+clear_ptr_cache_reg(int r)
+{
+    int ptcptr=ptr_cache;
+    int prev = 0;
+    while(ptcptr!=ptr_cache_last) {
+	if(car(ptcptr)&&caddr(ptcptr)==r) {
+	    free_register(r); caddr(ptcptr)=-1; car(ptcptr)=0;
+	    // remove me
+	    if (prev) cadr(prev) = cadr(ptcptr);
+	    else ptr_cache = cadr(ptcptr);
+	    // add me to the next of ptr_cache_last
+	    cadr(ptcptr) = cadr(ptr_cache_last);
+	    cadr(ptr_cache_last) = ptcptr;
+	    return;
+	}
+	ptcptr=cadr(prev=ptcptr);
+    }
+}
+
+extern int
+last_ptr_cache()
+{
+    int ptcptr=ptr_cache;
+    int r = 0;
+    while(ptcptr!=ptr_cache_last) {
+	r = caddr(ptcptr);
+	ptcptr=cadr(ptcptr);
+    }
+    return r;
+}
+
+
+extern void
+clear_ptr_cache()
+{
+    int ptcptr=ptr_cache;
+    while(ptcptr!=ptr_cache_last) {
+	free_register(caddr(ptcptr)); caddr(ptcptr)=-1; car(ptcptr)=0;
+	ptcptr=cadr(ptcptr);
+    }
+    ptr_cache_last = ptr_cache;
+}
+
+
+extern int
+get_ptr_cache(NMTBL *nptr)
+{
+    int r;
+    int ptcptr=ptr_cache;
+    int g = (int)nptr;
+    int prev=0,p;
+
+    // linear search cache 
+    while(ptcptr!=ptr_cache_last) {
+	if(car(ptcptr)==g) {
+	    if (prev) {
+		// make this top
+		cadr(prev)=cadr(ptcptr);
+		cadr(ptcptr) = ptr_cache;
+		ptr_cache = ptcptr;
+	    }
+	    return caddr(ptcptr);
+	}
+	ptcptr=cadr(prev=ptcptr);
+    }
+    if (!cadr(ptr_cache_last)) {
+	// cache is full
+	if (prev) {
+	    // remove oldest cache and it becomes the last
+	    free_register(caddr(prev)); caddr(ptcptr)=-1; car(ptcptr)=0;
+	    ptr_cache_last = prev;
+	}
+	else error(-1);
+    }
+    r = get_register(); // some ptr cache may remove by this
+    caddr(p = cadr(ptr_cache_last)) = r;
+    car(p) = g;
+    use_ptr_cache(r);
+
+    cadr(ptr_cache_last) = cadr(p);
+    cadr(p) = ptr_cache;
+    ptr_cache = p;
+    code_ptr_cache_def(r,nptr);
+    return r;
+}
+
+
 /* end */
--- a/mc-codegen.h	Sat Jun 26 00:26:01 2004 +0900
+++ b/mc-codegen.h	Sat Jun 26 11:09:11 2004 +0900
@@ -85,5 +85,10 @@
 extern int scalar(int t);
 extern int make_mask(int from,int to);
 
+extern void init_ptr_cache();
+extern void clear_ptr_cache_reg(int r);
+extern int last_ptr_cache();
+extern void clear_ptr_cache() ;
+extern int get_ptr_cache(NMTBL *nptr);
 
 /* end */
--- a/rsyncs	Sat Jun 26 00:26:01 2004 +0900
+++ b/rsyncs	Sat Jun 26 11:09:11 2004 +0900
@@ -10,5 +10,5 @@
 rm -f mc-ia32
 make depend
 rm b*.s
-touch *.[hc] conv/*.[hc]
+touch *.pl *.[hc] conv/*.[hc]
 make diff ARCH=ia32
--- a/test/bitfield1.c	Sat Jun 26 00:26:01 2004 +0900
+++ b/test/bitfield1.c	Sat Jun 26 11:09:11 2004 +0900
@@ -193,6 +193,7 @@
 		cc.b.a,cc.b.b,cc.b.c,cc.b.d,cc.b.e,cc.b.f);
     }
     ll.a[0]=ll.a[1]=ll.a[2]=ll.a[3]= ll.a[4]=ll.a[5]=ll.a[6]=ll.a[7]=0;
+        ll.b.v=ll.b.w=ll.b.x = 0;
     for(i=7;i>=0;i--) {
     bit=1;
 	for(j=0;j<32;j++,bit<<=1) {