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
|
/*
* Copyright (C) 2014 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.
*/
public class Main {
public static void doNothing(boolean b) {
System.out.println("In do nothing.");
}
public static void inIf() {
System.out.println("In if.");
}
public static int bar() {
return 42;
}
public static int foo1() {
int b = bar();
doNothing(b == 42);
// This second `b == 42` will be GVN'ed away.
if (b == 42) {
inIf();
return b;
}
return 0;
}
public static int foo2() {
int b = bar();
doNothing(b == 41);
// This second `b == 41` will be GVN'ed away.
if (b == 41) {
inIf();
return 0;
}
return b;
}
public static boolean $noinline$intEq0(int x) {
return x == 0;
}
public static boolean $noinline$intNe0(int x) {
return x != 0;
}
public static boolean $noinline$longEq0(long x) {
return x == 0;
}
public static boolean $noinline$longNe0(long x) {
return x != 0;
}
public static boolean $noinline$longEqCst(long x) {
return x == 0x0123456789ABCDEFL;
}
public static boolean $noinline$longNeCst(long x) {
return x != 0x0123456789ABCDEFL;
}
public static void assertEqual(boolean expected, boolean actual) {
if (expected != actual) {
throw new Error("Assertion failed: " + expected + " != " + actual);
}
}
// The purpose of this method is to test code generation for a materialized
// HCondition that is not equality or inequality, and that has one boolean
// input. That can't be done directly, so we have to rely on the instruction
// simplifier to transform the control-flow graph appropriately.
public static boolean $noinline$booleanCondition(boolean in) {
int value = in ? 1 : 0;
// Calling a non-inlineable method that uses `value` as well prevents a
// transformation of the return value into `false`.
$noinline$intNe0(value);
return value > 127;
}
public static void main(String[] args) {
System.out.println("foo1");
int res = foo1();
if (res != 42) {
throw new Error("Unexpected return value for foo1: " + res + ", expected 42.");
}
System.out.println("foo2");
res = foo2();
if (res != 42) {
throw new Error("Unexpected return value for foo2: " + res + ", expected 42.");
}
assertEqual($noinline$booleanCondition(false), false);
assertEqual($noinline$booleanCondition(true), false);
int[] int_inputs = {0, 1, -1, Integer.MIN_VALUE, Integer.MAX_VALUE, 42, -9000};
long[] long_inputs = {
0L, 1L, -1L, Long.MIN_VALUE, Long.MAX_VALUE, 0x100000000L,
0x100000001L, -9000L, 0x0123456789ABCDEFL};
boolean[] int_eq_0_expected = {true, false, false, false, false, false, false};
for (int i = 0; i < int_inputs.length; i++) {
assertEqual(int_eq_0_expected[i], $noinline$intEq0(int_inputs[i]));
}
boolean[] int_ne_0_expected = {false, true, true, true, true, true, true};
for (int i = 0; i < int_inputs.length; i++) {
assertEqual(int_ne_0_expected[i], $noinline$intNe0(int_inputs[i]));
}
boolean[] long_eq_0_expected = {true, false, false, false, false, false, false, false, false};
for (int i = 0; i < long_inputs.length; i++) {
assertEqual(long_eq_0_expected[i], $noinline$longEq0(long_inputs[i]));
}
boolean[] long_ne_0_expected = {false, true, true, true, true, true, true, true, true};
for (int i = 0; i < long_inputs.length; i++) {
assertEqual(long_ne_0_expected[i], $noinline$longNe0(long_inputs[i]));
}
boolean[] long_eq_cst_expected = {false, false, false, false, false, false, false, false, true};
for (int i = 0; i < long_inputs.length; i++) {
assertEqual(long_eq_cst_expected[i], $noinline$longEqCst(long_inputs[i]));
}
boolean[] long_ne_cst_expected = {true, true, true, true, true, true, true, true, false};
for (int i = 0; i < long_inputs.length; i++) {
assertEqual(long_ne_cst_expected[i], $noinline$longNeCst(long_inputs[i]));
}
}
}
|