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.hotspot.amd64;
024
025import jdk.internal.jvmci.code.*;
026import jdk.internal.jvmci.hotspot.*;
027import jdk.internal.jvmci.meta.*;
028import static jdk.internal.jvmci.amd64.AMD64.*;
029import static jdk.internal.jvmci.code.ValueUtil.*;
030import static jdk.internal.jvmci.common.JVMCIError.*;
031
032import com.oracle.graal.asm.amd64.*;
033import com.oracle.graal.hotspot.*;
034import com.oracle.graal.hotspot.meta.*;
035import com.oracle.graal.lir.*;
036import com.oracle.graal.lir.asm.*;
037
038@Opcode("BenchMarkCounter")
039public class AMD64HotSpotCounterOp extends HotSpotCounterOp {
040    public static final LIRInstructionClass<AMD64HotSpotCounterOp> TYPE = LIRInstructionClass.create(AMD64HotSpotCounterOp.class);
041
042    @Alive({OperandFlag.STACK, OperandFlag.UNINITIALIZED}) private StackSlotValue backupSlot;
043
044    public AMD64HotSpotCounterOp(String name, String group, Value increment, HotSpotRegistersProvider registers, HotSpotVMConfig config, StackSlotValue backupSlot) {
045        super(TYPE, name, group, increment, registers, config);
046        this.backupSlot = backupSlot;
047    }
048
049    public AMD64HotSpotCounterOp(String[] names, String[] groups, Value[] increments, HotSpotRegistersProvider registers, HotSpotVMConfig config, StackSlotValue backupSlot) {
050        super(TYPE, names, groups, increments, registers, config);
051        this.backupSlot = backupSlot;
052    }
053
054    @Override
055    public void emitCode(CompilationResultBuilder crb) {
056        AMD64MacroAssembler masm = (AMD64MacroAssembler) crb.asm;
057        TargetDescription target = crb.target;
058
059        Register scratch;
060        // It can happen that the rax register is the increment register, in this case we do not
061        // want to spill it to the stack.
062        if (!contains(increments, rax)) {
063            scratch = rax;
064        } else if (!contains(increments, rbx)) {
065            scratch = rbx;
066        } else {
067            // In this case rax and rbx are used as increment. Either we implement a third register
068            // or we implement a spillover the value from rax to rbx or vice versa during
069            // emitIncrement().
070            throw unimplemented("RAX and RBX are increment registers a the same time, spilling over the scratch register is not supported right now");
071        }
072
073        // address for counters array
074        AMD64Address countersArrayAddr = new AMD64Address(thread, config.jvmciCountersThreadOffset);
075        Register countersArrayReg = scratch;
076
077        // backup scratch register
078        masm.movq((AMD64Address) crb.asAddress(backupSlot), scratch);
079
080        // load counters array
081        masm.movptr(countersArrayReg, countersArrayAddr);
082        CounterProcedure emitProcedure = (counterIndex, increment, displacement) -> emitIncrement(masm, countersArrayReg, increment, displacement);
083        forEachCounter(emitProcedure, target);
084
085        // restore scratch register
086        masm.movq(scratch, (AMD64Address) crb.asAddress(backupSlot));
087    }
088
089    /**
090     * Tests if the array contains the register.
091     */
092    private static boolean contains(Value[] increments, Register register) {
093        for (Value increment : increments) {
094            if (isRegister(increment) && asRegister(increment).equals(register)) {
095                return true;
096            }
097        }
098        return false;
099    }
100
101    private static void emitIncrement(AMD64MacroAssembler masm, Register countersArrayReg, Value incrementValue, int displacement) {
102        // address for counter value
103        AMD64Address counterAddr = new AMD64Address(countersArrayReg, displacement);
104        // increment counter (in memory)
105        if (isConstant(incrementValue)) {
106            int increment = asInt(asConstant(incrementValue));
107            masm.incrementq(counterAddr, increment);
108        } else {
109            masm.addq(counterAddr, asRegister(incrementValue));
110        }
111
112    }
113}