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 #include "constant_propagation.h"
30
31 namespace panda::bytecodeopt {
32
LatticeElement(LatticeType type)33 LatticeElement::LatticeElement(LatticeType type) : type_(type) {}
34
TopElement()35 TopElement::TopElement() : LatticeElement(LatticeType::LATTICE_TOP) {}
36
Meet(LatticeElement * other)37 LatticeElement *TopElement::Meet(LatticeElement *other)
38 {
39 return other;
40 }
41
GetInstance()42 LatticeElement *TopElement::GetInstance()
43 {
44 static TopElement instance;
45 return &instance;
46 }
47
ToString()48 std::string TopElement::ToString()
49 {
50 return "Top";
51 }
52
BottomElement()53 BottomElement::BottomElement() : LatticeElement(LatticeType::LATTICE_BOTTOM) {}
54
Meet(LatticeElement * other)55 LatticeElement *BottomElement::Meet(LatticeElement *other)
56 {
57 return this;
58 }
59
GetInstance()60 LatticeElement *BottomElement::GetInstance()
61 {
62 static BottomElement instance;
63 return &instance;
64 }
65
ToString()66 std::string BottomElement::ToString()
67 {
68 return "Bottom";
69 }
70
ConstantElement(bool val)71 ConstantElement::ConstantElement(bool val)
72 : LatticeElement(LatticeType::LATTICE_CONSTANT), value_(val)
73 {
74 }
75
ConstantElement(int32_t val)76 ConstantElement::ConstantElement(int32_t val)
77 : LatticeElement(LatticeType::LATTICE_CONSTANT), value_(val)
78 {
79 }
80
ConstantElement(int64_t val)81 ConstantElement::ConstantElement(int64_t val)
82 : LatticeElement(LatticeType::LATTICE_CONSTANT), value_(val)
83 {
84 }
85
ConstantElement(double val)86 ConstantElement::ConstantElement(double val)
87 : LatticeElement(LatticeType::LATTICE_CONSTANT), value_(val)
88 {
89 }
90
ConstantElement(std::string val)91 ConstantElement::ConstantElement(std::string val)
92 : LatticeElement(LatticeType::LATTICE_CONSTANT), value_(val)
93 {
94 }
95
ConstantElement(const ConstantValue & val)96 ConstantElement::ConstantElement(const ConstantValue &val)
97 : LatticeElement(LatticeType::LATTICE_CONSTANT), value_(val)
98 {
99 }
100
Meet(LatticeElement * other)101 LatticeElement *ConstantElement::Meet(LatticeElement *other)
102 {
103 if (other->IsTopElement()) {
104 return this;
105 }
106 if (other->IsBottomElement()) {
107 return other;
108 }
109
110 if (value_ == other->AsConstant()->value_) {
111 return this;
112 }
113
114 return BottomElement::GetInstance();
115 }
116
ToString()117 std::string ConstantElement::ToString()
118 {
119 return "Constant: " + value_.ToString();
120 }
121
AsConstant()122 ConstantElement *ConstantElement::AsConstant()
123 {
124 return this;
125 }
126
127 } // namespace panda::bytecodeopt
128