• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef COMPILER_OPTIMIZER_OPTIMIZATIONS_REGALLOC_LOCATION_MASK_H
17 #define COMPILER_OPTIMIZER_OPTIMIZATIONS_REGALLOC_LOCATION_MASK_H
18 
19 #include "mem/arena_allocator.h"
20 #include "utils/arena_containers.h"
21 
22 namespace panda::compiler {
23 /**
24  * Helper-class to hold information about registers and stack slots usage.
25  */
26 class LocationMask {
27 public:
LocationMask(ArenaAllocator * allocator)28     explicit LocationMask(ArenaAllocator *allocator) : mask_(allocator->Adapter()), usage_(allocator->Adapter()) {}
29     ~LocationMask() = default;
30     DEFAULT_COPY_SEMANTIC(LocationMask);
31     DEFAULT_MOVE_SEMANTIC(LocationMask);
32 
33     template <typename T>
Init(const T & mask)34     void Init(const T &mask)
35     {
36         usage_.resize(mask.size());
37         mask_.resize(mask.size());
38         for (size_t idx = 0; idx < mask.size(); idx++) {
39             if (mask.test(idx)) {
40                 Set(idx);
41             } else {
42                 Reset(idx);
43             }
44         }
45     }
46 
Resize(size_t size)47     void Resize(size_t size)
48     {
49         mask_.resize(size, false);
50         usage_.resize(size, false);
51     }
52 
Set(size_t position)53     void Set(size_t position)
54     {
55         ASSERT(position < mask_.size());
56         mask_[position] = true;
57         usage_[position] = true;
58     }
59 
Reset(size_t position)60     void Reset(size_t position)
61     {
62         ASSERT(position < mask_.size());
63         mask_[position] = false;
64     }
65 
IsSet(size_t position)66     bool IsSet(size_t position) const
67     {
68         ASSERT(position < mask_.size());
69         return mask_[position];
70     }
71 
Reserve(size_t reserved_bit)72     void Reserve(size_t reserved_bit)
73     {
74         reserved_bit_ = reserved_bit;
75         Set(reserved_bit);
76     }
77 
GetReserved()78     std::optional<size_t> GetReserved() const
79     {
80         return reserved_bit_;
81     }
82 
GetVector()83     const auto &GetVector() const
84     {
85         return mask_;
86     }
87 
88     std::optional<size_t> GetNextNotSet(size_t first_bit = 0)
89     {
90         for (size_t r = first_bit; r < mask_.size(); r++) {
91             if (!mask_[r]) {
92                 Set(r);
93                 return r;
94             }
95         }
96         for (size_t r = 0; r < first_bit; r++) {
97             if (!mask_[r]) {
98                 Set(r);
99                 return r;
100             }
101         }
102         return std::nullopt;
103     }
104 
AllSet()105     bool AllSet() const
106     {
107         return std::all_of(mask_.begin(), mask_.end(), [](bool v) { return v; });
108     }
109 
GetUsedCount()110     size_t GetUsedCount() const
111     {
112         return std::count(usage_.begin(), usage_.end(), true);
113     }
114 
GetSize()115     size_t GetSize() const
116     {
117         return mask_.size();
118     }
119 
Empty()120     bool Empty() const
121     {
122         return mask_.empty();
123     }
124 
Dump(std::ostream * out)125     void Dump(std::ostream *out) const
126     {
127         for (bool val : mask_) {
128             (*out) << val;
129         }
130         (*out) << "\n";
131     }
132 
133 private:
134     ArenaVector<bool> mask_;
135     ArenaVector<bool> usage_;
136     std::optional<size_t> reserved_bit_ {std::nullopt};
137 };
138 }  // namespace panda::compiler
139 
140 #endif  // COMPILER_OPTIMIZER_OPTIMIZATIONS_REGALLOC_LOCATION_MASK_H