blob: eec6e218328693a48f6f78a738bca18f79beea2d [file] [log] [blame]
Igor Murashkin495e7832017-10-27 10:56:20 -07001/*
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"
Vladimir Markodc682aa2018-01-04 18:42:57 +000022#include "base/casts.h"
Igor Murashkin495e7832017-10-27 10:56:20 -070023#include "class_status.h"
24#include "subtype_check_bits.h"
25
26namespace 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.
34template <typename T>
35static constexpr size_t NonNumericBitSizeOf() {
36 return kBitsPerByte * sizeof(T);
37}
38
39/**
Vladimir Markodc682aa2018-01-04 18:42:57 +000040 * MSB (most significant bit) LSB
41 * +---------------+---------------------------------------------------+
42 * | | |
43 * | ClassStatus | SubtypeCheckBits |
44 * | | |
45 * +---------------+---------------------------------------------------+
46 * <-- 4 bits --> <----- 28 bits ----->
Igor Murashkin495e7832017-10-27 10:56:20 -070047 *
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 :=
Vladimir Markodc682aa2018-01-04 18:42:57 +000057 * (src & mask) == (target & mask) where mask := (1u << len(path-to-root(target)) - 1u
Igor Murashkin495e7832017-10-27 10:56:20 -070058 *
Vladimir Markodc682aa2018-01-04 18:42:57 +000059 * 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.
Igor Murashkin495e7832017-10-27 10:56:20 -070065 *
66 * (This requires that path-to-root in `target` is not truncated, i.e. it is in the Assigned state).
67 */
Vladimir Markodc682aa2018-01-04 18:42:57 +000068static constexpr size_t kClassStatusBitSize = MinimumBitsToStore(enum_cast<>(ClassStatus::kLast));
69static_assert(kClassStatusBitSize == 4u, "ClassStatus should need 4 bits.");
Igor Murashkin495e7832017-10-27 10:56:20 -070070BITSTRUCT_DEFINE_START(SubtypeCheckBitsAndStatus, BitSizeOf<BitString::StorageType>())
Andreas Gampe98ea9d92018-10-19 14:06:15 -070071 BitStructField<SubtypeCheckBits, /*lsb=*/ 0> subtype_check_info_;
Vladimir Markodc682aa2018-01-04 18:42:57 +000072 BitStructField<ClassStatus,
Andreas Gampe98ea9d92018-10-19 14:06:15 -070073 /*lsb=*/ SubtypeCheckBits::BitStructSizeOf(),
74 /*width=*/ kClassStatusBitSize> status_;
75 BitStructInt</*lsb=*/ 0, /*width=*/ BitSizeOf<BitString::StorageType>()> int32_alias_;
Igor Murashkin495e7832017-10-27 10:56:20 -070076BITSTRUCT_DEFINE_END(SubtypeCheckBitsAndStatus);
77
78// Use the spare alignment from "ClassStatus" to store all the new SubtypeCheckInfo data.
79static_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_