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