lin
2025-07-30 fcd736bf35fd93b563e9bbf594f2aa7b62028cc9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import java.lang.reflect.Method;
 
public class Main {
 
  // Note #1: `javac` flips the conditions of If statements.
  // Note #2: In the optimizing compiler, the first input of Phi is always
  //          the fall-through path, i.e. the false branch.
 
  public static void assertBoolEquals(boolean expected, boolean result) {
    if (expected != result) {
      throw new Error("Expected: " + expected + ", found: " + result);
    }
  }
 
  public static void assertIntEquals(int expected, int result) {
    if (expected != result) {
      throw new Error("Expected: " + expected + ", found: " + result);
    }
  }
 
  /*
   * Program which only delegates the condition, i.e. returns 1 when True
   * and 0 when False.
   */
 
  /// CHECK-START: boolean Main.GreaterThan(int, int) select_generator (before)
  /// CHECK-DAG:     <<ParamX:i\d+>>   ParameterValue
  /// CHECK-DAG:     <<ParamY:i\d+>>   ParameterValue
  /// CHECK-DAG:     <<Const0:i\d+>>   IntConstant 0
  /// CHECK-DAG:     <<Const1:i\d+>>   IntConstant 1
  /// CHECK-DAG:     <<Cond:z\d+>>     GreaterThan [<<ParamX>>,<<ParamY>>]
  /// CHECK-DAG:                       If [<<Cond>>]
  /// CHECK-DAG:     <<Phi:i\d+>>      Phi [<<Const0>>,<<Const1>>]
  /// CHECK-DAG:                       Return [<<Phi>>]
 
  /// CHECK-START: boolean Main.GreaterThan(int, int) select_generator (after)
  /// CHECK-DAG:     <<ParamX:i\d+>>   ParameterValue
  /// CHECK-DAG:     <<ParamY:i\d+>>   ParameterValue
  /// CHECK-DAG:     <<Const0:i\d+>>   IntConstant 0
  /// CHECK-DAG:     <<Const1:i\d+>>   IntConstant 1
  /// CHECK-DAG:     <<Cond:z\d+>>     GreaterThan [<<ParamX>>,<<ParamY>>]
  /// CHECK-DAG:     <<Select:i\d+>>   Select [<<Const0>>,<<Const1>>,<<Cond>>]
  /// CHECK-DAG:                       Return [<<Select>>]
 
  public static boolean GreaterThan(int x, int y) {
    return (x <= y) ? false : true;
  }
 
  /*
   * Program which negates a condition, i.e. returns 0 when True
   * and 1 when False.
   */
 
  /// CHECK-START: boolean Main.LessThan(int, int) select_generator (before)
  /// CHECK-DAG:     <<ParamX:i\d+>>   ParameterValue
  /// CHECK-DAG:     <<ParamY:i\d+>>   ParameterValue
  /// CHECK-DAG:     <<Const0:i\d+>>   IntConstant 0
  /// CHECK-DAG:     <<Const1:i\d+>>   IntConstant 1
  /// CHECK-DAG:     <<Cond:z\d+>>     GreaterThanOrEqual [<<ParamX>>,<<ParamY>>]
  /// CHECK-DAG:                       If [<<Cond>>]
  /// CHECK-DAG:     <<Phi:i\d+>>      Phi [<<Const1>>,<<Const0>>]
  /// CHECK-DAG:                       Return [<<Phi>>]
 
  /// CHECK-START: boolean Main.LessThan(int, int) select_generator (after)
  /// CHECK-DAG:     <<ParamX:i\d+>>   ParameterValue
  /// CHECK-DAG:     <<ParamY:i\d+>>   ParameterValue
  /// CHECK-DAG:     <<Const0:i\d+>>   IntConstant 0
  /// CHECK-DAG:     <<Const1:i\d+>>   IntConstant 1
  /// CHECK-DAG:     <<Cond:z\d+>>     GreaterThanOrEqual [<<ParamX>>,<<ParamY>>]
  /// CHECK-DAG:     <<Select:i\d+>>   Select [<<Const1>>,<<Const0>>,<<Cond>>]
  /// CHECK-DAG:                       Return [<<Select>>]
 
  public static boolean LessThan(int x, int y) {
    return (x < y) ? true : false;
  }
 
  /// CHECK-START: int Main.SimpleTrueBlock(boolean, int) select_generator (after)
  /// CHECK-DAG:     <<ParamX:z\d+>>   ParameterValue
  /// CHECK-DAG:     <<ParamY:i\d+>>   ParameterValue
  /// CHECK-DAG:     <<Const42:i\d+>>  IntConstant 42
  /// CHECK-DAG:     <<Const43:i\d+>>  IntConstant 43
  /// CHECK-DAG:     <<Add:i\d+>>      Add [<<ParamY>>,<<Const42>>]
  /// CHECK-DAG:     <<Select:i\d+>>   Select [<<Const43>>,<<Add>>,<<ParamX>>]
  /// CHECK-DAG:                       Return [<<Select>>]
 
  /// CHECK-START: int Main.SimpleTrueBlock(boolean, int) select_generator (after)
  /// CHECK-NOT:     If
 
  public static int SimpleTrueBlock(boolean x, int y) {
    return x ? y + 42 : 43;
  }
 
  /// CHECK-START: int Main.SimpleFalseBlock(boolean, int) select_generator (after)
  /// CHECK-DAG:     <<ParamX:z\d+>>   ParameterValue
  /// CHECK-DAG:     <<ParamY:i\d+>>   ParameterValue
  /// CHECK-DAG:     <<Const42:i\d+>>  IntConstant 42
  /// CHECK-DAG:     <<Const43:i\d+>>  IntConstant 43
  /// CHECK-DAG:     <<Add:i\d+>>      Add [<<ParamY>>,<<Const43>>]
  /// CHECK-DAG:     <<Select:i\d+>>   Select [<<Add>>,<<Const42>>,<<ParamX>>]
  /// CHECK-DAG:                       Return [<<Select>>]
 
  /// CHECK-START: int Main.SimpleFalseBlock(boolean, int) select_generator (after)
  /// CHECK-NOT:     If
 
  public static int SimpleFalseBlock(boolean x, int y) {
    return x ? 42 : y + 43;
  }
 
  /// CHECK-START: int Main.SimpleBothBlocks(boolean, int, int) select_generator (after)
  /// CHECK-DAG:     <<ParamX:z\d+>>   ParameterValue
  /// CHECK-DAG:     <<ParamY:i\d+>>   ParameterValue
  /// CHECK-DAG:     <<ParamZ:i\d+>>   ParameterValue
  /// CHECK-DAG:     <<Const42:i\d+>>  IntConstant 42
  /// CHECK-DAG:     <<Const43:i\d+>>  IntConstant 43
  /// CHECK-DAG:     <<AddTrue:i\d+>>  Add [<<ParamY>>,<<Const42>>]
  /// CHECK-DAG:     <<AddFalse:i\d+>> Add [<<ParamZ>>,<<Const43>>]
  /// CHECK-DAG:     <<Select:i\d+>>   Select [<<AddFalse>>,<<AddTrue>>,<<ParamX>>]
  /// CHECK-DAG:                       Return [<<Select>>]
 
  /// CHECK-START: int Main.SimpleBothBlocks(boolean, int, int) select_generator (after)
  /// CHECK-NOT:     If
 
  public static int SimpleBothBlocks(boolean x, int y, int z) {
    return x ? y + 42 : z + 43;
  }
 
  /// CHECK-START: int Main.ThreeBlocks(boolean, boolean) select_generator (after)
  /// CHECK-DAG:     <<ParamX:z\d+>>    ParameterValue
  /// CHECK-DAG:     <<ParamY:z\d+>>    ParameterValue
  /// CHECK-DAG:     <<Const1:i\d+>>    IntConstant 1
  /// CHECK-DAG:     <<Const2:i\d+>>    IntConstant 2
  /// CHECK-DAG:     <<Const3:i\d+>>    IntConstant 3
  /// CHECK-DAG:     <<Select23:i\d+>>  Select [<<Const3>>,<<Const2>>,<<ParamY>>]
  /// CHECK-DAG:     <<Select123:i\d+>> Select [<<Select23>>,<<Const1>>,<<ParamX>>]
  /// CHECK-DAG:                        Return [<<Select123>>]
 
  public static int ThreeBlocks(boolean x, boolean y) {
    if (x) {
      return 1;
    } else if (y) {
      return 2;
    } else {
      return 3;
    }
  }
 
  /// CHECK-START: int Main.TrueBlockWithTooManyInstructions(boolean) select_generator (before)
  /// CHECK-DAG:     <<This:l\d+>>    ParameterValue
  /// CHECK-DAG:     <<Cond:z\d+>>    ParameterValue
  /// CHECK-DAG:     <<Const2:i\d+>>  IntConstant 2
  /// CHECK-DAG:     <<Const43:i\d+>> IntConstant 43
  /// CHECK-DAG:                      If [<<Cond>>]
  /// CHECK-DAG:     <<Iget:i\d+>>    InstanceFieldGet [<<This>>]
  /// CHECK-DAG:     <<Add:i\d+>>     Add [<<Iget>>,<<Const2>>]
  /// CHECK-DAG:                      Phi [<<Add>>,<<Const43>>]
 
  /// CHECK-START: int Main.TrueBlockWithTooManyInstructions(boolean) select_generator (after)
  /// CHECK-NOT:     Select
 
  public int TrueBlockWithTooManyInstructions(boolean x) {
    return x ? (read_field + 2) : 43;
  }
 
  /// CHECK-START: int Main.FalseBlockWithTooManyInstructions(boolean) select_generator (before)
  /// CHECK-DAG:     <<This:l\d+>>    ParameterValue
  /// CHECK-DAG:     <<Cond:z\d+>>    ParameterValue
  /// CHECK-DAG:     <<Const3:i\d+>>  IntConstant 3
  /// CHECK-DAG:     <<Const42:i\d+>> IntConstant 42
  /// CHECK-DAG:                      If [<<Cond>>]
  /// CHECK-DAG:     <<Iget:i\d+>>    InstanceFieldGet [<<This>>]
  /// CHECK-DAG:     <<Add:i\d+>>     Add [<<Iget>>,<<Const3>>]
  /// CHECK-DAG:                      Phi [<<Const42>>,<<Add>>]
 
  /// CHECK-START: int Main.FalseBlockWithTooManyInstructions(boolean) select_generator (after)
  /// CHECK-NOT:     Select
 
  public int FalseBlockWithTooManyInstructions(boolean x) {
    return x ? 42 : (read_field + 3);
  }
 
  /// CHECK-START: int Main.TrueBlockWithSideEffects(boolean) select_generator (before)
  /// CHECK-DAG:     <<This:l\d+>>    ParameterValue
  /// CHECK-DAG:     <<Cond:z\d+>>    ParameterValue
  /// CHECK-DAG:     <<Const42:i\d+>> IntConstant 42
  /// CHECK-DAG:     <<Const43:i\d+>> IntConstant 43
  /// CHECK-DAG:                      If [<<Cond>>]
  /// CHECK-DAG:                      InstanceFieldSet [<<This>>,<<Const42>>]
  /// CHECK-DAG:                      Phi [<<Const42>>,<<Const43>>]
 
  /// CHECK-START: int Main.TrueBlockWithSideEffects(boolean) select_generator (after)
  /// CHECK-NOT:     Select
 
  public int TrueBlockWithSideEffects(boolean x) {
    return x ? (write_field = 42) : 43;
  }
 
  /// CHECK-START: int Main.FalseBlockWithSideEffects(boolean) select_generator (before)
  /// CHECK-DAG:     <<This:l\d+>>    ParameterValue
  /// CHECK-DAG:     <<Cond:z\d+>>    ParameterValue
  /// CHECK-DAG:     <<Const42:i\d+>> IntConstant 42
  /// CHECK-DAG:     <<Const43:i\d+>> IntConstant 43
  /// CHECK-DAG:                      If [<<Cond>>]
  /// CHECK-DAG:                      InstanceFieldSet [<<This>>,<<Const43>>]
  /// CHECK-DAG:                      Phi [<<Const42>>,<<Const43>>]
 
  /// CHECK-START: int Main.FalseBlockWithSideEffects(boolean) select_generator (after)
  /// CHECK-NOT:     Select
 
  public int FalseBlockWithSideEffects(boolean x) {
    return x ? 42 : (write_field = 43);
  }
 
  public static void main(String[] args) throws Exception {
    Class main2 = Class.forName("Main2");
    Method booleanNot = main2.getMethod("BooleanNot", boolean.class);
    Method valuesOrdered = main2.getMethod("ValuesOrdered", int.class, int.class, int.class);
    Method negatedCondition = main2.getMethod("NegatedCondition", boolean.class);
    Method multiplePhis = main2.getMethod("MultiplePhis");
 
    assertBoolEquals(false, (boolean)booleanNot.invoke(null, true));
    assertBoolEquals(true, (boolean)booleanNot.invoke(null, false));
    assertBoolEquals(true, GreaterThan(10, 5));
    assertBoolEquals(false, GreaterThan(10, 10));
    assertBoolEquals(false, GreaterThan(5, 10));
    assertBoolEquals(true, LessThan(5, 10));
    assertBoolEquals(false, LessThan(10, 10));
    assertBoolEquals(false, LessThan(10, 5));
 
    assertBoolEquals(true, (boolean)valuesOrdered.invoke(null, 1, 3, 5));
    assertBoolEquals(true, (boolean)valuesOrdered.invoke(null, 5, 3, 1));
    assertBoolEquals(false, (boolean)valuesOrdered.invoke(null, 1, 3, 2));
    assertBoolEquals(false, (boolean)valuesOrdered.invoke(null, 2, 3, 1));
    assertBoolEquals(true, (boolean)valuesOrdered.invoke(null, 3, 3, 3));
    assertBoolEquals(true, (boolean)valuesOrdered.invoke(null, 3, 3, 5));
    assertBoolEquals(false, (boolean)valuesOrdered.invoke(null, 5, 5, 3));
    assertIntEquals(42, (int)negatedCondition.invoke(null, true));
    assertIntEquals(43, (int)negatedCondition.invoke(null, false));
    assertIntEquals(46, SimpleTrueBlock(true, 4));
    assertIntEquals(43, SimpleTrueBlock(false, 4));
    assertIntEquals(42, SimpleFalseBlock(true, 7));
    assertIntEquals(50, SimpleFalseBlock(false, 7));
    assertIntEquals(48, SimpleBothBlocks(true, 6, 2));
    assertIntEquals(45, SimpleBothBlocks(false, 6, 2));
    assertIntEquals(1, ThreeBlocks(true, true));
    assertIntEquals(1, ThreeBlocks(true, false));
    assertIntEquals(2, ThreeBlocks(false, true));
    assertIntEquals(3, ThreeBlocks(false, false));
    assertIntEquals(13, (int)multiplePhis.invoke(null));
 
    Main m = new Main();
    assertIntEquals(42, m.TrueBlockWithTooManyInstructions(true));
    assertIntEquals(43, m.TrueBlockWithTooManyInstructions(false));
    assertIntEquals(42, m.FalseBlockWithTooManyInstructions(true));
    assertIntEquals(43, m.FalseBlockWithTooManyInstructions(false));
    assertIntEquals(42, m.TrueBlockWithSideEffects(true));
    assertIntEquals(43, m.TrueBlockWithSideEffects(false));
    assertIntEquals(42, m.FalseBlockWithSideEffects(true));
    assertIntEquals(43, m.FalseBlockWithSideEffects(false));
  }
 
  // These need to be instance fields so as to not generate a LoadClass for iget/iput.
  public int read_field = 40;
  public int write_field = 42;
}