1 /* 2 * Copyright (c) 2021-2024 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 COMPILER_OPTIMIZER_OPTIMIZATIONS_MEMORY_COALESCING_H 17 #define COMPILER_OPTIMIZER_OPTIMIZATIONS_MEMORY_COALESCING_H 18 19 #include "optimizer/ir/graph.h" 20 #include "optimizer/pass.h" 21 #include "compiler_options.h" 22 23 namespace ark::compiler { 24 class MemoryCoalescing : public Optimization { 25 using Optimization::Optimization; 26 27 public: 28 struct CoalescedPair { 29 Inst *first; 30 Inst *second; 31 }; 32 Optimization(graph)33 explicit MemoryCoalescing(Graph *graph, bool aligned = true) : Optimization(graph), alignedOnly_(aligned) {} 34 35 bool RunImpl() override; 36 IsEnable()37 bool IsEnable() const override 38 { 39 return g_options.IsCompilerMemoryCoalescing(); 40 } 41 GetPassName()42 const char *GetPassName() const override 43 { 44 return "MemoryCoalescing"; 45 } 46 47 /// Types of memory accesses that can be coalesced AcceptedType(DataType::Type type)48 static bool AcceptedType(DataType::Type type) 49 { 50 switch (type) { 51 case DataType::UINT32: 52 case DataType::INT32: 53 case DataType::UINT64: 54 case DataType::INT64: 55 case DataType::FLOAT32: 56 case DataType::FLOAT64: 57 case DataType::ANY: 58 return true; 59 case DataType::REFERENCE: 60 return g_options.IsCompilerMemoryCoalescingObjects(); 61 default: 62 return false; 63 } 64 } 65 66 NO_MOVE_SEMANTIC(MemoryCoalescing); 67 NO_COPY_SEMANTIC(MemoryCoalescing); 68 ~MemoryCoalescing() override = default; 69 70 static void RemoveAddI(Inst *inst); 71 72 private: 73 void ReplacePairs(ArenaVector<CoalescedPair> const &pairs); 74 void ReplacePair(Inst *first, Inst *second, Inst *insertAfter); 75 76 Inst *ReplaceLoadArray(Inst *first, Inst *second, Inst *insertAfter); 77 Inst *ReplaceLoadArrayI(Inst *first, Inst *second, Inst *insertAfter); 78 Inst *ReplaceLoadObject(Inst *first, Inst *second, Inst *insertAfter); 79 Inst *ReplaceStoreArray(Inst *first, Inst *second, Inst *insertAfter); 80 Inst *ReplaceStoreArrayI(Inst *first, Inst *second, Inst *insertAfter); 81 Inst *ReplaceStoreObject(Inst *first, Inst *second, Inst *insertAfter); 82 83 void CheckForObjectCandidates(Inst *inst, Inst *obj, uint8_t fieldSize, size_t fieldOffset); 84 85 private: 86 bool alignedOnly_; 87 }; 88 } // namespace ark::compiler 89 90 #endif // COMPILER_OPTIMIZER_OPTIMIZATIONS_MEMORY_COALESCING_H 91