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 "local_opt.h"
17 #include "cg.h"
18 #include "mpl_logging.h"
19 #if defined TARGX86_64
20 #include "x64_reaching.h"
21 #endif
22 /*
23 * this phase does optimization on local level(single bb or super bb)
24 * this phase requires liveanalysis
25 */
26 namespace maplebe {
DoLocalCopyPropOptmize()27 void LocalOpt::DoLocalCopyPropOptmize()
28 {
29 DoLocalCopyProp();
30 }
31
Run()32 void LocalPropOptimizePattern::Run()
33 {
34 FOR_ALL_BB(bb, &cgFunc) {
35 FOR_BB_INSNS(insn, bb) {
36 if (!insn->IsMachineInstruction()) {
37 continue;
38 }
39 if (!CheckCondition(*insn)) {
40 continue;
41 }
42 Optimize(*bb, *insn);
43 }
44 }
45 }
46
PhaseRun(maplebe::CGFunc & f)47 bool LocalCopyProp::PhaseRun(maplebe::CGFunc &f)
48 {
49 MemPool *mp = GetPhaseMemPool();
50 auto *reachingDef = f.GetCG()->CreateReachingDefinition(*mp, f);
51 LocalOpt *localOpt = f.GetCG()->CreateLocalOpt(*mp, f, *reachingDef);
52 localOpt->DoLocalCopyPropOptmize();
53 return false;
54 }
55
GetAnalysisDependence(maple::AnalysisDep & aDep) const56 void LocalCopyProp::GetAnalysisDependence(maple::AnalysisDep &aDep) const
57 {
58 aDep.AddRequired<CgLiveAnalysis>();
59 aDep.SetPreservedAll();
60 }
61
CheckCondition(Insn & insn)62 bool RedundantDefRemove::CheckCondition(Insn &insn)
63 {
64 uint32 opndNum = insn.GetOperandSize();
65 const InsnDesc *md = insn.GetDesc();
66 std::vector<Operand *> defOpnds;
67 for (uint32 i = 0; i < opndNum; ++i) {
68 Operand &opnd = insn.GetOperand(i);
69 auto *opndDesc = md->opndMD[i];
70 if (opndDesc->IsDef() && opndDesc->IsUse()) {
71 return false;
72 }
73 if (opnd.IsList()) {
74 continue;
75 }
76 if (opndDesc->IsDef()) {
77 defOpnds.emplace_back(&opnd);
78 }
79 }
80 if (defOpnds.size() != 1 || !defOpnds[0]->IsRegister()) {
81 return false;
82 }
83 auto ®Def = static_cast<RegOperand &>(*defOpnds[0]);
84 auto &liveOutRegSet = insn.GetBB()->GetLiveOutRegNO();
85 if (liveOutRegSet.find(regDef.GetRegisterNumber()) != liveOutRegSet.end()) {
86 return false;
87 }
88 return true;
89 }
90
91 MAPLE_TRANSFORM_PHASE_REGISTER_CANSKIP(LocalCopyProp, localcopyprop)
92 } /* namespace maplebe */
93