blob: 12ce98adc4805dc9aed47201edc0b5f634b87ae7 [file] [log] [blame]
Vladimir Marko8dea81c2014-06-06 14:50:36 +01001/*
2 * Copyright (C) 2014 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_COMPILER_DEX_QUICK_RESOURCE_MASK_H_
18#define ART_COMPILER_DEX_QUICK_RESOURCE_MASK_H_
19
20#include <stdint.h>
21
22#include "base/logging.h"
23#include "dex/reg_storage.h"
24
25namespace art {
26
27class ArenaAllocator;
28
29/**
30 * @brief Resource mask for LIR insn uses or defs.
31 * @detail Def/Use mask used for checking dependencies between LIR insns in local
32 * optimizations such as load hoisting.
33 */
34class ResourceMask {
35 private:
36 constexpr ResourceMask(uint64_t mask1, uint64_t mask2)
37 : masks_{ mask1, mask2 } { // NOLINT
38 }
39
40 public:
41 /*
42 * Def/Use encoding in 128-bit use_mask/def_mask. Low positions used for target-specific
43 * registers (and typically use the register number as the position). High positions
44 * reserved for common and abstract resources.
45 */
46 enum ResourceBit {
47 kMustNotAlias = 127,
48 kHeapRef = 126, // Default memory reference type.
49 kLiteral = 125, // Literal pool memory reference.
50 kDalvikReg = 124, // Dalvik v_reg memory reference.
51 kFPStatus = 123,
52 kCCode = 122,
53 kLowestCommonResource = kCCode,
54 kHighestCommonResource = kMustNotAlias
55 };
56
57 // Default-constructible.
58 constexpr ResourceMask()
59 : masks_ { 0u, 0u } {
60 }
61
62 // Copy-constructible and copyable.
63 ResourceMask(const ResourceMask& other) = default;
64 ResourceMask& operator=(const ResourceMask& other) = default;
65
66 static constexpr ResourceMask RawMask(uint64_t mask1, uint64_t mask2) {
67 return ResourceMask(mask1, mask2);
68 }
69
70 static constexpr ResourceMask Bit(size_t bit) {
71 return ResourceMask(bit >= 64u ? 0u : UINT64_C(1) << bit,
72 bit >= 64u ? UINT64_C(1) << (bit - 64u) : 0u);
73 }
74
75 // Two consecutive bits. The start_bit must be even.
76 static constexpr ResourceMask TwoBits(size_t start_bit) {
77 return
78 DCHECK_CONSTEXPR((start_bit & 1u) == 0u, << start_bit << " isn't even", Bit(0))
79 ResourceMask(start_bit >= 64u ? 0u : UINT64_C(3) << start_bit,
80 start_bit >= 64u ? UINT64_C(3) << (start_bit - 64u) : 0u);
81 }
82
83 static constexpr ResourceMask NoBits() {
84 return ResourceMask(UINT64_C(0), UINT64_C(0));
85 }
86
87 static constexpr ResourceMask AllBits() {
88 return ResourceMask(~UINT64_C(0), ~UINT64_C(0));
89 }
90
91 constexpr ResourceMask Union(const ResourceMask& other) const {
92 return ResourceMask(masks_[0] | other.masks_[0], masks_[1] | other.masks_[1]);
93 }
94
95 constexpr ResourceMask Intersection(const ResourceMask& other) const {
96 return ResourceMask(masks_[0] & other.masks_[0], masks_[1] & other.masks_[1]);
97 }
98
99 constexpr ResourceMask Without(const ResourceMask& other) const {
100 return ResourceMask(masks_[0] & ~other.masks_[0], masks_[1] & ~other.masks_[1]);
101 }
102
103 constexpr bool Equals(const ResourceMask& other) const {
104 return masks_[0] == other.masks_[0] && masks_[1] == other.masks_[1];
105 }
106
107 constexpr bool Intersects(const ResourceMask& other) const {
108 return (masks_[0] & other.masks_[0]) != 0u || (masks_[1] & other.masks_[1]) != 0u;
109 }
110
111 void SetBit(size_t bit) {
112 DCHECK_LE(bit, kHighestCommonResource);
113 masks_[bit / 64u] |= UINT64_C(1) << (bit & 63u);
114 }
115
116 constexpr bool HasBit(size_t bit) const {
117 return (masks_[bit / 64u] & (UINT64_C(1) << (bit & 63u))) != 0u;
118 }
119
120 ResourceMask& SetBits(const ResourceMask& other) {
121 masks_[0] |= other.masks_[0];
122 masks_[1] |= other.masks_[1];
123 return *this;
124 }
125
126 ResourceMask& ClearBits(const ResourceMask& other) {
127 masks_[0] &= ~other.masks_[0];
128 masks_[1] &= ~other.masks_[1];
129 return *this;
130 }
131
132 private:
133 uint64_t masks_[2];
134
135 friend class ResourceMaskCache;
136};
137
138constexpr ResourceMask kEncodeNone = ResourceMask::NoBits();
139constexpr ResourceMask kEncodeAll = ResourceMask::AllBits();
140constexpr ResourceMask kEncodeHeapRef = ResourceMask::Bit(ResourceMask::kHeapRef);
141constexpr ResourceMask kEncodeLiteral = ResourceMask::Bit(ResourceMask::kLiteral);
142constexpr ResourceMask kEncodeDalvikReg = ResourceMask::Bit(ResourceMask::kDalvikReg);
143constexpr ResourceMask kEncodeMem = kEncodeLiteral.Union(kEncodeDalvikReg).Union(
144 kEncodeHeapRef).Union(ResourceMask::Bit(ResourceMask::kMustNotAlias));
145
146class ResourceMaskCache {
147 public:
148 explicit ResourceMaskCache(ArenaAllocator* allocator)
149 : allocator_(allocator) {
150 }
151
152 const ResourceMask* GetMask(const ResourceMask& mask);
153
154 private:
155 ArenaAllocator* allocator_;
156};
157
158} // namespace art
159
160#endif // ART_COMPILER_DEX_QUICK_RESOURCE_MASK_H_