summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--all.h3
-rw-r--r--main.c1
-rw-r--r--simpl.c46
4 files changed, 51 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index fc649f8..4040923 100644
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ ABI = sysv
 V = @
 OBJDIR = obj
 
-SRC = main.c util.c parse.c cfg.c mem.c ssa.c alias.c load.c copy.c fold.c live.c $(ABI).c isel.c spill.c rega.c emit.c
+SRC = main.c util.c parse.c cfg.c mem.c ssa.c alias.c load.c simpl.c copy.c fold.c live.c $(ABI).c isel.c spill.c rega.c emit.c
 OBJ = $(SRC:%.c=$(OBJDIR)/%.o)
 
 CFLAGS += -Wall -Wextra -std=c99 -g -pedantic
diff --git a/all.h b/all.h
index 128d16c..f518a36 100644
--- a/all.h
+++ b/all.h
@@ -588,6 +588,9 @@ void fillrpo(Fn *);
 void ssa(Fn *);
 void ssacheck(Fn *);
 
+/* simpl.c */
+void simpl(Fn *);
+
 /* copy.c */
 void copy(Fn *);
 
diff --git a/main.c b/main.c
index fe68ae0..1a8973f 100644
--- a/main.c
+++ b/main.c
@@ -59,6 +59,7 @@ func(Fn *fn)
 	loadopt(fn);
 	filluse(fn);
 	ssacheck(fn);
+	simpl(fn);
 	copy(fn);
 	filluse(fn);
 	fold(fn);
diff --git a/simpl.c b/simpl.c
new file mode 100644
index 0000000..384a8da
--- /dev/null
+++ b/simpl.c
@@ -0,0 +1,46 @@
+#include "all.h"
+
+static void
+elimext(Ins *i, int ext, Fn *fn)
+{
+	Tmp *t;
+	Use *u;
+
+	assert(rtype(i->to) == RTmp);
+	t = &fn->tmp[i->to.val];
+	for (u=t->use; u<&t->use[t->nuse]; u++)
+		if (u->type == UIns
+		&& u->u.ins->op == ext
+		&& (u->u.ins->cls == i->cls || i->cls == Kl)) {
+			u->u.ins->op = Ocopy;
+			elimext(u->u.ins, ext, fn);
+		}
+}
+
+/* requires & preserves uses */
+void
+simpl(Fn *fn)
+{
+	Blk *b;
+	Ins *i;
+	int ext;
+
+	for (b=fn->start; b; b=b->link)
+		for (i=b->ins; i<&b->ins[b->nins]; i++)
+			switch (i->op) {
+			case Oloadsb:
+			case Oloadub:
+			case Oloadsh:
+			case Oloaduh:
+				ext = Oextsb + (i->op - Oloadsb);
+				goto Elimext;
+			case Oextsb:
+			case Oextub:
+			case Oextsh:
+			case Oextuh:
+				ext = i->op;
+			Elimext:
+				elimext(i, ext, fn);
+				break;
+			}
+}