summaryrefslogtreecommitdiff
path: root/test/476-checker-ctor-memory-barrier/src/Main.java
blob: e887cd32a0ce6a9857e5fdc0c0a50f77276028cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// TODO: Add more tests after we can inline functions with calls.

class ClassWithoutFinals {
  /// CHECK-START: void ClassWithoutFinals.<init>() inliner (after)
  /// CHECK-NOT: ConstructorFence
  public ClassWithoutFinals() {}
}

class ClassWithFinals {
  public final int x;
  public ClassWithFinals obj;
  public static boolean doThrow = false;

  public ClassWithFinals(boolean cond) {
    x = 1;
    throw new RuntimeException();
    // should not inline this constructor
  }

  /// CHECK-START: void ClassWithFinals.<init>() inliner (after)
  /// CHECK:      ConstructorFence
  /// CHECK-NOT:  ConstructorFence

  /*
   * Check that the correct assembly instructions are selected for a Store/Store fence.
   *
   * - ARM variants:   DMB ISHST (store-store fence for inner shareable domain)
   * - Intel variants: no-op (store-store does not need a fence).
   */

  /// CHECK-START-ARM64: void ClassWithFinals.<init>() disassembly (after)
  /// CHECK:      ConstructorFence
  /// CHECK-NEXT: dmb ishst

  /// CHECK-START-ARM: void ClassWithFinals.<init>() disassembly (after)
  /// CHECK:      ConstructorFence
  /// CHECK-NEXT: dmb ishst

  /// CHECK-START-X86_64: void ClassWithFinals.<init>() disassembly (after)
  /// CHECK:      ConstructorFence
  /// CHECK-NOT:  {{[slm]}}fence

  /// CHECK-START-X86: void ClassWithFinals.<init>() disassembly (after)
  /// CHECK:      ConstructorFence
  /// CHECK-NOT:  {{[slm]}}fence
  public ClassWithFinals() {
    // Exactly one constructor barrier.
    x = 0;
  }

  /// CHECK-START: void ClassWithFinals.<init>(int) inliner (after)
  /// CHECK: <<This:l\d+>>            ParameterValue
  /// CHECK: <<NewInstance:l\d+>>     NewInstance
  /// CHECK-DAG:                      ConstructorFence [<<NewInstance>>]
  /// CHECK-DAG:                      ConstructorFence [<<NewInstance>>]
  /// CHECK-DAG:                      ConstructorFence [<<This>>]
  /// CHECK-NOT:                      ConstructorFence
  public ClassWithFinals(int x) {
    // This should have exactly three barriers:
    //   - one for the new-instance
    //   - one for the constructor
    //   - one for the `new` which should be inlined.
    obj = new ClassWithFinals();
    this.x = x;
  }
}

class InheritFromClassWithFinals extends ClassWithFinals {
  /// CHECK-START: void InheritFromClassWithFinals.<init>() inliner (after)
  /// CHECK: <<This:l\d+>>            ParameterValue
  /// CHECK:                          ConstructorFence [<<This>>]
  /// CHECK-NOT:                      ConstructorFence

  /// CHECK-START: void InheritFromClassWithFinals.<init>() inliner (after)
  /// CHECK-NOT:  InvokeStaticOrDirect
  public InheritFromClassWithFinals() {
    // Should inline the super constructor.
    //
    // Exactly one constructor barrier here.
  }

  /// CHECK-START: void InheritFromClassWithFinals.<init>(boolean) inliner (after)
  /// CHECK:      InvokeStaticOrDirect

  /// CHECK-START: void InheritFromClassWithFinals.<init>(boolean) inliner (after)
  /// CHECK-NOT:  ConstructorFence
  public InheritFromClassWithFinals(boolean cond) {
    super(cond);
    // should not inline the super constructor
  }

  /// CHECK-START: void InheritFromClassWithFinals.<init>(int) inliner (after)
  /// CHECK: <<This:l\d+>>            ParameterValue
  /// CHECK-DAG: <<NewHere:l\d+>>     NewInstance klass:InheritFromClassWithFinals
  /// CHECK-DAG:                      ConstructorFence [<<This>>]
  /// CHECK-DAG:                      ConstructorFence [<<NewHere>>]
  /// CHECK-DAG:                      ConstructorFence [<<NewHere>>]
  /// CHECK-NOT:                      ConstructorFence

  /// CHECK-START: void InheritFromClassWithFinals.<init>(int) inliner (after)
  /// CHECK-NOT:  InvokeStaticOrDirect
  public InheritFromClassWithFinals(int unused) {
    // super();  // implicitly the first invoke in this constructor.
    //   Should inline the super constructor and insert a constructor fence there.

    // Should inline the new instance call (barrier); and add another one
    // because the superclass has finals.
    new InheritFromClassWithFinals();
  }
}

class HaveFinalsAndInheritFromClassWithFinals extends ClassWithFinals {
  final int y;

  /// CHECK-START: void HaveFinalsAndInheritFromClassWithFinals.<init>() inliner (after)
  /// CHECK: <<This:l\d+>>            ParameterValue
  /// CHECK:                          ConstructorFence [<<This>>]
  /// CHECK:                          ConstructorFence [<<This>>]
  /// CHECK-NOT:                      ConstructorFence

  /// CHECK-START: void HaveFinalsAndInheritFromClassWithFinals.<init>() inliner (after)
  /// CHECK-NOT: InvokeStaticOrDirect
  public HaveFinalsAndInheritFromClassWithFinals() {
    // Should inline the super constructor and keep the memory barrier.
    y = 0;
  }

  /// CHECK-START: void HaveFinalsAndInheritFromClassWithFinals.<init>(boolean) inliner (after)
  /// CHECK: <<This:l\d+>>            ParameterValue
  /// CHECK:                          InvokeStaticOrDirect
  /// CHECK:                          ConstructorFence [<<This>>]
  /// CHECK-NOT:                      ConstructorFence
  public HaveFinalsAndInheritFromClassWithFinals(boolean cond) {
    super(cond);
    // should not inline the super constructor
    y = 0;
  }

  /// CHECK-START: void HaveFinalsAndInheritFromClassWithFinals.<init>(int) inliner (after)
  /// CHECK: <<This:l\d+>>            ParameterValue
  /// CHECK-DAG: <<NewHF:l\d+>>       NewInstance klass:HaveFinalsAndInheritFromClassWithFinals
  /// CHECK-DAG: <<NewIF:l\d+>>       NewInstance klass:InheritFromClassWithFinals
  /// CHECK-DAG:                      ConstructorFence [<<This>>]
  /// CHECK-DAG:                      ConstructorFence [<<NewHF>>]
  /// CHECK-DAG:                      ConstructorFence [<<NewHF>>]
  /// CHECK-DAG:                      ConstructorFence [<<NewHF>>]
  /// CHECK-DAG:                      ConstructorFence [<<NewIF>>]
  /// CHECK-DAG:                      ConstructorFence [<<NewIF>>]
  /// CHECK-DAG:                      ConstructorFence [<<This>>]
  /// CHECK-NOT:                      ConstructorFence

  /// CHECK-START: void HaveFinalsAndInheritFromClassWithFinals.<init>(int) inliner (after)
  /// CHECK-NOT:  InvokeStaticOrDirect
  public HaveFinalsAndInheritFromClassWithFinals(int unused) {
    // super()
    // -- Inlined super constructor, insert memory barrier here.
    y = 0;

    // Should inline new instance and keep both memory barriers.
    // One more memory barrier for new-instance.
    // (3 total for this new-instance #1)
    new HaveFinalsAndInheritFromClassWithFinals();
    // Should inline new instance and have exactly one barrier.
    // One more barrier for new-instance.
    // (2 total for this new-instance #2)
    new InheritFromClassWithFinals();

    // -- End of constructor, insert memory barrier here to freeze 'y'.
  }
}

public class Main {

  /// CHECK-START: ClassWithFinals Main.noInlineNoConstructorBarrier() inliner (after)
  /// CHECK:      InvokeStaticOrDirect

  /// CHECK-START: ClassWithFinals Main.noInlineNoConstructorBarrier() inliner (after)
  /// CHECK: <<NewInstance:l\d+>>     NewInstance
  /// CHECK:                          ConstructorFence [<<NewInstance>>]
  /// CHECK-NOT:                      ConstructorFence
  public static ClassWithFinals noInlineNoConstructorBarrier() {
    // Exactly one barrier for the new-instance.
    return new ClassWithFinals(false);
    // should not inline the constructor
  }

  /// CHECK-START: void Main.inlineNew() inliner (after)
  /// CHECK: <<NewInstance:l\d+>>     NewInstance
  /// CHECK-DAG:                      ConstructorFence [<<NewInstance>>]
  /// CHECK-DAG:                      ConstructorFence [<<NewInstance>>]
  /// CHECK-NOT:                      ConstructorFence

  /// CHECK-START: void Main.inlineNew() inliner (after)
  /// CHECK-NOT:  InvokeStaticOrDirect
  public static void inlineNew() {
    // Exactly 2 barriers. One for new-instance, one for constructor with finals.
    new ClassWithFinals();
  }

  /// CHECK-START: void Main.inlineNew1() inliner (after)
  /// CHECK: <<NewInstance:l\d+>>     NewInstance
  /// CHECK-DAG:                      ConstructorFence [<<NewInstance>>]
  /// CHECK-DAG:                      ConstructorFence [<<NewInstance>>]
  /// CHECK-NOT:                      ConstructorFence

  /// CHECK-START: void Main.inlineNew1() inliner (after)
  /// CHECK-NOT:  InvokeStaticOrDirect
  public static void inlineNew1() {
    new InheritFromClassWithFinals();
  }

  /// CHECK-START: void Main.inlineNew2() inliner (after)
  /// CHECK: <<NewInstance:l\d+>>     NewInstance
  /// CHECK-DAG:                      ConstructorFence [<<NewInstance>>]
  /// CHECK-DAG:                      ConstructorFence [<<NewInstance>>]
  /// CHECK-DAG:                      ConstructorFence [<<NewInstance>>]
  /// CHECK-NOT:                      ConstructorFence

  /// CHECK-START: void Main.inlineNew2() inliner (after)
  /// CHECK-NOT:  InvokeStaticOrDirect
  public static void inlineNew2() {
    new HaveFinalsAndInheritFromClassWithFinals();
  }

  /// CHECK-START: void Main.inlineNew3() inliner (after)
  /// CHECK: <<NewInstance:l\d+>>     NewInstance
  /// CHECK-DAG:                      ConstructorFence [<<NewInstance>>]
  /// CHECK-DAG:                      ConstructorFence [<<NewInstance>>]
  /// CHECK-DAG:                      ConstructorFence [<<NewInstance>>]
  /// CHECK-NOT:                      ConstructorFence
  /// CHECK: <<NewInstance2:l\d+>>    NewInstance
  /// CHECK-DAG:                      ConstructorFence [<<NewInstance2>>]
  /// CHECK-DAG:                      ConstructorFence [<<NewInstance2>>]
  /// CHECK-DAG:                      ConstructorFence [<<NewInstance2>>]
  /// CHECK-NOT:                      ConstructorFence

  /// CHECK-START: void Main.inlineNew3() inliner (after)
  /// CHECK-NOT:  InvokeStaticOrDirect
  public static void inlineNew3() {
    new HaveFinalsAndInheritFromClassWithFinals();
    new HaveFinalsAndInheritFromClassWithFinals();
  }

  static int[] mCodePointsEmpty = new int[0];

  /// CHECK-START: void Main.testNewString() inliner (after)
  /// CHECK-NOT:  ConstructorFence
  /// CHECK:      InvokeStaticOrDirect method_load_kind:StringInit
  /// CHECK-NOT:  ConstructorFence
  /// CHECK-NOT:  InvokeStaticOrDirect
  public static void testNewString() {
    // Strings are special because of StringFactory hackeries.
    //
    // Assume they handle their own fencing internally in the StringFactory.
    int[] codePoints = null;
    String some_new_string = new String(mCodePointsEmpty, 0, 0);
  }

  public static void main(String[] args) {}
}