• 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 
16 #include "aarch64_dce.h"
17 #include "aarch64_operand.h"
18 namespace maplebe {
RemoveUnuseDef(VRegVersion & defVersion)19 bool AArch64Dce::RemoveUnuseDef(VRegVersion &defVersion)
20 {
21     /* delete defs which have no uses */
22     if (defVersion.GetAllUseInsns().empty()) {
23         DUInsnInfo *defInsnInfo = defVersion.GetDefInsnInfo();
24         if (defInsnInfo == nullptr) {
25             return false;
26         }
27         CHECK_FATAL(defInsnInfo->GetInsn() != nullptr, "Get def insn failed");
28         Insn *defInsn = defInsnInfo->GetInsn();
29         /* have not support asm/neon opt yet */
30         if (defInsn->GetMachineOpcode() == MOP_asm || defInsn->IsVectorOp() || defInsn->IsAtomic()) {
31             return false;
32         }
33         std::set<uint32> defRegs = defInsn->GetDefRegs();
34         if (defRegs.size() != 1) {
35             return false;
36         }
37         uint32 bothDUIdx = defInsn->GetBothDefUseOpnd();
38         if (!(bothDUIdx != kInsnMaxOpnd && defInsnInfo->GetOperands().count(bothDUIdx))) {
39             defInsn->GetBB()->RemoveInsn(*defInsn);
40             if (defInsn->IsPhi()) {
41                 defInsn->GetBB()->RemovePhiInsn(defVersion.GetOriginalRegNO());
42             }
43             defVersion.MarkDeleted();
44             uint32 opndNum = defInsn->GetOperandSize();
45             for (uint32 i = opndNum; i > 0; --i) {
46                 Operand &opnd = defInsn->GetOperand(i - 1);
47                 A64DeleteRegUseVisitor deleteUseRegVisitor(*GetSSAInfo(), defInsn->GetId());
48                 opnd.Accept(deleteUseRegVisitor);
49             }
50             return true;
51         }
52     }
53     return false;
54 }
55 
Visit(RegOperand * v)56 void A64DeleteRegUseVisitor::Visit(RegOperand *v)
57 {
58     if (v->IsSSAForm()) {
59         VRegVersion *regVersion = GetSSAInfo()->FindSSAVersion(v->GetRegisterNumber());
60         DEBUG_ASSERT(regVersion != nullptr, "nullptr check");
61         MapleUnorderedMap<uint32, DUInsnInfo *> &useInfos = regVersion->GetAllUseInsns();
62         auto it = useInfos.find(deleteInsnId);
63         if (it != useInfos.end()) {
64             useInfos.erase(it);
65         }
66     }
67 }
Visit(ListOperand * v)68 void A64DeleteRegUseVisitor::Visit(ListOperand *v)
69 {
70     for (auto *regOpnd : v->GetOperands()) {
71         Visit(regOpnd);
72     }
73 }
Visit(MemOperand * a64MemOpnd)74 void A64DeleteRegUseVisitor::Visit(MemOperand *a64MemOpnd)
75 {
76     RegOperand *baseRegOpnd = a64MemOpnd->GetBaseRegister();
77     RegOperand *indexRegOpnd = a64MemOpnd->GetIndexRegister();
78     if (baseRegOpnd != nullptr && baseRegOpnd->IsSSAForm()) {
79         Visit(baseRegOpnd);
80     }
81     if (indexRegOpnd != nullptr && indexRegOpnd->IsSSAForm()) {
82         Visit(indexRegOpnd);
83     }
84 }
85 
Visit(PhiOperand * v)86 void A64DeleteRegUseVisitor::Visit(PhiOperand *v)
87 {
88     for (auto phiOpndIt : v->GetOperands()) {
89         Visit(phiOpndIt.second);
90     }
91 }
92 }  // namespace maplebe
93