Constant fold Equal/NotEqual between null and non-null.

Test: Add new test cases to 442-checker-constant-folding.
Test: m test-art-host
Change-Id: I14509d5e13d30a66b3c2ac3d76d514f58501c9ab
diff --git a/test/442-checker-constant-folding/src/Main.java b/test/442-checker-constant-folding/src/Main.java
index 33ef10b..64180d5 100644
--- a/test/442-checker-constant-folding/src/Main.java
+++ b/test/442-checker-constant-folding/src/Main.java
@@ -27,6 +27,12 @@
     }
   }
 
+  public static void assertTrue(boolean condition) {
+    if (!condition) {
+      throw new Error();
+    }
+  }
+
   public static void assertIntEquals(int expected, int result) {
     if (expected != result) {
       throw new Error("Expected: " + expected + ", found: " + result);
@@ -1322,6 +1328,58 @@
 
 
   /**
+   * Test optimizations of comparisons with null yielding a constant result.
+   */
+
+  /// CHECK-START: boolean Main.ConstStringEqualsNull() constant_folding$after_inlining (before)
+  /// CHECK-DAG:     <<ConstStr:l\d+>> LoadString
+  /// CHECK-DAG:     <<Null:l\d+>>     NullConstant
+  /// CHECK-DAG:     <<Eq:z\d+>>       Equal [<<ConstStr>>,<<Null>>]
+  /// CHECK-DAG:                       If [<<Eq>>]
+
+  /// CHECK-START: boolean Main.ConstStringEqualsNull() constant_folding$after_inlining (after)
+  /// CHECK-DAG:     <<False:i\d+>>    IntConstant 0
+  /// CHECK-DAG:                       If [<<False>>]
+
+  /// CHECK-START: boolean Main.ConstStringEqualsNull() constant_folding$after_inlining (after)
+  /// CHECK-NOT:                       Equal
+
+  public static boolean ConstStringEqualsNull() {
+    // Due to Jack emitting code using the opposite condition, use != to generate Equal.
+    if ($inline$ConstString() != null) {
+      return false;
+    } else {
+      return true;
+    }
+  }
+
+  /// CHECK-START: boolean Main.ConstStringNotEqualsNull() constant_folding$after_inlining (before)
+  /// CHECK-DAG:     <<ConstStr:l\d+>> LoadString
+  /// CHECK-DAG:     <<Null:l\d+>>     NullConstant
+  /// CHECK-DAG:     <<Ne:z\d+>>       NotEqual [<<ConstStr>>,<<Null>>]
+  /// CHECK-DAG:                       If [<<Ne>>]
+
+  /// CHECK-START: boolean Main.ConstStringNotEqualsNull() constant_folding$after_inlining (after)
+  /// CHECK-DAG:     <<True:i\d+>>     IntConstant 1
+  /// CHECK-DAG:                       If [<<True>>]
+
+  /// CHECK-START: boolean Main.ConstStringNotEqualsNull() constant_folding$after_inlining (after)
+  /// CHECK-NOT:                       NotEqual
+
+  public static boolean ConstStringNotEqualsNull() {
+    // Due to Jack emitting code using the opposite condition, use == to generate NotEqual.
+    if ($inline$ConstString() == null) {
+      return false;
+    } else {
+      return true;
+    }
+  }
+
+  public static String $inline$ConstString() {
+    return "";
+  }
+
+  /**
    * Exercise constant folding on type conversions.
    */
 
@@ -1601,6 +1659,9 @@
     assertFalse(CmpFloatGreaterThanNaN(arbitrary));
     assertFalse(CmpDoubleLessThanNaN(arbitrary));
 
+    assertFalse(ConstStringEqualsNull());
+    assertTrue(ConstStringNotEqualsNull());
+
     Main main = new Main();
     assertIntEquals(1, main.smaliCmpLongConstants());
     assertIntEquals(-1, main.smaliCmpGtFloatConstants());