1/*
2 * Copyright (c) 2015, 2015, 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 */
23package org.graalvm.compiler.hotspot.amd64;
24
25import static org.graalvm.compiler.lir.LIRValueUtil.asJavaConstant;
26import static org.graalvm.compiler.lir.LIRValueUtil.isJavaConstant;
27import static jdk.vm.ci.amd64.AMD64.rax;
28import static jdk.vm.ci.amd64.AMD64.rbx;
29import static jdk.vm.ci.code.ValueUtil.asRegister;
30import static jdk.vm.ci.code.ValueUtil.isRegister;
31
32import org.graalvm.compiler.asm.amd64.AMD64Address;
33import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler;
34import org.graalvm.compiler.debug.GraalError;
35import org.graalvm.compiler.hotspot.HotSpotCounterOp;
36import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
37import org.graalvm.compiler.hotspot.meta.HotSpotRegistersProvider;
38import org.graalvm.compiler.lir.LIRInstructionClass;
39import org.graalvm.compiler.lir.Opcode;
40import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
41
42import jdk.vm.ci.code.Register;
43import jdk.vm.ci.code.TargetDescription;
44import jdk.vm.ci.meta.AllocatableValue;
45import jdk.vm.ci.meta.Value;
46
47@Opcode("BenchMarkCounter")
48public class AMD64HotSpotCounterOp extends HotSpotCounterOp {
49    public static final LIRInstructionClass<AMD64HotSpotCounterOp> TYPE = LIRInstructionClass.create(AMD64HotSpotCounterOp.class);
50
51    @Alive({OperandFlag.STACK, OperandFlag.UNINITIALIZED}) private AllocatableValue backupSlot;
52
53    public AMD64HotSpotCounterOp(String name, String group, Value increment, HotSpotRegistersProvider registers, GraalHotSpotVMConfig config, AllocatableValue backupSlot) {
54        super(TYPE, name, group, increment, registers, config);
55        this.backupSlot = backupSlot;
56    }
57
58    public AMD64HotSpotCounterOp(String[] names, String[] groups, Value[] increments, HotSpotRegistersProvider registers, GraalHotSpotVMConfig config, AllocatableValue backupSlot) {
59        super(TYPE, names, groups, increments, registers, config);
60        this.backupSlot = backupSlot;
61    }
62
63    @Override
64    public void emitCode(CompilationResultBuilder crb) {
65        AMD64MacroAssembler masm = (AMD64MacroAssembler) crb.asm;
66        TargetDescription target = crb.target;
67
68        Register scratch;
69        // It can happen that the rax register is the increment register, in this case we do not
70        // want to spill it to the stack.
71        if (!contains(increments, rax)) {
72            scratch = rax;
73        } else if (!contains(increments, rbx)) {
74            scratch = rbx;
75        } else {
76            // In this case rax and rbx are used as increment. Either we implement a third register
77            // or we implement a spillover the value from rax to rbx or vice versa during
78            // emitIncrement().
79            throw GraalError.unimplemented("RAX and RBX are increment registers at the same time, spilling over the scratch register is not supported right now");
80        }
81
82        // address for counters array
83        AMD64Address countersArrayAddr = new AMD64Address(thread, config.jvmciCountersThreadOffset);
84        Register countersArrayReg = scratch;
85
86        // backup scratch register
87        masm.movq((AMD64Address) crb.asAddress(backupSlot), scratch);
88
89        // load counters array
90        masm.movptr(countersArrayReg, countersArrayAddr);
91        CounterProcedure emitProcedure = (counterIndex, increment, displacement) -> emitIncrement(masm, countersArrayReg, increment, displacement);
92        forEachCounter(emitProcedure, target);
93
94        // restore scratch register
95        masm.movq(scratch, (AMD64Address) crb.asAddress(backupSlot));
96    }
97
98    /**
99     * Tests if the array contains the register.
100     */
101    private static boolean contains(Value[] increments, Register register) {
102        for (Value increment : increments) {
103            if (isRegister(increment) && asRegister(increment).equals(register)) {
104                return true;
105            }
106        }
107        return false;
108    }
109
110    private static void emitIncrement(AMD64MacroAssembler masm, Register countersArrayReg, Value incrementValue, int displacement) {
111        // address for counter value
112        AMD64Address counterAddr = new AMD64Address(countersArrayReg, displacement);
113        // increment counter (in memory)
114        if (isJavaConstant(incrementValue)) {
115            int increment = asInt(asJavaConstant(incrementValue));
116            masm.incrementq(counterAddr, increment);
117        } else {
118            masm.addq(counterAddr, asRegister(incrementValue));
119        }
120
121    }
122}
123