shithub: scc

Download patch

ref: 68036de468386ebd902f3341f006ac5e560d0740
parent: bb963053b66e9f3f8a62bbba26deb6028b174ba3
author: Roberto E. Vargas Caballero <[email protected]>
date: Tue Apr 26 15:59:28 EDT 2016

[cc2] Add general tree optimizations for jumps and labels

These commit adds:
	- Remove consecutive labels
	- Jump to jump
	- Jump to next instruction

--- a/cc2/optm.c
+++ b/cc2/optm.c
@@ -1,4 +1,6 @@
 
+#include <stddef.h>
+
 #include "arch.h"
 #include "cc2.h"
 
@@ -5,14 +7,38 @@
 Node *
 optm(Node *np)
 {
-	Node *dst;
+	Node *p, *dst, *next = np->next;
+	Symbol *sym, *osym;
 
 	switch (np->op) {
+	case ONOP:
+		if (next && next->op == ONOP) {
+			sym = np->u.sym;
+			osym = next->u.sym;
+			osym->id = sym->id;
+			osym->numid = sym->id;
+			osym->u.stmt = sym->u.stmt;
+			return NULL;
+		}
+		break;
 	case OJMP:
 	case OBRANCH:
-		dst = np->u.sym->u.stmt;
-		if (dst->op == OJMP)
+		for (;;) {
+			dst = np->u.sym->u.stmt;
+			if (dst->op != OJMP)
+				break;
 			np->u.sym = dst->u.sym;
+		}
+		for (p = np->next; p; p = p->next) {
+			if (p == dst)
+				return NULL;
+			if (p->op == ONOP ||
+			    p->op == OBLOOP ||
+			    p->op == OELOOP) {
+				continue;
+			}
+			break;
+		}
 		break;
 	}
 	return np;