src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.replacements.test/src/org/graalvm/compiler/replacements/test/StringCompareToTest.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58299 6df94ce3ab2f
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49451
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
     1
/*
58299
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 55509
diff changeset
     2
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
49451
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
     4
 *
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
     8
 *
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    13
 * accompanied this code).
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    14
 *
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    18
 *
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    21
 * questions.
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    22
 */
50858
2d3e99a72541 8205824: Update Graal
never
parents: 49873
diff changeset
    23
2d3e99a72541 8205824: Update Graal
never
parents: 49873
diff changeset
    24
49451
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    25
package org.graalvm.compiler.replacements.test;
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    26
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52956
diff changeset
    27
import static org.graalvm.compiler.core.common.GraalOptions.RemoveNeverExecutedCode;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52956
diff changeset
    28
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52956
diff changeset
    29
import java.util.List;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52956
diff changeset
    30
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    31
import org.graalvm.compiler.graph.Node;
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    32
import org.graalvm.compiler.nodes.StructuredGraph;
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    33
import org.graalvm.compiler.options.OptionValues;
49451
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    34
import org.graalvm.compiler.replacements.nodes.ArrayCompareToNode;
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    35
import org.graalvm.compiler.serviceprovider.GraalServices;
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52956
diff changeset
    36
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    37
import org.junit.Assert;
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    38
import org.junit.Assume;
49451
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    39
import org.junit.Test;
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    40
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52956
diff changeset
    41
import jdk.vm.ci.aarch64.AArch64;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52956
diff changeset
    42
import jdk.vm.ci.amd64.AMD64;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52956
diff changeset
    43
import jdk.vm.ci.meta.ResolvedJavaMethod;
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    44
49451
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    45
/**
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    46
 * Tests compareTo method intrinsic.
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    47
 */
51436
091c0d22e735 8206992: Update Graal
iveresov
parents: 50858
diff changeset
    48
public class StringCompareToTest extends StringSubstitutionTestBase {
49451
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    49
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    50
    // The compareTo() implementation in java.lang.String has 4 calls to compareTo implementation.
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    51
    private static final int EXPECT_NODE_COUNT = 4;
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    52
    private static final String DISABLE_COMPACTSTRINGS_FLAG = "-XX:-CompactStrings";
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    53
49451
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    54
    public StringCompareToTest() {
51436
091c0d22e735 8206992: Update Graal
iveresov
parents: 50858
diff changeset
    55
        initSubstitution(
091c0d22e735 8206992: Update Graal
iveresov
parents: 50858
diff changeset
    56
                        getResolvedJavaMethod(String.class, "compareTo", String.class),
091c0d22e735 8206992: Update Graal
iveresov
parents: 50858
diff changeset
    57
                        getResolvedJavaMethod("stringCompareTo"),
091c0d22e735 8206992: Update Graal
iveresov
parents: 50858
diff changeset
    58
                        ArrayCompareToNode.class);
49451
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    59
    }
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
    60
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    61
    private int countNode(ResolvedJavaMethod method, Class<?> expectedNode, OptionValues options) {
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    62
        StructuredGraph graph = parseForCompile(method, options);
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    63
        applyFrontEnd(graph);
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    64
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    65
        int c = 0;
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    66
        for (Node node : graph.getNodes()) {
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    67
            if (expectedNode.isInstance(node)) {
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    68
                c += 1;
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    69
            }
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    70
        }
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    71
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    72
        return c;
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    73
    }
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    74
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    75
    @Override
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    76
    protected void initSubstitution(ResolvedJavaMethod theRealMethod,
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    77
                    ResolvedJavaMethod theTestMethod, Class<?> expectedNode) {
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    78
        Assume.assumeTrue((getTarget().arch instanceof AMD64) || (getTarget().arch instanceof AArch64));
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    79
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    80
        realMethod = theRealMethod;
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    81
        testMethod = theTestMethod;
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    82
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    83
        StructuredGraph graph = testGraph(testMethod.getName());
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    84
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    85
        // Check to see if the resulting graph contains the expected node
54601
c40b2a190173 8221598: Update Graal
jwilhelm
parents: 54084
diff changeset
    86
        StructuredGraph replacement = getReplacements().getSubstitution(realMethod, -1, false, null, graph.getOptions());
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    87
        if (replacement == null) {
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    88
            assertInGraph(graph, expectedNode);
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    89
        }
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    90
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    91
        OptionValues options;
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    92
        boolean needCheckNode = true;
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    93
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54601
diff changeset
    94
        if (JavaVersionUtil.JAVA_SPEC <= 8) {
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    95
            needCheckNode = false;
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    96
        } else {
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    97
            List<String> vmArgs = GraalServices.getInputArguments();
52956
4b0b796dd581 8215224: Update Graal
iveresov
parents: 52910
diff changeset
    98
            Assume.assumeTrue(vmArgs != null);
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
    99
            for (String vmArg : vmArgs) {
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
   100
                if (vmArg.equals(DISABLE_COMPACTSTRINGS_FLAG)) {
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
   101
                    needCheckNode = false;
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
   102
                }
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
   103
            }
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
   104
        }
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
   105
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
   106
        if (needCheckNode) {
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
   107
            options = new OptionValues(getInitialOptions(), RemoveNeverExecutedCode, false);
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
   108
            Assert.assertEquals(EXPECT_NODE_COUNT, countNode(testMethod, expectedNode, options));
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
   109
        } else {
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
   110
            options = getInitialOptions();
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
   111
        }
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
   112
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
   113
        // Force compilation.
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
   114
        testCode = getCode(testMethod, options);
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
   115
        Assert.assertNotNull(testCode);
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
   116
    }
583fd71c47d6 8214023: Update Graal
dlong
parents: 51436
diff changeset
   117
49451
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
   118
    public static int stringCompareTo(String a, String b) {
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
   119
        return a.compareTo(b);
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
   120
    }
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
   121
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
   122
    @Test
51436
091c0d22e735 8206992: Update Graal
iveresov
parents: 50858
diff changeset
   123
    @Override
49451
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
   124
    public void testEqualString() {
51436
091c0d22e735 8206992: Update Graal
iveresov
parents: 50858
diff changeset
   125
        super.testEqualString();
49451
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
   126
    }
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
   127
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
   128
    @Test
51436
091c0d22e735 8206992: Update Graal
iveresov
parents: 50858
diff changeset
   129
    @Override
49451
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
   130
    public void testDifferentString() {
51436
091c0d22e735 8206992: Update Graal
iveresov
parents: 50858
diff changeset
   131
        super.testDifferentString();
49451
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
   132
    }
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
   133
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
   134
    @Test
51436
091c0d22e735 8206992: Update Graal
iveresov
parents: 50858
diff changeset
   135
    @Override
49451
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
   136
    public void testAllStrings() {
51436
091c0d22e735 8206992: Update Graal
iveresov
parents: 50858
diff changeset
   137
        super.testAllStrings();
49451
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
   138
    }
e06f9607f370 8198969: Update Graal
iveresov
parents:
diff changeset
   139
}