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 MPL2MPL_INCLUDE_SIMPLIFY_H 17 #define MPL2MPL_INCLUDE_SIMPLIFY_H 18 #include "phase_impl.h" 19 #include "factory.h" 20 #include "maple_phase_manager.h" 21 namespace maple { 22 23 const std::map<std::string, std::string> asmMap = { 24 #include "asm_map.def" 25 }; 26 27 enum ErrorNumber { 28 ERRNO_OK = EOK, 29 ERRNO_INVAL = EINVAL, 30 ERRNO_RANGE = ERANGE, 31 ERRNO_INVAL_AND_RESET = EINVAL_AND_RESET, 32 ERRNO_RANGE_AND_RESET = ERANGE_AND_RESET, 33 ERRNO_OVERLAP_AND_RESET = EOVERLAP_AND_RESET 34 }; 35 36 enum MemOpKind { MEM_OP_unknown, MEM_OP_memset, MEM_OP_memcpy, MEM_OP_memset_s, MEM_OP_memcpy_s }; 37 38 // MemEntry models a memory entry with high level type information. 39 struct MemEntry { 40 enum MemEntryKind { kMemEntryUnknown, kMemEntryPrimitive, kMemEntryStruct, kMemEntryArray }; 41 42 MemEntry() = default; MemEntryMemEntry43 MemEntry(BaseNode *addrExpr, MIRType *memType) : addrExpr(addrExpr), memType(memType) {} 44 GetKindMemEntry45 MemEntryKind GetKind() const 46 { 47 if (memType == nullptr) { 48 return kMemEntryUnknown; 49 } 50 auto typeKind = memType->GetKind(); 51 if (typeKind == kTypeScalar || typeKind == kTypePointer) { 52 return kMemEntryPrimitive; 53 } else if (typeKind == kTypeArray) { 54 return kMemEntryArray; 55 } else if (memType->IsStructType()) { 56 return kMemEntryStruct; 57 } 58 return kMemEntryUnknown; 59 } 60 61 BaseNode *BuildAsRhsExpr(MIRFunction &func) const; 62 void ExpandMemsetLowLevel(int64 byte, uint64 size, MIRFunction &func, StmtNode &stmt, BlockNode &block, 63 MemOpKind memOpKind, bool debug, ErrorNumber errorNumber) const; 64 static StmtNode *GenMemopRetAssign(StmtNode &stmt, MIRFunction &func, bool isLowLevel, MemOpKind memOpKind, 65 ErrorNumber errorNumber = ERRNO_OK); 66 67 BaseNode *addrExpr = nullptr; // memory address 68 MIRType *memType = nullptr; // memory type, this may be nullptr for low level memory entry 69 }; 70 71 // For simplifying memory operation, either memset or memcpy/memmove. 72 class SimplifyMemOp { 73 public: 74 static MemOpKind ComputeMemOpKind(StmtNode &stmt); 75 SimplifyMemOp() = default; 76 virtual ~SimplifyMemOp() = default; func(func)77 explicit SimplifyMemOp(MIRFunction *func, bool debug = false) : func(func), debug(debug) {} SetFunction(MIRFunction * f)78 void SetFunction(MIRFunction *f) 79 { 80 func = f; 81 } SetDebug(bool dbg)82 void SetDebug(bool dbg) 83 { 84 debug = dbg; 85 } 86 87 bool AutoSimplify(StmtNode &stmt, BlockNode &block, bool isLowLevel); 88 89 private: 90 static const uint32 thresholdMemsetExpand; 91 static const uint32 thresholdMemsetSExpand; 92 static const uint32 thresholdMemcpyExpand; 93 static const uint32 thresholdMemcpySExpand; 94 MIRFunction *func = nullptr; 95 bool debug = false; 96 }; 97 98 class Simplify : public FuncOptimizeImpl { 99 public: Simplify(MIRModule & mod,KlassHierarchy * kh,bool dump)100 Simplify(MIRModule &mod, KlassHierarchy *kh, bool dump) : FuncOptimizeImpl(mod, kh, dump), mirMod(mod) {} 101 Simplify(const Simplify &other) = delete; 102 Simplify &operator=(const Simplify &other) = delete; 103 ~Simplify() = default; Clone()104 FuncOptimizeImpl *Clone() override 105 { 106 CHECK_FATAL(false, "Simplify has pointer, should not be Cloned"); 107 } 108 109 void ProcessStmt(StmtNode &stmt) override; 110 void Finish() override; 111 112 private: 113 MIRModule &mirMod; 114 SimplifyMemOp simplifyMemOp; 115 bool IsMathSqrt(const std::string funcName); 116 bool IsMathAbs(const std::string funcName); 117 bool IsMathMin(const std::string funcName); 118 bool IsMathMax(const std::string funcName); 119 bool IsSymbolReplaceableWithConst(const MIRSymbol &symbol) const; 120 bool IsConstRepalceable(const MIRConst &mirConst) const; 121 bool SimplifyMathMethod(const StmtNode &stmt, BlockNode &block); 122 void SimplifyCallAssigned(StmtNode &stmt, BlockNode &block); 123 BaseNode *SimplifyExpr(BaseNode &expr); 124 BaseNode *ReplaceExprWithConst(DreadNode &dread); 125 MIRConst *GetElementConstFromFieldId(FieldID fieldId, MIRConst *mirConst); 126 }; 127 128 } // namespace maple 129 #endif // MPL2MPL_INCLUDE_SIMPLIFY_H 130