14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_COMPILER_PASS_MANAGER_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_COMPILER_PASS_MANAGER_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_compiler_preprocessor.h"
204514f5e3Sopenharmony_ci#include "ecmascript/compiler/jit_compilation_env.h"
214514f5e3Sopenharmony_ci#include "ecmascript/compiler/bytecode_info_collector.h"
224514f5e3Sopenharmony_ci#include "ecmascript/compiler/compilation_driver.h"
234514f5e3Sopenharmony_ci#include "ecmascript/compiler/compiler_log.h"
244514f5e3Sopenharmony_ci#include "ecmascript/compiler/file_generators.h"
254514f5e3Sopenharmony_ci#include "ecmascript/compiler/ir_module.h"
264514f5e3Sopenharmony_ci#include "ecmascript/compiler/ir_module.h"
274514f5e3Sopenharmony_ci#include "ecmascript/compiler/pass_options.h"
284514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h"
294514f5e3Sopenharmony_ci#include "ecmascript/jit/jit_profiler.h"
304514f5e3Sopenharmony_ci#include "ecmascript/jspandafile/method_literal.h"
314514f5e3Sopenharmony_ci
324514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_compiler_stats.h"
334514f5e3Sopenharmony_ci
344514f5e3Sopenharmony_cinamespace panda::ecmascript::kungfu {
354514f5e3Sopenharmony_ciclass Bytecodes;
364514f5e3Sopenharmony_ciclass CompilationConfig;
374514f5e3Sopenharmony_ciclass PassData;
384514f5e3Sopenharmony_ciclass CallMethodFlagMap;
394514f5e3Sopenharmony_cistruct AbcFileInfo;
404514f5e3Sopenharmony_ciclass PassContext {
414514f5e3Sopenharmony_cipublic:
424514f5e3Sopenharmony_ci    PassContext(const std::string &triple, CompilerLog *log, BytecodeInfoCollector* collector, IRModule *aotModule,
434514f5e3Sopenharmony_ci        PGOProfilerDecoder *decoder);
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_ci    PGOTypeManager* GetPTManager() const
464514f5e3Sopenharmony_ci    {
474514f5e3Sopenharmony_ci        return compilationEnv_->GetPTManager();
484514f5e3Sopenharmony_ci    }
494514f5e3Sopenharmony_ci
504514f5e3Sopenharmony_ci    Bytecodes* GetByteCodes()
514514f5e3Sopenharmony_ci    {
524514f5e3Sopenharmony_ci        return bytecodes_;
534514f5e3Sopenharmony_ci    }
544514f5e3Sopenharmony_ci
554514f5e3Sopenharmony_ci    CompilationConfig* GetCompilerConfig()
564514f5e3Sopenharmony_ci    {
574514f5e3Sopenharmony_ci        return &cmpCfg_;
584514f5e3Sopenharmony_ci    }
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_ci    CompilerLog* GetCompilerLog() const
614514f5e3Sopenharmony_ci    {
624514f5e3Sopenharmony_ci        return log_;
634514f5e3Sopenharmony_ci    }
644514f5e3Sopenharmony_ci
654514f5e3Sopenharmony_ci    const JSPandaFile *GetJSPandaFile() const
664514f5e3Sopenharmony_ci    {
674514f5e3Sopenharmony_ci        return jsPandaFile_;
684514f5e3Sopenharmony_ci    }
694514f5e3Sopenharmony_ci
704514f5e3Sopenharmony_ci    BytecodeInfoCollector* GetBytecodeInfoCollector() const
714514f5e3Sopenharmony_ci    {
724514f5e3Sopenharmony_ci        return bcInfoCollector_;
734514f5e3Sopenharmony_ci    }
744514f5e3Sopenharmony_ci
754514f5e3Sopenharmony_ci    IRModule* GetAOTModule() const
764514f5e3Sopenharmony_ci    {
774514f5e3Sopenharmony_ci        return aotModule_;
784514f5e3Sopenharmony_ci    }
794514f5e3Sopenharmony_ci
804514f5e3Sopenharmony_ci    bool FilterMethod(const MethodLiteral *methodLiteral, const MethodPcInfo &methodPCInfo) const
814514f5e3Sopenharmony_ci    {
824514f5e3Sopenharmony_ci        return bcInfoCollector_->FilterMethod(methodLiteral, methodPCInfo);
834514f5e3Sopenharmony_ci    }
844514f5e3Sopenharmony_ci
854514f5e3Sopenharmony_ci    bool IsSkippedMethod(uint32_t methodOffset) const
864514f5e3Sopenharmony_ci    {
874514f5e3Sopenharmony_ci        return bcInfoCollector_->IsSkippedMethod(methodOffset);
884514f5e3Sopenharmony_ci    }
894514f5e3Sopenharmony_ci
904514f5e3Sopenharmony_ci    BCInfo& GetBytecodeInfo()
914514f5e3Sopenharmony_ci    {
924514f5e3Sopenharmony_ci        return bcInfoCollector_->GetBytecodeInfo();
934514f5e3Sopenharmony_ci    }
944514f5e3Sopenharmony_ci
954514f5e3Sopenharmony_ci    NativeAreaAllocator *GetNativeAreaAllocator() const
964514f5e3Sopenharmony_ci    {
974514f5e3Sopenharmony_ci        return compilationEnv_->GetNativeAreaAllocator();
984514f5e3Sopenharmony_ci    }
994514f5e3Sopenharmony_ci
1004514f5e3Sopenharmony_ci    CompilationEnv *GetCompilationEnv() const
1014514f5e3Sopenharmony_ci    {
1024514f5e3Sopenharmony_ci        return compilationEnv_;
1034514f5e3Sopenharmony_ci    }
1044514f5e3Sopenharmony_ci
1054514f5e3Sopenharmony_ci    PGOProfilerDecoder *GetPfDecoder() const
1064514f5e3Sopenharmony_ci    {
1074514f5e3Sopenharmony_ci        return decoder_;
1084514f5e3Sopenharmony_ci    }
1094514f5e3Sopenharmony_ci
1104514f5e3Sopenharmony_ciprivate:
1114514f5e3Sopenharmony_ci    CompilationEnv *compilationEnv_ {nullptr};
1124514f5e3Sopenharmony_ci    BytecodeInfoCollector *bcInfoCollector_ {nullptr};
1134514f5e3Sopenharmony_ci    Bytecodes *bytecodes_ {nullptr};
1144514f5e3Sopenharmony_ci    CompilationConfig cmpCfg_;
1154514f5e3Sopenharmony_ci    CompilerLog *log_ {nullptr};
1164514f5e3Sopenharmony_ci    const JSPandaFile *jsPandaFile_ {nullptr};
1174514f5e3Sopenharmony_ci    IRModule *aotModule_ {nullptr};
1184514f5e3Sopenharmony_ci    PGOProfilerDecoder *decoder_ {nullptr};
1194514f5e3Sopenharmony_ci};
1204514f5e3Sopenharmony_ci
1214514f5e3Sopenharmony_ciclass PassManager {
1224514f5e3Sopenharmony_cipublic:
1234514f5e3Sopenharmony_ci    explicit PassManager(CompilationEnv *env, std::string &triple, size_t optLevel, size_t relocMode,
1244514f5e3Sopenharmony_ci        CompilerLog *log, AotMethodLogList *logList, size_t maxAotMethodSize, size_t maxMethodsInModule,
1254514f5e3Sopenharmony_ci        PGOProfilerDecoder &profilerDecoder, PassOptions *passOptions, CallMethodFlagMap *callMethodFlagMap,
1264514f5e3Sopenharmony_ci        const CVector<AbcFileInfo> &fileInfos, const CVector<std::unique_ptr<BytecodeInfoCollector>> &bcInfoCollectors,
1274514f5e3Sopenharmony_ci        std::string optBCRange)
1284514f5e3Sopenharmony_ci        : compilationEnv_(env), triple_(triple), optLevel_(optLevel), relocMode_(relocMode), log_(log),
1294514f5e3Sopenharmony_ci          logList_(logList), maxAotMethodSize_(maxAotMethodSize), maxMethodsInModule_(maxMethodsInModule),
1304514f5e3Sopenharmony_ci          profilerDecoder_(profilerDecoder), passOptions_(passOptions), callMethodFlagMap_(callMethodFlagMap),
1314514f5e3Sopenharmony_ci          fileInfos_(fileInfos), bcInfoCollectors_(bcInfoCollectors), optBCRange_(optBCRange) {
1324514f5e3Sopenharmony_ci                enableJITLog_ = compilationEnv_->GetJSOptions().GetTraceJIT();
1334514f5e3Sopenharmony_ci            };
1344514f5e3Sopenharmony_ci
1354514f5e3Sopenharmony_ci    virtual ~PassManager() = default;
1364514f5e3Sopenharmony_ci
1374514f5e3Sopenharmony_ci    void CompileValidFiles(AOTFileGenerator &generator, bool &ret, AotCompilerStats &compilerStats);
1384514f5e3Sopenharmony_ci
1394514f5e3Sopenharmony_ci    bool Compile(JSPandaFile *jsPandaFile, const std::string &fileName, AOTFileGenerator &generator,
1404514f5e3Sopenharmony_ci                 AotCompilerStats &compilerStats, BytecodeInfoCollector &collector);
1414514f5e3Sopenharmony_ci
1424514f5e3Sopenharmony_ciprotected:
1434514f5e3Sopenharmony_ci    bool IsReleasedPandaFile(const JSPandaFile *jsPandaFile) const;
1444514f5e3Sopenharmony_ci
1454514f5e3Sopenharmony_ci    CompilationEnv *compilationEnv_ {nullptr};
1464514f5e3Sopenharmony_ci    std::string triple_ {};
1474514f5e3Sopenharmony_ci    size_t optLevel_ {3}; // 3 : default backend optimization level
1484514f5e3Sopenharmony_ci    size_t relocMode_ {2}; // 2 : default relocation mode-- PIC
1494514f5e3Sopenharmony_ci    CompilerLog *log_ {nullptr};
1504514f5e3Sopenharmony_ci    AotMethodLogList *logList_ {nullptr};
1514514f5e3Sopenharmony_ci    size_t maxAotMethodSize_ {0};
1524514f5e3Sopenharmony_ci    size_t maxMethodsInModule_ {0};
1534514f5e3Sopenharmony_ci    PGOProfilerDecoder &profilerDecoder_;
1544514f5e3Sopenharmony_ci    PassOptions *passOptions_ {nullptr};
1554514f5e3Sopenharmony_ci    CallMethodFlagMap *callMethodFlagMap_ {nullptr};
1564514f5e3Sopenharmony_ci    const CVector<AbcFileInfo> &fileInfos_;
1574514f5e3Sopenharmony_ci    const CVector<std::unique_ptr<BytecodeInfoCollector>> &bcInfoCollectors_;
1584514f5e3Sopenharmony_ci    std::string optBCRange_ {};
1594514f5e3Sopenharmony_ci    bool enableJITLog_ {false};
1604514f5e3Sopenharmony_ci};
1614514f5e3Sopenharmony_ci
1624514f5e3Sopenharmony_ciclass JitPassManager : public PassManager {
1634514f5e3Sopenharmony_cipublic:
1644514f5e3Sopenharmony_ci    JitPassManager(JitCompilationEnv *env, std::string &triple, size_t optLevel, size_t relocMode,
1654514f5e3Sopenharmony_ci        CompilerLog *log, AotMethodLogList *logList,
1664514f5e3Sopenharmony_ci        PGOProfilerDecoder &profilerDecoder, PassOptions *passOptions)
1674514f5e3Sopenharmony_ci        : PassManager(env, triple, optLevel, relocMode, log, logList, 1, 1, profilerDecoder, passOptions,
1684514f5e3Sopenharmony_ci                      nullptr, CVector<AbcFileInfo> {}, CVector<std::unique_ptr<BytecodeInfoCollector>> {}, "") { };
1694514f5e3Sopenharmony_ci
1704514f5e3Sopenharmony_ci    bool Compile(JSHandle<ProfileTypeInfo> &profileTypeInfo, AOTFileGenerator &gen, int32_t osrOffset = -1);
1714514f5e3Sopenharmony_ci    bool RunCg();
1724514f5e3Sopenharmony_ci    virtual ~JitPassManager();
1734514f5e3Sopenharmony_ci
1744514f5e3Sopenharmony_ciprivate:
1754514f5e3Sopenharmony_ci    BytecodeInfoCollector *collector_ {nullptr};
1764514f5e3Sopenharmony_ci    LOptions *lOptions_ {nullptr};
1774514f5e3Sopenharmony_ci    JitCompilationDriver *cmpDriver_ {nullptr};
1784514f5e3Sopenharmony_ci
1794514f5e3Sopenharmony_ci    PassContext *ctx_ {nullptr};
1804514f5e3Sopenharmony_ci    Circuit *circuit_ {nullptr};
1814514f5e3Sopenharmony_ci    BytecodeCircuitBuilder *builder_ {nullptr};
1824514f5e3Sopenharmony_ci    JITProfiler *jitProfiler_ {nullptr};
1834514f5e3Sopenharmony_ci    PassData *data_ {nullptr};
1844514f5e3Sopenharmony_ci};
1854514f5e3Sopenharmony_ci}
1864514f5e3Sopenharmony_ci#endif // ECMASCRIPT_COMPILER_PASS_MANAGER_H
187