git: 9front

Download patch

ref: 78b4f3f1d6762f9f0f712211df4451cf3e16ef0e
parent: 453bcc2c16209430fa54ebdb116c7d626a9ed85b
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sun Feb 8 13:21:34 EST 2026

6c: implement storeprop peephole optimisation

This is ported from 7c, basically eliminates
loads of local variables/parameters right
after a store.

--- a/sys/src/cmd/6c/peep.c
+++ b/sys/src/cmd/6c/peep.c
@@ -1,5 +1,8 @@
 #include "gc.h"
 
+static void
+storeprop(int as, Adr *a, Adr *v, Reg *r);
+
 static int
 needc(Prog *p)
 {
@@ -100,8 +103,12 @@
 		case AMOVQ:
 		case AMOVSS:
 		case AMOVSD:
-			if(!regtyp(&p->to))
+			if(!regtyp(&p->to)){
+				/* registerize variable loads following stores */
+				if(regtyp(&p->from) && (p->to.type == D_AUTO || p->to.type == D_PARAM))
+					storeprop(p->as, &p->from, &p->to, r->s1);
 				break;
+			}
 			if(regtyp(&p->from)) {
 				if(copyprop(r)) {
 					excise(r);
@@ -991,4 +998,46 @@
 		return 0;
 	}
 	return 0;
+}
+
+/*
+ * Registerize loads from local variables:
+ *
+ * MOV a, v
+ * ... (a and v not touched)
+ * MOV v, b
+ * ----
+ * MOV a, v
+ * ... (a and v not touched)
+ * MOV a, b
+ */
+static void
+storeprop(int as, Adr *a, Adr *v, Reg *r)
+{
+	Prog *p;
+
+	for(; r != R; r = r->s1) {
+		if(uniqp(r) == R)
+			return;
+
+		p = r->prog;
+		if(as == p->as
+		&& copyas(&p->from, v)){
+			p->from = *a;
+			continue;
+		}
+
+		if(copyu(p, a, A) > 1)
+			return;
+
+		if(p->to.type >= D_INDIR)	/* might modify v */
+			return;
+
+		if(p->to.type == D_AUTO || p->to.type == D_PARAM)
+			if(copyas(&p->to, v))
+				return;
+
+		if(r->s2)
+			storeprop(as, a, v, r->s2);
+	}
 }
--