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