1 /*
  2  * Copyright (c) 2017, 2025, 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 package compiler.valhalla.inlinetypes;
 25 
 26 import compiler.lib.ir_framework.DontCompile;
 27 import compiler.lib.ir_framework.DontInline;
 28 import compiler.lib.ir_framework.ForceCompileClassInitializer;
 29 import compiler.lib.ir_framework.ForceInline;
 30 
 31 import java.util.Arrays;
 32 
 33 import jdk.internal.vm.annotation.LooselyConsistentValue;
 34 import jdk.internal.vm.annotation.NullRestricted;
 35 import jdk.internal.vm.annotation.Strict;
 36 
 37 @LooselyConsistentValue
 38 @ForceCompileClassInitializer
 39 public value class MyValue1 extends MyAbstract {
 40     static int s;
 41     static final long sf = InlineTypes.rL;
 42     int x;
 43     long y;
 44     short z;
 45     Integer o;
 46     int[] oa;
 47     @Strict
 48     @NullRestricted
 49     MyValue2 v1;
 50     @Strict
 51     @NullRestricted
 52     MyValue2 v2;
 53     @Strict
 54     @NullRestricted
 55     static final MyValue2 v3 = MyValue2.createWithFieldsInline(InlineTypes.rI, InlineTypes.rD);
 56     MyValue2 v4;
 57     @Strict
 58     @NullRestricted
 59     MyValue2 v5;
 60     int c;
 61 
 62     static final MyValue1 DEFAULT = createDefaultInline();
 63 
 64     @ForceInline
 65     public MyValue1(int x, long y, short z, Integer o, int[] oa, MyValue2 v1, MyValue2 v2, MyValue2 v4, MyValue2 v5, int c) {
 66         s = 0;
 67         this.x = x;
 68         this.y = y;
 69         this.z = z;
 70         this.o = o;
 71         this.oa = oa;
 72         this.v1 = v1;
 73         this.v2 = v2;
 74         this.v4 = v4;
 75         this.v5 = v5;
 76         this.c = c;
 77     }
 78 
 79     @DontInline
 80     static MyValue1 createDefaultDontInline() {
 81         return createDefaultInline();
 82     }
 83 
 84     @ForceInline
 85     static MyValue1 createDefaultInline() {
 86         return new MyValue1(0, 0, (short)0, null, null, MyValue2.createDefaultInline(), MyValue2.createDefaultInline(), null, MyValue2.createDefaultInline(), 0);
 87     }
 88 
 89     @DontInline
 90     static MyValue1 createWithFieldsDontInline(int x, long y) {
 91         return createWithFieldsInline(x, y);
 92     }
 93 
 94     @ForceInline
 95     static MyValue1 createWithFieldsInline(int x, long y) {
 96         MyValue1 v = createDefaultInline();
 97         v = setX(v, x);
 98         v = setY(v, y);
 99         v = setZ(v, (short)x);
100         // Don't use Integer.valueOf here to avoid control flow added by Integer cache check
101         v = setO(v, new Integer(x));
102         int[] oa = {x};
103         v = setOA(v, oa);
104         v = setV1(v, MyValue2.createWithFieldsInline(x, y, InlineTypes.rD));
105         v = setV2(v, MyValue2.createWithFieldsInline(x + 1, y + 1, InlineTypes.rD + 1));
106         v = setV4(v, MyValue2.createWithFieldsInline(x + 2, y + 2, InlineTypes.rD + 2));
107         v = setV5(v, MyValue2.createWithFieldsInline(x + 3, y + 3, InlineTypes.rD + 3));
108         v = setC(v, (int)(x+y));
109         return v;
110     }
111 
112     // Hash only primitive and inline type fields to avoid NullPointerException
113     @ForceInline
114     public long hashPrimitive() {
115         return s + sf + x + y + z + c + v1.hash() + v2.hash() + v3.hash() + v5.hash();
116     }
117 
118     @ForceInline
119     public long hash() {
120         long res = hashPrimitive();
121         try {
122             res += o;
123         } catch (NullPointerException npe) {}
124         try {
125             res += oa[0];
126         } catch (NullPointerException npe) {}
127         try {
128             res += v4.hash();
129         } catch (NullPointerException npe) {}
130         return res;
131     }
132 
133     @DontCompile
134     public long hashInterpreted() {
135         return s + sf + x + y + z + o + oa[0] + c + v1.hashInterpreted() + v2.hashInterpreted() + v3.hashInterpreted() + v4.hashInterpreted() + v5.hashInterpreted();
136     }
137 
138     @ForceInline
139     static MyValue1 setX(MyValue1 v, int x) {
140         return new MyValue1(x, v.y, v.z, v.o, v.oa, v.v1, v.v2, v.v4, v.v5, v.c);
141     }
142 
143     @ForceInline
144     static MyValue1 setY(MyValue1 v, long y) {
145         return new MyValue1(v.x, y, v.z, v.o, v.oa, v.v1, v.v2, v.v4, v.v5, v.c);
146     }
147 
148     @ForceInline
149     static MyValue1 setZ(MyValue1 v, short z) {
150         return new MyValue1(v.x, v.y, z, v.o, v.oa, v.v1, v.v2, v.v4, v.v5, v.c);
151     }
152 
153     @ForceInline
154     static MyValue1 setO(MyValue1 v, Integer o) {
155         return new MyValue1(v.x, v.y, v.z, o, v.oa, v.v1, v.v2, v.v4, v.v5, v.c);
156     }
157 
158     @ForceInline
159     static MyValue1 setOA(MyValue1 v, int[] oa) {
160         return new MyValue1(v.x, v.y, v.z, v.o, oa, v.v1, v.v2, v.v4, v.v5, v.c);
161     }
162 
163     @ForceInline
164     static MyValue1 setC(MyValue1 v, int c) {
165         return new MyValue1(v.x, v.y, v.z, v.o, v.oa, v.v1, v.v2, v.v4, v.v5, c);
166     }
167 
168     @ForceInline
169     static MyValue1 setV1(MyValue1 v, MyValue2 v1) {
170         return new MyValue1(v.x, v.y, v.z, v.o, v.oa, v1, v.v2, v.v4, v.v5, v.c);
171     }
172 
173     @ForceInline
174     static MyValue1 setV2(MyValue1 v, MyValue2 v2) {
175         return new MyValue1(v.x, v.y, v.z, v.o, v.oa, v.v1, v2, v.v4, v.v5, v.c);
176     }
177 
178     @ForceInline
179     static MyValue1 setV4(MyValue1 v, MyValue2 v4) {
180         return new MyValue1(v.x, v.y, v.z, v.o, v.oa, v.v1, v.v2, v4, v.v5, v.c);
181     }
182 
183     @ForceInline
184     static MyValue1 setV5(MyValue1 v, MyValue2 v5) {
185         return new MyValue1(v.x, v.y, v.z, v.o, v.oa, v.v1, v.v2, v.v4, v5, v.c);
186     }
187 
188     @Override
189     public String toString() {
190         return "MyValue1[s=" + s + ", sf=" + sf + ", x=" + x + ", y=" + y + ", z=" + z + ", o=" + o + ", oa=" + Arrays.toString(oa) +
191                 ", v1=" + v1 + ", v2=" + v2 + ", v4=" + v4 + ", c=" + c + "]";
192     }
193 }