1 /* 2 * Copyright (C) 2014 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 { 18 19 private static int mX = 2; 20 private static int mY = -3; 21 22 public static void main(String[] args) { 23 System.out.println(foo(3, 4)); 24 System.out.println(mulAndIntrinsic()); 25 System.out.println(directIntrinsic(-5)); 26 } 27 28 /// CHECK-START: int Main.foo(int, int) GVN (before) 29 /// CHECK: Add 30 /// CHECK: Add 31 /// CHECK: Add 32 33 /// CHECK-START: int Main.foo(int, int) GVN (after) 34 /// CHECK: Add 35 /// CHECK: Add 36 /// CHECK-NOT: Add 37 38 public static int foo(int x, int y) { 39 int sum1 = x + y; 40 int sum2 = y + x; 41 return sum1 + sum2; 42 } 43 44 /// CHECK-START: int Main.mulAndIntrinsic() GVN (before) 45 /// CHECK: StaticFieldGet 46 /// CHECK: StaticFieldGet 47 /// CHECK: Mul 48 /// CHECK: InvokeStaticOrDirect 49 /// CHECK: StaticFieldGet 50 /// CHECK: StaticFieldGet 51 /// CHECK: Mul 52 /// CHECK: Add 53 54 /// CHECK-START: int Main.mulAndIntrinsic() GVN (after) 55 /// CHECK: StaticFieldGet 56 /// CHECK: StaticFieldGet 57 /// CHECK: Mul 58 /// CHECK: InvokeStaticOrDirect 59 /// CHECK-NOT: StaticFieldGet 60 /// CHECK-NOT: StaticFieldGet 61 /// CHECK-NOT: Mul 62 /// CHECK: Add 63 64 public static int mulAndIntrinsic() { 65 // The intermediate call to abs() does not kill 66 // the common subexpression on the multiplication. 67 int mul1 = mX * mY; 68 int abs = Math.abs(mul1); 69 int mul2 = mY * mX; 70 return abs + mul2; 71 } 72 73 /// CHECK-START: int Main.directIntrinsic(int) GVN (before) 74 /// CHECK: InvokeStaticOrDirect 75 /// CHECK: InvokeStaticOrDirect 76 /// CHECK: Add 77 78 /// CHECK-START: int Main.directIntrinsic(int) GVN (after) 79 /// CHECK: InvokeStaticOrDirect 80 /// CHECK-NOT: InvokeStaticOrDirect 81 /// CHECK: Add 82 83 public static int directIntrinsic(int x) { 84 // Here, the two calls to abs() themselves can be replaced with just one. 85 int abs1 = Math.abs(x); 86 int abs2 = Math.abs(x); 87 return abs1 + abs2; 88 } 89 90 } 91