54084
|
1 |
/*
|
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
|
|
25 |
package org.graalvm.compiler.core.test;
|
|
26 |
|
|
27 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
28 |
import org.graalvm.compiler.nodes.extended.IntegerSwitchNode;
|
|
29 |
import org.junit.Test;
|
|
30 |
|
|
31 |
public class SwitchCanonicalizerTest extends GraalCompilerTest {
|
|
32 |
|
|
33 |
public int divByPowerOf2(int n) {
|
|
34 |
switch (n / 8) {
|
|
35 |
case Integer.MAX_VALUE / 8 + 1:
|
|
36 |
return hashCode();
|
|
37 |
default:
|
|
38 |
return 1;
|
|
39 |
}
|
|
40 |
}
|
|
41 |
|
|
42 |
@Test
|
|
43 |
public void testDivByPowerOf2() {
|
|
44 |
shouldFoldSwitch("divByPowerOf2");
|
|
45 |
}
|
|
46 |
|
|
47 |
public int divByNonPowerOf2(int n) {
|
|
48 |
switch (n / 7) {
|
|
49 |
case Integer.MAX_VALUE / 7 + 1:
|
|
50 |
return hashCode();
|
|
51 |
default:
|
|
52 |
return 1;
|
|
53 |
}
|
|
54 |
}
|
|
55 |
|
|
56 |
@Test
|
|
57 |
public void testDivByNonPowerOf2() {
|
|
58 |
shouldFoldSwitch("divByNonPowerOf2");
|
|
59 |
}
|
|
60 |
|
|
61 |
public int remByPowerOf2(int n) {
|
|
62 |
switch (n % 8) {
|
|
63 |
case 9:
|
|
64 |
return hashCode();
|
|
65 |
default:
|
|
66 |
return 1;
|
|
67 |
}
|
|
68 |
}
|
|
69 |
|
|
70 |
@Test
|
|
71 |
public void testRemByPowerOf2() {
|
|
72 |
shouldFoldSwitch("remByPowerOf2");
|
|
73 |
}
|
|
74 |
|
|
75 |
public int remByPowerOf2PositiveX(int n) {
|
|
76 |
int n0 = n > 0 ? 8 : 9;
|
|
77 |
switch (n0 % 8) {
|
|
78 |
case 9:
|
|
79 |
return hashCode();
|
|
80 |
default:
|
|
81 |
return 1;
|
|
82 |
}
|
|
83 |
}
|
|
84 |
|
|
85 |
@Test
|
|
86 |
public void testRemByPowerOf2PositiveX() {
|
|
87 |
shouldFoldSwitch("remByPowerOf2PositiveX");
|
|
88 |
}
|
|
89 |
|
|
90 |
public int remByPowerOf2NegativeX(int n) {
|
|
91 |
int n0 = n > 0 ? -8 : -9;
|
|
92 |
switch (n0 % 8) {
|
|
93 |
case 9:
|
|
94 |
return hashCode();
|
|
95 |
default:
|
|
96 |
return 1;
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|
|
100 |
@Test
|
|
101 |
public void testRemByPowerOf2NegativeX() {
|
|
102 |
shouldFoldSwitch("remByPowerOf2NegativeX");
|
|
103 |
}
|
|
104 |
|
|
105 |
public int remByNonPowerOf2(int n) {
|
|
106 |
switch (n % 7) {
|
|
107 |
case 9:
|
|
108 |
return hashCode();
|
|
109 |
default:
|
|
110 |
return 1;
|
|
111 |
}
|
|
112 |
}
|
|
113 |
|
|
114 |
@Test
|
|
115 |
public void testRemByNonPowerOf2() {
|
|
116 |
shouldFoldSwitch("remByNonPowerOf2");
|
|
117 |
}
|
|
118 |
|
|
119 |
private void shouldFoldSwitch(String methodName) {
|
|
120 |
StructuredGraph graph = parseForCompile(getResolvedJavaMethod(methodName));
|
58877
|
121 |
createCanonicalizerPhase().apply(graph, getDefaultHighTierContext());
|
58299
|
122 |
assertTrue(graph.getNodes().filter(IntegerSwitchNode.class).isEmpty());
|
54084
|
123 |
}
|
|
124 |
|
|
125 |
}
|