1 /* 2 * Copyright (c) 2016, 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 package org.graalvm.compiler.hotspot.amd64.test; 25 26 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG; 27 28 import org.junit.Assume; 29 import org.junit.Before; 30 import org.junit.Test; 31 32 import org.graalvm.compiler.api.replacements.Snippet; 33 import org.graalvm.compiler.asm.amd64.AMD64Address; 34 import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler; 35 import org.graalvm.compiler.core.common.LIRKind; 36 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor; 37 import org.graalvm.compiler.core.common.type.DataPointerConstant; 38 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage; 39 import org.graalvm.compiler.hotspot.meta.HotSpotForeignCallsProviderImpl; 40 import org.graalvm.compiler.hotspot.meta.HotSpotProviders; 41 import org.graalvm.compiler.hotspot.stubs.SnippetStub; 42 import org.graalvm.compiler.lir.LIRInstructionClass; 43 import org.graalvm.compiler.lir.Variable; 44 import org.graalvm.compiler.lir.amd64.AMD64LIRInstruction; 45 import org.graalvm.compiler.lir.asm.ArrayDataPointerConstant; 46 import org.graalvm.compiler.lir.asm.CompilationResultBuilder; 47 import org.graalvm.compiler.lir.gen.LIRGeneratorTool; 48 import org.graalvm.compiler.lir.jtt.LIRTest; 49 import org.graalvm.compiler.lir.jtt.LIRTestSpecification; 50 import org.graalvm.compiler.nodes.extended.ForeignCallNode; 51 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration; 52 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext; 53 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin; 54 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins; 55 56 import jdk.vm.ci.amd64.AMD64; 57 import jdk.vm.ci.amd64.AMD64.CPUFeature; 58 import jdk.vm.ci.amd64.AMD64Kind; 59 import jdk.vm.ci.code.Register; 60 import jdk.vm.ci.code.ValueUtil; 61 import jdk.vm.ci.meta.AllocatableValue; 62 import jdk.vm.ci.meta.ResolvedJavaMethod; 63 import jdk.vm.ci.meta.Value; 64 65 public class StubAVXTest extends LIRTest { 66 67 @Before 68 public void checkAMD64() { 69 Assume.assumeTrue("skipping AMD64 specific test", getTarget().arch instanceof AMD64); 70 Assume.assumeTrue("skipping AVX test", ((AMD64) getTarget().arch).getFeatures().contains(CPUFeature.AVX)); 71 } 72 73 private static final DataPointerConstant avxConstant = new ArrayDataPointerConstant(new float[]{1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f}, 32); 74 75 private static class LoadAVXConstant extends AMD64LIRInstruction { 76 public static final LIRInstructionClass<LoadAVXConstant> TYPE = LIRInstructionClass.create(LoadAVXConstant.class); 77 78 @Def({REG}) AllocatableValue result; 79 80 LoadAVXConstant(AllocatableValue result) { 81 super(TYPE); 82 this.result = result; 83 } 84 85 @Override 86 public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) { 87 masm.vmovdqu(ValueUtil.asRegister(result), (AMD64Address) crb.recordDataReferenceInCode(avxConstant)); 88 } 89 } 90 91 private static final LIRTestSpecification loadAVXConstant = new LIRTestSpecification() { 92 93 @Override 94 public void generate(LIRGeneratorTool gen) { 95 Variable ret = gen.newVariable(LIRKind.value(AMD64Kind.V256_SINGLE)); 96 gen.append(new LoadAVXConstant(ret)); 97 setResult(ret); 98 } 99 }; 100 101 @LIRIntrinsic 102 public static Object loadAVXConstant(@SuppressWarnings("unused") LIRTestSpecification spec) { 103 return null; 104 } 105 106 private static class CompareAVXRegister extends AMD64LIRInstruction { 107 public static final LIRInstructionClass<CompareAVXRegister> TYPE = LIRInstructionClass.create(CompareAVXRegister.class); 108 109 @Def({REG}) AllocatableValue result; 110 @Use({REG}) AllocatableValue left; 111 @Use({REG}) AllocatableValue right; 112 @Temp({REG}) AllocatableValue temp; 113 114 CompareAVXRegister(AllocatableValue result, AllocatableValue left, AllocatableValue right, AllocatableValue temp) { 115 super(TYPE); 116 this.result = result; 117 this.left = left; 118 this.right = right; 119 this.temp = temp; 120 } 121 122 private static int getRXB(Register reg, Register rm) { 123 int rxb = (reg.encoding & 0x08) >> 1; 124 rxb |= (rm.encoding & 0x08) >> 3; 125 return rxb; 126 } 127 128 @Override 129 public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) { 130 Register res = ValueUtil.asRegister(result); 131 Register x = ValueUtil.asRegister(left); 132 Register y = ValueUtil.asRegister(right); 133 Register tmp = ValueUtil.asRegister(temp); 134 135 // VEX.NDS.256.0F.WIG C2 /r ib(0) 136 // VCMPPS tmp, x, y, EQ 137 masm.emitByte(0xC4); // VEX 3-byte 138 masm.emitByte((~getRXB(tmp, y) & 0x7) << 5 | 0x01); // RXB m-mmmmm (0F) 139 masm.emitByte(((~x.encoding & 0x0f) << 3) | 0b1_00); // W(0) vvvv L(1) pp(0) 140 masm.emitByte(0xC2); 141 masm.emitByte(0xC0 | ((tmp.encoding & 0x07) << 3) | (y.encoding & 0x07)); 142 masm.emitByte(0); 143 144 // VEX.256.0F.WIG 50 /r 145 // VMOVMSKPS res, tmp 146 masm.emitByte(0xC4); // VEX 3-byte 147 masm.emitByte((~getRXB(res, tmp) & 0x7) << 5 | 0x01); // RXB m-mmmmm (0F) 148 masm.emitByte(0b0_1111_1_00); // W(0) vvvv L(1) pp(0) 149 masm.emitByte(0x50); 150 masm.emitByte(0xC0 | ((res.encoding & 0x07) << 3) | (tmp.encoding & 0x07)); 151 } 152 } 153 154 private static final LIRTestSpecification compareAVXRegister = new LIRTestSpecification() { 155 156 @Override 157 public void generate(LIRGeneratorTool gen, Value arg0, Value arg1) { 158 Variable ret = gen.newVariable(LIRKind.value(AMD64Kind.DWORD)); 159 gen.append(new CompareAVXRegister(ret, gen.asAllocatable(arg0), gen.asAllocatable(arg1), gen.newVariable(LIRKind.value(AMD64Kind.V256_QWORD)))); 160 setResult(ret); 161 } 162 }; 163 164 private static class TestStub extends SnippetStub { 165 166 TestStub(HotSpotProviders providers, HotSpotForeignCallLinkage linkage) { 167 super("testStub", providers, linkage); 168 } 169 170 @Snippet 171 static void testStub() { 172 } 173 } 174 175 public static final ForeignCallDescriptor TEST_STUB = new ForeignCallDescriptor("test_stub", void.class); 176 177 @LIRIntrinsic 178 public static int compareAVXRegister(@SuppressWarnings("unused") LIRTestSpecification spec, Object left, Object right) { 179 return left == right ? 0xff : 0; 180 } 181 182 @Override 183 protected GraphBuilderConfiguration editGraphBuilderConfiguration(GraphBuilderConfiguration conf) { 184 InvocationPlugins invocationPlugins = conf.getPlugins().getInvocationPlugins(); 185 InvocationPlugins.Registration r = new InvocationPlugins.Registration(invocationPlugins, TestStub.class); 186 r.register0("testStub", new InvocationPlugin() { 187 @Override 188 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, InvocationPlugin.Receiver receiver) { 189 b.add(new ForeignCallNode(getProviders().getForeignCalls(), TEST_STUB)); 190 return true; 191 } 192 }); 193 return super.editGraphBuilderConfiguration(conf); 194 } 195 196 public static int testStub() { 197 Object preStub = loadAVXConstant(loadAVXConstant); 198 199 // do something to potentially destroy the value 200 TestStub.testStub(); 201 202 Object postStub = loadAVXConstant(loadAVXConstant); 203 return compareAVXRegister(compareAVXRegister, preStub, postStub); 204 } 205 206 @Test 207 public void test() { 208 HotSpotProviders providers = (HotSpotProviders) getProviders(); 209 HotSpotForeignCallsProviderImpl foreignCalls = (HotSpotForeignCallsProviderImpl) providers.getForeignCalls(); 210 HotSpotForeignCallLinkage linkage = foreignCalls.registerStubCall(TEST_STUB, true, HotSpotForeignCallLinkage.Transition.LEAF_NOFP); 211 linkage.setCompiledStub(new TestStub(providers, linkage)); 212 runTest("testStub"); 213 } 214 }