Merge "Run Android N tests with D8 and R8"
diff --git a/build.gradle b/build.gradle
index b279231..4caf5fa 100644
--- a/build.gradle
+++ b/build.gradle
@@ -306,7 +306,7 @@
 }
 
 task createJctfTests(type: Exec) {
-    def outputDir = "build/generated/test/java/com/android/tools/r8/art"
+    def outputDir = "build/generated/test/java/com/android/tools/r8/jctf"
     def script = "scripts/create-jctf-tests.sh"
     inputs.file script
     outputs.dir outputDir
@@ -660,10 +660,10 @@
     }
     // TODO(tamaskenez) enable jctf on all_tests when consolidated
     if (!project.hasProperty('jctf') && !project.hasProperty('only_jctf')) {
-        exclude "com/android/tools/r8/art/jctf/**"
+        exclude "com/android/tools/r8/jctf/**"
     }
     if (project.hasProperty('only_jctf')) {
-        include "com/android/tools/r8/art/jctf/**"
+        include "com/android/tools/r8/jctf/**"
     }
     if (project.hasProperty('jctf_compile_only')) {
         println "JCTF: compiling only"
diff --git a/scripts/create-jctf-tests.sh b/scripts/create-jctf-tests.sh
index 538f3c3..2f4e91e 100755
--- a/scripts/create-jctf-tests.sh
+++ b/scripts/create-jctf-tests.sh
@@ -26,7 +26,7 @@
 // Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
-package com.android.tools.r8.art.jctf.${compilerUnderTest}.${relativePackage};
+package com.android.tools.r8.jctf.${compilerUnderTest}.${relativePackage};
 
 import org.junit.Test;
 import com.android.tools.r8.R8RunArtTestsTest;
@@ -57,14 +57,14 @@
 }
 
 JCTFROOT="third_party/jctf"
-DESTINATIONDIR="build/generated/test/java/com/android/tools/r8/art"
+DESTINATIONDIR="build/generated/test/java/com/android/tools/r8/jctf"
 
 if [ ! -e $JCTFROOT ]; then
   echo "Missing jctf tests in $JCTFROOT."
   exit
 fi
 
-for d in $DESTINATIONDIR/jctf/r8 $DESTINATIONDIR/jctf/d8; do
+for d in $DESTINATIONDIR/r8 $DESTINATIONDIR/d8; do
   rm -rf $d
   mkdir -p $d
 done
@@ -86,10 +86,10 @@
   RELATIVE_PACKAGE=$(expr "$PACKAGE" : ".*\.java\.\(.*$\)")
 
   generate_test $PACKAGE.$TESTNAME $TESTCLASSNAME \
-    "$DESTINATIONDIR/jctf/r8/$RELATIVE_TEST_PATH" r8 \
+    "$DESTINATIONDIR/r8/$RELATIVE_TEST_PATH" r8 \
     $CLASSFILE $RELATIVE_PACKAGE
   generate_test $PACKAGE.$TESTNAME $TESTCLASSNAME \
-    "$DESTINATIONDIR/jctf/d8/$RELATIVE_TEST_PATH" d8 \
+    "$DESTINATIONDIR/d8/$RELATIVE_TEST_PATH" d8 \
     $CLASSFILE $RELATIVE_PACKAGE
 done
 
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 e79abc1..f2d039d 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
@@ -470,7 +470,7 @@
     return realRegisterNumberFromAllocated(intervalsRegister);
   }
 
-  int realRegisterNumberFromAllocated(int allocated) {
+  int unadjustedRealRegisterFromAllocated(int allocated) {
     assert allocated != NO_REGISTER;
     assert allocated >= 0;
     int register;
@@ -484,6 +484,11 @@
       // For everything else use the lower numbers.
       register = allocated - numberOfArgumentRegisters - NUMBER_OF_SENTINEL_REGISTERS;
     }
+    return register;
+  }
+
+  int realRegisterNumberFromAllocated(int allocated) {
+    int register = unadjustedRealRegisterFromAllocated(allocated);
     // Adjust for spill registers that turn out to be unused because the value can be
     // rematerialized instead of spilled.
     if (unusedRegisters != null) {
diff --git a/src/main/java/com/android/tools/r8/ir/regalloc/LiveIntervals.java b/src/main/java/com/android/tools/r8/ir/regalloc/LiveIntervals.java
index f1d3b9e..c966813 100644
--- a/src/main/java/com/android/tools/r8/ir/regalloc/LiveIntervals.java
+++ b/src/main/java/com/android/tools/r8/ir/regalloc/LiveIntervals.java
@@ -89,8 +89,12 @@
     }
     // If one of the non-spilled splits uses a register that is higher than U8BIT_MAX we cannot
     // rematerialize it using a ConstNumber instruction and we use spill moves instead of
-    // rematerialization.
-    int max = registerAllocator.realRegisterNumberFromAllocated(getMaxNonSpilledRegister());
+    // rematerialization. We use this check both before and after we have computed the set
+    // of unused registers. We therefore have to be careful to use the same max number for
+    // these computations. We use the unadjusted real register number to make sure that
+    // isRematerializable for the same intervals does not change from one phase of
+    // compilation to the next.
+    int max = registerAllocator.unadjustedRealRegisterFromAllocated(getMaxNonSpilledRegister());
     return max < Constants.U8BIT_MAX;
   }