shithub: riscv

Download patch

ref: d602374635334d3bc02e108c1cf14604e6e7854a
parent: a3c75553b4c50247ce22a4da4d2a87c10d5b0835
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Mon Sep 11 09:53:53 EDT 2023

7c: replace {CMP $0,…; Bcond} with CB[N]Z/TB[N]Z when possible

--- a/sys/src/cmd/7c/peep.c
+++ b/sys/src/cmd/7c/peep.c
@@ -128,6 +128,12 @@
 	case ACMPW:
 	case AFCMPS:
 	case AFCMPD:
+	case ACBZ:
+	case ACBZW:
+	case ACBNZ:
+	case ACBNZW:
+	case ATBZ:
+	case ATBNZ:
 
 	case AB:
 	case ABL:
@@ -1494,6 +1500,12 @@
 	case ACMPW:
 	case ACMN:
 	case ACMNW:
+	case ACBZ:
+	case ACBZW:
+	case ACBNZ:
+	case ACBNZW:
+	case ATBZ:
+	case ATBNZ:
 		if(s != A) {
 			if(copysub(&p->from, v, s, 1))
 				return 1;
@@ -1562,6 +1574,12 @@
 	case ACMPW:
 	case ACMN:
 	case ACMNW:
+	case ACBZ:
+	case ACBZW:
+	case ACBNZ:
+	case ACBNZW:
+	case ATBZ:
+	case ATBNZ:
 
 	case AADD:
 	case AADDW:
--- a/sys/src/cmd/7c/txt.c
+++ b/sys/src/cmd/7c/txt.c
@@ -986,6 +986,35 @@
 		print("%P\n", p);
 }
 
+static int
+zcmp(Prog *p, int as)
+{
+	if(p->from.type != D_CONST || p->from.offset != 0)
+		return 0;
+	switch(p->as){
+	case ACMP:
+		p->as = as;
+		if(as == ATBZ || as == ATBNZ){
+			p->from.offset = 63;
+			return 1;
+		}
+		break;
+	case ACMPW:
+		p->as = as;
+		if(as == ATBZ || as == ATBNZ){
+			p->from.offset = 31;
+			return 1;
+		}
+		break;
+	default:
+		return 0;
+	}
+	p->from.type = D_REG;
+	p->from.reg = p->reg;
+	p->reg = NREG;
+	return 1;
+}
+
 void
 gopcode(int o, Node *f1, Node *f2, Node *t)
 {
@@ -1186,12 +1215,18 @@
 		raddr(f2, p);
 		switch(o) {
 		case OEQ:
+			if(zcmp(p, a == ACMP ? ACBZ : ACBZW))
+				goto done;
 			a = ABEQ;
 			break;
 		case ONE:
+			if(zcmp(p, a == ACMP ? ACBNZ : ACBNZW))
+				goto done;
 			a = ABNE;
 			break;
 		case OLT:
+			if(zcmp(p, ATBNZ))
+				goto done;
 			a = ABLT;
 			/* ensure NaN comparison is always false */
 			if(typefd[et] && !true)
@@ -1203,6 +1238,8 @@
 				a = ABLS;
 			break;
 		case OGE:
+			if(zcmp(p, ATBZ))
+				goto done;
 			a = ABGE;
 			if(typefd[et] && true)
 				a = ABPL;
@@ -1216,6 +1253,8 @@
 			a = ABLO;
 			break;
 		case OLS:
+			if(zcmp(p, a == ACMP ? ACBZ : ACBZW))
+				goto done;
 			a = ABLS;
 			break;
 		case OHS:
@@ -1222,6 +1261,8 @@
 			a = ABHS;
 			break;
 		case OHI:
+			if(zcmp(p, a == ACMP ? ACBNZ : ACBNZW))
+				goto done;
 			a = ABHI;
 			break;
 		}
@@ -1241,6 +1282,7 @@
 	}
 	if(t != Z)
 		naddr(t, &p->to);
+done:
 	if(debug['g'])
 		print("%P\n", p);
 }