1 /*
2  * Copyright (C) 2015 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 
$inline$method()19   public static int $inline$method() {
20     return 5;
21   }
22 
23   /// CHECK-START: int Main.wholeSwitchDead(int) dead_code_elimination$after_inlining (before)
24   /// CHECK-DAG:                      PackedSwitch
25 
26   /// CHECK-START: int Main.wholeSwitchDead(int) dead_code_elimination$after_inlining (after)
27   /// CHECK-DAG:    <<Const100:i\d+>> IntConstant 100
28   /// CHECK-DAG:                      Return [<<Const100>>]
29 
30   /// CHECK-START: int Main.wholeSwitchDead(int) dead_code_elimination$after_inlining (after)
31   /// CHECK-NOT:                      PackedSwitch
32 
wholeSwitchDead(int j)33   public static int wholeSwitchDead(int j) {
34     int i = $inline$method();
35     int l = 100;
36     if (i > 100) {
37       switch(j) {
38         case 1:
39           i++;
40           break;
41         case 2:
42           i = 99;
43           break;
44         case 3:
45           i = 100;
46           break;
47         case 4:
48           i = -100;
49           break;
50         case 5:
51           i = 7;
52           break;
53         case 6:
54           i = -9;
55           break;
56       }
57       l += i;
58     }
59 
60     return l;
61   }
62 
63   /// CHECK-START: int Main.constantSwitch_InRange() dead_code_elimination$after_inlining (before)
64   /// CHECK-DAG:                      PackedSwitch
65 
66   /// CHECK-START: int Main.constantSwitch_InRange() dead_code_elimination$after_inlining (after)
67   /// CHECK-DAG:     <<Const7:i\d+>>  IntConstant 7
68   /// CHECK-DAG:                      Return [<<Const7>>]
69 
70   /// CHECK-START: int Main.constantSwitch_InRange() dead_code_elimination$after_inlining (after)
71   /// CHECK-NOT:                      PackedSwitch
72 
constantSwitch_InRange()73   public static int constantSwitch_InRange() {
74     int i = $inline$method();
75     switch(i) {
76       case 1:
77         i++;
78         break;
79       case 2:
80         i = 99;
81         break;
82       case 3:
83         i = 100;
84         break;
85       case 4:
86         i = -100;
87         break;
88       case 5:
89         i = 7;
90         break;
91       case 6:
92         i = -9;
93         break;
94     }
95 
96     return i;
97   }
98 
99   /// CHECK-START: int Main.constantSwitch_AboveRange() dead_code_elimination$after_inlining (before)
100   /// CHECK-DAG:                      PackedSwitch
101 
102   /// CHECK-START: int Main.constantSwitch_AboveRange() dead_code_elimination$after_inlining (after)
103   /// CHECK-DAG:     <<Const15:i\d+>> IntConstant 15
104   /// CHECK-DAG:                      Return [<<Const15>>]
105 
106   /// CHECK-START: int Main.constantSwitch_AboveRange() dead_code_elimination$after_inlining (after)
107   /// CHECK-NOT:                      PackedSwitch
108 
constantSwitch_AboveRange()109   public static int constantSwitch_AboveRange() {
110     int i = $inline$method() + 10;
111     switch(i) {
112       case 1:
113         i++;
114         break;
115       case 2:
116         i = 99;
117         break;
118       case 3:
119         i = 100;
120         break;
121       case 4:
122         i = -100;
123         break;
124       case 5:
125         i = 7;
126         break;
127       case 6:
128         i = -9;
129         break;
130     }
131 
132     return i;
133   }
134 
135   /// CHECK-START: int Main.constantSwitch_BelowRange() dead_code_elimination$after_inlining (before)
136   /// CHECK-DAG:                      PackedSwitch
137 
138   /// CHECK-START: int Main.constantSwitch_BelowRange() dead_code_elimination$after_inlining (after)
139   /// CHECK-DAG:     <<ConstM5:i\d+>> IntConstant -5
140   /// CHECK-DAG:                      Return [<<ConstM5>>]
141 
142   /// CHECK-START: int Main.constantSwitch_BelowRange() dead_code_elimination$after_inlining (after)
143   /// CHECK-NOT:                      PackedSwitch
144 
constantSwitch_BelowRange()145   public static int constantSwitch_BelowRange() {
146     int i = $inline$method() - 10;
147     switch(i) {
148       case 1:
149         i++;
150         break;
151       case 2:
152         i = 99;
153         break;
154       case 3:
155         i = 100;
156         break;
157       case 4:
158         i = -100;
159         break;
160       case 5:
161         i = 7;
162         break;
163       case 6:
164         i = -9;
165         break;
166     }
167 
168     return i;
169   }
170 
main(String[] args)171   public static void main(String[] args) throws Exception {
172     int ret_val = wholeSwitchDead(10);
173     if (ret_val != 100) {
174       throw new Error("Incorrect return value from wholeSwitchDead:" + ret_val);
175     }
176 
177     ret_val = constantSwitch_InRange();
178     if (ret_val != 7) {
179       throw new Error("Incorrect return value from constantSwitch_InRange:" + ret_val);
180     }
181 
182     ret_val = constantSwitch_AboveRange();
183     if (ret_val != 15) {
184       throw new Error("Incorrect return value from constantSwitch_AboveRange:" + ret_val);
185     }
186 
187     ret_val = constantSwitch_BelowRange();
188     if (ret_val != -5) {
189       throw new Error("Incorrect return value from constantSwitch_BelowRange:" + ret_val);
190     }
191   }
192 }
193