1 /*
2  * Copyright (c) 2011, 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.replacements.test;
26 
27 import org.graalvm.compiler.core.common.CompilationIdentifier;
28 import org.graalvm.compiler.core.test.GraalCompilerTest;
29 import org.graalvm.compiler.nodes.StructuredGraph;
30 import org.graalvm.compiler.options.OptionValues;
31 
32 import jdk.vm.ci.code.InstalledCode;
33 import jdk.vm.ci.meta.JavaTypeProfile;
34 import jdk.vm.ci.meta.JavaTypeProfile.ProfiledType;
35 import jdk.vm.ci.meta.ResolvedJavaMethod;
36 import jdk.vm.ci.meta.TriState;
37 
38 /**
39  * Base class for instanceof test classes.
40  */
41 public abstract class TypeCheckTest extends GraalCompilerTest {
42 
replaceProfile(StructuredGraph graph, JavaTypeProfile profile)43     protected abstract void replaceProfile(StructuredGraph graph, JavaTypeProfile profile);
44 
45     protected JavaTypeProfile currentProfile;
46 
47     @Override
parseForCompile(ResolvedJavaMethod method, CompilationIdentifier compilationId, OptionValues options)48     protected StructuredGraph parseForCompile(ResolvedJavaMethod method, CompilationIdentifier compilationId, OptionValues options) {
49         StructuredGraph graph = super.parseForCompile(method, compilationId, options);
50         if (currentProfile != null) {
51             replaceProfile(graph, currentProfile);
52         }
53         return graph;
54     }
55 
56     @Override
getCode(final ResolvedJavaMethod method, final StructuredGraph graph, boolean ignore, boolean installAsDefault, OptionValues options)57     protected InstalledCode getCode(final ResolvedJavaMethod method, final StructuredGraph graph, boolean ignore, boolean installAsDefault, OptionValues options) {
58         return super.getCode(method, graph, currentProfile != null, installAsDefault, options);
59     }
60 
profile(Class<?>.... types)61     protected JavaTypeProfile profile(Class<?>... types) {
62         return profile(TriState.FALSE, types);
63     }
64 
profile(TriState nullSeen, Class<?>... types)65     protected JavaTypeProfile profile(TriState nullSeen, Class<?>... types) {
66         if (types.length == 0) {
67             return null;
68         }
69         ProfiledType[] ptypes = new ProfiledType[types.length];
70         for (int i = 0; i < types.length; i++) {
71             ptypes[i] = new ProfiledType(getMetaAccess().lookupJavaType(types[i]), 1.0D / types.length);
72         }
73         return new JavaTypeProfile(nullSeen, 0.0D, ptypes);
74     }
75 
test(String name, JavaTypeProfile profile, Object... args)76     protected void test(String name, JavaTypeProfile profile, Object... args) {
77         assert currentProfile == null;
78         currentProfile = profile;
79         try {
80             super.test(name, args);
81         } finally {
82             currentProfile = null;
83         }
84     }
85 }
86