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
16 #include "ecmascript/compiler/base/depend_chain_helper.h"
17
18 namespace panda::ecmascript::kungfu {
19
Merge(DependChains * that)20 void DependChains::Merge(DependChains* that)
21 {
22 // find common sub list
23 while (size_ > that->size_) {
24 head_ = head_->next;
25 size_--;
26 }
27
28 auto lhs = this->head_;
29 auto rhs = that->head_;
30 size_t rhsSize = that->size_;
31 while (rhsSize > size_) {
32 rhs = rhs->next;
33 rhsSize--;
34 }
35 while (lhs != rhs) {
36 ASSERT(lhs != nullptr);
37 lhs = lhs->next;
38 rhs = rhs->next;
39 size_--;
40 }
41 head_ = lhs;
42 }
43
Equals(DependChains * that)44 bool DependChains::Equals(DependChains* that)
45 {
46 if (that == nullptr) {
47 return false;
48 }
49 if (size_ != that->size_) {
50 return false;
51 }
52 auto lhs = this->head_;
53 auto rhs = that->head_;
54 while (lhs != rhs) {
55 if (lhs->gate != rhs->gate) {
56 return false;
57 }
58 lhs = lhs->next;
59 rhs = rhs->next;
60 }
61 return true;
62 }
63
FoundIndexCheckedForLength(RangeGuard * rangeGuard,GateRef input)64 uint32_t DependChains::FoundIndexCheckedForLength(RangeGuard* rangeGuard, GateRef input)
65 {
66 for (Node* node = head_; node != nullptr; node = node->next) {
67 uint32_t length = rangeGuard->CheckIndexCheckLengthInput(node->gate, input);
68 if (length > 0) { // found !!!
69 return length;
70 }
71 }
72 return 0;
73 }
74
FoundIndexCheckedForIndex(RangeGuard * rangeGuard,GateRef input)75 uint32_t DependChains::FoundIndexCheckedForIndex(RangeGuard* rangeGuard, GateRef input)
76 {
77 for (Node* node = head_; node != nullptr; node = node->next) {
78 uint32_t length = rangeGuard->CheckIndexCheckIndexInput(node->gate, input);
79 if (length > 0) { // found !!!
80 return length;
81 }
82 }
83 return 0;
84 }
85
LookupNode(LaterElimination * elimination,GateRef gate)86 GateRef DependChains::LookupNode(LaterElimination* elimination, GateRef gate)
87 {
88 for (Node* node = head_; node != nullptr; node = node->next) {
89 if (elimination->CheckReplacement(node->gate, gate)) {
90 return node->gate;
91 }
92 }
93 return Circuit::NullGate();
94 }
95
UpdateNode(GateRef gate)96 DependChains* DependChains::UpdateNode(GateRef gate)
97 {
98 // assign node->next to head
99 Node* node = chunk_->New<Node>(gate, head_);
100 DependChains* that = new (chunk_) DependChains(chunk_);
101 // assign head to node
102 that->head_ = node;
103 that->size_ = size_ + 1;
104 return that;
105 }
106 } // namespace panda::ecmascript::kungfu