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