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