summaryrefslogtreecommitdiff
path: root/runtime/jit/small_pattern_matcher.cc
blob: 1fc320f7803d3dc6ae3243952660f5c34497b5c0 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 * Copyright 2023 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.
 */

#include "small_pattern_matcher.h"

#include "art_method-inl.h"
#include "dex/dex_instruction-inl.h"
#include "entrypoints/entrypoint_utils-inl.h"

namespace art HIDDEN {
namespace jit {

// The following methods will be directly invoked by our own JIT/AOT compiled
// code.

static void EmptyMethod() {}
static int32_t ReturnZero() { return 0; }
static int32_t ReturnOne() { return 1; }
static int32_t ReturnFirstArgMethod([[maybe_unused]] ArtMethod* method, int32_t first_arg) {
  return first_arg;
}

template <int offset, typename T>
static std::conditional_t<(sizeof(T) < sizeof(int32_t)), int32_t, T> ReturnFieldAt(
    [[maybe_unused]] ArtMethod* method, mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
  return obj->GetFieldPrimitive<T, /* kIsVolatile= */ false>(
      MemberOffset(offset + sizeof(mirror::Object)));
}

template <int offset, typename unused>
static mirror::Object* ReturnFieldObjectAt([[maybe_unused]] ArtMethod* method, mirror::Object* obj)
    REQUIRES_SHARED(Locks::mutator_lock_) {
  return obj->GetFieldObject<mirror::Object>(MemberOffset(offset + sizeof(mirror::Object)));
}

template <int offset, typename T>
static std::conditional_t<(sizeof(T) < sizeof(int32_t)), int32_t, T> ReturnStaticFieldAt(
    ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
  ObjPtr<mirror::Class> cls = method->GetDeclaringClass();
  MemberOffset first_field_offset = cls->GetFirstReferenceStaticFieldOffset(kRuntimePointerSize);
  return cls->GetFieldPrimitive<T, /* kIsVolatile= */ false>(
      MemberOffset(offset + first_field_offset.Int32Value()));
}

template <int offset, typename unused>
static mirror::Object* ReturnStaticFieldObjectAt(ArtMethod* method)
    REQUIRES_SHARED(Locks::mutator_lock_) {
  ObjPtr<mirror::Class> cls = method->GetDeclaringClass();
  MemberOffset first_field_offset = cls->GetFirstReferenceStaticFieldOffset(kRuntimePointerSize);
  return cls->GetFieldObject<mirror::Object>(
      MemberOffset(offset + first_field_offset.Int32Value()));
}

template <int offset, typename T>
static void SetFieldAt([[maybe_unused]] ArtMethod* method, mirror::Object* obj, T value)
    REQUIRES_SHARED(Locks::mutator_lock_) {
  obj->SetFieldPrimitive<T, /* kIsVolatile= */ false>(
      MemberOffset(offset + sizeof(mirror::Object)), value);
}

template <int offset, typename unused>
static void SetFieldObjectAt([[maybe_unused]] ArtMethod* method,
                             mirror::Object* obj,
                             mirror::Object* value)
    REQUIRES_SHARED(Locks::mutator_lock_) {
  obj->SetFieldObject</* kTransactionActive */ false>(
      MemberOffset(offset + sizeof(mirror::Object)), value);
}

template <int offset, typename T>
static void ConstructorSetFieldAt([[maybe_unused]] ArtMethod* method, mirror::Object* obj, T value)
    REQUIRES_SHARED(Locks::mutator_lock_) {
  obj->SetFieldPrimitive<T, /* kIsVolatile= */ false>(
      MemberOffset(offset + sizeof(mirror::Object)), value);
  QuasiAtomic::ThreadFenceForConstructor();
}

template <int offset, typename unused>
static void ConstructorSetFieldObjectAt([[maybe_unused]] ArtMethod* method,
                                        mirror::Object* obj,
                                        mirror::Object* value)
    REQUIRES_SHARED(Locks::mutator_lock_) {
  obj->SetFieldObject</* kTransactionActive */ false>(
      MemberOffset(offset + sizeof(mirror::Object)), value);
  QuasiAtomic::ThreadFenceForConstructor();
}

#define SWITCH_CASE(offset, func, type) \
  case offset:                          \
    return reinterpret_cast<void*>(&func<offset, type>);  // NOLINT [bugprone-macro-parentheses]

#define DO_SWITCH_OFFSET(offset, F, T) \
  switch (offset) { \
    SWITCH_CASE(0, F, T) \
    SWITCH_CASE(4, F, T) \
    SWITCH_CASE(8, F, T) \
    SWITCH_CASE(12, F, T) \
    SWITCH_CASE(16, F, T) \
    SWITCH_CASE(20, F, T) \
    SWITCH_CASE(24, F, T) \
    SWITCH_CASE(28, F, T) \
    SWITCH_CASE(32, F, T) \
    SWITCH_CASE(36, F, T) \
    SWITCH_CASE(40, F, T) \
    SWITCH_CASE(44, F, T) \
    SWITCH_CASE(48, F, T) \
    SWITCH_CASE(52, F, T) \
    SWITCH_CASE(56, F, T) \
    SWITCH_CASE(60, F, T) \
    SWITCH_CASE(64, F, T) \
    default: return nullptr; \
  }

#define DO_SWITCH(offset, O, P, K)                  \
  DCHECK_EQ(is_object, (K) == Primitive::kPrimNot); \
  switch (K) {                                      \
    case Primitive::kPrimBoolean:                   \
      DO_SWITCH_OFFSET(offset, P, uint8_t);         \
    case Primitive::kPrimInt:                       \
      DO_SWITCH_OFFSET(offset, P, int32_t);         \
    case Primitive::kPrimLong:                      \
      DO_SWITCH_OFFSET(offset, P, int64_t);         \
    case Primitive::kPrimNot:                       \
      DO_SWITCH_OFFSET(offset, O, mirror::Object*); \
    case Primitive::kPrimFloat:                     \
      if (kRuntimeISA == InstructionSet::kArm64) {  \
        DO_SWITCH_OFFSET(offset, P, float);         \
      } else {                                      \
        return nullptr;                             \
      }                                             \
    case Primitive::kPrimDouble:                    \
      if (kRuntimeISA == InstructionSet::kArm64) {  \
        DO_SWITCH_OFFSET(offset, P, double);        \
      } else {                                      \
        return nullptr;                             \
      }                                             \
    default:                                        \
      return nullptr;                               \
  }

const void* SmallPatternMatcher::TryMatch(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
  CodeItemDataAccessor accessor(*method->GetDexFile(), method->GetCodeItem());

  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();

  bool is_recognizable_constructor =
      method->IsConstructor() &&
      !method->IsStatic() &&
      method->GetDeclaringClass()->GetSuperClass() != nullptr &&
      method->GetDeclaringClass()->GetSuperClass()->IsObjectClass();

  size_t insns_size = accessor.InsnsSizeInCodeUnits();
  if (insns_size >= 4u) {
    if (!is_recognizable_constructor) {
      return nullptr;
    }
    // We can recognize a constructor with 6 or 4 code units.
    if (insns_size != 4u && insns_size != 6u) {
      return nullptr;
    }
  }

  auto is_object_init_invoke = [&](const Instruction& instruction)
      REQUIRES_SHARED(Locks::mutator_lock_) {
    uint16_t method_idx = instruction.VRegB_35c();
    Thread* self = Thread::Current();
    ArtMethod* target_method = class_linker->ResolveMethodId(method_idx, method);
    if (target_method == nullptr) {
      self->ClearException();
      return false;
    }
    if (!target_method->GetDeclaringClass()->IsObjectClass()) {
      return false;
    }
    DCHECK(target_method->GetDeclaringClass()->IsVerified());
    CodeItemDataAccessor accessor(*target_method->GetDexFile(), target_method->GetCodeItem());
    DCHECK_EQ(accessor.InsnsSizeInCodeUnits(), 1u);
    DCHECK_EQ(accessor.begin().Inst().Opcode(), Instruction::RETURN_VOID);
    return true;
  };

  // Recognize a constructor of the form:
  //   invoke-direct v0, j.l.Object.<init>
  //   return-void
  if (insns_size == 4u) {
    DCHECK(is_recognizable_constructor);
    const Instruction& instruction = accessor.begin().Inst();
    if (instruction.Opcode() == Instruction::INVOKE_DIRECT &&
        is_object_init_invoke(instruction)) {
      return reinterpret_cast<void*>(&EmptyMethod);
    }
    return nullptr;
  }

  // Recognize:
  //   return-void
  // Or:
  //   return-object v0
  if (insns_size == 1u) {
    const Instruction& instruction = accessor.begin().Inst();
    if (instruction.Opcode() == Instruction::RETURN_VOID) {
      return reinterpret_cast<void*>(&EmptyMethod);
    }

    if (instruction.Opcode() == Instruction::RETURN_OBJECT) {
      uint16_t number_of_vregs = accessor.RegistersSize();
      uint16_t number_of_parameters = accessor.InsSize();
      uint16_t obj_reg = number_of_vregs - number_of_parameters;
      if (obj_reg == instruction.VRegA_11x()) {
        return reinterpret_cast<void*>(&ReturnFirstArgMethod);
      }
    }
    return nullptr;
  }

  // Recognize:
  //   const vX, 0/1
  //   return{-object} vX
  if (insns_size == 2u) {
    if (method->GetReturnTypePrimitive() == Primitive::kPrimFloat) {
      // Too rare to bother.
      return nullptr;
    }
    int32_t register_index = -1;
    int32_t constant = -1;
    for (DexInstructionPcPair pair : accessor) {
      const Instruction& instruction = pair.Inst();
      switch (pair->Opcode()) {
        case Instruction::CONST_4: {
          register_index = instruction.VRegA_11n();
          constant = instruction.VRegB_11n();
          if (constant != 0 && constant != 1) {
            return nullptr;
          }
          break;
        }
        case Instruction::CONST_16: {
          register_index = instruction.VRegA_21s();
          constant = instruction.VRegB_21s();
          if (constant != 0 && constant != 1) {
            return nullptr;
          }
          break;
        }
        case Instruction::RETURN:
        case Instruction::RETURN_OBJECT: {
          if (register_index == instruction.VRegA_11x()) {
            if (constant == 0) {
              return reinterpret_cast<void*>(&ReturnZero);
            } else if (constant == 1) {
              return reinterpret_cast<void*>(&ReturnOne);
            }
          }
          return nullptr;
        }
        default:
          return nullptr;
      }
    }
    return nullptr;
  }

  // Recognize:
  //   iget-{object,wide,boolean} vX, v0, field
  //   return-{object} vX
  // Or:
  //   iput-{object,wide,boolean} v1, v0, field
  //   return-void
  // Or:
  //   sget-object vX, field
  //   return-object vX
  // Or:
  //   iput-{object,wide,boolean} v1, v0, field
  //   invoke-direct v0, j.l.Object.<init>
  //   return-void
  // Or:
  //   invoke-direct v0, j.l.Object.<init>
  //   iput-{object,wide,boolean} v1, v0, field
  //   return-void
  if (insns_size == 3u || insns_size == 6u) {
    DCHECK_IMPLIES(insns_size == 6u, is_recognizable_constructor);
    uint16_t number_of_vregs = accessor.RegistersSize();
    uint16_t number_of_parameters = accessor.InsSize();
    uint16_t obj_reg = number_of_vregs - number_of_parameters;
    uint16_t first_param_reg = number_of_vregs - number_of_parameters + 1;
    uint16_t dest_reg = -1;
    uint32_t offset = -1;
    bool is_object = false;
    bool is_put = false;
    bool is_static = false;
    bool is_final = false;
    Primitive::Type field_type;
    for (DexInstructionPcPair pair : accessor) {
      const Instruction& instruction = pair.Inst();
      switch (pair->Opcode()) {
        case Instruction::INVOKE_DIRECT:
          if (!is_recognizable_constructor || !is_object_init_invoke(instruction)) {
            return nullptr;
          }
          break;
        case Instruction::SGET_OBJECT:
          is_static = true;
          FALLTHROUGH_INTENDED;
        case Instruction::IPUT_OBJECT:
        case Instruction::IGET_OBJECT:
          is_object = true;
          FALLTHROUGH_INTENDED;
        case Instruction::IPUT:
        case Instruction::IGET:
        case Instruction::IGET_BOOLEAN:
        case Instruction::IPUT_BOOLEAN:
        case Instruction::IGET_WIDE:
        case Instruction::IPUT_WIDE: {
          is_put = (pair->Opcode() == Instruction::IPUT ||
                    pair->Opcode() == Instruction::IPUT_OBJECT ||
                    pair->Opcode() == Instruction::IPUT_BOOLEAN ||
                    pair->Opcode() == Instruction::IPUT_WIDE);
          if (!is_static && obj_reg != instruction.VRegB_22c()) {
            // The field access is not on the first parameter.
            return nullptr;
          }
          if (!is_static && method->IsStatic()) {
            // Getting/setting an instance field on an object that can be null.
            // Our stubs cannot handle implicit null checks.
            return nullptr;
          }
          if (is_put) {
            if (first_param_reg != instruction.VRegA_22c()) {
              // The value being stored is not the first parameter after 'this'.
              return nullptr;
            }
          } else {
            dest_reg = is_static ? instruction.VRegA_21c() : instruction.VRegA_22c();
          }
          uint16_t field_index = is_static ? instruction.VRegB_21c() : instruction.VRegC_22c();
          Thread* self = Thread::Current();
          ArtField* field =
              ResolveFieldWithAccessChecks(Thread::Current(),
                                           class_linker,
                                           field_index,
                                           method,
                                           is_static,
                                           is_put,
                                           /* resolve_field_type= */ is_put && is_object);
          if (field == nullptr) {
            self->ClearException();
            return nullptr;
          }
          if (field->IsVolatile()) {
            return nullptr;
          }
          if (is_static && field->GetDeclaringClass() != method->GetDeclaringClass()) {
            return nullptr;
          }
          offset = field->GetOffset().Int32Value();
          if (is_static) {
            // We subtract the start of reference fields to share more stubs.
            MemberOffset first_field_offset =
                field->GetDeclaringClass()->GetFirstReferenceStaticFieldOffset(kRuntimePointerSize);
            offset = offset - first_field_offset.Int32Value();
          } else {
            offset = offset - sizeof(mirror::Object);
          }
          if (offset > 64) {
            return nullptr;
          }
          field_type = field->GetTypeAsPrimitiveType();
          is_final = field->IsFinal();
          break;
        }
        case Instruction::RETURN_OBJECT:
        case Instruction::RETURN_WIDE:
        case Instruction::RETURN: {
          if (is_put || dest_reg != instruction.VRegA_11x()) {
            // The returned value is not the fetched field.
            return nullptr;
          }
          if (is_static) {
            DO_SWITCH(offset, ReturnStaticFieldObjectAt, ReturnStaticFieldAt, field_type);
          } else {
            DO_SWITCH(offset, ReturnFieldObjectAt, ReturnFieldAt, field_type);
          }
        }
        case Instruction::RETURN_VOID: {
          if (!is_put) {
            return nullptr;
          }
          if (is_final) {
            DCHECK(is_recognizable_constructor);
            DO_SWITCH(offset,  ConstructorSetFieldObjectAt, ConstructorSetFieldAt, field_type);
          } else {
            DO_SWITCH(offset, SetFieldObjectAt, SetFieldAt, field_type);
          }
        }
        default:
          return nullptr;
      }
    }
  }

  return nullptr;
}

}  // namespace jit
}  // namespace art