• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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_IR_CONSTANTS_H
17 #define COMPILER_OPTIMIZER_IR_CONSTANTS_H
18 
19 #include <cstdint>
20 #include <limits>
21 
22 #include "compiler_options.h"
23 
24 namespace ark::compiler {
25 constexpr int BITS_PER_BYTE = 8;
26 constexpr int BITS_PER_INSTPTR = sizeof(intptr_t) * BITS_PER_BYTE;
27 
28 using PcType = uint32_t;
29 using LinearNumber = uint32_t;
30 
31 // Update this when it will be strictly necessary to assign more than 255 registers in bytecode optimizer.
32 using Register = uint16_t;
33 using StackSlot = uint16_t;
34 using ImmTableSlot = uint16_t;
35 
36 constexpr uint32_t MAX_NUM_STACK_SLOTS = std::numeric_limits<uint8_t>::max();
37 constexpr uint32_t MAX_NUM_IMM_SLOTS = std::numeric_limits<uint8_t>::max();
38 
39 constexpr uint32_t INVALID_PC = std::numeric_limits<PcType>::max();
40 constexpr uint32_t INVALID_ID = std::numeric_limits<uint32_t>::max();
41 constexpr uint32_t INVALID_VN = std::numeric_limits<uint32_t>::max();
42 constexpr LinearNumber INVALID_LINEAR_NUM = std::numeric_limits<LinearNumber>::max();
43 constexpr Register INVALID_REG = std::numeric_limits<uint8_t>::max();
44 constexpr StackSlot INVALID_STACK_SLOT = std::numeric_limits<uint8_t>::max();
45 constexpr ImmTableSlot INVALID_IMM_TABLE_SLOT = std::numeric_limits<uint8_t>::max();
46 constexpr std::uint32_t INVALID_COLUMN_NUM = std::numeric_limits<std::uint32_t>::max();
47 
48 constexpr Register VIRTUAL_FRAME_SIZE = INVALID_REG - 1U;
49 
50 using LifeNumber = uint32_t;
51 constexpr auto INVALID_LIFE_NUMBER = std::numeric_limits<LifeNumber>::max();
52 constexpr auto LIFE_NUMBER_GAP = 2U;
53 
54 enum ShiftType : uint8_t { LSL, LSR, ASR, ROR, INVALID_SHIFT };
55 
56 enum ShiftOpcode { NEG_SR, ADD_SR, SUB_SR, AND_SR, OR_SR, XOR_SR, AND_NOT_SR, OR_NOT_SR, XOR_NOT_SR, INVALID_SR };
57 
58 constexpr uint32_t MAX_SCALE = 3;
59 
60 constexpr int MAX_SUCCS_NUM = 2;
61 
62 #ifdef ENABLE_LIBABCKIT
63 constexpr uint32_t MAX_VALUE = ((1U << (2 * BITS_PER_BYTE - 4))) * 2 - 1U;
64 constexpr uint32_t MAX_NUM_STACK_SLOTS_LARGE = MAX_VALUE;
65 constexpr uint32_t MAX_NUM_IMM_SLOTS_LARGE = MAX_VALUE;
66 constexpr Register INVALID_REG_LARGE = MAX_VALUE;
67 constexpr StackSlot INVALID_STACK_SLOT_LARGE = MAX_VALUE;
68 constexpr ImmTableSlot INVALID_IMM_TABLE_SLOT_LARGE = MAX_VALUE;
69 constexpr Register VIRTUAL_FRAME_SIZE_LARGE = INVALID_REG_LARGE - 1U;
70 
IsFrameSizeLarge()71 inline bool IsFrameSizeLarge()
72 {
73     return UNLIKELY(g_options.GetCompilerFrameSize() == "large");
74 }
75 
GetFrameSize()76 inline uint16_t GetFrameSize()
77 {
78     return IsFrameSizeLarge() ? VIRTUAL_FRAME_SIZE_LARGE : VIRTUAL_FRAME_SIZE;
79 }
80 
GetInvalidReg()81 inline Register GetInvalidReg()
82 {
83     return IsFrameSizeLarge() ? INVALID_REG_LARGE : INVALID_REG;
84 }
85 
GetInvalidStackSlot()86 inline StackSlot GetInvalidStackSlot()
87 {
88     return IsFrameSizeLarge() ? INVALID_STACK_SLOT_LARGE : INVALID_STACK_SLOT;
89 }
90 
GetInvalidImmTableSlot()91 inline ImmTableSlot GetInvalidImmTableSlot()
92 {
93     return IsFrameSizeLarge() ? INVALID_IMM_TABLE_SLOT_LARGE : INVALID_IMM_TABLE_SLOT;
94 }
95 
GetMaxNumStackSlots()96 inline uint16_t GetMaxNumStackSlots()
97 {
98     return IsFrameSizeLarge() ? MAX_NUM_STACK_SLOTS_LARGE : MAX_NUM_STACK_SLOTS;
99 }
100 
GetMaxNumImmSlots()101 inline uint16_t GetMaxNumImmSlots()
102 {
103     return IsFrameSizeLarge() ? MAX_NUM_IMM_SLOTS_LARGE : MAX_NUM_IMM_SLOTS;
104 }
105 
106 #else
107 constexpr Register VIRTUAL_FRAME_SIZE_LARGE = VIRTUAL_FRAME_SIZE;
108 constexpr uint32_t MAX_NUM_STACK_SLOTS_LARGE = MAX_NUM_STACK_SLOTS;
IsFrameSizeLarge()109 inline bool IsFrameSizeLarge()
110 {
111     ASSERT(g_options.GetCompilerFrameSize() == "default");
112     return false;
113 }
114 
GetFrameSize()115 inline uint16_t GetFrameSize()
116 {
117     return VIRTUAL_FRAME_SIZE;
118 }
119 
GetInvalidReg()120 inline Register GetInvalidReg()
121 {
122     return INVALID_REG;
123 }
124 
GetInvalidStackSlot()125 inline StackSlot GetInvalidStackSlot()
126 {
127     return INVALID_STACK_SLOT;
128 }
129 
GetInvalidImmTableSlot()130 inline ImmTableSlot GetInvalidImmTableSlot()
131 {
132     return INVALID_IMM_TABLE_SLOT;
133 }
134 
GetMaxNumStackSlots()135 inline uint16_t GetMaxNumStackSlots()
136 {
137     return MAX_NUM_STACK_SLOTS;
138 }
139 
GetMaxNumImmSlots()140 inline uint16_t GetMaxNumImmSlots()
141 {
142     return MAX_NUM_IMM_SLOTS;
143 }
144 #endif
145 
146 }  // namespace ark::compiler
147 
148 #endif  // COMPILER_OPTIMIZER_IR_CONSTANTS_H
149