From 6150030e8b8ecd81f56aa6b8f06af663caf71fd5 Mon Sep 17 00:00:00 2001
From: Taylor R Campbell <campbell@mumble.net>
Date: Tue, 15 Jan 2019 03:17:35 +0000
Subject: [PATCH] Make RTL:CONSTANT-COST always return positive.

Otherwise CSE might substitute constants for registers where at best
it's not helpful and at worst we don't have rules for it.
---
 src/compiler/machines/aarch64/machine.scm | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/src/compiler/machines/aarch64/machine.scm b/src/compiler/machines/aarch64/machine.scm
index 01562ecf4..6548e90d4 100644
--- a/src/compiler/machines/aarch64/machine.scm
+++ b/src/compiler/machines/aarch64/machine.scm
@@ -397,18 +397,22 @@ USA.
       (error "Unknown interpreter register:" locative)))
 
 (define (rtl:constant-cost expression)
+  ;; Register reference costs 1.  If a cost is less than 1, CSE will
+  ;; always choose the constant over the register reference, which may
+  ;; not always work and is not always helpful even when it does work.
+  ;;
   ;; XXX Justify this by reference to cycle counts, &c.  This really
   ;; depends on which instruction we're talking about -- sometimes
   ;; immediates are cheaper.
-  (let ((cost:zero 0)
-        (cost:imm16 1)                  ;MOVZ/MOVN
-        (cost:imm32 2)                  ;MOVZ/MOVN + 1*MOVK
-        (cost:imm48 3)                  ;MOVZ/MOVN + 2*MOVK
-        (cost:imm64 4)                  ;MOVZ/MOVN + 3*MOVK
-        (cost:add 1)
-        (cost:adr 1)
+  (let ((cost:zero 1)
+        (cost:imm16 2)                  ;MOVZ/MOVN
+        (cost:imm32 3)                  ;MOVZ/MOVN + 1*MOVK
+        (cost:imm48 4)                  ;MOVZ/MOVN + 2*MOVK
+        (cost:imm64 5)                  ;MOVZ/MOVN + 3*MOVK
+        (cost:add 2)
+        (cost:adr 2)
         (cost:ldr 10)
-        (cost:bl 2))
+        (cost:bl 3))
     (define (immediate-cost immediate)
       (cond ((zero? immediate)
              cost:zero)
-- 
2.25.1