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