# HG changeset patch # User Josef Eisl # Date 1426609127 -3600 # Node ID 1fed7073f288a4358d14b6057d2d285d95049b70 # Parent d1b9c58e17ce0c43d339cb5d29f4b11d91c3c1c9 Add AMD64HotSpotCounterOp and SPARCHotSpotCounterOp. diff -r d1b9c58e17ce -r 1fed7073f288 graal/com.oracle.graal.hotspot.amd64/src/com/oracle/graal/hotspot/amd64/AMD64HotSpotCounterOp.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.hotspot.amd64/src/com/oracle/graal/hotspot/amd64/AMD64HotSpotCounterOp.java Tue Mar 17 17:18:47 2015 +0100 @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.graal.hotspot.amd64; + +import static com.oracle.graal.amd64.AMD64.*; +import static com.oracle.graal.api.code.ValueUtil.*; + +import com.oracle.graal.api.code.*; +import com.oracle.graal.api.meta.*; +import com.oracle.graal.asm.amd64.*; +import com.oracle.graal.hotspot.*; +import com.oracle.graal.hotspot.meta.*; +import com.oracle.graal.lir.*; +import com.oracle.graal.lir.asm.*; + +@Opcode("BenchMarkCounter") +public class AMD64HotSpotCounterOp extends HotSpotCounterOp { + public static final LIRInstructionClass TYPE = LIRInstructionClass.create(AMD64HotSpotCounterOp.class); + + @Alive({OperandFlag.STACK, OperandFlag.UNINITIALIZED}) private StackSlotValue backupSlot; + + public AMD64HotSpotCounterOp(String name, String group, Value increment, HotSpotRegistersProvider registers, HotSpotVMConfig config, StackSlotValue backupSlot) { + super(TYPE, name, group, increment, registers, config); + this.backupSlot = backupSlot; + } + + public AMD64HotSpotCounterOp(String[] names, String[] groups, Value[] increments, HotSpotRegistersProvider registers, HotSpotVMConfig config, StackSlotValue backupSlot) { + super(TYPE, names, groups, increments, registers, config); + this.backupSlot = backupSlot; + } + + @Override + public void emitCode(CompilationResultBuilder crb) { + AMD64MacroAssembler masm = (AMD64MacroAssembler) crb.asm; + TargetDescription target = crb.target; + + Register scratch = rax; + + // address for counters array + AMD64Address countersArrayAddr = new AMD64Address(thread, config.graalCountersThreadOffset); + Register countersArrayReg = scratch; + + // backup scratch register + masm.movq((AMD64Address) crb.asAddress(backupSlot), scratch); + + // load counters array + masm.movptr(countersArrayReg, countersArrayAddr); + + forEachCounter((name, group, increment) -> emitIncrement(masm, target, countersArrayReg, name, group, increment)); + + // restore scratch register + masm.movq(scratch, (AMD64Address) crb.asAddress(backupSlot)); + } + + private void emitIncrement(AMD64MacroAssembler masm, TargetDescription target, Register countersArrayReg, String name, String group, Value increment) { + // address for counter value + AMD64Address counterAddr = new AMD64Address(countersArrayReg, getDisplacementForLongIndex(target, getIndex(name, group, increment))); + // increment counter (in memory) + if (isConstant(increment)) { + masm.incrementl(counterAddr, asInt(asConstant(increment))); + } else { + masm.addq(counterAddr, asRegister(increment)); + } + } +} diff -r d1b9c58e17ce -r 1fed7073f288 graal/com.oracle.graal.hotspot.sparc/src/com/oracle/graal/hotspot/sparc/SPARCHotSpotCounterOp.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.hotspot.sparc/src/com/oracle/graal/hotspot/sparc/SPARCHotSpotCounterOp.java Tue Mar 17 17:18:47 2015 +0100 @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.graal.hotspot.sparc; + +import static com.oracle.graal.api.code.ValueUtil.*; + +import com.oracle.graal.api.code.*; +import com.oracle.graal.api.meta.*; +import com.oracle.graal.asm.sparc.*; +import com.oracle.graal.hotspot.*; +import com.oracle.graal.hotspot.meta.*; +import com.oracle.graal.lir.*; +import com.oracle.graal.lir.asm.*; + +@Opcode("BenchMarkCounter") +public class SPARCHotSpotCounterOp extends HotSpotCounterOp { + public static final LIRInstructionClass TYPE = LIRInstructionClass.create(SPARCHotSpotCounterOp.class); + + @Temp({OperandFlag.REG}) private AllocatableValue scratch0; + @Temp({OperandFlag.REG}) private AllocatableValue scratch1; + + public SPARCHotSpotCounterOp(String name, String group, Value increment, HotSpotRegistersProvider registers, HotSpotVMConfig config, AllocatableValue scratch0, AllocatableValue scratch1) { + super(TYPE, name, group, increment, registers, config); + this.scratch0 = scratch0; + this.scratch1 = scratch1; + } + + public SPARCHotSpotCounterOp(String[] names, String[] groups, Value[] increments, HotSpotRegistersProvider registers, HotSpotVMConfig config, AllocatableValue scratch0, AllocatableValue scratch1) { + super(TYPE, names, groups, increments, registers, config); + this.scratch0 = scratch0; + this.scratch1 = scratch1; + } + + @Override + public void emitCode(CompilationResultBuilder crb) { + SPARCMacroAssembler masm = (SPARCMacroAssembler) crb.asm; + TargetDescription target = crb.target; + + // address for counters array + SPARCAddress countersArrayAddr = new SPARCAddress(thread, config.graalCountersThreadOffset); + Register countersArrayReg = asRegister(scratch0); + + // load counters array + masm.ldx(countersArrayAddr, countersArrayReg); + + forEachCounter((name, group, increment) -> emitIncrement(masm, target, countersArrayReg, name, group, increment)); + } + + private void emitIncrement(SPARCMacroAssembler masm, TargetDescription target, Register countersArrayReg, String name, String group, Value increment) { + // address for counter + SPARCAddress counterAddr = new SPARCAddress(countersArrayReg, getDisplacementForLongIndex(target, getIndex(name, group, increment))); + Register counterReg = asRegister(scratch1); + // load counter value + masm.ldx(counterAddr, counterReg); + // increment counter + if (isConstant(increment)) { + masm.add(counterReg, asInt(asConstant(increment)), counterReg); + } else { + masm.add(counterReg, asRegister(increment), counterReg); + } + // store counter value + masm.stx(counterReg, counterAddr); + } +} diff -r d1b9c58e17ce -r 1fed7073f288 graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotCounterOp.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotCounterOp.java Tue Mar 17 17:18:47 2015 +0100 @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.graal.hotspot; + +import static com.oracle.graal.api.code.ValueUtil.*; + +import com.oracle.graal.api.code.*; +import com.oracle.graal.api.meta.*; +import com.oracle.graal.asm.*; +import com.oracle.graal.compiler.common.*; +import com.oracle.graal.hotspot.debug.*; +import com.oracle.graal.hotspot.meta.*; +import com.oracle.graal.lir.*; + +public abstract class HotSpotCounterOp extends LIRInstruction { + public static final LIRInstructionClass TYPE = LIRInstructionClass.create(HotSpotCounterOp.class); + + private final String[] names; + private final String[] groups; + protected final Register thread; + protected final HotSpotVMConfig config; + @Alive({OperandFlag.CONST, OperandFlag.REG}) protected Value[] increments; + + public HotSpotCounterOp(LIRInstructionClass c, String name, String group, Value increment, HotSpotRegistersProvider registers, HotSpotVMConfig config) { + this(c, new String[]{name}, new String[]{group}, new Value[]{increment}, registers, config); + } + + public HotSpotCounterOp(LIRInstructionClass c, String[] names, String[] groups, Value[] increments, HotSpotRegistersProvider registers, HotSpotVMConfig config) { + super(c); + + assert names.length == groups.length; + assert groups.length == increments.length; + + this.names = names; + this.groups = groups; + this.increments = increments; + this.thread = registers.getThreadRegister(); + this.config = config; + } + + protected static int getDisplacementForLongIndex(TargetDescription target, long index) { + long finalDisp = index * target.getSizeInBytes(Kind.Long); + if (!NumUtil.isInt(finalDisp)) { + throw GraalInternalError.unimplemented("cannot deal with indices that big: " + index); + } + return (int) finalDisp; + } + + protected interface CounterProcedure { + void apply(String name, String group, Value increment); + } + + protected void forEachCounter(CounterProcedure proc) { + for (int i = 0; i < names.length; i++) { + proc.apply(names[i], groups[i], increments[i]); + } + } + + protected int getIndex(String name, String group, Value increment) { + if (isConstant(increment)) { + // get index for the counter + return BenchmarkCounters.getIndexConstantIncrement(name, group, config, asLong(asConstant(increment))); + } + assert isRegister(increment) : "Unexpected Value: " + increment; + // get index for the counter + return BenchmarkCounters.getIndex(name, group, config); + } + + private static long asLong(JavaConstant value) { + Kind kind = value.getKind(); + switch (kind) { + case Byte: + case Short: + case Char: + case Int: + return value.asInt(); + case Long: + return value.asLong(); + default: + throw new IllegalArgumentException("not an integer kind: " + kind); + } + } + + protected static int asInt(JavaConstant value) { + long l = asLong(value); + if (!NumUtil.isInt(l)) { + throw GraalInternalError.shouldNotReachHere("value does not fit into int: " + l); + } + return (int) l; + } + +}