CompositeValue.java revision 12651:6ef01bd40ce2
1/*
2 * Copyright (c) 2013, 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 */
23package org.graalvm.compiler.lir;
24
25import java.lang.annotation.ElementType;
26import java.lang.annotation.Retention;
27import java.lang.annotation.RetentionPolicy;
28import java.lang.annotation.Target;
29import java.util.EnumSet;
30
31import org.graalvm.compiler.debug.Debug;
32import org.graalvm.compiler.debug.DebugCounter;
33import org.graalvm.compiler.lir.LIRInstruction.OperandFlag;
34import org.graalvm.compiler.lir.LIRInstruction.OperandMode;
35
36import jdk.vm.ci.meta.Value;
37import jdk.vm.ci.meta.ValueKind;
38
39/**
40 * Base class to represent values that need to be stored in more than one register. This is mainly
41 * intended to support addresses and not general arbitrary nesting of composite values. Because of
42 * the possibility of sharing of CompositeValues they should be immutable.
43 */
44public abstract class CompositeValue extends Value {
45
46    @Retention(RetentionPolicy.RUNTIME)
47    @Target(ElementType.FIELD)
48    public static @interface Component {
49
50        OperandFlag[] value() default OperandFlag.REG;
51    }
52
53    private static final DebugCounter COMPOSITE_VALUE_COUNT = Debug.counter("CompositeValues");
54
55    public CompositeValue(ValueKind<?> kind) {
56        super(kind);
57        COMPOSITE_VALUE_COUNT.increment();
58        assert CompositeValueClass.get(getClass()) != null;
59    }
60
61    /**
62     * Invoke {@code proc} on each {@link Value} element of this {@link CompositeValue}. If
63     * {@code proc} replaces any value then a new CompositeValue should be returned.
64     *
65     * @param inst
66     * @param mode
67     * @param proc
68     * @return the original CompositeValue or a copy with any modified values
69     */
70    public abstract CompositeValue forEachComponent(LIRInstruction inst, OperandMode mode, InstructionValueProcedure proc);
71
72    /**
73     * A helper method to visit {@link Value}[] ensuring that a copy of the array is made if it's
74     * needed.
75     *
76     * @param inst
77     * @param values
78     * @param mode
79     * @param proc
80     * @param flags
81     * @return the original {@code values} array or a copy if values changed
82     */
83    protected Value[] visitValueArray(LIRInstruction inst, Value[] values, OperandMode mode, InstructionValueProcedure proc, EnumSet<OperandFlag> flags) {
84        Value[] newValues = null;
85        for (int i = 0; i < values.length; i++) {
86            Value value = values[i];
87            Value newValue = proc.doValue(inst, value, mode, flags);
88            if (!value.identityEquals(newValue)) {
89                if (newValues == null) {
90                    newValues = values.clone();
91                }
92                newValues[i] = value;
93            }
94        }
95        return newValues != null ? newValues : values;
96    }
97
98    protected abstract void visitEachComponent(LIRInstruction inst, OperandMode mode, InstructionValueConsumer proc);
99
100    @Override
101    public String toString() {
102        return CompositeValueClass.format(this);
103    }
104
105    @Override
106    public int hashCode() {
107        return 53 * super.hashCode();
108    }
109
110    @Override
111    public boolean equals(Object obj) {
112        if (obj instanceof CompositeValue) {
113            CompositeValue other = (CompositeValue) obj;
114            return super.equals(other);
115        }
116        return false;
117    }
118}
119