001/* 002 * Copyright (c) 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 */ 023package com.oracle.graal.compiler.test; 024 025import jdk.internal.jvmci.code.*; 026import jdk.internal.jvmci.meta.*; 027import jdk.internal.jvmci.meta.Assumptions.*; 028 029import com.oracle.graal.nodes.*; 030import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions; 031 032public abstract class GraalCompilerAssumptionsTest extends GraalCompilerTest { 033 034 public GraalCompilerAssumptionsTest() { 035 super(); 036 } 037 038 public GraalCompilerAssumptionsTest(Class<? extends Architecture> arch) { 039 super(arch); 040 } 041 042 protected void testAssumptionInvalidate(String methodName, Assumption expected, String classToLoad) { 043 testAssumption(methodName, expected, classToLoad, true); 044 } 045 046 /** 047 * Checks the behavior of class loading on {@link Assumption invalidation}. {@code methodName} 048 * is compiled and the resulting graph is checked for {@code expectedAssumption}. The code is 049 * installed and optionally {@code classToLoad} is loaded. The class is assumed to be an inner 050 * class of the test class and the name of the class to load is constructed relative to that. 051 * 052 * @param methodName the method to compile 053 * @param expectedAssumption expected {@link Assumption} instance to find in graph 054 * @param classToLoad an optional class to load to trigger an invalidation check 055 * @param willInvalidate true if loading {@code classToLoad} should invalidate the method 056 */ 057 protected void testAssumption(String methodName, Assumption expectedAssumption, String classToLoad, boolean willInvalidate) { 058 ResolvedJavaMethod javaMethod = getResolvedJavaMethod(methodName); 059 060 StructuredGraph graph = parseEager(javaMethod, AllowAssumptions.YES); 061 assertTrue(!graph.getAssumptions().isEmpty()); 062 checkGraph(expectedAssumption, graph); 063 064 CompilationResult compilationResult = compile(javaMethod, graph); 065 final InstalledCode installedCode = getProviders().getCodeCache().setDefaultMethod(javaMethod, compilationResult); 066 assertTrue(installedCode.isValid()); 067 if (classToLoad != null) { 068 String fullName = getClass().getName() + "$" + classToLoad; 069 try { 070 Class.forName(fullName); 071 } catch (ClassNotFoundException e) { 072 fail("Can't find class %s", fullName); 073 } 074 assertTrue(!willInvalidate == installedCode.isValid(), "method should be %s", willInvalidate ? "invalid" : "valid"); 075 } 076 } 077 078 protected void checkGraph(Assumption expectedAssumption, StructuredGraph graph) { 079 boolean found = false; 080 for (Assumption a : graph.getAssumptions()) { 081 if (expectedAssumption.equals(a)) { 082 found = true; 083 } 084 } 085 assertTrue(found, "Can't find assumption %s", expectedAssumption); 086 } 087 088 /** 089 * Converts a {@link Class} to an initialized {@link ResolvedJavaType}. 090 */ 091 protected ResolvedJavaType resolveAndInitialize(Class<?> clazz) { 092 ResolvedJavaType type = getMetaAccess().lookupJavaType(clazz); 093 type.initialize(); 094 return type; 095 } 096}