summary refs log tree commit diff
path: root/fold.c
diff options
context:
space:
mode:
authorQuentin Carbonneaux <quentin@c9x.me>2022-12-18 17:35:19 +0100
committerQuentin Carbonneaux <quentin@c9x.me>2022-12-25 16:37:33 +0100
commit5e9726946dcb9248dbd34ded1bdd4f7af8dc2d31 (patch)
tree9842f9837784911e386357af18b08a2ca1b69896 /fold.c
parentc5cd65261e05029889450ca27050785504164853 (diff)
downloadroux-5e9726946dcb9248dbd34ded1bdd4f7af8dc2d31.tar.gz
new UNDEF Ref
Crashing loads of uninitialized memory
proved to be a problem when implementing
unions using qbe.  This patch introduces
a new UNDEF Ref to represent data that is
known to be uninitialized.  Optimization
passes can make use of it to eliminate
some code.  In the last compilation stages,
UNDEF is treated as the constant 0xdeaddead.
Diffstat (limited to 'fold.c')
-rw-r--r--fold.c12
1 files changed, 7 insertions, 5 deletions
diff --git a/fold.c b/fold.c
index f992b3a..3ff0bc7 100644
--- a/fold.c
+++ b/fold.c
@@ -1,8 +1,8 @@
 #include "all.h"
 
 enum {
-	Bot = -2, /* lattice bottom */
-	Top = -1, /* lattice top */
+	Bot = -1, /* lattice bottom */
+	Top = 0,  /* lattice top (matches UNDEF) */
 };
 
 typedef struct Edge Edge;
@@ -126,7 +126,6 @@ visitjmp(Blk *b, int n, Fn *fn)
 	switch (b->jmp.type) {
 	case Jjnz:
 		l = latval(b->jmp.arg);
-		assert(l != Top && "ssa invariant broken");
 		if (l == Bot) {
 			edge[n][1].work = flowrk;
 			edge[n][0].work = &edge[n][1];
@@ -174,7 +173,6 @@ renref(Ref *r)
 
 	if (rtype(*r) == RTmp)
 		if ((l=val[r->val]) != Bot) {
-			assert(l != Top && "ssa invariant broken");
 			*r = CON(l);
 			return 1;
 		}
@@ -294,9 +292,13 @@ fold(Fn *fn)
 		for (i=b->ins; i<&b->ins[b->nins]; i++)
 			if (renref(&i->to))
 				*i = (Ins){.op = Onop};
-			else
+			else {
 				for (n=0; n<2; n++)
 					renref(&i->arg[n]);
+				if (isstore(i->op))
+				if (req(i->arg[0], UNDEF))
+					*i = (Ins){.op = Onop};
+			}
 		renref(&b->jmp.arg);
 		if (b->jmp.type == Jjnz && rtype(b->jmp.arg) == RCon) {
 				if (iscon(&fn->con[b->jmp.arg.val], 0, 0)) {