001/*
002 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.compiler.test.ea;
024
025import java.lang.ref.*;
026
027import org.junit.*;
028
029import com.oracle.graal.compiler.test.*;
030import com.oracle.graal.graph.*;
031import com.oracle.graal.nodes.*;
032import com.oracle.graal.nodes.cfg.*;
033import com.oracle.graal.nodes.extended.*;
034import com.oracle.graal.nodes.java.*;
035import com.oracle.graal.nodes.virtual.*;
036import com.oracle.graal.phases.common.*;
037
038/**
039 * The PartialEscapeAnalysisPhase is expected to remove all allocations and return the correct
040 * values.
041 */
042public class PartialEscapeAnalysisTest extends EATestBase {
043
044    public static class TestObject {
045
046        public int x;
047        public int y;
048
049        public TestObject(int x, int y) {
050            this.x = x;
051            this.y = y;
052        }
053    }
054
055    public static class TestObject2 {
056
057        public Object x;
058        public Object y;
059
060        public TestObject2(Object x, Object y) {
061            this.x = x;
062            this.y = y;
063        }
064    }
065
066    @Test
067    public void test1() {
068        testPartialEscapeAnalysis("test1Snippet", 0.25, 1);
069    }
070
071    @SuppressWarnings("all")
072    public static Object test1Snippet(int a, int b, Object x, Object y) {
073        TestObject2 obj = new TestObject2(x, y);
074        if (a < 0) {
075            if (b < 0) {
076                return obj;
077            } else {
078                return obj.y;
079            }
080        } else {
081            return obj.x;
082        }
083    }
084
085    @Test
086    public void test2() {
087        testPartialEscapeAnalysis("test2Snippet", 1.5, 3, LoadIndexedNode.class);
088    }
089
090    public static Object test2Snippet(int a, Object x, Object y, Object z) {
091        TestObject2 obj = new TestObject2(x, y);
092        obj.x = new TestObject2(obj, z);
093        if (a < 0) {
094            ((TestObject2) obj.x).y = null;
095            obj.y = null;
096            return obj;
097        } else {
098            ((TestObject2) obj.x).y = Integer.class;
099            ((TestObject2) obj.x).x = null;
100            return obj.x;
101        }
102    }
103
104    @Test
105    public void test3() {
106        testPartialEscapeAnalysis("test3Snippet", 0.5, 1, StoreFieldNode.class, LoadFieldNode.class);
107    }
108
109    public static Object test3Snippet(int a) {
110        if (a < 0) {
111            TestObject obj = new TestObject(1, 2);
112            obj.x = 123;
113            obj.y = 234;
114            obj.x = 123111;
115            obj.y = new Integer(123).intValue();
116            return obj;
117        } else {
118            return null;
119        }
120    }
121
122    @Test
123    @Ignore
124    public void testCache() {
125        testPartialEscapeAnalysis("testCacheSnippet", 0.75, 1);
126    }
127
128    public static class CacheKey {
129
130        private final int idx;
131        private final Object ref;
132
133        public CacheKey(int idx, Object ref) {
134            this.idx = idx;
135            this.ref = ref;
136        }
137
138        @Override
139        public int hashCode() {
140            return 31 * idx + ref.hashCode();
141        }
142
143        public synchronized boolean equals(CacheKey other) {
144            return idx == other.idx && ref == other.ref;
145        }
146    }
147
148    public static CacheKey cacheKey = null;
149    public static Object value = null;
150
151    private static native Object createValue(CacheKey key);
152
153    public static Object testCacheSnippet(int idx, Object ref) {
154        CacheKey key = new CacheKey(idx, ref);
155        if (!key.equals(cacheKey)) {
156            cacheKey = key;
157            value = createValue(key);
158        }
159        return value;
160    }
161
162    public static int testReference1Snippet(Object a) {
163        SoftReference<Object> softReference = new SoftReference<>(a);
164        if (softReference.get().hashCode() == 0) {
165            return 1;
166        } else {
167            return 2;
168        }
169    }
170
171    @Test
172    public void testReference1() {
173        prepareGraph("testReference1Snippet", false);
174        assertDeepEquals(1, graph.getNodes().filter(NewInstanceNode.class).count());
175    }
176
177    public static int testCanonicalizeSnippet(int v) {
178        CacheKey key = new CacheKey(v, null);
179
180        CacheKey key2;
181        if (key.idx == v) {
182            key2 = new CacheKey(v, null);
183        } else {
184            key2 = null;
185        }
186        return key2.idx;
187    }
188
189    @Test
190    public void testCanonicalize() {
191        prepareGraph("testCanonicalizeSnippet", false);
192        assertTrue(graph.getNodes().filter(ReturnNode.class).count() == 1);
193        assertTrue(graph.getNodes().filter(ReturnNode.class).first().result() == graph.getParameter(0));
194    }
195
196    public static int testBoxLoopSnippet(int n) {
197        Integer sum = 0;
198        for (Integer i = 0; i < n; i++) {
199            if (sum == null) {
200                sum = null;
201            } else {
202                sum += i;
203            }
204        }
205        return sum;
206    }
207
208    @Test
209    public void testBoxLoop() {
210        testPartialEscapeAnalysis("testBoxLoopSnippet", 0, 0, BoxNode.class, UnboxNode.class);
211    }
212
213    @SafeVarargs
214    protected final void testPartialEscapeAnalysis(String snippet, double expectedProbability, int expectedCount, Class<? extends Node>... invalidNodeClasses) {
215        prepareGraph(snippet, false);
216        for (AbstractMergeNode merge : graph.getNodes(AbstractMergeNode.TYPE)) {
217            merge.setStateAfter(null);
218        }
219        new DeadCodeEliminationPhase().apply(graph);
220        new CanonicalizerPhase().apply(graph, context);
221        try {
222            Assert.assertTrue("partial escape analysis should have removed all NewInstanceNode allocations", graph.getNodes().filter(NewInstanceNode.class).isEmpty());
223            Assert.assertTrue("partial escape analysis should have removed all NewArrayNode allocations", graph.getNodes().filter(NewArrayNode.class).isEmpty());
224
225            ControlFlowGraph cfg = ControlFlowGraph.compute(graph, true, true, false, false);
226            double probabilitySum = 0;
227            int materializeCount = 0;
228            for (CommitAllocationNode materialize : graph.getNodes().filter(CommitAllocationNode.class)) {
229                probabilitySum += cfg.blockFor(materialize).probability() * materialize.getVirtualObjects().size();
230                materializeCount += materialize.getVirtualObjects().size();
231            }
232            Assert.assertEquals("unexpected number of MaterializeObjectNodes", expectedCount, materializeCount);
233            Assert.assertEquals("unexpected probability of MaterializeObjectNodes", expectedProbability, probabilitySum, 0.01);
234            for (Node node : graph.getNodes()) {
235                for (Class<? extends Node> clazz : invalidNodeClasses) {
236                    Assert.assertFalse("instance of invalid class: " + clazz.getSimpleName(), clazz.isInstance(node) && node.usages().isNotEmpty());
237                }
238            }
239        } catch (AssertionError e) {
240            TypeSystemTest.outputGraph(graph, snippet + ": " + e.getMessage());
241            throw e;
242        }
243    }
244}