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_SPARSE_DATAINFO_H 17 #define MAPLEBE_INCLUDE_CG_SPARSE_DATAINFO_H 18 #include "maple_string.h" 19 #include "common_utils.h" 20 #include "mempool.h" 21 #include "mempool_allocator.h" 22 #include "maple_sparse_bitvector.h" 23 24 namespace maplebe { 25 class SparseDataInfo { 26 /* 27 * SparseDataInfo has the same imterface like DataInfo 28 * it can be used when the data member is small while the data 29 * range is big.like in live analysis, in some extreme case the 30 * vreg num range is 10k while in each bb, the max num of is 30+ 31 */ 32 public: SparseDataInfo(uint32 bitNum,const MapleAllocator & alloc)33 SparseDataInfo(uint32 bitNum, const MapleAllocator &alloc) : allocator(alloc), info(allocator), maxRegNum(bitNum) {} 34 SparseDataInfo(const SparseDataInfo & other,const MapleAllocator & alloc)35 SparseDataInfo(const SparseDataInfo &other, const MapleAllocator &alloc) 36 : allocator(alloc), 37 info(other.info, allocator), 38 maxRegNum(other.maxRegNum) 39 { 40 } 41 SparseDataInfo(const SparseDataInfo & other)42 SparseDataInfo(const SparseDataInfo &other) 43 : allocator(other.allocator), 44 info(other.info, allocator), 45 maxRegNum(other.maxRegNum) 46 { 47 } 48 Clone(MapleAllocator & alloc)49 SparseDataInfo &Clone(MapleAllocator &alloc) const 50 { 51 auto *dataInfo = alloc.New<SparseDataInfo>(*this, alloc); 52 return *dataInfo; 53 } 54 55 ~SparseDataInfo() = default; 56 57 SparseDataInfo &operator=(const SparseDataInfo &other) 58 { 59 if (this == &other) { 60 return *this; 61 } 62 allocator = other.GetAllocator(); 63 info = other.GetInfo(); 64 maxRegNum = other.GetMaxRegNum(); 65 return *this; 66 } 67 SetBit(uint32 bitNO)68 void SetBit(uint32 bitNO) 69 { 70 info.Set(bitNO); 71 } 72 ResetBit(uint32 bitNO)73 void ResetBit(uint32 bitNO) 74 { 75 info.Reset(bitNO); 76 } 77 TestBit(uint32 bitNO)78 bool TestBit(uint32 bitNO) const 79 { 80 return info.Test(bitNO); 81 } 82 NoneBit()83 bool NoneBit() const 84 { 85 return info.Empty(); 86 } 87 Size()88 size_t Size() const 89 { 90 return maxRegNum; 91 } 92 GetAllocator()93 const MapleAllocator &GetAllocator() const 94 { 95 return allocator; 96 } 97 GetInfo()98 const MapleSparseBitVector<> &GetInfo() const 99 { 100 return info; 101 } 102 GetMaxRegNum()103 uint32 GetMaxRegNum() const 104 { 105 return maxRegNum; 106 } 107 IsEqual(const SparseDataInfo & secondInfo)108 bool IsEqual(const SparseDataInfo &secondInfo) const 109 { 110 return info == secondInfo.GetInfo(); 111 } 112 IsEqual(const MapleSparseBitVector<> & liveInfoBak)113 bool IsEqual(const MapleSparseBitVector<> &liveInfoBak) const 114 { 115 return info == liveInfoBak; 116 } 117 AndBits(const SparseDataInfo & secondInfo)118 void AndBits(const SparseDataInfo &secondInfo) 119 { 120 info &= secondInfo.info; 121 } 122 OrBits(const SparseDataInfo & secondInfo)123 bool OrBits(const SparseDataInfo &secondInfo) 124 { 125 return info |= secondInfo.info; 126 } 127 128 /* if bit in secondElem is 1, bit in current DataInfo is set 0 */ Difference(const SparseDataInfo & secondInfo)129 bool Difference(const SparseDataInfo &secondInfo) 130 { 131 return info.Diff(secondInfo.info); 132 } 133 ResetAllBit()134 void ResetAllBit() 135 { 136 info.Clear(); 137 } 138 EnlargeCapacityToAdaptSize(uint32)139 void EnlargeCapacityToAdaptSize(uint32 /* bitNO */) const 140 { 141 /* add one more size for each enlarge action */ 142 } 143 ClearDataInfo()144 void ClearDataInfo() 145 { 146 info.Clear(); 147 } 148 149 template<typename T> GetBitsOfInfo(T & wordRes)150 void GetBitsOfInfo(T &wordRes) const 151 { 152 info.ConvertToSet(wordRes); 153 } 154 155 private: 156 MapleAllocator allocator; 157 MapleSparseBitVector<> info; 158 uint32 maxRegNum; 159 }; 160 } /* namespace maplebe */ 161 #endif /* MAPLEBE_INCLUDE_CG_SPARSE_DATAINFO_H */ 162