1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2024 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker public class Main { main(String[] args)18*795d594fSAndroid Build Coastguard Worker public static void main(String[] args) { 19*795d594fSAndroid Build Coastguard Worker $noinline$testReturnValueOrDefault(); 20*795d594fSAndroid Build Coastguard Worker } 21*795d594fSAndroid Build Coastguard Worker 22*795d594fSAndroid Build Coastguard Worker private class InnerObject { 23*795d594fSAndroid Build Coastguard Worker int value; 24*795d594fSAndroid Build Coastguard Worker } 25*795d594fSAndroid Build Coastguard Worker 26*795d594fSAndroid Build Coastguard Worker static InnerObject inner_obj; 27*795d594fSAndroid Build Coastguard Worker 28*795d594fSAndroid Build Coastguard Worker // GVN removes the second `inner_obj` get. 29*795d594fSAndroid Build Coastguard Worker /// CHECK-START: int Main.$noinline$returnValueOrDefault() GVN (before) 30*795d594fSAndroid Build Coastguard Worker /// CHECK: StaticFieldGet field_name:Main.inner_obj 31*795d594fSAndroid Build Coastguard Worker /// CHECK: StaticFieldGet field_name:Main.inner_obj 32*795d594fSAndroid Build Coastguard Worker 33*795d594fSAndroid Build Coastguard Worker /// CHECK-START: int Main.$noinline$returnValueOrDefault() GVN (after) 34*795d594fSAndroid Build Coastguard Worker /// CHECK: StaticFieldGet field_name:Main.inner_obj 35*795d594fSAndroid Build Coastguard Worker /// CHECK-NOT: StaticFieldGet field_name:Main.inner_obj 36*795d594fSAndroid Build Coastguard Worker 37*795d594fSAndroid Build Coastguard Worker // Consistency check: No BoundTypes existed before. 38*795d594fSAndroid Build Coastguard Worker /// CHECK-START: int Main.$noinline$returnValueOrDefault() reference_type_propagation$after_gvn (before) 39*795d594fSAndroid Build Coastguard Worker /// CHECK-NOT: BoundType 40*795d594fSAndroid Build Coastguard Worker 41*795d594fSAndroid Build Coastguard Worker // Consistency check: Only one null check. 42*795d594fSAndroid Build Coastguard Worker /// CHECK-START: int Main.$noinline$returnValueOrDefault() reference_type_propagation$after_gvn (before) 43*795d594fSAndroid Build Coastguard Worker /// CHECK: NullCheck 44*795d594fSAndroid Build Coastguard Worker /// CHECK-NOT: NullCheck 45*795d594fSAndroid Build Coastguard Worker 46*795d594fSAndroid Build Coastguard Worker // RTP will add a BoundType between the `StaticFieldGet` and `NullCheck`. 47*795d594fSAndroid Build Coastguard Worker 48*795d594fSAndroid Build Coastguard Worker /// CHECK-START: int Main.$noinline$returnValueOrDefault() reference_type_propagation$after_gvn (before) 49*795d594fSAndroid Build Coastguard Worker /// CHECK: <<SFG:l\d+>> StaticFieldGet field_name:Main.inner_obj 50*795d594fSAndroid Build Coastguard Worker /// CHECK: NullCheck [<<SFG>>] 51*795d594fSAndroid Build Coastguard Worker 52*795d594fSAndroid Build Coastguard Worker /// CHECK-START: int Main.$noinline$returnValueOrDefault() reference_type_propagation$after_gvn (after) 53*795d594fSAndroid Build Coastguard Worker /// CHECK: <<SFG:l\d+>> StaticFieldGet field_name:Main.inner_obj 54*795d594fSAndroid Build Coastguard Worker /// CHECK: <<BT:l\d+>> BoundType [<<SFG>>] 55*795d594fSAndroid Build Coastguard Worker /// CHECK: NullCheck [<<BT>>] 56*795d594fSAndroid Build Coastguard Worker 57*795d594fSAndroid Build Coastguard Worker // Finally, InstructionSimplifier can remove the NullCheck 58*795d594fSAndroid Build Coastguard Worker 59*795d594fSAndroid Build Coastguard Worker /// CHECK-START: int Main.$noinline$returnValueOrDefault() instruction_simplifier$after_gvn (before) 60*795d594fSAndroid Build Coastguard Worker /// CHECK: NullCheck 61*795d594fSAndroid Build Coastguard Worker /// CHECK-NOT: NullCheck 62*795d594fSAndroid Build Coastguard Worker 63*795d594fSAndroid Build Coastguard Worker /// CHECK-START: int Main.$noinline$returnValueOrDefault() instruction_simplifier$after_gvn (after) 64*795d594fSAndroid Build Coastguard Worker /// CHECK-NOT: NullCheck $noinline$returnValueOrDefault()65*795d594fSAndroid Build Coastguard Worker static int $noinline$returnValueOrDefault() { 66*795d594fSAndroid Build Coastguard Worker if (inner_obj != null) { 67*795d594fSAndroid Build Coastguard Worker return inner_obj.value; 68*795d594fSAndroid Build Coastguard Worker } else { 69*795d594fSAndroid Build Coastguard Worker return -123; 70*795d594fSAndroid Build Coastguard Worker } 71*795d594fSAndroid Build Coastguard Worker } 72*795d594fSAndroid Build Coastguard Worker $noinline$testReturnValueOrDefault()73*795d594fSAndroid Build Coastguard Worker static void $noinline$testReturnValueOrDefault() { 74*795d594fSAndroid Build Coastguard Worker inner_obj = null; 75*795d594fSAndroid Build Coastguard Worker $noinline$assertIntEquals(-123, $noinline$returnValueOrDefault()); 76*795d594fSAndroid Build Coastguard Worker 77*795d594fSAndroid Build Coastguard Worker Main m = new Main(); 78*795d594fSAndroid Build Coastguard Worker inner_obj = m.new InnerObject(); 79*795d594fSAndroid Build Coastguard Worker inner_obj.value = 0; 80*795d594fSAndroid Build Coastguard Worker $noinline$assertIntEquals(0, $noinline$returnValueOrDefault()); 81*795d594fSAndroid Build Coastguard Worker 82*795d594fSAndroid Build Coastguard Worker inner_obj.value = 1000; 83*795d594fSAndroid Build Coastguard Worker $noinline$assertIntEquals(1000, $noinline$returnValueOrDefault()); 84*795d594fSAndroid Build Coastguard Worker } 85*795d594fSAndroid Build Coastguard Worker $noinline$assertIntEquals(int expected, int result)86*795d594fSAndroid Build Coastguard Worker public static void $noinline$assertIntEquals(int expected, int result) { 87*795d594fSAndroid Build Coastguard Worker if (expected != result) { 88*795d594fSAndroid Build Coastguard Worker throw new Error("Expected: " + expected + ", found: " + result); 89*795d594fSAndroid Build Coastguard Worker } 90*795d594fSAndroid Build Coastguard Worker } 91*795d594fSAndroid Build Coastguard Worker } 92