1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef ART_RUNTIME_SUBTYPE_CHECK_BITS_AND_STATUS_H_
18 #define ART_RUNTIME_SUBTYPE_CHECK_BITS_AND_STATUS_H_
19
20 #include "base/bit_struct.h"
21 #include "base/bit_utils.h"
22 #include "base/casts.h"
23 #include "base/macros.h"
24 #include "class_status.h"
25 #include "subtype_check_bits.h"
26
27 namespace art HIDDEN {
28
29 /*
30 * Enables a highly efficient O(1) subtype comparison by storing extra data
31 * in the unused padding bytes of ClassStatus.
32 */
33
34 // TODO: Update BitSizeOf with this version.
35 template <typename T>
NonNumericBitSizeOf()36 static constexpr size_t NonNumericBitSizeOf() {
37 return kBitsPerByte * sizeof(T);
38 }
39
40 /**
41 * MSB (most significant bit) LSB
42 * +---------------+---------------------------------------------------+
43 * | | |
44 * | ClassStatus | SubtypeCheckBits |
45 * | | |
46 * +---------------+---------------------------------------------------+
47 * <-- 4 bits --> <----- 28 bits ----->
48 *
49 * Invariants:
50 *
51 * AddressOf(ClassStatus) == AddressOf(SubtypeCheckBitsAndStatus)
52 * BitSizeOf(SubtypeCheckBitsAndStatus) == 32
53 *
54 * Note that with this representation the "Path To Root" in the MSB of this 32-bit word:
55 * This enables a highly efficient path comparison between any two labels:
56 *
57 * src <: target :=
58 * (src & mask) == (target & mask) where mask := (1u << len(path-to-root(target)) - 1u
59 *
60 * In the above example, the `len()` (and thus `mask`) is a function of the depth.
61 * Since the target is known at compile time, it becomes
62 * (src & #imm_mask) == #imm
63 * or
64 * ((src - #imm) << #imm_shift_to_remove_high_bits) == 0
65 * or a similar expression chosen for the best performance or code size.
66 *
67 * (This requires that path-to-root in `target` is not truncated, i.e. it is in the Assigned state).
68 */
69 static constexpr size_t kClassStatusBitSize = MinimumBitsToStore(enum_cast<>(ClassStatus::kLast));
70 static_assert(kClassStatusBitSize == 4u, "ClassStatus should need 4 bits.");
71 BITSTRUCT_DEFINE_START(SubtypeCheckBitsAndStatus, BitSizeOf<BitString::StorageType>())
72 BITSTRUCT_FIELD(SubtypeCheckBits,
73 /*lsb=*/ 0,
74 /*width=*/ SubtypeCheckBits::BitStructSizeOf()) subtype_check_info_;
75 BITSTRUCT_FIELD(ClassStatus,
76 /*lsb=*/ SubtypeCheckBits::BitStructSizeOf(),
77 /*width=*/ kClassStatusBitSize) status_;
78 BITSTRUCT_INT(/*lsb=*/ 0, /*width=*/ BitSizeOf<BitString::StorageType>()) int32_alias_;
79 BITSTRUCT_DEFINE_END(SubtypeCheckBitsAndStatus);
80
81 // Use the spare alignment from "ClassStatus" to store all the new SubtypeCheckInfo data.
82 static_assert(sizeof(SubtypeCheckBitsAndStatus) == sizeof(uint32_t),
83 "All of SubtypeCheckInfo+ClassStatus should fit into 4 bytes");
84 } // namespace art
85
86 #endif // ART_RUNTIME_SUBTYPE_CHECK_BITS_AND_STATUS_H_
87