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_MEM_REFERENCE_TABLE_H 17 #define MAPLE_IR_INCLUDE_MEM_REFERENCE_TABLE_H 18 19 #include "mempool_allocator.h" 20 #include "mir_module.h" 21 #include "orig_symbol.h" 22 23 namespace maple { 24 25 using MemDefUseSet = MapleUnorderedSet<OStIdx>; 26 27 class MemDefUse { 28 public: MemDefUse(MapleAllocator & allocator)29 explicit MemDefUse(MapleAllocator &allocator) : defSet(allocator.Adapter()), useSet(allocator.Adapter()) {} 30 ~MemDefUse(); 31 GetDefSet()32 const MemDefUseSet &GetDefSet() const 33 { 34 return defSet; 35 } 36 GetDefSet()37 MemDefUseSet &GetDefSet() 38 { 39 return defSet; 40 } 41 GetUseSet()42 const MemDefUseSet &GetUseSet() const 43 { 44 return useSet; 45 } 46 GetUseSet()47 MemDefUseSet &GetUseSet() 48 { 49 return useSet; 50 } 51 SetIndependent()52 void SetIndependent() 53 { 54 isIndependent = true; 55 } 56 IsIndependent()57 bool IsIndependent() const 58 { 59 return isIndependent; 60 } 61 MergeOthers(MemDefUse & rhs)62 void MergeOthers(MemDefUse &rhs) 63 { 64 if (this == &rhs) { 65 return; 66 } 67 defSet.insert(rhs.defSet.begin(), rhs.defSet.end()); 68 useSet.insert(rhs.useSet.begin(), rhs.useSet.end()); 69 isIndependent = rhs.isIndependent || isIndependent; 70 } 71 72 private: 73 MemDefUseSet defSet; 74 MemDefUseSet useSet; 75 // The field is set TRUE to indicate that the callee-save and callee-reload instructions do not 76 // conflict with any other memory access instructions. 77 bool isIndependent = false; 78 }; 79 80 using MemDefUsePart = MapleUnorderedMap<BaseNode *, MemDefUse *>; 81 using OstTable = MapleVector<OriginalSt>; 82 class MemReferenceTable { 83 public: MemReferenceTable(MapleAllocator & allocator,MIRFunction & func)84 MemReferenceTable(MapleAllocator &allocator, MIRFunction &func) 85 : allocator(allocator), func(func), ostTable(allocator.Adapter()), memDefUsePart(allocator.Adapter()) 86 { 87 } ~MemReferenceTable()88 ~MemReferenceTable() {} 89 GetFunction()90 MIRFunction &GetFunction() 91 { 92 return func; 93 } 94 GetMemDefUsePart()95 MemDefUsePart &GetMemDefUsePart() 96 { 97 return memDefUsePart; 98 } 99 GetOstTable()100 OstTable &GetOstTable() 101 { 102 return ostTable; 103 } 104 GetOrCreateMemDefUseFromBaseNode(BaseNode * node)105 MemDefUse *GetOrCreateMemDefUseFromBaseNode(BaseNode *node) 106 { 107 auto iter = memDefUsePart.find(node); 108 if (iter != memDefUsePart.end()) { 109 return iter->second; 110 } 111 auto *newDefUse = allocator.New<MemDefUse>(allocator); 112 memDefUsePart[node] = newDefUse; 113 return newDefUse; 114 } 115 116 private: 117 MapleAllocator &allocator; 118 MIRFunction &func; 119 OstTable ostTable; 120 MemDefUsePart memDefUsePart; 121 }; 122 } // namespace maple 123 #endif // MAPLE_IR_INCLUDE_MEM_REFERENCE_TABLE_H 124