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#ifndef ECMASCRIPT_COMPILER_TS_HCR_OPT_PASS_H
17#define ECMASCRIPT_COMPILER_TS_HCR_OPT_PASS_H
18
19#include "ecmascript/compiler/combined_pass_visitor.h"
20#include "ecmascript/compiler/pass_manager.h"
21#include "ecmascript/compiler/pgo_type/pgo_type_manager.h"
22#include "ecmascript/ecma_string-inl.h"
23
24namespace panda::ecmascript::kungfu {
25class TSHCROptPass : public PassVisitor {
26public:
27    TSHCROptPass(Circuit* circuit,
28                 RPOVisitor *visitor,
29                 Chunk* chunk,
30                 PassContext *ctx,
31                 bool enableLog,
32                 const std::string &name)
33        : PassVisitor(circuit, chunk, visitor),
34          builder_(circuit, ctx->GetCompilerConfig()),
35          compilationEnv_(ctx->GetCompilationEnv()),
36          enableLog_(enableLog),
37          methodName_(name)
38    {
39        if (ctx->GetCompilerConfig() != nullptr) {
40            typedOpProfiling_ = ctx->GetCompilerConfig()->IsTypedOpProfiling();
41        }
42    }
43
44    ~TSHCROptPass() = default;
45
46    GateRef VisitGate(GateRef gate) override;
47
48private:
49    void AddProfiling(GateRef gate);
50
51    bool IsTypedOpProfiling() const
52    {
53        return typedOpProfiling_;
54    }
55
56    bool IsLogEnabled() const
57    {
58        return enableLog_;
59    }
60
61    const std::string& GetMethodName() const
62    {
63        return methodName_;
64    }
65
66    JSTaggedValue GetStringFromConstantPool(uint32_t methodOffset, uint32_t cpIdx, bool allowAlloc = true) const
67    {
68        return compilationEnv_->GetStringFromConstantPool(methodOffset, cpIdx, allowAlloc);
69    }
70
71    GateRef VisitTypedBinaryOp(GateRef gate);
72
73    GateRef VisitStringBinOp(GateRef gate);
74    GateRef VisitStringEqual(GateRef gate);
75    bool IsSingleCharString(GateRef gate);
76    bool IsNotLoadStrOrStringLoadElement(GateRef gate);
77    GateRef ConvertStringEqualToConst(GateRef left, GateRef right);
78    GateRef ConvertConstSingleCharToInt32(GateRef gate);
79    GateRef ConvertToSingleCharComparison(GateRef left, GateRef right);
80
81    CircuitBuilder builder_;
82    const CompilationEnv *compilationEnv_ {nullptr};
83    bool enableLog_ {false};
84    std::string methodName_;
85    bool typedOpProfiling_ {false};
86};
87}  // panda::ecmascript::kungfu
88#endif  // ECMASCRIPT_COMPILER_TS_HCR_OPT_PASS_H
89