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 "ico.h"
17 #include "cg_option.h"
18 #ifdef TARGAARCH64
19 #include "aarch64_ico.h"
20 #include "aarch64_isa.h"
21 #include "aarch64_insn.h"
22 #elif TARGRISCV64
23 #include "riscv64_ico.h"
24 #include "riscv64_isa.h"
25 #include "riscv64_insn.h"
26 #elif TARGARM32
27 #include "arm32_ico.h"
28 #include "arm32_isa.h"
29 #include "arm32_insn.h"
30 #endif
31 #include "cg.h"
32
33 /*
34 * This phase implements if-conversion optimization,
35 * which tries to convert conditional branches into cset/csel instructions
36 */
37 #define ICO_DUMP_NEWPM CG_DEBUG_FUNC(f)
38 namespace maplebe {
FindLastCmpInsn(BB & bb) const39 Insn *ICOPattern::FindLastCmpInsn(BB &bb) const
40 {
41 if (bb.GetKind() != BB::kBBIf) {
42 return nullptr;
43 }
44 FOR_BB_INSNS_REV(insn, (&bb)) {
45 if (cgFunc->GetTheCFG()->GetInsnModifier()->IsCompareInsn(*insn)) {
46 return insn;
47 }
48 }
49 return nullptr;
50 }
51
GetLabelOpnds(Insn & insn) const52 std::vector<LabelOperand *> ICOPattern::GetLabelOpnds(Insn &insn) const
53 {
54 std::vector<LabelOperand *> labelOpnds;
55 for (uint32 i = 0; i < insn.GetOperandSize(); i++) {
56 if (insn.GetOperand(i).IsLabelOpnd()) {
57 labelOpnds.emplace_back(static_cast<LabelOperand *>(&insn.GetOperand(i)));
58 }
59 }
60 return labelOpnds;
61 }
62
PhaseRun(maplebe::CGFunc & f)63 bool CgIco::PhaseRun(maplebe::CGFunc &f)
64 {
65 LiveAnalysis *live = GET_ANALYSIS(CgLiveAnalysis, f);
66 if (ICO_DUMP_NEWPM) {
67 DotGenerator::GenerateDot("ico-before", f, f.GetMirModule());
68 }
69 MemPool *memPool = GetPhaseMemPool();
70 IfConversionOptimizer *ico = nullptr;
71 #if TARGAARCH64 || TARGRISCV64
72 ico = memPool->New<AArch64IfConversionOptimizer>(f, *memPool);
73 #endif
74 #if TARGARM32
75 ico = memPool->New<Arm32IfConversionOptimizer>(f, *memPool);
76 #endif
77 const std::string &funcClass = f.GetFunction().GetBaseClassName();
78 const std::string &funcName = f.GetFunction().GetBaseFuncName();
79 std::string name = funcClass + funcName;
80 ico->Run(name);
81 if (ICO_DUMP_NEWPM) {
82 DotGenerator::GenerateDot("ico-after", f, f.GetMirModule());
83 }
84 /* the live range info may changed, so invalid the info. */
85 if (live != nullptr) {
86 live->ClearInOutDataInfo();
87 }
88 return false;
89 }
GetAnalysisDependence(maple::AnalysisDep & aDep) const90 void CgIco::GetAnalysisDependence(maple::AnalysisDep &aDep) const
91 {
92 aDep.AddRequired<CgLiveAnalysis>();
93 aDep.PreservedAllExcept<CgLiveAnalysis>();
94 }
95 MAPLE_TRANSFORM_PHASE_REGISTER_CANSKIP(CgIco, ico)
96 } /* namespace maplebe */
97