xref: /aosp_15_r20/art/test/2257-checker-constant-folding-before-codegen/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2023 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         assertEquals(0, $noinline$testRemoveAbsAndReturnConstant());
20     }
21 
22     // After LSE we know that some values are 0, making the Abs operation redundant.
23 
24     /// CHECK-START: int Main.$noinline$testRemoveAbsAndReturnConstant() constant_folding$before_codegen (before)
25     /// CHECK:     Abs
26 
27     /// CHECK-START: int Main.$noinline$testRemoveAbsAndReturnConstant() constant_folding$before_codegen (after)
28     /// CHECK-NOT:  Abs
29 
30     // This enables DCE to know the return value at compile time.
31 
32     /// CHECK-START: int Main.$noinline$testRemoveAbsAndReturnConstant() dead_code_elimination$before_codegen (before)
33     /// CHECK:     <<ReturnPhi:i\d+>> Phi [<<Val1:i\d+>>,<<Val2:i\d+>>]
34     /// CHECK:     Return [<<ReturnPhi>>]
35 
36     /// CHECK-START: int Main.$noinline$testRemoveAbsAndReturnConstant() dead_code_elimination$before_codegen (after)
37     /// CHECK:     <<Const0:i\d+>> IntConstant 0
38     /// CHECK:     Return [<<Const0>>]
39 
$noinline$testRemoveAbsAndReturnConstant()40     private static int $noinline$testRemoveAbsAndReturnConstant() {
41         final int ARRAY_SIZE = 10;
42         int[] result = new int[ARRAY_SIZE];
43         int[] source = new int[ARRAY_SIZE];
44 
45         int value = 0;
46         for (int i = 0; i < ARRAY_SIZE; ++i) {
47             value += Math.abs(source[i]);
48             result[i] = value;
49         }
50         return value;
51     }
52 
assertEquals(int expected, int result)53     public static void assertEquals(int expected, int result) {
54         if (expected != result) {
55             throw new Error("Expected: " + expected + ", found: " + result);
56         }
57     }
58 }
59