• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/array_bounds_check_elimination.h"
17 #include "ecmascript/compiler/number_speculative_runner.h"
18 #include "ecmascript/compiler/range_analysis.h"
19 
20 namespace panda::ecmascript::kungfu {
Run()21 void NumberSpeculativeRunner::Run()
22 {
23     CombinedPassVisitor rangeGuardVisitor(circuit_, enableLog_, methodName_, chunk_);
24     RangeGuard rangeGuard(circuit_, &rangeGuardVisitor, chunk_);
25     rangeGuardVisitor.AddPass(&rangeGuard);
26     rangeGuardVisitor.VisitGraph();
27 
28     if (IsLogEnabled()) {
29         LOG_COMPILER(INFO) << "";
30         LOG_COMPILER(INFO) << "\033[34m"
31                            << "===================="
32                            << " After range guard "
33                            << "[" << GetMethodName() << "]"
34                            << "===================="
35                            << "\033[0m";
36         circuit_->PrintAllGatesWithBytecode();
37         LOG_COMPILER(INFO) << "\033[34m" << "========================= End ==========================" << "\033[0m";
38     }
39 
40     auto maxId = circuit_->GetMaxGateId();
41     typeInfos_.resize(maxId + 1, TypeInfo::NONE);
42 
43     if (enableArrayBoundsCheckElimination_) {
44         ArrayBoundsCheckElimination arrayBoundsCheck(circuit_, enableLog_, methodName_, chunk_);
45         arrayBoundsCheck.Run();
46         if (IsLogEnabled()) {
47             LOG_COMPILER(INFO) << "";
48             LOG_COMPILER(INFO) << "\033[34m"
49                             << "===================="
50                             << " After array bounds check elimination "
51                             << "[" << GetMethodName() << "]"
52                             << "===================="
53                             << "\033[0m";
54             circuit_->PrintAllGatesWithBytecode();
55             LOG_COMPILER(INFO) << "\033[34m" << "========================= End ==========================" << "\033[0m";
56         }
57     }
58 
59     maxId = circuit_->GetMaxGateId();
60     typeInfos_.resize(maxId + 1, TypeInfo::NONE);
61 
62     // visit gate in RPO, propagate use infos and
63     // reset the machine type of number operator gate and related phi,
64     // if some tagged phi is used as native value, change it to native phi.
65     NumberSpeculativeRetype retype(circuit_, chunk_, typeInfos_);
66     CombinedPassVisitor retypeVisitor(circuit_, enableLog_, methodName_, chunk_);
67     NumberSpeculativeRetypeManager retypePhase(circuit_, &retypeVisitor, chunk_,
68                                                &retype, NumberSpeculativeRetype::State::Retype);
69     retypeVisitor.AddPass(&retypePhase);
70     retypeVisitor.VisitGraph();
71     CombinedPassVisitor convertVisitor(circuit_, enableLog_, methodName_, chunk_);
72     NumberSpeculativeRetypeManager convertPhase(circuit_, &convertVisitor,
73                                                 chunk_, &retype, NumberSpeculativeRetype::State::Convert);
74     convertVisitor.AddPass(&convertPhase);
75     convertVisitor.VisitGraph();
76 
77     if (IsLogEnabled()) {
78         LOG_COMPILER(INFO) << "";
79         LOG_COMPILER(INFO) << "\033[34m"
80                            << "===================="
81                            << " After number speculative retype "
82                            << "[" << GetMethodName() << "]"
83                            << "===================="
84                            << "\033[0m";
85         circuit_->PrintAllGatesWithBytecode();
86         LOG_COMPILER(INFO) << "\033[34m" << "========================= End ==========================" << "\033[0m";
87     }
88 
89     maxId = circuit_->GetMaxGateId();
90     rangeInfos_.resize(maxId + 1, RangeInfo::NONE());
91     typeInfos_.resize(maxId + 1, TypeInfo::NONE);
92     CombinedPassVisitor rangeAnalysisVisitor(circuit_, enableLog_, methodName_, chunk_);
93     RangeAnalysis rangeAnalysis(circuit_, &rangeAnalysisVisitor, chunk_, typeInfos_, rangeInfos_);
94     rangeAnalysisVisitor.AddPass(&rangeAnalysis);
95     rangeAnalysisVisitor.VisitGraph();
96 
97     if (IsLogEnabled()) {
98         LOG_COMPILER(INFO) << "";
99         LOG_COMPILER(INFO) << "\033[34m"
100                            << "===================="
101                            << " After range analysis "
102                            << "[" << GetMethodName() << "]"
103                            << "===================="
104                            << "\033[0m";
105         rangeAnalysis.PrintRangeInfo();
106         LOG_COMPILER(INFO) << "\033[34m" << "========================= End ==========================" << "\033[0m";
107     }
108 
109     NumberSpeculativeLowering lowering(circuit_, cmpCfg_, chunk_, typeInfos_, rangeInfos_);
110     lowering.Run();
111     if (IsLogEnabled()) {
112         LOG_COMPILER(INFO) << "";
113         LOG_COMPILER(INFO) << "\033[34m"
114                            << "===================="
115                            << " After number speculative runner "
116                            << "[" << GetMethodName() << "]"
117                            << "===================="
118                            << "\033[0m";
119         circuit_->PrintAllGatesWithBytecode();
120         LOG_COMPILER(INFO) << "\033[34m" << "========================= End ==========================" << "\033[0m";
121     }
122 }
123 }  // panda::ecmascript::kungfu
124