AMD64HotSpotSafepointOp.java revision 12651:6ef01bd40ce2
1135446Strhodes/*
2224092Sdougb * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
3135446Strhodes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4135446Strhodes *
5193149Sdougb * This code is free software; you can redistribute it and/or modify it
6135446Strhodes * under the terms of the GNU General Public License version 2 only, as
7135446Strhodes * published by the Free Software Foundation.
8135446Strhodes *
9135446Strhodes * This code is distributed in the hope that it will be useful, but WITHOUT
10135446Strhodes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11135446Strhodes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12135446Strhodes * version 2 for more details (a copy is included in the LICENSE file that
13135446Strhodes * accompanied this code).
14135446Strhodes *
15135446Strhodes * You should have received a copy of the GNU General Public License version
16135446Strhodes * 2 along with this work; if not, write to the Free Software Foundation,
17135446Strhodes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18234010Sdougb *
19135446Strhodes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20170222Sdougb * or visit www.oracle.com if you need additional information or have any
21170222Sdougb * questions.
22135446Strhodes */
23135446Strhodespackage org.graalvm.compiler.hotspot.amd64;
24135446Strhodes
25135446Strhodesimport static org.graalvm.compiler.asm.NumUtil.isInt;
26224092Sdougbimport static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC;
27224092Sdougbimport static org.graalvm.compiler.core.common.GraalOptions.ImmutableCode;
28224092Sdougbimport static jdk.vm.ci.amd64.AMD64.rax;
29224092Sdougbimport static jdk.vm.ci.amd64.AMD64.rip;
30135446Strhodes
31135446Strhodesimport org.graalvm.compiler.asm.amd64.AMD64Address;
32135446Strhodesimport org.graalvm.compiler.asm.amd64.AMD64MacroAssembler;
33224092Sdougbimport org.graalvm.compiler.core.common.LIRKind;
34224092Sdougbimport org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
35135446Strhodesimport org.graalvm.compiler.lir.LIRFrameState;
36224092Sdougbimport org.graalvm.compiler.lir.LIRInstructionClass;
37135446Strhodesimport org.graalvm.compiler.lir.Opcode;
38224092Sdougbimport org.graalvm.compiler.lir.amd64.AMD64LIRInstruction;
39224092Sdougbimport org.graalvm.compiler.lir.asm.CompilationResultBuilder;
40224092Sdougbimport org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
41135446Strhodes
42135446Strhodesimport jdk.vm.ci.code.Register;
43135446Strhodesimport jdk.vm.ci.code.RegisterValue;
44135446Strhodesimport jdk.vm.ci.code.site.InfopointReason;
45170222Sdougbimport jdk.vm.ci.meta.AllocatableValue;
46135446Strhodesimport jdk.vm.ci.meta.JavaConstant;
47135446Strhodesimport jdk.vm.ci.meta.JavaKind;
48135446Strhodesimport jdk.vm.ci.meta.Value;
49135446Strhodes
50135446Strhodes/**
51135446Strhodes * Emits a safepoint poll.
52135446Strhodes */
53135446Strhodes@Opcode("SAFEPOINT")
54135446Strhodespublic final class AMD64HotSpotSafepointOp extends AMD64LIRInstruction {
55135446Strhodes    public static final LIRInstructionClass<AMD64HotSpotSafepointOp> TYPE = LIRInstructionClass.create(AMD64HotSpotSafepointOp.class);
56135446Strhodes
57135446Strhodes    @State protected LIRFrameState state;
58135446Strhodes    @Temp({OperandFlag.REG, OperandFlag.ILLEGAL}) private AllocatableValue temp;
59135446Strhodes
60135446Strhodes    private final GraalHotSpotVMConfig config;
61135446Strhodes
62135446Strhodes    public AMD64HotSpotSafepointOp(LIRFrameState state, GraalHotSpotVMConfig config, NodeLIRBuilderTool tool) {
63135446Strhodes        super(TYPE);
64135446Strhodes        this.state = state;
65135446Strhodes        this.config = config;
66135446Strhodes        if (isPollingPageFar(config) || ImmutableCode.getValue()) {
67135446Strhodes            temp = tool.getLIRGeneratorTool().newVariable(LIRKind.value(tool.getLIRGeneratorTool().target().arch.getWordKind()));
68135446Strhodes        } else {
69135446Strhodes            // Don't waste a register if it's unneeded
70135446Strhodes            temp = Value.ILLEGAL;
71135446Strhodes        }
72135446Strhodes    }
73135446Strhodes
74135446Strhodes    @Override
75224092Sdougb    public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler asm) {
76224092Sdougb        emitCode(crb, asm, config, false, state, temp instanceof RegisterValue ? ((RegisterValue) temp).getRegister() : null);
77224092Sdougb    }
78224092Sdougb
79224092Sdougb    /**
80224092Sdougb     * Tests if the polling page address can be reached from the code cache with 32-bit
81224092Sdougb     * displacements.
82224092Sdougb     */
83224092Sdougb    private static boolean isPollingPageFar(GraalHotSpotVMConfig config) {
84224092Sdougb        final long pollingPageAddress = config.safepointPollingAddress;
85224092Sdougb        return config.forceUnreachable || !isInt(pollingPageAddress - config.codeCacheLowBound) || !isInt(pollingPageAddress - config.codeCacheHighBound);
86224092Sdougb    }
87224092Sdougb
88224092Sdougb    public static void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler asm, GraalHotSpotVMConfig config, boolean atReturn, LIRFrameState state, Register scratch) {
89224092Sdougb        assert !atReturn || state == null : "state is unneeded at return";
90224092Sdougb        if (ImmutableCode.getValue()) {
91224092Sdougb            JavaKind hostWordKind = JavaKind.Long;
92224092Sdougb            int alignment = hostWordKind.getBitCount() / Byte.SIZE;
93224092Sdougb            JavaConstant pollingPageAddress = JavaConstant.forIntegerKind(hostWordKind, config.safepointPollingAddress);
94224092Sdougb            // This move will be patched to load the safepoint page from a data segment
95224092Sdougb            // co-located with the immutable code.
96224092Sdougb            if (GeneratePIC.getValue()) {
97224092Sdougb                asm.movq(scratch, asm.getPlaceholder(-1));
98224092Sdougb            } else {
99224092Sdougb                asm.movq(scratch, (AMD64Address) crb.recordDataReferenceInCode(pollingPageAddress, alignment));
100224092Sdougb            }
101224092Sdougb            final int pos = asm.position();
102224092Sdougb            crb.recordMark(atReturn ? config.MARKID_POLL_RETURN_FAR : config.MARKID_POLL_FAR);
103224092Sdougb            if (state != null) {
104224092Sdougb                crb.recordInfopoint(pos, state, InfopointReason.SAFEPOINT);
105224092Sdougb            }
106224092Sdougb            asm.testl(rax, new AMD64Address(scratch));
107224092Sdougb        } else if (isPollingPageFar(config)) {
108224092Sdougb            asm.movq(scratch, config.safepointPollingAddress);
109224092Sdougb            crb.recordMark(atReturn ? config.MARKID_POLL_RETURN_FAR : config.MARKID_POLL_FAR);
110224092Sdougb            final int pos = asm.position();
111224092Sdougb            if (state != null) {
112224092Sdougb                crb.recordInfopoint(pos, state, InfopointReason.SAFEPOINT);
113224092Sdougb            }
114224092Sdougb            asm.testl(rax, new AMD64Address(scratch));
115224092Sdougb        } else {
116224092Sdougb            crb.recordMark(atReturn ? config.MARKID_POLL_RETURN_NEAR : config.MARKID_POLL_NEAR);
117224092Sdougb            final int pos = asm.position();
118224092Sdougb            if (state != null) {
119224092Sdougb                crb.recordInfopoint(pos, state, InfopointReason.SAFEPOINT);
120224092Sdougb            }
121224092Sdougb            // The C++ code transforms the polling page offset into an RIP displacement
122224092Sdougb            // to the real address at that offset in the polling page.
123224092Sdougb            asm.testl(rax, new AMD64Address(rip, 0));
124224092Sdougb        }
125224092Sdougb    }
126224092Sdougb}
127224092Sdougb