46640
|
1 |
/*
|
58877
|
2 |
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
|
46640
|
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 |
|
50858
|
24 |
|
|
25 |
|
46640
|
26 |
package org.graalvm.compiler.core.test;
|
|
27 |
|
|
28 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
29 |
import org.graalvm.compiler.nodes.memory.WriteNode;
|
|
30 |
import org.graalvm.compiler.nodes.spi.LoweringTool;
|
|
31 |
import org.graalvm.compiler.phases.common.FloatingReadPhase;
|
|
32 |
import org.graalvm.compiler.phases.common.IncrementalCanonicalizerPhase;
|
|
33 |
import org.graalvm.compiler.phases.common.LoweringPhase;
|
|
34 |
import org.graalvm.compiler.phases.tiers.HighTierContext;
|
|
35 |
import org.junit.Test;
|
|
36 |
|
|
37 |
public class MemoryGraphCanonicalizeTest extends GraalCompilerTest {
|
|
38 |
static class TestObject {
|
|
39 |
Object object;
|
|
40 |
Integer integer;
|
|
41 |
int value;
|
|
42 |
volatile boolean written;
|
|
43 |
}
|
|
44 |
|
|
45 |
public static void simpleElimination(TestObject object) {
|
|
46 |
object.object = object;
|
|
47 |
object.value = object.integer;
|
|
48 |
object.value = object.integer + 2;
|
|
49 |
object.value = object.integer + 3;
|
|
50 |
}
|
|
51 |
|
|
52 |
@Test
|
|
53 |
public void testSimpleElimination() {
|
|
54 |
testGraph("simpleElimination", 2);
|
|
55 |
}
|
|
56 |
|
|
57 |
public static void complexElimination(TestObject object) {
|
|
58 |
object.object = object;
|
|
59 |
object.value = object.integer;
|
|
60 |
object.value = object.integer + 2;
|
|
61 |
if (object.object == null) {
|
|
62 |
object.value = object.integer + 3;
|
|
63 |
} else {
|
|
64 |
object.object = new Object();
|
|
65 |
}
|
|
66 |
object.written = true;
|
|
67 |
object.value = 5;
|
|
68 |
}
|
|
69 |
|
|
70 |
@Test
|
|
71 |
public void testComplexElimination() {
|
|
72 |
testGraph("complexElimination", 6);
|
|
73 |
}
|
|
74 |
|
|
75 |
public void testGraph(String name, int expectedWrites) {
|
|
76 |
StructuredGraph graph = parseEager(name, StructuredGraph.AllowAssumptions.YES);
|
|
77 |
HighTierContext context = getDefaultHighTierContext();
|
58877
|
78 |
new LoweringPhase(createCanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
|
|
79 |
new IncrementalCanonicalizerPhase<>(createCanonicalizerPhase(), new FloatingReadPhase()).apply(graph, context);
|
|
80 |
createCanonicalizerPhase().apply(graph, context);
|
46640
|
81 |
int writes = graph.getNodes().filter(WriteNode.class).count();
|
|
82 |
assertTrue(writes == expectedWrites, "Expected %d writes, found %d", expectedWrites, writes);
|
|
83 |
}
|
|
84 |
}
|