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 "class_status.h"
24 #include "subtype_check_bits.h"
25 
26 namespace art {
27 
28 /*
29  * Enables a highly efficient O(1) subtype comparison by storing extra data
30  * in the unused padding bytes of ClassStatus.
31  */
32 
33 // TODO: Update BitSizeOf with this version.
34 template <typename T>
NonNumericBitSizeOf()35 static constexpr size_t NonNumericBitSizeOf() {
36     return kBitsPerByte * sizeof(T);
37 }
38 
39 /**
40  * MSB (most significant bit)                                          LSB
41  *  +---------------+---------------------------------------------------+
42  *  |               |                                                   |
43  *  |  ClassStatus  |                 SubtypeCheckBits                  |
44  *  |               |                                                   |
45  *  +---------------+---------------------------------------------------+
46  *   <-- 4 bits -->             <-----     28 bits     ----->
47  *
48  * Invariants:
49  *
50  *  AddressOf(ClassStatus) == AddressOf(SubtypeCheckBitsAndStatus)
51  *  BitSizeOf(SubtypeCheckBitsAndStatus) == 32
52  *
53  * Note that with this representation the "Path To Root" in the MSB of this 32-bit word:
54  * This enables a highly efficient path comparison between any two labels:
55  *
56  * src <: target :=
57  *   (src & mask) == (target & mask)  where  mask := (1u << len(path-to-root(target)) - 1u
58  *
59  * In the above example, the `len()` (and thus `mask`) is a function of the depth.
60  * Since the target is known at compile time, it becomes
61  *   (src & #imm_mask) == #imm
62  * or
63  *   ((src - #imm) << #imm_shift_to_remove_high_bits) == 0
64  * or a similar expression chosen for the best performance or code size.
65  *
66  * (This requires that path-to-root in `target` is not truncated, i.e. it is in the Assigned state).
67  */
68 static constexpr size_t kClassStatusBitSize = MinimumBitsToStore(enum_cast<>(ClassStatus::kLast));
69 static_assert(kClassStatusBitSize == 4u, "ClassStatus should need 4 bits.");
70 BITSTRUCT_DEFINE_START(SubtypeCheckBitsAndStatus, BitSizeOf<BitString::StorageType>())
71   BitStructField<SubtypeCheckBits, /*lsb=*/ 0> subtype_check_info_;
72   BitStructField<ClassStatus,
73                  /*lsb=*/ SubtypeCheckBits::BitStructSizeOf(),
74                  /*width=*/ kClassStatusBitSize> status_;
75   BitStructInt</*lsb=*/ 0, /*width=*/ BitSizeOf<BitString::StorageType>()> int32_alias_;
76 BITSTRUCT_DEFINE_END(SubtypeCheckBitsAndStatus);
77 
78 // Use the spare alignment from "ClassStatus" to store all the new SubtypeCheckInfo data.
79 static_assert(sizeof(SubtypeCheckBitsAndStatus) == sizeof(uint32_t),
80               "All of SubtypeCheckInfo+ClassStatus should fit into 4 bytes");
81 }  // namespace art
82 
83 #endif  // ART_RUNTIME_SUBTYPE_CHECK_BITS_AND_STATUS_H_
84