xref: /aosp_15_r20/art/test/409-materialized-condition/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2014 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker public class Main {
18*795d594fSAndroid Build Coastguard Worker 
doNothing(boolean b)19*795d594fSAndroid Build Coastguard Worker   public static void doNothing(boolean b) {
20*795d594fSAndroid Build Coastguard Worker     System.out.println("In do nothing.");
21*795d594fSAndroid Build Coastguard Worker   }
22*795d594fSAndroid Build Coastguard Worker 
inIf()23*795d594fSAndroid Build Coastguard Worker   public static void inIf() {
24*795d594fSAndroid Build Coastguard Worker     System.out.println("In if.");
25*795d594fSAndroid Build Coastguard Worker   }
26*795d594fSAndroid Build Coastguard Worker 
bar()27*795d594fSAndroid Build Coastguard Worker   public static int bar() {
28*795d594fSAndroid Build Coastguard Worker     return 42;
29*795d594fSAndroid Build Coastguard Worker   }
30*795d594fSAndroid Build Coastguard Worker 
foo1()31*795d594fSAndroid Build Coastguard Worker   public static int foo1() {
32*795d594fSAndroid Build Coastguard Worker     int b = bar();
33*795d594fSAndroid Build Coastguard Worker     doNothing(b == 42);
34*795d594fSAndroid Build Coastguard Worker     // This second `b == 42` will be GVN'ed away.
35*795d594fSAndroid Build Coastguard Worker     if (b == 42) {
36*795d594fSAndroid Build Coastguard Worker       inIf();
37*795d594fSAndroid Build Coastguard Worker       return b;
38*795d594fSAndroid Build Coastguard Worker     }
39*795d594fSAndroid Build Coastguard Worker     return 0;
40*795d594fSAndroid Build Coastguard Worker   }
41*795d594fSAndroid Build Coastguard Worker 
foo2()42*795d594fSAndroid Build Coastguard Worker   public static int foo2() {
43*795d594fSAndroid Build Coastguard Worker     int b = bar();
44*795d594fSAndroid Build Coastguard Worker     doNothing(b == 41);
45*795d594fSAndroid Build Coastguard Worker     // This second `b == 41` will be GVN'ed away.
46*795d594fSAndroid Build Coastguard Worker     if (b == 41) {
47*795d594fSAndroid Build Coastguard Worker       inIf();
48*795d594fSAndroid Build Coastguard Worker       return 0;
49*795d594fSAndroid Build Coastguard Worker     }
50*795d594fSAndroid Build Coastguard Worker     return b;
51*795d594fSAndroid Build Coastguard Worker   }
52*795d594fSAndroid Build Coastguard Worker 
$noinline$intEq0(int x)53*795d594fSAndroid Build Coastguard Worker   public static boolean $noinline$intEq0(int x) {
54*795d594fSAndroid Build Coastguard Worker     return x == 0;
55*795d594fSAndroid Build Coastguard Worker   }
56*795d594fSAndroid Build Coastguard Worker 
$noinline$intNe0(int x)57*795d594fSAndroid Build Coastguard Worker   public static boolean $noinline$intNe0(int x) {
58*795d594fSAndroid Build Coastguard Worker     return x != 0;
59*795d594fSAndroid Build Coastguard Worker   }
60*795d594fSAndroid Build Coastguard Worker 
$noinline$longEq0(long x)61*795d594fSAndroid Build Coastguard Worker   public static boolean $noinline$longEq0(long x) {
62*795d594fSAndroid Build Coastguard Worker     return x == 0;
63*795d594fSAndroid Build Coastguard Worker   }
64*795d594fSAndroid Build Coastguard Worker 
$noinline$longNe0(long x)65*795d594fSAndroid Build Coastguard Worker   public static boolean $noinline$longNe0(long x) {
66*795d594fSAndroid Build Coastguard Worker     return x != 0;
67*795d594fSAndroid Build Coastguard Worker   }
68*795d594fSAndroid Build Coastguard Worker 
$noinline$longEqCst(long x)69*795d594fSAndroid Build Coastguard Worker   public static boolean $noinline$longEqCst(long x) {
70*795d594fSAndroid Build Coastguard Worker     return x == 0x0123456789ABCDEFL;
71*795d594fSAndroid Build Coastguard Worker   }
72*795d594fSAndroid Build Coastguard Worker 
$noinline$longNeCst(long x)73*795d594fSAndroid Build Coastguard Worker   public static boolean $noinline$longNeCst(long x) {
74*795d594fSAndroid Build Coastguard Worker     return x != 0x0123456789ABCDEFL;
75*795d594fSAndroid Build Coastguard Worker   }
76*795d594fSAndroid Build Coastguard Worker 
assertEqual(boolean expected, boolean actual)77*795d594fSAndroid Build Coastguard Worker   public static void assertEqual(boolean expected, boolean actual) {
78*795d594fSAndroid Build Coastguard Worker     if (expected != actual) {
79*795d594fSAndroid Build Coastguard Worker       throw new Error("Assertion failed: " + expected + " != " + actual);
80*795d594fSAndroid Build Coastguard Worker     }
81*795d594fSAndroid Build Coastguard Worker   }
82*795d594fSAndroid Build Coastguard Worker 
83*795d594fSAndroid Build Coastguard Worker   // The purpose of this method is to test code generation for a materialized
84*795d594fSAndroid Build Coastguard Worker   // HCondition that is not equality or inequality, and that has one boolean
85*795d594fSAndroid Build Coastguard Worker   // input. That can't be done directly, so we have to rely on the instruction
86*795d594fSAndroid Build Coastguard Worker   // simplifier to transform the control-flow graph appropriately.
$noinline$booleanCondition(boolean in)87*795d594fSAndroid Build Coastguard Worker   public static boolean $noinline$booleanCondition(boolean in) {
88*795d594fSAndroid Build Coastguard Worker     int value = in ? 1 : 0;
89*795d594fSAndroid Build Coastguard Worker 
90*795d594fSAndroid Build Coastguard Worker     // Calling a non-inlineable method that uses `value` as well prevents a
91*795d594fSAndroid Build Coastguard Worker     // transformation of the return value into `false`.
92*795d594fSAndroid Build Coastguard Worker     $noinline$intNe0(value);
93*795d594fSAndroid Build Coastguard Worker     return value > 127;
94*795d594fSAndroid Build Coastguard Worker   }
95*795d594fSAndroid Build Coastguard Worker 
main(String[] args)96*795d594fSAndroid Build Coastguard Worker   public static void main(String[] args) {
97*795d594fSAndroid Build Coastguard Worker     System.out.println("foo1");
98*795d594fSAndroid Build Coastguard Worker     int res = foo1();
99*795d594fSAndroid Build Coastguard Worker     if (res != 42) {
100*795d594fSAndroid Build Coastguard Worker       throw new Error("Unexpected return value for foo1: " + res + ", expected 42.");
101*795d594fSAndroid Build Coastguard Worker     }
102*795d594fSAndroid Build Coastguard Worker 
103*795d594fSAndroid Build Coastguard Worker     System.out.println("foo2");
104*795d594fSAndroid Build Coastguard Worker     res = foo2();
105*795d594fSAndroid Build Coastguard Worker     if (res != 42) {
106*795d594fSAndroid Build Coastguard Worker       throw new Error("Unexpected return value for foo2: " + res + ", expected 42.");
107*795d594fSAndroid Build Coastguard Worker     }
108*795d594fSAndroid Build Coastguard Worker 
109*795d594fSAndroid Build Coastguard Worker     assertEqual($noinline$booleanCondition(false), false);
110*795d594fSAndroid Build Coastguard Worker     assertEqual($noinline$booleanCondition(true), false);
111*795d594fSAndroid Build Coastguard Worker 
112*795d594fSAndroid Build Coastguard Worker     int[] int_inputs = {0, 1, -1, Integer.MIN_VALUE, Integer.MAX_VALUE, 42, -9000};
113*795d594fSAndroid Build Coastguard Worker     long[] long_inputs = {
114*795d594fSAndroid Build Coastguard Worker         0L, 1L, -1L, Long.MIN_VALUE, Long.MAX_VALUE, 0x100000000L,
115*795d594fSAndroid Build Coastguard Worker         0x100000001L, -9000L, 0x0123456789ABCDEFL};
116*795d594fSAndroid Build Coastguard Worker 
117*795d594fSAndroid Build Coastguard Worker     boolean[] int_eq_0_expected = {true, false, false, false, false, false, false};
118*795d594fSAndroid Build Coastguard Worker 
119*795d594fSAndroid Build Coastguard Worker     for (int i = 0; i < int_inputs.length; i++) {
120*795d594fSAndroid Build Coastguard Worker       assertEqual(int_eq_0_expected[i], $noinline$intEq0(int_inputs[i]));
121*795d594fSAndroid Build Coastguard Worker     }
122*795d594fSAndroid Build Coastguard Worker 
123*795d594fSAndroid Build Coastguard Worker     boolean[] int_ne_0_expected = {false, true, true, true, true, true, true};
124*795d594fSAndroid Build Coastguard Worker 
125*795d594fSAndroid Build Coastguard Worker     for (int i = 0; i < int_inputs.length; i++) {
126*795d594fSAndroid Build Coastguard Worker       assertEqual(int_ne_0_expected[i], $noinline$intNe0(int_inputs[i]));
127*795d594fSAndroid Build Coastguard Worker     }
128*795d594fSAndroid Build Coastguard Worker 
129*795d594fSAndroid Build Coastguard Worker     boolean[] long_eq_0_expected = {true, false, false, false, false, false, false, false, false};
130*795d594fSAndroid Build Coastguard Worker 
131*795d594fSAndroid Build Coastguard Worker     for (int i = 0; i < long_inputs.length; i++) {
132*795d594fSAndroid Build Coastguard Worker       assertEqual(long_eq_0_expected[i], $noinline$longEq0(long_inputs[i]));
133*795d594fSAndroid Build Coastguard Worker     }
134*795d594fSAndroid Build Coastguard Worker 
135*795d594fSAndroid Build Coastguard Worker     boolean[] long_ne_0_expected = {false, true, true, true, true, true, true, true, true};
136*795d594fSAndroid Build Coastguard Worker 
137*795d594fSAndroid Build Coastguard Worker     for (int i = 0; i < long_inputs.length; i++) {
138*795d594fSAndroid Build Coastguard Worker       assertEqual(long_ne_0_expected[i], $noinline$longNe0(long_inputs[i]));
139*795d594fSAndroid Build Coastguard Worker     }
140*795d594fSAndroid Build Coastguard Worker 
141*795d594fSAndroid Build Coastguard Worker     boolean[] long_eq_cst_expected = {false, false, false, false, false, false, false, false, true};
142*795d594fSAndroid Build Coastguard Worker 
143*795d594fSAndroid Build Coastguard Worker     for (int i = 0; i < long_inputs.length; i++) {
144*795d594fSAndroid Build Coastguard Worker       assertEqual(long_eq_cst_expected[i], $noinline$longEqCst(long_inputs[i]));
145*795d594fSAndroid Build Coastguard Worker     }
146*795d594fSAndroid Build Coastguard Worker 
147*795d594fSAndroid Build Coastguard Worker     boolean[] long_ne_cst_expected = {true, true, true, true, true, true, true, true, false};
148*795d594fSAndroid Build Coastguard Worker 
149*795d594fSAndroid Build Coastguard Worker     for (int i = 0; i < long_inputs.length; i++) {
150*795d594fSAndroid Build Coastguard Worker       assertEqual(long_ne_cst_expected[i], $noinline$longNeCst(long_inputs[i]));
151*795d594fSAndroid Build Coastguard Worker     }
152*795d594fSAndroid Build Coastguard Worker   }
153*795d594fSAndroid Build Coastguard Worker }
154