Use rematerializable candidate with short live range, if no alternative

Bug: b/280904554
Change-Id: I40782eeede946678c21544454d675329634d21be
diff --git a/src/main/java/com/android/tools/r8/ir/regalloc/LinearScanRegisterAllocator.java b/src/main/java/com/android/tools/r8/ir/regalloc/LinearScanRegisterAllocator.java
index 90c6c2b..093ff83 100644
--- a/src/main/java/com/android/tools/r8/ir/regalloc/LinearScanRegisterAllocator.java
+++ b/src/main/java/com/android/tools/r8/ir/regalloc/LinearScanRegisterAllocator.java
@@ -2127,15 +2127,18 @@
       int otherCandidate =
           getLargestValidCandidate(
               unhandledInterval, registerConstraint, needsRegisterPair, usePositions, Type.OTHER);
-      if (otherCandidate == Integer.MAX_VALUE || candidate == REGISTER_CANDIDATE_NOT_FOUND) {
-        candidate = otherCandidate;
-      } else {
-        int largestConstUsePosition =
-            getLargestPosition(usePositions, candidate, needsRegisterPair);
-        if (largestConstUsePosition - MIN_CONSTANT_FREE_FOR_POSITIONS <
-            unhandledInterval.getStart()) {
-          // The candidate that can be rematerialized has a live range too short to use it.
+      if (otherCandidate != REGISTER_CANDIDATE_NOT_FOUND) {
+        // There is a potential other candidate, check if that should be used instead.
+        if (otherCandidate == Integer.MAX_VALUE || candidate == REGISTER_CANDIDATE_NOT_FOUND) {
           candidate = otherCandidate;
+        } else {
+          int largestConstUsePosition =
+              getLargestPosition(usePositions, candidate, needsRegisterPair);
+          if (largestConstUsePosition - MIN_CONSTANT_FREE_FOR_POSITIONS
+              < unhandledInterval.getStart()) {
+            // The candidate that can be rematerialized has a live range too short to use it.
+            candidate = otherCandidate;
+          }
         }
       }