From c63bdde13d9d2cd7f82fae590611e2ff7a77bc49 Mon Sep 17 00:00:00 2001 From: Santiago Aboy Solanes Date: Fri, 31 Mar 2023 16:04:22 +0100 Subject: Optimize String's length/isEmpty for constant strings If at compile time we know the length of the string, we can insert that constant into the graph. As part of this CL, I introduced VisitArrayLength in ConstantFolding. This is done because: * We are folding the instrucion into a constant so it makes logical sense, and * It plays better with isEmpty since that's an ArrayLength+Equal which can be dealt with in one ConstantFolding pass. As a note, there's VisitArrayLength in InstructionSimplifier which would make sense to also move to ConstantFolding. However, in doing so we get code size regressions. Added a TODO to see if we can deal with that in the future. Locally speed-compiling in a Pixel 5: * SystemServer: -4.38 KB (-0.01%) * SystemUIGoogle: -23.92KB (-0.09%) * AGSA: -217.66KB (-0.07%) Bug: 276416001 Test: art/test/testrunner/testrunner.py --host --64 --optimizing -b Change-Id: Ic2081d489482157b016762e0ec103319d3b9a46e --- compiler/optimizing/instruction_simplifier.cc | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'compiler/optimizing/instruction_simplifier.cc') diff --git a/compiler/optimizing/instruction_simplifier.cc b/compiler/optimizing/instruction_simplifier.cc index 12ca30fb1b..b772fbbbb6 100644 --- a/compiler/optimizing/instruction_simplifier.cc +++ b/compiler/optimizing/instruction_simplifier.cc @@ -1117,6 +1117,10 @@ void InstructionSimplifierVisitor::VisitIf(HIf* instruction) { } } +// TODO(solanes): This optimization should be in ConstantFolding since we are folding to a constant. +// However, we get code size regressions when we do that since we sometimes have a NullCheck between +// HArrayLength and IsNewArray, and said NullCheck is eliminated in InstructionSimplifier. If we run +// ConstantFolding and InstructionSimplifier in lockstep this wouldn't be an issue. void InstructionSimplifierVisitor::VisitArrayLength(HArrayLength* instruction) { HInstruction* input = instruction->InputAt(0); // If the array is a NewArray with constant size, replace the array length -- cgit v1.2.3-59-g8ed1b