• 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 #include "ecmascript/compiler/aot_compiler_preprocessor.h"
16 
17 #include "ecmascript/compiler/bytecode_circuit_builder.h"
18 #include "ecmascript/compiler/pgo_type/pgo_type_parser.h"
19 #include "ecmascript/compiler/pass_manager.h"
20 #include "ecmascript/jspandafile/program_object.h"
21 #include "ecmascript/js_runtime_options.h"
22 #include "ecmascript/module/js_shared_module_manager.h"
23 #include "ecmascript/module/js_module_manager.h"
24 #include "ecmascript/ohos/ohos_pgo_processor.h"
25 #include "ecmascript/ohos/ohos_pkg_args.h"
26 
27 namespace panda::ecmascript::kungfu {
28 
29 using PGOProfilerManager = pgo::PGOProfilerManager;
30 
CompilationOptions(JSRuntimeOptions & runtimeOptions)31 CompilationOptions::CompilationOptions(JSRuntimeOptions &runtimeOptions)
32 {
33     triple_ = runtimeOptions.GetTargetTriple();
34     if (runtimeOptions.GetAOTOutputFile().empty()) {
35         runtimeOptions.SetAOTOutputFile("aot_file");
36     }
37     outputFileName_ = runtimeOptions.GetAOTOutputFile();
38     optLevel_ = runtimeOptions.GetOptLevel();
39     relocMode_ = runtimeOptions.GetRelocMode();
40     logOption_ = runtimeOptions.GetCompilerLogOption();
41     logMethodsList_ = runtimeOptions.GetMethodsListForLog();
42     compilerLogTime_ = runtimeOptions.IsEnableCompilerLogTime();
43     deviceIsScreenOff_ = runtimeOptions.GetDeviceState();
44     deviceThermalLevel_ = runtimeOptions.GetThermalLevel();
45     maxAotMethodSize_ = runtimeOptions.GetMaxAotMethodSize();
46     maxMethodsInModule_ = runtimeOptions.GetCompilerModuleMethods();
47     hotnessThreshold_ = runtimeOptions.GetPGOHotnessThreshold();
48     profilerIn_ = std::string(runtimeOptions.GetPGOProfilerPath());
49     needMerge_ = false;
50     isEnableArrayBoundsCheckElimination_ = runtimeOptions.IsEnableArrayBoundsCheckElimination();
51     isEnableTypeLowering_ = runtimeOptions.IsEnableTypeLowering();
52     isEnableEarlyElimination_ = runtimeOptions.IsEnableEarlyElimination();
53     isEnableEmptyCatchFunction_ = runtimeOptions.IsEnableEmptyCatchFunction();
54     isEnableLaterElimination_ = runtimeOptions.IsEnableLaterElimination();
55     isEnableValueNumbering_ = runtimeOptions.IsEnableValueNumbering();
56     isEnableOptInlining_ = runtimeOptions.IsEnableOptInlining();
57     isEnableOptString_ = runtimeOptions.IsEnableOptString();
58     isEnableOptPGOType_ = runtimeOptions.IsEnableOptPGOType();
59     isEnableOptTrackField_ = runtimeOptions.IsEnableOptTrackField();
60     isEnableOptLoopPeeling_ = runtimeOptions.IsEnableOptLoopPeeling();
61     isEnableOptLoopInvariantCodeMotion_ = runtimeOptions.IsEnableOptLoopInvariantCodeMotion();
62     isEnableOptConstantFolding_ = runtimeOptions.IsEnableOptConstantFolding();
63     isEnableLexenvSpecialization_ = runtimeOptions.IsEnableLexenvSpecialization();
64     isEnableNativeInline_ = runtimeOptions.IsEnableNativeInline();
65     isEnableLoweringBuiltin_ = runtimeOptions.IsEnableLoweringBuiltin();
66     isEnableOptBranchProfiling_ = runtimeOptions.IsEnableBranchProfiling();
67     optBCRange_ = runtimeOptions.GetOptCodeRange();
68     isEnableEscapeAnalysis_ = runtimeOptions.IsEnableEscapeAnalysis();
69     isEnableVerifierPass_ = !runtimeOptions.IsTargetCompilerMode();
70     isEnableInductionVariableAnalysis_ = runtimeOptions.IsEnableInductionVariableAnalysis();
71     isEnableBaselinePgo_ = runtimeOptions.IsEnableBaselinePgo();
72     std::string optionSelectMethods = runtimeOptions.GetCompilerSelectMethods();
73     std::string optionSkipMethods = runtimeOptions.GetCompilerSkipMethods();
74     if (!optionSelectMethods.empty() && !optionSkipMethods.empty()) {
75         LOG_COMPILER(FATAL) <<
76             "--compiler-select-methods and --compiler-skip-methods should not be set at the same time";
77     }
78     if (!optionSelectMethods.empty()) {
79         ParseOption(optionSelectMethods, optionSelectMethods_);
80     }
81     if (!optionSkipMethods.empty()) {
82         ParseOption(optionSkipMethods, optionSkipMethods_);
83     }
84 }
85 
SplitString(const std::string & str,const char ch) const86 std::vector<std::string> CompilationOptions::SplitString(const std::string &str, const char ch) const
87 {
88     std::vector<std::string> vec {};
89     std::istringstream sstr(str.c_str());
90     std::string split;
91     while (getline(sstr, split, ch)) {
92         vec.emplace_back(split);
93     }
94     return vec;
95 }
96 
ParseOption(const std::string & option,std::map<std::string,std::vector<std::string>> & optionMap) const97 void CompilationOptions::ParseOption(const std::string &option,
98     std::map<std::string, std::vector<std::string>> &optionMap) const
99 {
100     const char colon = ':';
101     const char comma = ',';
102     std::string str = option;
103     size_t posColon = 0;
104     size_t posComma = 0;
105     do {
106         posColon = str.find_last_of(colon);
107         std::string methodNameList = str.substr(posColon + 1, str.size());
108         std::vector<std::string> methodNameVec = SplitString(methodNameList, comma);
109         str = str.substr(0, posColon);
110         posComma = str.find_last_of(comma);
111         std::string recordName = str.substr(posComma + 1, str.size());
112         str = str.substr(0, posComma);
113         optionMap[recordName] = methodNameVec;
114     } while (posComma != std::string::npos);
115 }
116 
HandleTargetCompilerMode(CompilationOptions & cOptions)117 bool AotCompilerPreprocessor::HandleTargetCompilerMode(CompilationOptions &cOptions)
118 {
119     if (runtimeOptions_.IsTargetCompilerMode()) {
120         if (!OhosPkgArgs::ParseArgs(*this, cOptions)) {
121             LOG_COMPILER(ERROR) << GetHelper();
122             LOG_COMPILER(ERROR) << "Parse pkg info failed, exit.";
123             return false;
124         }
125         const auto& mainPkgArgs = GetMainPkgArgs();
126         if (!mainPkgArgs) {
127             LOG_COMPILER(ERROR) << "No main pkg args found, exit";
128             return false;
129         }
130         if (!OhosPgoProcessor::MergeAndRemoveRuntimeAp(cOptions, mainPkgArgs)) {
131             LOG_COMPILER(ERROR) << "Fusion runtime ap failed, exit";
132             return false;
133         }
134         HandleTargetModeInfo(cOptions);
135     }
136     return true;
137 }
138 
HandleTargetModeInfo(CompilationOptions & cOptions)139 void AotCompilerPreprocessor::HandleTargetModeInfo(CompilationOptions &cOptions)
140 {
141     JSRuntimeOptions &vmOpt = vm_->GetJSOptions();
142     ASSERT(vmOpt.IsTargetCompilerMode());
143     // target need fast compiler mode
144     vmOpt.SetFastAOTCompileMode(true);
145     vmOpt.SetOptLevel(DEFAULT_OPT_LEVEL);
146     cOptions.optLevel_ = DEFAULT_OPT_LEVEL;
147     cOptions.isEnableOptTrackField_ = false;
148     cOptions.isEnableLoweringBuiltin_ = false;
149 }
150 
MethodHasTryCatch(const JSPandaFile * jsPandaFile,const MethodLiteral * methodLiteral) const151 bool AotCompilerPreprocessor::MethodHasTryCatch(const JSPandaFile *jsPandaFile,
152                                                 const MethodLiteral *methodLiteral) const
153 {
154     if (jsPandaFile == nullptr || methodLiteral == nullptr) {
155         return false;
156     }
157     auto pf = jsPandaFile->GetPandaFile();
158     panda_file::MethodDataAccessor mda(*pf, methodLiteral->GetMethodId());
159     panda_file::CodeDataAccessor cda(*pf, mda.GetCodeId().value());
160     return cda.GetTriesSize() != 0;
161 }
162 
HandlePandaFileNames(const int argc,const char ** argv)163 bool AotCompilerPreprocessor::HandlePandaFileNames(const int argc, const char **argv)
164 {
165     if (runtimeOptions_.GetCompilerPkgJsonInfo().empty() || pkgsArgs_.empty()) {
166         // if no pkgArgs, last param must be abc file
167         std::string files = argv[argc - 1];
168         if (!base::StringHelper::EndsWith(files, ".abc")) {
169             LOG_COMPILER(ERROR) << "The last argument must be abc file" << std::endl;
170             LOG_COMPILER(ERROR) << GetHelper();
171             return false;
172         }
173         std::string delimiter = GetFileDelimiter();
174         pandaFileNames_ = base::StringHelper::SplitString(files, delimiter);
175     }
176     return true;
177 }
178 
AOTInitialize()179 void AotCompilerPreprocessor::AOTInitialize()
180 {
181     BytecodeStubCSigns::Initialize();
182     CommonStubCSigns::Initialize();
183     BuiltinsStubCSigns::Initialize();
184     RuntimeStubCSigns::Initialize();
185 }
186 
Process(CompilationOptions & cOptions)187 void AotCompilerPreprocessor::Process(CompilationOptions &cOptions)
188 {
189     GenerateBytecodeInfoCollectors(cOptions);
190     GeneratePGOTypes();
191     SnapshotInitialize();
192     DoPreAnalysis(cOptions);
193     GenerateMethodMap(cOptions);
194 }
195 
DoPreAnalysis(CompilationOptions & cOptions)196 void AotCompilerPreprocessor::DoPreAnalysis(CompilationOptions &cOptions)
197 {
198     for (uint32_t i = 0 ; i < fileInfos_.size(); ++i) {
199         JSPandaFile *jsPandaFile = fileInfos_[i].jsPandaFile_.get();
200         auto &collector = *bcInfoCollectors_[i];
201         AnalyzeGraphs(jsPandaFile, collector, cOptions);
202     }
203 }
204 
AnalyzeGraphs(JSPandaFile * jsPandaFile,BytecodeInfoCollector & collector,CompilationOptions & cOptions)205 void AotCompilerPreprocessor::AnalyzeGraphs(JSPandaFile *jsPandaFile,
206                                             BytecodeInfoCollector &collector, CompilationOptions &cOptions)
207 {
208     if (jsPandaFile == nullptr) {
209         return;
210     }
211     auto &bytecodeInfo = collector.GetBytecodeInfo();
212     auto &methodPcInfos = bytecodeInfo.GetMethodPcInfos();
213     for (auto &[methodId, methodInfo] : bytecodeInfo.GetMethodList()) {
214         auto methodLiteral = jsPandaFile->FindMethodLiteral(methodId);
215         auto &methodPcInfo = methodPcInfos[methodInfo.GetMethodPcInfoIndex()];
216         if (MethodHasTryCatch(jsPandaFile, methodLiteral)) {
217             AnalyzeGraph(bytecodeInfo, cOptions, collector, methodLiteral, methodPcInfo);
218         }
219     }
220 }
221 
AnalyzeGraph(BCInfo & bytecodeInfo,CompilationOptions & cOptions,BytecodeInfoCollector & collector,MethodLiteral * methodLiteral,MethodPcInfo & methodPCInfo)222 void AotCompilerPreprocessor::AnalyzeGraph(BCInfo &bytecodeInfo, CompilationOptions &cOptions,
223                                            BytecodeInfoCollector &collector, MethodLiteral *methodLiteral,
224                                            MethodPcInfo &methodPCInfo)
225 {
226     if (methodLiteral == nullptr) {
227         return;
228     }
229     AotMethodLogList logList(cOptions.logMethodsList_);
230     AOTCompilationEnv aotCompilationEnv(vm_);
231     CompilerLog log(cOptions.logOption_);
232     AOTFileGenerator generator(&log, &logList, &aotCompilationEnv, cOptions.triple_, false);
233     PGOProfilerDecoder profilerDecoder;
234     LOptions lOptions = LOptions(DEFAULT_OPT_LEVEL, FPFlag::RESERVE_FP, DEFAULT_REL_MODE);
235     Module *m = generator.AddModule("", cOptions.triple_, lOptions, false);
236     PassContext ctx(cOptions.triple_, &log, &collector, m->GetModule(), &profilerDecoder);
237     auto jsPandaFile = ctx.GetJSPandaFile();
238     auto cmpCfg = ctx.GetCompilerConfig();
239     auto module = m->GetModule();
240     std::string fullName = module->GetFuncName(methodLiteral, jsPandaFile);
241 
242     Circuit circuit(vm_->GetNativeAreaAllocator(), ctx.GetAOTModule()->GetDebugInfo(),
243                     fullName.c_str(), cmpCfg->Is64Bit(), FrameType::OPTIMIZED_JS_FUNCTION_FRAME);
244 
245     PGOProfilerDecoder defDecoder;
246     PGOProfilerDecoder *decoder = cOptions.isEnableOptPGOType_ ? &profilerDecoder : &defDecoder;
247 
248     BytecodeCircuitBuilder builder(jsPandaFile, methodLiteral, methodPCInfo, &circuit,
249                                     ctx.GetByteCodes(), false,
250                                     cOptions.isEnableTypeLowering_,
251                                     fullName, bytecodeInfo.GetRecordNameWithIndex(0), decoder, false);
252     {
253         builder.SetPreAnalysis();
254         builder.BytecodeToCircuit();
255         if (builder.HasEmptyCatchBB()) {
256             emptyCatchBBMethods_.push_back(fullName);
257         }
258         if (builder.HasIrreducibleLoop()) {
259             irreducibleMethods_.push_back(fullName);
260         }
261     }
262     m->DestroyModule();
263 }
264 
GenerateAbcFileInfos()265 uint32_t AotCompilerPreprocessor::GenerateAbcFileInfos()
266 {
267     size_t size = pandaFileNames_.size();
268     uint32_t checksum = 0;
269     for (size_t i = 0; i < size; ++i) {
270         const auto &fileName = pandaFileNames_.at(i);
271         auto extendedFilePath = panda::os::file::File::GetExtendedFilePath(fileName);
272         std::shared_ptr<JSPandaFile> jsPandaFile = CreateAndVerifyJSPandaFile(extendedFilePath);
273         AbcFileInfo fileInfo(extendedFilePath, jsPandaFile);
274         if (jsPandaFile == nullptr) {
275             LOG_COMPILER(ERROR) << "Cannot execute panda file '" << extendedFilePath << "'";
276             continue;
277         }
278 
279         if (runtimeOptions_.IsTargetCompilerMode()) {
280             if (fileName.compare(mainPkgName_) == 0) {
281                 checksum = jsPandaFile->GetChecksum();
282             }
283         } else {
284             checksum = jsPandaFile->GetChecksum();
285         }
286 
287         ResolveModule(jsPandaFile.get(), extendedFilePath);
288         fileInfos_.emplace_back(fileInfo);
289     }
290     return checksum;
291 }
292 
GenerateBytecodeInfoCollectors(const CompilationOptions & cOptions)293 void AotCompilerPreprocessor::GenerateBytecodeInfoCollectors(const CompilationOptions &cOptions)
294 {
295     bcInfoCollectors_.reserve(fileInfos_.size());
296     for (const AbcFileInfo &fileInfo : fileInfos_) {
297         JSPandaFile *jsPandaFile = fileInfo.jsPandaFile_.get();
298         auto collectorPtr = std::make_unique<BytecodeInfoCollector>(&aotCompilationEnv_, jsPandaFile,
299                                                                     profilerDecoder_, cOptions.maxAotMethodSize_);
300         bcInfoCollectors_.emplace_back(std::move(collectorPtr));
301     }
302 }
303 
HandleMergedPgoFile(uint32_t checksum)304 bool AotCompilerPreprocessor::HandleMergedPgoFile(uint32_t checksum)
305 {
306     return PGOProfilerManager::MergeApFiles(checksum, profilerDecoder_);
307 }
308 
CreateAndVerifyJSPandaFile(const std::string & fileName)309 std::shared_ptr<JSPandaFile> AotCompilerPreprocessor::CreateAndVerifyJSPandaFile(const std::string &fileName)
310 {
311     JSPandaFileManager *jsPandaFileManager = JSPandaFileManager::GetInstance();
312     std::shared_ptr<JSPandaFile> jsPandaFile = nullptr;
313     if (runtimeOptions_.IsTargetCompilerMode()) {
314         auto pkgArgsIter = pkgsArgs_.find(fileName);
315         if (pkgArgsIter == pkgsArgs_.end()) {
316             jsPandaFile = jsPandaFileManager->OpenJSPandaFile(fileName.c_str());
317         } else if (!(pkgArgsIter->second->GetJSPandaFile(runtimeOptions_, jsPandaFile,
318             pkgArgsIter->second->GetPkgFd()))) {
319             return nullptr;
320         }
321     } else {
322         jsPandaFile = jsPandaFileManager->OpenJSPandaFile(fileName.c_str());
323     }
324     if (jsPandaFile == nullptr) {
325         LOG_ECMA(ERROR) << "open file " << fileName << " error";
326         return nullptr;
327     }
328 
329     if (!jsPandaFile->IsNewVersion()) {
330         LOG_COMPILER(ERROR) << "AOT only support panda file with new ISA, while the '" <<
331             fileName << "' file is the old version";
332         return nullptr;
333     }
334 
335     jsPandaFileManager->AddJSPandaFile(jsPandaFile);
336     return jsPandaFile;
337 }
338 
ResolveModule(const JSPandaFile * jsPandaFile,const std::string & fileName)339 void AotCompilerPreprocessor::ResolveModule(const JSPandaFile *jsPandaFile, const std::string &fileName)
340 {
341     const auto &recordInfo = jsPandaFile->GetJSRecordInfo();
342     JSThread *thread = vm_->GetJSThread();
343     SharedModuleManager *sharedModuleManager = SharedModuleManager::GetInstance();
344     [[maybe_unused]] EcmaHandleScope scope(thread);
345     for (auto info: recordInfo) {
346         if (jsPandaFile->IsModule(info.second)) {
347             auto recordName = info.first;
348             sharedModuleManager->ResolveImportedModuleWithMerge(thread, fileName.c_str(), recordName, false);
349             SharedModuleManager::GetInstance()->TransferSModule(thread);
350         }
351     }
352 }
353 
GeneratePGOTypes()354 void AotCompilerPreprocessor::GeneratePGOTypes()
355 {
356     PGOTypeManager *ptManager = vm_->GetJSThread()->GetCurrentEcmaContext()->GetPTManager();
357     for (uint32_t i = 0; i < fileInfos_.size(); ++i) {
358         auto& collector = *bcInfoCollectors_[i];
359         PGOTypeParser parser(profilerDecoder_, ptManager);
360         parser.CreatePGOType(collector);
361     }
362 }
363 
SnapshotInitialize()364 void AotCompilerPreprocessor::SnapshotInitialize()
365 {
366     PGOTypeManager *ptManager = vm_->GetJSThread()->GetCurrentEcmaContext()->GetPTManager();
367     ptManager->InitAOTSnapshot(fileInfos_.size());
368 }
369 
SetIsFastCall(CString fileDesc,uint32_t methodOffset,bool isFastCall)370 void CallMethodFlagMap::SetIsFastCall(CString fileDesc, uint32_t methodOffset, bool isFastCall)
371 {
372     abcIdMethodIdToIsFastCall_[std::pair<CString, uint32_t>(fileDesc, methodOffset)] = isFastCall;
373 }
374 
IsFastCall(CString fileDesc,uint32_t methodOffset) const375 bool CallMethodFlagMap::IsFastCall(CString fileDesc, uint32_t methodOffset) const
376 {
377     if (!abcIdMethodIdToIsFastCall_.count(std::pair<CString, uint32_t>(fileDesc, methodOffset))) {
378         return false;
379     }
380     return abcIdMethodIdToIsFastCall_.at(std::pair<CString, uint32_t>(fileDesc, methodOffset));
381 }
382 
SetIsAotCompile(CString fileDesc,uint32_t methodOffset,bool isAotCompile)383 void CallMethodFlagMap::SetIsAotCompile(CString fileDesc, uint32_t methodOffset, bool isAotCompile)
384 {
385     abcIdMethodIdToIsAotCompile_[std::pair<CString, uint32_t>(fileDesc, methodOffset)] = isAotCompile;
386 }
387 
IsAotCompile(CString fileDesc,uint32_t methodOffset) const388 bool CallMethodFlagMap::IsAotCompile(CString fileDesc, uint32_t methodOffset) const
389 {
390     if (!abcIdMethodIdToIsAotCompile_.count(std::pair<CString, uint32_t>(fileDesc, methodOffset))) {
391         return false;
392     }
393     return abcIdMethodIdToIsAotCompile_.at(std::pair<CString, uint32_t>(fileDesc, methodOffset));
394 }
395 
SetIsJitCompile(CString fileDesc,uint32_t methodOffset,bool isAotCompile)396 void CallMethodFlagMap::SetIsJitCompile(CString fileDesc, uint32_t methodOffset, bool isAotCompile)
397 {
398     abcIdMethodIdToIsJitCompile_[std::pair<CString, uint32_t>(fileDesc, methodOffset)] = isAotCompile;
399 }
400 
IsJitCompile(CString fileDesc,uint32_t methodOffset) const401 bool CallMethodFlagMap::IsJitCompile(CString fileDesc, uint32_t methodOffset) const
402 {
403     if (!abcIdMethodIdToIsJitCompile_.count(std::pair<CString, uint32_t>(fileDesc, methodOffset))) {
404         return false;
405     }
406     return abcIdMethodIdToIsJitCompile_.at(std::pair<CString, uint32_t>(fileDesc, methodOffset));
407 }
408 
409 
FilterOption(const std::map<std::string,std::vector<std::string>> & optionMap,const std::string & recordName,const std::string & methodName) const410 bool AotCompilerPreprocessor::FilterOption(const std::map<std::string, std::vector<std::string>> &optionMap,
411     const std::string &recordName, const std::string &methodName) const
412 {
413     if (optionMap.empty()) {
414         return false;
415     }
416 
417     auto it = optionMap.find(recordName);
418     if (it == optionMap.end()) {
419         return false;
420     }
421 
422     std::vector<std::string> vec = it->second;
423     return find(vec.begin(), vec.end(), methodName) != vec.end();
424 }
425 
IsSkipMethod(const JSPandaFile * jsPandaFile,const BCInfo & bytecodeInfo,const CString & recordName,const MethodLiteral * methodLiteral,const MethodPcInfo & methodPCInfo,const std::string & methodName,CompilationOptions & cOptions) const426 bool AotCompilerPreprocessor::IsSkipMethod(const JSPandaFile *jsPandaFile, const BCInfo &bytecodeInfo,
427                                            const CString &recordName, const MethodLiteral *methodLiteral,
428                                            const MethodPcInfo &methodPCInfo, const std::string &methodName,
429                                            CompilationOptions &cOptions) const
430 {
431     if (methodPCInfo.methodsSize > bytecodeInfo.GetMaxMethodSize() ||
432         !profilerDecoder_.Match(jsPandaFile, recordName, methodLiteral->GetMethodId())) {
433         return true;
434     }
435 
436     if (OutCompiledMethodsRange()) {
437         return true;
438     }
439 
440     if (!cOptions.optionSelectMethods_.empty()) {
441         return !FilterOption(cOptions.optionSelectMethods_, ConvertToStdString(recordName), methodName);
442     } else if (!cOptions.optionSkipMethods_.empty()) {
443         return FilterOption(cOptions.optionSkipMethods_, ConvertToStdString(recordName), methodName);
444     }
445 
446     std::string fullName = IRModule::GetFuncName(methodLiteral, jsPandaFile);
447     if (HasSkipMethod(irreducibleMethods_, fullName)) {
448         return true;
449     }
450 
451     if (HasSkipMethod(emptyCatchBBMethods_, fullName) && !cOptions.isEnableEmptyCatchFunction_) {
452         return true;
453     }
454 
455     return false;
456 }
457 
GenerateMethodMap(CompilationOptions & cOptions)458 void AotCompilerPreprocessor::GenerateMethodMap(CompilationOptions &cOptions)
459 {
460     for (uint32_t i = 0; i < fileInfos_.size(); ++i) {
461         JSPandaFile *jsPandaFile = fileInfos_[i].jsPandaFile_.get();
462         auto& collector = *bcInfoCollectors_[i];
463         BCInfo &bytecodeInfo = collector.GetBytecodeInfo();
464         const auto &methodPcInfos = bytecodeInfo.GetMethodPcInfos();
465         auto &methodList = bytecodeInfo.GetMethodList();
466         for (auto it = methodList.begin(); it != methodList.end(); it++) {
467             uint32_t index = it->first;
468             auto &methodInfo = it->second;
469             auto &methodPcInfo = methodPcInfos[methodInfo.GetMethodPcInfoIndex()];
470             auto methodLiteral = jsPandaFile->FindMethodLiteral(index);
471             if (methodLiteral == nullptr) {
472                 continue;
473             }
474             auto methodId = methodLiteral->GetMethodId();
475             const std::string methodName(MethodLiteral::GetMethodName(jsPandaFile, methodId));
476             bool isAotcompile = !IsSkipMethod(jsPandaFile, bytecodeInfo, MethodLiteral::GetRecordName(
477                 jsPandaFile, EntityId(index)), methodLiteral, methodPcInfo, methodName, cOptions);
478             bool isFastCall = methodLiteral->IsFastCall();
479             CString fileDesc = jsPandaFile->GetNormalizedFileDesc();
480             uint32_t offset = methodId.GetOffset();
481             callMethodFlagMap_.SetIsAotCompile(fileDesc, offset, isAotcompile);
482             callMethodFlagMap_.SetIsFastCall(fileDesc, offset, isFastCall);
483             LOG_COMPILER(INFO) <<"!!!"<< fileDesc <<" "<< offset << " " << isAotcompile << " " << isFastCall;
484         }
485     }
486 }
487 
HasSkipMethod(const CVector<std::string> & methodList,const std::string & methodName) const488 bool AotCompilerPreprocessor::HasSkipMethod(const CVector<std::string> &methodList,
489                                             const std::string &methodName) const
490 {
491     return std::find(methodList.begin(), methodList.end(), methodName) != methodList.end();
492 }
493 
GetMainPkgArgsAppSignature() const494 std::string AotCompilerPreprocessor::GetMainPkgArgsAppSignature() const
495 {
496     return GetMainPkgArgs() == nullptr ? "" : GetMainPkgArgs()->GetAppSignature();
497 }
498 
ForbidenRebuildAOT(std::string & fileName) const499 bool AotCompilerPreprocessor::ForbidenRebuildAOT(std::string &fileName) const
500 {
501     std::string realPath;
502     if (!RealPath(fileName, realPath, false)) {
503         LOG_COMPILER(ERROR) << "Fail to get realPath: " << fileName;
504         return true;
505     }
506     if (FileExist(realPath.c_str())) {
507         LOG_COMPILER(ERROR) << "AOT file: " << realPath << " exist";
508         return true;
509     }
510     return false;
511 }
512 
HasPreloadAotFile() const513 bool AotCompilerPreprocessor::HasPreloadAotFile() const
514 {
515     std::string hapPath;
516     std::string moduleName;
517     if (GetMainPkgArgs()) {
518         hapPath = GetMainPkgArgs()->GetPath();
519         moduleName = GetMainPkgArgs()->GetModuleName();
520     }
521     std::string fileName = OhosPreloadAppInfo::GetPreloadAOTFileName(hapPath, moduleName);
522     std::string aiFileName = fileName + AOTFileManager::FILE_EXTENSION_AI;
523     std::string anFileName = fileName + AOTFileManager::FILE_EXTENSION_AN;
524     bool existsAnFile = ForbidenRebuildAOT(anFileName);
525     bool existsAiFile = ForbidenRebuildAOT(aiFileName);
526     return (existsAnFile || existsAiFile);
527 }
528 
HasExistsAOTFiles(CompilationOptions & cOptions) const529 bool AotCompilerPreprocessor::HasExistsAOTFiles(CompilationOptions &cOptions) const
530 {
531     std::string anFileName = cOptions.outputFileName_ + AOTFileManager::FILE_EXTENSION_AN;
532     std::string aiFileName = cOptions.outputFileName_ + AOTFileManager::FILE_EXTENSION_AI;
533     bool existsAnFile = ForbidenRebuildAOT(anFileName);
534     bool existsAiFile = ForbidenRebuildAOT(aiFileName);
535     return (existsAnFile || existsAiFile);
536 }
537 } // namespace panda::ecmascript::kungfu
538