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