1 /* 2 * Copyright (c) 2015, 2018, 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 */ 23 24 25 package org.graalvm.compiler.hotspot.sparc; 26 27 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.isSimm13; 28 import static org.graalvm.compiler.lir.LIRValueUtil.asJavaConstant; 29 import static org.graalvm.compiler.lir.LIRValueUtil.isJavaConstant; 30 import static jdk.vm.ci.code.ValueUtil.asRegister; 31 32 import org.graalvm.compiler.asm.Assembler; 33 import org.graalvm.compiler.asm.sparc.SPARCAddress; 34 import org.graalvm.compiler.asm.sparc.SPARCAssembler; 35 import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler; 36 import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler.ScratchRegister; 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.Value; 47 48 @Opcode("BenchMarkCounter") 49 public class SPARCHotSpotCounterOp extends HotSpotCounterOp { 50 public static final LIRInstructionClass<SPARCHotSpotCounterOp> TYPE = LIRInstructionClass.create(SPARCHotSpotCounterOp.class); 51 52 private int[] counterPatchOffsets; 53 54 public SPARCHotSpotCounterOp(String name, String group, Value increment, HotSpotRegistersProvider registers, GraalHotSpotVMConfig config) { 55 super(TYPE, name, group, increment, registers, config); 56 this.counterPatchOffsets = new int[1]; 57 } 58 59 public SPARCHotSpotCounterOp(String[] names, String[] groups, Value[] increments, HotSpotRegistersProvider registers, GraalHotSpotVMConfig config) { 60 super(TYPE, names, groups, increments, registers, config); 61 this.counterPatchOffsets = new int[names.length]; 62 } 63 64 @Override 65 public void emitCode(CompilationResultBuilder crb) { 66 SPARCMacroAssembler masm = (SPARCMacroAssembler) crb.asm; 67 TargetDescription target = crb.target; 68 69 // address for counters array 70 SPARCAddress countersArrayAddr = new SPARCAddress(thread, config.jvmciCountersThreadOffset); 71 try (ScratchRegister scratch = masm.getScratchRegister()) { 72 Register countersArrayReg = scratch.getRegister(); 73 74 // load counters array 75 masm.ldx(countersArrayAddr, countersArrayReg); 76 IncrementEmitter emitter = new IncrementEmitter(countersArrayReg, masm); 77 forEachCounter(emitter, target); 78 } 79 } 80 81 private void emitIncrement(int counterIndex, SPARCMacroAssembler masm, SPARCAddress counterAddr, Value increment) { 82 try (ScratchRegister scratch = masm.getScratchRegister()) { 83 Register counterReg = scratch.getRegister(); 84 // load counter value 85 masm.ldx(counterAddr, counterReg); 86 counterPatchOffsets[counterIndex] = masm.position(); 87 // increment counter 88 if (isJavaConstant(increment)) { 89 masm.add(counterReg, asInt(asJavaConstant(increment)), counterReg); 90 } else { 91 masm.add(counterReg, asRegister(increment), counterReg); 92 } 93 // store counter value 94 masm.stx(counterReg, counterAddr); 95 } 96 } 97 98 /** 99 * Patches the increment value in the instruction emitted by the 100 * {@link #emitIncrement(int, SPARCMacroAssembler, SPARCAddress, Value)} method. This method is 101 * used if patching is needed after assembly. 102 * 103 * @param asm 104 * @param increment 105 */ 106 @Override 107 public void patchCounterIncrement(Assembler asm, int[] increment) { 108 for (int i = 0; i < increment.length; i++) { 109 int inst = counterPatchOffsets[i]; 110 ((SPARCAssembler) asm).patchAddImmediate(inst, increment[i]); 111 } 112 } 113 114 public int[] getCounterPatchOffsets() { 115 return counterPatchOffsets; 116 } 117 118 private class IncrementEmitter implements CounterProcedure { 119 private int lastDisplacement = 0; 120 private final Register countersArrayReg; 121 private final SPARCMacroAssembler masm; 122 123 IncrementEmitter(Register countersArrayReg, SPARCMacroAssembler masm) { 124 super(); 125 this.countersArrayReg = countersArrayReg; 126 this.masm = masm; 127 } 128 129 @Override 130 public void apply(int counterIndex, Value increment, int displacement) { 131 SPARCAddress counterAddr; 132 int relativeDisplacement = displacement - lastDisplacement; 133 if (isSimm13(relativeDisplacement)) { // Displacement fits into ld instruction 134 counterAddr = new SPARCAddress(countersArrayReg, relativeDisplacement); 135 } else { 136 try (ScratchRegister scratch = masm.getScratchRegister()) { 137 Register tempOffsetRegister = scratch.getRegister(); 138 masm.setx(relativeDisplacement, tempOffsetRegister, false); 139 masm.add(countersArrayReg, tempOffsetRegister, countersArrayReg); 140 } 141 lastDisplacement = displacement; 142 counterAddr = new SPARCAddress(countersArrayReg, 0); 143 } 144 emitIncrement(counterIndex, masm, counterAddr, increment); 145 } 146 } 147 }