• 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 "standardize.h"
17 namespace maplebe {
18 
DoStandardize()19 void Standardize::DoStandardize()
20 {
21     /* two address mapping first */
22     FOR_ALL_BB(bb, cgFunc) {
23         FOR_BB_INSNS(insn, bb) {
24             if (insn->IsMachineInstruction()) {
25                 continue;
26             }
27             if (NeedAddressMapping(*insn)) {
28                 AddressMapping(*insn);
29             }
30         }
31     }
32 
33     /* standardize for each op */
34     FOR_ALL_BB(bb, cgFunc) {
35         FOR_BB_INSNS(insn, bb) {
36             if (insn->IsMachineInstruction()) {
37                 continue;
38             }
39             if (insn->IsMove()) {
40                 StdzMov(*insn);
41             } else if (insn->IsStore() || insn->IsLoad()) {
42                 StdzStrLdr(*insn);
43             } else if (insn->IsBasicOp()) {
44                 StdzBasicOp(*insn);
45             } else if (insn->IsUnaryOp()) {
46                 StdzUnaryOp(*insn, *cgFunc);
47             } else if (insn->IsConversion()) {
48                 StdzCvtOp(*insn, *cgFunc);
49             } else if (insn->IsShift()) {
50                 StdzShiftOp(*insn, *cgFunc);
51             } else {
52                 LogInfo::MapleLogger() << "Need STDZ function for " << insn->GetDesc()->GetName() << "\n";
53                 CHECK_FATAL(false, "NIY");
54             }
55         }
56     }
57 }
58 
AddressMapping(Insn & insn)59 void Standardize::AddressMapping(Insn &insn)
60 {
61     Operand &dest = insn.GetOperand(kInsnFirstOpnd);
62     Operand &src1 = insn.GetOperand(kInsnSecondOpnd);
63     uint32 destSize = dest.GetSize();
64     bool isInt = (static_cast<RegOperand&>(dest).GetRegisterType() == kRegTyInt);
65     MOperator mOp = abstract::MOP_undef;
66     switch (destSize) {
67         case k8BitSize:
68             mOp = isInt ? abstract::MOP_copy_rr_8 : abstract::MOP_undef;
69             break;
70         case k16BitSize:
71             mOp = isInt ? abstract::MOP_copy_rr_16 : abstract::MOP_undef;
72             break;
73         case k32BitSize:
74             mOp = isInt ? abstract::MOP_copy_rr_32 : abstract::MOP_copy_ff_32;
75             break;
76         case k64BitSize:
77             mOp = isInt ? abstract::MOP_copy_rr_64 : abstract::MOP_copy_ff_64;
78             break;
79         default:
80             break;
81     }
82     CHECK_FATAL(mOp != abstract::MOP_undef, "do two address mapping failed");
83     Insn &newInsn = cgFunc->GetInsnBuilder()->BuildInsn(mOp, InsnDesc::GetAbstractId(mOp));
84     (void)newInsn.AddOpndChain(dest).AddOpndChain(src1);
85     (void)insn.GetBB()->InsertInsnBefore(insn, newInsn);
86 }
87 }  // namespace maplebe
88