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 "cg_option.h"
17 #include <fstream>
18 #include <unordered_map>
19 #include "cg_options.h"
20 #include "driver_options.h"
21 #include "mpl_logging.h"
22 #include "parser_opt.h"
23 #include "mir_parser.h"
24 #include "string_utils.h"
25 #include "triple.h"
26
27 namespace maplebe {
28 using namespace maple;
29
30 const std::string kMplcgVersion = "";
31
32 bool CGOptions::timePhases = false;
33 std::string CGOptions::targetArch = "";
34 std::unordered_set<std::string> CGOptions::dumpPhases = {};
35 std::unordered_set<std::string> CGOptions::skipPhases = {};
36 std::unordered_map<std::string, std::vector<std::string>> CGOptions::cyclePatternMap = {};
37 std::string CGOptions::skipFrom = "";
38 std::string CGOptions::skipAfter = "";
39 std::string CGOptions::dumpFunc = "*";
40 std::string CGOptions::globalVarProfile = "";
41 std::string CGOptions::profileData = "";
42 std::string CGOptions::profileFuncData = "";
43 std::string CGOptions::profileClassData = "";
44 #ifdef TARGARM32
45 std::string CGOptions::duplicateAsmFile = "";
46 #else
47 std::string CGOptions::duplicateAsmFile = "maple/mrt/codetricks/arch/arm64/duplicateFunc.s";
48 #endif
49 Range CGOptions::range = Range();
50 std::string CGOptions::fastFuncsAsmFile = "";
51 Range CGOptions::spillRanges = Range();
52 uint8 CGOptions::fastAllocMode = 0; /* 0: fast, 1: spill all */
53 bool CGOptions::fastAlloc = false;
54 uint64 CGOptions::lsraBBOptSize = 150000;
55 uint64 CGOptions::lsraInsnOptSize = 200000;
56 uint64 CGOptions::overlapNum = 28;
57 uint8 CGOptions::rematLevel = 2;
58 bool CGOptions::optForSize = false;
59 bool CGOptions::enableHotColdSplit = false;
60 uint32 CGOptions::alignMinBBSize = 16;
61 uint32 CGOptions::alignMaxBBSize = 96;
62 uint32 CGOptions::loopAlignPow = 4;
63 uint32 CGOptions::jumpAlignPow = 5;
64 uint32 CGOptions::funcAlignPow = 5;
65 #if TARGAARCH64 || TARGRISCV64
66 bool CGOptions::useBarriersForVolatile = false;
67 #else
68 bool CGOptions::useBarriersForVolatile = true;
69 #endif
70 bool CGOptions::exclusiveEH = false;
71 bool CGOptions::doEBO = false;
72 bool CGOptions::doCGSSA = false;
73 bool CGOptions::doCGRegCoalesce = false;
74 bool CGOptions::doIPARA = true;
75 bool CGOptions::doCFGO = false;
76 bool CGOptions::doICO = false;
77 bool CGOptions::doStoreLoadOpt = false;
78 bool CGOptions::doGlobalOpt = false;
79 bool CGOptions::doVregRename = false;
80 bool CGOptions::doMultiPassColorRA = true;
81 bool CGOptions::doPrePeephole = false;
82 bool CGOptions::doPeephole = false;
83 bool CGOptions::doRetMerge = false;
84 bool CGOptions::doSchedule = false;
85 bool CGOptions::doWriteRefFieldOpt = false;
86 bool CGOptions::dumpOptimizeCommonLog = false;
87 bool CGOptions::checkArrayStore = false;
88 bool CGOptions::doPIC = false;
89 bool CGOptions::noDupBB = false;
90 bool CGOptions::noCalleeCFI = true;
91 bool CGOptions::emitCyclePattern = false;
92 bool CGOptions::insertYieldPoint = false;
93 bool CGOptions::mapleLinker = false;
94 bool CGOptions::printFunction = false;
95 bool CGOptions::nativeOpt = false;
96 bool CGOptions::lazyBinding = false;
97 bool CGOptions::hotFix = false;
98 bool CGOptions::debugSched = false;
99 bool CGOptions::bruteForceSched = false;
100 bool CGOptions::simulateSched = false;
101 CGOptions::ABIType CGOptions::abiType = kABIHard;
102 CGOptions::EmitFileType CGOptions::emitFileType = kAsm;
103 bool CGOptions::genLongCalls = false;
104 bool CGOptions::functionSections = false;
105 bool CGOptions::useFramePointer = false;
106 bool CGOptions::gcOnly = false;
107 bool CGOptions::quiet = false;
108 bool CGOptions::doPatchLongBranch = false;
109 bool CGOptions::doPreSchedule = false;
110 bool CGOptions::emitBlockMarker = true;
111 bool CGOptions::inRange = false;
112 bool CGOptions::doPreLSRAOpt = false;
113 bool CGOptions::doRegSavesOpt = false;
114 bool CGOptions::useSsaPreSave = false;
115 bool CGOptions::useSsuPreRestore = false;
116 bool CGOptions::replaceASM = false;
117 bool CGOptions::generalRegOnly = false;
118 bool CGOptions::fastMath = false;
119 bool CGOptions::doAlignAnalysis = false;
120 bool CGOptions::doCondBrAlign = false;
121 bool CGOptions::cgBigEndian = false;
122 bool CGOptions::arm64ilp32 = false;
123 bool CGOptions::noCommon = false;
124
GetInstance()125 CGOptions &CGOptions::GetInstance()
126 {
127 static CGOptions instance;
128 return instance;
129 }
130
DecideMplcgRealLevel(bool isDebug)131 void CGOptions::DecideMplcgRealLevel(bool isDebug)
132 {
133 if (opts::cg::o0) {
134 if (isDebug) {
135 LogInfo::MapleLogger() << "Real Mplcg level: O0\n";
136 }
137 EnableO0();
138 }
139
140 if (opts::cg::o1) {
141 if (isDebug) {
142 LogInfo::MapleLogger() << "Real Mplcg level: O1\n";
143 }
144 EnableO1();
145 }
146
147 if (opts::cg::o2 || opts::cg::os) {
148 if (opts::cg::os) {
149 optForSize = true;
150 }
151 if (isDebug) {
152 std::string oLog = (opts::cg::os == true) ? "Os" : "O2";
153 LogInfo::MapleLogger() << "Real Mplcg level: " << oLog << "\n";
154 }
155 EnableO2();
156 }
157 if (opts::cg::olitecg) {
158 if (isDebug) {
159 LogInfo::MapleLogger() << "Real Mplcg level: LiteCG\n";
160 }
161 EnableLiteCG();
162 }
163 }
164
SolveOptions(bool isDebug)165 bool CGOptions::SolveOptions(bool isDebug)
166 {
167 DecideMplcgRealLevel(isDebug);
168
169 for (const auto &opt : cgCategory.GetEnabledOptions()) {
170 std::string printOpt;
171 if (isDebug) {
172 for (const auto &val : opt->GetRawValues()) {
173 printOpt += opt->GetName() + " " + val + " ";
174 }
175 LogInfo::MapleLogger() << "cg options: " << printOpt << '\n';
176 }
177 }
178
179 if (opts::cg::quiet.IsEnabledByUser()) {
180 SetQuiet(true);
181 }
182
183 if (opts::verbose.IsEnabledByUser()) {
184 SetQuiet(false);
185 }
186
187 if (opts::cg::pie.IsEnabledByUser()) {
188 opts::cg::pie ? SetOption(CGOptions::kGenPie) : ClearOption(CGOptions::kGenPie);
189 }
190
191 if (opts::cg::fpic.IsEnabledByUser()) {
192 if (opts::cg::fpic) {
193 EnablePIC();
194 SetOption(CGOptions::kGenPic);
195 } else {
196 DisablePIC();
197 ClearOption(CGOptions::kGenPic);
198 }
199 }
200
201 if (opts::cg::verboseAsm.IsEnabledByUser()) {
202 opts::cg::verboseAsm ? SetOption(CGOptions::kVerboseAsm) : ClearOption(CGOptions::kVerboseAsm);
203 }
204
205 if (opts::cg::verboseCg.IsEnabledByUser()) {
206 opts::cg::verboseCg ? SetOption(CGOptions::kVerboseCG) : ClearOption(CGOptions::kVerboseCG);
207 }
208
209 if (opts::cg::maplelinker.IsEnabledByUser()) {
210 opts::cg::maplelinker ? EnableMapleLinker() : DisableMapleLinker();
211 }
212
213 if (opts::cg::fastAlloc.IsEnabledByUser()) {
214 EnableFastAlloc();
215 SetFastAllocMode(opts::cg::fastAlloc);
216 }
217
218 if (opts::cg::useBarriersForVolatile.IsEnabledByUser()) {
219 opts::cg::useBarriersForVolatile ? EnableBarriersForVolatile() : DisableBarriersForVolatile();
220 }
221
222 if (opts::cg::spillRange.IsEnabledByUser()) {
223 SetRange(opts::cg::spillRange, "--pill-range", GetSpillRanges());
224 }
225
226 if (opts::cg::range.IsEnabledByUser()) {
227 SetRange(opts::cg::range, "--range", GetRange());
228 }
229
230 if (opts::cg::timePhases.IsEnabledByUser()) {
231 opts::cg::timePhases ? EnableTimePhases() : DisableTimePhases();
232 }
233
234 if (opts::cg::dumpFunc.IsEnabledByUser()) {
235 SetDumpFunc(opts::cg::dumpFunc);
236 }
237
238 if (opts::cg::duplicateAsmList.IsEnabledByUser()) {
239 SetDuplicateAsmFile(opts::cg::duplicateAsmList);
240 }
241
242 if (opts::cg::duplicateAsmList2.IsEnabledByUser()) {
243 SetFastFuncsAsmFile(opts::cg::duplicateAsmList2);
244 }
245
246 if (opts::cg::insertCall.IsEnabledByUser()) {
247 SetOption(kGenInsertCall);
248 SetInstrumentationFunction(opts::cg::insertCall);
249 SetInsertCall(true);
250 }
251
252 if (opts::cg::stackProtectorStrong.IsEnabledByUser()) {
253 SetOption(kUseStackProtectorStrong);
254 }
255
256 if (opts::cg::stackProtectorAll.IsEnabledByUser()) {
257 SetOption(kUseStackProtectorAll);
258 }
259
260 if (opts::cg::debug.IsEnabledByUser()) {
261 SetOption(kDebugFriendly);
262 SetOption(kWithLoc);
263 ClearOption(kSuppressFileInfo);
264 }
265
266 if (opts::cg::gdwarf.IsEnabledByUser()) {
267 SetOption(kDebugFriendly);
268 SetOption(kWithLoc);
269 SetOption(kWithDwarf);
270 SetParserOption(kWithDbgInfo);
271 ClearOption(kSuppressFileInfo);
272 }
273
274 if (opts::cg::gsrc.IsEnabledByUser()) {
275 SetOption(kDebugFriendly);
276 SetOption(kWithLoc);
277 SetOption(kWithSrc);
278 ClearOption(kWithMpl);
279 }
280
281 if (opts::cg::gmixedsrc.IsEnabledByUser()) {
282 SetOption(kDebugFriendly);
283 SetOption(kWithLoc);
284 SetOption(kWithSrc);
285 SetOption(kWithMpl);
286 }
287
288 if (opts::cg::gmixedasm.IsEnabledByUser()) {
289 SetOption(kDebugFriendly);
290 SetOption(kWithLoc);
291 SetOption(kWithSrc);
292 SetOption(kWithMpl);
293 SetOption(kWithAsm);
294 }
295
296 if (opts::cg::profile.IsEnabledByUser()) {
297 SetOption(kWithProfileCode);
298 SetParserOption(kWithProfileInfo);
299 }
300
301 if (opts::cg::withRaLinearScan.IsEnabledByUser()) {
302 SetOption(kDoLinearScanRegAlloc);
303 ClearOption(kDoColorRegAlloc);
304 }
305
306 if (opts::cg::withRaGraphColor.IsEnabledByUser()) {
307 SetOption(kDoColorRegAlloc);
308 ClearOption(kDoLinearScanRegAlloc);
309 }
310
311 if (opts::cg::printFunc.IsEnabledByUser()) {
312 opts::cg::printFunc ? EnablePrintFunction() : DisablePrintFunction();
313 }
314
315 if (opts::cg::addDebugTrace.IsEnabledByUser()) {
316 SetOption(kAddDebugTrace);
317 }
318
319 if (opts::cg::addFuncProfile.IsEnabledByUser()) {
320 SetOption(kAddFuncProfile);
321 }
322
323 if (opts::cg::suppressFileinfo.IsEnabledByUser()) {
324 SetOption(kSuppressFileInfo);
325 }
326
327 if (opts::cg::patchLongBranch.IsEnabledByUser()) {
328 SetOption(kPatchLongBranch);
329 }
330
331 if (opts::cg::constFold.IsEnabledByUser()) {
332 opts::cg::constFold ? SetOption(kConstFold) : ClearOption(kConstFold);
333 }
334
335 if (opts::cg::dumpCfg.IsEnabledByUser()) {
336 SetOption(kDumpCFG);
337 }
338
339 if (opts::cg::classListFile.IsEnabledByUser()) {
340 SetClassListFile(opts::cg::classListFile);
341 }
342
343 if (opts::cg::genCMacroDef.IsEnabledByUser()) {
344 SetOrClear(GetGenerateFlags(), CGOptions::kCMacroDef, opts::cg::genCMacroDef);
345 }
346
347 if (opts::cg::genGctibFile.IsEnabledByUser()) {
348 SetOrClear(GetGenerateFlags(), CGOptions::kGctib, opts::cg::genGctibFile);
349 }
350
351 if (opts::cg::yieldpoint.IsEnabledByUser()) {
352 SetOrClear(GetGenerateFlags(), CGOptions::kGenYieldPoint, opts::cg::yieldpoint);
353 }
354
355 if (opts::cg::localRc.IsEnabledByUser()) {
356 SetOrClear(GetGenerateFlags(), CGOptions::kGenLocalRc, opts::cg::localRc);
357 }
358
359 if (opts::cg::ehExclusiveList.IsEnabledByUser()) {
360 SetEHExclusiveFile(opts::cg::ehExclusiveList);
361 EnableExclusiveEH();
362 ParseExclusiveFunc(opts::cg::ehExclusiveList);
363 }
364
365 if (opts::cg::cyclePatternList.IsEnabledByUser()) {
366 SetCyclePatternFile(opts::cg::cyclePatternList);
367 EnableEmitCyclePattern();
368 ParseCyclePattern(opts::cg::cyclePatternList);
369 }
370
371 if (opts::cg::cg.IsEnabledByUser()) {
372 SetRunCGFlag(opts::cg::cg);
373 opts::cg::cg ? SetOption(CGOptions::kDoCg) : ClearOption(CGOptions::kDoCg);
374 }
375
376 if (opts::cg::objmap.IsEnabledByUser()) {
377 SetGenerateObjectMap(opts::cg::objmap);
378 }
379
380 if (opts::cg::replaceAsm.IsEnabledByUser()) {
381 opts::cg::replaceAsm ? EnableReplaceASM() : DisableReplaceASM();
382 }
383
384 if (opts::cg::generalRegOnly.IsEnabledByUser()) {
385 opts::cg::generalRegOnly ? EnableGeneralRegOnly() : DisableGeneralRegOnly();
386 }
387
388 if (opts::cg::lazyBinding.IsEnabledByUser()) {
389 opts::cg::lazyBinding ? EnableLazyBinding() : DisableLazyBinding();
390 }
391
392 if (opts::cg::hotFix.IsEnabledByUser()) {
393 opts::cg::hotFix ? EnableHotFix() : DisableHotFix();
394 }
395
396 if (opts::cg::soeCheck.IsEnabledByUser()) {
397 SetOption(CGOptions::kSoeCheckInsert);
398 }
399
400 if (opts::cg::checkArraystore.IsEnabledByUser()) {
401 opts::cg::checkArraystore ? EnableCheckArrayStore() : DisableCheckArrayStore();
402 }
403
404 if (opts::cg::ebo.IsEnabledByUser()) {
405 opts::cg::ebo ? EnableEBO() : DisableEBO();
406 }
407
408 if (opts::cg::cfgo.IsEnabledByUser()) {
409 opts::cg::cfgo ? EnableCFGO() : DisableCFGO();
410 }
411
412 if (opts::cg::ico.IsEnabledByUser()) {
413 opts::cg::ico ? EnableICO() : DisableICO();
414 }
415
416 if (opts::cg::storeloadopt.IsEnabledByUser()) {
417 opts::cg::storeloadopt ? EnableStoreLoadOpt() : DisableStoreLoadOpt();
418 }
419
420 if (opts::cg::globalopt.IsEnabledByUser()) {
421 opts::cg::globalopt ? EnableGlobalOpt() : DisableGlobalOpt();
422 }
423
424 if (opts::cg::hotcoldsplit.IsEnabledByUser()) {
425 opts::cg::hotcoldsplit ? EnableHotColdSplit() : DisableHotColdSplit();
426 }
427
428 if (opts::cg::prelsra.IsEnabledByUser()) {
429 opts::cg::prelsra ? EnablePreLSRAOpt() : DisablePreLSRAOpt();
430 }
431
432 if (opts::cg::prepeep.IsEnabledByUser()) {
433 opts::cg::prepeep ? EnablePrePeephole() : DisablePrePeephole();
434 }
435
436 if (opts::cg::peep.IsEnabledByUser()) {
437 opts::cg::peep ? EnablePeephole() : DisablePeephole();
438 }
439
440 if (opts::cg::retMerge.IsEnabledByUser()) {
441 opts::cg::retMerge ? EnableRetMerge() : DisableRetMerge();
442 }
443
444 if (opts::cg::preschedule.IsEnabledByUser()) {
445 opts::cg::preschedule ? EnablePreSchedule() : DisablePreSchedule();
446 }
447
448 if (opts::cg::schedule.IsEnabledByUser()) {
449 opts::cg::schedule ? EnableSchedule() : DisableSchedule();
450 }
451
452 if (opts::cg::vregRename.IsEnabledByUser()) {
453 opts::cg::vregRename ? EnableVregRename() : DisableVregRename();
454 }
455
456 if (opts::cg::fullcolor.IsEnabledByUser()) {
457 opts::cg::fullcolor ? EnableMultiPassColorRA() : DisableMultiPassColorRA();
458 }
459
460 if (opts::cg::writefieldopt.IsEnabledByUser()) {
461 opts::cg::writefieldopt ? EnableWriteRefFieldOpt() : DisableWriteRefFieldOpt();
462 }
463
464 if (opts::cg::dumpOlog.IsEnabledByUser()) {
465 opts::cg::dumpOlog ? EnableDumpOptimizeCommonLog() : DisableDumpOptimizeCommonLog();
466 }
467
468 if (opts::cg::nativeopt.IsEnabledByUser()) {
469 // Disabling Looks strage: should be checked by author of the code
470 DisableNativeOpt();
471 }
472
473 if (opts::cg::dupBb.IsEnabledByUser()) {
474 opts::cg::dupBb ? DisableNoDupBB() : EnableNoDupBB();
475 }
476
477 if (opts::cg::calleeCfi.IsEnabledByUser()) {
478 opts::cg::calleeCfi ? DisableNoCalleeCFI() : EnableNoCalleeCFI();
479 }
480
481 if (opts::cg::proepilogue.IsEnabledByUser()) {
482 opts::cg::proepilogue ? SetOption(CGOptions::kProEpilogueOpt) : ClearOption(CGOptions::kProEpilogueOpt);
483 }
484
485 if (opts::cg::tailcall.IsEnabledByUser()) {
486 opts::cg::tailcall ? SetOption(CGOptions::kTailCallOpt) : ClearOption(CGOptions::kTailCallOpt);
487 }
488
489 if (opts::cg::calleeregsPlacement.IsEnabledByUser()) {
490 opts::cg::calleeregsPlacement ? EnableRegSavesOpt() : DisableRegSavesOpt();
491 }
492
493 if (opts::cg::ssapreSave.IsEnabledByUser()) {
494 opts::cg::ssapreSave ? EnableSsaPreSave() : DisableSsaPreSave();
495 }
496
497 if (opts::cg::ssupreRestore.IsEnabledByUser()) {
498 opts::cg::ssupreRestore ? EnableSsuPreRestore() : DisableSsuPreRestore();
499 }
500
501 if (opts::cg::lsraBb.IsEnabledByUser()) {
502 SetLSRABBOptSize(opts::cg::lsraBb);
503 }
504
505 if (opts::cg::lsraInsn.IsEnabledByUser()) {
506 SetLSRAInsnOptSize(opts::cg::lsraInsn);
507 }
508
509 if (opts::cg::lsraOverlap.IsEnabledByUser()) {
510 SetOverlapNum(opts::cg::lsraOverlap);
511 }
512
513 if (opts::cg::remat.IsEnabledByUser()) {
514 SetRematLevel(opts::cg::remat);
515 }
516
517 if (opts::cg::dumpPhases.IsEnabledByUser()) {
518 SplitPhases(opts::cg::dumpPhases, GetDumpPhases());
519 }
520
521 if (opts::cg::target.IsEnabledByUser()) {
522 SetTargetMachine(opts::cg::target);
523 }
524
525 if (opts::cg::skipPhases.IsEnabledByUser()) {
526 SplitPhases(opts::cg::skipPhases, GetSkipPhases());
527 }
528
529 if (opts::cg::skipFrom.IsEnabledByUser()) {
530 SetSkipFrom(opts::cg::skipFrom);
531 }
532
533 if (opts::cg::skipAfter.IsEnabledByUser()) {
534 SetSkipAfter(opts::cg::skipAfter);
535 }
536
537 if (opts::cg::debugSchedule.IsEnabledByUser()) {
538 opts::cg::debugSchedule ? EnableDebugSched() : DisableDebugSched();
539 }
540
541 if (opts::cg::bruteforceSchedule.IsEnabledByUser()) {
542 opts::cg::bruteforceSchedule ? EnableDruteForceSched() : DisableDruteForceSched();
543 }
544
545 if (opts::cg::simulateSchedule.IsEnabledByUser()) {
546 opts::cg::simulateSchedule ? EnableSimulateSched() : DisableSimulateSched();
547 }
548
549 if (opts::cg::floatAbi.IsEnabledByUser()) {
550 SetABIType(opts::cg::floatAbi);
551 }
552
553 if (opts::cg::filetype.IsEnabledByUser()) {
554 SetEmitFileType(opts::cg::filetype);
555 }
556
557 if (opts::cg::longCalls.IsEnabledByUser()) {
558 opts::cg::longCalls ? EnableLongCalls() : DisableLongCalls();
559 }
560
561 if (opts::cg::functionSections.IsEnabledByUser()) {
562 opts::cg::functionSections ? EnableFunctionSections() : DisableFunctionSections();
563 }
564
565 if (opts::cg::omitFramePointer.IsEnabledByUser()) {
566 opts::cg::omitFramePointer ? DisableFramePointer() : EnableFramePointer();
567 }
568
569 if (opts::cg::fastMath.IsEnabledByUser()) {
570 opts::cg::fastMath ? EnableFastMath() : DisableFastMath();
571 }
572
573 if (opts::cg::alignAnalysis.IsEnabledByUser()) {
574 opts::cg::alignAnalysis ? EnableAlignAnalysis() : DisableAlignAnalysis();
575 }
576
577 if (opts::cg::condbrAlign.IsEnabledByUser()) {
578 opts::cg::condbrAlign ? EnableCondBrAlign() : DisableCondBrAlign();
579 }
580
581 /* big endian can be set with several options: --target, -Be.
582 * Triple takes to account all these options and allows to detect big endian with IsBigEndian() interface */
583 Triple::GetTriple().IsBigEndian() ? EnableBigEndianInCG() : DisableBigEndianInCG();
584 (maple::Triple::GetTriple().GetEnvironment() == Triple::GNUILP32) ? EnableArm64ilp32() : DisableArm64ilp32();
585
586 if (opts::cg::cgSsa.IsEnabledByUser()) {
587 opts::cg::cgSsa ? EnableCGSSA() : DisableCGSSA();
588 }
589
590 if (opts::cg::common.IsEnabledByUser()) {
591 opts::cg::common ? EnableCommon() : DisableCommon();
592 }
593
594 if (opts::cg::alignMinBbSize.IsEnabledByUser()) {
595 SetAlignMinBBSize(opts::cg::alignMinBbSize);
596 }
597
598 if (opts::cg::alignMaxBbSize.IsEnabledByUser()) {
599 SetAlignMaxBBSize(opts::cg::alignMaxBbSize);
600 }
601
602 if (opts::cg::loopAlignPow.IsEnabledByUser()) {
603 SetLoopAlignPow(opts::cg::loopAlignPow);
604 }
605
606 if (opts::cg::jumpAlignPow.IsEnabledByUser()) {
607 SetJumpAlignPow(opts::cg::jumpAlignPow);
608 }
609
610 if (opts::cg::funcAlignPow.IsEnabledByUser()) {
611 SetFuncAlignPow(opts::cg::funcAlignPow);
612 }
613
614 /* override some options when loc, dwarf is generated */
615 if (WithLoc()) {
616 DisableSchedule();
617 SetOption(kWithSrc);
618 }
619 if (WithDwarf()) {
620 DisableEBO();
621 DisableCFGO();
622 DisableICO();
623 DisableSchedule();
624 SetOption(kDebugFriendly);
625 SetOption(kWithSrc);
626 SetOption(kWithLoc);
627 ClearOption(kSuppressFileInfo);
628 }
629
630 return true;
631 }
632
ParseExclusiveFunc(const std::string & fileName)633 void CGOptions::ParseExclusiveFunc(const std::string &fileName)
634 {
635 std::ifstream file(fileName);
636 if (!file.is_open()) {
637 ERR(kLncErr, "%s open failed!", fileName.c_str());
638 return;
639 }
640 std::string content;
641 while (file >> content) {
642 ehExclusiveFunctionName.push_back(content);
643 }
644 }
645
ParseCyclePattern(const std::string & fileName)646 void CGOptions::ParseCyclePattern(const std::string &fileName)
647 {
648 std::ifstream file(fileName);
649 if (!file.is_open()) {
650 ERR(kLncErr, "%s open failed!", fileName.c_str());
651 return;
652 }
653 std::string content;
654 std::string classStr("class: ");
655 while (getline(file, content)) {
656 if (content.compare(0, classStr.length(), classStr) == 0) {
657 std::vector<std::string> classPatternContent;
658 std::string patternContent;
659 while (getline(file, patternContent)) {
660 if (patternContent.length() == 0) {
661 break;
662 }
663 classPatternContent.push_back(patternContent);
664 }
665 std::string className = content.substr(classStr.length());
666 CGOptions::cyclePatternMap[className] = std::move(classPatternContent);
667 }
668 }
669 }
670
SetRange(const std::string & str,const std::string & cmd,Range & subRange)671 void CGOptions::SetRange(const std::string &str, const std::string &cmd, Range &subRange)
672 {
673 const std::string &tmpStr = str;
674 size_t comma = tmpStr.find_first_of(",", 0);
675 subRange.enable = true;
676
677 if (comma != std::string::npos) {
678 subRange.begin = std::stoul(tmpStr.substr(0, comma), nullptr);
679 subRange.end = std::stoul(tmpStr.substr(comma + 1, std::string::npos - (comma + 1)), nullptr);
680 }
681 CHECK_FATAL(range.begin < range.end, "invalid values for %s=%lu,%lu", cmd.c_str(), subRange.begin, subRange.end);
682 }
683
684 /* Set default options according to different languages. */
SetDefaultOptions(const maple::MIRModule & mod)685 void CGOptions::SetDefaultOptions(const maple::MIRModule &mod)
686 {
687 if (mod.IsJavaModule()) {
688 generateFlag = generateFlag | kGenYieldPoint | kGenLocalRc | kGrootList | kPrimorList;
689 }
690 insertYieldPoint = GenYieldPoint();
691 }
692
EnableO0()693 void CGOptions::EnableO0()
694 {
695 optimizeLevel = kLevel0;
696 doEBO = false;
697 doCGSSA = false;
698 doCFGO = false;
699 doICO = false;
700 doPrePeephole = false;
701 doPeephole = false;
702 doStoreLoadOpt = false;
703 doGlobalOpt = false;
704 doPreLSRAOpt = false;
705 doPreSchedule = false;
706 doSchedule = false;
707 doRegSavesOpt = false;
708 useSsaPreSave = false;
709 useSsuPreRestore = false;
710 doWriteRefFieldOpt = false;
711 doAlignAnalysis = false;
712 doCondBrAlign = false;
713
714 if (maple::Triple::GetTriple().GetEnvironment() == Triple::GNUILP32) {
715 ClearOption(kUseStackProtectorStrong);
716 ClearOption(kUseStackProtectorAll);
717 } else {
718 SetOption(kUseStackProtectorStrong);
719 SetOption(kUseStackProtectorAll);
720 }
721
722 ClearOption(kConstFold);
723 ClearOption(kProEpilogueOpt);
724 ClearOption(kTailCallOpt);
725 }
726
EnableO1()727 void CGOptions::EnableO1()
728 {
729 optimizeLevel = kLevel1;
730 doPreLSRAOpt = true;
731 SetOption(kConstFold);
732 SetOption(kProEpilogueOpt);
733 SetOption(kTailCallOpt);
734 ClearOption(kUseStackProtectorStrong);
735 ClearOption(kUseStackProtectorAll);
736 }
737
EnableO2()738 void CGOptions::EnableO2()
739 {
740 optimizeLevel = kLevel2;
741 doEBO = true;
742 doCGSSA = true;
743 doCFGO = true;
744 doICO = true;
745 doPrePeephole = true;
746 doPeephole = true;
747 doStoreLoadOpt = true;
748 doGlobalOpt = true;
749 doPreSchedule = true;
750 doSchedule = true;
751 doAlignAnalysis = true;
752 doCondBrAlign = true;
753 SetOption(kConstFold);
754 ClearOption(kUseStackProtectorStrong);
755 ClearOption(kUseStackProtectorAll);
756 #if TARGARM32
757 doPreLSRAOpt = false;
758 doWriteRefFieldOpt = false;
759 ClearOption(kProEpilogueOpt);
760 ClearOption(kTailCallOpt);
761 #else
762 doPreLSRAOpt = true;
763 doRegSavesOpt = false;
764 useSsaPreSave = false;
765 useSsuPreRestore = true;
766 doWriteRefFieldOpt = true;
767 SetOption(kProEpilogueOpt);
768 SetOption(kTailCallOpt);
769 #endif
770 }
771
EnableLiteCG()772 void CGOptions::EnableLiteCG()
773 {
774 optimizeLevel = kLevelLiteCG;
775 doEBO = false;
776 doCGSSA = false;
777 doCGRegCoalesce = false;
778 doCFGO = true;
779 doICO = false;
780 doPrePeephole = false;
781 doPeephole = true;
782 doStoreLoadOpt = false;
783 doGlobalOpt = false;
784 doPreLSRAOpt = false;
785 doPreSchedule = false;
786 doSchedule = false;
787 doRegSavesOpt = false;
788 useSsaPreSave = false;
789 useSsuPreRestore = false;
790 doWriteRefFieldOpt = false;
791 doAlignAnalysis = false;
792 doCondBrAlign = false;
793
794 ClearOption(kUseStackProtectorStrong);
795 ClearOption(kUseStackProtectorAll);
796 ClearOption(kConstFold);
797 ClearOption(kProEpilogueOpt);
798 ClearOption(kTailCallOpt);
799 }
800
SetTargetMachine(const std::string & str)801 void CGOptions::SetTargetMachine(const std::string &str)
802 {
803 if (str == "aarch64") {
804 targetArch = "aarch64";
805 } else if (str == "x86_64") {
806 targetArch = "x86_64";
807 }
808 CHECK_FATAL(false, "unknown target. not implement yet");
809 }
810
SplitPhases(const std::string & str,std::unordered_set<std::string> & set)811 void CGOptions::SplitPhases(const std::string &str, std::unordered_set<std::string> &set)
812 {
813 const std::string &tmpStr {str};
814 if ((tmpStr.compare("*") == 0) || (tmpStr.compare("cgir") == 0)) {
815 (void)set.insert(tmpStr);
816 return;
817 }
818 StringUtils::Split(tmpStr, set, ',');
819 }
820
DumpPhase(const std::string & phase)821 bool CGOptions::DumpPhase(const std::string &phase)
822 {
823 return (IS_STR_IN_SET(dumpPhases, "*") || IS_STR_IN_SET(dumpPhases, "cgir") || IS_STR_IN_SET(dumpPhases, phase));
824 }
825
826 /* match sub std::string of function name */
FuncFilter(const std::string & name)827 bool CGOptions::FuncFilter(const std::string &name)
828 {
829 return dumpFunc == "*" || dumpFunc == name;
830 }
831 } /* namespace maplebe */
832