001/*
002 * Copyright (c) 2014, 2015, 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 */
023
024package com.oracle.graal.hotspot.test;
025
026import jdk.internal.jvmci.hotspot.*;
027
028import org.junit.*;
029
030import com.oracle.graal.api.replacements.*;
031import com.oracle.graal.compiler.test.*;
032import com.oracle.graal.graph.*;
033import com.oracle.graal.hotspot.nodes.*;
034import com.oracle.graal.hotspot.nodes.CompressionNode.CompressionOp;
035import com.oracle.graal.nodeinfo.*;
036import com.oracle.graal.nodes.*;
037import com.oracle.graal.nodes.calc.*;
038import com.oracle.graal.nodes.spi.*;
039
040public class DataPatchTest extends GraalCompilerTest {
041
042    public static double doubleSnippet() {
043        return 84.72;
044    }
045
046    @Test
047    public void doubleTest() {
048        test("doubleSnippet");
049    }
050
051    public static Object oopSnippet() {
052        return "asdf";
053    }
054
055    @Test
056    public void oopTest() {
057        test("oopSnippet");
058    }
059
060    private static Object compressUncompress(Object obj) {
061        return obj;
062    }
063
064    public static Object narrowOopSnippet() {
065        return compressUncompress("narrowAsdf");
066    }
067
068    @Test
069    public void narrowOopTest() {
070        Assume.assumeTrue("skipping narrow oop data patch test", config.useCompressedOops);
071        test("narrowOopSnippet");
072    }
073
074    private static final HotSpotVMConfig config = HotSpotJVMCIRuntime.runtime().getConfig();
075    private static boolean initReplacements = false;
076
077    @Before
078    public void initReplacements() {
079        if (!initReplacements) {
080            getReplacements().registerSubstitutions(DataPatchTest.class, DataPatchTestSubstitutions.class);
081            initReplacements = true;
082        }
083    }
084
085    @ClassSubstitution(DataPatchTest.class)
086    private static class DataPatchTestSubstitutions {
087
088        @MethodSubstitution
089        public static Object compressUncompress(Object obj) {
090            Object compressed = CompressionNode.compression(CompressionOp.Compress, obj, config.getOopEncoding());
091            Object proxy = ConstantFoldBarrier.wrap(compressed);
092            return CompressionNode.compression(CompressionOp.Uncompress, proxy, config.getOopEncoding());
093        }
094    }
095
096    @NodeInfo
097    private static final class ConstantFoldBarrier extends FloatingNode implements LIRLowerable {
098
099        public static final NodeClass<ConstantFoldBarrier> TYPE = NodeClass.create(ConstantFoldBarrier.class);
100        @Input protected ValueNode input;
101
102        public ConstantFoldBarrier(ValueNode input) {
103            super(TYPE, input.stamp());
104            this.input = input;
105        }
106
107        public void generate(NodeLIRBuilderTool generator) {
108            generator.setResult(this, generator.operand(input));
109        }
110
111        @NodeIntrinsic
112        public static native Object wrap(Object object);
113    }
114}