• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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  * Copyright (c) 2024 Huawei Device Co., Ltd.
16  * Licensed under the Apache License, Version 2.0 (the "License");
17  * you may not use this file except in compliance with the License.
18  * You may obtain a copy of the License at
19 
20  * http://www.apache.org/licenses/LICENSE-2.0
21  *
22  * Unless required by applicable law or agreed to in writing, software
23  * distributed under the License is distributed on an "AS IS" BASIS,
24  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  * See the License for the specific language governing permissions and
26  * limitations under the License.
27  */
28 
29 #ifndef BYTECODE_OPTIMIZER_CONSTANT_PROPAGATION_LATTICE_ELEMENT_H
30 #define BYTECODE_OPTIMIZER_CONSTANT_PROPAGATION_LATTICE_ELEMENT_H
31 
32 #include <variant>
33 #include "compiler/optimizer/ir/basicblock.h"
34 #include "compiler/optimizer/ir/graph.h"
35 #include "compiler/optimizer/ir/graph_visitor.h"
36 #include "compiler/optimizer/pass.h"
37 #include "constant_value.h"
38 #include "utils/hash.h"
39 
40 namespace panda::bytecodeopt {
41 
42 class ConstantElement;
43 class LatticeElement {
44 public:
45     enum class LatticeType {
46         LATTICE_TOP,
47         LATTICE_CONSTANT,
48         LATTICE_BOTTOM,
49     };
50 
51     explicit LatticeElement(LatticeType type);
52     virtual ~LatticeElement() = default;
53     NO_MOVE_SEMANTIC(LatticeElement);
54     NO_COPY_SEMANTIC(LatticeElement);
55 
IsConstantElement()56     bool IsConstantElement()
57     {
58         return type_ == LatticeType::LATTICE_CONSTANT;
59     }
60 
IsTopElement()61     bool IsTopElement()
62     {
63         return type_ == LatticeType::LATTICE_TOP;
64     }
65 
IsBottomElement()66     bool IsBottomElement()
67     {
68         return type_ == LatticeType::LATTICE_BOTTOM;
69     }
70 
AsConstant()71     virtual ConstantElement *AsConstant()
72     {
73         return nullptr;
74     }
75 
76     virtual LatticeElement *Meet(LatticeElement *other) = 0;
77     virtual std::string ToString() = 0;
78 
79 private:
80     LatticeType type_;
81 };
82 
83 class TopElement : public LatticeElement {
84 public:
85     NO_MOVE_SEMANTIC(TopElement);
86     NO_COPY_SEMANTIC(TopElement);
87     static LatticeElement *GetInstance();
88     ~TopElement() override = default;
89     LatticeElement *Meet(LatticeElement *other) override;
90     std::string ToString() override;
91 
92 protected:
93     TopElement();
94 };
95 
96 class BottomElement : public LatticeElement {
97 public:
98     NO_MOVE_SEMANTIC(BottomElement);
99     NO_COPY_SEMANTIC(BottomElement);
100     ~BottomElement() override = default;
101     LatticeElement *Meet(LatticeElement *other) override;
102     std::string ToString() override;
103     static LatticeElement *GetInstance();
104 
105 protected:
106     BottomElement();
107 };
108 
109 class ConstantElement : public LatticeElement {
110 public:
111     NO_MOVE_SEMANTIC(ConstantElement);
112     NO_COPY_SEMANTIC(ConstantElement);
113     explicit ConstantElement(bool val);
114     explicit ConstantElement(int32_t val);
115     explicit ConstantElement(int64_t val);
116     explicit ConstantElement(double val);
117     explicit ConstantElement(std::string val);
118     explicit ConstantElement(const ConstantValue &val);
119     ~ConstantElement() override = default;
120     LatticeElement *Meet(LatticeElement *other) override;
121     std::string ToString() override;
122     ConstantElement *AsConstant() override;
123 
124     template<class T>
GetValue()125     T GetValue() const
126     {
127         return value_.GetValue<T>();
128     }
129 
GetValue()130     auto &GetValue() const
131     {
132         return value_.GetValue();
133     }
134 
GetType()135     ConstantValue::ConstantType GetType() const
136     {
137         return value_.GetType();
138     }
139 
140 private:
141     ConstantValue value_;
142 };
143 
144 }  // namespace panda::bytecodeopt
145 
146 #endif  // BYTECODE_OPTIMIZER_CONSTANT_PROPAGATION_LATTICE_ELEMENT_H
147