• 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 #ifndef MAPLE_ME_INCLUDE_CAST_OPT_H
17 #define MAPLE_ME_INCLUDE_CAST_OPT_H
18 #include "mir_nodes.h"
19 #include "me_ir.h"
20 
21 namespace maple {
22 // The order matters
23 enum CastKind {
24     CAST_intTrunc = 0,
25     CAST_zext = 1,
26     CAST_sext = 2,
27     CAST_int2fp = 3,
28     CAST_fp2int = 4,
29     CAST_fpTrunc = 5,
30     CAST_fpExt = 6,
31     CAST_retype = 7,
32     CAST_unknown = 8
33 };
34 
35 template <typename T,
36           std::enable_if_t<std::is_base_of<MeExpr, T>::value || std::is_base_of<BaseNode, T>::value, bool> = true>
37 class CastInfo {
38 public:
CastInfo(T * expr)39     explicit CastInfo(T *expr) : expr(expr) {}
40     virtual ~CastInfo() = default;
GetOp()41     virtual Opcode GetOp()
42     {
43         CHECK_FATAL(false, "NYI");
44     }
GetPrimType()45     PrimType GetPrimType() const
46     {
47         return expr->GetPrimType();
48     }
GetBitsSize()49     virtual size_t GetBitsSize()
50     {
51         CHECK_FATAL(false, "NYI");
52     }
GetOpnd(size_t index)53     virtual T *GetOpnd(size_t index __attribute__((unused)))
54     {
55         CHECK_FATAL(false, "NYI");
56     }
57 
IsInvalid()58     bool IsInvalid() const
59     {
60         return kind == CAST_unknown;
61     }
62     CastKind kind = CAST_unknown;  // CastInfo is invalid if kind is CAST_unknown
63     PrimType srcType = PTY_begin;
64     PrimType dstType = PTY_end;
65     T *expr = nullptr;  // expr's type must be MeExpr* or BaseNode*
66 };
67 
68 class BaseNodeCastInfo : public CastInfo<BaseNode> {
69 public:
BaseNodeCastInfo(BaseNode * expr)70     explicit BaseNodeCastInfo(BaseNode *expr) : CastInfo(expr) {}
71     ~BaseNodeCastInfo() = default;
72 
GetOp()73     Opcode GetOp() override
74     {
75         return expr->GetOpCode();
76     }
77 
GetBitsSize()78     size_t GetBitsSize() override
79     {
80         switch (GetOp()) {
81             case OP_zext:
82             case OP_sext:
83                 return static_cast<const ExtractbitsNode *>(expr)->GetBitsSize();
84             default:
85                 CHECK_FATAL(false, "NYI");
86                 break;
87         }
88     }
89 
GetOpnd(size_t index)90     BaseNode *GetOpnd(size_t index) override
91     {
92         return expr->Opnd(index);
93     }
94 };
95 
96 class CastOpt {
97 public:
98     static bool IsExplicitCastOp(Opcode op);
99     static bool IsImplicitCastOp(Opcode op);
100     static bool IsCompareOp(Opcode op);
101 };
102 
103 class MapleCastOpt : public CastOpt {
104 public:
105     static BaseNode *CreateMapleExprByCastKind(MIRBuilder &mirBuilder, CastKind castKind, PrimType srcType,
106                                                PrimType dstType, BaseNode *opnd, TyIdx dstTyIdx = TyIdx(0));
107     static BaseNode *SimplifyCastSingle(MIRBuilder &mirBuilder, const BaseNodeCastInfo &castInfo);
108     static BaseNode *TransformCvtU1ToNe(MIRBuilder &mirBuilder, const TypeCvtNode *cvtExpr);
109 };
110 }  // namespace maple
111 #endif
112