001/*
002 * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.hotspot;
024
025import static jdk.internal.jvmci.code.ValueUtil.*;
026
027import java.util.*;
028
029import jdk.internal.jvmci.code.*;
030import jdk.internal.jvmci.common.*;
031import jdk.internal.jvmci.hotspot.*;
032import jdk.internal.jvmci.meta.*;
033
034import com.oracle.graal.asm.*;
035import com.oracle.graal.hotspot.debug.*;
036import com.oracle.graal.hotspot.meta.*;
037import com.oracle.graal.lir.*;
038
039public abstract class HotSpotCounterOp extends LIRInstruction {
040    public static final LIRInstructionClass<HotSpotCounterOp> TYPE = LIRInstructionClass.create(HotSpotCounterOp.class);
041
042    private final String[] names;
043    private final String[] groups;
044    protected final Register thread;
045    protected final HotSpotVMConfig config;
046    @Alive({OperandFlag.CONST, OperandFlag.REG}) protected Value[] increments;
047
048    public HotSpotCounterOp(LIRInstructionClass<? extends HotSpotCounterOp> c, String name, String group, Value increment, HotSpotRegistersProvider registers, HotSpotVMConfig config) {
049        this(c, new String[]{name}, new String[]{group}, new Value[]{increment}, registers, config);
050    }
051
052    public HotSpotCounterOp(LIRInstructionClass<? extends HotSpotCounterOp> c, String[] names, String[] groups, Value[] increments, HotSpotRegistersProvider registers, HotSpotVMConfig config) {
053        super(c);
054
055        assert names.length == groups.length;
056        assert groups.length == increments.length;
057
058        this.names = names;
059        this.groups = groups;
060        this.increments = increments;
061        this.thread = registers.getThreadRegister();
062        this.config = config;
063    }
064
065    protected static int getDisplacementForLongIndex(TargetDescription target, long index) {
066        long finalDisp = index * target.getSizeInBytes(Kind.Long);
067        if (!NumUtil.isInt(finalDisp)) {
068            throw JVMCIError.unimplemented("cannot deal with indices that big: " + index);
069        }
070        return (int) finalDisp;
071    }
072
073    protected interface CounterProcedure {
074        /**
075         * Lambda interface for iterating over counters declared in this op.
076         *
077         * @param counterIndex Index in this CounterOp object.
078         * @param increment Value for increment
079         * @param displacement Displacement in bytes in the counter array
080         */
081        void apply(int counterIndex, Value increment, int displacement);
082    }
083
084    /**
085     * Calls the {@link CounterProcedure} for each counter in ascending order of their displacement
086     * in the counter array.
087     *
088     * @param proc The procedure to be called
089     * @param target Target architecture (used to calculate the array displacements)
090     */
091    protected void forEachCounter(CounterProcedure proc, TargetDescription target) {
092        if (names.length == 1) { // fast path
093            int arrayIndex = getIndex(names[0], groups[0], increments[0]);
094            int displacement = getDisplacementForLongIndex(target, arrayIndex);
095            proc.apply(0, increments[0], displacement);
096        } else { // Slow path with sort by displacements ascending
097            int[] displacements = new int[names.length];
098            HashMap<Integer, Integer> offsetMap = new HashMap<>(names.length);
099            for (int i = 0; i < names.length; i++) {
100                int arrayIndex = getIndex(names[i], groups[i], increments[i]);
101                displacements[i] = getDisplacementForLongIndex(target, arrayIndex);
102                offsetMap.put(displacements[i], i);
103            }
104            Arrays.sort(displacements);
105            // Now apply in order
106            for (int offset : displacements) {
107                int idx = offsetMap.get(offset);
108                proc.apply(idx, increments[idx], displacements[idx]);
109            }
110        }
111    }
112
113    protected int getIndex(String name, String group, Value increment) {
114        if (isConstant(increment)) {
115            // get index for the counter
116            return BenchmarkCounters.getIndexConstantIncrement(name, group, config, asLong(asConstant(increment)));
117        }
118        assert isRegister(increment) : "Unexpected Value: " + increment;
119        // get index for the counter
120        return BenchmarkCounters.getIndex(name, group, config);
121    }
122
123    /**
124     * Patches the increment value in the instruction emitted by this instruction. Use only, if
125     * patching is needed after assembly.
126     *
127     * @param asm
128     * @param increment
129     */
130    public void patchCounterIncrement(Assembler asm, int[] increment) {
131        throw JVMCIError.unimplemented();
132    }
133
134    private static long asLong(JavaConstant value) {
135        Kind kind = value.getKind();
136        switch (kind) {
137            case Byte:
138            case Short:
139            case Char:
140            case Int:
141                return value.asInt();
142            case Long:
143                return value.asLong();
144            default:
145                throw new IllegalArgumentException("not an integer kind: " + kind);
146        }
147    }
148
149    protected static int asInt(JavaConstant value) {
150        long l = asLong(value);
151        if (!NumUtil.isInt(l)) {
152            throw JVMCIError.shouldNotReachHere("value does not fit into int: " + l);
153        }
154        return (int) l;
155    }
156
157    public String[] getNames() {
158        return names;
159    }
160
161    public String[] getGroups() {
162        return groups;
163    }
164}