From 316b57e37eb10cc127526886f58f6cad24916bf1 Mon Sep 17 00:00:00 2001
From: Quentin Carbonneaux <quentin@c9x.me>
Date: Tue, 5 Feb 2019 11:48:20 +0100
Subject: [PATCH] new spiller heuristic for loops

If a variable is spilled in a loop, the
spiller now tries to keep it spilled over
the whole loop.

Thanks to Michael Forney for sharing a test
case exhibiting a pathological reload.
---
 spill.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/spill.c b/spill.c
index 77496f6..2f7ebb2 100644
--- a/spill.c
+++ b/spill.c
@@ -360,12 +360,17 @@ spill(Fn *fn)
 				bsunion(v, u);
 			}
 		} else if (s1) {
-			liveon(v, b, s1);
+			/* avoid reloading temporaries
+			 * in the middle of loops */
+			bszero(v);
+			liveon(w, b, s1);
+			if (s1->loop >= b->loop)
+				bsunion(v, w);
 			if (s2) {
 				liveon(u, b, s2);
-				bscopy(w, u);
-				bsinter(w, v);
-				bsunion(v, u);
+				if (s2->loop >= b->loop)
+					bsunion(v, u);
+				bsinter(w, u);
 			}
 			limit2(v, 0, 0, w);
 		} else {