1495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin/*
2495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin * Copyright (C) 2017 The Android Open Source Project
3495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin *
4495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin * Licensed under the Apache License, Version 2.0 (the "License");
5495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin * you may not use this file except in compliance with the License.
6495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin * You may obtain a copy of the License at
7495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin *
8495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin *      http://www.apache.org/licenses/LICENSE-2.0
9495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin *
10495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin * Unless required by applicable law or agreed to in writing, software
11495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin * distributed under the License is distributed on an "AS IS" BASIS,
12495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin * See the License for the specific language governing permissions and
14495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin * limitations under the License.
15495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin */
16495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin
17495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin#ifndef ART_RUNTIME_SUBTYPE_CHECK_BITS_AND_STATUS_H_
18495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin#define ART_RUNTIME_SUBTYPE_CHECK_BITS_AND_STATUS_H_
19495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin
20495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin#include "base/bit_struct.h"
21495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin#include "base/bit_utils.h"
22dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Marko#include "base/casts.h"
23495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin#include "class_status.h"
24495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin#include "subtype_check_bits.h"
25495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin
26495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkinnamespace art {
27495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin
28495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin/*
29495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin * Enables a highly efficient O(1) subtype comparison by storing extra data
30495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin * in the unused padding bytes of ClassStatus.
31495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin */
32495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin
33495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin// TODO: Update BitSizeOf with this version.
34495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkintemplate <typename T>
35495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkinstatic constexpr size_t NonNumericBitSizeOf() {
36495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin    return kBitsPerByte * sizeof(T);
37495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin}
38495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin
39495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin/**
40dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Marko * MSB (most significant bit)                                          LSB
41dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Marko *  +---------------+---------------------------------------------------+
42dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Marko *  |               |                                                   |
43dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Marko *  |  ClassStatus  |                 SubtypeCheckBits                  |
44dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Marko *  |               |                                                   |
45dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Marko *  +---------------+---------------------------------------------------+
46dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Marko *   <-- 4 bits -->             <-----     28 bits     ----->
47495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin *
48495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin * Invariants:
49495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin *
50495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin *  AddressOf(ClassStatus) == AddressOf(SubtypeCheckBitsAndStatus)
51495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin *  BitSizeOf(SubtypeCheckBitsAndStatus) == 32
52495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin *
53495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin * Note that with this representation the "Path To Root" in the MSB of this 32-bit word:
54495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin * This enables a highly efficient path comparison between any two labels:
55495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin *
56495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin * src <: target :=
57dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Marko *   (src & mask) == (target & mask)  where  mask := (1u << len(path-to-root(target)) - 1u
58495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin *
59dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Marko * In the above example, the `len()` (and thus `mask`) is a function of the depth.
60dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Marko * Since the target is known at compile time, it becomes
61dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Marko *   (src & #imm_mask) == #imm
62dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Marko * or
63dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Marko *   ((src - #imm) << #imm_shift_to_remove_high_bits) == 0
64dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Marko * or a similar expression chosen for the best performance or code size.
65495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin *
66495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin * (This requires that path-to-root in `target` is not truncated, i.e. it is in the Assigned state).
67495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin */
68dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Markostatic constexpr size_t kClassStatusBitSize = MinimumBitsToStore(enum_cast<>(ClassStatus::kLast));
69dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Markostatic_assert(kClassStatusBitSize == 4u, "ClassStatus should need 4 bits.");
70495e783c9180c3fc033ce459ee0a783e633f7754Igor MurashkinBITSTRUCT_DEFINE_START(SubtypeCheckBitsAndStatus, BitSizeOf<BitString::StorageType>())
71dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Marko  BitStructField<SubtypeCheckBits, /*lsb*/ 0> subtype_check_info_;
72dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Marko  BitStructField<ClassStatus,
73dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Marko                 /*lsb*/ SubtypeCheckBits::BitStructSizeOf(),
74dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Marko                 /*width*/ kClassStatusBitSize> status_;
75dc682aa9d0eae1a851af059434adb6f6cf8f06f8Vladimir Marko  BitStructInt</*lsb*/ 0, /*width*/ BitSizeOf<BitString::StorageType>()> int32_alias_;
76495e783c9180c3fc033ce459ee0a783e633f7754Igor MurashkinBITSTRUCT_DEFINE_END(SubtypeCheckBitsAndStatus);
77495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin
78495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin// Use the spare alignment from "ClassStatus" to store all the new SubtypeCheckInfo data.
79495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkinstatic_assert(sizeof(SubtypeCheckBitsAndStatus) == sizeof(uint32_t),
80495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin              "All of SubtypeCheckInfo+ClassStatus should fit into 4 bytes");
81495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin}  // namespace art
82495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin
83495e783c9180c3fc033ce459ee0a783e633f7754Igor Murashkin#endif  // ART_RUNTIME_SUBTYPE_CHECK_BITS_AND_STATUS_H_
84