1 /*
2 * Copyright (c) 2023 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 #include "ecmascript/compiler/value_numbering.h"
16
17 namespace panda::ecmascript::kungfu {
Run()18 void ValueNumbering::Run()
19 {
20 VisitGraph();
21 if (IsLogEnabled()) {
22 LOG_COMPILER(INFO) << "";
23 LOG_COMPILER(INFO) << "\033[34m"
24 << "===================="
25 << " After value numbering "
26 << "[" << GetMethodName() << "]"
27 << "===================="
28 << "\033[0m";
29 circuit_->PrintAllGatesWithBytecode();
30 LOG_COMPILER(INFO) << "\033[34m" << "========================= End ==========================" << "\033[0m";
31 }
32 }
33
VisitGate(GateRef gate)34 GateRef ValueNumbering::VisitGate(GateRef gate)
35 {
36 auto opcode = acc_.GetOpCode(gate);
37 if (opcode != OpCode::CONVERT) {
38 return Circuit::NullGate();
39 }
40 size_t hash = HashCode(gate);
41 if (entries_.size() == 0) {
42 entries_.resize(CACHE_LENGTH, Circuit::NullGate());
43 SetEntry(hash, gate);
44 return Circuit::NullGate();
45 }
46 GateRef replacement = GetEntry(hash);
47 if (replacement != Circuit::NullGate() &&
48 CheckReplacement(gate, replacement)) {
49 return replacement;
50 }
51 SetEntry(hash, gate);
52 return Circuit::NullGate();
53 }
54
HashCode(GateRef gate)55 size_t ValueNumbering::HashCode(GateRef gate)
56 {
57 size_t hash = static_cast<size_t>(acc_.GetOpCode(gate));
58 hash ^= acc_.TryGetValue(gate);
59 size_t valueCount = acc_.GetNumValueIn(gate);
60 for (size_t i = 0; i < valueCount; i++) {
61 GateRef input = acc_.GetValueIn(gate);
62 auto id = acc_.GetId(input);
63 hash ^= id;
64 }
65 return hash % CACHE_LENGTH;
66 }
67
CheckReplacement(GateRef lhs,GateRef rhs)68 bool ValueNumbering::CheckReplacement(GateRef lhs, GateRef rhs)
69 {
70 if (!acc_.MetaDataEqu(lhs, rhs)) {
71 if (acc_.GetOpCode(lhs) != acc_.GetOpCode(rhs)) {
72 return false;
73 }
74 }
75 size_t valueCount = acc_.GetNumValueIn(lhs);
76 for (size_t i = 0; i < valueCount; i++) {
77 if (acc_.GetValueIn(lhs, i) != acc_.GetValueIn(rhs, i)) {
78 return false;
79 }
80 }
81 if (acc_.HasFrameState(lhs)) {
82 ASSERT(acc_.HasFrameState(rhs));
83 if (acc_.GetFrameState(lhs) != acc_.GetFrameState(rhs)) {
84 return false;
85 }
86 }
87 auto opcode = acc_.GetOpCode(lhs);
88 if (opcode == OpCode::CONVERT) {
89 if (acc_.GetSrcType(lhs) != acc_.GetSrcType(rhs)) {
90 return false;
91 }
92 if (acc_.GetDstType(lhs) != acc_.GetDstType(rhs)) {
93 return false;
94 }
95 }
96 return true;
97 }
98 } // namespace panda::ecmascript::kungfu