1 /*
2  * Copyright (c) 2018, 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.ea;
26 
27 import java.util.concurrent.atomic.AtomicReference;
28 import java.util.concurrent.atomic.AtomicReferenceArray;
29 
30 import org.graalvm.compiler.nodes.java.LogicCompareAndSwapNode;
31 import org.junit.Test;
32 
33 import jdk.vm.ci.meta.JavaConstant;
34 
35 public class UnsafeCompareAndSwapVirtualizationTest extends EATestBase {
36 
37     private static Object obj1 = new Object();
38     private static Object obj2 = new Object();
39     private static final Object OBJ1 = new Object();
40 
bothVirtualNoMatch()41     public static boolean bothVirtualNoMatch() {
42         AtomicReference<Object> a = new AtomicReference<>();
43         return a.compareAndSet(new Object(), new Object());
44     }
45 
46     @Test
bothVirtualNoMatchTest()47     public void bothVirtualNoMatchTest() {
48         testEscapeAnalysis("bothVirtualNoMatch", JavaConstant.INT_0, true);
49         assertTrue(graph.getNodes(LogicCompareAndSwapNode.TYPE).isEmpty());
50     }
51 
bothVirtualMatch()52     public static boolean bothVirtualMatch() {
53         Object expect = new Object();
54         AtomicReference<Object> a = new AtomicReference<>(expect);
55         return a.compareAndSet(expect, new Object());
56     }
57 
58     @Test
bothVirtualMatchTest()59     public void bothVirtualMatchTest() {
60         testEscapeAnalysis("bothVirtualMatch", JavaConstant.INT_1, true);
61         assertTrue(graph.getNodes(LogicCompareAndSwapNode.TYPE).isEmpty());
62     }
63 
expectedVirtualMatch()64     public static boolean expectedVirtualMatch() {
65         Object o = new Object();
66         AtomicReference<Object> a = new AtomicReference<>(o);
67         return a.compareAndSet(o, obj1);
68     }
69 
70     @Test
expectedVirtualMatchTest()71     public void expectedVirtualMatchTest() {
72         testEscapeAnalysis("expectedVirtualMatch", JavaConstant.INT_1, true);
73         assertTrue(graph.getNodes(LogicCompareAndSwapNode.TYPE).isEmpty());
74     }
75 
expectedVirtualNoMatch()76     public static boolean expectedVirtualNoMatch() {
77         Object o = new Object();
78         AtomicReference<Object> a = new AtomicReference<>();
79         return a.compareAndSet(o, obj1);
80     }
81 
82     @Test
expectedVirtualNoMatchTest()83     public void expectedVirtualNoMatchTest() {
84         testEscapeAnalysis("expectedVirtualNoMatch", JavaConstant.INT_0, true);
85         assertTrue(graph.getNodes(LogicCompareAndSwapNode.TYPE).isEmpty());
86     }
87 
bothNonVirtualNoMatch()88     public static boolean bothNonVirtualNoMatch() {
89         AtomicReference<Object> a = new AtomicReference<>();
90         return a.compareAndSet(OBJ1, obj2);
91     }
92 
93     @Test
bothNonVirtualNoMatchTest()94     public void bothNonVirtualNoMatchTest() {
95         testEscapeAnalysis("bothNonVirtualNoMatch", JavaConstant.INT_0, true);
96         assertTrue(graph.getNodes(LogicCompareAndSwapNode.TYPE).isEmpty());
97     }
98 
bothNonVirtualMatch()99     public static boolean bothNonVirtualMatch() {
100         AtomicReference<Object> a = new AtomicReference<>(OBJ1);
101         return a.compareAndSet(OBJ1, obj2);
102     }
103 
104     @Test
bothNonVirtualMatchTest()105     public void bothNonVirtualMatchTest() {
106         testEscapeAnalysis("bothNonVirtualMatch", JavaConstant.INT_1, true);
107         assertTrue(graph.getNodes(LogicCompareAndSwapNode.TYPE).isEmpty());
108     }
109 
onlyInitialValueVirtualNoMatch()110     public static boolean onlyInitialValueVirtualNoMatch() {
111         AtomicReference<Object> a = new AtomicReference<>(new Object());
112         return a.compareAndSet(obj1, obj2);
113     }
114 
115     @Test
onlyInitialValueVirtualNoMatchTest()116     public void onlyInitialValueVirtualNoMatchTest() {
117         testEscapeAnalysis("onlyInitialValueVirtualNoMatch", JavaConstant.INT_0, true);
118         assertTrue(graph.getNodes(LogicCompareAndSwapNode.TYPE).isEmpty());
119     }
120 
onlyInitialValueVirtualMatch()121     public static boolean onlyInitialValueVirtualMatch() {
122         Object o = new Object();
123         AtomicReference<Object> a = new AtomicReference<>(o);
124         return a.compareAndSet(o, obj2);
125     }
126 
127     @Test
onlyInitialValueVirtualMatchTest()128     public void onlyInitialValueVirtualMatchTest() {
129         testEscapeAnalysis("onlyInitialValueVirtualMatch", JavaConstant.INT_1, true);
130         assertTrue(graph.getNodes(LogicCompareAndSwapNode.TYPE).isEmpty());
131     }
132 
bothVirtualNoMatchArray()133     public static boolean bothVirtualNoMatchArray() {
134         AtomicReferenceArray<Object> array = new AtomicReferenceArray<>(1);
135         return array.compareAndSet(0, new Object(), new Object());
136     }
137 
138     @Test
bothVirtualNoMatchArrayTest()139     public void bothVirtualNoMatchArrayTest() {
140         testEscapeAnalysis("bothVirtualNoMatchArray", JavaConstant.INT_0, true);
141         assertTrue(graph.getNodes(LogicCompareAndSwapNode.TYPE).isEmpty());
142     }
143 }
144