001/* 002 * Copyright (c) 2015, 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.replacements.test; 024 025import com.oracle.graal.debug.*; 026import jdk.internal.jvmci.meta.*; 027 028import org.junit.*; 029 030import com.oracle.graal.compiler.common.type.*; 031import com.oracle.graal.compiler.test.*; 032import com.oracle.graal.graphbuilderconf.*; 033import com.oracle.graal.graphbuilderconf.InvocationPlugins.Registration; 034import com.oracle.graal.nodes.*; 035import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions; 036import com.oracle.graal.nodes.memory.HeapAccess.BarrierType; 037import com.oracle.graal.nodes.memory.*; 038import com.oracle.graal.nodes.memory.address.*; 039import com.oracle.graal.phases.*; 040import com.oracle.graal.phases.common.*; 041import com.oracle.graal.phases.tiers.*; 042import com.oracle.graal.replacements.*; 043 044public class PEGraphDecoderTest extends GraalCompilerTest { 045 046 /** 047 * This method is intrinsified to a node with a guard dependency on the block it is in. The 048 * various tests ensure that this guard is correctly updated when blocks are merged during 049 * inlining. 050 */ 051 private static native int readInt(Object obj, long offset); 052 053 private static boolean flag; 054 private static int value; 055 056 private static void invokeSimple() { 057 value = 111; 058 } 059 060 private static void invokeComplicated() { 061 if (flag) { 062 value = 0; 063 } else { 064 value = 42; 065 } 066 } 067 068 private static int readInt1(Object obj) { 069 return readInt(obj, 16); 070 } 071 072 private static int readInt2(Object obj) { 073 invokeSimple(); 074 return readInt(obj, 16); 075 } 076 077 private static int readInt3(Object obj) { 078 invokeComplicated(); 079 return readInt(obj, 16); 080 } 081 082 private static int readInt4(Object obj, int n) { 083 if (n > 0) { 084 invokeComplicated(); 085 } 086 return readInt(obj, 16); 087 } 088 089 public static int doTest(Object obj) { 090 int result = 0; 091 result += readInt1(obj); 092 result += readInt2(obj); 093 result += readInt3(obj); 094 result += readInt4(obj, 2); 095 return result; 096 } 097 098 private static void registerPlugins(InvocationPlugins plugins) { 099 Registration r = new Registration(plugins, PEGraphDecoderTest.class); 100 r.register2("readInt", Object.class, long.class, new InvocationPlugin() { 101 @Override 102 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unused, ValueNode obj, ValueNode offset) { 103 AddressNode address = b.add(new OffsetAddressNode(obj, offset)); 104 ReadNode read = b.addPush(Kind.Int, new ReadNode(address, LocationIdentity.any(), StampFactory.forKind(Kind.Int), BarrierType.NONE)); 105 read.setGuard(AbstractBeginNode.prevBegin(read)); 106 return true; 107 } 108 }); 109 } 110 111 class InlineAll implements InlineInvokePlugin { 112 @Override 113 public InlineInfo shouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args, JavaType returnType) { 114 return new InlineInfo(method, false); 115 } 116 } 117 118 @Test 119 public void test() { 120 ResolvedJavaMethod testMethod = getResolvedJavaMethod(PEGraphDecoderTest.class, "doTest", Object.class); 121 StructuredGraph targetGraph = null; 122 try (Debug.Scope scope = Debug.scope("GraphPETest", testMethod)) { 123 GraphBuilderConfiguration graphBuilderConfig = GraphBuilderConfiguration.getEagerDefault(getDefaultGraphBuilderPlugins()); 124 registerPlugins(graphBuilderConfig.getPlugins().getInvocationPlugins()); 125 CachingPEGraphDecoder decoder = new CachingPEGraphDecoder(getProviders(), graphBuilderConfig, OptimisticOptimizations.NONE, AllowAssumptions.YES, getTarget().arch); 126 127 targetGraph = new StructuredGraph(testMethod, AllowAssumptions.YES); 128 decoder.decode(targetGraph, testMethod, null, null, new InlineInvokePlugin[]{new InlineAll()}, null); 129 Debug.dump(targetGraph, "Target Graph"); 130 targetGraph.verify(); 131 132 PhaseContext context = new PhaseContext(getProviders()); 133 new CanonicalizerPhase().apply(targetGraph, context); 134 targetGraph.verify(); 135 136 } catch (Throwable ex) { 137 if (targetGraph != null) { 138 Debug.dump(targetGraph, ex.toString()); 139 } 140 Debug.handle(ex); 141 } 142 } 143}