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