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/compiler/optimizing/constant_folding.cc b/compiler/optimizing/constant_folding.cc
index 0614945..5f39a49 100644
--- a/compiler/optimizing/constant_folding.cc
+++ b/compiler/optimizing/constant_folding.cc
@@ -47,6 +47,9 @@
  private:
   void VisitShift(HBinaryOperation* shift);
 
+  void VisitEqual(HEqual* instruction) OVERRIDE;
+  void VisitNotEqual(HNotEqual* instruction) OVERRIDE;
+
   void VisitAbove(HAbove* instruction) OVERRIDE;
   void VisitAboveOrEqual(HAboveOrEqual* instruction) OVERRIDE;
   void VisitBelow(HBelow* instruction) OVERRIDE;
@@ -140,6 +143,30 @@
   }
 }
 
+void InstructionWithAbsorbingInputSimplifier::VisitEqual(HEqual* instruction) {
+  if ((instruction->GetLeft()->IsNullConstant() && !instruction->GetRight()->CanBeNull()) ||
+      (instruction->GetRight()->IsNullConstant() && !instruction->GetLeft()->CanBeNull())) {
+    // Replace code looking like
+    //    EQUAL lhs, null
+    // where lhs cannot be null with
+    //    CONSTANT false
+    instruction->ReplaceWith(GetGraph()->GetConstant(Primitive::kPrimBoolean, 0));
+    instruction->GetBlock()->RemoveInstruction(instruction);
+  }
+}
+
+void InstructionWithAbsorbingInputSimplifier::VisitNotEqual(HNotEqual* instruction) {
+  if ((instruction->GetLeft()->IsNullConstant() && !instruction->GetRight()->CanBeNull()) ||
+      (instruction->GetRight()->IsNullConstant() && !instruction->GetLeft()->CanBeNull())) {
+    // Replace code looking like
+    //    NOT_EQUAL lhs, null
+    // where lhs cannot be null with
+    //    CONSTANT true
+    instruction->ReplaceWith(GetGraph()->GetConstant(Primitive::kPrimBoolean, 1));
+    instruction->GetBlock()->RemoveInstruction(instruction);
+  }
+}
+
 void InstructionWithAbsorbingInputSimplifier::VisitAbove(HAbove* instruction) {
   if (instruction->GetLeft()->IsConstant() &&
       instruction->GetLeft()->AsConstant()->IsArithmeticZero()) {