From 854ecd40092a33184aaa20b0bfbc023c7df6a3d0 Mon Sep 17 00:00:00 2001
From: Quentin Carbonneaux <quentin.carbonneaux@yale.edu>
Date: Mon, 27 Jul 2015 14:57:56 -0400
Subject: [PATCH] add crippled dce to the allocator

---
 lisc/lisc.h  |  1 +
 lisc/parse.c |  1 +
 lisc/rega.c  | 25 ++++++++++++++++++-------
 3 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/lisc/lisc.h b/lisc/lisc.h
index 9031ede..f3b9913 100644
--- a/lisc/lisc.h
+++ b/lisc/lisc.h
@@ -88,6 +88,7 @@ enum {
 	OStore,
 	OLoad,
 	/* reserved instructions */
+	ONop,
 	OCopy,
 	OSwap,
 	OIACltd,
diff --git a/lisc/parse.c b/lisc/parse.c
index 768fc8e..77bde86 100644
--- a/lisc/parse.c
+++ b/lisc/parse.c
@@ -18,6 +18,7 @@ OpDesc opdesc[OLast] = {
 	[ORem]    = { 2, 0, "rem" },
 	[OStore]  = { 2, 0, "store" },
 	[OLoad]   = { 1, 0, "load" },
+	[ONop]    = { 0, 0, "nop" },
 	[OCopy]   = { 1, 0, "copy" },
 	[OSwap]   = { 2, 1, "swap" },
 	[OIADiv]  = { 1, 0, "iadiv" },
diff --git a/lisc/rega.c b/lisc/rega.c
index 3c8c4bd..b1731b8 100644
--- a/lisc/rega.c
+++ b/lisc/rega.c
@@ -256,7 +256,7 @@ rega(Fn *fn)
 		/* process instructions */
 		end[n] = cur;
 		if (debug['R']) {
-			fprintf(stderr, "\tend %-10s ", b->name);
+			fprintf(stderr, "\t%-10s end", b->name);
 			mdump(&cur);
 		}
 		if (rtype(b->jmp.arg) == RSym)
@@ -270,8 +270,11 @@ rega(Fn *fn)
 			}
 			if (!req(i->to, R)) {
 				r = rfree(&cur, i->to.val);
-				if (r)
-					i->to = SYM(r);
+				if (!r) {
+					*i = (Ins){ONop, R, {R, R}};
+					continue;
+				}
+				i->to = SYM(r);
 			}
 			if (rtype(i->arg[0]) == RSym) {
 				/* <arch>
@@ -301,9 +304,13 @@ rega(Fn *fn)
 			}
 		}
 		if (debug['R']) {
-			fprintf(stderr, "\tbeg %-10s ", b->name);
+			fprintf(stderr, "\t           beg");
 			mdump(&cur);
 		}
+		b->in = cur.bt;
+		for (p=b->phi; p; p=p->link)
+			if (rtype(p->to) == RSym)
+				BCLR(b->in, p->to.val);
 		beg[n] = cur;
 	}
 	if (debug['R'])
@@ -318,14 +325,18 @@ rega(Fn *fn)
 			for (p=s->phi; p; p=p->link) {
 				assert(rtype(p->to) == RSlot
 					|| rtype(p->to) == RSym);
+				dst = p->to;
+				if (rtype(dst) == RSym) {
+					r = rfind(&beg[s->id], dst.val);
+					if (!r)
+						continue;
+					dst = SYM(r);
+				}
 				for (a=0; p->blk[a]!=b; a++)
 					assert(a+1 < p->narg);
-				dst = p->to;
 				src = p->arg[a];
 				if (rtype(src) == RSym)
 					src = rref(&end[b->id], src.val);
-				if (rtype(dst) == RSym)
-					dst = rref(&beg[s->id], dst.val);
 				pmadd(src, dst);
 			}
 			for (t=Tmp0; t<fn->ntmp; t++)