Fix bug in shortening of live ranges.

The old code would incorrectly allow the placement of a constant
after a throwing instruction if we could deduce that the
instruction would not throw. However, that makes the constant
unavailable in the exception handler.

R=sgjesse@google.com

Bug: 65104300
Change-Id: Ief5fcb78d163e06a32529441cd5bbaece2554ca9
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java b/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
index 5c1114b..454d6cd 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
@@ -1251,11 +1251,10 @@
     InstructionListIterator insertAt = block.listIterator();
     // Place the instruction as late in the block as we can. It needs to go before users
     // and if we have catch handlers it needs to be placed before the throwing instruction.
-    insertAt.nextUntil(i -> {
-      return i.inValues().contains(instruction.outValue())
-          || i.isJumpInstruction()
-          || (hasCatchHandlers && i.instructionInstanceCanThrow());
-    });
+    insertAt.nextUntil(i ->
+        i.inValues().contains(instruction.outValue())
+        || i.isJumpInstruction()
+        || (hasCatchHandlers && i.instructionTypeCanThrow()));
     insertAt.previous();
     insertAt.add(instruction);
   }
diff --git a/src/test/examples/regress_65104300/Regress.java b/src/test/examples/regress_65104300/Regress.java
new file mode 100644
index 0000000..05a4333
--- /dev/null
+++ b/src/test/examples/regress_65104300/Regress.java
@@ -0,0 +1,20 @@
+// 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 regress_65104300;
+
+public class Regress {
+  // We correctly deduce that the array put cannot throw. However, we had a bug
+  // where we did not remove the handler but we allowed lowering the const 0 below
+  // the array put which makes it unavailable in the handler block.
+  public static void main(String[] args) {
+    Object[] objects = new Object[10];
+    Object o = new Object();
+    try {
+      objects[4] = o;
+      System.out.println(0);
+    } catch (Exception e) {
+      System.out.println(0);
+    }
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/R8RunExamplesTest.java b/src/test/java/com/android/tools/r8/R8RunExamplesTest.java
index 1676731..c8561b8 100644
--- a/src/test/java/com/android/tools/r8/R8RunExamplesTest.java
+++ b/src/test/java/com/android/tools/r8/R8RunExamplesTest.java
@@ -108,6 +108,7 @@
         "regress_37955340.Regress",
         "regress_62300145.Regress",
         "regress_64881691.Regress",
+        "regress_65104300.Regress",
         "memberrebinding2.Memberrebinding",
         "memberrebinding3.Memberrebinding",
         "minification.Minification",