Changeset 5828 for icGREP/icgrepdevel/icgrep/pablo/expression_map.hpp
 Timestamp:
 Jan 13, 2018, 11:57:43 AM (13 months ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

icGREP/icgrepdevel/icgrep/pablo/expression_map.hpp
r5710 r5828 15 15 friend struct ExpressionTable; 16 16 17 explicit FixedArgMap(Type * predecessor = nullptr) : mPredecessor(predecessor) { } 17 explicit FixedArgMap(Type * predecessor = nullptr) noexcept 18 : mPredecessor(predecessor) { 19 20 } 18 21 19 22 explicit FixedArgMap(Type && other) noexcept … … 23 26 } 24 27 25 FixedArgMap & operator=(Type && other) {28 FixedArgMap & operator=(Type && other) noexcept { 26 29 mPredecessor = other.mPredecessor; 27 30 mMap = std::move(other.mMap); … … 30 33 31 34 template <class Functor, typename... Params> 32 PabloAST * findOrCall(Functor && functor, const PabloAST::ClassTypeId type , Args... args, Params... params){33 Key key = std::make_tuple(type , args...);35 PabloAST * findOrCall(Functor && functor, const PabloAST::ClassTypeId typeId, Args... args, Params... params) noexcept { 36 Key key = std::make_tuple(typeId, args...); 34 37 PabloAST * const f = find(key); 35 38 if (f) { … … 41 44 } 42 45 43 std::pair<PabloAST *, bool> findOrAdd(PabloAST * object, const PabloAST::ClassTypeId type , Args... args){44 Key key = std::make_tuple(type , args...);46 std::pair<PabloAST *, bool> findOrAdd(PabloAST * object, const PabloAST::ClassTypeId typeId, Args... args) noexcept { 47 Key key = std::make_tuple(typeId, args...); 45 48 PabloAST * const entry = find(key); 46 49 if (entry) { … … 51 54 } 52 55 53 bool erase(const PabloAST::ClassTypeId type, Args... args) {56 bool erase(const PabloAST::ClassTypeId type, Args... args) noexcept { 54 57 Key key = std::make_tuple(type, args...); 55 58 for (Type * obj = this; obj; obj = obj>mPredecessor) { … … 63 66 } 64 67 65 inline PabloAST * find(const PabloAST::ClassTypeId type, Args... args) const {68 inline PabloAST * find(const PabloAST::ClassTypeId type, Args... args) const noexcept { 66 69 return find(std::make_tuple(type, args...)); 67 70 } … … 93 96 }; 94 97 95 96 struct VarArgMap {97 98 friend struct ExpressionTable;99 100 using Allocator = SlabAllocator<const PabloAST *>;101 102 struct Key {103 104 inline Key(PabloAST::ClassTypeId type, const PabloAST * arg1, const std::vector<PabloAST *> & args, Allocator & allocator)105 : mType(type)106 , mArgs(1 + args.size())107 , mArg(allocator.allocate(mArgs)) {108 unsigned i = 1;109 mArg[0] = arg1;110 for (PabloAST * arg : args) {111 mArg[i++] = arg;112 }113 }114 115 inline Key(PabloAST::ClassTypeId type, const Variadic * stmt, Allocator & allocator)116 : mType(type)117 , mArgs(stmt>getNumOperands())118 , mArg(allocator.allocate(mArgs)) {119 unsigned i = 0;120 for (PabloAST * arg : *stmt) {121 mArg[i++] = arg;122 }123 }124 125 inline Key(const Key & key) = default;126 127 inline Key(Key && key) = default;128 129 inline bool operator < (const Key & other) const {130 if (mType != other.mType) {131 return mType < other.mType;132 } else if (mArgs != other.mArgs) {133 return mArgs < other.mArgs;134 }135 for (unsigned i = 0; i != mArgs; ++i) {136 if (mArg[i] != other.mArg[i]) {137 return mArg[i] < other.mArg[i];138 }139 }140 return false;141 }142 143 const PabloAST::ClassTypeId mType;144 const unsigned mArgs;145 const PabloAST ** mArg;146 };147 148 using Map = std::map<Key, PabloAST *>;149 150 explicit VarArgMap(VarArgMap * predecessor = nullptr)151 : mPredecessor(predecessor) {152 153 }154 155 explicit VarArgMap(VarArgMap && other) noexcept156 : mPredecessor(other.mPredecessor)157 , mMap(std::move(other.mMap)) {158 159 }160 161 VarArgMap & operator=(VarArgMap && other) {162 mPredecessor = other.mPredecessor;163 mMap = std::move(other.mMap);164 return *this;165 }166 167 template <class Functor, typename... Params>168 PabloAST * findOrCall(Functor && functor, const PabloAST::ClassTypeId type, const PabloAST * arg1, const std::vector<PabloAST *> & args, Params... params) {169 Key key(type, arg1, args, mAllocator);170 PabloAST * const f = find(key);171 if (f) {172 mAllocator.deallocate(key.mArg);173 return f;174 }175 PabloAST * const object = functor(args, std::forward<Params>(params)...);176 mMap.insert(std::make_pair(std::move(key), object));177 return object;178 }179 180 std::pair<PabloAST *, bool> findOrAdd(Variadic * object, const PabloAST::ClassTypeId type) {181 Key key(type, object, mAllocator);182 PabloAST * const entry = find(key);183 if (entry) {184 mAllocator.deallocate(key.mArg);185 return std::make_pair(entry, false);186 }187 mMap.insert(std::make_pair(std::move(key), object));188 return std::make_pair(object, true);189 }190 191 private:192 193 PabloAST * find(const Key & key) const {194 // check this map to see if we have it195 auto itr = mMap.find(key);196 if (itr != mMap.end()) {197 return itr>second;198 } else { // check any previous maps to see if it exists199 auto * pred = mPredecessor;200 while (pred) {201 itr = pred>mMap.find(key);202 if (itr == pred>mMap.end()) {203 pred = pred>mPredecessor;204 continue;205 }206 return itr>second;207 }208 }209 return nullptr;210 }211 212 private:213 VarArgMap * mPredecessor;214 Map mMap;215 Allocator mAllocator;216 };217 218 98 struct ExpressionTable { 219 99 … … 223 103 mBinary.mPredecessor = &(predecessor>mBinary); 224 104 mTernary.mPredecessor = &(predecessor>mTernary); 225 mVariadic.mPredecessor = &(predecessor>mVariadic);226 105 } 227 106 } … … 232 111 : mUnary(std::move(other.mUnary)) 233 112 , mBinary(std::move(other.mBinary)) 234 , mTernary(std::move(other.mTernary)) 235 , mVariadic(std::move(other.mVariadic)) { 113 , mTernary(std::move(other.mTernary)) { 236 114 237 115 } 238 116 239 ExpressionTable & operator=(ExpressionTable && other) {117 ExpressionTable & operator=(ExpressionTable && other) noexcept { 240 118 mUnary = std::move(other.mUnary); 241 119 mBinary = std::move(other.mBinary); 242 120 mTernary = std::move(other.mTernary); 243 mVariadic = std::move(other.mVariadic);244 121 return *this; 245 122 } 246 123 247 124 template <class Functor, typename... Params> 248 inline PabloAST * findUnaryOrCall(Functor && functor, const PabloAST::ClassTypeId type , PabloAST * expr, Params... params){249 return mUnary.findOrCall(std::move(functor), type , expr, std::forward<Params>(params)...);125 inline PabloAST * findUnaryOrCall(Functor && functor, const PabloAST::ClassTypeId typeId, void * expr, Params... params) noexcept { 126 return mUnary.findOrCall(std::move(functor), typeId, expr, std::forward<Params>(params)...); 250 127 } 251 128 252 129 template <class Functor, typename... Params> 253 inline PabloAST * findBinaryOrCall(Functor && functor, const PabloAST::ClassTypeId type , PabloAST * expr1, PabloAST * expr2, Params... params){254 return mBinary.findOrCall(std::move(functor), type , expr1, expr2, std::forward<Params>(params)...);130 inline PabloAST * findBinaryOrCall(Functor && functor, const PabloAST::ClassTypeId typeId, void * expr1, void * expr2, Params... params) noexcept { 131 return mBinary.findOrCall(std::move(functor), typeId, expr1, expr2, std::forward<Params>(params)...); 255 132 } 256 133 257 134 template <class Functor, typename... Params> 258 inline PabloAST * findTernaryOrCall(Functor && functor, const PabloAST::ClassTypeId type , PabloAST * expr1, PabloAST * expr2, PabloAST * expr3, Params... params){259 return mTernary.findOrCall(std::move(functor), type , expr1, expr2, expr3, std::forward<Params>(params)...);135 inline PabloAST * findTernaryOrCall(Functor && functor, const PabloAST::ClassTypeId typeId, void * expr1, void * expr2, void * expr3, Params... params) noexcept { 136 return mTernary.findOrCall(std::move(functor), typeId, expr1, expr2, expr3, std::forward<Params>(params)...); 260 137 } 261 138 262 template <class Functor, typename... Params> 263 inline PabloAST * findVariadicOrCall(Functor && functor, const PabloAST::ClassTypeId type, const PabloAST * arg1, const std::vector<PabloAST *> & args, Params... params) { 264 return mVariadic.findOrCall(std::move(functor), type, arg1, args, std::forward<Params>(params)...); 265 } 266 267 std::pair<PabloAST *, bool> findOrAdd(Statement * stmt) { 139 std::pair<PabloAST *, bool> findOrAdd(Statement * stmt) noexcept { 268 140 switch (stmt>getClassTypeId()) { 269 141 case PabloAST::ClassTypeId::Var: 270 142 case PabloAST::ClassTypeId::Not: 271 case PabloAST::ClassTypeId::Count: 143 case PabloAST::ClassTypeId::Count: 272 144 return mUnary.findOrAdd(stmt, stmt>getClassTypeId(), stmt>getOperand(0)); 273 145 case PabloAST::ClassTypeId::And: 274 146 case PabloAST::ClassTypeId::Or: 275 147 case PabloAST::ClassTypeId::Xor: 276 return mVariadic.findOrAdd(llvm::cast<Variadic>(stmt), stmt>getClassTypeId());277 148 case PabloAST::ClassTypeId::Advance: 278 149 case PabloAST::ClassTypeId::ScanThru: … … 280 151 case PabloAST::ClassTypeId::Assign: 281 152 case PabloAST::ClassTypeId::Extract: 282 return mBinary.findOrAdd(stmt, stmt>getClassTypeId(), stmt>getOperand(0), stmt>getOperand(1)); 153 return mBinary.findOrAdd(stmt, stmt>getClassTypeId(), stmt>getOperand(0), stmt>getOperand(1)); 154 case PabloAST::ClassTypeId::Fill: 155 return mBinary.findOrAdd(stmt, stmt>getClassTypeId(), stmt>getOperand(0), stmt>getType()); 283 156 case PabloAST::ClassTypeId::Sel: 284 157 case PabloAST::ClassTypeId::IndexedAdvance: … … 289 162 } 290 163 291 292 164 private: 293 FixedArgMap<PabloAST *> mUnary; 294 FixedArgMap<PabloAST *, PabloAST *> mBinary; 295 FixedArgMap<PabloAST *, PabloAST *, PabloAST *> mTernary; 296 VarArgMap mVariadic; 165 FixedArgMap<void *> mUnary; 166 FixedArgMap<void *, void *> mBinary; 167 FixedArgMap<void *, void *, void *> mTernary; 297 168 }; 298 169
Note: See TracChangeset
for help on using the changeset viewer.