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 MAPLEBE_INCLUDE_CG_STACKMAP_H 17 #define MAPLEBE_INCLUDE_CG_STACKMAP_H 18 /* Maple IR header */ 19 #include "operand.h" 20 #include "mempool.h" 21 #include "mempool_allocator.h" 22 23 namespace maplebe { 24 using namespace maple; 25 enum VregLocationKind { 26 kUnknownDeoptKind, 27 kOnStack, 28 kInRegister, 29 kInConstValue, 30 }; 31 32 struct LocationInfo { 33 VregLocationKind locationKind; 34 int64 value; // physical registerNO, stack frame offset, or immediate value 35 }; 36 37 struct DeoptVregLocationInfo { 38 int32 deoptVreg; 39 LocationInfo locationInfo; 40 DeoptVregLocationInfoDeoptVregLocationInfo41 DeoptVregLocationInfo(int32 vreg, LocationInfo info) : deoptVreg(vreg), locationInfo(info) {} 42 }; 43 44 class DeoptInfo { 45 public: DeoptInfo(MapleAllocator & alloc)46 DeoptInfo(MapleAllocator &alloc) : deoptVreg2Opnd(alloc.Adapter()), deoptVreg2LocationInfo(alloc.Adapter()) {} 47 ~DeoptInfo() = default; 48 AddDeoptBundleInfo(int32 deoptVreg,Operand & opnd)49 void AddDeoptBundleInfo(int32 deoptVreg, Operand &opnd) 50 { 51 deoptVreg2Opnd.insert(std::pair<int32, Operand *>(deoptVreg, &opnd)); 52 } 53 ReplaceDeoptBundleInfo(int32 deoptVreg,Operand & opnd)54 void ReplaceDeoptBundleInfo(int32 deoptVreg, Operand &opnd) 55 { 56 deoptVreg2Opnd.insert_or_assign(deoptVreg, &opnd); 57 } 58 GetDeoptBundleInfo()59 const MapleUnorderedMap<int32, Operand *> &GetDeoptBundleInfo() const 60 { 61 return deoptVreg2Opnd; 62 } 63 RecordDeoptVreg2LocationInfo(int32 deoptVregNO,LocationInfo locationInfo)64 void RecordDeoptVreg2LocationInfo(int32 deoptVregNO, LocationInfo locationInfo) 65 { 66 deoptVreg2LocationInfo.emplace_back(deoptVregNO, locationInfo); 67 } 68 GetDeoptVreg2LocationInfo()69 const MapleVector<DeoptVregLocationInfo> &GetDeoptVreg2LocationInfo() const 70 { 71 return deoptVreg2LocationInfo; 72 } 73 Dump()74 void Dump() const 75 { 76 for (const auto &elem : deoptVreg2LocationInfo) { 77 LogInfo::MapleLogger() << " deoptVreg: " << elem.deoptVreg; 78 if (elem.locationInfo.locationKind == kInRegister) { 79 LogInfo::MapleLogger() << " in register: " << elem.locationInfo.value << "\n"; 80 } else if (elem.locationInfo.locationKind == kOnStack) { 81 LogInfo::MapleLogger() << " on stack: " << elem.locationInfo.value << "\n"; 82 } else { 83 LogInfo::MapleLogger() << " in const value: " << elem.locationInfo.value << "\n"; 84 } 85 } 86 } 87 SerializeInfo()88 std::vector<uint64> SerializeInfo() const 89 { 90 std::vector<uint64> info; 91 for (const auto &elem : deoptVreg2LocationInfo) { 92 info.push_back(static_cast<uint64_t>(elem.deoptVreg)); 93 info.push_back(static_cast<uint64_t>(elem.locationInfo.locationKind)); 94 info.push_back(static_cast<uint64_t>(elem.locationInfo.value)); 95 } 96 return info; 97 } 98 99 private: 100 // info before RA 101 MapleUnorderedMap<int32, Operand *> deoptVreg2Opnd; 102 MapleVector<DeoptVregLocationInfo> deoptVreg2LocationInfo; 103 }; 104 105 class ReferenceMap { 106 public: ReferenceMap(MapleAllocator & alloc)107 ReferenceMap(MapleAllocator &alloc) : referenceLocations(alloc.Adapter()) {} 108 ~ReferenceMap() = default; ReocordRegisterRoots(uint32 regNO)109 void ReocordRegisterRoots(uint32 regNO) 110 { 111 referenceLocations.push_back({kInRegister, regNO}); 112 } 113 ReocordStackRoots(int64 offset)114 void ReocordStackRoots(int64 offset) 115 { 116 referenceLocations.push_back({kOnStack, offset}); 117 } 118 GetReferenceLocations()119 const MapleVector<LocationInfo> &GetReferenceLocations() const 120 { 121 return referenceLocations; 122 } 123 Dump()124 void Dump() const 125 { 126 for (const auto &elem : referenceLocations) { 127 if (elem.locationKind == kInRegister) { 128 LogInfo::MapleLogger() << " in register: " << elem.value << "\n"; 129 } else { 130 LogInfo::MapleLogger() << " on stack: " << elem.value << "\n"; 131 } 132 } 133 } 134 SerializeInfo()135 std::vector<uint64> SerializeInfo() const 136 { 137 std::vector<uint64> info; 138 for (const auto &elem : referenceLocations) { 139 info.push_back(static_cast<uint64_t>(elem.locationKind)); 140 info.push_back(static_cast<uint64_t>(elem.value)); 141 } 142 return info; 143 } 144 145 private: 146 MapleVector<LocationInfo> referenceLocations; 147 }; 148 149 class StackMap { 150 public: StackMap(MapleAllocator & alloc)151 StackMap(MapleAllocator &alloc) : deoptInfo(alloc), referenceMap(alloc) {} 152 ~StackMap() = default; 153 GetDeoptInfo()154 DeoptInfo &GetDeoptInfo() 155 { 156 return deoptInfo; 157 } 158 GetDeoptInfo()159 const DeoptInfo &GetDeoptInfo() const 160 { 161 return deoptInfo; 162 } 163 GetReferenceMap()164 ReferenceMap &GetReferenceMap() 165 { 166 return referenceMap; 167 } 168 GetReferenceMap()169 const ReferenceMap &GetReferenceMap() const 170 { 171 return referenceMap; 172 } 173 174 private: 175 DeoptInfo deoptInfo; 176 ReferenceMap referenceMap; 177 }; 178 } /* namespace maplebe */ 179 180 #endif /* MAPLEBE_INCLUDE_CG_STACKMAP_H */ 181