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 
  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     }
  59 
  60     public AMD64HotSpotCounterOp(String[] names, String[] groups, Value[] increments, HotSpotRegistersProvider registers, GraalHotSpotVMConfig config, AllocatableValue backupSlot) {
  61         super(TYPE, names, groups, increments, registers, config);
  62         this.backupSlot = backupSlot;
  63     }
  64 
  65     @Override
  66     public void emitCode(CompilationResultBuilder crb) {
  67         AMD64MacroAssembler masm = (AMD64MacroAssembler) crb.asm;
  68         TargetDescription target = crb.target;
  69 
  70         Register scratch;
  71         // It can happen that the rax register is the increment register, in this case we do not
  72         // want to spill it to the stack.
  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 }