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