From a341f352ca944d848ebf39b54d0ad353ff9ebaff Mon Sep 17 00:00:00 2001 From: Vladimir Marko Date: Wed, 31 Aug 2016 12:18:20 +0100 Subject: 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 --- compiler/optimizing/constant_folding.cc | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'compiler/optimizing') diff --git a/compiler/optimizing/constant_folding.cc b/compiler/optimizing/constant_folding.cc index 0614945ddc..5f39a49d68 100644 --- a/compiler/optimizing/constant_folding.cc +++ b/compiler/optimizing/constant_folding.cc @@ -47,6 +47,9 @@ class InstructionWithAbsorbingInputSimplifier : public HGraphVisitor { 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::VisitShift(HBinaryOperation* instr } } +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()) { -- cgit v1.2.3-59-g8ed1b