1 /*
   2  * Copyright (c) 2016, 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 java.lang.ref.Reference;
  28 import java.lang.ref.ReferenceQueue;
  29 import java.lang.ref.WeakReference;
  30 
  31 import org.junit.Test;
  32 import org.graalvm.compiler.graph.Node;
  33 import org.graalvm.compiler.loop.LoopEx;
  34 import org.graalvm.compiler.loop.LoopsData;
  35 import org.graalvm.compiler.nodes.FieldLocationIdentity;
  36 import org.graalvm.compiler.nodes.StructuredGraph;
  37 import org.graalvm.compiler.nodes.memory.Access;
  38 import jdk.internal.vm.compiler.word.LocationIdentity;
  39 
  40 import jdk.vm.ci.meta.ResolvedJavaField;
  41 
  42 public class ReferenceGetLoopTest extends GraalCompilerTest {
  43 
  44     @Override
  45     protected boolean checkMidTierGraph(StructuredGraph graph) {
  46         final LoopsData loops = new LoopsData(graph);
  47         boolean found = false;
  48         for (LoopEx loop : loops.loops()) {
  49             for (Node node : loop.inside().nodes()) {
  50                 if (node instanceof Access) {
  51                     Access access = (Access) node;
  52                     LocationIdentity location = access.getLocationIdentity();
  53                     if (location instanceof FieldLocationIdentity) {
  54                         ResolvedJavaField field = ((FieldLocationIdentity) location).getField();
  55                         if (field.getName().equals("referent") && field.getDeclaringClass().equals(getMetaAccess().lookupJavaType(Reference.class))) {
  56                             found = true;
  57                         }
  58                     }
  59                 }
  60             }
  61         }
  62         if (!found) {
  63             assertTrue(false, "Reference.referent not found in loop: " + getCanonicalGraphString(graph, true, false));
  64         }
  65         return true;
  66     }
  67 
  68     public volatile Object referent;
  69     public final FinalWeakReference<Object> ref;
  70     public final ReferenceQueue<Object> refQueue;
  71 
  72     /*
  73      * Ensure that the Reference.get invoke is statically bindable.
  74      */
  75     public static final class FinalWeakReference<T> extends WeakReference<T> {
  76         public FinalWeakReference(T referent, ReferenceQueue<? super T> q) {
  77             super(referent, q);
  78         }
  79     }
  80 
  81     public ReferenceGetLoopTest() {
  82         referent = new Object();
  83         refQueue = new ReferenceQueue<>();
  84         ref = new FinalWeakReference<>(referent, refQueue);
  85     }
  86 
  87     @Test
  88     public void test() {
  89         getCode(getMetaAccess().lookupJavaMethod(getMethod("testSnippet")));
  90     }
  91 
  92     public void testSnippet() {
  93         while (ref.get() != null) {
  94             // burn!
  95         }
  96     }
  97 }