• 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_REG_MAP_H
17 #define COMPILER_OPTIMIZER_OPTIMIZATIONS_REGALLOC_REG_MAP_H
18 
19 #include "utils/arena_containers.h"
20 #include "location_mask.h"
21 #include "optimizer/ir/constants.h"
22 #include "utils/arch.h"
23 
24 namespace panda::compiler {
25 
26 /**
27  * Since the set of available codegen's registers can be sparse, we create local regalloc registers vector with size
28  * equal to the number of given registers and save map to restore actual codegen register number from the regalloc's
29  * register number.
30  *
31  * For example
32  * - there are 3 available registers: r16, r18, r20;
33  * - r18 has priority to be assigned;
34  *
35  * - `codegen_reg_map_` will be equal to [18, 20, 16]
36  * - RegAlloc locally assigns registers with numbers 0, 1 and 2 and then replace them by the codegen's registers 18,
37  * 20 and 16 accordingly.
38  */
39 class RegisterMap {
40 public:
RegisterMap(ArenaAllocator * allocator)41     explicit RegisterMap(ArenaAllocator *allocator) : codegen_reg_map_(allocator->Adapter()) {}
42     ~RegisterMap() = default;
43     NO_MOVE_SEMANTIC(RegisterMap);
44     NO_COPY_SEMANTIC(RegisterMap);
45 
46     void SetMask(const LocationMask &reg_mask, size_t priority_reg);
47     void SetCallerFirstMask(const LocationMask &reg_mask, size_t first_callee_reg, size_t last_callee_reg);
48     size_t Size() const;
49     size_t GetAvailableRegsCount() const;
50     bool IsRegAvailable(Register reg, Arch arch) const;
51     Register CodegenToRegallocReg(Register codegen_reg) const;
52     Register RegallocToCodegenReg(Register regalloc_reg) const;
GetBorder()53     Register GetBorder() const
54     {
55         return border_;
56     }
57     void Dump(std::ostream *out) const;
58 
59 private:
60     ArenaVector<Register> codegen_reg_map_;
61     size_t available_regs_count_ {0};
62     Register border_ {0};
63 };
64 
65 }  // namespace panda::compiler
66 
67 #endif  // COMPILER_OPTIMIZER_OPTIMIZATIONS_REGALLOC_REG_MAP_H
68