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 #ifndef MAPLE_IR_INCLUDE_OPCODES_H
17 #define MAPLE_IR_INCLUDE_OPCODES_H
18 #include "types_def.h"
19 #include "mpl_logging.h"
20
21 namespace maple {
22 enum Opcode : uint8 {
23 OP_undef,
24 #define OPCODE(STR, YY, ZZ, SS) OP_##STR,
25 #include "opcodes.def"
26 #undef OPCODE
27 OP_last,
28 };
29
IsDAssign(Opcode code)30 inline constexpr bool IsDAssign(Opcode code)
31 {
32 return (code == OP_dassign);
33 }
34
IsCallAssigned(Opcode code)35 inline constexpr bool IsCallAssigned(Opcode code)
36 {
37 return (code == OP_callassigned ||
38 code == OP_icallassigned || code == OP_icallprotoassigned || code == OP_intrinsiccallassigned);
39 }
40
IsBranch(Opcode opcode)41 inline constexpr bool IsBranch(Opcode opcode)
42 {
43 return (opcode == OP_goto || opcode == OP_brtrue || opcode == OP_brfalse || opcode == OP_switch);
44 }
45
IsLogicalShift(Opcode opcode)46 inline constexpr bool IsLogicalShift(Opcode opcode)
47 {
48 return (opcode == OP_lshr || opcode == OP_shl);
49 }
50
IsCommutative(Opcode opcode)51 constexpr bool IsCommutative(Opcode opcode)
52 {
53 switch (opcode) {
54 case OP_add:
55 case OP_mul:
56 case OP_max:
57 case OP_min:
58 case OP_band:
59 case OP_bior:
60 case OP_bxor:
61 case OP_eq:
62 case OP_ne:
63 return true;
64 default:
65 return false;
66 }
67 }
68
IsStmtMustRequire(Opcode opcode)69 constexpr bool IsStmtMustRequire(Opcode opcode)
70 {
71 switch (opcode) {
72 case OP_return:
73 case OP_call:
74 case OP_callassigned:
75 case OP_icall:
76 case OP_icallassigned:
77 case OP_icallproto:
78 case OP_icallprotoassigned:
79 case OP_intrinsiccall:
80 case OP_intrinsiccallassigned:
81 case OP_intrinsiccallwithtype: {
82 return true;
83 }
84 default:
85 return false;
86 }
87 }
88
89 // the result of these op is actually u1(may be set as other type, but its return value can only be zero or one)
90 // different from kOpcodeInfo.IsCompare(op) : cmp/cmpg/cmpl have no reverse op, and may return -1/0/1
IsCompareHasReverseOp(Opcode op)91 constexpr bool IsCompareHasReverseOp(Opcode op)
92 {
93 if (op == OP_eq || op == OP_ne || op == OP_ge || op == OP_gt || op == OP_le || op == OP_lt) {
94 return true;
95 }
96 return false;
97 }
98
GetSwapCmpOp(Opcode op)99 constexpr Opcode GetSwapCmpOp(Opcode op)
100 {
101 switch (op) {
102 case OP_eq:
103 return OP_eq;
104 case OP_ne:
105 return OP_ne;
106 case OP_ge:
107 return OP_le;
108 case OP_gt:
109 return OP_lt;
110 case OP_le:
111 return OP_ge;
112 case OP_lt:
113 return OP_gt;
114 default:
115 CHECK_FATAL(false, "can't swap op");
116 return op;
117 }
118 }
119
GetReverseCmpOp(Opcode op)120 constexpr Opcode GetReverseCmpOp(Opcode op)
121 {
122 switch (op) {
123 case OP_eq:
124 return OP_ne;
125 case OP_ne:
126 return OP_eq;
127 case OP_ge:
128 return OP_lt;
129 case OP_gt:
130 return OP_le;
131 case OP_le:
132 return OP_gt;
133 case OP_lt:
134 return OP_ge;
135 default:
136 CHECK_FATAL(false, "opcode has no reverse op");
137 return op;
138 }
139 }
140
IsSupportedOpForCopyInPhasesLoopUnrollAndVRP(Opcode op)141 constexpr bool IsSupportedOpForCopyInPhasesLoopUnrollAndVRP(Opcode op)
142 {
143 switch (op) {
144 case OP_switch:
145 case OP_comment:
146 case OP_goto:
147 case OP_dassign:
148 case OP_regassign:
149 case OP_brfalse:
150 case OP_brtrue:
151 case OP_iassign:
152 case OP_call:
153 case OP_callassigned:
154 case OP_intrinsiccall:
155 case OP_intrinsiccallassigned:
156 case OP_intrinsiccallwithtype: {
157 return true;
158 }
159 default:
160 return false;
161 }
162 }
163 } // namespace maple
164 #endif // MAPLE_IR_INCLUDE_OPCODES_H
165