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   /// CHECK-START: int Main.inlineIfThenElse() inliner (before)
20   /// CHECK-DAG:     <<Invoke:i\d+>>  InvokeStaticOrDirect
21   /// CHECK-DAG:                      Return [<<Invoke>>]
22 
23   /// CHECK-START: int Main.inlineIfThenElse() inliner (after)
24   /// CHECK-NOT:                      InvokeStaticOrDirect
25 
inlineIfThenElse()26   public static int inlineIfThenElse() {
27     return foo(true);
28   }
29 
foo(boolean value)30   private static int foo(boolean value) {
31     if (value) {
32       return 1;
33     } else {
34       return 0;
35     }
36   }
37 
38   /// CHECK-START: int Main.inlineInLoop() inliner (before)
39   /// CHECK-DAG:     InvokeStaticOrDirect
40 
41   /// CHECK-START: int Main.inlineInLoop() inliner (after)
42   /// CHECK-NOT:     InvokeStaticOrDirect
43 
inlineInLoop()44   public static int inlineInLoop() {
45     int result = 0;
46     for (int i = 0; i < 32; ++i) {
47       result += foo(i % 2 == 0);
48     }
49     return result;
50   }
51 
52   /// CHECK-START: int Main.inlineInLoopHeader() inliner (before)
53   /// CHECK-DAG:     InvokeStaticOrDirect
54 
55   /// CHECK-START: int Main.inlineInLoopHeader() inliner (after)
56   /// CHECK-NOT:     InvokeStaticOrDirect
57 
inlineInLoopHeader()58   public static int inlineInLoopHeader() {
59     int result = 0;
60     for (int i = 0; i < foo(i % 2 == 0); ++i) {
61       result += 42;
62     }
63     return result;
64   }
65 
main(String[] args)66   public static void main(String[] args) {
67     if (inlineIfThenElse() != 1) {
68       throw new Error("Expected 1");
69     }
70     if (inlineInLoop() != 16) {
71       throw new Error("Expected 16");
72     }
73     if (inlineInLoopHeader() != 42) {
74       throw new Error("Expected 16");
75     }
76   }
77 }
78